From b380bf330db1041ef0e08418f7a62d9039ba5dd7 Mon Sep 17 00:00:00 2001 From: artem-ogre Date: Mon, 21 Aug 2023 14:42:52 +0200 Subject: [PATCH] #7 Refactor checking if vertex is Steiner vertex, fix a bug --- CDT/include/Triangulation.h | 10 ++++++--- CDT/include/Triangulation.hpp | 38 ++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CDT/include/Triangulation.h b/CDT/include/Triangulation.h index 0156eb8..d9131c6 100644 --- a/CDT/include/Triangulation.h +++ b/CDT/include/Triangulation.h @@ -127,7 +127,6 @@ class CDT_EXPORT Triangulation public: typedef std::vector > V2dVec; ///< Vertices vector V2dVec vertices; ///< triangulation's vertices - std::vector isSteinerVertex; ///< triangulation's vertices Steiner point flag TriangleVec triangles; ///< triangulation's triangles EdgeUSet fixedEdges; ///< triangulation's constraints (fixed edges) @@ -535,11 +534,16 @@ class CDT_EXPORT Triangulation TriIndVec resolveEncroachedEdges( EdgeQue encroachedEdges, VertInd& newVertBudget, - const V2d* const circumcenterOrNull = NULL, + VertInd steinerVerticesOffset, + const V2d* circumcenterOrNull = NULL, RefinementCriterion::Enum refinementCriterion = RefinementCriterion::SmallestAngle, T badTriangleThreshold = T(0)); - VertInd splitEncroachedEdge(Edge e, TriInd iT, TriInd iTopo); + VertInd splitEncroachedEdge( + Edge e, + TriInd iT, + TriInd iTopo, + VertInd steinerVerticesOffset); void changeNeighbor(TriInd iT, TriInd oldNeighbor, TriInd newNeighbor); void changeNeighbor( TriInd iT, diff --git a/CDT/include/Triangulation.hpp b/CDT/include/Triangulation.hpp index 8c40ce6..aa6dde5 100644 --- a/CDT/include/Triangulation.hpp +++ b/CDT/include/Triangulation.hpp @@ -1022,7 +1022,6 @@ void Triangulation::addNewVertex( const bool isSteiner) { vertices.push_back(pos); - isSteinerVertex.push_back(isSteiner); m_vertTris.push_back(iT); } @@ -1280,6 +1279,7 @@ TriInd Triangulation::edgeTriangle(const Edge edge) const } currTri = t.next(edge.v1()).first; } while(currTri != start); + assert(iT != invalidIndex); return iT; } @@ -1303,12 +1303,12 @@ bool Triangulation::isRefinementNeeded( return false; } -/// Search in all fixed edges to find encroached edges, each fixed edge is -/// checked against its opposite vertices -/// Returns queue of encroached edges template EdgeQue Triangulation::detectEncroachedEdges() { + // Search in all fixed edges to find encroached edges, each fixed edge is + // checked against its opposite vertices + // Returns queue of encroached edges EdgeQue encroachedEdges; for(EdgeUSet::const_iterator cit = fixedEdges.begin(); cit != fixedEdges.end(); @@ -1332,13 +1332,13 @@ EdgeQue Triangulation::detectEncroachedEdges() return encroachedEdges; } -/// Search in all fixed edges to find encroached edges, each fixed edge is -/// checked against its opposite vertices and vertex v -/// Returns queue of encroached edges template EdgeQue Triangulation::detectEncroachedEdges(const V2d& v) { + // Search in all fixed edges to find encroached edges, each fixed edge is + // checked against its opposite vertices and vertex v + // Returns queue of encroached edges EdgeQue encroachedEdges; for(EdgeUSet::const_iterator cit = fixedEdges.begin(); cit != fixedEdges.end(); @@ -1357,6 +1357,7 @@ template TriIndVec Triangulation::resolveEncroachedEdges( EdgeQue encroachedEdges, VertInd& newVertBudget, + const VertInd steinerVerticesOffset, const V2d* const circumcenterOrNull, const RefinementCriterion::Enum refinementCriterion, const T badTriangleThreshold) @@ -1375,7 +1376,10 @@ TriIndVec Triangulation::resolveEncroachedEdges( TriInd iT = edgeTriangle(edge); const Triangle& t = triangles[iT]; VertInd i = splitEncroachedEdge( - edge, iT, edgeNeighbor(triangles[iT], edge.v1(), edge.v2())); + edge, + iT, + edgeNeighbor(triangles[iT], edge.v1(), edge.v2()), + steinerVerticesOffset); --newVertBudget; TriInd start = m_vertTris[i]; @@ -1420,13 +1424,16 @@ template VertInd Triangulation::splitEncroachedEdge( const Edge splitEdge, const TriInd iT, - const TriInd iTopo) + const TriInd iTopo, + const VertInd steinerVerticesOffset) { const VertInd iMid = static_cast(vertices.size()); const V2d& start = vertices[splitEdge.v1()]; const V2d& end = vertices[splitEdge.v2()]; T split = T(0.5); - if(isSteinerVertex[splitEdge.v1()] || isSteinerVertex[splitEdge.v1()]) + // check if any of the split edge vertices are Steiner vertices + if(splitEdge.v1() >= steinerVerticesOffset || + splitEdge.v2() >= steinerVerticesOffset) { // In Ruppert's paper, he used D(0.01) factor to divide edge length, but // that introduces FP rounding erros, so it's avoided. @@ -1444,7 +1451,7 @@ VertInd Triangulation::splitEncroachedEdge( } assert(abs(nearestPowerOfTwo - pow(2, round(log(d) / log(2.0)))) < 1e6); split = nearestPowerOfTwo / len; - if(isSteinerVertex[splitEdge.v1()]) + if(splitEdge.v1() >= steinerVerticesOffset) split = T(1) - split; } V2d mid = V2d::make( @@ -2284,7 +2291,9 @@ void Triangulation::refineTriangles( tryInitNearestPointLocator(); VertInd newVertBudget = maxVerticesToInsert; - resolveEncroachedEdges(detectEncroachedEdges(), newVertBudget); + const VertInd steinerVerticesOffset = vertices.size(); + resolveEncroachedEdges( + detectEncroachedEdges(), newVertBudget, steinerVerticesOffset); std::queue badTriangles; for(TriInd iT(0), n = triangles.size(); iT < n; ++iT) @@ -2327,13 +2336,14 @@ void Triangulation::refineTriangles( { continue; } - TriIndVec badTris = resolveEncroachedEdges( + + const TriIndVec badTris = resolveEncroachedEdges( detectEncroachedEdges(vert), newVertBudget, + steinerVerticesOffset, &vert, refinementCriterion, refinementThreshold); - for(IndexSizeType i(0); i < TriInd(badTris.size()); ++i) { badTriangles.push(badTris[i]);