CDT  2.1.0
C++ library for constrained Delaunay triangulation
Loading...
Searching...
No Matches
Triangulation.hpp
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#ifndef CDT_pDqrlveWIOrIWeUCkPqX
10#define CDT_pDqrlveWIOrIWeUCkPqX
11
12#include "Triangulation.h"
13#include "portable_nth_element.hpp"
14
15#include <algorithm>
16#include <cassert>
17#include <cmath>
18#include <deque>
19#include <stdexcept>
20
21CDT_ENSURE_PRECISE_MATH_FOR_CONSTRUCTIONS
22
23namespace CDT
24{
25
26typedef std::deque<TriInd> TriDeque;
27
28namespace detail
29{
30
32inline EdgeQueue toQueue(const EdgeVec& edges)
33{
34 return EdgeQueue(EdgeQueue::container_type(edges.begin(), edges.end()));
35}
36
38template <typename TVec>
39void sortUnique(TVec& v)
40{
41 std::sort(v.begin(), v.end());
42 v.erase(std::unique(v.begin(), v.end()), v.end());
43}
44
45namespace defaults
46{
47
48const VertexInsertionOrder::Enum vertexInsertionOrder =
50const IntersectingConstraintEdges::Enum intersectingEdgesStrategy =
52const float minDistToConstraintEdge(0);
53
54} // namespace defaults
55
56} // namespace detail
57
58CDT_INLINE_IF_HEADER_ONLY Unrefined::Unrefined()
63 , shortEdges(0)
65{}
66
67template <typename T, typename TNearPointLocator>
69 : m_vertexInsertionOrder(detail::defaults::vertexInsertionOrder)
70 , m_intersectingEdgesStrategy(detail::defaults::intersectingEdgesStrategy)
71 , m_minDistToConstraintEdge(detail::defaults::minDistToConstraintEdge)
72#ifdef CDT_ENABLE_CALLBACK_HANDLER
73 , m_callbackHandler(NULL)
74#endif
75{}
76
77template <typename T, typename TNearPointLocator>
79 const VertexInsertionOrder::Enum vertexInsertionOrder)
80 : m_vertexInsertionOrder(vertexInsertionOrder)
81 , m_intersectingEdgesStrategy(detail::defaults::intersectingEdgesStrategy)
82 , m_minDistToConstraintEdge(detail::defaults::minDistToConstraintEdge)
83#ifdef CDT_ENABLE_CALLBACK_HANDLER
84 , m_callbackHandler(NULL)
85#endif
86{}
87
88template <typename T, typename TNearPointLocator>
90 const VertexInsertionOrder::Enum vertexInsertionOrder,
91 const IntersectingConstraintEdges::Enum intersectingEdgesStrategy,
92 const T minDistToConstraintEdge)
93 : m_vertexInsertionOrder(vertexInsertionOrder)
94 , m_intersectingEdgesStrategy(intersectingEdgesStrategy)
95 , m_minDistToConstraintEdge(minDistToConstraintEdge)
96#ifdef CDT_ENABLE_CALLBACK_HANDLER
97 , m_callbackHandler(NULL)
98#endif
99{}
100
101template <typename T, typename TNearPointLocator>
103 const VertexInsertionOrder::Enum vertexInsertionOrder,
104 const TNearPointLocator& nearPtLocator,
105 const IntersectingConstraintEdges::Enum intersectingEdgesStrategy,
106 const T minDistToConstraintEdge)
107 : m_nearPtLocator(nearPtLocator)
108 , m_vertexInsertionOrder(vertexInsertionOrder)
109 , m_intersectingEdgesStrategy(intersectingEdgesStrategy)
110 , m_minDistToConstraintEdge(minDistToConstraintEdge)
111#ifdef CDT_ENABLE_CALLBACK_HANDLER
112 , m_callbackHandler(NULL)
113#endif
114{}
115
116template <typename T, typename TNearPointLocator>
118{
119 finalizeTriangulation(collectSuperTriangle());
120}
121
122template <typename T, typename TNearPointLocator>
127
128template <typename T, typename TNearPointLocator>
133
134template <typename T, typename TNearPointLocator>
136{
137 if(isFinalized())
138 handleException(FinalizedError(CDT_SOURCE_LOCATION));
139
140 // find triangles adjacent to super-triangle's vertices
141 TriIndUSet toErase;
142 for(TriInd iT(0); iT < TriInd(triangles.size()); ++iT)
143 {
145 toErase.insert(iT);
146 }
147 return toErase;
148}
149
150template <typename T, typename TNearPointLocator>
152{
153 if(isFinalized())
154 handleException(FinalizedError(CDT_SOURCE_LOCATION));
155
156 assert(m_vertTris[0] != noNeighbor);
157 const std::stack<TriInd> seed(std::deque<TriInd>(1, m_vertTris[0]));
158 return growToBoundary(seed);
159}
160
161template <typename T, typename TNearPointLocator>
164{
165 if(isFinalized())
166 handleException(FinalizedError(CDT_SOURCE_LOCATION));
167
168 const std::vector<LayerDepth> triDepths = calculateTriangleDepths();
169 TriIndUSet toErase;
170 toErase.reserve(triangles.size());
171 for(std::size_t iT = 0; iT != triangles.size(); ++iT)
172 {
173 if(triDepths[iT] % 2 == 0)
174 toErase.insert(static_cast<TriInd>(iT));
175 }
176 return toErase;
177}
178
181{
182 return Edge(
183 VertInd(e.v1() - nSuperTriVerts), VertInd(e.v2() - nSuperTriVerts));
184}
185
186template <typename T, typename TNearPointLocator>
188 const TriIndUSet& removedTriangles)
189{
190 if(removedTriangles.empty())
191 return;
192 // remove triangles and calculate triangle index mapping
193 TriIndUMap triIndMap;
194 for(TriInd iT(0), iTnew(0); iT < TriInd(triangles.size()); ++iT)
195 {
196 if(removedTriangles.count(iT))
197 continue;
198 triIndMap[iT] = iTnew;
199 triangles[iTnew] = triangles[iT];
200 iTnew++;
201 }
202 triangles.erase(triangles.end() - removedTriangles.size(), triangles.end());
203 // adjust triangles' neighbors
204 for(TriInd iT(0); iT < triangles.size(); ++iT)
205 {
206 Triangle& t = triangles[iT];
207 // update neighbors to account for removed triangles
208 NeighborsArr3& nn = t.neighbors;
209 for(NeighborsArr3::iterator n = nn.begin(); n != nn.end(); ++n)
210 {
211 if(removedTriangles.count(*n))
212 {
213 *n = noNeighbor;
214 }
215 else if(*n != noNeighbor)
216 {
217 *n = triIndMap[*n];
218 }
219 }
220 }
221}
222
223template <typename T, typename TNearPointLocator>
228
229template <typename T, typename TNearPointLocator>
231{
232 return m_vertTris;
233}
234
235template <typename T, typename TNearPointLocator>
237 const TriIndUSet& removedTriangles)
238{
239 if(isFinalized())
240 handleException(FinalizedError(CDT_SOURCE_LOCATION));
241
242 m_vertTris = TriIndVec();
243 // remove super-triangle
244 vertices.erase(vertices.begin(), vertices.begin() + nSuperTriVerts);
245 // Edge re-mapping
246 { // fixed edges
247 EdgeUSet updatedFixedEdges;
248 typedef CDT::EdgeUSet::const_iterator It;
249 for(It e = fixedEdges.begin(); e != fixedEdges.end(); ++e)
250 {
251 updatedFixedEdges.insert(RemapNoSuperTriangle(*e));
252 }
253 fixedEdges = updatedFixedEdges;
254 }
255 { // overlap count
256 unordered_map<Edge, BoundaryOverlapCount> updatedOverlapCount;
257 typedef unordered_map<Edge, BoundaryOverlapCount>::const_iterator It;
258 for(It it = overlapCount.begin(); it != overlapCount.end(); ++it)
259 {
260 updatedOverlapCount.insert(
261 std::make_pair(RemapNoSuperTriangle(it->first), it->second));
262 }
263 overlapCount = updatedOverlapCount;
264 }
265 { // split edges mapping
266 unordered_map<Edge, EdgeVec> updatedPieceToOriginals;
267 typedef unordered_map<Edge, EdgeVec>::const_iterator It;
268 for(It it = pieceToOriginals.begin(); it != pieceToOriginals.end();
269 ++it)
270 {
271 EdgeVec ee = it->second;
272 for(EdgeVec::iterator eeIt = ee.begin(); eeIt != ee.end(); ++eeIt)
273 {
274 *eeIt = RemapNoSuperTriangle(*eeIt);
275 }
276 updatedPieceToOriginals.insert(
277 std::make_pair(RemapNoSuperTriangle(it->first), ee));
278 }
279 pieceToOriginals = updatedPieceToOriginals;
280 }
281 // remove other triangles
282 removeTriangles(removedTriangles);
283 // adjust triangle vertices: account for removed super-triangle
284 for(TriangleVec::iterator t = triangles.begin(); t != triangles.end(); ++t)
285 {
286 VerticesArr3& vv = t->vertices;
287 for(VerticesArr3::iterator v = vv.begin(); v != vv.end(); ++v)
288 {
289 *v -= nSuperTriVerts;
290 }
291 }
292}
293
294template <typename T, typename TNearPointLocator>
295TriIndUSet Triangulation<T, TNearPointLocator>::growToBoundary(
296 std::stack<TriInd> seeds) const
297{
298 TriIndUSet traversed;
299 while(!seeds.empty())
300 {
301 const TriInd iT = seeds.top();
302 seeds.pop();
303 traversed.insert(iT);
304 const Triangle& t = triangles[iT];
305 for(Index i(0); i < Index(3); ++i)
306 {
307 const Edge opEdge(t.vertices[ccw(i)], t.vertices[cw(i)]);
308 if(fixedEdges.count(opEdge))
309 continue;
310 const TriInd iN = t.neighbors[opoNbr(i)];
311 if(iN != noNeighbor && traversed.count(iN) == 0)
312 seeds.push(iN);
313 }
314 }
315 return traversed;
316}
317
318template <typename T, typename TNearPointLocator>
319TriInd Triangulation<T, TNearPointLocator>::addTriangle(const Triangle& t)
320{
321 const TriInd iT = trianglesCount();
322 triangles.push_back(t);
323 return iT;
324}
325
326template <typename T, typename TNearPointLocator>
327TriInd Triangulation<T, TNearPointLocator>::addTriangle()
328{
329 return addTriangle(Triangle());
330}
331
332template <typename T, typename TNearPointLocator>
333VertInd Triangulation<T, TNearPointLocator>::verticesCount() const
334{
335 return static_cast<VertInd>(vertices.size());
336}
337
338template <typename T, typename TNearPointLocator>
339TriInd Triangulation<T, TNearPointLocator>::trianglesCount() const
340{
341 return static_cast<TriInd>(triangles.size());
342}
343
344template <typename T, typename TNearPointLocator>
346 const std::vector<Edge>& edges)
347{
348 insertEdges(edges.begin(), edges.end(), edge_get_v1, edge_get_v2);
349}
350
351template <typename T, typename TNearPointLocator>
353 const std::vector<Edge>& edges)
354{
355 conformToEdges(edges.begin(), edges.end(), edge_get_v1, edge_get_v2);
356}
357
358template <typename T, typename TNearPointLocator>
359void Triangulation<T, TNearPointLocator>::fixEdge(const Edge& edge)
360{
361 if(!fixedEdges.insert(edge).second)
362 {
363 ++overlapCount[edge]; // if edge is already fixed increment the counter
364 }
365}
366
367namespace detail
368{
369
370// add element to 'to' if not already in 'to'
371template <typename T, typename Allocator1>
372void insert_unique(std::vector<T, Allocator1>& to, const T& elem)
373{
374 if(std::find(to.begin(), to.end(), elem) == to.end())
375 {
376 to.push_back(elem);
377 }
378}
379
380// add elements of 'from' that are not present in 'to' to 'to'
381template <typename T, typename Allocator1, typename Allocator2>
382void insert_unique(
383 std::vector<T, Allocator1>& to,
384 const std::vector<T, Allocator2>& from)
385{
386 typedef typename std::vector<T, Allocator2>::const_iterator Cit;
387 to.reserve(to.size() + from.size());
388 for(Cit cit = from.begin(); cit != from.end(); ++cit)
389 {
390 insert_unique(to, *cit);
391 }
392}
393
394} // namespace detail
395
396template <typename T, typename TNearPointLocator>
397void Triangulation<T, TNearPointLocator>::splitFixedEdge(
398 const Edge& edge,
399 const VertInd iSplitVert)
400{
401 // split constraint (fixed) edge that already exists in triangulation
402 const Edge half1(edge.v1(), iSplitVert);
403 const Edge half2(iSplitVert, edge.v2());
404 // remove the edge that and add its halves
405 fixedEdges.erase(edge);
406 fixEdge(half1);
407 fixEdge(half2);
408 // maintain overlaps
409 typedef unordered_map<Edge, BoundaryOverlapCount>::const_iterator It;
410 const It overlapIt = overlapCount.find(edge);
411 if(overlapIt != overlapCount.end())
412 {
413 overlapCount[half1] += overlapIt->second;
414 overlapCount[half2] += overlapIt->second;
415 overlapCount.erase(overlapIt);
416 }
417 // maintain piece-to-original mapping
418 EdgeVec newOriginals(1, edge);
419 const unordered_map<Edge, EdgeVec>::const_iterator originalsIt =
420 pieceToOriginals.find(edge);
421 if(originalsIt != pieceToOriginals.end())
422 { // edge being split was split before: pass-through originals
423 newOriginals = originalsIt->second;
424 pieceToOriginals.erase(originalsIt);
425 }
426 detail::insert_unique(pieceToOriginals[half1], newOriginals);
427 detail::insert_unique(pieceToOriginals[half2], newOriginals);
428}
429
430template <typename T, typename TNearPointLocator>
431VertInd Triangulation<T, TNearPointLocator>::addSplitEdgeVertex(
432 const Edge& edge,
433 const V2d<T>& splitVert,
434 const TriInd iT,
435 const TriInd iTopo,
436 const AddVertexType::Enum vertexType)
437{
438 // add a new point on the edge that splits an edge in two
439 const VertInd iSplitVert = verticesCount();
440 addNewVertex(splitVert, noNeighbor);
441
442#ifdef CDT_ENABLE_CALLBACK_HANDLER
443 if(m_callbackHandler)
444 {
445 m_callbackHandler->onAddVertexStart(iSplitVert, vertexType);
446 }
447#else
448 (void)vertexType;
449#endif
450
451 std::stack<TriInd> triStack = insertVertexOnEdge(iSplitVert, iT, iTopo);
452 // before the flips: isFlipNeeded needs the halves to be fixed
453 splitFixedEdge(edge, iSplitVert);
454 tryAddVertexToLocator(iSplitVert);
455 ensureDelaunayByEdgeFlips(iSplitVert, triStack);
456 return iSplitVert;
457}
458
459template <typename T, typename TNearPointLocator>
460OptionalVertInd Triangulation<T, TNearPointLocator>::splitFixedEdgeAt(
461 const Edge& edge,
462 const V2d<T>& splitVert,
463 const TriInd iT,
464 const TriInd iTopo,
465 const AddVertexType::Enum vertexType)
466{
467 if(!isEdgeSplitVertexValid(splitVert, iT, iTopo))
468 return OptionalVertInd(noVertex);
469 const VertInd iSplitVert =
470 addSplitEdgeVertex(edge, splitVert, iT, iTopo, vertexType);
471 return OptionalVertInd(iSplitVert);
472}
473
474template <typename T, typename TNearPointLocator>
475bool Triangulation<T, TNearPointLocator>::isEdgeSplitVertexValid(
476 const V2d<T>& splitVert,
477 const TriInd iT,
478 const TriInd iTopo) const
479{
480 // quadrilateral of the two triangles, CCW: v2 and v4 end the split edge
481 const Triangle& t1 = triangles[iT];
482 const Index i = opposedVertexInd(t1.neighbors, iTopo);
483 const V2d<T>& v1 = vertices[t1.vertices[i]];
484 const V2d<T>& v2 = vertices[t1.vertices[ccw(i)]];
485 const V2d<T>& v4 = vertices[t1.vertices[cw(i)]];
486 const Triangle& t2 = triangles[iTopo];
487 const V2d<T>& v3 =
488 vertices[t2.vertices[opposedVertexInd(t2.neighbors, iT)]];
489 return locatePointLine(splitVert, v1, v2) == PtLineLocation::Left &&
490 locatePointLine(splitVert, v2, v3) == PtLineLocation::Left &&
491 locatePointLine(splitVert, v3, v4) == PtLineLocation::Left &&
492 locatePointLine(splitVert, v4, v1) == PtLineLocation::Left;
493}
494
495template <typename T, typename TNearPointLocator>
496Edge Triangulation<T, TNearPointLocator>::originalInputEdge(const Edge& e) const
497{
498 const Edge orig =
499 pieceToOriginals.count(e) ? pieceToOriginals.at(e).front() : e;
500 return Edge(
501 VertInd(orig.v1() - nSuperTriVerts),
502 VertInd(orig.v2() - nSuperTriVerts));
503}
504
505template <typename T, typename TNearPointLocator>
506const Triangle&
507Triangulation<T, TNearPointLocator>::triangleAt(const TriInd iT) const
508{
509 if(iT >= TriInd(triangles.size()))
510 handleException(Error(
511 iT == noNeighbor
512 ? "Attempted reading no-neighbor sentinel value triangle"
513 : "Triangle index " + CDT::to_string(iT) + " out of range " +
514 CDT::to_string(triangles.size()),
515 CDT_SOURCE_LOCATION));
516 return triangles[iT];
517}
518
519template <typename T, typename TNearPointLocator>
520void Triangulation<T, TNearPointLocator>::fixEdge(
521 const Edge& edge,
522 const Edge& originalEdge)
523{
524 fixEdge(edge);
525 if(edge != originalEdge)
526 detail::insert_unique(pieceToOriginals[edge], originalEdge);
527}
528
529namespace detail
530{
531
532template <typename T>
533T lerp(const T& a, const T& b, const T t)
534{
535 return (T(1) - t) * a + t * b;
536}
537
539template <typename T>
540bool isAngleAtApexSmall(const V2d<T>& apex, const V2d<T>& a, const V2d<T>& b)
541{
542 const T ux = a.x - apex.x, uy = a.y - apex.y;
543 const T wx = b.x - apex.x, wy = b.y - apex.y;
544 const T dot = ux * wx + uy * wy;
545 // cos(60°) = 0.5
546 return dot > T(0) && T(2) * dot > distance(apex, a) * distance(apex, b);
547}
548
549// Precondition: ab and cd intersect normally
550template <typename T>
551V2d<T> intersectionPosition(
552 const V2d<T>& a,
553 const V2d<T>& b,
554 const V2d<T>& c,
555 const V2d<T>& d)
556{
557 // note: for better accuracy we interpolate x and y separately
558 // on a segment with the shortest x/y-projection correspondingly
559 const T a_cd = predicates::orient2d(c.x, c.y, d.x, d.y, a.x, a.y);
560 const T b_cd = predicates::orient2d(c.x, c.y, d.x, d.y, b.x, b.y);
561 const T t_ab = a_cd / (a_cd - b_cd);
562
563 const T c_ab = predicates::orient2d(a.x, a.y, b.x, b.y, c.x, c.y);
564 const T d_ab = predicates::orient2d(a.x, a.y, b.x, b.y, d.x, d.y);
565 const T t_cd = c_ab / (c_ab - d_ab);
566
567 return V2d<T>(
568 std::fabs(a.x - b.x) < std::fabs(c.x - d.x) ? lerp(a.x, b.x, t_ab)
569 : lerp(c.x, d.x, t_cd),
570 std::fabs(a.y - b.y) < std::fabs(c.y - d.y) ? lerp(a.y, b.y, t_ab)
571 : lerp(c.y, d.y, t_cd));
572}
573
574} // namespace detail
575
576template <typename T, typename TNearPointLocator>
577void Triangulation<T, TNearPointLocator>::insertEdgeIteration(
578 const Edge edge,
579 const Edge originalEdge,
580 EdgeVec& remaining,
581 std::vector<TriangulatePseudoPolygonTask>& tppIterations)
582{
583 const VertInd iA = edge.v1();
584 VertInd iB = edge.v2();
585 if(iA == iB) // edge connects a vertex to itself
586 return;
587
588 const V2d<T>& a = vertices[iA];
589 const V2d<T>& b = vertices[iB];
590 const T distanceTolerance =
591 m_minDistToConstraintEdge == T(0)
592 ? T(0)
593 : m_minDistToConstraintEdge * distance(a, b);
594
595 if(hasEdge(iA, iB))
596 {
597 const VertInd iV = opposedVertexOnEdge(edge, distanceTolerance);
598 if(iV != noVertex)
599 {
600 remaining.push_back(Edge(iV, iB));
601 remaining.push_back(Edge(iA, iV));
602 return;
603 }
604 fixEdge(edge, originalEdge);
605 return;
606 }
607
608 TriInd iT;
609 // Note: 'L' is left and 'R' is right of the inserted constraint edge
610 VertInd iVL, iVR;
611 tie(iT, iVL, iVR) = intersectedTriangle(iA, a, b, distanceTolerance);
612 // if one of the triangle vertices is on the edge, split edge at it
613 if(iT == noNeighbor)
614 {
615 remaining.push_back(Edge(iVL, iB));
616 remaining.push_back(Edge(iA, iVL));
617 return;
618 }
619 Triangle t = triangles[iT];
620 std::vector<TriInd> intersected(1, iT);
621 std::vector<VertInd> polyL, polyR;
622 polyL.reserve(2);
623 polyL.push_back(iA);
624 polyL.push_back(iVL);
625 polyR.reserve(2);
626 polyR.push_back(iA);
627 polyR.push_back(iVR);
628 unordered_map<Edge, TriInd> outerTris;
629 outerTris[Edge(iA, iVL)] = edgeNeighbor(t, iA, iVL);
630 outerTris[Edge(iA, iVR)] = edgeNeighbor(t, iA, iVR);
631 VertInd iV = iA;
632
633 while(!t.containsVertex(iB))
634 {
635 const TriInd iTopo = opposedTriangle(t, iV);
636 const Triangle& tOpo = triangleAt(iTopo);
637 const VertInd iVopo = opposedVertex(tOpo, iT);
638
639 switch(m_intersectingEdgesStrategy)
640 {
642 if(fixedEdges.count(Edge(iVL, iVR)))
643 handleException(IntersectingConstraintsError(
644 originalInputEdge(originalEdge),
645 originalInputEdge(Edge(iVL, iVR)),
646 CDT_SOURCE_LOCATION));
647 break;
649 {
650 if(!fixedEdges.count(Edge(iVL, iVR)))
651 break;
652 // split edge at the intersection of two constraint edges
653 const V2d<T> newV = detail::intersectionPosition(
654 vertices[iA], vertices[iB], vertices[iVL], vertices[iVR]);
655 const OptionalVertInd splitVert = splitFixedEdgeAt(
656 Edge(iVL, iVR),
657 newV,
658 iT,
659 iTopo,
661 if(!splitVert.hasValue())
662 handleException(InvalidEdgeSplitVertex(
663 originalInputEdge(originalEdge),
664 originalInputEdge(Edge(iVL, iVR)),
665 CDT_SOURCE_LOCATION));
666 const VertInd iNewVert = splitVert.value();
667 // TODO: is it's possible to re-use pseudo-polygons
668 // for inserting [iA, iNewVert] edge half?
669 remaining.push_back(Edge(iA, iNewVert));
670 remaining.push_back(Edge(iNewVert, iB));
671 return;
672 }
674 assert(!fixedEdges.count(Edge(iVL, iVR)));
675 break;
676 }
677
678 const PtLineLocation::Enum loc =
679 locatePointLine(vertices[iVopo], a, b, distanceTolerance);
680 if(loc == PtLineLocation::Left)
681 {
682 const Edge e(polyL.back(), iVopo);
683 const TriInd outer = edgeNeighbor(tOpo, e.v1(), e.v2());
684 if(!outerTris.insert(std::make_pair(e, outer)).second)
685 outerTris.at(e) = noNeighbor; // hanging edge detected
686 polyL.push_back(iVopo);
687 iV = iVL;
688 iVL = iVopo;
689 }
690 else if(loc == PtLineLocation::Right)
691 {
692 const Edge e(polyR.back(), iVopo);
693 const TriInd outer = edgeNeighbor(tOpo, e.v1(), e.v2());
694 if(!outerTris.insert(std::make_pair(e, outer)).second)
695 outerTris.at(e) = noNeighbor; // hanging edge detected
696 polyR.push_back(iVopo);
697 iV = iVR;
698 iVR = iVopo;
699 }
700 else // encountered point on the edge
701 iB = iVopo;
702
703 intersected.push_back(iTopo);
704 iT = iTopo;
705 t = triangles[iT];
706 }
707 outerTris[Edge(polyL.back(), iB)] = edgeNeighbor(t, polyL.back(), iB);
708 outerTris[Edge(polyR.back(), iB)] = edgeNeighbor(t, polyR.back(), iB);
709 polyL.push_back(iB);
710 polyR.push_back(iB);
711
712 assert(!intersected.empty());
713 // make sure start/end vertices have a valid adjacent triangle
714 // that is not intersected by an edge
715 if(m_vertTris[iA] == intersected.front())
716 pivotVertexTriangleCW(iA);
717 if(m_vertTris[iB] == intersected.back())
718 pivotVertexTriangleCW(iB);
719
720 {
721#ifdef CDT_ENABLE_CALLBACK_HANDLER
722 if(m_callbackHandler)
723 {
724 m_callbackHandler->onReTriangulatePolygon(intersected);
725 }
726#endif
727
728 // Triangulate pseudo-polygons on both sides
729 std::reverse(polyR.begin(), polyR.end());
730
731 // note: intersected triangles are re-used for new triangles
732 // every triangulation of an n-gon has n − 2 triangles
733 // even if outer polygon has hanging edges it holds
734 assert(intersected.size() >= 2);
735 const TriInd iTL = intersected.back();
736 intersected.pop_back();
737 const TriInd iTR = intersected.back();
738 intersected.pop_back();
739
740 triangulatePseudoPolygon(
741 polyL, outerTris, iTL, iTR, intersected, tppIterations);
742 triangulatePseudoPolygon(
743 polyR, outerTris, iTR, iTL, intersected, tppIterations);
744 assert(intersected.empty());
745 }
746
747 if(iB != edge.v2()) // encountered point on the edge
748 {
749 // fix edge part
750 const Edge edgePart(iA, iB);
751 fixEdge(edgePart, originalEdge);
752 remaining.push_back(Edge(iB, edge.v2()));
753 return;
754 }
755 else
756 {
757 fixEdge(edge, originalEdge);
758 }
759}
760
761template <typename T, typename TNearPointLocator>
762void Triangulation<T, TNearPointLocator>::insertEdge(
763 Edge edge,
764 const Edge originalEdge,
765 EdgeVec& remaining,
766 std::vector<TriangulatePseudoPolygonTask>& tppIterations)
767{
768#ifdef CDT_ENABLE_CALLBACK_HANDLER
769 if(m_callbackHandler)
770 {
771 m_callbackHandler->onAddEdgeStart(edge);
772 }
773#endif
774
775 // use iteration over recursion to avoid stack overflows
776 remaining.clear();
777 remaining.push_back(edge);
778 while(!remaining.empty())
779 {
780 edge = remaining.back();
781 remaining.pop_back();
782 insertEdgeIteration(edge, originalEdge, remaining, tppIterations);
783 }
784}
785
786template <typename T, typename TNearPointLocator>
787void Triangulation<T, TNearPointLocator>::conformToEdgeIteration(
788 Edge edge,
789 const EdgeVec& originals,
790 BoundaryOverlapCount overlaps,
791 std::vector<ConformToEdgeTask>& remaining)
792{
793 const VertInd iA = edge.v1();
794 VertInd iB = edge.v2();
795 if(iA == iB) // edge connects a vertex to itself
796 return;
797
798 const V2d<T>& a = vertices[iA];
799 const V2d<T>& b = vertices[iB];
800 const T distanceTolerance =
801 m_minDistToConstraintEdge == T(0)
802 ? T(0)
803 : m_minDistToConstraintEdge * distance(a, b);
804
805 if(hasEdge(iA, iB))
806 {
807 const VertInd iV = opposedVertexOnEdge(edge, distanceTolerance);
808 if(iV != noVertex)
809 {
810#ifdef CDT_CXX11_IS_SUPPORTED
811 remaining.emplace_back(Edge(iV, iB), originals, overlaps);
812 remaining.emplace_back(Edge(iA, iV), originals, overlaps);
813#else
814 remaining.push_back(make_tuple(Edge(iV, iB), originals, overlaps));
815 remaining.push_back(make_tuple(Edge(iA, iV), originals, overlaps));
816#endif
817 return;
818 }
819 fixEdge(edge);
820 if(overlaps > 0)
821 overlapCount[edge] = overlaps;
822 // avoid marking edge as a part of itself
823 if(!originals.empty() && edge != originals.front())
824 {
825 detail::insert_unique(pieceToOriginals[edge], originals);
826 }
827 return;
828 }
829
830 TriInd iT;
831 VertInd iVleft, iVright;
832 tie(iT, iVleft, iVright) = intersectedTriangle(iA, a, b, distanceTolerance);
833 // if one of the triangle vertices is on the edge, split edge at it
834 if(iT == noNeighbor)
835 {
836#ifdef CDT_CXX11_IS_SUPPORTED
837 remaining.emplace_back(Edge(iVleft, iB), originals, overlaps);
838 remaining.emplace_back(Edge(iA, iVleft), originals, overlaps);
839#else
840 remaining.push_back(make_tuple(Edge(iVleft, iB), originals, overlaps));
841 remaining.push_back(make_tuple(Edge(iA, iVleft), originals, overlaps));
842#endif
843 return;
844 }
845
846 VertInd iV = iA;
847 Triangle t = triangles[iT];
848 while(std::find(t.vertices.begin(), t.vertices.end(), iB) ==
849 t.vertices.end())
850 {
851 const TriInd iTopo = opposedTriangle(t, iV);
852 const Triangle& tOpo = triangleAt(iTopo);
853 const VertInd iVopo = opposedVertex(tOpo, iT);
854 const V2d<T> vOpo = vertices[iVopo];
855
856 switch(m_intersectingEdgesStrategy)
857 {
859 if(fixedEdges.count(Edge(iVleft, iVright)))
860 handleException(IntersectingConstraintsError(
861 originalInputEdge(edge),
862 originalInputEdge(Edge(iVleft, iVright)),
863 CDT_SOURCE_LOCATION));
864 break;
866 {
867 if(!fixedEdges.count(Edge(iVleft, iVright)))
868 break;
869 // split edge at the intersection of two constraint edges
870 const V2d<T> newV = detail::intersectionPosition(
871 vertices[iA],
872 vertices[iB],
873 vertices[iVleft],
874 vertices[iVright]);
875 const OptionalVertInd splitVert = splitFixedEdgeAt(
876 Edge(iVleft, iVright),
877 newV,
878 iT,
879 iTopo,
881 if(!splitVert.hasValue())
882 handleException(InvalidEdgeSplitVertex(
883 originalInputEdge(edge),
884 originalInputEdge(Edge(iVleft, iVright)),
885 CDT_SOURCE_LOCATION));
886 const VertInd iNewVert = splitVert.value();
887#ifdef CDT_CXX11_IS_SUPPORTED
888 remaining.emplace_back(Edge(iNewVert, iB), originals, overlaps);
889 remaining.emplace_back(Edge(iA, iNewVert), originals, overlaps);
890#else
891 remaining.push_back(
892 make_tuple(Edge(iNewVert, iB), originals, overlaps));
893 remaining.push_back(
894 make_tuple(Edge(iA, iNewVert), originals, overlaps));
895#endif
896 return;
897 }
899 assert(!fixedEdges.count(Edge(iVleft, iVright)));
900 break;
901 }
902
903 iT = iTopo;
904 t = triangles[iT];
905
906 const PtLineLocation::Enum loc =
907 locatePointLine(vOpo, a, b, distanceTolerance);
908 if(loc == PtLineLocation::Left)
909 {
910 iV = iVleft;
911 iVleft = iVopo;
912 }
913 else if(loc == PtLineLocation::Right)
914 {
915 iV = iVright;
916 iVright = iVopo;
917 }
918 else // encountered point on the edge
919 iB = iVopo;
920 }
921
922 // encountered one or more points on the edge: add remaining edge part
923 if(iB != edge.v2())
924 {
925#ifdef CDT_CXX11_IS_SUPPORTED
926 remaining.emplace_back(Edge(iB, edge.v2()), originals, overlaps);
927#else
928 remaining.push_back(
929 make_tuple(Edge(iB, edge.v2()), originals, overlaps));
930#endif
931 }
932
933 // add mid-point to triangulation
934 const VertInd iMid = verticesCount();
935 const V2d<T>& start = vertices[iA];
936 const V2d<T>& end = vertices[iB];
937 addNewVertex(
938 V2d<T>((start.x + end.x) / T(2), (start.y + end.y) / T(2)), noNeighbor);
939
940#ifdef CDT_ENABLE_CALLBACK_HANDLER
941 if(m_callbackHandler)
942 {
943 m_callbackHandler->onAddVertexStart(
945 }
946#endif
947
948 const std::vector<Edge> flippedFixedEdges =
949 insertVertex_FlipFixedEdges(iMid);
950
951#ifdef CDT_CXX11_IS_SUPPORTED
952 remaining.emplace_back(Edge(iMid, iB), originals, overlaps);
953 remaining.emplace_back(Edge(iA, iMid), originals, overlaps);
954#else
955 remaining.push_back(make_tuple(Edge(iMid, iB), originals, overlaps));
956 remaining.push_back(make_tuple(Edge(iA, iMid), originals, overlaps));
957#endif
958
959 // re-introduce fixed edges that were flipped
960 // and make sure overlap count is preserved
961 for(std::vector<Edge>::const_iterator it = flippedFixedEdges.begin();
962 it != flippedFixedEdges.end();
963 ++it)
964 {
965 const Edge& flippedFixedEdge = *it;
966 fixedEdges.erase(flippedFixedEdge);
967
968 BoundaryOverlapCount prevOverlaps = 0;
969 const unordered_map<Edge, BoundaryOverlapCount>::const_iterator
970 overlapsIt = overlapCount.find(flippedFixedEdge);
971 if(overlapsIt != overlapCount.end())
972 {
973 prevOverlaps = overlapsIt->second;
974 overlapCount.erase(overlapsIt);
975 }
976 // override overlapping boundaries count when re-inserting an edge
977 EdgeVec prevOriginals(1, flippedFixedEdge);
978 const unordered_map<Edge, EdgeVec>::const_iterator originalsIt =
979 pieceToOriginals.find(flippedFixedEdge);
980 if(originalsIt != pieceToOriginals.end())
981 {
982 prevOriginals = originalsIt->second;
983 }
984#ifdef CDT_CXX11_IS_SUPPORTED
985 remaining.emplace_back(flippedFixedEdge, prevOriginals, prevOverlaps);
986#else
987 remaining.push_back(
988 make_tuple(flippedFixedEdge, prevOriginals, prevOverlaps));
989#endif
990 }
991}
992
993template <typename T, typename TNearPointLocator>
994void Triangulation<T, TNearPointLocator>::conformToEdge(
995 Edge edge,
996 EdgeVec originals,
997 BoundaryOverlapCount overlaps,
998 std::vector<ConformToEdgeTask>& remaining)
999{
1000#ifdef CDT_ENABLE_CALLBACK_HANDLER
1001 if(m_callbackHandler)
1002 {
1003 m_callbackHandler->onAddEdgeStart(edge);
1004 }
1005#endif
1006
1007 // use iteration over recursion to avoid stack overflows
1008 remaining.clear();
1009#ifdef CDT_CXX11_IS_SUPPORTED
1010 remaining.emplace_back(edge, originals, overlaps);
1011#else
1012 remaining.push_back(make_tuple(edge, originals, overlaps));
1013#endif
1014 while(!remaining.empty())
1015 {
1016 tie(edge, originals, overlaps) = remaining.back();
1017 remaining.pop_back();
1018 conformToEdgeIteration(edge, originals, overlaps, remaining);
1019 }
1020}
1021
1032template <typename T, typename TNearPointLocator>
1033tuple<TriInd, VertInd, VertInd>
1034Triangulation<T, TNearPointLocator>::intersectedTriangle(
1035 const VertInd iA,
1036 const V2d<T>& a,
1037 const V2d<T>& b,
1038 const T orientationTolerance) const
1039{
1040 const TriInd startTri = m_vertTris[iA];
1041 TriInd iT = startTri;
1042 do
1043 {
1044 const Triangle t = triangles[iT];
1045 const Index i = vertexInd(t.vertices, iA);
1046 const VertInd iP2 = t.vertices[ccw(i)];
1047 const T orientP2 = orient2D(vertices[iP2], a, b);
1048 const PtLineLocation::Enum locP2 = classifyOrientation(orientP2);
1049 if(locP2 == PtLineLocation::Right)
1050 {
1051 const VertInd iP1 = t.vertices[cw(i)];
1052 const T orientP1 = orient2D(vertices[iP1], a, b);
1053 const PtLineLocation::Enum locP1 = classifyOrientation(orientP1);
1054 if(locP1 == PtLineLocation::OnLine)
1055 {
1056 return make_tuple(noNeighbor, iP1, iP1);
1057 }
1058 if(locP1 == PtLineLocation::Left)
1059 {
1060 if(orientationTolerance)
1061 {
1062 T closestOrient;
1063 VertInd iClosestP;
1064 if(std::abs(orientP1) <= std::abs(orientP2))
1065 {
1066 closestOrient = orientP1;
1067 iClosestP = iP1;
1068 }
1069 else
1070 {
1071 closestOrient = orientP2;
1072 iClosestP = iP2;
1073 }
1075 closestOrient, orientationTolerance) ==
1076 PtLineLocation::OnLine)
1077 {
1078 return make_tuple(noNeighbor, iClosestP, iClosestP);
1079 }
1080 }
1081 return make_tuple(iT, iP1, iP2);
1082 }
1083 }
1084 iT = t.next(iA).first;
1085 } while(iT != startTri);
1086
1087 handleException(Error(
1088 "Could not find vertex triangle intersected by an edge.",
1089 CDT_SOURCE_LOCATION));
1090 return make_tuple(noNeighbor, noVertex, noVertex);
1091}
1092
1093template <typename T, typename TNearPointLocator>
1094void Triangulation<T, TNearPointLocator>::addSuperTriangle(const Box2d<T>& box)
1095{
1096 const V2d<T> center(
1097 (box.min.x + box.max.x) / T(2), (box.min.y + box.max.y) / T(2));
1098 const T w = box.max.x - box.min.x;
1099 const T h = box.max.y - box.min.y;
1100 T r = std::max(w, h); // incircle radius upper bound
1101
1102 // Note: make sure radius is big enough. Constants chosen experimentally.
1103 // - for tiny bounding boxes: use 1.0 as the smallest radius
1104 // - multiply radius by 2.0 for extra safety margin
1105 r = std::max(T(2) * r, T(1));
1106
1107 // Note: for very large floating point numbers rounding can lead to wrong
1108 // super-triangle coordinates. This is a very rare corner-case so the
1109 // handling is very primitive.
1110 { // note: '<=' means '==' but avoids the warning
1111 while(center.y <= center.y - r)
1112 r *= T(2);
1113 }
1114
1115 const T R = T(2) * r; // excircle radius
1116 const T cos_30_deg = T(0.8660254037844386); // note: (std::sqrt(3.0) / 2.0)
1117 const T shiftX = R * cos_30_deg;
1118 const V2d<T> posV1(center.x - shiftX, center.y - r);
1119 const V2d<T> posV2(center.x + shiftX, center.y - r);
1120 const V2d<T> posV3(center.x, center.y + R);
1121 addNewVertex(posV1, TriInd(0));
1122 addNewVertex(posV2, TriInd(0));
1123 addNewVertex(posV3, TriInd(0));
1124
1125#ifdef CDT_ENABLE_CALLBACK_HANDLER
1126 if(m_callbackHandler)
1127 {
1128 m_callbackHandler->onAddSuperTriangle();
1129 }
1130#endif
1131
1132 addTriangle(
1133 Triangle(arr3(VertInd(0), VertInd(1), VertInd(2)), arr3(noNeighbor)));
1134
1135 if(m_vertexInsertionOrder != VertexInsertionOrder::Auto)
1136 {
1137 m_nearPtLocator.initialize(vertices);
1138 }
1139}
1140
1141template <typename T, typename TNearPointLocator>
1142void Triangulation<T, TNearPointLocator>::addNewVertex(
1143 const V2d<T>& pos,
1144 const TriInd iT)
1145{
1146 vertices.push_back(pos);
1147 m_vertTris.push_back(iT);
1148}
1149
1150template <typename T, typename TNearPointLocator>
1151std::vector<Edge>
1152Triangulation<T, TNearPointLocator>::insertVertex_FlipFixedEdges(
1153 const VertInd iV1)
1154{
1155 std::vector<Edge> flippedFixedEdges;
1156
1157 const V2d<T>& v1 = vertices[iV1];
1158 const VertInd startVertex = m_nearPtLocator.nearPoint(v1, vertices);
1159 array<TriInd, 2> trisAt = walkingSearchTrianglesAt(iV1, startVertex);
1160 std::stack<TriInd> triStack =
1161 trisAt[1] == noNeighbor ? insertVertexInsideTriangle(iV1, trisAt[0])
1162 : insertVertexOnEdge(iV1, trisAt[0], trisAt[1]);
1163
1164 TriInd iTopo, n1, n2, n3, n4;
1165 VertInd iV2, iV3, iV4;
1166 while(!triStack.empty())
1167 {
1168 const TriInd iT = triStack.top();
1169 triStack.pop();
1170
1171 edgeFlipInfo(iT, iV1, iTopo, iV2, iV3, iV4, n1, n2, n3, n4);
1172 if(iTopo != noNeighbor && isFlipNeeded(iV1, iV2, iV3, iV4, true))
1173 {
1174 // if flipped edge is fixed, remember it
1175 const Edge flippedEdge(iV2, iV4);
1176 if(!fixedEdges.empty() &&
1177 fixedEdges.find(flippedEdge) != fixedEdges.end())
1178 {
1179 flippedFixedEdges.push_back(flippedEdge);
1180 }
1181
1182 flipEdge(iT, iTopo, iV1, iV2, iV3, iV4, n1, n2, n3, n4);
1183 triStack.push(iT);
1184 triStack.push(iTopo);
1185 }
1186 }
1187
1188 tryAddVertexToLocator(iV1);
1189 return flippedFixedEdges;
1190}
1191
1192template <typename T, typename TNearPointLocator>
1193void Triangulation<T, TNearPointLocator>::insertVertex(
1194 const VertInd iVert,
1195 const VertInd walkStart)
1196{
1197#ifdef CDT_ENABLE_CALLBACK_HANDLER
1198 if(m_callbackHandler)
1199 {
1200 m_callbackHandler->onAddVertexStart(iVert, AddVertexType::UserInput);
1201 }
1202#endif
1203
1204 const array<TriInd, 2> trisAt = walkingSearchTrianglesAt(iVert, walkStart);
1205 std::stack<TriInd> triStack =
1206 trisAt[1] == noNeighbor
1207 ? insertVertexInsideTriangle(iVert, trisAt[0])
1208 : insertVertexOnEdge(iVert, trisAt[0], trisAt[1], true);
1209 ensureDelaunayByEdgeFlips(iVert, triStack);
1210}
1211
1212template <typename T, typename TNearPointLocator>
1213void Triangulation<T, TNearPointLocator>::insertVertex(const VertInd iVert)
1214{
1215 const V2d<T>& v = vertices[iVert];
1216 const VertInd walkStart = m_nearPtLocator.nearPoint(v, vertices);
1217 insertVertex(iVert, walkStart);
1218 tryAddVertexToLocator(iVert);
1219}
1220
1221template <typename T, typename TNearPointLocator>
1222void Triangulation<T, TNearPointLocator>::ensureDelaunayByEdgeFlips(
1223 const VertInd iV1,
1224 std::stack<TriInd>& triStack)
1225{
1226 TriInd iTopo, n1, n2, n3, n4;
1227 VertInd iV2, iV3, iV4;
1228 while(!triStack.empty())
1229 {
1230 const TriInd iT = triStack.top();
1231 triStack.pop();
1232
1233 edgeFlipInfo(iT, iV1, iTopo, iV2, iV3, iV4, n1, n2, n3, n4);
1234 if(iTopo != noNeighbor && isFlipNeeded(iV1, iV2, iV3, iV4))
1235 {
1236 flipEdge(iT, iTopo, iV1, iV2, iV3, iV4, n1, n2, n3, n4);
1237 triStack.push(iT);
1238 triStack.push(iTopo);
1239 }
1240 }
1241}
1242
1243/*
1244 * v4 original edge: (v1, v3)
1245 * /|\ flip-candidate edge: (v, v2)
1246 * / | \
1247 * n3 / | \ n4
1248 * / | \
1249 * new vertex--> v1 T | Topo v3
1250 * \ | /
1251 * n1 \ | / n2
1252 * \ | /
1253 * \|/
1254 * v2
1255 */
1256template <typename T, typename TNearPointLocator>
1257void Triangulation<T, TNearPointLocator>::edgeFlipInfo(
1258 const TriInd iT,
1259 const VertInd iV1,
1260 TriInd& iTopo,
1261 VertInd& iV2,
1262 VertInd& iV3,
1263 VertInd& iV4,
1264 TriInd& n1,
1265 TriInd& n2,
1266 TriInd& n3,
1267 TriInd& n4)
1268{
1269 /* v[2]
1270 / \
1271 n[2]/ \n[1]
1272 /_____\
1273 v[0] n[0] v[1] */
1274 const Triangle& t = triangles[iT];
1275 if(t.vertices[0] == iV1)
1276 {
1277 iV2 = t.vertices[1];
1278 iV4 = t.vertices[2];
1279 n1 = t.neighbors[0];
1280 n3 = t.neighbors[2];
1281 iTopo = t.neighbors[1];
1282 }
1283 else if(t.vertices[1] == iV1)
1284 {
1285 iV2 = t.vertices[2];
1286 iV4 = t.vertices[0];
1287 n1 = t.neighbors[1];
1288 n3 = t.neighbors[0];
1289 iTopo = t.neighbors[2];
1290 }
1291 else
1292 {
1293 iV2 = t.vertices[0];
1294 iV4 = t.vertices[1];
1295 n1 = t.neighbors[2];
1296 n3 = t.neighbors[1];
1297 iTopo = t.neighbors[0];
1298 }
1299 if(iTopo == noNeighbor)
1300 return;
1301 const Triangle& tOpo = triangles[iTopo];
1302 if(tOpo.neighbors[0] == iT)
1303 {
1304 iV3 = tOpo.vertices[2];
1305 n2 = tOpo.neighbors[1];
1306 n4 = tOpo.neighbors[2];
1307 }
1308 else if(tOpo.neighbors[1] == iT)
1309 {
1310 iV3 = tOpo.vertices[0];
1311 n2 = tOpo.neighbors[2];
1312 n4 = tOpo.neighbors[0];
1313 }
1314 else
1315 {
1316 iV3 = tOpo.vertices[1];
1317 n2 = tOpo.neighbors[0];
1318 n4 = tOpo.neighbors[1];
1319 }
1320}
1321
1322template <typename T, typename TNearPointLocator>
1323bool Triangulation<T, TNearPointLocator>::isSameOriginalEdge(
1324 const Edge& e1,
1325 const Edge& e2) const
1326{
1327 if(!fixedEdges.count(e1) || !fixedEdges.count(e2))
1328 return false;
1329 typedef unordered_map<Edge, EdgeVec>::const_iterator It;
1330 const It it1 = pieceToOriginals.find(e1);
1331 const It it2 = pieceToOriginals.find(e2);
1332 return (it1 == pieceToOriginals.end() ? e1 : it1->second.front()) ==
1333 (it2 == pieceToOriginals.end() ? e2 : it2->second.front());
1334}
1335
1346/*
1347 * v4 original edge: (v2, v4)
1348 * /|\ flip-candidate edge: (v1, v3)
1349 * / | \
1350 * / | \
1351 * / | \
1352 * new vertex--> v1 | v3
1353 * \ | /
1354 * \ | /
1355 * \ | /
1356 * \|/
1357 * v2
1358 */
1359template <typename T, typename TNearPointLocator>
1360bool Triangulation<T, TNearPointLocator>::isFlipNeeded(
1361 const VertInd iV1,
1362 const VertInd iV2,
1363 const VertInd iV3,
1364 const VertInd iV4,
1365 const bool doFlipFixedEdges) const
1366{
1367 if(!doFlipFixedEdges && fixedEdges.count(Edge(iV2, iV4)))
1368 return false; // flip not needed if the original edge is fixed
1369 // the flip would make a triangle out of two pieces of one input edge
1370 if(!fixedEdges.empty() &&
1371 (isSameOriginalEdge(Edge(iV1, iV2), Edge(iV2, iV3)) ||
1372 isSameOriginalEdge(Edge(iV3, iV4), Edge(iV4, iV1))))
1373 return false;
1374 const V2d<T>& v1 = vertices[iV1];
1375 const V2d<T>& v2 = vertices[iV2];
1376 const V2d<T>& v3 = vertices[iV3];
1377 const V2d<T>& v4 = vertices[iV4];
1378 // If flip-candidate edge touches super-triangle in-circumference
1379 // test has to be replaced with orient2d test against the line
1380 // formed by two non-artificial vertices (that don't belong to
1381 // super-triangle)
1382 if(iV1 < nSuperTriVerts) // flip-candidate edge touches super-triangle
1383 {
1384 // does original edge also touch super-triangle?
1385 if(iV2 < nSuperTriVerts)
1386 return locatePointLine(v2, v3, v4) == locatePointLine(v1, v3, v4);
1387 if(iV4 < nSuperTriVerts)
1388 return locatePointLine(v4, v2, v3) == locatePointLine(v1, v2, v3);
1389 return false; // original edge does not touch super-triangle
1390 }
1391 if(iV3 < nSuperTriVerts) // flip-candidate edge touches super-triangle
1392 {
1393 // does original edge also touch super-triangle?
1394 if(iV2 < nSuperTriVerts)
1395 {
1396 return locatePointLine(v2, v1, v4) == locatePointLine(v3, v1, v4);
1397 }
1398 if(iV4 < nSuperTriVerts)
1399 {
1400 return locatePointLine(v4, v2, v1) == locatePointLine(v3, v2, v1);
1401 }
1402 return false; // original edge does not touch super-triangle
1403 }
1404 // flip-candidate edge does not touch super-triangle
1405 if(iV2 < nSuperTriVerts)
1406 return locatePointLine(v2, v3, v4) == locatePointLine(v1, v3, v4);
1407 if(iV4 < nSuperTriVerts)
1408 return locatePointLine(v4, v2, v3) == locatePointLine(v1, v2, v3);
1409 return isInCircumcircle(v1, v2, v3, v4);
1410}
1411
1412/* Flip edge between T and Topo:
1413 *
1414 * v4 | - old edge
1415 * /|\ ~ - new edge
1416 * / | \
1417 * n3 / T' \ n4
1418 * / | \
1419 * / | \
1420 * T -> v1~~~~~~~~~v3 <- Topo
1421 * \ | /
1422 * \ | /
1423 * n1 \Topo'/ n2
1424 * \ | /
1425 * \|/
1426 * v2
1427 */
1428template <typename T, typename TNearPointLocator>
1430 const TriInd iT,
1431 const TriInd iTopo)
1432{
1433#ifdef CDT_ENABLE_CALLBACK_HANDLER
1434 if(m_callbackHandler)
1435 {
1436 m_callbackHandler->onFlipEdge(iT, iTopo);
1437 }
1438#endif
1439
1440 Triangle& t = triangles[iT];
1441 Triangle& tOpo = triangles[iTopo];
1442 const array<TriInd, 3>& triNs = t.neighbors;
1443 const array<TriInd, 3>& triOpoNs = tOpo.neighbors;
1444 const array<VertInd, 3>& triVs = t.vertices;
1445 const array<VertInd, 3>& triOpoVs = tOpo.vertices;
1446 // find vertices and neighbors
1447 Index i = opposedVertexInd(t.neighbors, iTopo);
1448 const VertInd v1 = triVs[i];
1449 const VertInd v2 = triVs[ccw(i)];
1450 const TriInd n1 = triNs[i];
1451 const TriInd n3 = triNs[cw(i)];
1452 i = opposedVertexInd(tOpo.neighbors, iT);
1453 const VertInd v3 = triOpoVs[i];
1454 const VertInd v4 = triOpoVs[ccw(i)];
1455 const TriInd n4 = triOpoNs[i];
1456 const TriInd n2 = triOpoNs[cw(i)];
1457 // change vertices and neighbors
1458 t = Triangle(arr3(v4, v1, v3), arr3(n3, iTopo, n4));
1459 tOpo = Triangle(arr3(v2, v3, v1), arr3(n2, iT, n1));
1460 // adjust neighboring triangles and vertices
1461 changeNeighbor(n1, iT, iTopo);
1462 changeNeighbor(n4, iTopo, iT);
1463 // only adjust adjacent triangles if triangulation is not finalized:
1464 // can happen when called from outside on an already finalized
1465 // triangulation
1466 if(!isFinalized())
1467 {
1468 setAdjacentTriangle(v4, iT);
1469 setAdjacentTriangle(v2, iTopo);
1470 }
1471}
1472
1473template <typename T, typename TNearPointLocator>
1474bool Triangulation<T, TNearPointLocator>::isRefinementNeeded(
1475 const Triangle& tri,
1476 const RefinementCriterion::Enum refinementCriterion,
1477 const T refinementThreshold) const
1478{
1479 const V2d<T>& a = vertices[tri.vertices[0]];
1480 const V2d<T>& b = vertices[tri.vertices[1]];
1481 const V2d<T>& c = vertices[tri.vertices[2]];
1482 switch(refinementCriterion)
1483 {
1485 return smallestAngle(a, b, c) < refinementThreshold;
1487 return area(a, b, c) > refinementThreshold;
1488 }
1489 assert(false); // unreachable code
1490 return false;
1491}
1492
1493template <typename T, typename TNearPointLocator>
1494T Triangulation<T, TNearPointLocator>::triangleBadness(
1495 const Triangle& tri,
1496 const RefinementCriterion::Enum refinementCriterion) const
1497{
1498 const V2d<T>& a = vertices[tri.vertices[0]];
1499 const V2d<T>& b = vertices[tri.vertices[1]];
1500 const V2d<T>& c = vertices[tri.vertices[2]];
1501 return refinementCriterion == RefinementCriterion::SmallestAngle
1502 ? -smallestAngle(a, b, c)
1503 : area(a, b, c);
1504}
1505
1506template <typename T, typename TNearPointLocator>
1507bool Triangulation<T, TNearPointLocator>::isSmallestAngleFixed(
1508 const Triangle& tri) const
1509{
1510 Index iApex(0);
1511 T shortestSqLen = distanceSquared(
1512 vertices[tri.vertices[ccw(Index(0))]],
1513 vertices[tri.vertices[cw(Index(0))]]);
1514 for(Index i(1); i < Index(3); ++i)
1515 {
1516 const T sqLen = distanceSquared(
1517 vertices[tri.vertices[ccw(i)]], vertices[tri.vertices[cw(i)]]);
1518 if(sqLen < shortestSqLen)
1519 {
1520 shortestSqLen = sqLen;
1521 iApex = i;
1522 }
1523 }
1524 const VertInd apex = tri.vertices[iApex];
1525 return fixedEdges.count(Edge(apex, tri.vertices[ccw(iApex)])) &&
1526 fixedEdges.count(Edge(apex, tri.vertices[cw(iApex)]));
1527}
1528
1529template <typename T, typename TNearPointLocator>
1530bool Triangulation<T, TNearPointLocator>::isEdgeEncroached(
1531 const Edge& edge) const
1532{
1533 TriInd iT, iTopo;
1534 tie(iT, iTopo) = edgeTriangles(edge.v1(), edge.v2());
1535 assert(iT != noNeighbor && iTopo != noNeighbor);
1536 const VertInd v1 = opposedVertex(triangles[iT], iTopo);
1537 const VertInd v2 = opposedVertex(triangles[iTopo], iT);
1538 const V2d<T>& edgeStart = vertices[edge.v1()];
1539 const V2d<T>& edgeEnd = vertices[edge.v2()];
1540 return detail::isEncroachingOnEdge(vertices[v1], edgeStart, edgeEnd) ||
1541 detail::isEncroachingOnEdge(vertices[v2], edgeStart, edgeEnd);
1542}
1543
1544template <typename T, typename TNearPointLocator>
1545bool Triangulation<T, TNearPointLocator>::isEdgeEncroachedBy(
1546 const Edge& edge,
1547 const V2d<T>& v) const
1548{
1549 return detail::isEncroachingOnEdge(
1550 v, vertices[edge.v1()], vertices[edge.v2()]);
1551}
1552
1553template <typename T, typename TNearPointLocator>
1555{
1556 if(isFinalized())
1557 handleException(FinalizedError(CDT_SOURCE_LOCATION));
1558
1559 // Search in all fixed edges to find encroached edges
1560 EdgeVec encroachedEdges;
1561 typedef EdgeUSet::const_iterator Iter;
1562 for(Iter it = fixedEdges.begin(); it != fixedEdges.end(); ++it)
1563 {
1564 const Edge edge = *it;
1565 if(isEdgeEncroached(edge))
1566 {
1567 encroachedEdges.push_back(edge);
1568 }
1569 }
1570 // fixedEdges is a hash set: its iteration order is platform-dependent,
1571 // so sort to keep refinement output deterministic
1572 std::sort(encroachedEdges.begin(), encroachedEdges.end());
1573 return encroachedEdges;
1574}
1575
1576template <typename T, typename TNearPointLocator>
1578 const RefinementCriterion::Enum refinementCriterion,
1579 const T refinementThreshold) const
1580{
1581 const bool hasSuperTriangle = !isFinalized();
1582 TriIndVec unrefinedTriangles;
1583 for(TriInd iT(0), n(triangles.size()); iT < n; ++iT)
1584 {
1585 const Triangle& t = triangles[iT];
1586 if(hasSuperTriangle && touchesSuperTriangle(t))
1587 continue;
1588 if(isRefinementNeeded(t, refinementCriterion, refinementThreshold))
1589 unrefinedTriangles.push_back(iT);
1590 }
1591 return unrefinedTriangles;
1592}
1593
1594template <typename T, typename TNearPointLocator>
1595EdgeVec Triangulation<T, TNearPointLocator>::edgesEncroachedBy(
1596 const V2d<T>& v,
1597 const TriInd iT) const
1598{
1599 /*
1600 * A fixed edge not yet encroached by an existing vertex can only be
1601 * encroached by v if v is inside the circumscribed circle of the edge's
1602 * triangle on v's side: the circumscribed circles of an edge's two
1603 * triangles cover the edge's diametral circle. Such triangles are visible
1604 * from v, so growing from the triangle at v without crossing fixed edges
1605 * reaches all of them.
1606 */
1607 EdgeVec encroachedEdges;
1608 TriIndUSet traversed;
1609 std::stack<TriInd> toTraverse;
1610 toTraverse.push(iT);
1611 traversed.insert(iT);
1612 while(!toTraverse.empty())
1613 {
1614 const Triangle& t = triangles[toTraverse.top()];
1615 toTraverse.pop();
1616 for(Index i(0); i < Index(3); ++i)
1617 {
1618 const Edge opEdge(t.vertices[ccw(i)], t.vertices[cw(i)]);
1619 if(fixedEdges.count(opEdge))
1620 {
1621 // both edge's triangles can be reached: avoid duplicate edges
1622 if(isEdgeEncroachedBy(opEdge, v))
1623 detail::insert_unique(encroachedEdges, opEdge);
1624 continue;
1625 }
1626 const TriInd iN = t.neighbors[opoNbr(i)];
1627 if(iN == noNeighbor || traversed.count(iN))
1628 continue;
1629 const Triangle& n = triangles[iN];
1630 if(!isInCircumcircle(
1631 v,
1632 vertices[n.vertices[0]],
1633 vertices[n.vertices[1]],
1634 vertices[n.vertices[2]]))
1635 continue;
1636 traversed.insert(iN);
1637 toTraverse.push(iN);
1638 }
1639 }
1640 return encroachedEdges;
1641}
1642
1643template <typename T, typename TNearPointLocator>
1644TriIndVec Triangulation<T, TNearPointLocator>::resolveEncroachedEdges(
1645 EdgeQueue encroachedEdges,
1646 VertInd& remainingVertexBudget,
1647 const VertInd steinerVerticesOffset,
1648 const V2d<T>* const circumcenterOrNull,
1649 const RefinementCriterion::Enum refinementCriterion,
1650 const T badTriangleThreshold,
1651 TriIndUSet* const toEraseOrNull,
1652 const T minEdgeLength,
1653 Unrefined& unrefined)
1654{
1655 std::vector<TriInd> badTriangles;
1656
1657 while(!encroachedEdges.empty() && remainingVertexBudget > 0)
1658 {
1659 const Edge edge = encroachedEdges.front();
1660 encroachedEdges.pop();
1661 if(fixedEdges.find(edge) == fixedEdges.end())
1662 {
1663 continue;
1664 }
1665 // give up on already-too-short edges rather than split forever
1666 if(distance(vertices[edge.v1()], vertices[edge.v2()]) <= minEdgeLength)
1667 {
1668 ++unrefined.shortEdges;
1669 continue;
1670 }
1671 // split encroached edge
1672 const OptionalVertInd splitVert = splitEncroachedEdge(
1673 edge, steinerVerticesOffset, toEraseOrNull, unrefined);
1674 if(!splitVert.hasValue())
1675 continue;
1676 const VertInd iSplitVert = splitVert.value();
1677 --remainingVertexBudget;
1678
1679 const TriInd start = m_vertTris[iSplitVert];
1680 TriInd iT = start;
1681 do
1682 {
1683 const Triangle& t = triangles[iT];
1684 const bool isMarkedForErasure =
1685 toEraseOrNull && toEraseOrNull->count(iT);
1686 if(circumcenterOrNull && !isMarkedForErasure &&
1688 isRefinementNeeded(t, refinementCriterion, badTriangleThreshold))
1689 {
1690 badTriangles.push_back(iT);
1691 }
1692 for(Index i(0); i < Index(3); ++i)
1693 {
1694 const Edge triEdge(t.vertices[i], t.vertices[cw(i)]);
1695 if(fixedEdges.find(triEdge) == fixedEdges.end())
1696 continue;
1697 if(isEdgeEncroached(triEdge))
1698 {
1699 encroachedEdges.push(triEdge);
1700 }
1701 }
1702 iT = t.next(iSplitVert).first;
1703 } while(iT != start);
1704 }
1705 return badTriangles;
1706}
1707
1708template <typename T, typename TNearPointLocator>
1709OptionalVertInd Triangulation<T, TNearPointLocator>::splitEncroachedEdge(
1710 const Edge edge,
1711 const VertInd steinerVerticesOffset,
1712 TriIndUSet* const toEraseOrNull,
1713 Unrefined& unrefined)
1714{
1715 const V2d<T>& start = vertices[edge.v1()];
1716 const V2d<T>& end = vertices[edge.v2()];
1717
1718 TriInd iT, iTopo;
1719 tie(iT, iTopo) = edgeTriangles(edge.v1(), edge.v2());
1720 assert(iT != noNeighbor && iTopo != noNeighbor);
1721
1722 T split = T(0.5);
1723 // Edge sorts its vertices and Steiner ones are appended after the input
1724 // ones, so only v1 can be an input vertex here
1725 if(edge.v1() < steinerVerticesOffset &&
1726 edge.v2() >= steinerVerticesOffset &&
1727 hasAnotherFixedEdgeAtSmallAngle(edge.v1(), edge))
1728 {
1729 // In Ruppert's paper, he used D(0.01) factor to divide edge length, but
1730 // that introduces FP rounding errors, so it's avoided.
1731 const T len = distance(start, end);
1732 const T d = len / T(2);
1733 // Find the splitting distance
1734 T nearestPowerOfTwo = T(1);
1735 while(d > nearestPowerOfTwo)
1736 {
1737 nearestPowerOfTwo *= T(2);
1738 }
1739 while(d < T(0.75) * nearestPowerOfTwo)
1740 {
1741 nearestPowerOfTwo *= T(0.5);
1742 }
1743 split = nearestPowerOfTwo / len;
1744 }
1745
1746 const V2d<T> mid = V2d<T>(
1747 detail::lerp(start.x, end.x, split),
1748 detail::lerp(start.y, end.y, split));
1749
1750 const OptionalVertInd iMid = splitFixedEdgeAt(
1751 edge, mid, iT, iTopo, AddVertexType::RefinementEdgeSplit);
1752 if(!iMid.hasValue())
1753 {
1754 ++unrefined.splitVertexInvalid;
1755 }
1756 else if(toEraseOrNull)
1757 {
1758 // splitting reuses iT/iTopo for two of the four resulting triangles and
1759 // appends the other two: propagate erasure marks to the new triangles
1760 if(toEraseOrNull->count(iT))
1761 toEraseOrNull->insert(TriInd(triangles.size() - 2));
1762 if(toEraseOrNull->count(iTopo))
1763 toEraseOrNull->insert(TriInd(triangles.size() - 1));
1764 }
1765 return iMid;
1766}
1767
1768/* Flip edge between T and Topo:
1769 *
1770 * v4 | - old edge
1771 * /|\ ~ - new edge
1772 * / | \
1773 * n3 / T' \ n4
1774 * / | \
1775 * / | \
1776 * T -> v1 ~~~~~~~~ v3 <- Topo
1777 * \ | /
1778 * \ | /
1779 * n1 \Topo'/ n2
1780 * \ | /
1781 * \|/
1782 * v2
1783 */
1784template <typename T, typename TNearPointLocator>
1786 const TriInd iT,
1787 const TriInd iTopo,
1788 const VertInd v1,
1789 const VertInd v2,
1790 const VertInd v3,
1791 const VertInd v4,
1792 const TriInd n1,
1793 const TriInd n2,
1794 const TriInd n3,
1795 const TriInd n4)
1796{
1797#ifdef CDT_ENABLE_CALLBACK_HANDLER
1798 if(m_callbackHandler)
1799 {
1800 m_callbackHandler->onFlipEdge(iT, iTopo);
1801 }
1802#endif
1803
1804 // change vertices and neighbors
1805 triangles[iT] = Triangle(arr3(v4, v1, v3), arr3(n3, iTopo, n4));
1806 triangles[iTopo] = Triangle(arr3(v2, v3, v1), arr3(n2, iT, n1));
1807 // adjust neighboring triangles and vertices
1808 changeNeighbor(n1, iT, iTopo);
1809 changeNeighbor(n4, iTopo, iT);
1810 // only adjust adjacent triangles if triangulation is not finalized:
1811 // can happen when called from outside on an already finalized
1812 // triangulation
1813 if(!isFinalized())
1814 {
1815 setAdjacentTriangle(v4, iT);
1816 setAdjacentTriangle(v2, iTopo);
1817 }
1818}
1819
1820/* Insert point into triangle: split into 3 triangles:
1821 * - create 2 new triangles
1822 * - re-use old triangle for the 3rd
1823 * v3
1824 * / | \
1825 * / | \ <-- original triangle (t)
1826 * / | \
1827 * n3 / | \ n2
1828 * /newT2|newT1\
1829 * / v \
1830 * / __/ \__ \
1831 * / __/ \__ \
1832 * / _/ t' \_ \
1833 * v1 ___________________ v2
1834 * n1
1835 */
1836template <typename T, typename TNearPointLocator>
1837std::stack<TriInd>
1838Triangulation<T, TNearPointLocator>::insertVertexInsideTriangle(
1839 VertInd v,
1840 TriInd iT)
1841{
1842 const TriInd iNewT1 = addTriangle();
1843 const TriInd iNewT2 = addTriangle();
1844
1845#ifdef CDT_ENABLE_CALLBACK_HANDLER
1846 if(m_callbackHandler)
1847 {
1848 m_callbackHandler->onInsertVertexInsideTriangle(iT, iNewT1, iNewT2);
1849 }
1850#endif
1851
1852 Triangle& t = triangles[iT];
1853 const array<VertInd, 3> vv = t.vertices;
1854 const array<TriInd, 3> nn = t.neighbors;
1855 const VertInd v1 = vv[0], v2 = vv[1], v3 = vv[2];
1856 const TriInd n1 = nn[0], n2 = nn[1], n3 = nn[2];
1857 // make two new triangles and convert current triangle to 3rd new
1858 // triangle
1859 triangles[iNewT1] = Triangle(arr3(v2, v3, v), arr3(n2, iNewT2, iT));
1860 triangles[iNewT2] = Triangle(arr3(v3, v1, v), arr3(n3, iT, iNewT1));
1861 t = Triangle(arr3(v1, v2, v), arr3(n1, iNewT1, iNewT2));
1862 // adjust adjacent triangles
1863 setAdjacentTriangle(v, iT);
1864 setAdjacentTriangle(v3, iNewT1);
1865 // change triangle neighbor's neighbors to new triangles
1866 changeNeighbor(n2, iT, iNewT1);
1867 changeNeighbor(n3, iT, iNewT2);
1868 // return newly added triangles
1869 std::stack<TriInd> newTriangles;
1870 newTriangles.push(iT);
1871 newTriangles.push(iNewT1);
1872 newTriangles.push(iNewT2);
1873 return newTriangles;
1874}
1875
1876/* Inserting a point on the edge between two triangles
1877 * T1 (top) v1
1878 * /|\
1879 * n1 / | \ n4
1880 * / | \
1881 * / T1' | Tnew1\
1882 * v2-------v-------v4
1883 * \ T2' | Tnew2/
1884 * \ | /
1885 * n2 \ | / n3
1886 * \|/
1887 * T2 (bottom) v3
1888 */
1889template <typename T, typename TNearPointLocator>
1890std::stack<TriInd> Triangulation<T, TNearPointLocator>::insertVertexOnEdge(
1891 VertInd v,
1892 TriInd iT1,
1893 TriInd iT2,
1894 const bool doHandleFixedSplitEdge)
1895{
1896 const TriInd iTnew1 = addTriangle();
1897 const TriInd iTnew2 = addTriangle();
1898
1899#ifdef CDT_ENABLE_CALLBACK_HANDLER
1900 if(m_callbackHandler)
1901 {
1902 m_callbackHandler->onInsertVertexOnEdge(iT1, iT2, iTnew1, iTnew2);
1903 }
1904#endif
1905
1906 Triangle& t1 = triangles[iT1];
1907 Triangle& t2 = triangles[iT2];
1908 Index i = opposedVertexInd(t1.neighbors, iT2);
1909 const VertInd v1 = t1.vertices[i];
1910 const VertInd v2 = t1.vertices[ccw(i)];
1911 const TriInd n1 = t1.neighbors[i];
1912 const TriInd n4 = t1.neighbors[cw(i)];
1913 i = opposedVertexInd(t2.neighbors, iT1);
1914 const VertInd v3 = t2.vertices[i];
1915 const VertInd v4 = t2.vertices[ccw(i)];
1916 const TriInd n3 = t2.neighbors[i];
1917 const TriInd n2 = t2.neighbors[cw(i)];
1918 // add new triangles and change existing ones
1919 t1 = Triangle(arr3(v, v1, v2), arr3(iTnew1, n1, iT2));
1920 t2 = Triangle(arr3(v, v2, v3), arr3(iT1, n2, iTnew2));
1921 triangles[iTnew1] = Triangle(arr3(v, v4, v1), arr3(iTnew2, n4, iT1));
1922 triangles[iTnew2] = Triangle(arr3(v, v3, v4), arr3(iT2, n3, iTnew1));
1923 // adjust adjacent triangles
1924 setAdjacentTriangle(v, iT1);
1925 setAdjacentTriangle(v4, iTnew1);
1926 // adjust neighboring triangles and vertices
1927 changeNeighbor(n4, iT1, iTnew1);
1928 changeNeighbor(n3, iT2, iTnew2);
1929 // properly handle the case when the split edge is a fixed edge
1930 if(doHandleFixedSplitEdge)
1931 {
1932 const Edge sharedEdge(v2, v4);
1933 if(fixedEdges.count(sharedEdge))
1934 splitFixedEdge(sharedEdge, v);
1935 }
1936 // return newly added triangles
1937 std::stack<TriInd> newTriangles;
1938 newTriangles.push(iT1);
1939 newTriangles.push(iTnew2);
1940 newTriangles.push(iT2);
1941 newTriangles.push(iTnew1);
1942 return newTriangles;
1943}
1944
1945template <typename T, typename TNearPointLocator>
1946array<TriInd, 2>
1947Triangulation<T, TNearPointLocator>::trianglesAt(const V2d<T>& pos) const
1948{
1949 array<TriInd, 2> out = {noNeighbor, noNeighbor};
1950 for(TriInd i = TriInd(0); i < TriInd(triangles.size()); ++i)
1951 {
1952 const Triangle& t = triangles[i];
1953 const V2d<T>& v1 = vertices[t.vertices[0]];
1954 const V2d<T>& v2 = vertices[t.vertices[1]];
1955 const V2d<T>& v3 = vertices[t.vertices[2]];
1956 const PtTriLocation::Enum loc = locatePointTriangle(pos, v1, v2, v3);
1957 if(loc == PtTriLocation::Outside)
1958 continue;
1959 out[0] = i;
1960 if(isOnEdge(loc))
1961 out[1] = t.neighbors[edgeNeighbor(loc)];
1962 return out;
1963 }
1964 handleException(
1965 Error("No triangle was found at position", CDT_SOURCE_LOCATION));
1966 return out;
1967}
1968
1969template <typename T, typename TNearPointLocator>
1970OptionalTriInd Triangulation<T, TNearPointLocator>::walkTriangles(
1971 const VertInd startVertex,
1972 const V2d<T>& pos) const
1973{
1974 // begin walk in search of triangle at pos
1975 TriInd currTri = m_vertTris[startVertex];
1976 bool found = false;
1977 bool isOutside = false;
1978 detail::SplitMix64RandGen prng;
1979 while(!found)
1980 {
1981 const Triangle& t = triangles[currTri];
1982 found = true;
1983 isOutside = false;
1984 // stochastic offset to randomize which edge we check first
1985 const Index offset(prng() % 3);
1986 for(Index i_(0); i_ < Index(3); ++i_)
1987 {
1988 const Index i((i_ + offset) % 3);
1989 const V2d<T>& vStart = vertices[t.vertices[i]];
1990 const V2d<T>& vEnd = vertices[t.vertices[ccw(i)]];
1991 const PtLineLocation::Enum edgeCheck =
1992 locatePointLine(pos, vStart, vEnd);
1993 const TriInd iN = t.neighbors[i];
1994 if(edgeCheck == PtLineLocation::Right)
1995 {
1996 if(iN != noNeighbor)
1997 {
1998 found = false;
1999 currTri = iN;
2000 break;
2001 }
2002 // crossing this edge would leave the triangulated area:
2003 // other edges can still lead to a triangle containing pos
2004 isOutside = true;
2005 }
2006 }
2007 }
2008 return isOutside ? OptionalTriInd(noNeighbor) : OptionalTriInd(currTri);
2009}
2010
2011template <typename T, typename TNearPointLocator>
2012array<TriInd, 2> Triangulation<T, TNearPointLocator>::walkingSearchTrianglesAt(
2013 const VertInd iV,
2014 const VertInd startVertex) const
2015{
2016 const V2d<T> v = vertices[iV];
2017 array<TriInd, 2> out = {noNeighbor, noNeighbor};
2018 const OptionalTriInd walkResult = walkTriangles(startVertex, v);
2019 if(!walkResult.hasValue())
2020 {
2021 handleException(
2022 Error("No triangle was found at position", CDT_SOURCE_LOCATION));
2023 }
2024 const TriInd iT = walkResult.value();
2025 // Finished walk, locate point in current triangle
2026 const Triangle& t = triangles[iT];
2027 const V2d<T>& v1 = vertices[t.vertices[0]];
2028 const V2d<T>& v2 = vertices[t.vertices[1]];
2029 const V2d<T>& v3 = vertices[t.vertices[2]];
2030 const PtTriLocation::Enum loc = locatePointTriangle(v, v1, v2, v3);
2031
2032 if(loc == PtTriLocation::Outside)
2033 {
2034 handleException(
2035 Error("No triangle was found at position", CDT_SOURCE_LOCATION));
2036 }
2037 if(loc == PtTriLocation::OnVertex)
2038 {
2039 const VertInd iDupe = v1 == v ? t.vertices[0]
2040 : v2 == v ? t.vertices[1]
2041 : t.vertices[2];
2042 handleException(DuplicateVertexError(
2043 VertInd(iV - nSuperTriVerts),
2044 VertInd(iDupe - nSuperTriVerts),
2045 CDT_SOURCE_LOCATION));
2046 }
2047
2048 out[0] = iT;
2049 if(isOnEdge(loc))
2050 out[1] = t.neighbors[edgeNeighbor(loc)];
2051 return out;
2052}
2053
2054template <typename T, typename TNearPointLocator>
2055void Triangulation<T, TNearPointLocator>::changeNeighbor(
2056 const TriInd iT,
2057 const TriInd oldNeighbor,
2058 const TriInd newNeighbor)
2059{
2060 if(iT == noNeighbor)
2061 return;
2062 NeighborsArr3& nn = triangles[iT].neighbors;
2063 assert(
2064 nn[0] == oldNeighbor || nn[1] == oldNeighbor || nn[2] == oldNeighbor);
2065 if(nn[0] == oldNeighbor)
2066 nn[0] = newNeighbor;
2067 else if(nn[1] == oldNeighbor)
2068 nn[1] = newNeighbor;
2069 else
2070 nn[2] = newNeighbor;
2071}
2072
2073template <typename T, typename TNearPointLocator>
2074void Triangulation<T, TNearPointLocator>::changeNeighbor(
2075 const TriInd iT,
2076 const VertInd iVedge1,
2077 const VertInd iVedge2,
2078 const TriInd newNeighbor)
2079{
2080 assert(iT != noNeighbor);
2081 Triangle& t = triangles[iT];
2082 t.neighbors[edgeNeighborInd(t.vertices, iVedge1, iVedge2)] = newNeighbor;
2083}
2084
2085template <typename T, typename TNearPointLocator>
2086void Triangulation<T, TNearPointLocator>::triangulatePseudoPolygon(
2087 const std::vector<VertInd>& poly,
2088 unordered_map<Edge, TriInd>& outerTris,
2089 TriInd iT,
2090 TriInd iN,
2091 std::vector<TriInd>& trianglesToReuse,
2092 std::vector<TriangulatePseudoPolygonTask>& iterations)
2093{
2094 assert(poly.size() > 2);
2095 // note: uses iteration instead of recursion to avoid stack overflows
2096 iterations.clear();
2097 iterations.push_back(make_tuple(
2098 IndexSizeType(0),
2099 static_cast<IndexSizeType>(poly.size() - 1),
2100 iT,
2101 iN,
2102 Index(0)));
2103 while(!iterations.empty())
2104 {
2105 triangulatePseudoPolygonIteration(
2106 poly, outerTris, trianglesToReuse, iterations);
2107 }
2108}
2109
2110template <typename T, typename TNearPointLocator>
2111void Triangulation<T, TNearPointLocator>::triangulatePseudoPolygonIteration(
2112 const std::vector<VertInd>& poly,
2113 unordered_map<Edge, TriInd>& outerTris,
2114 std::vector<TriInd>& trianglesToReuse,
2115 std::vector<TriangulatePseudoPolygonTask>& iterations)
2116{
2117 IndexSizeType iA, iB;
2118 TriInd iT, iParent;
2119 Index iInParent;
2120 assert(!iterations.empty());
2121 tie(iA, iB, iT, iParent, iInParent) = iterations.back();
2122 iterations.pop_back();
2123 assert(iB - iA > 1 && iT != noNeighbor && iParent != noNeighbor);
2124 Triangle& t = triangles[iT];
2125 // find Delaunay point
2126 const IndexSizeType iC = findDelaunayPoint(poly, iA, iB);
2127
2128 const VertInd a = poly[iA];
2129 const VertInd b = poly[iB];
2130 const VertInd c = poly[iC];
2131
2132 // split pseudo-polygon in two parts and triangulate them
2133 // note: second part needs to be pushed on stack first to be processed first
2134
2135 // second part: points after the Delaunay point
2136 if(iB - iC > 1)
2137 {
2138 assert(!trianglesToReuse.empty());
2139 const TriInd iNext = trianglesToReuse.back();
2140 trianglesToReuse.pop_back();
2141 iterations.push_back(make_tuple(iC, iB, iNext, iT, Index(1)));
2142 }
2143 else // pseudo-poly is reduced to a single outer edge
2144 {
2145 const Edge outerEdge(b, c);
2146 const TriInd outerTri = outerTris.at(outerEdge);
2147 if(outerTri != noNeighbor)
2148 {
2149 assert(outerTri != iT);
2150 t.neighbors[1] = outerTri;
2151 changeNeighbor(outerTri, c, b, iT);
2152 }
2153 else
2154 outerTris.at(outerEdge) = iT;
2155 }
2156 // first part: points before the Delaunay point
2157 if(iC - iA > 1)
2158 { // add next triangle and add another iteration
2159 assert(!trianglesToReuse.empty());
2160 const TriInd iNext = trianglesToReuse.back();
2161 trianglesToReuse.pop_back();
2162 iterations.push_back(make_tuple(iA, iC, iNext, iT, Index(2)));
2163 }
2164 else
2165 { // pseudo-poly is reduced to a single outer edge
2166 const Edge outerEdge(c, a);
2167 const TriInd outerTri = outerTris.at(outerEdge);
2168 if(outerTri != noNeighbor)
2169 {
2170 assert(outerTri != iT);
2171 t.neighbors[2] = outerTri;
2172 changeNeighbor(outerTri, c, a, iT);
2173 }
2174 else
2175 outerTris.at(outerEdge) = iT;
2176 }
2177 // Finalize triangle
2178 // note: only when triangle is finalized to we add it as a neighbor to
2179 // parent to maintain triangulation topology consistency
2180 triangles[iParent].neighbors[iInParent] = iT;
2181 t.neighbors[0] = iParent;
2182 t.vertices = arr3(a, b, c);
2183 setAdjacentTriangle(c, iT);
2184}
2185
2186template <typename T, typename TNearPointLocator>
2187IndexSizeType Triangulation<T, TNearPointLocator>::findDelaunayPoint(
2188 const std::vector<VertInd>& poly,
2189 const IndexSizeType iA,
2190 const IndexSizeType iB) const
2191{
2192 assert(iB - iA > 1);
2193 const V2d<T>& a = vertices[poly[iA]];
2194 const V2d<T>& b = vertices[poly[iB]];
2195 IndexSizeType out = iA + 1;
2196 const V2d<T>* c = &vertices[poly[out]]; // caching for better performance
2197 for(IndexSizeType i = iA + 1; i < iB; ++i)
2198 {
2199 const V2d<T>& v = vertices[poly[i]];
2200 if(isInCircumcircle(v, a, b, *c))
2201 {
2202 out = i;
2203 c = &v;
2204 }
2205 }
2206 assert(out > iA && out < iB); // point is between ends
2207 return out;
2208}
2209
2210template <typename T, typename TNearPointLocator>
2212 const std::vector<V2d<T> >& newVertices)
2213{
2214 return insertVertices(
2215 newVertices.begin(), newVertices.end(), getX_V2d<T>, getY_V2d<T>);
2216}
2217
2218template <typename T, typename TNearPointLocator>
2220{
2221 return m_vertTris.empty() && !vertices.empty();
2222}
2223
2224template <typename T, typename TNearPointLocator>
2225unordered_map<TriInd, LayerDepth>
2226Triangulation<T, TNearPointLocator>::peelLayer(
2227 std::stack<TriInd> seeds,
2228 const LayerDepth layerDepth,
2229 std::vector<LayerDepth>& triDepths) const
2230{
2231 unordered_map<TriInd, LayerDepth> behindBoundary;
2232 while(!seeds.empty())
2233 {
2234 const TriInd iT = seeds.top();
2235 seeds.pop();
2236 triDepths[iT] = std::min(triDepths[iT], layerDepth);
2237 behindBoundary.erase(iT);
2238 const Triangle& t = triangles[iT];
2239 for(Index i(0); i < Index(3); ++i)
2240 {
2241 const Edge opEdge(t.vertices[ccw(i)], t.vertices[cw(i)]);
2242 const TriInd iN = t.neighbors[opoNbr(i)];
2243 if(iN == noNeighbor || triDepths[iN] <= layerDepth)
2244 continue;
2245 if(fixedEdges.count(opEdge))
2246 {
2247 const unordered_map<Edge, LayerDepth>::const_iterator cit =
2248 overlapCount.find(opEdge);
2249 const LayerDepth triDepth = cit == overlapCount.end()
2250 ? layerDepth + 1
2251 : layerDepth + cit->second + 1;
2252 behindBoundary[iN] = triDepth;
2253 continue;
2254 }
2255 seeds.push(iN);
2256 }
2257 }
2258 return behindBoundary;
2259}
2260
2261template <typename T, typename TNearPointLocator>
2262std::vector<LayerDepth>
2264{
2265 if(isFinalized())
2266 handleException(FinalizedError(CDT_SOURCE_LOCATION));
2267
2268 std::vector<LayerDepth> triDepths(
2269 triangles.size(), std::numeric_limits<LayerDepth>::max());
2270 std::stack<TriInd> seeds(TriDeque(1, m_vertTris[0]));
2271 LayerDepth layerDepth = 0;
2272 LayerDepth deepestSeedDepth = 0;
2273
2274 unordered_map<LayerDepth, TriIndUSet> seedsByDepth;
2275 do
2276 {
2277 const unordered_map<TriInd, LayerDepth>& newSeeds =
2278 peelLayer(seeds, layerDepth, triDepths);
2279
2280 seedsByDepth.erase(layerDepth);
2281 typedef unordered_map<TriInd, LayerDepth>::const_iterator Iter;
2282 for(Iter it = newSeeds.begin(); it != newSeeds.end(); ++it)
2283 {
2284 deepestSeedDepth = std::max(deepestSeedDepth, it->second);
2285 seedsByDepth[it->second].insert(it->first);
2286 }
2287 const TriIndUSet& nextLayerSeeds = seedsByDepth[layerDepth + 1];
2288 seeds = std::stack<TriInd>(
2289 TriDeque(nextLayerSeeds.begin(), nextLayerSeeds.end()));
2290 ++layerDepth;
2291 } while(!seeds.empty() || deepestSeedDepth > layerDepth);
2292
2293 return triDepths;
2294}
2295
2296#ifdef CDT_ENABLE_CALLBACK_HANDLER
2297template <typename T, typename TNearPointLocator>
2299 ICallbackHandler* callbackHandler)
2300{
2301 m_callbackHandler = callbackHandler;
2302}
2303#endif
2304
2305template <typename T, typename TNearPointLocator>
2306void Triangulation<T, TNearPointLocator>::insertVertices_AsProvided(
2307 VertInd superGeomVertCount)
2308{
2309 for(VertInd iV = superGeomVertCount; iV < vertices.size(); ++iV)
2310 {
2311#ifdef CDT_ENABLE_CALLBACK_HANDLER
2312 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
2313 {
2314 return;
2315 }
2316#endif
2317 insertVertex(iV);
2318 }
2319}
2320
2321template <typename T, typename TNearPointLocator>
2322void Triangulation<T, TNearPointLocator>::insertVertices_Randomized(
2323 VertInd superGeomVertCount)
2324{
2325 std::size_t vertexCount = vertices.size() - superGeomVertCount;
2326 std::vector<VertInd> ii(vertexCount);
2327 detail::iota(ii.begin(), ii.end(), superGeomVertCount);
2328 detail::random_shuffle(ii.begin(), ii.end());
2329 for(std::vector<VertInd>::iterator it = ii.begin(); it != ii.end(); ++it)
2330 {
2331#ifdef CDT_ENABLE_CALLBACK_HANDLER
2332 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
2333 {
2334 return;
2335 }
2336#endif
2337 insertVertex(*it);
2338 }
2339}
2340
2341namespace detail
2342{
2343
2344// log2 implementation backwards compatible with pre c++11
2345template <typename T>
2346inline double log2_bc(T x)
2347{
2348#ifdef CDT_CXX11_IS_SUPPORTED
2349 return std::log2(x);
2350#else
2351 static double log2_constant = std::log(2.0);
2352 return std::log(static_cast<double>(x)) / log2_constant;
2353#endif
2354}
2355
2360inline std::size_t maxQueueLengthBFSKDTree(const std::size_t vertexCount)
2361{
2362 const int filledLayerPow2 =
2363 static_cast<int>(std::floor(log2_bc(vertexCount)) - 1);
2364 const std::size_t nodesInFilledTree =
2365 static_cast<std::size_t>(std::pow(2., filledLayerPow2 + 1) - 1);
2366 const std::size_t nodesInLastFilledLayer =
2367 static_cast<std::size_t>(std::pow(2., filledLayerPow2));
2368 const std::size_t nodesInLastLayer = vertexCount - nodesInFilledTree;
2369 return nodesInLastLayer >= nodesInLastFilledLayer
2370 ? nodesInLastFilledLayer + nodesInLastLayer -
2371 nodesInLastFilledLayer
2372 : nodesInLastFilledLayer;
2373}
2374
2375template <typename T>
2376class FixedCapacityQueue
2377{
2378public:
2379 FixedCapacityQueue(const std::size_t capacity)
2380 : m_vec(capacity)
2381 , m_front(m_vec.begin())
2382 , m_back(m_vec.begin())
2383 , m_size(0)
2384 {}
2385 bool empty() const
2386 {
2387 return m_size == 0;
2388 }
2389 const T& front() const
2390 {
2391 return *m_front;
2392 }
2393 void pop()
2394 {
2395 assert(m_size > 0);
2396 ++m_front;
2397 if(m_front == m_vec.end())
2398 m_front = m_vec.begin();
2399 --m_size;
2400 }
2401 void push(const T& t)
2402 {
2403 assert(m_size < m_vec.size());
2404 *m_back = t;
2405 ++m_back;
2406 if(m_back == m_vec.end())
2407 m_back = m_vec.begin();
2408 ++m_size;
2409 }
2410#ifdef CDT_CXX11_IS_SUPPORTED
2411 void push(const T&& t)
2412 {
2413 assert(m_size < m_vec.size());
2414 *m_back = t;
2415 ++m_back;
2416 if(m_back == m_vec.end())
2417 m_back = m_vec.begin();
2418 ++m_size;
2419 }
2420#endif
2421private:
2422 std::vector<T> m_vec;
2423 typename std::vector<T>::iterator m_front;
2424 typename std::vector<T>::iterator m_back;
2425 std::size_t m_size;
2426};
2427
2428template <typename T>
2429class less_than_x
2430{
2431 const std::vector<V2d<T> >& m_vertices;
2432
2433public:
2434 less_than_x(const std::vector<V2d<T> >& vertices)
2435 : m_vertices(vertices)
2436 {}
2437 bool operator()(const VertInd a, const VertInd b) const
2438 {
2439 return m_vertices[a].x < m_vertices[b].x;
2440 }
2441};
2442
2443template <typename T>
2444class less_than_y
2445{
2446 const std::vector<V2d<T> >& m_vertices;
2447
2448public:
2449 less_than_y(const std::vector<V2d<T> >& vertices)
2450 : m_vertices(vertices)
2451 {}
2452 bool operator()(const VertInd a, const VertInd b) const
2453 {
2454 return m_vertices[a].y < m_vertices[b].y;
2455 }
2456};
2457
2458} // namespace detail
2459
2460template <typename T, typename TNearPointLocator>
2461void Triangulation<T, TNearPointLocator>::insertVertices_KDTreeBFS(
2462 VertInd superGeomVertCount,
2463 Box2d<T> box)
2464{
2465 // calculate original indices
2466 const VertInd vertexCount(verticesCount() - superGeomVertCount);
2467 if(vertexCount <= VertInd(0))
2468 return;
2469 std::vector<VertInd> ii(vertexCount);
2470 detail::iota(ii.begin(), ii.end(), superGeomVertCount);
2471
2472 typedef std::vector<VertInd>::iterator It;
2474 detail::maxQueueLengthBFSKDTree(vertexCount));
2475 queue.push(make_tuple(ii.begin(), ii.end(), box.min, box.max, VertInd(0)));
2476
2477 It first, last;
2478 V2d<T> newBoxMin, newBoxMax;
2479 VertInd parent, mid;
2480
2481 const detail::less_than_x<T> cmpX(vertices);
2482 const detail::less_than_y<T> cmpY(vertices);
2483
2484 while(!queue.empty())
2485 {
2486#ifdef CDT_ENABLE_CALLBACK_HANDLER
2487 if(m_callbackHandler && m_callbackHandler->isAbortCalculation())
2488 {
2489 return;
2490 }
2491#endif
2492 tie(first, last, box.min, box.max, parent) = queue.front();
2493 queue.pop();
2494 assert(first != last);
2495
2496 const std::ptrdiff_t len = std::distance(first, last);
2497 if(len == 1)
2498 {
2499 insertVertex(*first, parent);
2500 continue;
2501 }
2502 const It midIt = first + len / 2;
2503 if(box.max.x - box.min.x >= box.max.y - box.min.y)
2504 {
2505 detail::portable_nth_element(first, midIt, last, cmpX);
2506 mid = *midIt;
2507 const T split = vertices[mid].x;
2508 newBoxMin.x = split;
2509 newBoxMin.y = box.min.y;
2510 newBoxMax.x = split;
2511 newBoxMax.y = box.max.y;
2512 }
2513 else
2514 {
2515 detail::portable_nth_element(first, midIt, last, cmpY);
2516 mid = *midIt;
2517 const T split = vertices[mid].y;
2518 newBoxMin.x = box.min.x;
2519 newBoxMin.y = split;
2520 newBoxMax.x = box.max.x;
2521 newBoxMax.y = split;
2522 }
2523 insertVertex(mid, parent);
2524 if(first != midIt)
2525 {
2526 queue.push(make_tuple(first, midIt, box.min, newBoxMax, mid));
2527 }
2528 if(midIt + 1 != last)
2529 {
2530 queue.push(make_tuple(midIt + 1, last, newBoxMin, box.max, mid));
2531 }
2532 }
2533}
2534
2535template <typename T, typename TNearPointLocator>
2536std::pair<TriInd, TriInd> Triangulation<T, TNearPointLocator>::edgeTriangles(
2537 const VertInd a,
2538 const VertInd b) const
2539{
2540 const TriInd triStart = m_vertTris[a];
2541 assert(triStart != noNeighbor);
2542 TriInd iT = triStart, iTNext = triStart;
2543 VertInd iV = noVertex;
2544 do
2545 {
2546 const Triangle& t = triangles[iT];
2547 tie(iTNext, iV) = t.next(a);
2548 assert(iTNext != noNeighbor);
2549 if(iV == b)
2550 {
2551 return std::make_pair(iT, iTNext);
2552 }
2553 iT = iTNext;
2554 } while(iT != triStart);
2555 return std::make_pair(noNeighbor, noNeighbor);
2556}
2557
2558template <typename T, typename TNearPointLocator>
2559bool Triangulation<T, TNearPointLocator>::hasEdge(
2560 const VertInd a,
2561 const VertInd b) const
2562{
2563 return edgeTriangles(a, b).first != invalidIndexSizeType;
2564}
2565
2566template <typename T, typename TNearPointLocator>
2567VertInd Triangulation<T, TNearPointLocator>::opposedVertexOnEdge(
2568 const Edge& edge,
2569 const T tolerance) const
2570{
2571 if(fixedEdges.count(edge))
2572 return noVertex;
2573 const V2d<T>& a = vertices[edge.v1()];
2574 const V2d<T>& b = vertices[edge.v2()];
2575 const std::pair<TriInd, TriInd> tris = edgeTriangles(edge.v1(), edge.v2());
2576 const VertInd opposed[] = {
2577 opposedVertex(triangles[tris.first], tris.second),
2578 opposedVertex(triangles[tris.second], tris.first)};
2579 for(int i = 0; i < 2; ++i)
2580 {
2581 const V2d<T>& v = vertices[opposed[i]];
2582 // near line ab and inside its diametral circle: both parts are
2583 // shorter than ab, so splitting terminates
2584 if(locatePointLine(v, a, b, tolerance) == PtLineLocation::OnLine &&
2585 detail::isEncroachingOnEdge(v, a, b))
2586 return opposed[i];
2587 }
2588 return noVertex;
2589}
2590
2594template <typename T, typename TNearPointLocator>
2595bool Triangulation<T, TNearPointLocator>::hasAnotherFixedEdgeAtSmallAngle(
2596 const VertInd v,
2597 const Edge& excludeEdge) const
2598{
2599 // splits stay on the original segment: its direction from v is unchanged
2600 const VertInd iVOther =
2601 excludeEdge.v1() == v ? excludeEdge.v2() : excludeEdge.v1();
2602 const TriInd triStart = m_vertTris[v];
2603 assert(triStart != noNeighbor);
2604 TriInd iT = triStart;
2605 do
2606 {
2607 const Triangle& t = triangles[iT];
2608 TriInd iTNext;
2609 VertInd iV;
2610 tie(iTNext, iV) = t.next(v);
2611 const Edge candidate(v, iV);
2612 if(candidate != excludeEdge && fixedEdges.count(candidate) &&
2613 detail::isAngleAtApexSmall(
2614 vertices[v], vertices[iVOther], vertices[iV]))
2615 {
2616 return true;
2617 }
2618 iT = iTNext;
2619 } while(iT != triStart);
2620 return false;
2621}
2622
2623template <typename T, typename TNearPointLocator>
2624void Triangulation<T, TNearPointLocator>::setAdjacentTriangle(
2625 const VertInd v,
2626 const TriInd t)
2627{
2628 assert(t != noNeighbor);
2629 m_vertTris[v] = t;
2630 assert(
2631 triangles[t].vertices[0] == v || triangles[t].vertices[1] == v ||
2632 triangles[t].vertices[2] == v);
2633}
2634
2635template <typename T, typename TNearPointLocator>
2636void Triangulation<T, TNearPointLocator>::pivotVertexTriangleCW(const VertInd v)
2637{
2638 assert(m_vertTris[v] != noNeighbor);
2639 m_vertTris[v] = triangles[m_vertTris[v]].next(v).first;
2640 assert(m_vertTris[v] != noNeighbor);
2641 assert(
2642 triangles[m_vertTris[v]].vertices[0] == v ||
2643 triangles[m_vertTris[v]].vertices[1] == v ||
2644 triangles[m_vertTris[v]].vertices[2] == v);
2645}
2646
2647template <typename T, typename TNearPointLocator>
2648void Triangulation<T, TNearPointLocator>::tryAddVertexToLocator(const VertInd v)
2649{
2650 if(!m_nearPtLocator.empty()) // only if locator is initialized already
2651 m_nearPtLocator.addPoint(v, vertices);
2652}
2653
2654template <typename T, typename TNearPointLocator>
2655void Triangulation<T, TNearPointLocator>::tryInitNearestPointLocator()
2656{
2657 if(!vertices.empty() && m_nearPtLocator.empty())
2658 {
2659 m_nearPtLocator.initialize(vertices);
2660 }
2661}
2662
2663template <typename T, typename TNearPointLocator>
2665 const VertInd maxVerticesToInsert,
2666 const RefinementCriterion::Enum refinementCriterion,
2667 const T refinementThreshold,
2668 TriIndUSet* const toEraseOrNull,
2669 const T minEdgeLength,
2670 const RefinementOrder::Enum refinementOrder)
2671{
2672 switch(refinementOrder)
2673 {
2676 maxVerticesToInsert,
2677 refinementCriterion,
2678 refinementThreshold,
2679 toEraseOrNull,
2680 minEdgeLength);
2682 return refineTriangles<
2683 std::priority_queue<tuple<T, TriInd, VerticesArr3> > >(
2684 maxVerticesToInsert,
2685 refinementCriterion,
2686 refinementThreshold,
2687 toEraseOrNull,
2688 minEdgeLength);
2689 }
2690 assert(false); // unreachable code
2691 return Unrefined();
2692}
2693
2694template <typename T, typename TNearPointLocator>
2695template <typename TQueue>
2697 const VertInd maxVerticesToInsert,
2698 const RefinementCriterion::Enum refinementCriterion,
2699 const T refinementThreshold,
2700 TriIndUSet* const toEraseOrNull,
2701 const T minEdgeLength)
2702{
2703 if(isFinalized())
2704 handleException(FinalizedError(CDT_SOURCE_LOCATION));
2705
2706 tryInitNearestPointLocator();
2707
2708 Unrefined unrefined;
2709 VertInd remainingVertexBudget = maxVerticesToInsert;
2710 const VertInd steinerVerticesOffset = VertInd(vertices.size());
2711
2712 // split all the encroached constrained (fixed) edges
2713 resolveEncroachedEdges(
2715 remainingVertexBudget,
2716 steinerVerticesOffset,
2717 NULL, // no circumcenter yet: only edge-vs-edge encroachment matters
2718 refinementCriterion,
2719 refinementThreshold,
2720 toEraseOrNull,
2721 minEdgeLength,
2722 unrefined);
2723
2724 // refine triangulation by inserting bad-quality triangles' circumcenters
2725 // keep vertices to detect triangles modified after enqueueing
2726 TQueue badTriangles;
2727 for(TriInd iT(0), n(triangles.size()); iT < n; ++iT)
2728 {
2729 const Triangle& t = triangles[iT];
2730 if(!touchesSuperTriangle(t) &&
2731 !(toEraseOrNull && toEraseOrNull->count(iT)) &&
2732 isRefinementNeeded(t, refinementCriterion, refinementThreshold))
2733 {
2734 badTriangles.push(make_tuple(
2735 triangleBadness(t, refinementCriterion), iT, t.vertices));
2736 }
2737 }
2738
2739 while(!badTriangles.empty() && remainingVertexBudget > 0)
2740 {
2741 const TriInd iT = get<1>(badTriangles.top());
2742 const VerticesArr3 vv = get<2>(badTriangles.top());
2743 const Triangle& t = triangles[iT];
2744 badTriangles.pop();
2745 if(toEraseOrNull && toEraseOrNull->count(iT))
2746 continue;
2747 if(vv != t.vertices)
2748 continue; // skip triangle that got modified since put in queue
2749 if(!isRefinementNeeded(t, refinementCriterion, refinementThreshold) ||
2750 (refinementCriterion == RefinementCriterion::SmallestAngle &&
2751 isSmallestAngleFixed(t)))
2752 {
2753 // fixed-edge sharp angle is impossible to refine
2754 continue;
2755 }
2756 // copy: resolveEncroachedEdges below can re-allocate 'triangles'
2757 const V2d<T>& v0 = vertices[vv[0]];
2758 const V2d<T>& v1 = vertices[vv[1]];
2759 const V2d<T>& v2 = vertices[vv[2]];
2760 const T shortestEdge = std::min(
2761 distance(v0, v1), std::min(distance(v1, v2), distance(v2, v0)));
2762 if(shortestEdge <= minEdgeLength)
2763 {
2764 // same minEdgeLength give-up
2765 ++unrefined.shortEdgeTriangles;
2766 continue;
2767 }
2768 const V2d<T> circumcenterPos = circumcenter(v0, v1, v2);
2769 const OptionalTriInd triAtCircumcenter = walkTriangles(
2770 m_nearPtLocator.nearPoint(circumcenterPos, vertices),
2771 circumcenterPos);
2772 if(!triAtCircumcenter.hasValue())
2773 {
2774 // circumcenter falls outside triangulated area
2775 ++unrefined.circumcenterOutside;
2776 continue;
2777 }
2778
2779 const VertInd budgetBeforeSplits = remainingVertexBudget;
2780 const EdgeVec encroachedEdges =
2781 edgesEncroachedBy(circumcenterPos, triAtCircumcenter.value());
2782 const TriIndVec badTris = resolveEncroachedEdges(
2783 detail::toQueue(encroachedEdges),
2784 remainingVertexBudget,
2785 steinerVerticesOffset,
2786 &circumcenterPos,
2787 refinementCriterion,
2788 refinementThreshold,
2789 toEraseOrNull,
2790 minEdgeLength,
2791 unrefined);
2792 if(!remainingVertexBudget)
2793 break;
2794
2795 // a circumcenter encroaching on fixed edges:
2796 // split edges instead and re-visit the triangle later
2797 if(remainingVertexBudget != budgetBeforeSplits || !badTris.empty())
2798 {
2799 typedef TriIndVec::const_iterator It;
2800 for(It it = badTris.begin(); it != badTris.end(); ++it)
2801 {
2802 const Triangle& bad = triangles[*it];
2803 badTriangles.push(make_tuple(
2804 triangleBadness(bad, refinementCriterion),
2805 *it,
2806 bad.vertices));
2807 }
2808 badTriangles.push(make_tuple(
2809 triangleBadness(triangles[iT], refinementCriterion),
2810 iT,
2811 triangles[iT].vertices));
2812 continue;
2813 }
2814 // splitting was given up on: inserting would encroach anyway
2815 if(!encroachedEdges.empty())
2816 continue;
2817
2818 const TriInd iCircumcenterTri = triAtCircumcenter.value();
2819 const Triangle& circumcenterTri = triangles[iCircumcenterTri];
2821 circumcenterPos,
2822 vertices[circumcenterTri.vertices[0]],
2823 vertices[circumcenterTri.vertices[1]],
2824 vertices[circumcenterTri.vertices[2]]);
2825 if(loc == PtTriLocation::OnVertex)
2826 {
2827 // circumcenter coincides with an existing vertex
2828 ++unrefined.circumcenterOnVertex;
2829 continue;
2830 }
2831 const array<TriInd, 2> trisAt = {
2832 iCircumcenterTri,
2833 isOnEdge(loc) ? circumcenterTri.neighbors[edgeNeighbor(loc)]
2834 : noNeighbor};
2835 // Skip adding a Steiner point if the triangle will be removed anyway.
2836 if(toEraseOrNull &&
2837 (toEraseOrNull->count(trisAt[0]) || toEraseOrNull->count(trisAt[1])))
2838 {
2839 // circumcenter falls into a triangle that will be removed
2840 ++unrefined.circumcenterOutside;
2841 continue;
2842 }
2843
2844 --remainingVertexBudget;
2845 const VertInd iVert = static_cast<VertInd>(vertices.size());
2846 addNewVertex(circumcenterPos, noNeighbor);
2847#ifdef CDT_ENABLE_CALLBACK_HANDLER
2848 if(m_callbackHandler)
2849 {
2850 m_callbackHandler->onAddVertexStart(
2852 }
2853#endif
2854 std::stack<TriInd> triStack =
2855 trisAt[1] == noNeighbor
2856 ? insertVertexInsideTriangle(iVert, trisAt[0])
2857 : insertVertexOnEdge(iVert, trisAt[0], trisAt[1], true);
2858 ensureDelaunayByEdgeFlips(iVert, triStack);
2859 tryAddVertexToLocator(iVert);
2860
2861 TriInd start = m_vertTris[iVert];
2862 TriInd currTri = start;
2863 do
2864 {
2865 const Triangle& t = triangles[currTri];
2866 if(!(toEraseOrNull && toEraseOrNull->count(currTri)) &&
2868 isRefinementNeeded(t, refinementCriterion, refinementThreshold))
2869 {
2870 badTriangles.push(make_tuple(
2871 triangleBadness(t, refinementCriterion),
2872 currTri,
2873 t.vertices));
2874 }
2875 currTri = t.next(iVert).first;
2876 } while(currTri != start);
2877 }
2878
2879 // sharp corners that come from the input are impossible to refine: report
2880 // the triangles that are left too sharp because of them
2881 if(refinementCriterion == RefinementCriterion::SmallestAngle)
2882 {
2883 for(TriInd iT(0), n(triangles.size()); iT < n; ++iT)
2884 {
2885 const Triangle& t = triangles[iT];
2886 if(touchesSuperTriangle(t) ||
2887 (toEraseOrNull && toEraseOrNull->count(iT)))
2888 {
2889 continue;
2890 }
2891 if(isRefinementNeeded(
2892 t, refinementCriterion, refinementThreshold) &&
2893 isSmallestAngleFixed(t))
2894 {
2895 ++unrefined.sharpFixedCorner;
2896 }
2897 }
2898 }
2899 return unrefined;
2900}
2901
2902} // namespace CDT
2903
2904CDT_RESTORE_MATH_SETTINGS_FOR_CONSTRUCTIONS
2905
2906#endif // header-guard
Triangulation class.
void iota(ForwardIt first, ForwardIt last, T value)
backport from c++11
std::size_t maxQueueLengthBFSKDTree(const std::size_t vertexCount)
Since KD-tree bulk load builds a balanced tree the maximum length of a queue can be pre-calculated: i...
bool isAngleAtApexSmall(const V2d< T > &apex, const V2d< T > &a, const V2d< T > &b)
Whether the angle at apex between apex->a and apex->b is smaller than 60°
EdgeQueue toQueue(const EdgeVec &edges)
Turn a vector into a queue, preserving order.
void sortUnique(TVec &v)
Sort a vector and remove its duplicate elements.
Base class for errors.
Error thrown when triangulation modification is attempted after it was finalized.
Interface for the callback handler that user can derive from and inject into the triangulation to mon...
Error thrown when intersecting constraint edges are detected, but triangulation is not configured to ...
Error thrown when resolving intersecting constraints fails: floating-point rounding places the comput...
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< 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...
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.
void eraseSuperTriangle()
Erase triangles adjacent to super triangle.
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
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
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY VertInd opposedVertex(const Triangle &tri, TriInd iTopo)
Given two triangles, return vertex of first triangle opposed to the second.
Definition CDTUtils.hpp:204
unordered_set< Edge > EdgeUSet
Hash table of edges.
Definition CDTUtils.h:402
VertInd edge_get_v2(const Edge &e)
Get edge second vertex.
Definition CDTUtils.h:388
CDT_EXPORT T area(const V2d< T > &a, const V2d< T > &b, const V2d< T > &c)
Surface area of a triangle ABC.
Definition CDTUtils.hpp:303
std::vector< TriInd > TriIndVec
Vector of triangle indices.
Definition CDTUtils.h:271
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index edgeNeighborInd(const VerticesArr3 &vv, VertInd iVedge1, VertInd iVedge2)
Index of triangle's neighbor opposed to an edge.
Definition CDTUtils.hpp:142
VertInd edge_get_v1(const Edge &e)
Get edge first vertex.
Definition CDTUtils.h:382
CDT_EXPORT PtLineLocation::Enum classifyOrientation(T orientation, T orientationTolerance=T(0))
Classify value of orient2d predicate.
Definition CDTUtils.hpp:64
array< TriInd, 3 > NeighborsArr3
array of three neighbors
Definition CDTUtils.h:273
CDT_EXPORT T distance(const V2d< T > &a, const V2d< T > &b)
Distance between two 2D points.
Definition CDTUtils.hpp:251
IndexSizeType VertInd
Vertex index.
Definition CDTUtils.h:253
CDT_EXPORT Index cw(Index i)
Advance vertex or neighbor index clockwise.
Definition CDTUtils.hpp:28
array< VertInd, 3 > VerticesArr3
array of three vertex indices
Definition CDTUtils.h:272
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY bool touchesSuperTriangle(const Triangle &t)
Check if any of triangle's vertices belongs to a super-triangle.
Definition CDTUtils.hpp:262
array< T, 3 > arr3(const T &v0, const T &v1, const T &v2)
Needed for c++03 compatibility (no uniform initialization available)
Definition CDTUtils.h:174
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opoNbr(Index vertIndex)
Opposed neighbor index from vertex index.
Definition CDTUtils.hpp:105
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index vertexInd(const VerticesArr3 &vv, VertInd iV)
If triangle has a given vertex return vertex-index.
Definition CDTUtils.hpp:187
CDT_EXPORT T orient2D(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2)
Orient p against line v1-v2 2D: robust geometric predicate.
Definition CDTUtils.hpp:47
CDT_EXPORT PtLineLocation::Enum locatePointLine(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2, T orientationTolerance=T(0))
Check if point lies to the left of, to the right of, or on a line.
Definition CDTUtils.hpp:53
CDT_EXPORT T distanceSquared(const V2d< T > &a, const V2d< T > &b)
Squared distance between two 2D points.
Definition CDTUtils.hpp:257
unordered_set< TriInd > TriIndUSet
Hash table of triangles.
Definition CDTUtils.h:403
CDT_EXPORT Index ccw(Index i)
Advance vertex or neighbor index counter-clockwise.
Definition CDTUtils.hpp:23
CDT_EXPORT V2d< T > circumcenter(V2d< T > a, V2d< T > b, V2d< T > c)
Position of ABC triangle circumcenter.
Definition CDTUtils.hpp:309
CDT_EXPORT Index edgeNeighbor(PtTriLocation::Enum location)
Neighbor index from a on-edge location.
Definition CDTUtils.hpp:40
unordered_map< TriInd, TriInd > TriIndUMap
Triangle hash map.
Definition CDTUtils.h:404
const T & getX_V2d(const V2d< T > &v)
X- coordinate getter for V2d.
Definition CDTUtils.h:210
unsigned char Index
Index in triangle.
Definition CDTUtils.h:251
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY TriInd opposedTriangle(const Triangle &tri, VertInd iVert)
Given triangle and a vertex find opposed triangle.
Definition CDTUtils.hpp:198
CDT_EXPORT T smallestAngle(const V2d< T > &a, const V2d< T > &b, const V2d< T > &c)
Smallest angle of triangle ABC in radians.
Definition CDTUtils.hpp:322
CDT_EXPORT bool isInCircumcircle(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2, const V2d< T > &v3)
Test if point lies in a circumscribed circle of a triangle.
Definition CDTUtils.hpp:217
Edge RemapNoSuperTriangle(const Edge &e)
Remap removing super-triangle: subtract 3 from vertices.
CDT_EXPORT bool isOnEdge(PtTriLocation::Enum location)
Check if location is classified as on any of three edges.
Definition CDTUtils.hpp:33
IndexSizeType TriInd
Triangle index.
Definition CDTUtils.h:255
CDT_EXPORT PtTriLocation::Enum locatePointTriangle(const V2d< T > &p, const V2d< T > &v1, const V2d< T > &v2, const V2d< T > &v3)
Check if point a lies inside of, outside of, or on an edge of a triangle.
Definition CDTUtils.hpp:74
std::queue< Edge > EdgeQueue
Queue of edges.
Definition CDTUtils.h:400
CDT_EXPORT CDT_INLINE_IF_HEADER_ONLY Index opposedVertexInd(const NeighborsArr3 &nn, TriInd iTopo)
Index of triangle's vertex opposed to a triangle.
Definition CDTUtils.hpp:176
const T & getY_V2d(const V2d< T > &v)
Y-coordinate getter for V2d.
Definition CDTUtils.h:217
Enum
The Enum itself.
@ RefinementCircumcenter
Refinement: circumcenter of a poor-quality triangle.
@ FixedEdgeMidpoint
During conforming triangulation edge mid-point is added.
@ FixedEdgesIntersection
Resolving fixed/constraint edges' intersection.
2D bounding box
Definition CDTUtils.h:278
V2d< T > max
max box corner
Definition CDTUtils.h:280
V2d< T > min
min box corner
Definition CDTUtils.h:279
Edge connecting two vertices: vertex with smaller index is always first.
Definition CDTUtils.h:333
VertInd v1() const
V1 getter.
Definition CDTUtils.h:360
VertInd v2() const
V2 getter.
Definition CDTUtils.h:366
@ TryResolve
attempt to resolve constraint edge intersections
@ NotAllowed
constraint edge intersections are not allowed
@ DontCheck
No checks: slightly faster but less safe.
@ LargestArea
constraint maximum triangles area
@ SmallestAngle
constraint minimum triangles angle
Enum
The Enum itself.
@ FIFO
in the order found
@ WorstFirst
worst first
Triangulation triangle (counter-clockwise winding)
Definition CDTUtils.h:415
VerticesArr3 vertices
triangle's three vertices
Definition CDTUtils.h:416
NeighborsArr3 neighbors
triangle's three neighbors
Definition CDTUtils.h:417
std::pair< TriInd, VertInd > next(const VertInd i) const
Next triangle adjacent to a vertex (clockwise)
Definition CDTUtils.h:433
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
T y
Y-coordinate.
Definition CDTUtils.h:193
T x
X-coordinate.
Definition CDTUtils.h:192
@ Auto
Automatic insertion order optimized for better performance.