26template <std::
size_t I,
typename T>
29 inline static typename std::tuple_element<I, T>::type get(
const T& t)
31 return std::get<I>(t);
39template <
typename N = u
int32_t>
43 std::vector<N> indices;
44 std::size_t vertices = 0;
46 template <
typename Polygon>
47 void operator()(
const Polygon& points);
54 Node(N index,
double x_,
double y_)
57 , i(index & ((N(1) << (
sizeof(N) * 8 - 1)) - 1))
60 Node(
const Node&) =
delete;
61 Node& operator=(
const Node&) =
delete;
62 Node(Node&&) =
delete;
63 Node& operator=(Node&&) =
delete;
76 const N i : (
sizeof(N) * 8 - 1);
82 Node* prevZ =
nullptr;
83 Node* nextZ =
nullptr;
94 const double minX, minY, maxX, maxY;
96 Triangle(
const Node* a,
const Node* b,
const Node* c)
103 , minX(std::min<double>(ax, std::min<double>(bx, cx)))
104 , minY(std::min<double>(ay, std::min<double>(by, cy)))
105 , maxX(std::max<double>(ax, std::max<double>(bx, cx)))
106 , maxY(std::max<double>(ay, std::max<double>(by, cy)))
109 inline double area()
const {
return (by - ay) * (cx - bx) - (bx - ax) * (cy - by); }
111 inline bool inBBox(
double px,
double py)
const
113 return px >= minX && px <= maxX && py >= minY && py <= maxY;
116 inline bool containsPoint(
double px,
double py)
const
118 return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
119 (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
120 (bx - px) * (cy - py) >= (cx - px) * (by - py);
124 inline bool containsPointExceptFirst(
double px,
double py)
const
126 return !(ax == px && ay == py) && containsPoint(px, py);
130 template <
typename Ring>
131 Node* linkedList(
const Ring& points,
const bool clockwise);
132 Node* filterPoints(Node* start, Node* end =
nullptr);
133 void earcutLinked(Node* ear);
134 bool isEar(Node* ear);
135 bool isEarHashed(Node* ear);
136 Node* cureLocalIntersections(Node* start);
137 void splitEarcut(Node* start);
138 template <
typename Polygon>
139 Node* eliminateHoles(
const Polygon& points, Node* outerNode);
140 Node* eliminateHole(Node* hole, Node* outerNode);
141 Node* findHoleBridge(Node* hole, Node* outerNode);
142 void buildBlockIndex(std::size_t maxNodes, std::size_t numHoles);
143 void indexSegment(Node* head, Node* stop);
144 void growBlock(Node* head, Node* tail);
145 Node* liveBlockHead(std::size_t b);
146 Node* liveBlockStop(std::size_t b);
147 bool sectorContainsSector(
const Node* m,
const Node* p);
148 void indexCurve(Node* start);
149 Node* sortLinked(Node* list);
150 int32_t zOrder(
const double x_,
const double y_);
151 Node* getLeftmost(Node* start);
152 bool pointInTriangle(
161 bool isValidDiagonal(Node* a, Node* b);
162 double area(
const Node* p,
const Node* q,
const Node* r)
const;
163 bool equals(
const Node* p1,
const Node* p2);
169 bool includeBoundary =
true);
170 bool onSegment(
const Node* p,
const Node* q,
const Node* r);
171 bool intersectsPolygon(
const Node* a,
const Node* b);
172 bool locallyInside(
const Node* a,
const Node* b);
173 bool middleInside(
const Node* a,
const Node* b);
174 Node* splitPolygon(Node* a, Node* b);
175 template <
typename Po
int>
176 Node* insertNode(std::size_t i,
const Point& p, Node* last);
177 void removeNode(Node* p);
182 bool filteredOut =
false;
187 template <
typename T,
typename Alloc = std::allocator<T>>
191 ObjectPool(std::size_t blockSize_)
192 : baseBlockSize(std::max<std::size_t>(blockSize_, 256))
196 ~ObjectPool() { clear(); }
197 template <
typename...
Args>
198 T* construct(
Args&&... args)
201 if (currentIndex >= baseBlockSize) {
203 if (currentBlockIndex < memoryBlocks.size()) {
212 T*
object = memoryBlocks[currentBlockIndex].get() + currentIndex;
213 alloc_traits::construct(alloc,
object, std::forward<Args>(args)...);
221 std::size_t objectsDestroyed = 0;
222 for (std::size_t blockIdx = 0;
223 blockIdx < memoryBlocks.size() && objectsDestroyed < totalObjects;
226 std::size_t objectsInThisBlock =
227 std::min(baseBlockSize, totalObjects - objectsDestroyed);
228 for (std::size_t i = 0; i < objectsInThisBlock; ++i) {
229 T*
object = memoryBlocks[blockIdx].get() + i;
230 alloc_traits::destroy(alloc,
object);
232 objectsDestroyed += objectsInThisBlock;
235 currentBlockIndex = 0;
242 typedef typename std::allocator_traits<Alloc> alloc_traits;
248 std::size_t capacity;
249 void operator()(T* ptr) { alloc_traits::deallocate(alloc, ptr, capacity); }
252 std::vector<std::unique_ptr<T[], AllocDeleter>> memoryBlocks;
253 std::size_t currentBlockIndex = 0;
254 std::size_t currentIndex = 0;
255 std::size_t totalObjects = 0;
256 const std::size_t baseBlockSize;
258 void allocateNewBlock()
260 T* rawMemory = alloc_traits::allocate(alloc, baseBlockSize);
262 std::unique_ptr<T[], AllocDeleter>(rawMemory, AllocDeleter{alloc, baseBlockSize});
263 memoryBlocks.push_back(std::move(newBlock));
264 currentBlockIndex = memoryBlocks.size() - 1;
269 std::unique_ptr<ObjectPool<Node>> nodes;
270 std::vector<Node*> holeQueue;
272 std::vector<Node*> sortBuffer;
286 static constexpr int32_t K = 16;
287 std::vector<double> blockBBox;
288 std::vector<Node*> blockHead;
289 std::vector<Node*> blockStop;
290 std::size_t numBlocks = 0;
293 bool indexActive =
false;
297template <
typename Polygon>
298void Earcut<N>::operator()(
const Polygon& points)
304 if (points.empty())
return;
311 const std::size_t numRings =
static_cast<std::size_t
>(points.size());
312 for (std::size_t i = 0; threshold >= 0 && i < numRings; i++) {
313 threshold -=
static_cast<int>(points[i].size());
314 len +=
static_cast<std::size_t
>(points[i].size());
319 std::size_t estimatedNodes = len * 3 / 2;
320 nodes = std::make_unique<ObjectPool<Node>>(std::max<std::size_t>(estimatedNodes, 256));
322 indices.reserve(len +
static_cast<std::size_t
>(points[0].size()));
324 Node* outerNode = linkedList(points[0],
true);
325 if (!outerNode || outerNode->prev == outerNode->next)
return;
327 if (points.size() > 1) outerNode = eliminateHoles(points, outerNode);
330 hashing = threshold < 0;
332 Node* p = outerNode->next;
333 minX = maxX = outerNode->x;
334 minY = maxY = outerNode->y;
338 minX = std::min<double>(minX, x);
339 minY = std::min<double>(minY, y);
340 maxX = std::max<double>(maxX, x);
341 maxY = std::max<double>(maxY, y);
343 }
while (p != outerNode);
347 inv_size = std::max<double>(maxX - minX, maxY - minY);
348 inv_size = inv_size != .0 ? (32767. / inv_size) : .0;
351 earcutLinked(outerNode);
359template <
typename Ring>
360typename Earcut<N>::Node* Earcut<N>::linkedList(
const Ring& points,
const bool clockwise)
362 using Point =
typename Ring::value_type;
364 const std::size_t len =
static_cast<std::size_t
>(points.size());
366 Node* last =
nullptr;
369 for (i = 0, j = len > 0 ? len - 1 : 0; i < len; j = i++) {
370 const auto& p1 = points[i];
371 const auto& p2 = points[j];
372 const double p20 = util::nth<0, Point>::get(p2);
373 const double p10 = util::nth<0, Point>::get(p1);
374 const double p11 = util::nth<1, Point>::get(p1);
375 const double p21 = util::nth<1, Point>::get(p2);
376 sum += (p20 - p10) * (p11 + p21);
380 if (clockwise == (sum > 0)) {
381 for (i = 0; i < len; i++) last = insertNode(vertices + i, points[i], last);
383 for (i = len; i-- > 0;) last = insertNode(vertices + i, points[i], last);
386 if (last && equals(last, last->next)) {
402typename Earcut<N>::Node* Earcut<N>::filterPoints(Node* start, Node* end)
404 if (!start)
return start;
405 const bool full = !end;
406 if (full) end = start;
412 if (p != p->next && !p->steiner && (equals(p, p->next) || area(p->prev, p, p->next) == 0)) {
413 if (full || p == end) end = p->prev;
418 }
else if (full || p != end) {
422 }
while (again || p != end);
429void Earcut<N>::earcutLinked(Node* ear)
434 if (hashing) indexCurve(ear);
442 while (ear->prev != ear->next) {
447 if (area(prev, ear, next) < 0 && (hashing ? isEarHashed(ear) : isEar(ear))) {
449 indices.emplace_back(prev->i);
450 indices.emplace_back(ear->i);
451 indices.emplace_back(next->i);
468 ear = filterPoints(ear);
476 ear = cureLocalIntersections(ear);
491bool Earcut<N>::isEar(Node* ear)
493 const Node* a = ear->prev;
495 const Node* c = ear->next;
498 const Triangle tri(a, b, c);
501 Node* p = ear->next->next;
503 while (p != ear->prev) {
504 if (tri.inBBox(p->x, p->y) && tri.containsPointExceptFirst(p->x, p->y) &&
505 area(p->prev, p, p->next) >= 0)
514bool Earcut<N>::isEarHashed(Node* ear)
516 const Node* a = ear->prev;
518 const Node* c = ear->next;
521 const Triangle tri(a, b, c);
524 const int32_t minZ = zOrder(tri.minX, tri.minY);
525 const int32_t maxZ = zOrder(tri.maxX, tri.maxY);
528 Node* p = ear->nextZ;
530 while (p && p->z <= maxZ) {
531 if (p != ear->next && tri.inBBox(p->x, p->y) && tri.containsPointExceptFirst(p->x, p->y) &&
532 area(p->prev, p, p->next) >= 0)
540 while (p && p->z >= minZ) {
541 if (p != ear->next && tri.inBBox(p->x, p->y) && tri.containsPointExceptFirst(p->x, p->y) &&
542 area(p->prev, p, p->next) >= 0)
552typename Earcut<N>::Node* Earcut<N>::cureLocalIntersections(Node* start)
558 Node* b = p->next->next;
562 if (intersects(a, p, p->next, b,
false) && locallyInside(a, b) && locallyInside(b, a)) {
563 indices.emplace_back(a->i);
564 indices.emplace_back(p->i);
565 indices.emplace_back(b->i);
575 }
while (p != start);
577 return cured ? filterPoints(p) : p;
582void Earcut<N>::splitEarcut(Node* start)
587 Node* b = a->next->next;
588 while (b != a->prev) {
589 if (a->i != b->i && isValidDiagonal(a, b)) {
591 Node* c = splitPolygon(a, b);
594 a = filterPoints(a, a->next);
595 c = filterPoints(c, c->next);
605 }
while (a != start);
610template <
typename Polygon>
611typename Earcut<N>::Node* Earcut<N>::eliminateHoles(
const Polygon& points, Node* outerNode)
613 const size_t len =
static_cast<std::size_t
>(points.size());
616 for (
size_t i = 1; i < len; i++) {
617 Node* list = linkedList(points[i],
false);
619 if (list == list->next) list->steiner =
true;
620 holeQueue.push_back(getLeftmost(list));
625 std::sort(holeQueue.begin(), holeQueue.end(), [](
const Node* a,
const Node* b) {
626 if (a->x != b->x) return a->x < b->x;
627 if (a->y != b->y) return a->y < b->y;
628 const double adx = a->next->x - a->x, ady = a->next->y - a->y;
629 const double bdx = b->next->x - b->x, bdy = b->next->y - b->y;
630 const bool aDegenerate = adx == 0 && ady == 0;
631 const bool bDegenerate = bdx == 0 && bdy == 0;
632 if (aDegenerate != bDegenerate) return aDegenerate;
633 return ady * bdx < bdy * adx;
638 buildBlockIndex(vertices, holeQueue.size());
639 indexSegment(outerNode, outerNode);
644 for (
size_t i = 0; i < holeQueue.size(); i++) {
645 outerNode = eliminateHole(holeQueue[i], outerNode);
650 return filterPoints(outerNode);
655typename Earcut<N>::Node* Earcut<N>::eliminateHole(Node* hole, Node* outerNode)
657 Node* bridge = findHoleBridge(hole, outerNode);
662 Node* bridgeReverse = splitPolygon(bridge, hole);
668 Node* bridge2 = bridgeReverse->next;
669 indexSegment(bridge, bridge2->next);
672 filterPoints(bridgeReverse, bridgeReverse->next);
673 return filterPoints(bridge, bridge->next);
678typename Earcut<N>::Node* Earcut<N>::findHoleBridge(Node* hole, Node* outerNode)
683 double qx = -std::numeric_limits<double>::max();
689 if (equals(hole, p))
return p;
693 for (std::size_t b = 0, g = 0; b < numBlocks; b++, g += 4) {
694 if (hy < blockBBox[g + 1] || hy > blockBBox[g + 3] || blockBBox[g] > hx ||
695 blockBBox[g + 2] <= qx)
699 const Node* stop = liveBlockStop(b);
700 p = liveBlockHead(b);
702 if (p->prev->next == p) {
703 if (equals(hole, p->next))
705 else if (hy <= p->y && hy >= p->next->y && p->next->y != p->y) {
706 double x = p->x + (hy - p->y) * (p->next->x - p->x) / (p->next->y - p->y);
707 if (x <= hx && x > qx) {
709 m = p->x < p->next->x ? p : p->next;
710 if (x == hx)
return m;
724 const double mx = m->x;
725 const double my = m->y;
726 const double tminY = std::min<double>(hy, my);
727 const double tmaxY = std::max<double>(hy, my);
728 double tanMin = std::numeric_limits<double>::max();
732 for (std::size_t b = 0, g = 0; b < numBlocks; b++, g += 4) {
733 if (blockBBox[g + 2] < mx || blockBBox[g] > hx || blockBBox[g + 3] < tminY ||
734 blockBBox[g + 1] > tmaxY)
737 const Node* stop = liveBlockStop(b);
738 p = liveBlockHead(b);
740 if (p->prev->next == p && hx >= p->x && p->x >= mx && hx != p->x &&
741 pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p->x, p->y)) {
742 const double tanCur = std::abs(hy - p->y) / (hx - p->x);
746 if ((locallyInside(p, hole) ||
747 (p->y == hy && p->next->y == hy && p->next->x > hx)) &&
750 (p->x > m->x || (p->x == m->x && sectorContainsSector(m, p)))))) {
764void Earcut<N>::buildBlockIndex(std::size_t maxNodes, std::size_t numHoles)
768 const std::size_t maxBlocks = (maxNodes + 2 * numHoles + K - 1) / K + numHoles + 2;
769 if (blockBBox.size() < maxBlocks * 4) blockBBox.resize(maxBlocks * 4);
770 if (blockHead.size() < maxBlocks) {
771 blockHead.resize(maxBlocks);
772 blockStop.resize(maxBlocks);
780void Earcut<N>::indexSegment(Node* head, Node* stop)
784 const std::size_t b = numBlocks++;
786 double bMinX = std::numeric_limits<double>::max();
787 double bMinY = std::numeric_limits<double>::max();
788 double bMaxX = std::numeric_limits<double>::lowest();
789 double bMaxY = std::numeric_limits<double>::lowest();
793 p->z =
static_cast<int32_t
>(
795 if (p->x < bMinX) bMinX = p->x;
796 if (p->x > bMaxX) bMaxX = p->x;
797 if (p->y < bMinY) bMinY = p->y;
798 if (p->y > bMaxY) bMaxY = p->y;
799 if (c->x < bMinX) bMinX = c->x;
800 if (c->x > bMaxX) bMaxX = c->x;
801 if (c->y < bMinY) bMinY = c->y;
802 if (c->y > bMaxY) bMaxY = c->y;
804 }
while (++k < K && p != stop);
806 const std::size_t g = b * 4;
807 blockBBox[g] = bMinX;
808 blockBBox[g + 1] = bMinY;
809 blockBBox[g + 2] = bMaxX;
810 blockBBox[g + 3] = bMaxY;
818void Earcut<N>::growBlock(Node* head, Node* tail)
820 const std::size_t g =
static_cast<std::size_t
>(head->z) * 4;
821 if (tail->x < blockBBox[g]) blockBBox[g] = tail->x;
822 if (tail->y < blockBBox[g + 1]) blockBBox[g + 1] = tail->y;
823 if (tail->x > blockBBox[g + 2]) blockBBox[g + 2] = tail->x;
824 if (tail->y > blockBBox[g + 3]) blockBBox[g + 3] = tail->y;
832typename Earcut<N>::Node* Earcut<N>::liveBlockHead(std::size_t b)
834 Node* head = blockHead[b];
835 while (head->prev->next != head) head = head->next;
841typename Earcut<N>::Node* Earcut<N>::liveBlockStop(std::size_t b)
843 Node* stop = blockStop[b];
844 while (stop->prev->next != stop) stop = stop->next;
851bool Earcut<N>::sectorContainsSector(
const Node* m,
const Node* p)
853 return area(m->prev, m, p->prev) < 0 && area(p->next, m, m->next) < 0;
858void Earcut<N>::indexCurve(Node* start)
865 p->z = zOrder(p->x, p->y);
869 }
while (p != start);
871 p->prevZ->nextZ =
nullptr;
882typename Earcut<N>::Node* Earcut<N>::sortLinked(Node* list)
887 for (Node* p = list; p; p = p->nextZ) sortBuffer.push_back(p);
889 std::sort(sortBuffer.begin(), sortBuffer.end(), [](
const Node* a,
const Node* b) {
894 Node* prev =
nullptr;
895 for (Node* p : sortBuffer) {
897 if (prev) prev->nextZ = p;
900 prev->nextZ =
nullptr;
901 return sortBuffer.front();
906int32_t Earcut<N>::zOrder(
const double x_,
const double y_)
909 int32_t x =
static_cast<int32_t
>((x_ - minX) * inv_size);
910 int32_t y =
static_cast<int32_t
>((y_ - minY) * inv_size);
912 x = (x | (x << 8)) & 0x00FF00FF;
913 x = (x | (x << 4)) & 0x0F0F0F0F;
914 x = (x | (x << 2)) & 0x33333333;
915 x = (x | (x << 1)) & 0x55555555;
917 y = (y | (y << 8)) & 0x00FF00FF;
918 y = (y | (y << 4)) & 0x0F0F0F0F;
919 y = (y | (y << 2)) & 0x33333333;
920 y = (y | (y << 1)) & 0x55555555;
927typename Earcut<N>::Node* Earcut<N>::getLeftmost(Node* start)
930 Node* leftmost = start;
932 if (p->x < leftmost->x || (p->x == leftmost->x && p->y < leftmost->y)) leftmost = p;
934 }
while (p != start);
941bool Earcut<N>::pointInTriangle(
951 return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
952 (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
953 (bx - px) * (cy - py) >= (cx - px) * (by - py);
958bool Earcut<N>::isValidDiagonal(Node* a, Node* b)
961 const bool zeroLength =
962 equals(a, b) && area(a->prev, a, a->next) > 0 && area(b->prev, b, b->next) > 0;
963 return a->next->i != b->i &&
964 (zeroLength || (locallyInside(a, b) && locallyInside(b, a) &&
965 (area(a->prev, a, b->prev) != 0.0 ||
966 area(a, b->prev, b) != 0.0))) &&
967 !intersectsPolygon(a, b) &&
968 (zeroLength || middleInside(a, b));
973double Earcut<N>::area(
const Node* p,
const Node* q,
const Node* r)
const
975 return (q->y - p->y) * (r->x - q->x) - (q->x - p->x) * (r->y - q->y);
980bool Earcut<N>::equals(
const Node* p1,
const Node* p2)
982 return p1->x == p2->x && p1->y == p2->y;
987bool Earcut<N>::intersects(
992 bool includeBoundary)
994 const double o1 = area(p1, q1, p2);
995 const double o2 = area(p1, q1, q2);
996 const double o3 = area(p2, q2, p1);
997 const double o4 = area(p2, q2, q1);
1000 if (((o1 > 0 && o2 < 0) || (o1 < 0 && o2 > 0)) && ((o3 > 0 && o4 < 0) || (o3 < 0 && o4 > 0)))
1003 if (!includeBoundary)
return false;
1005 if (o1 == 0 && onSegment(p1, p2, q1))
1007 if (o2 == 0 && onSegment(p1, q2, q1))
1009 if (o3 == 0 && onSegment(p2, p1, q2))
1011 if (o4 == 0 && onSegment(p2, q1, q2))
1018template <
typename N>
1019bool Earcut<N>::onSegment(
const Node* p,
const Node* q,
const Node* r)
1021 return q->x <= std::max<double>(p->x, r->x) && q->x >= std::min<double>(p->x, r->x) &&
1022 q->y <= std::max<double>(p->y, r->y) && q->y >= std::min<double>(p->y, r->y);
1026template <
typename N>
1027bool Earcut<N>::intersectsPolygon(
const Node* a,
const Node* b)
1031 const double diagMinX = std::min<double>(a->x, b->x);
1032 const double diagMaxX = std::max<double>(a->x, b->x);
1033 const double diagMinY = std::min<double>(a->y, b->y);
1034 const double diagMaxY = std::max<double>(a->y, b->y);
1038 const Node* n = p->next;
1039 if ((p->x > diagMaxX && n->x > diagMaxX) || (p->x < diagMinX && n->x < diagMinX) ||
1040 (p->y > diagMaxY && n->y > diagMaxY) || (p->y < diagMinY && n->y < diagMinY)) {
1044 if (p->i != a->i && n->i != a->i && p->i != b->i && n->i != b->i && intersects(p, n, a, b))
1053template <
typename N>
1054bool Earcut<N>::locallyInside(
const Node* a,
const Node* b)
1056 return area(a->prev, a, a->next) < 0 ? area(a, b, a->next) >= 0 && area(a, a->prev, b) >= 0
1057 : area(a, b, a->prev) < 0 || area(a, a->next, b) < 0;
1061template <
typename N>
1062bool Earcut<N>::middleInside(
const Node* a,
const Node* b)
1065 bool inside =
false;
1066 double px = (a->x + b->x) / 2;
1067 double py = (a->y + b->y) / 2;
1069 const Node* n = p->next;
1070 if (((p->y > py) != (n->y > py)) &&
1071 (px < (n->x - p->x) * (py - p->y) / (n->y - p->y) + p->x))
1082template <
typename N>
1083typename Earcut<N>::Node* Earcut<N>::splitPolygon(Node* a, Node* b)
1085 Node* a2 = nodes->construct(a->i, a->x, a->y);
1086 Node* b2 = nodes->construct(b->i, b->x, b->y);
1106template <
typename N>
1107template <
typename Po
int>
1108typename Earcut<N>::Node* Earcut<N>::insertNode(std::size_t i,
const Point& pt, Node* last)
1110 Node* p = nodes->construct(
1112 util::nth<0, Point>::get(pt),
1113 util::nth<1, Point>::get(pt));
1121 p->next = last->next;
1123 last->next->prev = p;
1129template <
typename N>
1130void Earcut<N>::removeNode(Node* p)
1132 p->next->prev = p->prev;
1133 p->prev->next = p->next;
1135 if (p->prevZ) p->prevZ->nextZ = p->nextZ;
1136 if (p->nextZ) p->nextZ->prevZ = p->prevZ;
1139 if (indexActive) growBlock(p->prev, p->next);
1143template <
typename N = u
int32_t,
typename Polygon>
1144std::vector<N> earcut(
const Polygon& poly)
1146 mapbox::detail::Earcut<N> earcut;
1148 return std::move(earcut.indices);
1157template <
typename N>
1164 template <
typename Coords>
1165 void operator()(std::vector<N>& triangles,
const Coords& coords)
1167 using Point =
typename std::decay<
decltype(coords[0])>::type;
1168 const int n =
static_cast<int>(triangles.size());
1170 ensureScratch(
static_cast<std::size_t
>(n));
1172 std::fill(heVec.begin(), heVec.begin() + n, -1);
1176 N* t = triangles.data();
1177 int32_t* he = heVec.data();
1178 int32_t* edgeStack = edgeStackVec.data();
1179 int32_t* hTable = hTableVec.data();
1180 uint32_t* hStamp = hStampVec.data();
1181 uint8_t* edgeStamp = edgeStampVec.data();
1183 auto X = [&](N p) ->
double {
1184 return static_cast<double>(util::nth<0, Point>::get(coords[p]));
1186 auto Y = [&](N p) ->
double {
1187 return static_cast<double>(util::nth<1, Point>::get(coords[p]));
1196 for (
int e = 0; e < n; e++) {
1197 const N a = t[e], b = t[nextHE(e)];
1198 const N lo = a < b ? a : b, hi = a < b ? b : a;
1199 uint32_t h = (uint32_t(lo) * 0x9e3779b1u ^ uint32_t(hi) * 0x85ebca6bu) & hMask;
1200 while (hStamp[h] == gen) {
1201 const int32_t s = hTable[h];
1204 const N sa = t[s], sb = t[nextHE(s)];
1205 if ((sa == lo && sb == hi) || (sa == hi && sb == lo)) {
1214 h = (h + 1) & hMask;
1216 if (hStamp[h] != gen) {
1223 const int a = edgeStack[--i];
1225 const int b = he[a];
1226 if (b == -1)
continue;
1228 const int a0 = a - a % 3;
1229 const int b0 = b - b % 3;
1230 const int ar = a0 + (a + 2) % 3;
1231 const int al = a0 + (a + 1) % 3;
1232 const int bl = b0 + (b + 2) % 3;
1233 const int br = b0 + (b + 1) % 3;
1234 const N p0 = t[ar], pr = t[a], pl = t[al], p1 = t[bl];
1236 const double x0 = X(p0), y0 = Y(p0);
1237 const double xr = X(pr), yr = Y(pr);
1238 const double xl = X(pl), yl = Y(pl);
1239 const double x1 = X(p1), y1 = Y(p1);
1246 if (!inCircle(x0, y0, xr, yr, xl, yl, x1, y1) && orient(x0, y0, xr, yr, x1, y1) > 0 &&
1247 orient(x0, y0, x1, y1, xl, yl) > 0) {
1250 const int32_t hbl = he[bl], har = he[ar];
1252 if (hbl != -1) he[hbl] = a;
1254 if (har != -1) he[har] = b;
1260 if (hbl != -1 && edgeStamp[a] == 0) {
1264 if (har != -1 && edgeStamp[b] == 0) {
1268 if (he[al] != -1 && edgeStamp[al] == 0) {
1270 edgeStack[i++] = al;
1272 if (he[br] != -1 && edgeStamp[br] == 0) {
1274 edgeStack[i++] = br;
1285 std::vector<int32_t> heVec, edgeStackVec, hTableVec;
1286 std::vector<uint32_t> hStampVec;
1287 std::vector<uint8_t> edgeStampVec;
1288 uint32_t hMask = 0, gen = 0;
1290 static int nextHE(
int e) {
return e - e % 3 + (e + 1) % 3; }
1292 static double orient(
double ax,
double ay,
double bx,
double by,
double cx,
double cy)
1294 return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
1302 inCircle(
double ax,
double ay,
double bx,
double by,
double cx,
double cy,
double px,
double py)
1304 const double dx = ax - px, dy = ay - py, ex = bx - px, ey = by - py, fx = cx - px,
1306 const double ap = dx * dx + dy * dy, bp = ex * ex + ey * ey, cp = fx * fx + fy * fy;
1312 const double s = ap + bp + cp;
1313 return dx * (ey * cp - bp * fy) - dy * (ex * cp - bp * fx) + ap * (ex * fy - ey * fx) <=
1317 void ensureScratch(std::size_t n)
1320 if (edgeStackVec.size() < n) edgeStackVec.resize(n);
1321 if (heVec.size() < n) heVec.resize(n);
1322 if (edgeStampVec.size() < n) edgeStampVec.resize(n, 0);
1323 std::size_t size = 1;
1324 while (size < n * 4) size <<= 1;
1325 if (hTableVec.size() < size) {
1326 hTableVec.resize(size);
1327 hStampVec.resize(size, 0);
1329 hMask = uint32_t(size) - 1;
1338template <
typename N,
typename Coords>
1339void refine(std::vector<N>& triangles,
const Coords& coords)
1342 refiner(triangles, coords);
Main namespace for Lagrange.
Definition project.cpp:27