From 10e4a596f661651a51f9186ad59b2c4690b4915e Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Mon, 18 May 2015 16:19:38 +0200 Subject: [PATCH] python wrapper for mesh manipulation --- libsrc/meshing/meshclass.hpp | 3 + libsrc/meshing/python_mesh.cpp | 130 ++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/libsrc/meshing/meshclass.hpp b/libsrc/meshing/meshclass.hpp index f07b6e9b..cf32a736 100644 --- a/libsrc/meshing/meshclass.hpp +++ b/libsrc/meshing/meshclass.hpp @@ -243,6 +243,9 @@ namespace netgen const Segment & operator[] (SegmentIndex si) const { return segments[si]; } Segment & operator[] (SegmentIndex si) { return segments[si]; } + const Array & LineSegments() const { return segments; } + Array & LineSegments() { return segments; } + diff --git a/libsrc/meshing/python_mesh.cpp b/libsrc/meshing/python_mesh.cpp index d67e9180..bc286b7e 100644 --- a/libsrc/meshing/python_mesh.cpp +++ b/libsrc/meshing/python_mesh.cpp @@ -52,6 +52,27 @@ DLL_HEADER void ExportNetgenMeshing() .add_property("nr", &PointIndex::operator int) ; + bp::class_("ElementId3D", bp::init()) + .def("__repr__", &ToString) + .def("__str__", &ToString) + .add_property("nr", &ElementIndex::operator int) + ; + + + bp::class_("ElementId2D", bp::init()) + .def("__repr__", &ToString) + .def("__str__", &ToString) + .add_property("nr", &SurfaceElementIndex::operator int) + ; + + bp::class_("ElementId1D", bp::init()) + .def("__repr__", &ToString) + .def("__str__", &ToString) + .add_property("nr", &SegmentIndex::operator int) + ; + + + /* bp::class_> ("Point") .def(bp::init()) @@ -59,7 +80,9 @@ DLL_HEADER void ExportNetgenMeshing() */ bp::class_> */ >("MeshPoint") - // .def(bp::init>()) + .def(bp::init>()) + .def("__str__", &ToString) + .def("__repr__", &ToString) .add_property("p", FunctionPointer([](const MeshPoint & self) { bp::list l; @@ -71,6 +94,20 @@ DLL_HEADER void ExportNetgenMeshing() ; bp::class_("Element3D") + .def("__init__", bp::make_constructor + (FunctionPointer ([](int index, bp::list vertices) + { + Element * tmp = new Element(TET); + for (int i = 0; i < 4; i++) + (*tmp)[i] = bp::extract(vertices[i]); + tmp->SetIndex(index); + return tmp; + }), + bp::default_call_policies(), // need it to use arguments + (bp::arg("index")=1,bp::arg("vertices"))), + "create volume element" + ) + .def("__repr__", &ToString) .add_property("index", &Element::GetIndex, &Element::SetIndex) .add_property("vertices", FunctionPointer ([](const Element & self) -> bp::list @@ -83,6 +120,19 @@ DLL_HEADER void ExportNetgenMeshing() ; bp::class_("Element2D") + .def("__init__", bp::make_constructor + (FunctionPointer ([](int index, bp::list vertices) + { + Element2d * tmp = new Element2d(TRIG); + for (int i = 0; i < 3; i++) + (*tmp)[i] = bp::extract(vertices[i]); + tmp->SetIndex(index); + return tmp; + }), + bp::default_call_policies(), // need it to use arguments + (bp::arg("index")=1,bp::arg("vertices"))), + "create surface element" + ) .add_property("index", &Element2d::GetIndex, &Element2d::SetIndex) .add_property("vertices", FunctionPointer([](const Element2d & self) -> bp::list @@ -93,9 +143,59 @@ DLL_HEADER void ExportNetgenMeshing() return li; })) ; + + bp::class_("Element1D") + .def("__init__", bp::make_constructor + (FunctionPointer ([](bp::list vertices, bp::list surfaces) + { + Segment * tmp = new Segment; + for (int i = 0; i < 2; i++) + (*tmp)[i] = bp::extract(vertices[i]); + + tmp->surfnr1 = bp::extract(surfaces[0]); + tmp->surfnr2 = bp::extract(surfaces[1]); + return tmp; + }), + bp::default_call_policies(), + (bp::arg("vertices"),bp::arg("surfaces"))), + "create segment element" + ) + .def("__repr__", &ToString) + .add_property("vertices", + FunctionPointer ([](const Segment & self) -> bp::list + { + bp::list li; + for (int i = 0; i < 2; i++) + li.append (self[i]); + return li; + })) + .add_property("surfaces", + FunctionPointer ([](const Segment & self) -> bp::list + { + bp::list li; + li.append (self.surfnr1); + li.append (self.surfnr2); + return li; + })) + ; + + + + + bp::class_("FaceDescriptor") + .def(bp::init()) + .def("__str__", &ToString) + .def("__repr__", &ToString) + .add_property("surfnr", &FaceDescriptor::SurfNr, &FaceDescriptor::SetSurfNr) + ; + + + ExportArray(); ExportArray(); + ExportArray(); ExportArray(); + ExportArray(); ; @@ -128,10 +228,17 @@ DLL_HEADER void ExportNetgenMeshing() static_cast&(Mesh::*)()> (&Mesh::SurfaceElements), bp::return_value_policy()) + .def("Elements1D", + static_cast&(Mesh::*)()> (&Mesh::LineSegments), + bp::return_value_policy()) + + .def("Points", static_cast (&Mesh::Points), bp::return_value_policy()) + .def("FaceDescriptor", static_cast (&Mesh::GetFaceDescriptor), + bp::return_value_policy()) .def("__getitem__", FunctionPointer ([](const Mesh & self, PointIndex pi) { @@ -142,6 +249,27 @@ DLL_HEADER void ExportNetgenMeshing() { return self.AddPoint (Point3d(p)); })) + + .def ("Add", FunctionPointer ([](Mesh & self, const Element & el) + { + return self.AddVolumeElement (el); + })) + + .def ("Add", FunctionPointer ([](Mesh & self, const Element2d & el) + { + return self.AddSurfaceElement (el); + })) + + .def ("Add", FunctionPointer ([](Mesh & self, const Segment & el) + { + return self.AddSegment (el); + })) + + .def ("Add", FunctionPointer ([](Mesh & self, const FaceDescriptor & fd) + { + return self.AddFaceDescriptor (fd); + })) + /* .def("__init__", bp::make_constructor (FunctionPointer ([]()