From 1a051ec5556f57149259fb1590671e0bd51e9c04 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Fri, 16 Oct 2020 12:05:03 +0200 Subject: [PATCH] export Polyhedra to Python and add test case --- libsrc/csg/python_csg.cpp | 29 +++++++++++++++++++++++++++++ tests/pytest/test_csg.py | 27 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/pytest/test_csg.py diff --git a/libsrc/csg/python_csg.cpp b/libsrc/csg/python_csg.cpp index 09063e51..0954b2d6 100644 --- a/libsrc/csg/python_csg.cpp +++ b/libsrc/csg/python_csg.cpp @@ -354,6 +354,35 @@ When r =1, the truncated elliptic cone becomes an elliptic cylinder. When r tends to zero, the truncated elliptic cone tends to a full elliptic cone. However, when r = 0, the top part becomes a point(tip) and meshing fails! )raw_string"); + + m.def("Polyhedron", [](py::list points, py::list faces) + { + auto poly = new Polyhedra(); + for(auto p : points) + poly->AddPoint(py::cast>(p)); + int fnr = 0; + for(auto face : faces) + { + auto lface = py::cast(face); + if(py::len(lface) == 3) + poly->AddFace(py::cast(lface[0]), + py::cast(lface[1]), + py::cast(lface[2]), + fnr++); + else if(py::len(lface) == 4) + { + poly->AddFace(py::cast(lface[0]), + py::cast(lface[1]), + py::cast(lface[2]), + fnr); + poly->AddFace(py::cast(lface[0]), + py::cast(lface[2]), + py::cast(lface[3]), + fnr++); + } + } + return make_shared(new Solid(poly)); + }); m.def ("Or", FunctionPointer([](shared_ptr s1, shared_ptr s2) { diff --git a/tests/pytest/test_csg.py b/tests/pytest/test_csg.py new file mode 100644 index 00000000..55adcbbd --- /dev/null +++ b/tests/pytest/test_csg.py @@ -0,0 +1,27 @@ +from netgen.csg import * + +def test_2_polyhedra(): + geo = CSGeometry() + first = Polyhedron([(0,0,0), (0,1,0), (3,1,0), (3,0,0), + (0,1,1), (3,1,1), (3,0,1), (0,0,1)], + [(0,1,2,3), (1,4,5,2), (2,5,6,3), (3,6,7,0), + (0,7,4,1), (7,6,5,4)]) + # TODO: height = 0.1 not working! + height = 0.3 + second = Polyhedron([(0,0,1), (0,1,1), (3,1,1), (3,0,1), + (0,1,1+height), (3,1,1+height), + (3,0,1+height), (0,0,1+height)], + [(0,1,2,3), (1,4,5,2), (2,5,6,3), (3,6,7,0), + (0,7,4,1), (7,6,5,4)]) + + geo.Add(first) + geo.Add(second) + Draw(geo) + mesh = geo.GenerateMesh() + return mesh + + +if __name__ == "__main__": + from ngsolve import Mesh, Draw + mesh = Mesh(test_2_polyhedra()) + Draw(mesh)