From 538ae878752ce5d216b44e2fc7cd8dcc0bfa8362 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 8 Oct 2021 12:41:07 +0200 Subject: [PATCH] Part: add convenience functions to retrieve triangulation of a face or edge --- src/Mod/Part/App/Tools.cpp | 170 ++++++++++++++++++++++++++++++++++++- src/Mod/Part/App/Tools.h | 31 ++++++- 2 files changed, 198 insertions(+), 3 deletions(-) diff --git a/src/Mod/Part/App/Tools.cpp b/src/Mod/Part/App/Tools.cpp index b02c056045..c0e5944508 100644 --- a/src/Mod/Part/App/Tools.cpp +++ b/src/Mod/Part/App/Tools.cpp @@ -25,25 +25,31 @@ # include # include # include -# include +# include # include # include -# include # include # include # include +# include # include # include # include # include # include +# include # include # include # include +# include # include # include # include # include +# if OCC_VERSION_HEX < 0x070600 +# include +# include +# endif #endif #include @@ -138,6 +144,20 @@ Part::Tools::makeSurface(const TColStd_ListOfTransient &theBoundaries, assert (0); Standard_ConstructionError::Raise ("Tools::makeSurface()"); } +#if OCC_VERSION_HEX >= 0x070600 + else if (aCur->IsKind (STANDARD_TYPE (Adaptor3d_CurveOnSurface))) { + //G1 constraint + Handle(Adaptor3d_CurveOnSurface) aHCOS (Handle(Adaptor3d_CurveOnSurface)::DownCast (aCur)); + Handle (GeomPlate_CurveConstraint) aConst = new GeomPlate_CurveConstraint (aHCOS, 1 /*GeomAbs_G1*/,aNbPnts, aTol3d, anAngTol, aCurvTol); + aPlateBuilder.Add (aConst); + } + else if (aCur->IsKind (STANDARD_TYPE (GeomAdaptor_Curve))) { + //G0 constraint + Handle(GeomAdaptor_Curve) aHC (Handle(GeomAdaptor_Curve)::DownCast (aCur)); + Handle (GeomPlate_CurveConstraint) aConst = new GeomPlate_CurveConstraint (aHC, 0 /*GeomAbs_G0*/, aNbPnts, aTol3d); + aPlateBuilder.Add (aConst); + } +#else else if (aCur->IsKind (STANDARD_TYPE (Adaptor3d_HCurveOnSurface))) { //G1 constraint Handle(Adaptor3d_HCurveOnSurface) aHCOS (Handle(Adaptor3d_HCurveOnSurface)::DownCast (aCur)); @@ -150,6 +170,7 @@ Part::Tools::makeSurface(const TColStd_ListOfTransient &theBoundaries, Handle (GeomPlate_CurveConstraint) aConst = new GeomPlate_CurveConstraint (aHC, 0 /*GeomAbs_G0*/, aNbPnts, aTol3d); aPlateBuilder.Add (aConst); } +#endif else if (aCur->IsKind (STANDARD_TYPE (Geom_Point))) { //Point constraint Handle(Geom_Point) aGP (Handle(Geom_Point)::DownCast (aCur)); @@ -190,3 +211,148 @@ Part::Tools::makeSurface(const TColStd_ListOfTransient &theBoundaries, return aRes; } + +bool Part::Tools::getTriangulation(const TopoDS_Face& face, std::vector& points, std::vector& facets) +{ + TopLoc_Location loc; + Handle(Poly_Triangulation) hTria = BRep_Tool::Triangulation(face, loc); + if (hTria.IsNull()) + return false; + + // getting the transformation of the face + gp_Trsf transf; + bool identity = true; + if (!loc.IsIdentity()) { + identity = false; + transf = loc.Transformation(); + } + + // check orientation + TopAbs_Orientation orient = face.Orientation(); + + Standard_Integer nbNodes = hTria->NbNodes(); + Standard_Integer nbTriangles = hTria->NbTriangles(); +#if OCC_VERSION_HEX < 0x070600 + const TColgp_Array1OfPnt& nodes = hTria->Nodes(); + const Poly_Array1OfTriangle& triangles = hTria->Triangles(); +#endif + + points.reserve(nbNodes); + facets.reserve(nbTriangles); + + // cycling through the poly mesh + // + for (int i = 1; i <= nbNodes; i++) { +#if OCC_VERSION_HEX < 0x070600 + gp_Pnt p = nodes(i); +#else + gp_Pnt p = hTria->Node(i); +#endif + + // transform the vertices to the location of the face + if (!identity) { + p.Transform(transf); + } + + points.push_back(p); + } + + for (int i = 1; i <= nbTriangles; i++) { + // Get the triangle + Standard_Integer n1,n2,n3; +#if OCC_VERSION_HEX < 0x070600 + triangles(i).Get(n1, n2, n3); +#else + hTria->Triangle(i).Get(n1, n2, n3); +#endif + --n1; --n2; --n3; + + // change orientation of the triangles + if (orient != TopAbs_FORWARD) { + std::swap(n1, n2); + } + + facets.emplace_back(n1, n2, n3); + } + + return true; +} + +bool Part::Tools::getPolygonOnTriangulation(const TopoDS_Edge& edge, const TopoDS_Face& face, std::vector& points) +{ + TopLoc_Location loc; + Handle(Poly_Triangulation) hTria = BRep_Tool::Triangulation(face, loc); + if (hTria.IsNull()) + return false; + + // this holds the indices of the edge's triangulation to the actual points + Handle(Poly_PolygonOnTriangulation) hPoly = BRep_Tool::PolygonOnTriangulation(edge, hTria, loc); + if (hPoly.IsNull()) + return false; + + // getting the transformation of the edge + gp_Trsf transf; + bool identity = true; + if (!loc.IsIdentity()) { + identity = false; + transf = loc.Transformation(); + } + + // getting size and create the array + Standard_Integer nbNodes = hPoly->NbNodes(); + points.reserve(nbNodes); + const TColStd_Array1OfInteger& indices = hPoly->Nodes(); +#if OCC_VERSION_HEX < 0x070600 + const TColgp_Array1OfPnt& Nodes = hTria->Nodes(); +#endif + + // go through the index array + for (Standard_Integer i = indices.Lower(); i <= indices.Upper(); i++) { +#if OCC_VERSION_HEX < 0x070600 + gp_Pnt p = Nodes(indices(i)); +#else + gp_Pnt p = hTria->Node(indices(i)); +#endif + if (!identity) { + p.Transform(transf); + } + + points.push_back(p); + } + + return true; +} + +bool Part::Tools::getPolygon3D(const TopoDS_Edge& edge, std::vector& points) +{ + TopLoc_Location loc; + Handle(Poly_Polygon3D) hPoly = BRep_Tool::Polygon3D(edge, loc); + if (hPoly.IsNull()) + return false; + + // getting the transformation of the edge + gp_Trsf transf; + bool identity = true; + if (!loc.IsIdentity()) { + identity = false; + transf = loc.Transformation(); + } + + // getting size and create the array + Standard_Integer nbNodes = hPoly->NbNodes(); + points.reserve(nbNodes); + const TColgp_Array1OfPnt& nodes = hPoly->Nodes(); + + for (int i = 1; i <= nbNodes; i++) { + gp_Pnt p = nodes(i); + + // transform the vertices to the location of the face + if (!identity) { + p.Transform(transf); + } + + points.push_back(p); + } + + return true; +} diff --git a/src/Mod/Part/App/Tools.h b/src/Mod/Part/App/Tools.h index a936c352c6..19ecf937fd 100644 --- a/src/Mod/Part/App/Tools.h +++ b/src/Mod/Part/App/Tools.h @@ -30,7 +30,11 @@ #include #include #include +#include #include +#include +#include +#include class gp_Lin; class gp_Pln; @@ -104,7 +108,32 @@ public: const Standard_Integer theNbPnts, const Standard_Integer theNbIter, const Standard_Integer theMaxDeg); - + /*! + * @brief getTriangulation + * The indexes of the triangles are adjusted to the points vector. + * @param face + * @param points + * @param facets + * @return true if a triangulation exists or false otherwise + */ + static bool getTriangulation(const TopoDS_Face& face, std::vector& points, std::vector& facets); + /*! + * \brief getPolygonOnTriangulation + * Get the polygon of edge. + * \note \a edge must belong to face. + * \param edge + * \param face + * \param points + * \return true if a triangulation exists or false otherwise + */ + static bool getPolygonOnTriangulation(const TopoDS_Edge& edge, const TopoDS_Face& face, std::vector& points); + /*! + * \brief getPolygon3D + * \param edge + * \param points + * \return true if a polygon exists or false otherwise + */ + static bool getPolygon3D(const TopoDS_Edge& edge, std::vector& points); }; } //namespace Part