Lagrange
earcut.h
1// Source: https://github.com/mapbox/earcut.hpp
2// SPDX-License-Identifier: ISC
3//
4// This file has been modified by Adobe.
5//
6// All modifications are Copyright 2022 Adobe.
7#pragma once
8
9#include <algorithm>
10#include <cassert>
11#include <cmath>
12#include <cstddef>
13#include <limits>
14#include <memory>
15#include <utility>
16#include <vector>
17
18namespace lagrange {
19namespace mapbox {
20
21namespace util {
22
23template <std::size_t I, typename T>
24struct nth
25{
26 inline static typename std::tuple_element<I, T>::type get(const T& t)
27 {
28 return std::get<I>(t);
29 };
30};
31
32} // namespace util
33
34namespace detail {
35
36template <typename N = uint32_t>
37class Earcut
38{
39public:
40 std::vector<N> indices;
41 std::size_t vertices = 0;
42
43 template <typename Polygon>
44 void operator()(const Polygon& points);
45
46private:
47 struct Node
48 {
49 Node(N index, double x_, double y_)
50 : i(index)
51 , x(x_)
52 , y(y_)
53 {}
54 Node(const Node&) = delete;
55 Node& operator=(const Node&) = delete;
56 Node(Node&&) = delete;
57 Node& operator=(Node&&) = delete;
58
59 const N i;
60 const double x;
61 const double y;
62
63 // previous and next vertice nodes in a polygon ring
64 Node* prev = nullptr;
65 Node* next = nullptr;
66
67 // z-order curve value
68 int32_t z = 0;
69
70 // previous and next nodes in z-order
71 Node* prevZ = nullptr;
72 Node* nextZ = nullptr;
73
74 // indicates whether this is a steiner point
75 bool steiner = false;
76 };
77
78 template <typename Ring>
79 Node* linkedList(const Ring& points, const bool clockwise);
80 Node* filterPoints(Node* start, Node* end = nullptr);
81 void earcutLinked(Node* ear, int pass = 0);
82 bool isEar(Node* ear);
83 bool isEarHashed(Node* ear);
84 Node* cureLocalIntersections(Node* start);
85 void splitEarcut(Node* start);
86 template <typename Polygon>
87 Node* eliminateHoles(const Polygon& points, Node* outerNode);
88 Node* eliminateHole(Node* hole, Node* outerNode);
89 Node* findHoleBridge(Node* hole, Node* outerNode);
90 bool sectorContainsSector(const Node* m, const Node* p);
91 void indexCurve(Node* start);
92 Node* sortLinked(Node* list);
93 int32_t zOrder(const double x_, const double y_);
94 Node* getLeftmost(Node* start);
95 bool pointInTriangle(
96 double ax,
97 double ay,
98 double bx,
99 double by,
100 double cx,
101 double cy,
102 double px,
103 double py) const;
104 bool isValidDiagonal(Node* a, Node* b);
105 double area(const Node* p, const Node* q, const Node* r) const;
106 bool equals(const Node* p1, const Node* p2);
107 bool intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2);
108 bool onSegment(const Node* p, const Node* q, const Node* r);
109 int sign(double val);
110 bool intersectsPolygon(const Node* a, const Node* b);
111 bool locallyInside(const Node* a, const Node* b);
112 bool middleInside(const Node* a, const Node* b);
113 Node* splitPolygon(Node* a, Node* b);
114 template <typename Point>
115 Node* insertNode(std::size_t i, const Point& p, Node* last);
116 void removeNode(Node* p);
117
118 bool hashing;
119 double minX, maxX;
120 double minY, maxY;
121 double inv_size = 0;
122
123 template <typename T, typename Alloc = std::allocator<T>>
124 class ObjectPool
125 {
126 public:
127 ObjectPool() {}
128 ObjectPool(std::size_t blockSize_) { reset(blockSize_); }
129 ~ObjectPool() { clear(); }
130 template <typename... Args>
131 T* construct(Args&&... args)
132 {
133 if (currentIndex >= blockSize) {
134 currentBlock = alloc_traits::allocate(alloc, blockSize);
135 allocations.emplace_back(currentBlock);
136 currentIndex = 0;
137 }
138 T* object = &currentBlock[currentIndex++];
139 alloc_traits::construct(alloc, object, std::forward<Args>(args)...);
140 return object;
141 }
142 void reset(std::size_t newBlockSize)
143 {
144 for (auto allocation : allocations) {
145 alloc_traits::deallocate(alloc, allocation, blockSize);
146 }
147 allocations.clear();
148 blockSize = std::max<std::size_t>(1, newBlockSize);
149 currentBlock = nullptr;
150 currentIndex = blockSize;
151 }
152 void clear() { reset(blockSize); }
153
154 private:
155 T* currentBlock = nullptr;
156 std::size_t currentIndex = 1;
157 std::size_t blockSize = 1;
158 std::vector<T*> allocations;
159 Alloc alloc;
160 typedef typename std::allocator_traits<Alloc> alloc_traits;
161 };
162 ObjectPool<Node> nodes;
163};
164
165template <typename N>
166template <typename Polygon>
167void Earcut<N>::operator()(const Polygon& points)
168{
169 // reset
170 indices.clear();
171 vertices = 0;
172
173 if (points.empty()) return;
174
175 double x;
176 double y;
177 int threshold = 80;
178 std::size_t len = 0;
179
180 for (size_t i = 0; threshold >= 0 && i < points.size(); i++) {
181 threshold -= static_cast<int>(points[i].size());
182 len += points[i].size();
183 }
184
185 // estimate size of nodes and indices
186 nodes.reset(len * 3 / 2);
187 indices.reserve(len + points[0].size());
188
189 Node* outerNode = linkedList(points[0], true);
190 if (!outerNode || outerNode->prev == outerNode->next) return;
191
192 if (points.size() > 1) outerNode = eliminateHoles(points, outerNode);
193
194 // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
195 hashing = threshold < 0;
196 if (hashing) {
197 Node* p = outerNode->next;
198 minX = maxX = outerNode->x;
199 minY = maxY = outerNode->y;
200 do {
201 x = p->x;
202 y = p->y;
203 minX = std::min<double>(minX, x);
204 minY = std::min<double>(minY, y);
205 maxX = std::max<double>(maxX, x);
206 maxY = std::max<double>(maxY, y);
207 p = p->next;
208 } while (p != outerNode);
209
210 // minX, minY and size are later used to transform coords into integers for z-order
211 // calculation
212 inv_size = std::max<double>(maxX - minX, maxY - minY);
213 inv_size = inv_size != .0 ? (1. / inv_size) : .0;
214 }
215
216 earcutLinked(outerNode);
217
218 nodes.clear();
219}
220
221// create a circular doubly linked list from polygon points in the specified winding order
222template <typename N>
223template <typename Ring>
224typename Earcut<N>::Node* Earcut<N>::linkedList(const Ring& points, const bool clockwise)
225{
226 using Point = typename Ring::value_type;
227 double sum = 0;
228 const std::size_t len = points.size();
229 std::size_t i, j;
230 Node* last = nullptr;
231
232 // calculate original winding order of a polygon ring
233 for (i = 0, j = len > 0 ? len - 1 : 0; i < len; j = i++) {
234 const auto& p1 = points[i];
235 const auto& p2 = points[j];
236 const double p20 = util::nth<0, Point>::get(p2);
237 const double p10 = util::nth<0, Point>::get(p1);
238 const double p11 = util::nth<1, Point>::get(p1);
239 const double p21 = util::nth<1, Point>::get(p2);
240 sum += (p20 - p10) * (p11 + p21);
241 }
242
243 // link points into circular doubly-linked list in the specified winding order
244 if (clockwise == (sum > 0)) {
245 for (i = 0; i < len; i++) last = insertNode(vertices + i, points[i], last);
246 } else {
247 for (i = len; i-- > 0;) last = insertNode(vertices + i, points[i], last);
248 }
249
250 if (last && equals(last, last->next)) {
251 removeNode(last);
252 last = last->next;
253 }
254
255 vertices += len;
256
257 return last;
258}
259
260// eliminate colinear or duplicate points
261template <typename N>
262typename Earcut<N>::Node* Earcut<N>::filterPoints(Node* start, Node* end)
263{
264 if (!end) end = start;
265
266 Node* p = start;
267 bool again;
268 do {
269 again = false;
270
271 if (!p->steiner && (equals(p, p->next) || area(p->prev, p, p->next) == 0)) {
272 removeNode(p);
273 p = end = p->prev;
274
275 if (p == p->next) break;
276 again = true;
277
278 } else {
279 p = p->next;
280 }
281 } while (again || p != end);
282
283 return end;
284}
285
286// main ear slicing loop which triangulates a polygon (given as a linked list)
287template <typename N>
288void Earcut<N>::earcutLinked(Node* ear, int pass)
289{
290 if (!ear) return;
291
292 // interlink polygon nodes in z-order
293 if (!pass && hashing) indexCurve(ear);
294
295 Node* stop = ear;
296 Node* prev;
297 Node* next;
298
299
300 // iterate through ears, slicing them one by one
301 while (ear->prev != ear->next) {
302 prev = ear->prev;
303 next = ear->next;
304
305 if (hashing ? isEarHashed(ear) : isEar(ear)) {
306 // cut off the triangle
307 indices.emplace_back(prev->i);
308 indices.emplace_back(ear->i);
309 indices.emplace_back(next->i);
310
311 removeNode(ear);
312
313 // skipping the next vertice leads to less sliver triangles
314 ear = next->next;
315 stop = next->next;
316
317 continue;
318 }
319
320 ear = next;
321
322 // if we looped through the whole remaining polygon and can't find any more ears
323 if (ear == stop) {
324 // try filtering points and slicing again
325 if (!pass) earcutLinked(filterPoints(ear), 1);
326
327 // if this didn't work, try curing all small self-intersections locally
328 else if (pass == 1) {
329 ear = cureLocalIntersections(filterPoints(ear));
330 earcutLinked(ear, 2);
331
332 // as a last resort, try splitting the remaining polygon into two
333 } else if (pass == 2)
334 splitEarcut(ear);
335
336 break;
337 }
338 }
339}
340
341// check whether a polygon node forms a valid ear with adjacent nodes
342template <typename N>
343bool Earcut<N>::isEar(Node* ear)
344{
345 const Node* a = ear->prev;
346 const Node* b = ear;
347 const Node* c = ear->next;
348
349 if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
350
351 // now make sure we don't have other points inside the potential ear
352 Node* p = ear->next->next;
353
354 while (p != ear->prev) {
355 if (pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) &&
356 area(p->prev, p, p->next) >= 0)
357 return false;
358 p = p->next;
359 }
360
361 return true;
362}
363
364template <typename N>
365bool Earcut<N>::isEarHashed(Node* ear)
366{
367 const Node* a = ear->prev;
368 const Node* b = ear;
369 const Node* c = ear->next;
370
371 if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
372
373 // triangle bbox; min & max are calculated like this for speed
374 const double minTX = std::min<double>(a->x, std::min<double>(b->x, c->x));
375 const double minTY = std::min<double>(a->y, std::min<double>(b->y, c->y));
376 const double maxTX = std::max<double>(a->x, std::max<double>(b->x, c->x));
377 const double maxTY = std::max<double>(a->y, std::max<double>(b->y, c->y));
378
379 // z-order range for the current triangle bbox;
380 const int32_t minZ = zOrder(minTX, minTY);
381 const int32_t maxZ = zOrder(maxTX, maxTY);
382
383 // first look for points inside the triangle in increasing z-order
384 Node* p = ear->nextZ;
385
386 while (p && p->z <= maxZ) {
387 if (p != ear->prev && p != ear->next &&
388 pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) &&
389 area(p->prev, p, p->next) >= 0)
390 return false;
391 p = p->nextZ;
392 }
393
394 // then look for points in decreasing z-order
395 p = ear->prevZ;
396
397 while (p && p->z >= minZ) {
398 if (p != ear->prev && p != ear->next &&
399 pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) &&
400 area(p->prev, p, p->next) >= 0)
401 return false;
402 p = p->prevZ;
403 }
404
405 return true;
406}
407
408// go through all polygon nodes and cure small local self-intersections
409template <typename N>
410typename Earcut<N>::Node* Earcut<N>::cureLocalIntersections(Node* start)
411{
412 Node* p = start;
413 do {
414 Node* a = p->prev;
415 Node* b = p->next->next;
416
417 // a self-intersection where edge (v[i-1],v[i]) intersects (v[i+1],v[i+2])
418 if (!equals(a, b) && intersects(a, p, p->next, b) && locallyInside(a, b) &&
419 locallyInside(b, a)) {
420 indices.emplace_back(a->i);
421 indices.emplace_back(p->i);
422 indices.emplace_back(b->i);
423
424 // remove two nodes involved
425 removeNode(p);
426 removeNode(p->next);
427
428 p = start = b;
429 }
430 p = p->next;
431 } while (p != start);
432
433 return filterPoints(p);
434}
435
436// try splitting polygon into two and triangulate them independently
437template <typename N>
438void Earcut<N>::splitEarcut(Node* start)
439{
440 // look for a valid diagonal that divides the polygon into two
441 Node* a = start;
442 do {
443 Node* b = a->next->next;
444 while (b != a->prev) {
445 if (a->i != b->i && isValidDiagonal(a, b)) {
446 // split the polygon in two by the diagonal
447 Node* c = splitPolygon(a, b);
448
449 // filter colinear points around the cuts
450 a = filterPoints(a, a->next);
451 c = filterPoints(c, c->next);
452
453 // run earcut on each half
454 earcutLinked(a);
455 earcutLinked(c);
456 return;
457 }
458 b = b->next;
459 }
460 a = a->next;
461 } while (a != start);
462}
463
464// link every hole into the outer loop, producing a single-ring polygon without holes
465template <typename N>
466template <typename Polygon>
467typename Earcut<N>::Node* Earcut<N>::eliminateHoles(const Polygon& points, Node* outerNode)
468{
469 const size_t len = points.size();
470
471 std::vector<Node*> queue;
472 for (size_t i = 1; i < len; i++) {
473 Node* list = linkedList(points[i], false);
474 if (list) {
475 if (list == list->next) list->steiner = true;
476 queue.push_back(getLeftmost(list));
477 }
478 }
479 std::sort(queue.begin(), queue.end(), [](const Node* a, const Node* b) { return a->x < b->x; });
480
481 // process holes from left to right
482 for (size_t i = 0; i < queue.size(); i++) {
483 outerNode = eliminateHole(queue[i], outerNode);
484 outerNode = filterPoints(outerNode, outerNode->next);
485 }
486
487 return outerNode;
488}
489
490// find a bridge between vertices that connects hole with an outer ring and and link it
491template <typename N>
492typename Earcut<N>::Node* Earcut<N>::eliminateHole(Node* hole, Node* outerNode)
493{
494 Node* bridge = findHoleBridge(hole, outerNode);
495 if (!bridge) {
496 return outerNode;
497 }
498
499 Node* bridgeReverse = splitPolygon(bridge, hole);
500
501 // filter collinear points around the cuts
502 Node* filteredBridge = filterPoints(bridge, bridge->next);
503 filterPoints(bridgeReverse, bridgeReverse->next);
504
505 // Check if input node was removed by the filtering
506 return outerNode == bridge ? filteredBridge : outerNode;
507}
508
509// David Eberly's algorithm for finding a bridge between hole and outer polygon
510template <typename N>
511typename Earcut<N>::Node* Earcut<N>::findHoleBridge(Node* hole, Node* outerNode)
512{
513 Node* p = outerNode;
514 double hx = hole->x;
515 double hy = hole->y;
516 double qx = -std::numeric_limits<double>::infinity();
517 Node* m = nullptr;
518
519 // find a segment intersected by a ray from the hole's leftmost Vertex to the left;
520 // segment's endpoint with lesser x will be potential connection Vertex
521 do {
522 if (hy <= p->y && hy >= p->next->y && p->next->y != p->y) {
523 double x = p->x + (hy - p->y) * (p->next->x - p->x) / (p->next->y - p->y);
524 if (x <= hx && x > qx) {
525 qx = x;
526 if (x == hx) {
527 if (hy == p->y) return p;
528 if (hy == p->next->y) return p->next;
529 }
530 m = p->x < p->next->x ? p : p->next;
531 }
532 }
533 p = p->next;
534 } while (p != outerNode);
535
536 if (!m) return 0;
537
538 if (hx == qx) return m; // hole touches outer segment; pick leftmost endpoint
539
540 // look for points inside the triangle of hole Vertex, segment intersection and endpoint;
541 // if there are no points found, we have a valid connection;
542 // otherwise choose the Vertex of the minimum angle with the ray as connection Vertex
543
544 const Node* stop = m;
545 double tanMin = std::numeric_limits<double>::infinity();
546 double tanCur = 0;
547
548 p = m;
549 double mx = m->x;
550 double my = m->y;
551
552 do {
553 if (hx >= p->x && p->x >= mx && hx != p->x &&
554 pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p->x, p->y)) {
555 tanCur = std::abs(hy - p->y) / (hx - p->x); // tangential
556
557 if (locallyInside(p, hole) &&
558 (tanCur < tanMin ||
559 (tanCur == tanMin && (p->x > m->x || sectorContainsSector(m, p))))) {
560 m = p;
561 tanMin = tanCur;
562 }
563 }
564
565 p = p->next;
566 } while (p != stop);
567
568 return m;
569}
570
571// whether sector in vertex m contains sector in vertex p in the same coordinates
572template <typename N>
573bool Earcut<N>::sectorContainsSector(const Node* m, const Node* p)
574{
575 return area(m->prev, m, p->prev) < 0 && area(p->next, m, m->next) < 0;
576}
577
578// interlink polygon nodes in z-order
579template <typename N>
580void Earcut<N>::indexCurve(Node* start)
581{
582 assert(start);
583 Node* p = start;
584
585 do {
586 p->z = p->z ? p->z : zOrder(p->x, p->y);
587 p->prevZ = p->prev;
588 p->nextZ = p->next;
589 p = p->next;
590 } while (p != start);
591
592 p->prevZ->nextZ = nullptr;
593 p->prevZ = nullptr;
594
595 sortLinked(p);
596}
597
598// Simon Tatham's linked list merge sort algorithm
599// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
600template <typename N>
601typename Earcut<N>::Node* Earcut<N>::sortLinked(Node* list)
602{
603 assert(list);
604 Node* p;
605 Node* q;
606 Node* e;
607 Node* tail;
608 int i, numMerges, pSize, qSize;
609 int inSize = 1;
610
611 for (;;) {
612 p = list;
613 list = nullptr;
614 tail = nullptr;
615 numMerges = 0;
616
617 while (p) {
618 numMerges++;
619 q = p;
620 pSize = 0;
621 for (i = 0; i < inSize; i++) {
622 pSize++;
623 q = q->nextZ;
624 if (!q) break;
625 }
626
627 qSize = inSize;
628
629 while (pSize > 0 || (qSize > 0 && q)) {
630 if (pSize == 0) {
631 e = q;
632 q = q->nextZ;
633 qSize--;
634 } else if (qSize == 0 || !q) {
635 e = p;
636 p = p->nextZ;
637 pSize--;
638 } else if (p->z <= q->z) {
639 e = p;
640 p = p->nextZ;
641 pSize--;
642 } else {
643 e = q;
644 q = q->nextZ;
645 qSize--;
646 }
647
648 if (tail)
649 tail->nextZ = e;
650 else
651 list = e;
652
653 e->prevZ = tail;
654 tail = e;
655 }
656
657 p = q;
658 }
659
660 tail->nextZ = nullptr;
661
662 if (numMerges <= 1) return list;
663
664 inSize *= 2;
665 }
666}
667
668// z-order of a Vertex given coords and size of the data bounding box
669template <typename N>
670int32_t Earcut<N>::zOrder(const double x_, const double y_)
671{
672 // coords are transformed into non-negative 15-bit integer range
673 int32_t x = static_cast<int32_t>(32767.0 * (x_ - minX) * inv_size);
674 int32_t y = static_cast<int32_t>(32767.0 * (y_ - minY) * inv_size);
675
676 x = (x | (x << 8)) & 0x00FF00FF;
677 x = (x | (x << 4)) & 0x0F0F0F0F;
678 x = (x | (x << 2)) & 0x33333333;
679 x = (x | (x << 1)) & 0x55555555;
680
681 y = (y | (y << 8)) & 0x00FF00FF;
682 y = (y | (y << 4)) & 0x0F0F0F0F;
683 y = (y | (y << 2)) & 0x33333333;
684 y = (y | (y << 1)) & 0x55555555;
685
686 return x | (y << 1);
687}
688
689// find the leftmost node of a polygon ring
690template <typename N>
691typename Earcut<N>::Node* Earcut<N>::getLeftmost(Node* start)
692{
693 Node* p = start;
694 Node* leftmost = start;
695 do {
696 if (p->x < leftmost->x || (p->x == leftmost->x && p->y < leftmost->y)) leftmost = p;
697 p = p->next;
698 } while (p != start);
699
700 return leftmost;
701}
702
703// check if a point lies within a convex triangle
704template <typename N>
705bool Earcut<N>::pointInTriangle(
706 double ax,
707 double ay,
708 double bx,
709 double by,
710 double cx,
711 double cy,
712 double px,
713 double py) const
714{
715 return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
716 (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
717 (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
718}
719
720// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
721template <typename N>
722bool Earcut<N>::isValidDiagonal(Node* a, Node* b)
723{
724 return a->next->i != b->i && a->prev->i != b->i &&
725 !intersectsPolygon(a, b) && // dones't intersect other edges
726 ((locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
727 (area(a->prev, a, b->prev) != 0.0 ||
728 area(a, b->prev, b) != 0.0)) || // does not create opposite-facing sectors
729 (equals(a, b) && area(a->prev, a, a->next) > 0 &&
730 area(b->prev, b, b->next) > 0)); // special zero-length case
731}
732
733// signed area of a triangle
734template <typename N>
735double Earcut<N>::area(const Node* p, const Node* q, const Node* r) const
736{
737 return (q->y - p->y) * (r->x - q->x) - (q->x - p->x) * (r->y - q->y);
738}
739
740// check if two points are equal
741template <typename N>
742bool Earcut<N>::equals(const Node* p1, const Node* p2)
743{
744 return p1->x == p2->x && p1->y == p2->y;
745}
746
747// check if two segments intersect
748template <typename N>
749bool Earcut<N>::intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2)
750{
751 int o1 = sign(area(p1, q1, p2));
752 int o2 = sign(area(p1, q1, q2));
753 int o3 = sign(area(p2, q2, p1));
754 int o4 = sign(area(p2, q2, q1));
755
756 if (o1 != o2 && o3 != o4) return true; // general case
757
758 if (o1 == 0 && onSegment(p1, p2, q1))
759 return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
760 if (o2 == 0 && onSegment(p1, q2, q1))
761 return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
762 if (o3 == 0 && onSegment(p2, p1, q2))
763 return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
764 if (o4 == 0 && onSegment(p2, q1, q2))
765 return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
766
767 return false;
768}
769
770// for collinear points p, q, r, check if point q lies on segment pr
771template <typename N>
772bool Earcut<N>::onSegment(const Node* p, const Node* q, const Node* r)
773{
774 return q->x <= std::max<double>(p->x, r->x) && q->x >= std::min<double>(p->x, r->x) &&
775 q->y <= std::max<double>(p->y, r->y) && q->y >= std::min<double>(p->y, r->y);
776}
777
778template <typename N>
779int Earcut<N>::sign(double val)
780{
781 return (0.0 < val) - (val < 0.0);
782}
783
784// check if a polygon diagonal intersects any polygon segments
785template <typename N>
786bool Earcut<N>::intersectsPolygon(const Node* a, const Node* b)
787{
788 const Node* p = a;
789 do {
790 if (p->i != a->i && p->next->i != a->i && p->i != b->i && p->next->i != b->i &&
791 intersects(p, p->next, a, b))
792 return true;
793 p = p->next;
794 } while (p != a);
795
796 return false;
797}
798
799// check if a polygon diagonal is locally inside the polygon
800template <typename N>
801bool Earcut<N>::locallyInside(const Node* a, const Node* b)
802{
803 return area(a->prev, a, a->next) < 0 ? area(a, b, a->next) >= 0 && area(a, a->prev, b) >= 0
804 : area(a, b, a->prev) < 0 || area(a, a->next, b) < 0;
805}
806
807// check if the middle Vertex of a polygon diagonal is inside the polygon
808template <typename N>
809bool Earcut<N>::middleInside(const Node* a, const Node* b)
810{
811 const Node* p = a;
812 bool inside = false;
813 double px = (a->x + b->x) / 2;
814 double py = (a->y + b->y) / 2;
815 do {
816 if (((p->y > py) != (p->next->y > py)) && p->next->y != p->y &&
817 (px < (p->next->x - p->x) * (py - p->y) / (p->next->y - p->y) + p->x))
818 inside = !inside;
819 p = p->next;
820 } while (p != a);
821
822 return inside;
823}
824
825// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits
826// polygon into two; if one belongs to the outer ring and another to a hole, it merges it into a
827// single ring
828template <typename N>
829typename Earcut<N>::Node* Earcut<N>::splitPolygon(Node* a, Node* b)
830{
831 Node* a2 = nodes.construct(a->i, a->x, a->y);
832 Node* b2 = nodes.construct(b->i, b->x, b->y);
833 Node* an = a->next;
834 Node* bp = b->prev;
835
836 a->next = b;
837 b->prev = a;
838
839 a2->next = an;
840 an->prev = a2;
841
842 b2->next = a2;
843 a2->prev = b2;
844
845 bp->next = b2;
846 b2->prev = bp;
847
848 return b2;
849}
850
851// create a node and util::optionally link it with previous one (in a circular doubly linked list)
852template <typename N>
853template <typename Point>
854typename Earcut<N>::Node* Earcut<N>::insertNode(std::size_t i, const Point& pt, Node* last)
855{
856 Node* p = nodes.construct(
857 static_cast<N>(i),
858 util::nth<0, Point>::get(pt),
859 util::nth<1, Point>::get(pt));
860
861 if (!last) {
862 p->prev = p;
863 p->next = p;
864
865 } else {
866 assert(last);
867 p->next = last->next;
868 p->prev = last;
869 last->next->prev = p;
870 last->next = p;
871 }
872 return p;
873}
874
875template <typename N>
876void Earcut<N>::removeNode(Node* p)
877{
878 p->next->prev = p->prev;
879 p->prev->next = p->next;
880
881 if (p->prevZ) p->prevZ->nextZ = p->nextZ;
882 if (p->nextZ) p->nextZ->prevZ = p->prevZ;
883}
884} // namespace detail
885
886template <typename N = uint32_t, typename Polygon>
887std::vector<N> earcut(const Polygon& poly)
888{
889 mapbox::detail::Earcut<N> earcut;
890 earcut(poly);
891 return std::move(earcut.indices);
892}
893
894} // namespace mapbox
895} // namespace lagrange
Definition: earcut.h:38
Main namespace for Lagrange.
Definition: AABBIGL.h:30
int sign(T val)
Get the sign of the value Returns either -1, 0, or 1.
Definition: utils.h:43
Definition: project.cpp:27
Definition: earcut.h:25