CDT  2.1.0
C++ library for constrained Delaunay triangulation
Loading...
Searching...
No Matches
Triangulation.h
Go to the documentation of this file.
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
9
10#ifndef CDT_vW1vZ0lO8rS4gY4uI4fB
11#define CDT_vW1vZ0lO8rS4gY4uI4fB
12
13#include "CDTUtils.h"
14#include "LocatorKDTree.h"
15
16#include <algorithm>
17#include <cstdlib>
18#include <iterator>
19#include <stack>
20#include <stdexcept>
21#include <string>
22#include <utility>
23#include <vector>
24
26namespace CDT
27{
28
31
38struct CDT_EXPORT VertexInsertionOrder
39{
44 enum Enum
45 {
54 };
55};
56
61{
76};
77
81struct CDT_EXPORT RefinementCriterion
82{
92};
93
97struct CDT_EXPORT RefinementOrder
98{
103 enum Enum
104 {
107 };
108};
109
111template <typename T>
112struct FifoQueue : std::queue<T>
113{
114 const T& top() const
115 {
116 return this->front();
117 }
118};
119
130struct CDT_EXPORT Unrefined
131{
140 std::size_t sharpFixedCorner;
142 std::size_t shortEdges;
147
149 Unrefined();
150};
151
157typedef unsigned short LayerDepth;
158typedef LayerDepth BoundaryOverlapCount;
159
164{
165public:
167 SourceLocation(const std::string& file, const std::string& func, int line)
168 : m_file(file)
169 , m_func(func)
170 , m_line(line)
171 {}
172
173 const std::string& file() const
174 {
175 return m_file;
176 }
177
178 const std::string& func() const
179 {
180 return m_func;
181 }
182
183 int line() const
184 {
185 return m_line;
186 }
187
188private:
189 std::string m_file;
190 std::string m_func;
191 int m_line;
192};
193
195#define CDT_SOURCE_LOCATION \
196 SourceLocation(std::string(__FILE__), std::string(__func__), __LINE__)
197
202class CDT_EXPORT Error : public std::runtime_error
203{
204public:
206 Error(const std::string& description, const SourceLocation& srcLoc)
207 : std::runtime_error(
208 description + "\nin '" + srcLoc.func() + "' at " + srcLoc.file() +
209 ":" + CDT::to_string(srcLoc.line()))
210 , m_description(description)
211 , m_srcLoc(srcLoc)
212 {}
213
214 virtual ~Error() CDT_NOEXCEPT
215 {}
216
217 const std::string& description() const
218 {
219 return m_description;
220 }
221
223 {
224 return m_srcLoc;
225 }
226
227private:
228 std::string m_description;
229 SourceLocation m_srcLoc;
230};
231
236class CDT_EXPORT FinalizedError : public Error
237{
238public:
241 : Error(
242 "Triangulation was finalized with 'erase...' method. Further "
243 "modification is not possible.",
244 srcLoc)
245 {}
246};
247
251class CDT_EXPORT DuplicateVertexError : public Error
252{
253public:
256 const VertInd v1,
257 const VertInd v2,
258 const SourceLocation& srcLoc)
259 : Error(
260 "Duplicate vertex detected: #" + CDT::to_string(v1) +
261 " is a duplicate of #" + CDT::to_string(v2),
262 srcLoc)
263 , m_v1(v1)
264 , m_v2(v2)
265 {}
266
267 VertInd v1() const
268 {
269 return m_v1;
270 }
271
272 VertInd v2() const
273 {
274 return m_v2;
275 }
276
277private:
278 VertInd m_v1, m_v2;
279};
280
285class CDT_EXPORT IntersectingConstraintsError : public Error
286{
287public:
290 const Edge& e1,
291 const Edge& e2,
292 const SourceLocation& srcLoc)
293 : Error(
294 "Intersecting constraint edges detected: (" +
295 CDT::to_string(e1.v1()) + ", " + CDT::to_string(e1.v2()) +
296 ") intersects (" + CDT::to_string(e2.v1()) + ", " +
297 CDT::to_string(e2.v2()) + ")",
298 srcLoc)
299 , m_e1(e1)
300 , m_e2(e2)
301 {}
302
303 const Edge& e1() const
304 {
305 return m_e1;
306 }
307
308 const Edge& e2() const
309 {
310 return m_e2;
311 }
312
313private:
314 Edge m_e1, m_e2;
315};
316
322class CDT_EXPORT InvalidEdgeSplitVertex : public Error
323{
324public:
327 const Edge& e1,
328 const Edge& e2,
329 const SourceLocation& srcLoc)
330 : Error(
331 "Intersection of constraint edges (" + CDT::to_string(e1.v1()) +
332 ", " + CDT::to_string(e1.v2()) + ") and (" +
333 CDT::to_string(e2.v1()) + ", " + CDT::to_string(e2.v2()) +
334 ") can not be resolved: computed split vertex is invalid",
335 srcLoc)
336 , m_e1(e1)
337 , m_e2(e2)
338 {}
339
340 const Edge& e1() const
341 {
342 return m_e1;
343 }
344
345 const Edge& e2() const
346 {
347 return m_e2;
348 }
349
350private:
351 Edge m_e1, m_e2;
352};
353
354class CDT_EXPORT AccessingInvalidIndex : public Error
355{
356public:
357 AccessingInvalidIndex(const SourceLocation& srcLoc)
358 : Error("Accessing invalid index", srcLoc)
359 {}
360};
361
362template <typename TIndex>
363class CDT_EXPORT OptionalIndex
364{
365public:
366 OptionalIndex(const TIndex index)
367 : m_index(index)
368 {}
369 bool hasValue() const
370 {
371 return m_index != TIndex(invalidIndexSizeType);
372 }
373 TIndex value() const
374 {
375 if(!hasValue())
376 handleException(AccessingInvalidIndex(CDT_SOURCE_LOCATION));
377 return m_index;
378 }
379
380private:
381 TIndex m_index;
382};
383
386
410
411#ifdef CDT_ENABLE_CALLBACK_HANDLER
412
416struct CDT_EXPORT TriangleChangeType
417{
427};
428
429// parameter names are used for documentation purposes, even if they are un-used
430// in the interface's default implementation
431#pragma GCC diagnostic push
432#pragma GCC diagnostic ignored "-Wunused-parameter"
433
438class CDT_EXPORT ICallbackHandler
439{
440public:
443 {}
444
450 virtual void onAddSuperTriangle()
451 {}
452
463 const TriInd iRepurposedTri,
464 const TriInd iNewTri1,
465 const TriInd iNewTri2)
466 {}
467
480 const TriInd iRepurposedTri1,
481 const TriInd iRepurposedTri2,
482 const TriInd iNewTri1,
483 const TriInd iNewTri2)
484 {}
485
491 virtual void onFlipEdge(const TriInd iT, const TriInd iTopo)
492 {}
493
499 virtual void
500 onAddVertexStart(const VertInd iV, const AddVertexType::Enum vertexType)
501 {}
502
507 virtual void onAddEdgeStart(const Edge& edge)
508 {}
509
516 virtual void onReTriangulatePolygon(const std::vector<TriInd>& tris)
517 {}
518
523 virtual bool isAbortCalculation() const
524 {
525 return false;
526 };
527};
528
529// parameter names are used for documentation purposes, even if they are
530// un-used in the interface's default implementation
531#pragma GCC diagnostic pop
532
533#endif
534
540
549template <typename T, typename TNearPointLocator = LocatorKDTree<T> >
550class CDT_EXPORT Triangulation
551{
552public:
553 typedef std::vector<V2d<T> > V2dVec;
557
565 unordered_map<Edge, BoundaryOverlapCount> overlapCount;
566
572 unordered_map<Edge, EdgeVec> pieceToOriginals;
573
574 /*____ API _____*/
581 explicit Triangulation(VertexInsertionOrder::Enum vertexInsertionOrder);
592 VertexInsertionOrder::Enum vertexInsertionOrder,
593 IntersectingConstraintEdges::Enum intersectingEdgesStrategy,
594 T minDistToConstraintEdge);
607 VertexInsertionOrder::Enum vertexInsertionOrder,
608 const TNearPointLocator& nearPtLocator,
609 IntersectingConstraintEdges::Enum intersectingEdgesStrategy,
610 T minDistToConstraintEdge);
625 template <
626 typename TVertexIter,
627 typename TGetVertexCoordX,
628 typename TGetVertexCoordY>
629 void insertVertices(
630 TVertexIter first,
631 TVertexIter last,
632 TGetVertexCoordX getX,
633 TGetVertexCoordY getY);
640 void insertVertices(const std::vector<V2d<T> >& vertices);
675 template <
676 typename TEdgeIter,
677 typename TGetEdgeVertexStart,
678 typename TGetEdgeVertexEnd>
679 void insertEdges(
680 TEdgeIter first,
681 TEdgeIter last,
682 TGetEdgeVertexStart getStart,
683 TGetEdgeVertexEnd getEnd);
709 void insertEdges(const std::vector<Edge>& edges);
744 template <
745 typename TEdgeIter,
746 typename TGetEdgeVertexStart,
747 typename TGetEdgeVertexEnd>
748 void conformToEdges(
749 TEdgeIter first,
750 TEdgeIter last,
751 TGetEdgeVertexStart getStart,
752 TGetEdgeVertexEnd getEnd);
778 void conformToEdges(const std::vector<Edge>& edges);
798 VertInd maxVerticesToInsert,
799 RefinementCriterion::Enum refinementCriterion =
801 T refinementThreshold = degToRad(T(20)),
802 TriIndUSet* toEraseOrNull = NULL,
803 T minEdgeLength = T(1e-6),
810 template <typename TQueue>
812 VertInd maxVerticesToInsert,
813 RefinementCriterion::Enum refinementCriterion =
815 T refinementThreshold = degToRad(T(20)),
816 TriIndUSet* toEraseOrNull = NULL,
817 T minEdgeLength = T(1e-6));
840 RefinementCriterion::Enum refinementCriterion =
842 T refinementThreshold = degToRad(T(20))) const;
852 void eraseOuterTriangles();
888 void finalizeTriangulation(const TriIndUSet& removedTriangles);
889
895 bool isFinalized() const;
896
910 std::vector<LayerDepth> calculateTriangleDepths() const;
911
912#ifdef CDT_ENABLE_CALLBACK_HANDLER
920 void setCallbackHandler(ICallbackHandler* callbackHandler);
921#endif
928
936 void flipEdge(TriInd iT, TriInd iTopo);
937
942 void flipEdge(
943 TriInd iT,
944 TriInd iTopo,
945 VertInd v1,
946 VertInd v2,
947 VertInd v3,
948 VertInd v4,
949 TriInd n1,
950 TriInd n2,
951 TriInd n3,
952 TriInd n4);
953
959 void removeTriangles(const TriIndUSet& removedTriangles);
960
964 const TriIndVec& VertTrisInternal() const;
966
967private:
968 /*____ Detail __*/
969 void addSuperTriangle(const Box2d<T>& box);
970 void addNewVertex(const V2d<T>& pos, TriInd iT);
971 void insertVertex(VertInd iVert);
972 void insertVertex(VertInd iVert, VertInd walkStart);
973 void ensureDelaunayByEdgeFlips(VertInd iV1, std::stack<TriInd>& triStack);
975 std::vector<Edge> insertVertex_FlipFixedEdges(VertInd iV1);
976
978 typedef tuple<IndexSizeType, IndexSizeType, TriInd, TriInd, Index>
979 TriangulatePseudoPolygonTask;
980
993 void insertEdge(
994 Edge edge,
995 Edge originalEdge,
996 EdgeVec& remaining,
997 std::vector<TriangulatePseudoPolygonTask>& tppIterations);
998
1011 void insertEdgeIteration(
1012 Edge edge,
1013 Edge originalEdge,
1014 EdgeVec& remaining,
1015 std::vector<TriangulatePseudoPolygonTask>& tppIterations);
1016
1018 typedef tuple<Edge, EdgeVec, BoundaryOverlapCount> ConformToEdgeTask;
1019
1032 void conformToEdge(
1033 Edge edge,
1034 EdgeVec originals,
1035 BoundaryOverlapCount overlaps,
1036 std::vector<ConformToEdgeTask>& remaining);
1037
1049 void conformToEdgeIteration(
1050 Edge edge,
1051 const EdgeVec& originals,
1052 BoundaryOverlapCount overlaps,
1053 std::vector<ConformToEdgeTask>& remaining);
1054
1055 tuple<TriInd, VertInd, VertInd> intersectedTriangle(
1056 VertInd iA,
1057 const V2d<T>& a,
1058 const V2d<T>& b,
1059 T orientationTolerance = T(0)) const;
1061 std::stack<TriInd> insertVertexInsideTriangle(VertInd v, TriInd iT);
1063 std::stack<TriInd> insertVertexOnEdge(
1064 VertInd v,
1065 TriInd iT1,
1066 TriInd iT2,
1067 const bool doHandleFixedSplitEdge = false);
1068 array<TriInd, 2> trianglesAt(const V2d<T>& pos) const;
1069 array<TriInd, 2>
1070 walkingSearchTrianglesAt(VertInd iV, VertInd startVertex) const;
1074 OptionalTriInd walkTriangles(VertInd startVertex, const V2d<T>& pos) const;
1077 void edgeFlipInfo(
1078 TriInd iT,
1079 VertInd iV1,
1080 TriInd& iTopo,
1081 VertInd& iV2,
1082 VertInd& iV3,
1083 VertInd& iV4,
1084 TriInd& n1,
1085 TriInd& n2,
1086 TriInd& n3,
1087 TriInd& n4);
1088 bool isFlipNeeded(
1089 VertInd iV1,
1090 VertInd iV2,
1091 VertInd iV3,
1092 VertInd iV4,
1093 const bool doFlipFixedEdges = false) const;
1095 bool isSameOriginalEdge(const Edge& e1, const Edge& e2) const;
1096 bool isRefinementNeeded(
1097 const Triangle& tri,
1098 RefinementCriterion::Enum refinementCriterion,
1099 T refinementThreshold) const;
1100 T triangleBadness(
1101 const Triangle& tri,
1102 RefinementCriterion::Enum refinementCriterion) const;
1105 bool isSmallestAngleFixed(const Triangle& tri) const;
1107 bool isEdgeEncroached(const Edge& edge) const;
1108 bool isEdgeEncroachedBy(const Edge& edge, const V2d<T>& v) const;
1113 EdgeVec edgesEncroachedBy(const V2d<T>& v, TriInd iT) const;
1115 TriIndVec resolveEncroachedEdges(
1116 EdgeQueue encroachedEdges,
1117 VertInd& newVertBudget,
1118 VertInd steinerVerticesOffset,
1119 const V2d<T>* circumcenterOrNull,
1120 RefinementCriterion::Enum refinementCriterion,
1121 T badTriangleThreshold,
1122 TriIndUSet* toEraseOrNull,
1123 T minEdgeLength,
1124 Unrefined& unrefined);
1125 OptionalVertInd splitEncroachedEdge(
1126 Edge edge,
1127 VertInd steinerVerticesOffset,
1128 TriIndUSet* toEraseOrNull,
1129 Unrefined& unrefined);
1130 void changeNeighbor(TriInd iT, TriInd oldNeighbor, TriInd newNeighbor);
1131 void changeNeighbor(
1132 TriInd iT,
1133 VertInd iVedge1,
1134 VertInd iVedge2,
1135 TriInd newNeighbor);
1136 void triangulatePseudoPolygon(
1137 const std::vector<VertInd>& poly,
1138 unordered_map<Edge, TriInd>& outerTris,
1139 TriInd iT,
1140 TriInd iN,
1141 std::vector<TriInd>& trianglesToReuse,
1142 std::vector<TriangulatePseudoPolygonTask>& iterations);
1143 void triangulatePseudoPolygonIteration(
1144 const std::vector<VertInd>& poly,
1145 unordered_map<Edge, TriInd>& outerTris,
1146 std::vector<TriInd>& trianglesToReuse,
1147 std::vector<TriangulatePseudoPolygonTask>& iterations);
1148 IndexSizeType findDelaunayPoint(
1149 const std::vector<VertInd>& poly,
1150 IndexSizeType iA,
1151 IndexSizeType iB) const;
1152 TriInd addTriangle(const Triangle& t);
1153 TriInd addTriangle();
1154 VertInd verticesCount() const;
1155 TriInd trianglesCount() const;
1156 TriIndUSet growToBoundary(std::stack<TriInd> seeds) const;
1157 void fixEdge(const Edge& edge);
1158 void fixEdge(const Edge& edge, const Edge& originalEdge);
1164 void splitFixedEdge(const Edge& edge, const VertInd iSplitVert);
1175 VertInd addSplitEdgeVertex(
1176 const Edge& edge,
1177 const V2d<T>& splitVert,
1178 const TriInd iT,
1179 const TriInd iTopo,
1180 const AddVertexType::Enum vertexType);
1193 OptionalVertInd splitFixedEdgeAt(
1194 const Edge& edge,
1195 const V2d<T>& splitVert,
1196 const TriInd iT,
1197 const TriInd iTopo,
1198 const AddVertexType::Enum vertexType);
1212 bool isEdgeSplitVertexValid(
1213 const V2d<T>& splitVert,
1214 TriInd iT,
1215 TriInd iTopo) const;
1223 Edge originalInputEdge(const Edge& e) const;
1231 const Triangle& triangleAt(TriInd iT) const;
1247 unordered_map<TriInd, LayerDepth> peelLayer(
1248 std::stack<TriInd> seeds,
1249 LayerDepth layerDepth,
1250 std::vector<LayerDepth>& triDepths) const;
1251
1252 void insertVertices_AsProvided(VertInd superGeomVertCount);
1253 void insertVertices_Randomized(VertInd superGeomVertCount);
1254 void insertVertices_KDTreeBFS(VertInd superGeomVertCount, Box2d<T> box);
1255 std::pair<TriInd, TriInd> edgeTriangles(VertInd a, VertInd b) const;
1256 bool hasEdge(VertInd a, VertInd b) const;
1258 VertInd opposedVertexOnEdge(const Edge& edge, T tolerance) const;
1259 bool
1260 hasAnotherFixedEdgeAtSmallAngle(VertInd v, const Edge& excludeEdge) const;
1261 void setAdjacentTriangle(const VertInd v, const TriInd t);
1262 void pivotVertexTriangleCW(VertInd v);
1264 void tryAddVertexToLocator(const VertInd v);
1267 void tryInitNearestPointLocator();
1268
1269 TNearPointLocator m_nearPtLocator;
1270 VertexInsertionOrder::Enum m_vertexInsertionOrder;
1271 IntersectingConstraintEdges::Enum m_intersectingEdgesStrategy;
1272 T m_minDistToConstraintEdge;
1273 TriIndVec m_vertTris;
1274#ifdef CDT_ENABLE_CALLBACK_HANDLER
1275 ICallbackHandler* m_callbackHandler;
1276#endif
1277};
1278
1281
1282namespace detail
1283{
1284
1287{
1288 typedef unsigned long long uint64;
1292 : m_state(state)
1293 {}
1294
1296 : m_state(0)
1297 {}
1298
1300 {
1301 uint64 z = (m_state += 0x9e3779b97f4a7c15);
1302 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
1303 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
1304 return z ^ (z >> 31);
1305 }
1306};
1307
1309template <class RandomIt>
1310void random_shuffle(RandomIt first, RandomIt last)
1311{
1313 typename std::iterator_traits<RandomIt>::difference_type i, n;
1314 n = last - first;
1315 for(i = n - 1; i > 0; --i)
1316 {
1317 std::swap(first[i], first[prng() % (i + 1)]);
1318 }
1319}
1320
1322template <class ForwardIt, class T>
1323void iota(ForwardIt first, ForwardIt last, T value)
1324{
1325 while(first != last)
1326 {
1327 *first++ = value;
1328 ++value;
1329 }
1330}
1331
1332} // namespace detail
1333
1334//-----------------------
1335// Triangulation methods
1336//-----------------------
1337template <typename T, typename TNearPointLocator>
1338template <
1339 typename TVertexIter,
1340 typename TGetVertexCoordX,
1341 typename TGetVertexCoordY>
1343 const TVertexIter first,
1344 const TVertexIter last,
1345 TGetVertexCoordX getX,
1346 TGetVertexCoordY getY)
1347{
1348 if(isFinalized())
1349 handleException(FinalizedError(CDT_SOURCE_LOCATION));
1350
1351 const bool isFirstTime = vertices.empty();
1352
1353 //
1354 // performance optimization: pre-allocate triangles and vertices
1355 //
1356 const std::size_t nNewVertices = std::distance(first, last);
1357 std::size_t exactCapacityTriangles = triangles.size() + 2 * nNewVertices;
1358 std::size_t exactCapacityVertices = vertices.size() + nNewVertices;
1359 if(isFirstTime) // account for adding super-triangle on the first run
1360 {
1361 exactCapacityTriangles += 1;
1362 exactCapacityVertices += nSuperTriVerts;
1363 }
1364 std::size_t capacityTriangles = exactCapacityTriangles;
1365 std::size_t capacityVertices = exactCapacityVertices;
1366 // to avoid re-allocation and unused memory
1367 // over-allocate by a fixed factor
1368 // when constraint edge intersections are resolved and vertices are many
1369 // because vertex is added for each intersection
1370 // and total number of intersections is unknown
1371 const VertInd overAllocationVerticesThreshold(1000);
1372 const std::size_t overAllocationFraction(10);
1373 const bool isOverPreAllocated =
1374 m_intersectingEdgesStrategy ==
1376 VertInd(nNewVertices) >= overAllocationVerticesThreshold;
1377 if(isOverPreAllocated)
1378 {
1379 capacityTriangles += capacityTriangles / overAllocationFraction;
1380 capacityVertices += capacityVertices / overAllocationFraction;
1381 }
1382 triangles.reserve(capacityTriangles);
1383 vertices.reserve(capacityVertices);
1384 m_vertTris.reserve(capacityVertices);
1385
1386 Box2d<T> box;
1387 if(isFirstTime)
1388 {
1389 box.envelopPoints(first, last, getX, getY);
1390 addSuperTriangle(box);
1391 }
1392 tryInitNearestPointLocator();
1393 const VertInd nExistingVerts = verticesCount();
1394
1395 for(TVertexIter it = first; it != last; ++it)
1396 addNewVertex(V2d<T>(getX(*it), getY(*it)), noNeighbor);
1397
1398 switch(m_vertexInsertionOrder)
1399 {
1401 insertVertices_AsProvided(nExistingVerts);
1402 break;
1404 isFirstTime ? insertVertices_KDTreeBFS(nExistingVerts, box)
1405 : insertVertices_Randomized(nExistingVerts);
1406 break;
1407 }
1408
1409// make sure pre-allocation was correct
1410#ifdef CDT_ENABLE_CALLBACK_HANDLER
1411 assert(
1412 !m_callbackHandler || m_callbackHandler->isAbortCalculation() ||
1413 (vertices.size() == exactCapacityVertices));
1414 assert(
1415 !m_callbackHandler || m_callbackHandler->isAbortCalculation() ||
1416 (triangles.size() == exactCapacityTriangles));
1417#else
1418 assert(vertices.size() == exactCapacityVertices);
1419 assert(triangles.size() == exactCapacityTriangles);
1420#endif
1421}
1422
1423template <typename T, typename TNearPointLocator>
1424template <
1425 typename TEdgeIter,
1426 typename TGetEdgeVertexStart,
1427 typename TGetEdgeVertexEnd>
1429 TEdgeIter first,
1430 const TEdgeIter last,
1431 TGetEdgeVertexStart getStart,
1432 TGetEdgeVertexEnd getEnd)
1433{
1434 if(isFinalized())
1435 handleException(FinalizedError(CDT_SOURCE_LOCATION));
1436
1437 std::vector<TriangulatePseudoPolygonTask> tppIterations;
1438 EdgeVec remaining;
1439 for(; first != last; ++first)
1440 {
1441#ifdef CDT_ENABLE_CALLBACK_HANDLER
1442 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
1443 {
1444 return;
1445 }
1446#endif
1447 // +3 to account for super-triangle vertices
1448 const Edge edge(
1449 VertInd(getStart(*first) + nSuperTriVerts),
1450 VertInd(getEnd(*first) + nSuperTriVerts));
1451 insertEdge(edge, edge, remaining, tppIterations);
1452 }
1453}
1454
1455template <typename T, typename TNearPointLocator>
1456template <
1457 typename TEdgeIter,
1458 typename TGetEdgeVertexStart,
1459 typename TGetEdgeVertexEnd>
1461 TEdgeIter first,
1462 const TEdgeIter last,
1463 TGetEdgeVertexStart getStart,
1464 TGetEdgeVertexEnd getEnd)
1465{
1466 if(isFinalized())
1467 handleException(FinalizedError(CDT_SOURCE_LOCATION));
1468
1469 tryInitNearestPointLocator();
1470 // state shared between different runs for performance gains
1471 std::vector<ConformToEdgeTask> remaining;
1472 for(; first != last; ++first)
1473 {
1474#ifdef CDT_ENABLE_CALLBACK_HANDLER
1475 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
1476 {
1477 return;
1478 }
1479#endif
1480 // +3 to account for super-triangle vertices
1481 const Edge e(
1482 VertInd(getStart(*first) + nSuperTriVerts),
1483 VertInd(getEnd(*first) + nSuperTriVerts));
1484 conformToEdge(e, EdgeVec(1, e), 0, remaining);
1485 }
1486}
1487
1488} // namespace CDT
1489
1490#ifndef CDT_USE_AS_COMPILED_LIBRARY
1491#include "Triangulation.hpp"
1492#endif
1493
1494#endif // header-guard
Utilities and helpers.
Adapter between for KDTree and CDT.
void random_shuffle(RandomIt first, RandomIt last)
backport from c++11
void iota(ForwardIt first, ForwardIt last, T value)
backport from c++11
Triangulation class - implementation.
VertInd v2() const
second duplicate
VertInd v1() const
first duplicate
DuplicateVertexError(const VertInd v1, const VertInd v2, const SourceLocation &srcLoc)
Constructor.
virtual ~Error() CDT_NOEXCEPT
Destructor.
const SourceLocation & sourceLocation() const
Get source location from where the error was thrown.
Error(const std::string &description, const SourceLocation &srcLoc)
Constructor.
const std::string & description() const
Get error description.
Error thrown when triangulation modification is attempted after it was finalized.
FinalizedError(const SourceLocation &srcLoc)
Constructor.
Interface for the callback handler that user can derive from and inject into the triangulation to mon...
virtual void onAddSuperTriangle()
Called when super-triangle is added.
virtual void onInsertVertexOnEdge(const TriInd iRepurposedTri1, const TriInd iRepurposedTri2, const TriInd iNewTri1, const TriInd iNewTri2)
Called when inserted vertex is on an edge.
virtual bool isAbortCalculation() const
Tells whether the user wants to abort the triangulation at the earliest opportunity.
virtual ~ICallbackHandler()
Virtual destructor.
virtual void onReTriangulatePolygon(const std::vector< TriInd > &tris)
Called when inserting a constraint edge causes polygon containing triangles to be re-triangulated @tr...
virtual void onFlipEdge(const TriInd iT, const TriInd iTopo)
Called just before an edge between tro triangles is flipped.
virtual void onInsertVertexInsideTriangle(const TriInd iRepurposedTri, const TriInd iNewTri1, const TriInd iNewTri2)
Called when inserted vertex is inside a triangle.
virtual void onAddEdgeStart(const Edge &edge)
Called at the start of adding a constraint edge to the triangulation.
virtual void onAddVertexStart(const VertInd iV, const AddVertexType::Enum vertexType)
Called at the start of adding new vertex to the triangulation.
const Edge & e1() const
first intersecting constraint
const Edge & e2() const
second intersecting constraint
IntersectingConstraintsError(const Edge &e1, const Edge &e2, const SourceLocation &srcLoc)
Constructor.
const Edge & e2() const
second intersecting constraint
const Edge & e1() const
first intersecting constraint
InvalidEdgeSplitVertex(const Edge &e1, const Edge &e2, const SourceLocation &srcLoc)
Constructor.
Contains source location info: file, function, line.
SourceLocation(const std::string &file, const std::string &func, int line)
Constructor.
const std::string & func() const
source function
int line() const
source line
const std::string & file() const
source file
EdgeUSet fixedEdges
triangulation's constraints (fixed edges)
void eraseOuterTriangles()
Erase triangles outside of constrained boundary using growing.
void conformToEdges(TEdgeIter first, TEdgeIter last, TGetEdgeVertexStart getStart, TGetEdgeVertexEnd getEnd)
Insert constraint edges into triangulation for Conforming Delaunay Triangulation (for example see fig...
std::vector< V2d< T > > V2dVec
Vertices vector.
std::vector< LayerDepth > calculateTriangleDepths() const
Calculate depth of each triangle in constraint triangulation.
TriIndUSet collectSuperTriangle() const
Collect triangles adjacent to super-triangle: same triangles that eraseSuperTriangle would remove.
bool isFinalized() const
Check if the triangulation was finalized with erase... method and super-triangle was removed.
TriIndUSet collectOuterTrianglesAndHoles() const
Collect triangles outside of constrained boundary and auto-detected holes: same triangles that eraseO...
Triangulation(VertexInsertionOrder::Enum vertexInsertionOrder)
Constructor.
TriIndVec findUnrefinedTriangles(RefinementCriterion::Enum refinementCriterion=RefinementCriterion::SmallestAngle, T refinementThreshold=degToRad(T(20))) const
Find triangles that don't fulfill the refinement criterion.
void finalizeTriangulation(const TriIndUSet &removedTriangles)
Remove super-triangle and triangles with specified indices.
void eraseOuterTrianglesAndHoles()
Erase triangles outside of constrained boundary and auto-detected holes.
V2dVec vertices
triangulation's vertices
TriIndUSet collectOuterTriangles() const
Collect triangles outside of constrained boundary: same triangles that eraseOuterTriangles would remo...
Unrefined refineTriangles(VertInd maxVerticesToInsert, RefinementCriterion::Enum refinementCriterion=RefinementCriterion::SmallestAngle, T refinementThreshold=degToRad(T(20)), TriIndUSet *toEraseOrNull=NULL, T minEdgeLength=T(1e-6), RefinementOrder::Enum refinementOrder=RefinementOrder::FIFO)
Triangles refinement by splitting bad triangles.
void insertEdges(TEdgeIter first, TEdgeIter last, TGetEdgeVertexStart getStart, TGetEdgeVertexEnd getEnd)
Insert constraint edges into triangulation for Constrained Delaunay Triangulation (for example see fi...
void insertVertices(TVertexIter first, TVertexIter last, TGetVertexCoordX getX, TGetVertexCoordY getY)
Insert custom point-types specified by iterator range and X/Y-getters.
Triangulation(VertexInsertionOrder::Enum vertexInsertionOrder, IntersectingConstraintEdges::Enum intersectingEdgesStrategy, T minDistToConstraintEdge)
Constructor.
void eraseSuperTriangle()
Erase triangles adjacent to super triangle.
unordered_map< Edge, EdgeVec > pieceToOriginals
Stores list of original edges represented by a given fixed edge.
Triangulation(VertexInsertionOrder::Enum vertexInsertionOrder, const TNearPointLocator &nearPtLocator, IntersectingConstraintEdges::Enum intersectingEdgesStrategy, T minDistToConstraintEdge)
Constructor.
unordered_map< Edge, BoundaryOverlapCount > overlapCount
Stores count of overlapping boundaries for a fixed edge.
TriangleVec triangles
triangulation's triangles
Triangulation()
Default constructor.
EdgeVec findEncroachedFixedEdges() const
Find all fixed edges encroached by their opposed vertices.
void setCallbackHandler(ICallbackHandler *callbackHandler)
Set user-provided callback handler.
unsigned short LayerDepth
Type used for storing layer depths for triangles.
Definition CDT.h:39
OptionalIndex< VertInd > OptionalVertInd
Optional vertex index.
OptionalIndex< TriInd > OptionalTriInd
Optional triangle index.
void flipEdge(TriInd iT, TriInd iTopo)
Flip an edge between two triangle.
TriIndVec & VertTrisInternal()
Access internal vertex adjacent triangles.
void removeTriangles(const TriIndUSet &removedTriangles)
Remove triangles with specified indices.
Namespace containing triangulation functionality.
std::vector< Edge > EdgeVec
Vector of edges.
Definition CDTUtils.h:399
unordered_set< Edge > EdgeUSet
Hash table of edges.
Definition CDTUtils.h:402
CDT_EXPORT T degToRad(T degrees)
Convert an angle from degrees to radians.
Definition CDTUtils.hpp:330
std::vector< TriInd > TriIndVec
Vector of triangle indices.
Definition CDTUtils.h:271
IndexSizeType VertInd
Vertex index.
Definition CDTUtils.h:253
unordered_set< TriInd > TriIndUSet
Hash table of triangles.
Definition CDTUtils.h:403
IndexSizeType TriInd
Triangle index.
Definition CDTUtils.h:255
std::vector< Triangle > TriangleVec
Vector of triangles.
Definition CDTUtils.h:466
std::queue< Edge > EdgeQueue
Queue of edges.
Definition CDTUtils.h:400
What type of vertex is added to the triangulation.
Enum
The Enum itself.
@ RefinementCircumcenter
Refinement: circumcenter of a poor-quality triangle.
@ UserInput
Original vertex from user input.
@ RefinementEdgeSplit
Refinement: split of an encroached fixed edge.
@ FixedEdgeMidpoint
During conforming triangulation edge mid-point is added.
@ FixedEdgesIntersection
Resolving fixed/constraint edges' intersection.
2D bounding box
Definition CDTUtils.h:278
Box2d< T > & envelopPoints(TVertexIter first, TVertexIter last, TGetVertexCoordX getX, TGetVertexCoordY getY)
Envelop box around a collection of custom points.
Definition CDTUtils.h:309
Edge connecting two vertices: vertex with smaller index is always first.
Definition CDTUtils.h:333
FIFO queue with top() like std::priority_queue
Enum of strategies for treating intersecting constraint edges.
@ TryResolve
attempt to resolve constraint edge intersections
@ NotAllowed
constraint edge intersections are not allowed
@ DontCheck
No checks: slightly faster but less safe.
Enum of strategies for triangles refinement.
@ LargestArea
constraint maximum triangles area
@ SmallestAngle
constraint minimum triangles angle
Enum of bad triangles refinement orders.
Enum
The Enum itself.
@ FIFO
in the order found
@ WorstFirst
worst first
What type of triangle change happened.
@ AddedNew
new triangle added to the triangulation
@ ModifiedExisting
existing triangle was modified
Triangulation triangle (counter-clockwise winding)
Definition CDTUtils.h:415
Counts of the refinements that Triangulation::refineTriangles was not able to perform.
std::size_t circumcenterOnVertex
triangles whose circumcenter coincides with an existing vertex
std::size_t sharpFixedCorner
triangles whose smallest angle is enclosed by two fixed edges: such an angle comes from the input and...
std::size_t splitVertexInvalid
fixed edges whose split vertex can not be placed: inserting it would break the triangulation's topolo...
std::size_t circumcenterOutside
triangles whose circumcenter is outside the triangulated area
std::size_t shortEdges
fixed edges that are shorter than the threshold
std::size_t shortEdgeTriangles
triangles whose shortest edge is shorter than the threshold
Unrefined()
Constructor: all the counts start at zero.
2D vector
Definition CDTUtils.h:191
Enum of strategies specifying order in which a range of vertices is inserted.
@ AsProvided
insert vertices in same order they are provided
@ Auto
Automatic insertion order optimized for better performance.
SplitMix64 pseudo-random number generator.
SplitMix64RandGen()
default constructor
uint64 operator()()
functor's operator
unsigned long long uint64
uint64 type
SplitMix64RandGen(uint64 state)
constructor