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