From d1de4a7cf915c8659d8769198b6ab8f1a890560c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Sch=C3=B6berl?= Date: Thu, 5 Jan 2017 05:08:24 +0100 Subject: [PATCH] Introduce NetgenGeometry base class to Python. export meshing first, then csg --- libsrc/csg/python_csg.cpp | 70 +------------------------ libsrc/geom2d/python_geom2d.cpp | 3 +- libsrc/meshing/python_mesh.cpp | 92 +++++++++++++++++++++++++++++---- ng/netgenpy.cpp | 8 +-- python/csg.py | 1 + 5 files changed, 89 insertions(+), 85 deletions(-) diff --git a/libsrc/csg/python_csg.cpp b/libsrc/csg/python_csg.cpp index fefc6604..94c5074d 100644 --- a/libsrc/csg/python_csg.cpp +++ b/libsrc/csg/python_csg.cpp @@ -163,77 +163,9 @@ namespace netgen } -static Transformation<3> global_trafo(Vec<3> (0,0,0)); DLL_HEADER void ExportCSG(py::module &m) { - py::class_(m, "NGDummyArgument") - .def("__bool__", []( NGDummyArgument &self ) { return false; } ) - ; - - py::class_> (m, "Point2d") - .def(py::init()) - .def ("__str__", &ToString>) - .def(py::self-py::self) - .def(py::self+Vec<2>()) - .def(py::self-Vec<2>()) - ; - - py::class_> (m, "Point3d") - .def(py::init()) - .def ("__str__", &ToString>) - .def(py::self-py::self) - .def(py::self+Vec<3>()) - .def(py::self-Vec<3>()) - ; - - m.def ("Pnt", FunctionPointer - ([](double x, double y, double z) { return global_trafo(Point<3>(x,y,z)); })); - m.def ("Pnt", FunctionPointer - ([](double x, double y) { return Point<2>(x,y); })); - - - m.def ("Pnt", FunctionPointer - ([](double x, double y, double z) { return Point<3>(x,y,z); })); - m.def ("Pnt", FunctionPointer - ([](double x, double y) { return Point<2>(x,y); })); - - m.def ("SetTransformation", FunctionPointer - ([](int dir, double angle) - { - if (dir > 0) - global_trafo.SetAxisRotation (dir, angle*M_PI/180); - else - global_trafo = Transformation<3> (Vec<3>(0,0,0)); - }), - py::arg("dir")=int(0), py::arg("angle")=int(0)); - - py::class_> (m, "Vec2d") - .def(py::init()) - .def ("__str__", &ToString>) - .def(py::self+py::self) - .def(py::self-py::self) - .def(-py::self) - .def(double()*py::self) - .def("Norm", &Vec<2>::Length) - ; - - py::class_> (m, "Vec3d") - .def(py::init()) - .def ("__str__", &ToString>) - .def(py::self+py::self) - .def(py::self-py::self) - .def(-py::self) - .def(double()*py::self) - .def("Norm", &Vec<3>::Length) - ; - - m.def ("Vec", FunctionPointer - ([] (double x, double y, double z) { return global_trafo(Vec<3>(x,y,z)); })); - m.def ("Vec", FunctionPointer - ([] (double x, double y) { return Vec<2>(x,y); })); - - py::class_> (m, "SplineCurve2d") .def(py::init<>()) .def ("AddPoint", FunctionPointer @@ -387,7 +319,7 @@ DLL_HEADER void ExportCSG(py::module &m) })); - py::class_> (m, "CSGeometry") + py::class_> (m, "CSGeometry") .def(py::init<>()) .def("__init__", [](CSGeometry *instance, const string & filename) diff --git a/libsrc/geom2d/python_geom2d.cpp b/libsrc/geom2d/python_geom2d.cpp index f39919e5..6dbca157 100644 --- a/libsrc/geom2d/python_geom2d.cpp +++ b/libsrc/geom2d/python_geom2d.cpp @@ -15,8 +15,7 @@ namespace netgen DLL_HEADER void ExportGeom2d(py::module &m) { - - py::class_> + py::class_> (m, "SplineGeometry", "a 2d boundary representation geometry model by lines and splines") .def(py::init<>()) diff --git a/libsrc/meshing/python_mesh.cpp b/libsrc/meshing/python_mesh.cpp index ed927e6c..997b86c3 100644 --- a/libsrc/meshing/python_mesh.cpp +++ b/libsrc/meshing/python_mesh.cpp @@ -4,8 +4,8 @@ #include #include "meshing.hpp" -#include -#include +// #include +// #include #include <../interface/writeuser.hpp> @@ -45,10 +45,78 @@ void TranslateException (const NgException & ex) PyErr_SetString(PyExc_RuntimeError, err.c_str()); } +static Transformation<3> global_trafo(Vec<3> (0,0,0)); DLL_HEADER void ExportNetgenMeshing(py::module &m) { + py::class_(m, "NGDummyArgument") + .def("__bool__", []( NGDummyArgument &self ) { return false; } ) + ; + py::class_> (m, "Point2d") + .def(py::init()) + .def ("__str__", &ToString>) + .def(py::self-py::self) + .def(py::self+Vec<2>()) + .def(py::self-Vec<2>()) + ; + + py::class_> (m, "Point3d") + .def(py::init()) + .def ("__str__", &ToString>) + .def(py::self-py::self) + .def(py::self+Vec<3>()) + .def(py::self-Vec<3>()) + ; + + m.def ("Pnt", FunctionPointer + ([](double x, double y, double z) { return global_trafo(Point<3>(x,y,z)); })); + m.def ("Pnt", FunctionPointer + ([](double x, double y) { return Point<2>(x,y); })); + + + m.def ("Pnt", FunctionPointer + ([](double x, double y, double z) { return Point<3>(x,y,z); })); + m.def ("Pnt", FunctionPointer + ([](double x, double y) { return Point<2>(x,y); })); + + py::class_> (m, "Vec2d") + .def(py::init()) + .def ("__str__", &ToString>) + .def(py::self+py::self) + .def(py::self-py::self) + .def(-py::self) + .def(double()*py::self) + .def("Norm", &Vec<2>::Length) + ; + + py::class_> (m, "Vec3d") + .def(py::init()) + .def ("__str__", &ToString>) + .def(py::self+py::self) + .def(py::self-py::self) + .def(-py::self) + .def(double()*py::self) + .def("Norm", &Vec<3>::Length) + ; + + m.def ("Vec", FunctionPointer + ([] (double x, double y, double z) { return global_trafo(Vec<3>(x,y,z)); })); + m.def ("Vec", FunctionPointer + ([] (double x, double y) { return Vec<2>(x,y); })); + + + m.def ("SetTransformation", FunctionPointer + ([](int dir, double angle) + { + if (dir > 0) + global_trafo.SetAxisRotation (dir, angle*M_PI/180); + else + global_trafo = Transformation<3> (Vec<3>(0,0,0)); + }), + py::arg("dir")=int(0), py::arg("angle")=int(0)); + + py::class_(m, "PointId") .def(py::init()) @@ -312,6 +380,9 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m) ExportArray(m); py::implicitly_convertible< int, PointIndex>(); + + py::class_> (m, "NetgenGeometry") + ; py::class_>(m, "Mesh") // .def(py::init<>("create empty mesh")) @@ -517,18 +588,19 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m) })) .def ("SetGeometry", FunctionPointer - ([](Mesh & self, shared_ptr geo) + ([](Mesh & self, shared_ptr geo) { self.SetGeometry(geo); })) - // TODO: fix this dependency on libgeom2d.so -// .def ("SetGeometry", FunctionPointer -// ([](Mesh & self, shared_ptr geo) -// { -// self.SetGeometry(geo); -// })) - + /* + .def ("SetGeometry", FunctionPointer + ([](Mesh & self, shared_ptr geo) + { + self.SetGeometry(geo); + })) + */ + .def ("BuildSearchTree", &Mesh::BuildElementSearchTree) .def ("BoundaryLayer", FunctionPointer diff --git a/ng/netgenpy.cpp b/ng/netgenpy.cpp index 3a91e373..988e14db 100644 --- a/ng/netgenpy.cpp +++ b/ng/netgenpy.cpp @@ -21,14 +21,14 @@ void DLL_HEADER ExportSTLVis(py::module &m); PYBIND11_PLUGIN(libngpy) { py::module ngpy("libngpy", "pybind netgen module"); - py::module csg = ngpy.def_submodule("_csg", "pybind csg module"); - ExportCSG(csg); - py::module csgvis = ngpy.def_submodule("csgvis", "pybind csgvis module"); - ExportCSGVis(csgvis); py::module meshing = ngpy.def_submodule("_meshing", "pybind meshing module"); ExportNetgenMeshing(meshing); py::module meshvis = ngpy.def_submodule("meshvis", "pybind meshvis module"); ExportMeshVis(meshvis); + py::module csg = ngpy.def_submodule("_csg", "pybind csg module"); + ExportCSG(csg); + py::module csgvis = ngpy.def_submodule("csgvis", "pybind csgvis module"); + ExportCSGVis(csgvis); py::module geom2d = ngpy.def_submodule("_geom2d", "pybind geom2d module"); ExportGeom2d(geom2d); py::module stl = ngpy.def_submodule("_stl", "pybind stl module"); diff --git a/python/csg.py b/python/csg.py index 11f54147..52315e36 100644 --- a/python/csg.py +++ b/python/csg.py @@ -3,6 +3,7 @@ from libngpy._csg import * import libngpy.csgvis as csgvis from libngpy.csgvis import MouseMove from libngpy._meshing import MeshingParameters +from libngpy._meshing import Pnt CSGeometry.VS = csgvis.VS