From 0fd968b337be532655f7c9ec52210c795750e15f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Br=C3=A9mond?= Date: Mon, 10 Sep 2018 17:27:26 -0700 Subject: [PATCH] [feature] Improve Python bindings --- .../Mathematics/Geometry/3D/Intersection.hpp | 38 +++++++++++++++++++ .../Geometry/3D/Intersection.cpp | 31 +++++++++++++++ .../Geometry/3D/Objects.cpp | 2 + .../Geometry/3D/Objects/Composite.cpp | 20 ++++++++-- 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/include/Library/Mathematics/Geometry/3D/Intersection.hpp b/include/Library/Mathematics/Geometry/3D/Intersection.hpp index f3d81f21..6ac5d017 100755 --- a/include/Library/Mathematics/Geometry/3D/Intersection.hpp +++ b/include/Library/Mathematics/Geometry/3D/Intersection.hpp @@ -172,6 +172,44 @@ class Intersection bool isComplex ( ) const ; + /// @brief Returns true if intersection can be converted to underlying object + /// + /// Only valid if the intersection only contains one object. + /// + /// @return True if intersection can be converted to underlying object + + template + bool is ( ) const + { + + if (!this->isDefined()) + { + throw library::core::error::runtime::Undefined("Intersection") ; + } + + return composite_.is() ; + + } + + /// @brief Access intersection as its underlying object + /// + /// Only valid if the intersection only contains one object. + /// + /// @return Reference to underlying object + + template + const Type& as ( ) const + { + + if (!this->isDefined()) + { + throw library::core::error::runtime::Undefined("Intersection") ; + } + + return composite_.as() ; + + } + /// @brief Access composite object /// /// @return Reference to composite object diff --git a/share/python/src/LibraryMathematicsPy/Geometry/3D/Intersection.cpp b/share/python/src/LibraryMathematicsPy/Geometry/3D/Intersection.cpp index f3d69eb5..31d048ff 100755 --- a/share/python/src/LibraryMathematicsPy/Geometry/3D/Intersection.cpp +++ b/share/python/src/LibraryMathematicsPy/Geometry/3D/Intersection.cpp @@ -30,7 +30,9 @@ inline void LibraryMathematicsPy_Geometry_3D_Intersection ( using library::math::geom::d3::objects::Plane ; using library::math::geom::d3::objects::Sphere ; using library::math::geom::d3::objects::Ellipsoid ; + using library::math::geom::d3::objects::Cuboid ; using library::math::geom::d3::objects::Pyramid ; + using library::math::geom::d3::objects::Composite ; using library::math::geom::d3::Intersection ; // scope in_Intersection = class_("Intersection", init>>()) @@ -39,6 +41,9 @@ inline void LibraryMathematicsPy_Geometry_3D_Intersection ( .def(self == self) .def(self != self) + .def(self + self) + .def(self += self) + .def(self_ns::str(self_ns::self)) .def(self_ns::repr(self_ns::self)) @@ -46,6 +51,32 @@ inline void LibraryMathematicsPy_Geometry_3D_Intersection ( .def("isEmpty", &Intersection::isEmpty) .def("isComplex", &Intersection::isComplex) + .def("isPoint", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isPointSet", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isLine", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isRay", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isSegment", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isLineString", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isPolygon", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isPlane", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isSphere", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isEllipsoid", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isPyramid", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + .def("isComposite", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is() ; }) + + .def("asPoint", +[] (const Intersection& anIntersection) -> Point { return anIntersection.as() ; }) + .def("asPointSet", +[] (const Intersection& anIntersection) -> PointSet { return anIntersection.as() ; }) + .def("asLine", +[] (const Intersection& anIntersection) -> Line { return anIntersection.as() ; }) + .def("asRay", +[] (const Intersection& anIntersection) -> Ray { return anIntersection.as() ; }) + .def("asSegment", +[] (const Intersection& anIntersection) -> Segment { return anIntersection.as() ; }) + .def("asLineString", +[] (const Intersection& anIntersection) -> LineString { return anIntersection.as() ; }) + .def("asPolygon", +[] (const Intersection& anIntersection) -> Polygon { return anIntersection.as() ; }) + .def("asPlane", +[] (const Intersection& anIntersection) -> Plane { return anIntersection.as() ; }) + .def("asSphere", +[] (const Intersection& anIntersection) -> Sphere { return anIntersection.as() ; }) + .def("asEllipsoid", +[] (const Intersection& anIntersection) -> Ellipsoid { return anIntersection.as() ; }) + .def("asPyramid", +[] (const Intersection& anIntersection) -> Pyramid { return anIntersection.as() ; }) + .def("asComposite", +[] (const Intersection& anIntersection) -> Composite { return anIntersection.as() ; }) + .def("accessComposite", &Intersection::accessComposite, return_value_policy()) .def("getType", &Intersection::getType) diff --git a/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects.cpp b/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects.cpp index ddc50f2b..fe8a65e1 100755 --- a/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects.cpp +++ b/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects.cpp @@ -7,6 +7,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#include #include // #include #include @@ -45,6 +46,7 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects ( ) LibraryMathematicsPy_Geometry_3D_Objects_Sphere() ; LibraryMathematicsPy_Geometry_3D_Objects_Ellipsoid() ; LibraryMathematicsPy_Geometry_3D_Objects_Pyramid() ; + LibraryMathematicsPy_Geometry_3D_Objects_Composite() ; } diff --git a/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects/Composite.cpp b/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects/Composite.cpp index 7b47d069..a8acc9ff 100755 --- a/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects/Composite.cpp +++ b/share/python/src/LibraryMathematicsPy/Geometry/3D/Objects/Composite.cpp @@ -29,8 +29,11 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi using library::math::geom::d3::objects::Line ; using library::math::geom::d3::objects::Ray ; using library::math::geom::d3::objects::Segment ; + using library::math::geom::d3::objects::LineString ; + using library::math::geom::d3::objects::Polygon ; using library::math::geom::d3::objects::Plane ; using library::math::geom::d3::objects::Sphere ; + using library::math::geom::d3::objects::Ellipsoid ; using library::math::geom::d3::objects::Cuboid ; using library::math::geom::d3::objects::Pyramid ; using library::math::geom::d3::objects::Composite ; @@ -40,7 +43,10 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi scope in_Composite = class_, bases>("Composite", no_init) .def(self == self) - .def(self != self) + .def(self == self) + + .def(self + self) + .def(self += self) .def(self_ns::str(self_ns::self)) .def(self_ns::repr(self_ns::self)) @@ -51,16 +57,19 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi .def("isPointSet", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isLine", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isRay", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) - .def("isLineString", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isSegment", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) + .def("isLineString", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isPolygon", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isPlane", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isSphere", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isEllipsoid", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) .def("isPyramid", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) + .def("isComposite", +[] (const Composite& aComposite) -> bool { return aComposite.is() ; }) - .def("intersects", &Composite::intersects) - .def("contains", &Composite::contains) + .def("intersectsObject", +[] (const Composite& aComposite, const Object& anObject) -> bool { return aComposite.intersects(anObject) ; }) + .def("intersectsComposite", +[] (const Composite& aComposite, const Composite& anotherComposite) -> bool { return aComposite.intersects(anotherComposite) ; }) + .def("containsObject", +[] (const Composite& aComposite, const Object& anObject) -> bool { return aComposite.contains(anObject) ; }) + .def("containsComposite", +[] (const Composite& aComposite, const Composite& anotherComposite) -> bool { return aComposite.contains(anotherComposite) ; }) .def("asPoint", +[] (const Composite& aComposite) -> Point { return aComposite.as() ; }) .def("asPointSet", +[] (const Composite& aComposite) -> PointSet { return aComposite.as() ; }) @@ -73,10 +82,13 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi .def("asSphere", +[] (const Composite& aComposite) -> Sphere { return aComposite.as() ; }) .def("asEllipsoid", +[] (const Composite& aComposite) -> Ellipsoid { return aComposite.as() ; }) .def("asPyramid", +[] (const Composite& aComposite) -> Pyramid { return aComposite.as() ; }) + .def("asComposite", +[] (const Composite& aComposite) -> Composite { return aComposite.as() ; }) .def("accessObjectAt", &Composite::accessObjectAt, return_value_policy()) .def("accessObjects", &Composite::accessObjects, return_value_policy()) .def("getObjectCount", &Composite::getObjectCount) + .def("intersectionWithObject", +[] (const Composite& aComposite, const Object& anObject) -> Intersection { return aComposite.intersectionWith(anObject) ; }) + .def("intersectionWithComposite", +[] (const Composite& aComposite, const Composite& anotherComposite) -> Intersection { return aComposite.intersectionWith(anotherComposite) ; }) .def("applyTransformation", &Composite::applyTransformation)