10#ifndef CDT_vW1vZ0lO8rS4gY4uI4fB
11#define CDT_vW1vZ0lO8rS4gY4uI4fB
116 return this->front();
173 const std::string&
file()
const
178 const std::string&
func()
const
195#define CDT_SOURCE_LOCATION \
196 SourceLocation(std::string(__FILE__), std::string(__func__), __LINE__)
202class CDT_EXPORT
Error :
public std::runtime_error
207 : std::runtime_error(
208 description +
"\nin '" + srcLoc.func() +
"' at " + srcLoc.file() +
209 ":" +
CDT::to_string(srcLoc.line()))
219 return m_description;
228 std::string m_description;
242 "Triangulation was finalized with 'erase...' method. Further "
243 "modification is not possible.",
260 "Duplicate vertex detected: #" +
CDT::to_string(
v1) +
261 " is a duplicate of #" +
CDT::to_string(
v2),
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()) +
")",
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",
354class CDT_EXPORT AccessingInvalidIndex :
public Error
358 :
Error(
"Accessing invalid index", srcLoc)
362template <
typename TIndex>
363class CDT_EXPORT OptionalIndex
366 OptionalIndex(
const TIndex index)
369 bool hasValue()
const
371 return m_index != TIndex(invalidIndexSizeType);
411#ifdef CDT_ENABLE_CALLBACK_HANDLER
431#pragma GCC diagnostic push
432#pragma GCC diagnostic ignored "-Wunused-parameter"
463 const TriInd iRepurposedTri,
480 const TriInd iRepurposedTri1,
481 const TriInd iRepurposedTri2,
531#pragma GCC diagnostic pop
549template <
typename T,
typename TNearPo
intLocator = LocatorKDTree<T> >
594 T minDistToConstraintEdge);
608 const TNearPointLocator& nearPtLocator,
610 T minDistToConstraintEdge);
626 typename TVertexIter,
627 typename TGetVertexCoordX,
628 typename TGetVertexCoordY>
632 TGetVertexCoordX getX,
633 TGetVertexCoordY getY);
677 typename TGetEdgeVertexStart,
678 typename TGetEdgeVertexEnd>
682 TGetEdgeVertexStart getStart,
683 TGetEdgeVertexEnd getEnd);
746 typename TGetEdgeVertexStart,
747 typename TGetEdgeVertexEnd>
751 TGetEdgeVertexStart getStart,
752 TGetEdgeVertexEnd getEnd);
801 T refinementThreshold =
degToRad(T(20)),
803 T minEdgeLength = T(1e-6),
810 template <
typename TQueue>
815 T refinementThreshold =
degToRad(T(20)),
817 T minEdgeLength = T(1e-6));
842 T refinementThreshold =
degToRad(T(20)))
const;
912#ifdef CDT_ENABLE_CALLBACK_HANDLER
969 void addSuperTriangle(
const Box2d<T>& box);
971 void insertVertex(
VertInd iVert);
973 void ensureDelaunayByEdgeFlips(
VertInd iV1, std::stack<TriInd>& triStack);
975 std::vector<Edge> insertVertex_FlipFixedEdges(
VertInd iV1);
978 typedef tuple<IndexSizeType, IndexSizeType, TriInd, TriInd, Index>
979 TriangulatePseudoPolygonTask;
997 std::vector<TriangulatePseudoPolygonTask>& tppIterations);
1011 void insertEdgeIteration(
1015 std::vector<TriangulatePseudoPolygonTask>& tppIterations);
1018 typedef tuple<Edge, EdgeVec, BoundaryOverlapCount> ConformToEdgeTask;
1035 BoundaryOverlapCount overlaps,
1036 std::vector<ConformToEdgeTask>& remaining);
1049 void conformToEdgeIteration(
1052 BoundaryOverlapCount overlaps,
1053 std::vector<ConformToEdgeTask>& remaining);
1055 tuple<TriInd, VertInd, VertInd> intersectedTriangle(
1059 T orientationTolerance = T(0))
const;
1061 std::stack<TriInd> insertVertexInsideTriangle(
VertInd v,
TriInd iT);
1063 std::stack<TriInd> insertVertexOnEdge(
1067 const bool doHandleFixedSplitEdge =
false);
1068 array<TriInd, 2> trianglesAt(
const V2d<T>& pos)
const;
1093 const bool doFlipFixedEdges =
false)
const;
1095 bool isSameOriginalEdge(
const Edge& e1,
const Edge& e2)
const;
1096 bool isRefinementNeeded(
1099 T refinementThreshold)
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;
1118 VertInd steinerVerticesOffset,
1119 const V2d<T>* circumcenterOrNull,
1121 T badTriangleThreshold,
1127 VertInd steinerVerticesOffset,
1131 void changeNeighbor(
1136 void triangulatePseudoPolygon(
1137 const std::vector<VertInd>& poly,
1138 unordered_map<Edge, TriInd>& outerTris,
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,
1151 IndexSizeType iB)
const;
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);
1212 bool isEdgeSplitVertexValid(
1223 Edge originalInputEdge(
const Edge& e)
const;
1247 unordered_map<TriInd, LayerDepth> peelLayer(
1248 std::stack<TriInd> seeds,
1250 std::vector<LayerDepth>& triDepths)
const;
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;
1258 VertInd opposedVertexOnEdge(
const Edge& edge, T tolerance)
const;
1260 hasAnotherFixedEdgeAtSmallAngle(
VertInd v,
const Edge& excludeEdge)
const;
1262 void pivotVertexTriangleCW(
VertInd v);
1264 void tryAddVertexToLocator(
const VertInd v);
1267 void tryInitNearestPointLocator();
1269 TNearPointLocator m_nearPtLocator;
1272 T m_minDistToConstraintEdge;
1274#ifdef CDT_ENABLE_CALLBACK_HANDLER
1302 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
1303 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
1304 return z ^ (z >> 31);
1309template <
class RandomIt>
1313 typename std::iterator_traits<RandomIt>::difference_type i, n;
1315 for(i = n - 1; i > 0; --i)
1317 std::swap(first[i], first[prng() % (i + 1)]);
1322template <
class ForwardIt,
class T>
1323void iota(ForwardIt first, ForwardIt last, T value)
1325 while(first != last)
1337template <
typename T,
typename TNearPo
intLocator>
1339 typename TVertexIter,
1340 typename TGetVertexCoordX,
1341 typename TGetVertexCoordY>
1343 const TVertexIter first,
1344 const TVertexIter last,
1345 TGetVertexCoordX getX,
1346 TGetVertexCoordY getY)
1351 const bool isFirstTime =
vertices.empty();
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;
1361 exactCapacityTriangles += 1;
1362 exactCapacityVertices += nSuperTriVerts;
1364 std::size_t capacityTriangles = exactCapacityTriangles;
1365 std::size_t capacityVertices = exactCapacityVertices;
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)
1379 capacityTriangles += capacityTriangles / overAllocationFraction;
1380 capacityVertices += capacityVertices / overAllocationFraction;
1383 vertices.reserve(capacityVertices);
1384 m_vertTris.reserve(capacityVertices);
1390 addSuperTriangle(box);
1392 tryInitNearestPointLocator();
1393 const VertInd nExistingVerts = verticesCount();
1395 for(TVertexIter it = first; it != last; ++it)
1396 addNewVertex(
V2d<T>(getX(*it), getY(*it)), noNeighbor);
1398 switch(m_vertexInsertionOrder)
1401 insertVertices_AsProvided(nExistingVerts);
1404 isFirstTime ? insertVertices_KDTreeBFS(nExistingVerts, box)
1405 : insertVertices_Randomized(nExistingVerts);
1410#ifdef CDT_ENABLE_CALLBACK_HANDLER
1412 !m_callbackHandler || m_callbackHandler->isAbortCalculation() ||
1413 (
vertices.size() == exactCapacityVertices));
1415 !m_callbackHandler || m_callbackHandler->isAbortCalculation() ||
1416 (
triangles.size() == exactCapacityTriangles));
1418 assert(
vertices.size() == exactCapacityVertices);
1419 assert(
triangles.size() == exactCapacityTriangles);
1423template <
typename T,
typename TNearPo
intLocator>
1426 typename TGetEdgeVertexStart,
1427 typename TGetEdgeVertexEnd>
1430 const TEdgeIter last,
1431 TGetEdgeVertexStart getStart,
1432 TGetEdgeVertexEnd getEnd)
1437 std::vector<TriangulatePseudoPolygonTask> tppIterations;
1439 for(; first != last; ++first)
1441#ifdef CDT_ENABLE_CALLBACK_HANDLER
1442 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
1449 VertInd(getStart(*first) + nSuperTriVerts),
1450 VertInd(getEnd(*first) + nSuperTriVerts));
1451 insertEdge(edge, edge, remaining, tppIterations);
1455template <
typename T,
typename TNearPo
intLocator>
1458 typename TGetEdgeVertexStart,
1459 typename TGetEdgeVertexEnd>
1462 const TEdgeIter last,
1463 TGetEdgeVertexStart getStart,
1464 TGetEdgeVertexEnd getEnd)
1469 tryInitNearestPointLocator();
1471 std::vector<ConformToEdgeTask> remaining;
1472 for(; first != last; ++first)
1474#ifdef CDT_ENABLE_CALLBACK_HANDLER
1475 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
1482 VertInd(getStart(*first) + nSuperTriVerts),
1483 VertInd(getEnd(*first) + nSuperTriVerts));
1484 conformToEdge(e,
EdgeVec(1, e), 0, remaining);
1490#ifndef CDT_USE_AS_COMPILED_LIBRARY
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.
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.
unordered_set< Edge > EdgeUSet
Hash table of edges.
CDT_EXPORT T degToRad(T degrees)
Convert an angle from degrees to radians.
std::vector< TriInd > TriIndVec
Vector of triangle indices.
IndexSizeType VertInd
Vertex index.
unordered_set< TriInd > TriIndUSet
Hash table of triangles.
IndexSizeType TriInd
Triangle index.
std::vector< Triangle > TriangleVec
Vector of triangles.
std::queue< Edge > EdgeQueue
Queue of edges.
What type of vertex is added to the triangulation.
@ 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.
Box2d< T > & envelopPoints(TVertexIter first, TVertexIter last, TGetVertexCoordX getX, TGetVertexCoordY getY)
Envelop box around a collection of custom points.
Edge connecting two vertices: vertex with smaller index is always first.
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.
What type of triangle change happened.
@ AddedNew
new triangle added to the triangulation
@ ModifiedExisting
existing triangle was modified
Triangulation triangle (counter-clockwise winding)
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.
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.
uint64 m_state
PRNG's state.
SplitMix64RandGen()
default constructor
uint64 operator()()
functor's operator
unsigned long long uint64
uint64 type
SplitMix64RandGen(uint64 state)
constructor