Skip to content

Commit

Permalink
[feature] Improve Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-bremond committed Sep 11, 2018
1 parent 821b9b3 commit 0fd968b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
38 changes: 38 additions & 0 deletions include/Library/Mathematics/Geometry/3D/Intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class Type>
bool is ( ) const
{

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

return composite_.is<Type>() ;

}

/// @brief Access intersection as its underlying object
///
/// Only valid if the intersection only contains one object.
///
/// @return Reference to underlying object

template <class Type>
const Type& as ( ) const
{

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

return composite_.as<Type>() ;

}

/// @brief Access composite object
///
/// @return Reference to composite object
Expand Down
31 changes: 31 additions & 0 deletions share/python/src/LibraryMathematicsPy/Geometry/3D/Intersection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>("Intersection", init<const Array<Unique<Object>>>())
Expand All @@ -39,13 +41,42 @@ 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))

.def("isDefined", &Intersection::isDefined)
.def("isEmpty", &Intersection::isEmpty)
.def("isComplex", &Intersection::isComplex)

.def("isPoint", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Point>() ; })
.def("isPointSet", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<PointSet>() ; })
.def("isLine", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Line>() ; })
.def("isRay", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Ray>() ; })
.def("isSegment", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Segment>() ; })
.def("isLineString", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<LineString>() ; })
.def("isPolygon", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Polygon>() ; })
.def("isPlane", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Plane>() ; })
.def("isSphere", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Sphere>() ; })
.def("isEllipsoid", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Ellipsoid>() ; })
.def("isPyramid", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Pyramid>() ; })
.def("isComposite", +[] (const Intersection& anIntersection) -> bool { return anIntersection.is<Composite>() ; })

.def("asPoint", +[] (const Intersection& anIntersection) -> Point { return anIntersection.as<Point>() ; })
.def("asPointSet", +[] (const Intersection& anIntersection) -> PointSet { return anIntersection.as<PointSet>() ; })
.def("asLine", +[] (const Intersection& anIntersection) -> Line { return anIntersection.as<Line>() ; })
.def("asRay", +[] (const Intersection& anIntersection) -> Ray { return anIntersection.as<Ray>() ; })
.def("asSegment", +[] (const Intersection& anIntersection) -> Segment { return anIntersection.as<Segment>() ; })
.def("asLineString", +[] (const Intersection& anIntersection) -> LineString { return anIntersection.as<LineString>() ; })
.def("asPolygon", +[] (const Intersection& anIntersection) -> Polygon { return anIntersection.as<Polygon>() ; })
.def("asPlane", +[] (const Intersection& anIntersection) -> Plane { return anIntersection.as<Plane>() ; })
.def("asSphere", +[] (const Intersection& anIntersection) -> Sphere { return anIntersection.as<Sphere>() ; })
.def("asEllipsoid", +[] (const Intersection& anIntersection) -> Ellipsoid { return anIntersection.as<Ellipsoid>() ; })
.def("asPyramid", +[] (const Intersection& anIntersection) -> Pyramid { return anIntersection.as<Pyramid>() ; })
.def("asComposite", +[] (const Intersection& anIntersection) -> Composite { return anIntersection.as<Composite>() ; })

.def("accessComposite", &Intersection::accessComposite, return_value_policy<reference_existing_object>())

.def("getType", &Intersection::getType)
Expand Down
2 changes: 2 additions & 0 deletions share/python/src/LibraryMathematicsPy/Geometry/3D/Objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <LibraryMathematicsPy/Geometry/3D/Objects/Composite.cpp>
#include <LibraryMathematicsPy/Geometry/3D/Objects/Pyramid.cpp>
// #include <LibraryMathematicsPy/Geometry/3D/Objects/Cone.cpp>
#include <LibraryMathematicsPy/Geometry/3D/Objects/Ellipsoid.cpp>
Expand Down Expand Up @@ -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() ;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand All @@ -40,7 +43,10 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi
scope in_Composite = class_<Composite, Shared<Composite>, bases<Object>>("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))
Expand All @@ -51,16 +57,19 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi
.def("isPointSet", +[] (const Composite& aComposite) -> bool { return aComposite.is<PointSet>() ; })
.def("isLine", +[] (const Composite& aComposite) -> bool { return aComposite.is<Line>() ; })
.def("isRay", +[] (const Composite& aComposite) -> bool { return aComposite.is<Ray>() ; })
.def("isLineString", +[] (const Composite& aComposite) -> bool { return aComposite.is<LineString>() ; })
.def("isSegment", +[] (const Composite& aComposite) -> bool { return aComposite.is<Segment>() ; })
.def("isLineString", +[] (const Composite& aComposite) -> bool { return aComposite.is<LineString>() ; })
.def("isPolygon", +[] (const Composite& aComposite) -> bool { return aComposite.is<Polygon>() ; })
.def("isPlane", +[] (const Composite& aComposite) -> bool { return aComposite.is<Plane>() ; })
.def("isSphere", +[] (const Composite& aComposite) -> bool { return aComposite.is<Sphere>() ; })
.def("isEllipsoid", +[] (const Composite& aComposite) -> bool { return aComposite.is<Ellipsoid>() ; })
.def("isPyramid", +[] (const Composite& aComposite) -> bool { return aComposite.is<Pyramid>() ; })
.def("isComposite", +[] (const Composite& aComposite) -> bool { return aComposite.is<Composite>() ; })

.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<Point>() ; })
.def("asPointSet", +[] (const Composite& aComposite) -> PointSet { return aComposite.as<PointSet>() ; })
Expand All @@ -73,10 +82,13 @@ inline void LibraryMathematicsPy_Geometry_3D_Objects_Composi
.def("asSphere", +[] (const Composite& aComposite) -> Sphere { return aComposite.as<Sphere>() ; })
.def("asEllipsoid", +[] (const Composite& aComposite) -> Ellipsoid { return aComposite.as<Ellipsoid>() ; })
.def("asPyramid", +[] (const Composite& aComposite) -> Pyramid { return aComposite.as<Pyramid>() ; })
.def("asComposite", +[] (const Composite& aComposite) -> Composite { return aComposite.as<Composite>() ; })

.def("accessObjectAt", &Composite::accessObjectAt, return_value_policy<reference_existing_object>())
.def("accessObjects", &Composite::accessObjects, return_value_policy<reference_existing_object>())
.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)

Expand Down

0 comments on commit 0fd968b

Please sign in to comment.