diff --git a/generator/tesselator.cpp b/generator/tesselator.cpp index 89b4fd6188..3b180f9285 100644 --- a/generator/tesselator.cpp +++ b/generator/tesselator.cpp @@ -49,7 +49,10 @@ int TesselateInterior(PolygonsT const & polys, TrianglesInfo & info) TESSindex const * elements = tessGetElements(tess.get()); info.Reserve(elementCount); for (int i = 0; i < elementCount; ++i) - info.Add(elements[i * 3], elements[i * 3 + 1], elements[i * 3 + 2]); + { + if (!info.Add(elements[i * 3], elements[i * 3 + 1], elements[i * 3 + 2])) + return 0; + } } return elementCount; } @@ -60,23 +63,22 @@ int TesselateInterior(PolygonsT const & polys, TrianglesInfo & info) int TrianglesInfo::ListInfo::empty_key = -1; - void TrianglesInfo::ListInfo::AddNeighbour(int p1, int p2, int trg) + bool TrianglesInfo::ListInfo::AddNeighbour(int p1, int p2, int trg) { - // find or insert element for key - std::pair ret = m_neighbors.insert(std::make_pair(std::make_pair(p1, p2), trg)); - - // triangles should not duplicate - CHECK ( ret.second, ("Duplicating triangles for indices : ", p1, p2) ); + return m_neighbors.insert({{p1, p2}, trg}).second; } - void TrianglesInfo::ListInfo::Add(int p0, int p1, int p2) + bool TrianglesInfo::ListInfo::Add(int p0, int p1, int p2) { - m_triangles.emplace_back(p0, p1, p2); + int const trg = static_cast(m_triangles.size()); + if (!AddNeighbour(p0, p1, trg) || !AddNeighbour(p1, p2, trg) || !AddNeighbour(p2, p0, trg)) + { + LOG(LERROR, ("Duplicating triangle {", p0, p1, p2, "}")); + return false; + } - int const trg = static_cast(m_triangles.size()) - 1; - AddNeighbour(p0, p1, trg); - AddNeighbour(p1, p2, trg); - AddNeighbour(p2, p0, trg); + m_triangles.emplace_back(p0, p1, p2); + return true; } template size_t GetBufferSize(IterT b, IterT e) @@ -236,9 +238,9 @@ int TesselateInterior(PolygonsT const & polys, TrianglesInfo & info) // MakeTrianglesChainImpl(points, start, chain); } - void TrianglesInfo::Add(int p0, int p1, int p2) + bool TrianglesInfo::Add(int p0, int p1, int p2) { - m_triangles.back().Add(p0, p1, p2); + return m_triangles.back().Add(p0, p1, p2); } void TrianglesInfo::GetPointsInfo(m2::PointU const & baseP, diff --git a/generator/tesselator.hpp b/generator/tesselator.hpp index e6e2233ca8..2109770f0e 100644 --- a/generator/tesselator.hpp +++ b/generator/tesselator.hpp @@ -76,7 +76,7 @@ namespace tesselator using TNeighbours = std::unordered_map, int, HashPair>; TNeighbours m_neighbors; - void AddNeighbour(int p1, int p2, int trg); + bool AddNeighbour(int p1, int p2, int trg); void GetNeighbors( Triangle const & trg, Triangle const & from, int * nb) const; @@ -87,12 +87,12 @@ namespace tesselator public: using TIterator = TNeighbours::const_iterator; - ListInfo(size_t count) + explicit ListInfo(size_t count) { m_triangles.reserve(count); } - void Add(int p0, int p1, int p2); + bool Add(int p0, int p1, int p2); void Start() const { @@ -134,14 +134,14 @@ namespace tesselator void Reserve(size_t count) { m_triangles.push_back(ListInfo(count)); } - void Add(int p0, int p1, int p2); + bool Add(int p0, int p1, int p2); //@} inline bool IsEmpty() const { return m_triangles.empty(); } - template void ForEachTriangle(ToDo toDo) const + template void ForEachTriangle(ToDo && toDo) const { - for (std::list::const_iterator i = m_triangles.begin(); i != m_triangles.end(); ++i) + for (auto i = m_triangles.begin(); i != m_triangles.end(); ++i) { size_t const count = i->GetCount(); for (size_t j = 0; j < count; ++j)