From 6a552f3d37658f09dc8566f668abec87348c454e 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 e0f446e..4015746 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) @@ -527,11 +526,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 0e98e75..bd6e7b1 100644 --- a/CDT/include/Triangulation.hpp +++ b/CDT/include/Triangulation.hpp @@ -989,7 +989,6 @@ void Triangulation::addNewVertex( const bool isSteiner) { vertices.push_back(pos); - isSteinerVertex.push_back(isSteiner); m_vertTris.push_back(iT); } @@ -1247,6 +1246,7 @@ TriInd Triangulation::edgeTriangle(const Edge edge) const } currTri = t.next(edge.v1()).first; } while(currTri != start); + assert(iT != invalidIndex); return iT; } @@ -1270,12 +1270,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(); @@ -1299,13 +1299,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(); @@ -1324,6 +1324,7 @@ template TriIndVec Triangulation::resolveEncroachedEdges( EdgeQue encroachedEdges, VertInd& newVertBudget, + const VertInd steinerVerticesOffset, const V2d* const circumcenterOrNull, const RefinementCriterion::Enum refinementCriterion, const T badTriangleThreshold) @@ -1342,7 +1343,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]; @@ -1387,13 +1391,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. @@ -1411,7 +1418,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( @@ -2262,7 +2269,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) @@ -2305,13 +2314,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]);