xl-public-utils 1.0.11 → 1.0.12

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