From 0626383ac8282f2cb3c0a05b4b47aa6d3aa4791d Mon Sep 17 00:00:00 2001 From: pavel Date: Thu, 2 Jul 2020 17:33:35 +0200 Subject: [PATCH] added safety checks to avoid infinite loop --- Graph.hpp | 56 +++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Graph.hpp b/Graph.hpp index e1065a0..7ea199f 100644 --- a/Graph.hpp +++ b/Graph.hpp @@ -507,38 +507,31 @@ class Graph { } Vertex::HalfEdgeRange halfEdgeRing(VertexHandle vh) const { - return { Vertex::HalfEdgeIterator(*this, vh), - Vertex::HalfEdgeIterator(*this, vh, Vertex::EndTag{}) }; + return { Vertex::HalfEdgeIterator(*this, vh), Vertex::HalfEdgeIterator(*this, vh, Vertex::EndTag{}) }; } Vertex::EdgeRange edgeRing(VertexHandle vh) const { - return { Vertex::EdgeIterator(*this, vh), - Vertex::EdgeIterator(*this, vh, Vertex::EndTag{}) }; + return { Vertex::EdgeIterator(*this, vh), Vertex::EdgeIterator(*this, vh, Vertex::EndTag{}) }; } Vertex::VertexRange vertexRing(VertexHandle vh) const { - return { Vertex::VertexIterator(*this, vh), - Vertex::VertexIterator(*this, vh, Vertex::EndTag{}) }; + return { Vertex::VertexIterator(*this, vh), Vertex::VertexIterator(*this, vh, Vertex::EndTag{}) }; } Vertex::FaceRange faceRing(VertexHandle vh) const { - return { Vertex::FaceIterator(*this, vh), - Vertex::FaceIterator(*this, vh, Vertex::EndTag{}) }; + return { Vertex::FaceIterator(*this, vh), Vertex::FaceIterator(*this, vh, Vertex::EndTag{}) }; } Face::HalfEdgeRange halfEdgeRing(FaceHandle fh) const { - return { Face::HalfEdgeIterator(*this, fh), - Face::HalfEdgeIterator(*this, fh, Face::EndTag{}) }; + return { Face::HalfEdgeIterator(*this, fh), Face::HalfEdgeIterator(*this, fh, Face::EndTag{}) }; } Face::VertexRange vertexRing(FaceHandle fh) const { - return { Face::VertexIterator(*this, fh), - Face::VertexIterator(*this, fh, Face::EndTag{}) }; + return { Face::VertexIterator(*this, fh), Face::VertexIterator(*this, fh, Face::EndTag{}) }; } Face::FaceRange faceRing(FaceHandle fh) const { - return { Face::FaceIterator(*this, fh), - Face::FaceIterator(*this, fh, Face::EndTag{}) }; + return { Face::FaceIterator(*this, fh), Face::FaceIterator(*this, fh, Face::EndTag{}) }; } std::array faceVertices(FaceHandle fh) const { @@ -589,32 +582,45 @@ class Graph { } for (int i = 0; i < 3; ++i) { + int safetyCntr = 0; for (HalfEdgeHandle neh : halfEdgeRing(to(eh[i]))) { // std::cout << "Visiting " << neh << "\n"; if (to(neh) == from(eh[i])) { if (!halfEdges_[neh].boundary()) { - std::cout << "Complex edge!\n"; + std::cout << "Complex edge! when setting opposites" << std::endl; break; } halfEdges_[neh].opposite = eh[i]; halfEdges_[eh[i]].opposite = neh; break; } + + if (++safetyCntr > 50) { + std::cout << "Iterated 50times without finding the end, terminating" << std::endl; + break; + } } if (halfEdges_[eh[i]].boundary()) { // opposite not found by circulation std::set removed; + int safetyCntr = 0; + for (HalfEdgeHandle neh : boundaryEdges_[to(eh[i])]) { if (to(neh) == from(eh[i])) { if (!halfEdges_[neh].boundary()) { - std::cout << "Complex edge!\n"; + std::cout << "Complex edge! when finding boundary" << std::endl; + break; } halfEdges_[neh].opposite = eh[i]; halfEdges_[eh[i]].opposite = neh; removed.insert(neh); break; } + if (++safetyCntr > 50) { + std::cout << "Iterated 50times without finding the end, terminating" << std::endl; + break; + } } for (HalfEdgeHandle neh : removed) { boundaryEdges_[to(eh[i])].erase(neh); @@ -834,12 +840,10 @@ class Graph { }*/ } std::vector is; - std::set_intersection( - ring1.begin(), ring1.end(), ring2.begin(), ring2.end(), std::back_inserter(is)); + std::set_intersection(ring1.begin(), ring1.end(), ring2.begin(), ring2.end(), std::back_inserter(is)); VertexHandle vl = to(next(eh)); VertexHandle vr = !boundary(eh) ? to(next(opposite(eh))) : VertexHandle(-1); - return std::all_of( - is.begin(), is.end(), [vl, vr](VertexHandle vh) { return vh == vl || vh == vr; }); + return std::all_of(is.begin(), is.end(), [vl, vr](VertexHandle vh) { return vh == vl || vh == vr; }); // return is.size() == 2; // if (is.size() != 2) { // return false; @@ -911,26 +915,22 @@ class Graph { for (VertexHandle nvh : vertexRing(vh)) { if (!valid(nvh)) { - std::cout << "vertex " << vh << " connected to invalid vertex " << nvh - << std::endl; + std::cout << "vertex " << vh << " connected to invalid vertex " << nvh << std::endl; } } for (HalfEdgeHandle heh : halfEdgeRing(vh)) { if (!valid(heh)) { - std::cout << "vertex " << vh << " connected to invalid halfedge " << heh - << std::endl; + std::cout << "vertex " << vh << " connected to invalid halfedge " << heh << std::endl; } } for (FaceHandle fh : faceRing(vh)) { if (!valid(fh)) { - std::cout << "vertex " << vh << " connected to invalid face " << fh - << std::endl; + std::cout << "vertex " << vh << " connected to invalid face " << fh << std::endl; } } for (EdgeHandle eh : edgeRing(vh)) { if (!valid(eh)) { - std::cout << "vertex " << vh << " connected to invalid edge " << eh - << std::endl; + std::cout << "vertex " << vh << " connected to invalid edge " << eh << std::endl; } } }