@math.gl/polygon 3.5.3 → 3.6.0-alpha.1

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