From b8aa568626e585bb9e4a01c6678fa97637481350 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Thu, 29 Feb 2024 17:19:47 +0100 Subject: [PATCH] Utility function to generate OCC shape triangulation -> always use same parameters --- libsrc/occ/occ_utils.cpp | 22 ++++++++++++++++++ libsrc/occ/occ_utils.hpp | 2 ++ libsrc/occ/occgenmesh.cpp | 25 +------------------- libsrc/occ/occgeom.cpp | 10 +++----- libsrc/occ/python_occ_shapes.cpp | 40 ++++---------------------------- 5 files changed, 32 insertions(+), 67 deletions(-) diff --git a/libsrc/occ/occ_utils.cpp b/libsrc/occ/occ_utils.cpp index 97c6f836..0b97f6e7 100644 --- a/libsrc/occ/occ_utils.cpp +++ b/libsrc/occ/occ_utils.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "occ_utils.hpp" @@ -55,4 +57,24 @@ namespace netgen #endif return {occ2ng(bb.CornerMin()), occ2ng(bb.CornerMax())}; } + + Standard_Integer BuildTriangulation( const TopoDS_Shape & shape ) + { + BRepTools::Clean (shape); + double deflection = 0.01; + + // https://dev.opencascade.org/doc/overview/html/occt_user_guides__mesh.html + // from Standard_Boolean meshing_imeshtools_parameters() + IMeshTools_Parameters aMeshParams; + aMeshParams.Deflection = 0.01; + aMeshParams.Angle = 0.5; + aMeshParams.Relative = Standard_False; + aMeshParams.InParallel = Standard_True; + aMeshParams.MinSize = Precision::Confusion(); + aMeshParams.InternalVerticesMode = Standard_True; + aMeshParams.ControlSurfaceDeflection = Standard_True; + + BRepMesh_IncrementalMesh aMesher (shape, aMeshParams); + return aMesher.GetStatusFlags(); + } } diff --git a/libsrc/occ/occ_utils.hpp b/libsrc/occ/occ_utils.hpp index e3c1c24b..42d92e6b 100644 --- a/libsrc/occ/occ_utils.hpp +++ b/libsrc/occ/occ_utils.hpp @@ -76,6 +76,8 @@ namespace netgen bool opposite_direction; }; + Standard_Integer BuildTriangulation( const TopoDS_Shape & shape ); + class MyExplorer { diff --git a/libsrc/occ/occgenmesh.cpp b/libsrc/occ/occgenmesh.cpp index 45565a2b..9a6c269e 100644 --- a/libsrc/occ/occgenmesh.cpp +++ b/libsrc/occ/occgenmesh.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -654,6 +653,7 @@ namespace netgen int nfaces = geom.fmap.Extent(); + BuildTriangulation(geom.shape); for (int i = 1; i <= nfaces && !multithread.terminate; i++) { multithread.percent = 100 * (i-1)/double(nfaces); @@ -663,29 +663,6 @@ namespace netgen Handle(Geom_Surface) surf = BRep_Tool::Surface (face); Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation (face, loc); - if (triangulation.IsNull()) - { - BRepTools::Clean (geom.shape); - // BRepMesh_IncrementalMesh (geom.shape, 0.01, true); - - // https://dev.opencascade.org/doc/overview/html/occt_user_guides__mesh.html - IMeshTools_Parameters aMeshParams; - aMeshParams.Deflection = 0.01; - aMeshParams.Angle = 0.5; - aMeshParams.Relative = Standard_False; - aMeshParams.InParallel = Standard_True; - aMeshParams.MinSize = Precision::Confusion(); - aMeshParams.InternalVerticesMode = Standard_True; - aMeshParams.ControlSurfaceDeflection = Standard_True; - - BRepMesh_IncrementalMesh aMesher (geom.shape, aMeshParams); - const Standard_Integer aStatus = aMesher.GetStatusFlags(); - if (aStatus != 0) - cout << "BRepMesh_IncrementalMesh.status = " << aStatus << endl; - - triangulation = BRep_Tool::Triangulation (face, loc); - } - if(triangulation.IsNull()) { if (geom.shape.Infinite()) diff --git a/libsrc/occ/occgeom.cpp b/libsrc/occ/occgeom.cpp index 00ed0d03..39402f10 100644 --- a/libsrc/occ/occgeom.cpp +++ b/libsrc/occ/occgeom.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -1387,12 +1386,9 @@ namespace netgen void OCCGeometry :: BuildVisualizationMesh (double deflection) { - cout << "Preparing visualization (deflection = " << deflection << ") ... " << flush; - - BRepTools::Clean (shape); - // BRepMesh_IncrementalMesh:: - BRepMesh_IncrementalMesh (shape, deflection, true); - cout << "done" << endl; + // cout << IM(5) << "Preparing visualization (deflection = " << deflection << ") ... " << flush; + BuildTriangulation(shape); + // cout << IM(5) << "done" << endl; } diff --git a/libsrc/occ/python_occ_shapes.cpp b/libsrc/occ/python_occ_shapes.cpp index 6614a418..0fb7c51e 100644 --- a/libsrc/occ/python_occ_shapes.cpp +++ b/libsrc/occ/python_occ_shapes.cpp @@ -9,6 +9,7 @@ #include #include "occgeom.hpp" +#include "occ_utils.hpp" #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -32,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include @@ -1150,9 +1149,7 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) .def("MakeTriangulation", [](const TopoDS_Shape & shape) { - BRepTools::Clean (shape); - double deflection = 0.01; - BRepMesh_IncrementalMesh (shape, deflection, true); + BuildTriangulation(shape); }) @@ -1181,12 +1178,6 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) throw NgException ("Triangulation: shape is not a face"); } - /* - BRepTools::Clean (shape); - double deflection = 0.01; - BRepMesh_IncrementalMesh (shape, deflection, true); - */ - Handle(Geom_Surface) surf = BRep_Tool::Surface (face); TopLoc_Location loc; @@ -1194,9 +1185,7 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) if (triangulation.IsNull()) { - BRepTools::Clean (shape); - double deflection = 0.01; - BRepMesh_IncrementalMesh (shape, deflection, true); + BuildTriangulation(shape); triangulation = BRep_Tool::Triangulation (face, loc); } // throw Exception("Don't have a triangulation, call 'MakeTriangulation' first"); @@ -1217,30 +1206,9 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) }) .def("_webgui_data", [](const TopoDS_Shape & shape) { - BRepTools::Clean (shape); - double deflection = 0.01; - - // BRepMesh_IncrementalMesh mesher(shape, deflection,Standard_True, 0.01, true); - // mesher.Perform(); - - - // https://dev.opencascade.org/doc/overview/html/occt_user_guides__mesh.html - // from Standard_Boolean meshing_imeshtools_parameters() - IMeshTools_Parameters aMeshParams; - aMeshParams.Deflection = 0.01; - aMeshParams.Angle = 0.5; - aMeshParams.Relative = Standard_False; - aMeshParams.InParallel = Standard_True; - aMeshParams.MinSize = Precision::Confusion(); - aMeshParams.InternalVerticesMode = Standard_True; - aMeshParams.ControlSurfaceDeflection = Standard_True; - - BRepMesh_IncrementalMesh aMesher (shape, aMeshParams); - const Standard_Integer aStatus = aMesher.GetStatusFlags(); + auto status = BuildTriangulation(shape); // cout << "status = " << aStatus << endl; - // triangulation = BRep_Tool::Triangulation (face, loc); - std::vector p[3]; std::vector n[3]; py::list names, colors, solid_names;