Coverage Report

Created: 2026-05-23 06:04

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