Skip to content

Commit

Permalink
[feature] Add d2::Polygon::intersects
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-bremond committed Mar 2, 2019
1 parent 9d12da4 commit b12e0b5
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 24 deletions.
9 changes: 2 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ OPTION (BUILD_UNIT_TESTS "Build tests" ON)
OPTION (BUILD_PYTHON_BINDINGS "Build Python bindings." ON)
OPTION (BUILD_CODE_COVERAGE "Build code coverage" OFF)
OPTION (BUILD_DOCUMENTATION "Build documentation" OFF)
OPTION (INSTALL_DATA "Install data" ON)

################################################################################################################################################################

Expand Down Expand Up @@ -454,13 +453,9 @@ ADD_CUSTOM_TARGET ("uninstall" COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINA

INSTALL (FILES "LICENSE" DESTINATION ${INSTALL_ROOT} COMPONENT "documentation")

IF (INSTALL_DATA)
IF (EXISTS "${PROJECT_SOURCE_DIR}/share")

IF (EXISTS "${PROJECT_SOURCE_DIR}/share")

INSTALL (DIRECTORY "${PROJECT_SOURCE_DIR}/share" DESTINATION ${INSTALL_DATA} COMPONENT "data")

ENDIF ()
INSTALL (DIRECTORY "${PROJECT_SOURCE_DIR}/share/" DESTINATION ${INSTALL_DATA} COMPONENT "data")

ENDIF ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ inline void LibraryMathematicsPy_Geometry_2D_Objects_Polygon
.def(self_ns::repr(self_ns::self))

.def("isDefined", &Polygon::isDefined)
.def("intersectsPolygon", +[] (const Polygon& aPolygon, const Polygon& anotherPolygon) -> bool { return aPolygon.intersects(anotherPolygon) ; })
.def("containsPoint", +[] (const Polygon& aPolygon, const Point& aPoint) -> bool { return aPolygon.contains(aPoint) ; })
.def("containsPointSet", +[] (const Polygon& aPolygon, const PointSet& aPointSet) -> bool { return aPolygon.contains(aPointSet) ; })

Expand All @@ -50,6 +51,7 @@ inline void LibraryMathematicsPy_Geometry_2D_Objects_Polygon
.def("getEdges", &Polygon::getEdges)
.def("getVertices", &Polygon::getVertices)
.def("toString", &Polygon::toString, LibraryMathematicsPy_Geometry_2D_Objects_Polygon_toString_overloads())
// .def("unionWith", &Polygon::unionWith)
.def("applyTransformation", &Polygon::applyTransformation)

.def("Undefined", &Polygon::Undefined).staticmethod("Undefined")
Expand Down
15 changes: 14 additions & 1 deletion include/Library/Mathematics/Geometry/2D/Objects/Polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ class Polygon : public Object

virtual bool isDefined ( ) const override ;

/// @brief Check if polygon intersects polygon
///
/// @code
/// Polygon polygon = ... ;
/// Polygon anotherPolygon = ... ;
/// polygon.intersects(anotherPolygon) ;
/// @endcode
///
/// @param [in] aPolygon A polygon
/// @return True if polygon intersects polygon

bool intersects ( const Polygon& aPolygon ) const ;

/// @brief Check if polygon contains point
///
/// @code
Expand Down Expand Up @@ -222,7 +235,7 @@ class Polygon : public Object
/// @param [in] aPolygon A polygon
/// @return A multi-polygon

MultiPolygon unionWith ( const Polygon& aPolygon ) const ;
// MultiPolygon unionWith ( const Polygon& aPolygon ) const ;

/// @brief Get string representation
///
Expand Down
71 changes: 69 additions & 2 deletions src/Library/Mathematics/Geometry/2D/Objects/Polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Polygon::Impl

bool isDefined ( ) const ;

bool intersects ( const Polygon& aPolygon ) const ;

bool contains ( const Point& aPoint ) const ;

bool contains ( const PointSet& aPointSet ) const ;
Expand Down Expand Up @@ -92,6 +94,10 @@ class Polygon::Impl

Array<Polygon::Vertex> getVertices ( ) const ;

// Intersection intersectionWith ( const Polygon& aPolygon ) const ;

// MultiPolygon unionWith ( const Polygon& aPolygon ) const ;

String toString ( const Object::Format& aFormat,
const Integer& aPrecision ) const ;

Expand Down Expand Up @@ -149,6 +155,22 @@ bool Polygon::Impl::isDefined ( )
return polygon_.outer().size() >= 3 ;
}

bool Polygon::Impl::intersects ( const Polygon& aPolygon ) const
{

try
{
return boost::geometry::intersects(polygon_, aPolygon.implUPtr_->polygon_) ;
}
catch (const std::exception& anException)
{
throw library::core::error::RuntimeError("Error when checking if polygon intersects polygon: [{}]", anException.what()) ;
}

return false ;

}

bool Polygon::Impl::contains ( const Point& aPoint ) const
{

Expand All @@ -168,9 +190,17 @@ bool Polygon::Impl::contains (
bool Polygon::Impl::contains ( const PointSet& aPointSet ) const
{

throw library::core::error::runtime::ToBeImplemented("Polygon::contains (PointSet&)") ;
for (const auto& point : aPointSet)
{

return false ;
if (!this->contains(point))
{
return false ;
}

}

return true ;

}

Expand Down Expand Up @@ -490,6 +520,23 @@ bool Polygon::isDefined ( )
return (implUPtr_ != nullptr) && implUPtr_->isDefined() ;
}

bool Polygon::intersects ( const Polygon& aPolygon ) const
{

if (!aPolygon.isDefined())
{
throw library::core::error::runtime::Undefined("Point") ;
}

if (!this->isDefined())
{
throw library::core::error::runtime::Undefined("Polygon") ;
}

return implUPtr_->intersects(aPolygon) ;

}

bool Polygon::contains ( const Point& aPoint ) const
{

Expand Down Expand Up @@ -694,6 +741,26 @@ String Polygon::toString (

}

// Intersection Polygon::intersectionWith ( const Polygon& aPolygon ) const
// {

// if (!this->isDefined())
// {
// throw library::core::error::runtime::Undefined("Polygon") ;
// }

// }

// MultiPolygon Polygon::unionWith ( const Polygon& aPolygon ) const
// {

// if (!this->isDefined())
// {
// throw library::core::error::runtime::Undefined("Polygon") ;
// }

// }

void Polygon::applyTransformation ( const Transformation& aTransformation )
{

Expand Down
117 changes: 117 additions & 0 deletions test/Library/Mathematics/Geometry/2D/Objects/Polygon.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <Library/Mathematics/Geometry/2D/Transformation.hpp>
#include <Library/Mathematics/Geometry/2D/Objects/Polygon.hpp>

#include <Library/Core/Containers/Tuple.hpp>

#include <Global.test.hpp>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -370,6 +372,95 @@ TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, IsDefined)

}

TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, IntersectsPolygon)
{

using library::core::ctnr::Tuple ;
using library::core::ctnr::Array ;

using library::math::geom::d2::objects::Polygon ;

{

const Array<Tuple<Polygon, Polygon, bool>> testCases =
{
{ Polygon { { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 0.0 } } }, Polygon { { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 0.0 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, false },
{ Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, Polygon { { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } }, true },
{ Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, true },
{ Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, false },
{ Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, false },
{ Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, Polygon { { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } }, false },
{ Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, false },
{ Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, Polygon { { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } }, Polygon { { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 } } }, true },
{ Polygon { { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } }, Polygon { { { 5.0, 0.0 }, { 5.0, 0.0 }, { 6.0, 6.0 }, { 0.0, 5.0 } } }, false },
{ Polygon { { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } } }, Polygon { { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 0.5 }, { 0.0, 0.5 } } }, true },
} ;

for (const auto& testCase : testCases)
{

const Polygon& firstPolygon = std::get<0>(testCase) ;
const Polygon& secondPolygon = std::get<1>(testCase) ;
const bool result = std::get<2>(testCase) ;

EXPECT_EQ(result, firstPolygon.intersects(secondPolygon)) ;

}

}

{

const Array<Polygon::Vertex> vertices =
{
{ 0.0, 0.0 },
{ 0.0, 1.0 },
{ 1.0, 1.0 },
{ 1.0, 0.0 }
} ;

const Polygon polygon = { vertices } ;

EXPECT_ANY_THROW(Polygon::Undefined().intersects(Polygon::Undefined())) ;
EXPECT_ANY_THROW(polygon.intersects(Polygon::Undefined())) ;
EXPECT_ANY_THROW(Polygon::Undefined().intersects(polygon)) ;

}

}

// TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, ContainsPoint)
// {

// using library::math::geom::d2::objects::Polygon ;

// {

// FAIL() ;

// }

// }

// TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, ContainsPointSet)
// {

// using library::math::geom::d2::objects::Polygon ;

// {

// FAIL() ;

// }

// }

TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, GetInnerRingCount)
{

Expand Down Expand Up @@ -891,6 +982,32 @@ TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, GetVertices)

}

// TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, IntersectionWith)
// {

// using library::math::geom::d2::objects::Polygon ;

// {

// FAIL() ;

// }

// }

// TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, UnionWith)
// {

// using library::math::geom::d2::objects::Polygon ;

// {

// FAIL() ;

// }

// }

TEST (Library_Mathematics_Geometry_2D_Objects_Polygon, ToString)
{

Expand Down
15 changes: 2 additions & 13 deletions tools/python/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,15 @@ LABEL maintainer="[email protected]"

USER root

RUN ln -s /opt/conda/include/python3.6m /opt/conda/include/python3.6

RUN cd /tmp \
&& wget --quiet http://sourceforge.net/projects/boost/files/boost/1.68.0/boost_1_68_0.tar.gz \
&& tar -xf boost_1_68_0.tar.gz \
&& cd boost_1_68_0 \
&& ./bootstrap.sh --with-python=/opt/conda/bin/python3 --with-python-version=3.6 --with-python-root=/opt/conda/lib/python3.6 \
&& echo "using python : 3.6 : /usr : /opt/conda/include/python3.6m ;" >> project-config.jam \
&& ./b2 --with-python -j 8 install \
&& rm -rf /tmp/boost_1_68_0

RUN jupyter labextension install @jupyterlab/plotly-extension

RUN python -m pip install --quiet numpy --upgrade \
&& python -m pip install --quiet plotly

COPY ./shortcuts-extension /home/jovyan/.jupyter/lab/user-settings/@jupyterlab/shortcuts-extension

RUN chown -R $NB_UID:$NB_UID /home/jovyan/.jupyter
RUN chown -R ${NB_UID}:${NB_UID} /home/jovyan/.jupyter

USER $NB_UID
USER ${NB_UID}

################################################################################################################################################################
2 changes: 1 addition & 1 deletion tools/python/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ else
--volume="${project_directory}/bindings/python/docs:/home/jovyan/docs" \
--volume="${project_directory}/tutorials/python/notebooks:/home/jovyan/tutorials" \
--workdir="/home/jovyan" \
"jupyter/scipy-notebook:latest" \
"${image_name}-python:${image_version}" \
start-notebook.sh --NotebookApp.token=''

fi
Expand Down

0 comments on commit b12e0b5

Please sign in to comment.