@carbonenginejs/core-math 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,766 @@
1
+ export function triangulate(data, holeIndices, dim)
2
+ {
3
+ dim = dim || 2;
4
+
5
+ let hasHoles = holeIndices && holeIndices.length,
6
+ outerLen = hasHoles ? holeIndices[0] * dim : data.length,
7
+ outerNode = linkedList(data, 0, outerLen, dim, true),
8
+ triangles = [];
9
+
10
+ if (!outerNode || outerNode.next === outerNode.prev) return triangles;
11
+
12
+ let minX, minY, maxX, maxY, x, y, invSize;
13
+
14
+ if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
15
+
16
+ // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
17
+ if (data.length > 80 * dim)
18
+ {
19
+ minX = maxX = data[0];
20
+ minY = maxY = data[1];
21
+
22
+ for (let i = dim; i < outerLen; i += dim)
23
+ {
24
+ x = data[i];
25
+ y = data[i + 1];
26
+ if (x < minX) minX = x;
27
+ if (y < minY) minY = y;
28
+ if (x > maxX) maxX = x;
29
+ if (y > maxY) maxY = y;
30
+ }
31
+
32
+ // minX, minY and invSize are later used to transform coords into integers for z-order calculation
33
+ invSize = Math.max(maxX - minX, maxY - minY);
34
+ invSize = invSize !== 0 ? 32767 / invSize : 0;
35
+ }
36
+
37
+ earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
38
+
39
+ return triangles;
40
+ }
41
+
42
+ // create a circular doubly linked list from polygon points in the specified winding order
43
+ function linkedList(data, start, end, dim, clockwise)
44
+ {
45
+ let i, last;
46
+
47
+ if (clockwise === (signedArea(data, start, end, dim) > 0))
48
+ {
49
+ for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
50
+ }
51
+ else
52
+ {
53
+ for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
54
+ }
55
+
56
+ if (last && equals(last, last.next))
57
+ {
58
+ removeNode(last);
59
+ last = last.next;
60
+ }
61
+
62
+ return last;
63
+ }
64
+
65
+ // eliminate colinear or duplicate points
66
+ function filterPoints(start, end)
67
+ {
68
+ if (!start) return start;
69
+ if (!end) end = start;
70
+
71
+ let p = start,
72
+ again;
73
+ do
74
+ {
75
+ again = false;
76
+
77
+ if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0))
78
+ {
79
+ removeNode(p);
80
+ p = end = p.prev;
81
+ if (p === p.next) break;
82
+ again = true;
83
+
84
+ }
85
+ else
86
+ {
87
+ p = p.next;
88
+ }
89
+ } while (again || p !== end);
90
+
91
+ return end;
92
+ }
93
+
94
+ // main ear slicing loop which triangulates a polygon (given as a linked list)
95
+ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass)
96
+ {
97
+ if (!ear) return;
98
+
99
+ // interlink polygon nodes in z-order
100
+ if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
101
+
102
+ let stop = ear,
103
+ prev, next;
104
+
105
+ // iterate through ears, slicing them one by one
106
+ while (ear.prev !== ear.next)
107
+ {
108
+ prev = ear.prev;
109
+ next = ear.next;
110
+
111
+ if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear))
112
+ {
113
+ // cut off the triangle
114
+ triangles.push(prev.i / dim | 0);
115
+ triangles.push(ear.i / dim | 0);
116
+ triangles.push(next.i / dim | 0);
117
+
118
+ removeNode(ear);
119
+
120
+ // skipping the next vertex leads to less sliver triangles
121
+ ear = next.next;
122
+ stop = next.next;
123
+
124
+ continue;
125
+ }
126
+
127
+ ear = next;
128
+
129
+ // if we looped through the whole remaining polygon and can't find any more ears
130
+ if (ear === stop)
131
+ {
132
+ // try filtering points and slicing again
133
+ if (!pass)
134
+ {
135
+ earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
136
+
137
+ // if this didn't work, try curing all small self-intersections locally
138
+ }
139
+ else if (pass === 1)
140
+ {
141
+ ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
142
+ earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
143
+
144
+ // as a last resort, try splitting the remaining polygon into two
145
+ }
146
+ else if (pass === 2)
147
+ {
148
+ splitEarcut(ear, triangles, dim, minX, minY, invSize);
149
+ }
150
+
151
+ break;
152
+ }
153
+ }
154
+ }
155
+
156
+ // check whether a polygon node forms a valid ear with adjacent nodes
157
+ function isEar(ear)
158
+ {
159
+ let a = ear.prev,
160
+ b = ear,
161
+ c = ear.next;
162
+
163
+ if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
164
+
165
+ // now make sure we don't have other points inside the potential ear
166
+ let ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
167
+
168
+ // triangle bbox; min & max are calculated like this for speed
169
+ let x0 = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx),
170
+ y0 = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy),
171
+ x1 = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx),
172
+ y1 = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
173
+
174
+ let p = c.next;
175
+ while (p !== a)
176
+ {
177
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
178
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
179
+ area(p.prev, p, p.next) >= 0) return false;
180
+ p = p.next;
181
+ }
182
+
183
+ return true;
184
+ }
185
+
186
+ function isEarHashed(ear, minX, minY, invSize)
187
+ {
188
+ let a = ear.prev,
189
+ b = ear,
190
+ c = ear.next;
191
+
192
+ if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
193
+
194
+ let ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
195
+
196
+ // triangle bbox; min & max are calculated like this for speed
197
+ let x0 = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx),
198
+ y0 = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy),
199
+ x1 = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx),
200
+ y1 = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
201
+
202
+ // z-order range for the current triangle bbox;
203
+ let minZ = zOrder(x0, y0, minX, minY, invSize),
204
+ maxZ = zOrder(x1, y1, minX, minY, invSize);
205
+
206
+ let p = ear.prevZ,
207
+ n = ear.nextZ;
208
+
209
+ // look for points inside the triangle in both directions
210
+ while (p && p.z >= minZ && n && n.z <= maxZ)
211
+ {
212
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
213
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
214
+ p = p.prevZ;
215
+
216
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
217
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
218
+ n = n.nextZ;
219
+ }
220
+
221
+ // look for remaining points in decreasing z-order
222
+ while (p && p.z >= minZ)
223
+ {
224
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
225
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
226
+ p = p.prevZ;
227
+ }
228
+
229
+ // look for remaining points in increasing z-order
230
+ while (n && n.z <= maxZ)
231
+ {
232
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
233
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
234
+ n = n.nextZ;
235
+ }
236
+
237
+ return true;
238
+ }
239
+
240
+ // go through all polygon nodes and cure small local self-intersections
241
+ function cureLocalIntersections(start, triangles, dim)
242
+ {
243
+ let p = start;
244
+ do
245
+ {
246
+ let a = p.prev,
247
+ b = p.next.next;
248
+
249
+ if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a))
250
+ {
251
+
252
+ triangles.push(a.i / dim | 0);
253
+ triangles.push(p.i / dim | 0);
254
+ triangles.push(b.i / dim | 0);
255
+
256
+ // remove two nodes involved
257
+ removeNode(p);
258
+ removeNode(p.next);
259
+
260
+ p = start = b;
261
+ }
262
+ p = p.next;
263
+ } while (p !== start);
264
+
265
+ return filterPoints(p);
266
+ }
267
+
268
+ // try splitting polygon into two and triangulate them independently
269
+ function splitEarcut(start, triangles, dim, minX, minY, invSize)
270
+ {
271
+ // look for a valid diagonal that divides the polygon into two
272
+ let a = start;
273
+ do
274
+ {
275
+ let b = a.next.next;
276
+ while (b !== a.prev)
277
+ {
278
+ if (a.i !== b.i && isValidDiagonal(a, b))
279
+ {
280
+ // split the polygon in two by the diagonal
281
+ let c = splitPolygon(a, b);
282
+
283
+ // filter colinear points around the cuts
284
+ a = filterPoints(a, a.next);
285
+ c = filterPoints(c, c.next);
286
+
287
+ // run earcut on each half
288
+ earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
289
+ earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
290
+ return;
291
+ }
292
+ b = b.next;
293
+ }
294
+ a = a.next;
295
+ } while (a !== start);
296
+ }
297
+
298
+ // link every hole into the outer loop, producing a single-ring polygon without holes
299
+ function eliminateHoles(data, holeIndices, outerNode, dim)
300
+ {
301
+ let queue = [],
302
+ i, len, start, end, list;
303
+
304
+ for (i = 0, len = holeIndices.length; i < len; i++)
305
+ {
306
+ start = holeIndices[i] * dim;
307
+ end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
308
+ list = linkedList(data, start, end, dim, false);
309
+ if (list === list.next) list.steiner = true;
310
+ queue.push(getLeftmost(list));
311
+ }
312
+
313
+ queue.sort(compareX);
314
+
315
+ // process holes from left to right
316
+ for (i = 0; i < queue.length; i++)
317
+ {
318
+ outerNode = eliminateHole(queue[i], outerNode);
319
+ }
320
+
321
+ return outerNode;
322
+ }
323
+
324
+ function compareX(a, b)
325
+ {
326
+ return a.x - b.x;
327
+ }
328
+
329
+ // find a bridge between vertices that connects hole with an outer ring and and link it
330
+ function eliminateHole(hole, outerNode)
331
+ {
332
+ let bridge = findHoleBridge(hole, outerNode);
333
+ if (!bridge)
334
+ {
335
+ return outerNode;
336
+ }
337
+
338
+ let bridgeReverse = splitPolygon(bridge, hole);
339
+
340
+ // filter collinear points around the cuts
341
+ filterPoints(bridgeReverse, bridgeReverse.next);
342
+ return filterPoints(bridge, bridge.next);
343
+ }
344
+
345
+ // David Eberly's algorithm for finding a bridge between hole and outer polygon
346
+ function findHoleBridge(hole, outerNode)
347
+ {
348
+ let p = outerNode,
349
+ hx = hole.x,
350
+ hy = hole.y,
351
+ qx = -Infinity,
352
+ m;
353
+
354
+ // find a segment intersected by a ray from the hole's leftmost point to the left;
355
+ // segment's endpoint with lesser x will be potential connection point
356
+ do
357
+ {
358
+ if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y)
359
+ {
360
+ let x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
361
+ if (x <= hx && x > qx)
362
+ {
363
+ qx = x;
364
+ m = p.x < p.next.x ? p : p.next;
365
+ if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
366
+ }
367
+ }
368
+ p = p.next;
369
+ } while (p !== outerNode);
370
+
371
+ if (!m) return null;
372
+
373
+ // look for points inside the triangle of hole point, segment intersection and endpoint;
374
+ // if there are no points found, we have a valid connection;
375
+ // otherwise choose the point of the minimum angle with the ray as connection point
376
+
377
+ let stop = m,
378
+ mx = m.x,
379
+ my = m.y,
380
+ tanMin = Infinity,
381
+ tan;
382
+
383
+ p = m;
384
+
385
+ do
386
+ {
387
+ if (hx >= p.x && p.x >= mx && hx !== p.x &&
388
+ pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y))
389
+ {
390
+
391
+ tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
392
+
393
+ if (locallyInside(p, hole) &&
394
+ (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p))))))
395
+ {
396
+ m = p;
397
+ tanMin = tan;
398
+ }
399
+ }
400
+
401
+ p = p.next;
402
+ } while (p !== stop);
403
+
404
+ return m;
405
+ }
406
+
407
+ // whether sector in vertex m contains sector in vertex p in the same coordinates
408
+ function sectorContainsSector(m, p)
409
+ {
410
+ return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
411
+ }
412
+
413
+ // interlink polygon nodes in z-order
414
+ function indexCurve(start, minX, minY, invSize)
415
+ {
416
+ let p = start;
417
+ do
418
+ {
419
+ if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
420
+ p.prevZ = p.prev;
421
+ p.nextZ = p.next;
422
+ p = p.next;
423
+ } while (p !== start);
424
+
425
+ p.prevZ.nextZ = null;
426
+ p.prevZ = null;
427
+
428
+ sortLinked(p);
429
+ }
430
+
431
+ // Simon Tatham's linked list merge sort algorithm
432
+ // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
433
+ function sortLinked(list)
434
+ {
435
+ let i, p, q, e, tail, numMerges, pSize, qSize,
436
+ inSize = 1;
437
+
438
+ do
439
+ {
440
+ p = list;
441
+ list = null;
442
+ tail = null;
443
+ numMerges = 0;
444
+
445
+ while (p)
446
+ {
447
+ numMerges++;
448
+ q = p;
449
+ pSize = 0;
450
+ for (i = 0; i < inSize; i++)
451
+ {
452
+ pSize++;
453
+ q = q.nextZ;
454
+ if (!q) break;
455
+ }
456
+ qSize = inSize;
457
+
458
+ while (pSize > 0 || (qSize > 0 && q))
459
+ {
460
+
461
+ if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z))
462
+ {
463
+ e = p;
464
+ p = p.nextZ;
465
+ pSize--;
466
+ }
467
+ else
468
+ {
469
+ e = q;
470
+ q = q.nextZ;
471
+ qSize--;
472
+ }
473
+
474
+ if (tail) tail.nextZ = e;
475
+ else list = e;
476
+
477
+ e.prevZ = tail;
478
+ tail = e;
479
+ }
480
+
481
+ p = q;
482
+ }
483
+
484
+ tail.nextZ = null;
485
+ inSize *= 2;
486
+
487
+ } while (numMerges > 1);
488
+
489
+ return list;
490
+ }
491
+
492
+ // z-order of a point given coords and inverse of the longer side of data bbox
493
+ function zOrder(x, y, minX, minY, invSize)
494
+ {
495
+ // coords are transformed into non-negative 15-bit integer range
496
+ x = (x - minX) * invSize | 0;
497
+ y = (y - minY) * invSize | 0;
498
+
499
+ x = (x | (x << 8)) & 0x00FF00FF;
500
+ x = (x | (x << 4)) & 0x0F0F0F0F;
501
+ x = (x | (x << 2)) & 0x33333333;
502
+ x = (x | (x << 1)) & 0x55555555;
503
+
504
+ y = (y | (y << 8)) & 0x00FF00FF;
505
+ y = (y | (y << 4)) & 0x0F0F0F0F;
506
+ y = (y | (y << 2)) & 0x33333333;
507
+ y = (y | (y << 1)) & 0x55555555;
508
+
509
+ return x | (y << 1);
510
+ }
511
+
512
+ // find the leftmost node of a polygon ring
513
+ function getLeftmost(start)
514
+ {
515
+ let p = start,
516
+ leftmost = start;
517
+ do
518
+ {
519
+ if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
520
+ p = p.next;
521
+ } while (p !== start);
522
+
523
+ return leftmost;
524
+ }
525
+
526
+ // check if a point lies within a convex triangle
527
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py)
528
+ {
529
+ return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
530
+ (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
531
+ (bx - px) * (cy - py) >= (cx - px) * (by - py);
532
+ }
533
+
534
+ // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
535
+ function isValidDiagonal(a, b)
536
+ {
537
+ return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
538
+ (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
539
+ (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
540
+ equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
541
+ }
542
+
543
+ // signed area of a triangle
544
+ function area(p, q, r)
545
+ {
546
+ return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
547
+ }
548
+
549
+ // check if two points are equal
550
+ function equals(p1, p2)
551
+ {
552
+ return p1.x === p2.x && p1.y === p2.y;
553
+ }
554
+
555
+ // check if two segments intersect
556
+ function intersects(p1, q1, p2, q2)
557
+ {
558
+ let o1 = sign(area(p1, q1, p2));
559
+ let o2 = sign(area(p1, q1, q2));
560
+ let o3 = sign(area(p2, q2, p1));
561
+ let o4 = sign(area(p2, q2, q1));
562
+
563
+ if (o1 !== o2 && o3 !== o4) return true; // general case
564
+
565
+ if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
566
+ if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
567
+ if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
568
+ if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
569
+
570
+ return false;
571
+ }
572
+
573
+ // for collinear points p, q, r, check if point q lies on segment pr
574
+ function onSegment(p, q, r)
575
+ {
576
+ return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
577
+ }
578
+
579
+ function sign(num)
580
+ {
581
+ return num > 0 ? 1 : num < 0 ? -1 : 0;
582
+ }
583
+
584
+ // check if a polygon diagonal intersects any polygon segments
585
+ function intersectsPolygon(a, b)
586
+ {
587
+ let p = a;
588
+ do
589
+ {
590
+ if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
591
+ intersects(p, p.next, a, b)) return true;
592
+ p = p.next;
593
+ } while (p !== a);
594
+
595
+ return false;
596
+ }
597
+
598
+ // check if a polygon diagonal is locally inside the polygon
599
+ function locallyInside(a, b)
600
+ {
601
+ return area(a.prev, a, a.next) < 0 ?
602
+ area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
603
+ area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
604
+ }
605
+
606
+ // check if the middle point of a polygon diagonal is inside the polygon
607
+ function middleInside(a, b)
608
+ {
609
+ let p = a,
610
+ inside = false,
611
+ px = (a.x + b.x) / 2,
612
+ py = (a.y + b.y) / 2;
613
+ do
614
+ {
615
+ if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
616
+ (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
617
+ inside = !inside;
618
+ p = p.next;
619
+ } while (p !== a);
620
+
621
+ return inside;
622
+ }
623
+
624
+ // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
625
+ // if one belongs to the outer ring and another to a hole, it merges it into a single ring
626
+ function splitPolygon(a, b)
627
+ {
628
+ let a2 = new Node(a.i, a.x, a.y),
629
+ b2 = new Node(b.i, b.x, b.y),
630
+ an = a.next,
631
+ bp = b.prev;
632
+
633
+ a.next = b;
634
+ b.prev = a;
635
+
636
+ a2.next = an;
637
+ an.prev = a2;
638
+
639
+ b2.next = a2;
640
+ a2.prev = b2;
641
+
642
+ bp.next = b2;
643
+ b2.prev = bp;
644
+
645
+ return b2;
646
+ }
647
+
648
+ // create a node and optionally link it with previous one (in a circular doubly linked list)
649
+ function insertNode(i, x, y, last)
650
+ {
651
+ let p = new Node(i, x, y);
652
+
653
+ if (!last)
654
+ {
655
+ p.prev = p;
656
+ p.next = p;
657
+
658
+ }
659
+ else
660
+ {
661
+ p.next = last.next;
662
+ p.prev = last;
663
+ last.next.prev = p;
664
+ last.next = p;
665
+ }
666
+ return p;
667
+ }
668
+
669
+ function removeNode(p)
670
+ {
671
+ p.next.prev = p.prev;
672
+ p.prev.next = p.next;
673
+
674
+ if (p.prevZ) p.prevZ.nextZ = p.nextZ;
675
+ if (p.nextZ) p.nextZ.prevZ = p.prevZ;
676
+ }
677
+
678
+ function Node(i, x, y)
679
+ {
680
+ // vertex index in coordinates array
681
+ this.i = i;
682
+
683
+ // vertex coordinates
684
+ this.x = x;
685
+ this.y = y;
686
+
687
+ // previous and next vertex nodes in a polygon ring
688
+ this.prev = null;
689
+ this.next = null;
690
+
691
+ // z-order curve value
692
+ this.z = 0;
693
+
694
+ // previous and next nodes in z-order
695
+ this.prevZ = null;
696
+ this.nextZ = null;
697
+
698
+ // indicates whether this is a steiner point
699
+ this.steiner = false;
700
+ }
701
+
702
+ // return a percentage difference between the polygon area and its triangulation area;
703
+ // used to verify correctness of triangulation
704
+ export function deviation(data, holeIndices, dim, triangles)
705
+ {
706
+ let hasHoles = holeIndices && holeIndices.length;
707
+ let outerLen = hasHoles ? holeIndices[0] * dim : data.length;
708
+
709
+ let polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
710
+ if (hasHoles)
711
+ {
712
+ for (let i = 0, len = holeIndices.length; i < len; i++)
713
+ {
714
+ let start = holeIndices[i] * dim;
715
+ let end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
716
+ polygonArea -= Math.abs(signedArea(data, start, end, dim));
717
+ }
718
+ }
719
+
720
+ let trianglesArea = 0;
721
+ for (let i = 0; i < triangles.length; i += 3)
722
+ {
723
+ let a = triangles[i] * dim;
724
+ let b = triangles[i + 1] * dim;
725
+ let c = triangles[i + 2] * dim;
726
+ trianglesArea += Math.abs(
727
+ (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
728
+ (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
729
+ }
730
+
731
+ return polygonArea === 0 && trianglesArea === 0 ? 0 :
732
+ Math.abs((trianglesArea - polygonArea) / polygonArea);
733
+ }
734
+
735
+ function signedArea(data, start, end, dim)
736
+ {
737
+ let sum = 0;
738
+ for (let i = start, j = end - dim; i < end; i += dim)
739
+ {
740
+ sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
741
+ j = i;
742
+ }
743
+ return sum;
744
+ }
745
+
746
+ // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
747
+ export function flatten(data)
748
+ {
749
+ let dim = data[0][0].length,
750
+ result = { vertices: [], holes: [], dimensions: dim },
751
+ holeIndex = 0;
752
+
753
+ for (let i = 0; i < data.length; i++)
754
+ {
755
+ for (let j = 0; j < data[i].length; j++)
756
+ {
757
+ for (let d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
758
+ }
759
+ if (i > 0)
760
+ {
761
+ holeIndex += data[i - 1].length;
762
+ result.holes.push(holeIndex);
763
+ }
764
+ }
765
+ return result;
766
+ }