From 2c5d8bce3e173707794a513c6ec320144c3ffbf2 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sun, 27 Jul 2025 22:15:11 +0200 Subject: [PATCH] Part: Cleanup setupCoinGeometry method --- src/Mod/Part/App/Tools.cpp | 24 ++++++++++++++ src/Mod/Part/App/Tools.h | 41 +++++++++++++++++++++++ src/Mod/Part/Gui/ViewProviderExt.cpp | 49 ++++++++++------------------ 3 files changed, 83 insertions(+), 31 deletions(-) diff --git a/src/Mod/Part/App/Tools.cpp b/src/Mod/Part/App/Tools.cpp index 71caa4dac8..bf1f9197c6 100644 --- a/src/Mod/Part/App/Tools.cpp +++ b/src/Mod/Part/App/Tools.cpp @@ -26,6 +26,7 @@ # include # include # include +# include # include # include # include @@ -818,3 +819,26 @@ bool Part::Tools::isShapeEmpty(const TopoDS_Shape& shape) TopExp_Explorer explorer(shape, TopAbs_VERTEX); return !explorer.More(); } + +Bnd_Box Part::Tools::getBounds(const TopoDS_Shape& shape) +{ + Bnd_Box bounds; + BRepBndLib::Add(shape, bounds); + bounds.SetGap(0.0); + + return bounds; +} + +Standard_Real Part::Tools::getDeflection(const Bnd_Box& bounds, double deviation) +{ + Standard_Real xMin, yMin, zMin, xMax, yMax, zMax; + bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax); + + // calculating the deflection value + return ((xMax - xMin) + (yMax - yMin) + (zMax - zMin)) / 300.0 * deviation; +} + +Standard_Real Part::Tools::getDeflection(const TopoDS_Shape& shape, double deviation) +{ + return getDeflection(getBounds(shape), deviation); +} diff --git a/src/Mod/Part/App/Tools.h b/src/Mod/Part/App/Tools.h index 64a21e18e8..56eb3c18a2 100644 --- a/src/Mod/Part/App/Tools.h +++ b/src/Mod/Part/App/Tools.h @@ -49,6 +49,7 @@ class TopoShape; } class gp_Lin; class gp_Pln; +class Bnd_Box; namespace Base { // Specialization for gp_Pnt @@ -259,6 +260,46 @@ public: * \return `true` if the shape is empty, otherwise `false`. */ static bool isShapeEmpty(const TopoDS_Shape& shape); + + /** + * \brief Computes the bounding box for the given TopoDS_Shape. + * + * This function calculates the axis-aligned bounding box for the specified shape. + * The bounding box represents the spatial boundaries of the shape in 3D space. + * + * \param[in] shape The shape for which the bounding box is to be calculated. + * \return A \ref Bnd_Box object containing the minimum and maximum extents of the shape + * in the X, Y, and Z dimensions. + */ + static Bnd_Box getBounds(const TopoDS_Shape& shape); + + /** + * \brief Calculates the deflection value based on the bounding box and a deviation factor. + * + * This function computes a deflection value that is typically used for + * meshing or approximation. The deflection is derived from the dimensions + * of the bounding box and scaled by a given deviation value. + * + * \param[in] bounds The bounding box dimensions of a shape. + * \param[in] deviation The deviation factor to apply. + * + * \return The computed deflection value. + */ + static Standard_Real getDeflection(const Bnd_Box& bounds, double deviation); + + /** + * \brief Computes the deflection value for a given shape and a deviation factor. + * + * This function calculates the deflection value for the specified shape by + * first determining its bounding box and then using the bounding box dimensions + * to compute the deflection. The deviation factor provides additional scaling. + * + * \param[in] shape The shape for which the deflection value is to be computed. + * \param[in] deviation The deviation factor to apply. + * + * \return The computed deflection value. + */ + static Standard_Real getDeflection(const TopoDS_Shape& shape, double deviation); }; } //namespace Part diff --git a/src/Mod/Part/Gui/ViewProviderExt.cpp b/src/Mod/Part/Gui/ViewProviderExt.cpp index 602c13f5a8..4445637018 100644 --- a/src/Mod/Part/Gui/ViewProviderExt.cpp +++ b/src/Mod/Part/Gui/ViewProviderExt.cpp @@ -82,6 +82,8 @@ #include #include #include +#include + #include #include @@ -955,12 +957,7 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, std::set faceEdges; // calculating the deflection value - Bnd_Box bounds; - BRepBndLib::Add(shape, bounds); - bounds.SetGap(0.0); - Standard_Real xMin, yMin, zMin, xMax, yMax, zMax; - bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax); - Standard_Real deflection = ((xMax - xMin) + (yMax - yMin) + (zMax - zMin)) / 300.0 * deviation; + Standard_Real deflection = Part::Tools::getDeflection(shape, deviation); // Since OCCT 7.6 a value of equal 0 is not allowed any more, this can happen if a single // vertex should be displayed. @@ -1143,9 +1140,9 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, NV3.SetXYZ(Normals(N3).XYZ()); } else { - gp_Vec v1(V1.X(), V1.Y(), V1.Z()), - v2(V2.X(), V2.Y(), V2.Z()), - v3(V3.X(), V3.Y(), V3.Z()); + gp_Vec v1 = Base::convertTo(V1); + gp_Vec v2 = Base::convertTo(V2); + gp_Vec v3 = Base::convertTo(V3); gp_Vec normal = (v2 - v1) ^ (v3 - v1); NV1 = normal; @@ -1166,20 +1163,14 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, } // add the normals for all points of this triangle - norms[faceNodeOffset + N1 - 1] += SbVec3f(NV1.X(), NV1.Y(), NV1.Z()); - norms[faceNodeOffset + N2 - 1] += SbVec3f(NV2.X(), NV2.Y(), NV2.Z()); - norms[faceNodeOffset + N3 - 1] += SbVec3f(NV3.X(), NV3.Y(), NV3.Z()); + norms[faceNodeOffset + N1 - 1] += Base::convertTo(NV1); + norms[faceNodeOffset + N2 - 1] += Base::convertTo(NV2); + norms[faceNodeOffset + N3 - 1] += Base::convertTo(NV3); // set the vertices - verts[faceNodeOffset + N1 - 1].setValue((float)(V1.X()), - (float)(V1.Y()), - (float)(V1.Z())); - verts[faceNodeOffset + N2 - 1].setValue((float)(V2.X()), - (float)(V2.Y()), - (float)(V2.Z())); - verts[faceNodeOffset + N3 - 1].setValue((float)(V3.X()), - (float)(V3.Y()), - (float)(V3.Z())); + verts[faceNodeOffset + N1 - 1] = Base::convertTo(V1); + verts[faceNodeOffset + N2 - 1] = Base::convertTo(V2); + verts[faceNodeOffset + N3 - 1] = Base::convertTo(V3); // set the index vector with the 3 point indexes and the end delimiter index[faceTriaOffset * 4 + 4 * (g - 1)] = faceNodeOffset + N1 - 1; @@ -1227,7 +1218,7 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, if (!identity) { p.Transform(myTransf); } - verts[index].setValue((float)(p.X()), (float)(p.Y()), (float)(p.Z())); + verts[index] = Base::convertTo(p); } // remove the handled edge index from the set @@ -1269,7 +1260,7 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, pnt.Transform(myTransf); } int index = faceNodeOffset + j - 1; - verts[index].setValue((float)(pnt.X()), (float)(pnt.Y()), (float)(pnt.Z())); + verts[index] = Base::convertTo(pnt); lineSetMap[i].push_back(index); } @@ -1282,9 +1273,8 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, for (int i = 0; i < vertexMap.Extent(); i++) { const TopoDS_Vertex& aVertex = TopoDS::Vertex(vertexMap(i + 1)); gp_Pnt pnt = BRep_Tool::Pnt(aVertex); - verts[faceNodeOffset + i].setValue((float)(pnt.X()), - (float)(pnt.Y()), - (float)(pnt.Z())); + + verts[faceNodeOffset + i] = Base::convertTo(pnt); } // normalize all normals @@ -1304,9 +1294,7 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, int32_t* lines = lineset->coordIndex.startEditing(); int l = 0; - for (std::vector::const_iterator it = lineSetCoords.begin(); - it != lineSetCoords.end(); - ++it, l++) { + for (auto it = lineSetCoords.begin(); it != lineSetCoords.end(); ++it, l++) { lines[l] = *it; } @@ -1318,9 +1306,8 @@ void ViewProviderPartExt::setupCoinGeometry(TopoDS_Shape shape, lineset->coordIndex.finishEditing(); # ifdef FC_DEBUG - // printing some information Base::Console().log("ViewProvider update time: %f s\n",Base::TimeElapsed::diffTimeF(startTime,Base::TimeElapsed())); - Base::Console().log("Shape tria info: Faces:%d Edges:%d Nodes:%d Triangles:%d IdxVec:%d\n",numFaces,numEdges,numNodes,numTriangles,numLines); + Base::Console().log("Shape mesh info: Faces:%d Edges:%d Nodes:%d Triangles:%d IdxVec:%d\n",numFaces,numEdges,numNodes,numTriangles,numLines); # endif }