]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Improve Point interface.
authorBruno Turcksin <bruno.turcksin@gmail.com>
Thu, 2 Jun 2016 18:53:47 +0000 (14:53 -0400)
committerBruno Turcksin <bruno.turcksin@gmail.com>
Wed, 3 Aug 2016 20:34:02 +0000 (16:34 -0400)
source/python/export_point.cc

index 92c981723b15b22e10e61429279278353c22170a..95b42740b3f3748028fd45025034b32655e293ee 100644 (file)
 
 #include <boost/python.hpp>
 #include <deal.II/base/point.h>
+#include <deal.II/base/exceptions.h>
 
 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<void> point;
+  };
+
 
-  template <int dim>
-  double get_x(const dealii::Point<dim> &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<double>(coord[0]),
+                                        boost::python::extract<double>(coord[1])));
+    else if (dim == 3)
+      point.reset(new dealii::Point<3> (boost::python::extract<double>(coord[0]),
+                                        boost::python::extract<double>(coord[1]),
+                                        boost::python::extract<double>(coord[2])));
+    else
+      AssertThrow(false,
+                  dealii::ExcMessage("The list of coordinates must contain two or three elements."));
   }
 
-  template <int dim>
-  double get_y(const dealii::Point<dim> &point)
+
+
+  double PointWrapper::get_x()
   {
-    return point(1);
+    if (dim == 2)
+      return (*std::static_pointer_cast<dealii::Point<2>>(point))(0);
+    else
+      return (*std::static_pointer_cast<dealii::Point<3>>(point))(0);
   }
 
+
+
+  void PointWrapper::set_x(double x)
+  {
+    if (dim == 2)
+      (*std::static_pointer_cast<dealii::Point<2>>(point))(0) = x;
+    else
+      (*std::static_pointer_cast<dealii::Point<3>>(point))(0) = x;
+  }
+
+
+
+  double PointWrapper::get_y()
+  {
+    if (dim == 2)
+      return (*std::static_pointer_cast<dealii::Point<2>>(point))(1);
+    else
+      return (*std::static_pointer_cast<dealii::Point<3>>(point))(1);
+  }
+
+
+
+  void PointWrapper::set_y(double y)
+  {
+    if (dim == 2)
+      (*std::static_pointer_cast<dealii::Point<2>>(point))(1) = y;
+    else
+      (*std::static_pointer_cast<dealii::Point<3>>(point))(1) = y;
+  }
+
+
+
+  double PointWrapper::get_z()
+  {
+    if (dim == 3)
+      return (*std::static_pointer_cast<dealii::Point<3>>(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<dealii::Point<3>>(point))(2) = z;
+    else
+      AssertThrow(false,
+                  dealii::ExcMessage("The z coordinate is only available for three-dimensional points"));
+  }
+
+
+
   void export_point()
   {
-    boost::python::class_<dealii::Point<2>> ("Point", "Constructor. Requires x and y.",
-                                             boost::python::init<double, double>())
-                                         .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_<PointWrapper> ("Point",boost::python::init<boost::python::list>())
+    .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.");
   }
 
 }

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.