From 046443259ea4d47284705728640ff911e374fc7f Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 28 Mar 2022 18:34:22 +0000 Subject: [PATCH] [occ] gp_GTrsf for anisotropic trafos --- libsrc/occ/occ_face.cpp | 2 +- libsrc/occ/occ_utils.cpp | 16 ++++++++++++++++ libsrc/occ/occ_utils.hpp | 10 ++++++++++ libsrc/occ/occgeom.cpp | 25 ++++++++----------------- libsrc/occ/occgeom.hpp | 4 ++-- libsrc/occ/python_occ_basic.cpp | 24 +++++++++++++++++++++++- libsrc/occ/python_occ_shapes.cpp | 16 +++++++++++----- 7 files changed, 71 insertions(+), 26 deletions(-) diff --git a/libsrc/occ/occ_face.cpp b/libsrc/occ/occ_face.cpp index 7b985f06..032ace63 100644 --- a/libsrc/occ/occ_face.cpp +++ b/libsrc/occ/occ_face.cpp @@ -12,7 +12,7 @@ namespace netgen : tface(dshape.TShape()), face(TopoDS::Face(dshape)) { - BRepGProp::LinearProperties(face, props); + BRepGProp::SurfaceProperties (dshape, props); bbox = ::netgen::GetBoundingBox(face); surface = BRep_Tool::Surface(face); diff --git a/libsrc/occ/occ_utils.cpp b/libsrc/occ/occ_utils.cpp index 210e358e..5f36d357 100644 --- a/libsrc/occ/occ_utils.cpp +++ b/libsrc/occ/occ_utils.cpp @@ -27,6 +27,22 @@ namespace netgen return trafo; } + Transformation<3> occ2ng (const gp_GTrsf & occ_trafo) + { + Transformation<3> trafo; + auto v = occ_trafo.TranslationPart(); + auto m = occ_trafo.VectorialPart(); + auto & tv = trafo.GetVector(); + auto & tm = trafo.GetMatrix(); + for(auto i : Range(3)) + { + tv[i] = v.Coord(i+1); + for(auto k : Range(3)) + tm(i,k) = m(i+1,k+1); + } + return trafo; + } + Box<3> GetBoundingBox( const TopoDS_Shape & shape ) { Bnd_Box bb; diff --git a/libsrc/occ/occ_utils.hpp b/libsrc/occ/occ_utils.hpp index e51a2b51..85ae0ad1 100644 --- a/libsrc/occ/occ_utils.hpp +++ b/libsrc/occ/occ_utils.hpp @@ -1,6 +1,8 @@ #ifndef FILE_OCC_UTILS_INCLUDED #define FILE_OCC_UTILS_INCLUDED +#include + #include #include #include @@ -10,6 +12,7 @@ #include #include #include +#include #include "meshing.hpp" @@ -49,6 +52,13 @@ namespace netgen } DLL_HEADER Transformation<3> occ2ng (const gp_Trsf & t); + DLL_HEADER Transformation<3> occ2ng (const gp_GTrsf & t); + inline Transformation<3> occ2ng (const variant & t) + { + if(auto t1 = get_if(&t)) + return occ2ng(*t1); + return occ2ng(get(t)); + } inline gp_Pnt ng2occ (const Point<3> & p) { diff --git a/libsrc/occ/occgeom.cpp b/libsrc/occ/occgeom.cpp index 3d36bb7a..a163baf3 100644 --- a/libsrc/occ/occgeom.cpp +++ b/libsrc/occ/occgeom.cpp @@ -1955,13 +1955,6 @@ namespace netgen return false; } - Point<3> GetCenter(const TopoDS_Shape & shape) - { - GProp_GProps props; - BRepGProp::LinearProperties(shape, props); - return occ2ng( props.CentreOfMass() ); - } - bool IsMappedShape(const Transformation<3> & trafo, const TopoDS_Shape & me, const TopoDS_Shape & you) { if(me.ShapeType() != you.ShapeType()) return false; @@ -1971,8 +1964,8 @@ namespace netgen BRepBndLib::Add(you, bbox); BoxTree<3> tree( occ2ng(bbox.CornerMin()), occ2ng(bbox.CornerMax()) ); - Point<3> c_me = GetCenter(me); - Point<3> c_you = GetCenter(you); + Point<3> c_me = occ2ng(Center(me)); + Point<3> c_you = occ2ng(Center(you)); if(tree.GetTolerance() < Dist(trafo(c_me), c_you)) return false; @@ -2010,17 +2003,17 @@ namespace netgen return true; } - void Identify(const TopoDS_Shape & me, const TopoDS_Shape & you, string name, Identifications::ID_TYPE type, std::optional opt_trafo) + void Identify(const TopoDS_Shape & me, const TopoDS_Shape & you, string name, Identifications::ID_TYPE type, std::optional> opt_trafo) { - gp_Trsf trafo; + Transformation<3> trafo; if(opt_trafo) { - trafo = *opt_trafo; + trafo = occ2ng(*opt_trafo); } else { - auto v = GetCenter(you) - GetCenter(me); - trafo.SetTranslation(gp_Vec(v[0], v[1], v[2])); + auto v = occ2ng(Center(you)) - occ2ng(Center(me)); + trafo = Transformation<3>(v); } ListOfShapes list_me, list_you; @@ -2029,10 +2022,8 @@ namespace netgen Identify(list_me, list_you, name, type, trafo); } - void Identify(const ListOfShapes & me, const ListOfShapes & you, string name, Identifications::ID_TYPE type, gp_Trsf occ_trafo) + void Identify(const ListOfShapes & me, const ListOfShapes & you, string name, Identifications::ID_TYPE type, Transformation<3> trafo) { - Transformation<3> trafo = occ2ng(occ_trafo); - ListOfShapes id_me; ListOfShapes id_you; diff --git a/libsrc/occ/occgeom.hpp b/libsrc/occ/occgeom.hpp index d266c69d..45859026 100644 --- a/libsrc/occ/occgeom.hpp +++ b/libsrc/occ/occgeom.hpp @@ -351,8 +351,8 @@ namespace netgen //bool FastProject (int surfi, Point<3> & ap, double& u, double& v) const; }; - void Identify(const ListOfShapes & me, const ListOfShapes & you, string name, Identifications::ID_TYPE type, gp_Trsf occ_trafo); - void Identify(const TopoDS_Shape & me, const TopoDS_Shape & you, string name, Identifications::ID_TYPE type, std::optional opt_trafo); + void Identify(const ListOfShapes & me, const ListOfShapes & you, string name, Identifications::ID_TYPE type, Transformation<3> trafo); + void Identify(const TopoDS_Shape & me, const TopoDS_Shape & you, string name, Identifications::ID_TYPE type, std::optional> opt_trafo); void PrintContents (OCCGeometry * geom); diff --git a/libsrc/occ/python_occ_basic.cpp b/libsrc/occ/python_occ_basic.cpp index cdf338fe..276e783e 100644 --- a/libsrc/occ/python_occ_basic.cpp +++ b/libsrc/occ/python_occ_basic.cpp @@ -9,11 +9,13 @@ #include "occgeom.hpp" #include +#include #include #include #include #include #include +#include using namespace netgen; @@ -264,7 +266,27 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m) }), py::arg("p")=gp_Pnt2d(0,0), py::arg("d")=gp_Dir2d(1,0)) ; - + py::class_(m, "gp_GTrsf") + .def(py::init([](const std::vector& mat, + const std::vector& vec) + { + if(mat.size() != 9) + throw Exception("Need 9 matrix values for construction of gp_GTrsf"); + if(vec.size() != 3) + throw Exception("Need 3 vector values for construction of gp_GTrsf"); + gp_GTrsf trafo; + trafo.SetVectorialPart({ mat[0], mat[1], mat[2], + mat[3], mat[4], mat[5], + mat[6], mat[7], mat[8] }); + trafo.SetTranslationPart( { vec[0], vec[1], vec[2] }); + return trafo; + }), py::arg("mat"), py::arg("vec") = std::vector{ 0., 0., 0. }) + .def("__call__", [] (gp_GTrsf & trafo, const TopoDS_Shape & shape) { + BRepBuilderAPI_GTransform builder(shape, trafo, true); + PropagateProperties(builder, shape, occ2ng(trafo)); + return builder.Shape(); + }) + ; py::class_(m, "gp_Trsf") .def(py::init<>()) diff --git a/libsrc/occ/python_occ_shapes.cpp b/libsrc/occ/python_occ_shapes.cpp index 99f58086..5304eefa 100644 --- a/libsrc/occ/python_occ_shapes.cpp +++ b/libsrc/occ/python_occ_shapes.cpp @@ -1098,7 +1098,7 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) }) - .def("Identify", py::overload_cast>(&Identify), + .def("Identify", py::overload_cast>>(&Identify), py::arg("other"), py::arg("name"), py::arg("type")=Identifications::PERIODIC, py::arg("trafo")=nullopt, "Identify shapes for periodic meshing") @@ -1689,10 +1689,16 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) OCCGeometry::global_shape_properties[shape.TShape()].quad_dominated = quad_dominated; }) - .def("Identify", py::overload_cast(&Identify), - py::arg("other"), py::arg("name"), - py::arg("type")=Identifications::PERIODIC, py::arg("trafo"), - "Identify shapes for periodic meshing") + .def("Identify", [](const ListOfShapes& me, + const ListOfShapes& other, + string name, + Identifications::ID_TYPE type, + std::variant trafo) + { + Identify(me, other, name, type, occ2ng(trafo)); + }, py::arg("other"), py::arg("name"), + py::arg("type")=Identifications::PERIODIC, py::arg("trafo"), + "Identify shapes for periodic meshing") ;