From a5650c992d4d46bc80deb0f5ed83e0daedb999c4 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Sun, 31 Aug 2014 09:14:18 +0000 Subject: [PATCH] python exports --- libsrc/meshing/meshclass.hpp | 3 ++ libsrc/meshing/meshfunc.cpp | 3 +- libsrc/meshing/meshtype.cpp | 2 +- libsrc/meshing/meshtype.hpp | 18 +++++---- libsrc/meshing/python_mesh.cpp | 67 ++++++++++++++++++++++++++++------ python/mesh.py | 38 ++++++++++++++++--- 6 files changed, 105 insertions(+), 26 deletions(-) diff --git a/libsrc/meshing/meshclass.hpp b/libsrc/meshing/meshclass.hpp index d4bb0287..8ed0ec1d 100644 --- a/libsrc/meshing/meshclass.hpp +++ b/libsrc/meshing/meshclass.hpp @@ -272,6 +272,9 @@ namespace netgen Element2d & operator[] (SurfaceElementIndex ei) { return surfelements[ei]; } + const T_SURFELEMENTS & SurfaceElements() const { return surfelements; } + T_SURFELEMENTS & SurfaceElements() { return surfelements; } + DLL_HEADER void RebuildSurfaceElementLists (); DLL_HEADER void GetSurfaceElementsOfFace (int facenr, Array & sei) const; diff --git a/libsrc/meshing/meshfunc.cpp b/libsrc/meshing/meshfunc.cpp index 91002d1b..728eb0ee 100644 --- a/libsrc/meshing/meshfunc.cpp +++ b/libsrc/meshing/meshfunc.cpp @@ -639,7 +639,8 @@ namespace netgen MeshOptimize3d optmesh(mp); // teterrpow = mp.opterrpow; - for (size_t j = 1; j <= strlen(mp.optimize3d); j++) + // for (size_t j = 1; j <= strlen(mp.optimize3d); j++) + for (size_t j = 1; j <= mp.optimize3d.length(); j++) { if (multithread.terminate) break; diff --git a/libsrc/meshing/meshtype.cpp b/libsrc/meshing/meshtype.cpp index 54842609..aceedd5b 100644 --- a/libsrc/meshing/meshtype.cpp +++ b/libsrc/meshing/meshtype.cpp @@ -2517,7 +2517,7 @@ namespace netgen //optimize3d = "cmdmstm"; optsteps3d = 3; optimize2d = "smsmsmSmSmSm"; - optsteps2d = 3; + // optsteps2d = 3; opterrpow = 2; blockfill = 1; filldist = 0.1; diff --git a/libsrc/meshing/meshtype.hpp b/libsrc/meshing/meshtype.hpp index ba01c166..b68ce3ea 100644 --- a/libsrc/meshing/meshtype.hpp +++ b/libsrc/meshing/meshtype.hpp @@ -1011,9 +1011,9 @@ namespace netgen // h .. Histogramm, no pause // H .. Histogramm, pause */ - const char * optimize3d; + string optimize3d; /// number of 3d optimization steps - int optsteps3d; + int optsteps3d = 3; /** 2d optimization strategy: // s .. swap, opt 6 lines/node @@ -1091,13 +1091,13 @@ namespace netgen /// high order element curvature int elementorder; /// quad-dominated surface meshing - int quad; + int quad = 0; /// - int inverttets; + int inverttets = 0; /// - int inverttrigs; + int inverttrigs = 0; /// - int autozrefine; + int autozrefine = 0; /// MeshingParameters (); /// @@ -1113,7 +1113,11 @@ namespace netgen } }; - + inline ostream & operator<< (ostream & ost, const MeshingParameters & mp) + { + mp.Print (ost); + return ost; + } class DebugParameters { diff --git a/libsrc/meshing/python_mesh.cpp b/libsrc/meshing/python_mesh.cpp index dfe7b4d8..d1aca4fd 100644 --- a/libsrc/meshing/python_mesh.cpp +++ b/libsrc/meshing/python_mesh.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "meshing.hpp" @@ -37,20 +38,25 @@ inline string ToString (const T& t) -template +template void ExportArray () { string name = string("Array_") + typeid(T).name(); - bp::class_,boost::noncopyable>(name.c_str()) - .def ("__len__", &Array::Size) + bp::class_,boost::noncopyable>(name.c_str()) + .def ("__len__", &Array::Size) .def ("__getitem__", - FunctionPointer ([](Array & self, int i) -> T& + FunctionPointer ([](Array & self, TIND i) -> T& { - if (i < 0 || i >= self.Size()) + 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()]; }))) + ; } @@ -71,12 +77,28 @@ void ExportNetgenMeshing() bp::scope local_scope(module); - bp::class_("PointId") + 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("vertices", FunctionPointer ([](const Element & self) -> bp::list @@ -89,6 +111,8 @@ void ExportNetgenMeshing() ; ExportArray(); + ExportArray(); + ExportArray(); ; @@ -100,16 +124,35 @@ void ExportNetgenMeshing() .def("Elements3D", static_cast&(Mesh::*)()> (& &Mesh::VolumeElements), bp::return_value_policy()) - - /* - .def("Elements2D", &Mesh::SurfaceElements, + + .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)); + })) ; + - - bp::class_ ("MeshingParameters") + typedef MeshingParameters MP; + bp::class_ ("MeshingParameters") + .def("__str__", &ToString) + .add_property("maxh", + FunctionPointer ([](const MP & mp ) { return mp.maxh; }), + FunctionPointer ([](MP & mp, double maxh) { return mp.maxh = maxh; })) + ; } diff --git a/python/mesh.py b/python/mesh.py index 3acacf43..4dc09c41 100644 --- a/python/mesh.py +++ b/python/mesh.py @@ -5,18 +5,46 @@ from libmesh.meshing import * from libcsg.csg import * -geo = CSGeometry("shaft.geo") +geo = CSGeometry("cube.geo") geo.ntlo param = MeshingParameters() +# param.maxh = 100 +print (param) + m1 = GenerateMesh (geo, param) -els = [ i for i in m1.Elements3D() ] -for i in els: - print (i.vertices) -m1.Save("pymesh.vol") +for el in m1.Elements3D(): + vi = el.vertices + for j in vi: + print (j.nr, m1[j].p) + print () + + +print ("num points = ", len (m1.Points())) + +for p in m1.Points(): + print (p.p) + + +m2 = Mesh() + +for p in m1.Points(): + l = p.p + print (l) + m2.Add ( MeshPoint (Point(l[0],l[1],l[2])) ) + +print ("Mesh2 is ", m2) + + + +# els = [ i for i in m1.Elements3D() ] +# for i in els: +# print (i.vertices) + +# m1.Save("pymesh.vol") # mesh = Mesh() # mesh.Load ("shaft.vol.gz")