From f125fefce4b912d44fd1a9a1c752b184a2e8ef9d Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Thu, 2 Jun 2016 14:53:47 -0400 Subject: [PATCH] Improve Point interface. --- source/python/export_point.cc | 105 ++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/source/python/export_point.cc b/source/python/export_point.cc index 92c981723b..95b42740b3 100644 --- a/source/python/export_point.cc +++ b/source/python/export_point.cc @@ -15,28 +15,113 @@ #include #include +#include namespace PyDealII { + class PointWrapper + { + public: + PointWrapper(boost::python::list list); + double get_x(); + void set_x(double x); + double get_y(); + void set_y(double y); + double get_z(); + void set_z(double z); + + private: + int dim; + std::shared_ptr point; + }; + - template - double get_x(const dealii::Point &point) + + PointWrapper::PointWrapper(boost::python::list coord) { - return point(0); + dim = boost::python::len(coord); + if (dim == 2) + point.reset(new dealii::Point<2> (boost::python::extract(coord[0]), + boost::python::extract(coord[1]))); + else if (dim == 3) + point.reset(new dealii::Point<3> (boost::python::extract(coord[0]), + boost::python::extract(coord[1]), + boost::python::extract(coord[2]))); + else + AssertThrow(false, + dealii::ExcMessage("The list of coordinates must contain two or three elements.")); } - template - double get_y(const dealii::Point &point) + + + double PointWrapper::get_x() { - return point(1); + if (dim == 2) + return (*std::static_pointer_cast>(point))(0); + else + return (*std::static_pointer_cast>(point))(0); } + + + void PointWrapper::set_x(double x) + { + if (dim == 2) + (*std::static_pointer_cast>(point))(0) = x; + else + (*std::static_pointer_cast>(point))(0) = x; + } + + + + double PointWrapper::get_y() + { + if (dim == 2) + return (*std::static_pointer_cast>(point))(1); + else + return (*std::static_pointer_cast>(point))(1); + } + + + + void PointWrapper::set_y(double y) + { + if (dim == 2) + (*std::static_pointer_cast>(point))(1) = y; + else + (*std::static_pointer_cast>(point))(1) = y; + } + + + + double PointWrapper::get_z() + { + if (dim == 3) + return (*std::static_pointer_cast>(point))(2); + else + AssertThrow(false, + dealii::ExcMessage("The z coordinate is only available for three-dimensional points")); + } + + + + void PointWrapper::set_z(double z) + { + if (dim == 3) + (*std::static_pointer_cast>(point))(2) = z; + else + AssertThrow(false, + dealii::ExcMessage("The z coordinate is only available for three-dimensional points")); + } + + + void export_point() { - boost::python::class_> ("Point", "Constructor. Requires x and y.", - boost::python::init()) - .add_property("x", get_x<2>, "Return the x component of the point.") - .add_property("y", get_y<2>, "Return the y component of the point."); + boost::python::class_ ("Point",boost::python::init()) + .add_property("x", &PointWrapper::get_x, &PointWrapper::set_x, "Get the x component of the point.") + .add_property("y", &PointWrapper::get_y, &PointWrapper::set_y, "Get the y component of the point.") + .add_property("z", &PointWrapper::get_z, &PointWrapper::set_z, "Get the z component of the point."); } } -- 2.39.5