10#ifndef CDT_vW1vZ0lO8rS4gY4uI4fB
11#define CDT_vW1vZ0lO8rS4gY4uI4fB
108template <
typename T,
typename TNearPo
intLocator = LocatorKDTree<T> >
153 T minDistToConstraintEdge);
167 const TNearPointLocator& nearPtLocator,
169 T minDistToConstraintEdge);
183 typename TVertexIter,
184 typename TGetVertexCoordX,
185 typename TGetVertexCoordY>
189 TGetVertexCoordX getX,
190 TGetVertexCoordY getY);
195 void insertVertices(
const std::vector<
V2d<T> >& vertices);
218 typename TGetEdgeVertexStart,
219 typename TGetEdgeVertexEnd>
223 TGetEdgeVertexStart getStart,
224 TGetEdgeVertexEnd getEnd);
236 void insertEdges(
const std::vector<Edge>& edges);
259 typename TGetEdgeVertexStart,
260 typename TGetEdgeVertexEnd>
264 TGetEdgeVertexStart getStart,
265 TGetEdgeVertexEnd getEnd);
277 void conformToEdges(
const std::vector<Edge>& edges);
283 void eraseSuperTriangle();
285 void eraseOuterTriangles();
292 void eraseOuterTrianglesAndHoles();
297 void initializedWithCustomSuperGeometry();
304 bool isFinalized()
const;
319 std::vector<LayerDepth> calculateTriangleDepths()
const;
354 void removeTriangles(
const TriIndUSet& removedTriangles);
362 void addSuperTriangle(
const Box2d<T>& box);
364 void insertVertex(
VertInd iVert);
366 void ensureDelaunayByEdgeFlips(
369 std::stack<TriInd>& triStack);
371 std::vector<Edge> insertVertex_FlipFixedEdges(
VertInd iV1);
374 typedef tuple<IndexSizeType, IndexSizeType, TriInd, TriInd, Index>
375 TriangulatePseudopolygonTask;
393 std::vector<TriangulatePseudopolygonTask>& tppIterations);
407 void insertEdgeIteration(
411 std::vector<TriangulatePseudopolygonTask>& tppIterations);
417 IndexSizeType previousEdgeEncounter(
419 const std::vector<VertInd>& poly)
const;
422 typedef tuple<Edge, EdgeVec, BoundaryOverlapCount> ConformToEdgeTask;
439 BoundaryOverlapCount overlaps,
440 std::vector<ConformToEdgeTask>& remaining);
453 void conformToEdgeIteration(
456 BoundaryOverlapCount overlaps,
457 std::vector<ConformToEdgeTask>& remaining);
459 tuple<TriInd, VertInd, VertInd> intersectedTriangle(
463 T orientationTolerance = T(0))
const;
465 std::stack<TriInd> insertVertexInsideTriangle(
VertInd v,
TriInd iT);
468 array<TriInd, 2> trianglesAt(
const V2d<T>& pos)
const;
470 walkingSearchTrianglesAt(
const V2d<T>& pos,
VertInd startVertex)
const;
497 void triangulatePseudopolygon(
498 const std::vector<VertInd>& poly,
499 const std::vector<TriInd>& outerTris,
502 std::vector<TriangulatePseudopolygonTask>& iterations);
503 void triangulatePseudopolygonIteration(
504 const std::vector<VertInd>& poly,
505 const std::vector<TriInd>& outerTris,
506 std::vector<TriangulatePseudopolygonTask>& iterations);
507 IndexSizeType findDelaunayPoint(
508 const std::vector<VertInd>& poly,
510 IndexSizeType iB)
const;
518 void finalizeTriangulation(
const TriIndUSet& removedTriangles);
519 TriIndUSet growToBoundary(std::stack<TriInd> seeds)
const;
520 void fixEdge(
const Edge& edge, BoundaryOverlapCount overlaps);
521 void fixEdge(
const Edge& edge);
522 void fixEdge(
const Edge& edge,
const Edge& originalEdge);
529 void makeDummy(
TriInd iT);
551 unordered_map<TriInd, LayerDepth> peelLayer(
552 std::stack<TriInd> seeds,
554 std::vector<LayerDepth>& triDepths)
const;
556 void insertVertices_AsProvided(
VertInd superGeomVertCount);
557 void insertVertices_Randomized(
VertInd superGeomVertCount);
558 void insertVertices_KDTreeBFS(
564 void pivotVertexTriangleCW(
VertInd v);
565 void removeAdjacentTriangle(
VertInd v);
567 void tryAddVertexToLocator(
const VertInd v);
570 void tryInitNearestPointLocator();
572 std::vector<TriInd> m_dummyTris;
573 TNearPointLocator m_nearPtLocator;
574 std::size_t m_nTargetVerts;
578 T m_minDistToConstraintEdge;
591 typedef unsigned long long uint64;
601 uint64 z = (m_state += 0x9e3779b97f4a7c15);
602 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
603 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
604 return z ^ (z >> 31);
608template <
class RandomIt>
609void random_shuffle(RandomIt first, RandomIt last)
612 typename std::iterator_traits<RandomIt>::difference_type i, n;
614 for(i = n - 1; i > 0; --i)
616 std::swap(first[i], first[prng() % (i + 1)]);
621template <
class ForwardIt,
class T>
622void iota(ForwardIt first, ForwardIt last, T value)
636template <
typename T,
typename TNearPo
intLocator>
638 typename TVertexIter,
639 typename TGetVertexCoordX,
640 typename TGetVertexCoordY>
642 const TVertexIter first,
643 const TVertexIter last,
644 TGetVertexCoordX getX,
645 TGetVertexCoordY getY)
649 throw std::runtime_error(
650 "Triangulation was finalized with 'erase...' method. Inserting new "
651 "vertices is not possible");
654 const bool isFirstTime = vertices.empty();
658 box = envelopBox<T>(first, last, getX, getY);
659 addSuperTriangle(box);
661 tryInitNearestPointLocator();
663 const VertInd nExistingVerts(vertices.size());
664 const VertInd nVerts(nExistingVerts + std::distance(first, last));
666 triangles.reserve(triangles.size() + 2 * nVerts);
667 vertices.reserve(nVerts);
668 m_vertTris.reserve(nVerts);
669 for(TVertexIter it = first; it != last; ++it)
670 addNewVertex(
V2d<T>::make(getX(*it), getY(*it)), noNeighbor);
672 switch(m_vertexInsertionOrder)
675 insertVertices_AsProvided(nExistingVerts);
678 isFirstTime ? insertVertices_KDTreeBFS(nExistingVerts, box.
min, box.
max)
679 : insertVertices_Randomized(nExistingVerts);
684template <
typename T,
typename TNearPo
intLocator>
687 typename TGetEdgeVertexStart,
688 typename TGetEdgeVertexEnd>
691 const TEdgeIter last,
692 TGetEdgeVertexStart getStart,
693 TGetEdgeVertexEnd getEnd)
696 std::vector<TriangulatePseudopolygonTask> tppIterations;
700 throw std::runtime_error(
701 "Triangulation was finalized with 'erase...' method. Inserting new "
702 "edges is not possible");
704 for(; first != last; ++first)
708 VertInd(getStart(*first) + m_nTargetVerts),
709 VertInd(getEnd(*first) + m_nTargetVerts));
710 insertEdge(edge, edge, remaining, tppIterations);
715template <
typename T,
typename TNearPo
intLocator>
718 typename TGetEdgeVertexStart,
719 typename TGetEdgeVertexEnd>
722 const TEdgeIter last,
723 TGetEdgeVertexStart getStart,
724 TGetEdgeVertexEnd getEnd)
728 throw std::runtime_error(
729 "Triangulation was finalized with 'erase...' method. Conforming to "
730 "new edges is not possible");
732 tryInitNearestPointLocator();
734 std::vector<ConformToEdgeTask> remaining;
735 for(; first != last; ++first)
739 VertInd(getStart(*first) + m_nTargetVerts),
740 VertInd(getEnd(*first) + m_nTargetVerts));
741 conformToEdge(e,
EdgeVec(1, e), 0, remaining);
748#ifndef CDT_USE_AS_COMPILED_LIBRARY
#define CDT_EXPORT
Export not needed in header-only mode.
Adapter between for KDTree and CDT.
Triangulation class - implementation.
Data structure representing a 2D constrained Delaunay triangulation.
EdgeUSet fixedEdges
triangulation's constraints (fixed edges)
void conformToEdges(TEdgeIter first, TEdgeIter last, TGetEdgeVertexStart getStart, TGetEdgeVertexEnd getEnd)
Ensure that triangulation conforms to constraints (fixed edges)
std::vector< V2d< T > > V2dVec
Vertices vector.
V2dVec vertices
triangulation's vertices
void insertEdges(TEdgeIter first, TEdgeIter last, TGetEdgeVertexStart getStart, TGetEdgeVertexEnd getEnd)
Insert constraints (custom-type fixed edges) into triangulation.
void insertVertices(TVertexIter first, TVertexIter last, TGetVertexCoordX getX, TGetVertexCoordY getY)
Insert custom point-types specified by iterator range and X/Y-getters.
unordered_map< Edge, EdgeVec > pieceToOriginals
Stores list of original edges represented by a given fixed edge.
unordered_map< Edge, BoundaryOverlapCount > overlapCount
Stores count of overlapping boundaries for a fixed edge.
TriangleVec triangles
triangulation's triangles
unsigned short LayerDepth
Type used for storing layer depths for triangles.
Namespace containing triangulation functionality.
std::vector< Edge > EdgeVec
Vector of edges.
unordered_set< Edge > EdgeUSet
Hash table of edges.
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.
V2d< T > max
max box corner
V2d< T > min
min box corner
Edge connecting two vertices: vertex with smaller index is always first.
Enum of strategies for treating intersecting constraint edges.
@ Ignore
constraint edge intersections are not checked
@ Resolve
constraint edge intersections are resolved
Enum of what type of geometry used to embed triangulation into.
@ SuperTriangle
conventional super-triangle
@ Custom
user-specified custom geometry (e.g., grid)
Triangulation triangle (counter-clockwise winding)
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.