#ifdef NG_PYTHON #include #include #include #include "meshing.hpp" using namespace netgen; namespace bp = boost::python; ////////////////////////////////////////////////////////////////////// // Lambda to function pointer conversion template struct function_traits : public function_traits {}; template struct function_traits { typedef ReturnType (*pointer)(Args...); typedef ReturnType return_type; }; template typename function_traits::pointer FunctionPointer (const Function& lambda) { return static_cast::pointer>(lambda); } template inline string ToString (const T& t) { stringstream ss; ss << t; return ss.str(); } template void ExportArray () { string name = string("Array_") + typeid(T).name(); bp::class_,boost::noncopyable>(name.c_str()) .def ("__len__", &Array::Size) .def ("__getitem__", FunctionPointer ([](Array & self, TIND i) -> T& { if (i < BASE || i >= BASE+self.Size()) bp::exec("raise IndexError()\n"); return self[i]; }), bp::return_value_policy()) .def ("__iter__", bp::range (FunctionPointer([](Array & self) { return &self[BASE]; }), FunctionPointer([](Array & self) { return &self[BASE+self.Size()]; }))) ; } void ExportNetgenMeshing() { std::string nested_name = "meshing"; if( bp::scope() ) nested_name = bp::extract(bp::scope().attr("__name__") + ".meshing"); bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule(nested_name.c_str())))); cout << "exporting meshing " << nested_name << endl; bp::object parent = bp::scope() ? bp::scope() : bp::import("__main__"); parent.attr("meshing") = module ; bp::scope local_scope(module); bp::class_("PointId", bp::init()) .def("__repr__", &ToString) .def("__str__", &ToString) .add_property("nr", &PointIndex::operator int) ; /* bp::class_> ("Point") .def(bp::init()) ; */ bp::class_> */ >("MeshPoint") // .def(bp::init>()) .add_property("p", FunctionPointer([](const MeshPoint & self) { bp::list l; l.append ( (self)[0] ); l.append ( (self)[1] ); l.append ( (self)[2] ); return l; })) ; bp::class_("Element3D") .add_property("index", &Element::GetIndex, &Element::SetIndex) .add_property("vertices", FunctionPointer ([](const Element & self) -> bp::list { bp::list li; for (int i = 0; i < self.GetNV(); i++) li.append (self[i]); return li; })) ; bp::class_("Element2D") .add_property("index", &Element2d::GetIndex, &Element2d::SetIndex) .add_property("vertices", FunctionPointer([](const Element2d & self) -> bp::list { bp::list li; for (int i = 0; i < self.GetNV(); i++) li.append(self[i]); return li; })) ; ExportArray(); ExportArray(); ExportArray(); ; bp::class_,boost::noncopyable>("Mesh") .def("__str__", &ToString) .def("Load", static_cast(&Mesh::Load)) .def("Save", static_cast(&Mesh::Save)) .def("Elements3D", static_cast&(Mesh::*)()> (&Mesh::VolumeElements), bp::return_value_policy()) .def("Elements2D", static_cast&(Mesh::*)()> (&Mesh::SurfaceElements), bp::return_value_policy()) .def("Points", static_cast (&Mesh::Points), bp::return_value_policy()) .def("__getitem__", FunctionPointer ([](const Mesh & self, PointIndex pi) { return self[pi]; })) .def ("Add", FunctionPointer ([](Mesh & self, MeshPoint p) { return self.AddPoint (Point3d(p)); })) ; typedef MeshingParameters MP; bp::class_ ("MeshingParameters", bp::init<>()) .def("__init__", bp::make_constructor (FunctionPointer ([](double maxh) { auto tmp = new MeshingParameters; tmp->maxh = maxh; return tmp; }), bp::default_call_policies(), // need it to use arguments (bp::arg("maxh")=1000)), "create meshing parameters" ) .def("__str__", &ToString) .add_property("maxh", FunctionPointer ([](const MP & mp ) { return mp.maxh; }), FunctionPointer ([](MP & mp, double maxh) { return mp.maxh = maxh; })) ; } BOOST_PYTHON_MODULE(libmesh) { ExportNetgenMeshing(); } #endif