@math.gl/geometry 5.0.0-beta.11 → 5.0.0-beta.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.
- package/dist/arrow/index.cjs +926 -0
- package/dist/arrow/index.cjs.map +6 -0
- package/dist/arrow/index.d.ts +3 -0
- package/dist/arrow/index.d.ts.map +1 -0
- package/dist/arrow/index.js +6 -0
- package/dist/arrow/index.js.map +1 -0
- package/dist/arrow/stream-tessellate.d.ts +34 -0
- package/dist/arrow/stream-tessellate.d.ts.map +1 -0
- package/dist/arrow/stream-tessellate.js +171 -0
- package/dist/arrow/stream-tessellate.js.map +1 -0
- package/dist/arrow/tessellate.d.ts +31 -0
- package/dist/arrow/tessellate.d.ts.map +1 -0
- package/dist/arrow/tessellate.js +102 -0
- package/dist/arrow/tessellate.js.map +1 -0
- package/dist/color/index.cjs +3 -108
- package/dist/color/index.cjs.map +3 -3
- package/dist/color/index.d.ts +2 -3
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +1 -1
- package/dist/color/index.js.map +1 -1
- package/dist/culling/index.cjs +3 -1
- package/dist/culling/index.cjs.map +1 -1
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts +4 -1
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts.map +1 -1
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js +9 -2
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js.map +1 -1
- package/dist/index.cjs +625 -523
- package/dist/index.cjs.map +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/polygon/earcut.d.ts +12 -1
- package/dist/polygon/earcut.d.ts.map +1 -1
- package/dist/polygon/earcut.js +855 -630
- package/dist/polygon/earcut.js.map +1 -1
- package/dist/polygon/index.d.ts +1 -0
- package/dist/polygon/index.d.ts.map +1 -1
- package/dist/polygon/index.js.map +1 -1
- package/package.json +16 -3
- package/{dist/color/color-lut-types.js → src/arrow/index.ts} +3 -2
- package/src/arrow/stream-tessellate.ts +209 -0
- package/src/arrow/tessellate.ts +133 -0
- package/src/color/index.ts +3 -3
- package/src/culling/lib/algorithms/oriented-bounding-box-quaternion.ts +8 -2
- package/src/index.ts +14 -13
- package/src/polygon/earcut.ts +946 -668
- package/src/polygon/index.ts +1 -0
- package/dist/color/color-lut-types.d.ts +0 -16
- package/dist/color/color-lut-types.d.ts.map +0 -1
- package/dist/color/color-lut-types.js.map +0 -1
- package/dist/color/sample-lut.d.ts +0 -13
- package/dist/color/sample-lut.d.ts.map +0 -1
- package/dist/color/sample-lut.js +0 -132
- package/dist/color/sample-lut.js.map +0 -1
- package/src/color/color-lut-types.ts +0 -19
- package/src/color/sample-lut.ts +0 -175
package/src/polygon/earcut.ts
CHANGED
|
@@ -35,12 +35,24 @@
|
|
|
35
35
|
import type {NumericArray} from '@math.gl/core';
|
|
36
36
|
import {DimIndex, Plane2D} from './polygon-utils';
|
|
37
37
|
|
|
38
|
+
/** Controls the tradeoff between clipping speed and triangle layout. */
|
|
39
|
+
export type EarcutOptions = {
|
|
40
|
+
/**
|
|
41
|
+
* `fast` follows upstream's next-ear order. `balanced` skips a vertex after each
|
|
42
|
+
* cut to reduce long triangle fans, which can help subsequent mesh refinement.
|
|
43
|
+
* This is a heuristic, not a minimum-angle or Delaunay guarantee. Default `fast`.
|
|
44
|
+
*/
|
|
45
|
+
triangulation?: 'fast' | 'balanced';
|
|
46
|
+
};
|
|
47
|
+
|
|
38
48
|
/**
|
|
39
49
|
* Computes a triangulation of a polygon
|
|
40
50
|
* @param positions a flat array of the vertex positions that define the polygon.
|
|
41
51
|
* @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).
|
|
42
52
|
* @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`.
|
|
43
53
|
* @param areas areas of outer polygon and holes as computed by `getPolygonSignedArea()`. Can be optionally supplied to speed up triangulation
|
|
54
|
+
* @param plane the coordinate plane to triangulate (`xy`, `xz`, or `yz`).
|
|
55
|
+
* @param options clipping strategy; `balanced` trades some speed for fewer long fans.
|
|
44
56
|
* @returns array of indices into the `positions` array that describes the triangulation of the polygon
|
|
45
57
|
* Adapted from https://github.com/mapbox/earcut
|
|
46
58
|
*/
|
|
@@ -49,808 +61,1074 @@ export function earcut(
|
|
|
49
61
|
holeIndices?: NumericArray,
|
|
50
62
|
dim: number = 2,
|
|
51
63
|
areas?: NumericArray,
|
|
52
|
-
plane: Plane2D = 'xy'
|
|
64
|
+
plane: Plane2D = 'xy',
|
|
65
|
+
options: EarcutOptions = {}
|
|
53
66
|
): number[] {
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const savedQueue = activeHoleQueue;
|
|
57
|
-
if (!holeQueues[callDepth]) holeQueues[callDepth] = [];
|
|
58
|
-
activeHoleQueue = holeQueues[callDepth];
|
|
67
|
+
if (!triangulators[callDepth]) triangulators[callDepth] = createTriangulator();
|
|
68
|
+
const triangulate = triangulators[callDepth];
|
|
59
69
|
callDepth++;
|
|
60
70
|
try {
|
|
61
|
-
return triangulate(positions, holeIndices, dim, areas, plane);
|
|
71
|
+
return triangulate(positions, holeIndices, dim, areas, plane, options);
|
|
62
72
|
} finally {
|
|
63
|
-
activeHoleQueue.length = 0;
|
|
64
|
-
activeHoleQueue = savedQueue;
|
|
65
|
-
vertexPoolIndex = savedIndex;
|
|
66
73
|
callDepth--;
|
|
67
|
-
// Do not retain arbitrarily large object graphs after a one-off polygon.
|
|
68
|
-
if (callDepth === 0 && vertexPool.length > 65536) vertexPool.length = 0;
|
|
69
74
|
}
|
|
70
75
|
}
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const triangles = [];
|
|
77
|
+
/** Reusable, isolated triangulators for calls nested through input getters. */
|
|
78
|
+
const triangulators: ReturnType<typeof createTriangulator>[] = [];
|
|
79
|
+
/** Number of active calls; each depth owns independent scratch storage. */
|
|
80
|
+
let callDepth = 0;
|
|
77
81
|
|
|
78
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Creates an isolated triangulation core with reusable scratch storage.
|
|
84
|
+
* Adapted from mapbox/earcut at 934e54d8bed85387330b7acaa5d2f5ba30a460a3.
|
|
85
|
+
* Keeps upstream's cut order (does not include PR #222).
|
|
86
|
+
*/
|
|
87
|
+
function createTriangulator() {
|
|
88
|
+
/** Whether to skip the next candidate after clipping an ear in this call. */
|
|
89
|
+
let balanceTriangles = false;
|
|
90
|
+
/**
|
|
91
|
+
* A vertex in a circular doubly linked list representing a polygon ring.
|
|
92
|
+
* `prev`/`next` are always linked (set immediately after {@link createNode}), so they're typed
|
|
93
|
+
* non-null; `prevZ`/`nextZ` are the z-order list links and are null at the ends.
|
|
94
|
+
*
|
|
95
|
+
* @typedef {object} Node
|
|
96
|
+
* @property {number} i vertex index in the coordinates array
|
|
97
|
+
* @property {number} x vertex x coordinate
|
|
98
|
+
* @property {number} y vertex y coordinate
|
|
99
|
+
* @property {Node} prev previous vertex node in the polygon ring
|
|
100
|
+
* @property {Node} next next vertex node in the polygon ring
|
|
101
|
+
* @property {number} z z-order curve value; doubles as the owning block index during eliminateHoles
|
|
102
|
+
* @property {Node | null} prevZ previous node in z-order
|
|
103
|
+
* @property {Node | null} nextZ next node in z-order
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
// single-vertex holes to preserve through filterPoints (steiner points); kept off the Node
|
|
107
|
+
// shape since they're rare — the empty-set fast path means non-steiner inputs pay nothing
|
|
108
|
+
/** @type {Set<Node>} */
|
|
109
|
+
const steiners = new Set();
|
|
110
|
+
|
|
111
|
+
// halves of split polygons waiting to be triangulated, drained by earcut in a loop so that the
|
|
112
|
+
// call stack doesn't grow with the nesting depth of splits
|
|
113
|
+
/** @type {Node[]} */
|
|
114
|
+
const splitStack = [];
|
|
115
|
+
|
|
116
|
+
// set by filterPoints whenever it removes at least one node; read by earcutLinked's stall
|
|
117
|
+
// handler to decide whether another clip pass is worth attempting before the costlier stages
|
|
118
|
+
let filteredOut = false;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Triangulate a polygon given as a flat array of vertex coordinates.
|
|
122
|
+
*
|
|
123
|
+
* @param {ArrayLike<number>} data flat array of vertex coordinates
|
|
124
|
+
* @param {ArrayLike<number> | null} [holeIndices] indices (in vertices, not coordinates) where each hole ring starts
|
|
125
|
+
* @param {number} [dim=2] number of coordinates per vertex in `data`
|
|
126
|
+
* @returns {number[]} triangles as triplets of vertex indices into `data`
|
|
127
|
+
* @param areas optional precomputed signed half-areas in the selected plane
|
|
128
|
+
* @param plane coordinate plane used for both ring winding and spatial indexes
|
|
129
|
+
* @example earcut([10,0, 0,50, 60,60, 70,10]); // [1,0,3, 1,3,2]
|
|
130
|
+
*/
|
|
131
|
+
function triangulate(data, holeIndices, dim, areas, plane) {
|
|
132
|
+
const hasHoles = holeIndices && holeIndices.length;
|
|
133
|
+
const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
|
|
134
|
+
if (steiners.size) steiners.clear();
|
|
135
|
+
|
|
136
|
+
let outerNode = linkedList(data, 0, outerLen, dim, true, areas && areas[0], plane);
|
|
137
|
+
/** @type {number[]} */
|
|
138
|
+
const triangles = [];
|
|
139
|
+
|
|
140
|
+
if (!outerNode || outerNode.next === outerNode.prev) return triangles;
|
|
141
|
+
|
|
142
|
+
let minX = 0,
|
|
143
|
+
minY = 0,
|
|
144
|
+
invSize = 0;
|
|
145
|
+
|
|
146
|
+
if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim, areas, plane);
|
|
147
|
+
|
|
148
|
+
// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
|
|
149
|
+
if (data.length > 80 * dim) {
|
|
150
|
+
const i0 = DimIndex[plane[0]],
|
|
151
|
+
i1 = DimIndex[plane[1]];
|
|
152
|
+
minX = data[i0];
|
|
153
|
+
minY = data[i1];
|
|
154
|
+
let maxX = minX;
|
|
155
|
+
let maxY = minY;
|
|
156
|
+
|
|
157
|
+
for (let i = dim; i < outerLen; i += dim) {
|
|
158
|
+
const x = data[i + i0];
|
|
159
|
+
const y = data[i + i1];
|
|
160
|
+
if (x < minX) minX = x;
|
|
161
|
+
if (y < minY) minY = y;
|
|
162
|
+
if (x > maxX) maxX = x;
|
|
163
|
+
if (y > maxY) maxY = y;
|
|
164
|
+
}
|
|
79
165
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
let minY;
|
|
85
|
-
let x;
|
|
86
|
-
let y;
|
|
166
|
+
// minX, minY and invSize are later used to transform coords into integers for z-order calculation
|
|
167
|
+
invSize = Math.max(maxX - minX, maxY - minY);
|
|
168
|
+
invSize = invSize !== 0 ? 32767 / invSize : 0;
|
|
169
|
+
}
|
|
87
170
|
|
|
88
|
-
|
|
171
|
+
earcutLinked(outerNode, triangles, minX, minY, invSize);
|
|
172
|
+
while (splitStack.length)
|
|
173
|
+
earcutLinked(/** @type {Node} */ (splitStack.pop()), triangles, minX, minY, invSize);
|
|
89
174
|
|
|
90
|
-
|
|
91
|
-
|
|
175
|
+
return triangles;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// create a circular doubly linked list from polygon points in the specified winding order
|
|
179
|
+
/** @param {ArrayLike<number>} data @param {number} start @param {number} end @param {number} dim @param {boolean} clockwise @returns {Node | null} */
|
|
180
|
+
function linkedList(
|
|
181
|
+
data: NumericArray,
|
|
182
|
+
start: number,
|
|
183
|
+
end: number,
|
|
184
|
+
dim: number,
|
|
185
|
+
clockwise: boolean,
|
|
186
|
+
area: number | undefined,
|
|
187
|
+
plane: Plane2D
|
|
188
|
+
): Vertex {
|
|
189
|
+
if (start >= end) return null;
|
|
190
|
+
let i;
|
|
191
|
+
let last;
|
|
92
192
|
const i0 = DimIndex[plane[0]];
|
|
93
193
|
const i1 = DimIndex[plane[1]];
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
194
|
+
if (area === undefined) {
|
|
195
|
+
// Combine coordinate reads and area accumulation, as in loaders.gl.
|
|
196
|
+
let sum = 0;
|
|
197
|
+
let previousX = data[end - dim + i0];
|
|
198
|
+
let previousY = data[end - dim + i1];
|
|
199
|
+
for (i = start; i < end; i += dim) {
|
|
200
|
+
const x = data[i + i0];
|
|
201
|
+
const y = data[i + i1];
|
|
202
|
+
sum += (x - previousX) * (y + previousY);
|
|
203
|
+
last = insertNode(i / dim, x, y, last);
|
|
204
|
+
previousX = x;
|
|
205
|
+
previousY = y;
|
|
206
|
+
}
|
|
207
|
+
if (last && clockwise !== sum / 2 < 0) {
|
|
208
|
+
let node = last;
|
|
209
|
+
do {
|
|
210
|
+
const next = node.next;
|
|
211
|
+
node.next = node.prev;
|
|
212
|
+
node.prev = next;
|
|
213
|
+
node = next;
|
|
214
|
+
} while (node !== last);
|
|
215
|
+
// Preserve the starting vertex and triangle order of reverse insertion.
|
|
216
|
+
last = last.prev;
|
|
217
|
+
}
|
|
218
|
+
} else if (clockwise === area < 0) {
|
|
219
|
+
for (i = start; i < end; i += dim)
|
|
220
|
+
last = insertNode(i / dim, data[i + i0], data[i + i1], last);
|
|
221
|
+
} else {
|
|
222
|
+
for (i = end - dim; i >= start; i -= dim)
|
|
223
|
+
last = insertNode(i / dim, data[i + i0], data[i + i1], last);
|
|
104
224
|
}
|
|
105
225
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
earcutLinked(outerNode, triangles, minX, minY, invSize, 0);
|
|
112
|
-
|
|
113
|
-
return triangles;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// create a circular doubly linked list from polygon points in the specified winding order
|
|
117
|
-
function linkedList(
|
|
118
|
-
data: NumericArray,
|
|
119
|
-
start: number,
|
|
120
|
-
end: number,
|
|
121
|
-
dim: number,
|
|
122
|
-
clockwise: boolean,
|
|
123
|
-
area: number | undefined,
|
|
124
|
-
plane: Plane2D
|
|
125
|
-
): Vertex {
|
|
126
|
-
if (start >= end) return null;
|
|
127
|
-
let i;
|
|
128
|
-
let last;
|
|
129
|
-
const i0 = DimIndex[plane[0]];
|
|
130
|
-
const i1 = DimIndex[plane[1]];
|
|
131
|
-
if (area === undefined) {
|
|
132
|
-
// Combine coordinate reads and area accumulation, as in loaders.gl.
|
|
133
|
-
let sum = 0;
|
|
134
|
-
let previousX = data[end - dim + i0];
|
|
135
|
-
let previousY = data[end - dim + i1];
|
|
136
|
-
for (i = start; i < end; i += dim) {
|
|
137
|
-
const x = data[i + i0];
|
|
138
|
-
const y = data[i + i1];
|
|
139
|
-
sum += (x - previousX) * (y + previousY);
|
|
140
|
-
last = insertNode(i / dim, x, y, last);
|
|
141
|
-
previousX = x;
|
|
142
|
-
previousY = y;
|
|
226
|
+
if (last && equals(last, last.next)) {
|
|
227
|
+
removeNode(last);
|
|
228
|
+
last = last.next;
|
|
143
229
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
do {
|
|
147
|
-
const next = node.next;
|
|
148
|
-
node.next = node.prev;
|
|
149
|
-
node.prev = next;
|
|
150
|
-
node = next;
|
|
151
|
-
} while (node !== last);
|
|
152
|
-
// Preserve the starting vertex and triangle order of reverse insertion.
|
|
153
|
-
last = last.prev;
|
|
154
|
-
}
|
|
155
|
-
} else if (clockwise === area < 0) {
|
|
156
|
-
for (i = start; i < end; i += dim) last = insertNode(i / dim, data[i + i0], data[i + i1], last);
|
|
157
|
-
} else {
|
|
158
|
-
for (i = end - dim; i >= start; i -= dim)
|
|
159
|
-
last = insertNode(i / dim, data[i + i0], data[i + i1], last);
|
|
230
|
+
|
|
231
|
+
return last;
|
|
160
232
|
}
|
|
161
233
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
234
|
+
// Remove collinear or coincident points; removability depends only on a node's immediate
|
|
235
|
+
// neighbors, so we sweep forward and re-check the predecessor after each removal. With no `end`
|
|
236
|
+
// we sweep the whole ring, lapping until nothing is removable (the fixpoint the clipper needs).
|
|
237
|
+
// With an explicit `end` we heal only the dirty window around a bridge/diagonal cut, stopping at
|
|
238
|
+
// `end` rather than lapping — O(window) instead of O(ring).
|
|
239
|
+
/** @param {Node} start @param {Node} [end] @returns {Node} */
|
|
240
|
+
function filterPoints(start, end = start) {
|
|
241
|
+
const full = end === start;
|
|
242
|
+
|
|
243
|
+
let p = start,
|
|
244
|
+
again;
|
|
245
|
+
do {
|
|
246
|
+
again = false;
|
|
247
|
+
if (
|
|
248
|
+
p !== p.next &&
|
|
249
|
+
(steiners.size === 0 || !steiners.has(p)) &&
|
|
250
|
+
(equals(p, p.next) || area(p.prev, p, p.next) === 0)
|
|
251
|
+
) {
|
|
252
|
+
if (full || p === end) end = p.prev; // pull the stop bound back past the removal
|
|
253
|
+
filteredOut = true;
|
|
254
|
+
removeNode(p);
|
|
255
|
+
p = p.prev; // re-check the predecessor
|
|
256
|
+
again = true;
|
|
257
|
+
} else if (full || p !== end) {
|
|
258
|
+
p = p.next;
|
|
259
|
+
again = !full; // local heal: keep looping until the sweep reaches end
|
|
260
|
+
}
|
|
261
|
+
} while (again || p !== end);
|
|
262
|
+
|
|
263
|
+
return end;
|
|
165
264
|
}
|
|
166
265
|
|
|
167
|
-
|
|
168
|
-
}
|
|
266
|
+
// main ear slicing loop which triangulates a polygon (given as a linked list)
|
|
267
|
+
/** @param {Node} ear @param {number[]} triangles @param {number} minX @param {number} minY @param {number} invSize */
|
|
268
|
+
function earcutLinked(ear, triangles, minX, minY, invSize) {
|
|
269
|
+
// interlink polygon nodes in z-order
|
|
270
|
+
if (invSize) indexCurve(ear, minX, minY, invSize);
|
|
169
271
|
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
let p = start;
|
|
176
|
-
let again;
|
|
177
|
-
do {
|
|
178
|
-
again = false;
|
|
179
|
-
|
|
180
|
-
if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
|
|
181
|
-
removeNode(p);
|
|
182
|
-
p = end = p.prev;
|
|
183
|
-
if (p === p.next) break;
|
|
184
|
-
again = true;
|
|
185
|
-
} else {
|
|
186
|
-
p = p.next;
|
|
187
|
-
}
|
|
188
|
-
} while (again || p !== end);
|
|
272
|
+
// a strictly convex ring has no vertex that could block an ear, so skip the point checks until a candidate fails;
|
|
273
|
+
// without this, a big convex ring is cut as one fan of long triangles and each hashed check scans O(n) points
|
|
274
|
+
let stop = ear,
|
|
275
|
+
cured = false,
|
|
276
|
+
convex = invSize ? isConvex(ear) : false;
|
|
189
277
|
|
|
190
|
-
|
|
191
|
-
|
|
278
|
+
// iterate through ears, slicing them one by one
|
|
279
|
+
while (ear.prev !== ear.next) {
|
|
280
|
+
const prev = ear.prev;
|
|
281
|
+
/** @type {Node} */
|
|
282
|
+
const next = ear.next;
|
|
192
283
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
284
|
+
if (
|
|
285
|
+
area(prev, ear, next) < 0 &&
|
|
286
|
+
(convex || (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)))
|
|
287
|
+
) {
|
|
288
|
+
triangles.push(prev.i, ear.i, next.i); // cut off the triangle
|
|
196
289
|
|
|
197
|
-
|
|
198
|
-
|
|
290
|
+
removeNode(ear);
|
|
291
|
+
ear = balanceTriangles ? next.next : next;
|
|
292
|
+
stop = ear;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
199
295
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
296
|
+
convex = false;
|
|
297
|
+
ear = next;
|
|
298
|
+
|
|
299
|
+
// if we looped through the whole remaining polygon and can't find any more ears
|
|
300
|
+
if (ear === stop) {
|
|
301
|
+
// try filtering collinear/coincident points and slicing again — repeat as long as
|
|
302
|
+
// filtering actually removes nodes, since each removal can expose new ears
|
|
303
|
+
filteredOut = false;
|
|
304
|
+
ear = filterPoints(ear);
|
|
305
|
+
if (filteredOut) {
|
|
306
|
+
stop = ear;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
203
309
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
310
|
+
// filtering is exhausted: cure small local self-intersections once, then retry
|
|
311
|
+
if (!cured) {
|
|
312
|
+
ear = cureLocalIntersections(ear, triangles);
|
|
313
|
+
stop = ear;
|
|
314
|
+
cured = true;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
208
317
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
318
|
+
// as a last resort, try splitting the remaining polygon into two
|
|
319
|
+
splitEarcut(ear);
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
214
324
|
|
|
215
|
-
|
|
325
|
+
// check whether every vertex of a ring is strictly convex
|
|
326
|
+
/** @param {Node} start @returns {boolean} */
|
|
327
|
+
function isConvex(start) {
|
|
328
|
+
let p = start;
|
|
329
|
+
do {
|
|
330
|
+
if (area(p.prev, p, p.next) >= 0) return false;
|
|
331
|
+
p = p.next;
|
|
332
|
+
} while (p !== start);
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
216
335
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
336
|
+
// check whether a polygon node forms a valid ear with adjacent nodes
|
|
337
|
+
/** @param {Node} ear @returns {boolean} */
|
|
338
|
+
function isEar(ear) {
|
|
339
|
+
// reflex check (area(a, b, c) >= 0) is hoisted into the earcutLinked caller to avoid non-inlined call here
|
|
340
|
+
const a = ear.prev,
|
|
341
|
+
b = ear,
|
|
342
|
+
c = ear.next,
|
|
343
|
+
ax = a.x,
|
|
344
|
+
bx = b.x,
|
|
345
|
+
cx = c.x,
|
|
346
|
+
ay = a.y,
|
|
347
|
+
by = b.y,
|
|
348
|
+
cy = c.y,
|
|
349
|
+
x0 = Math.min(ax, bx, cx), // triangle bbox
|
|
350
|
+
y0 = Math.min(ay, by, cy),
|
|
351
|
+
x1 = Math.max(ax, bx, cx),
|
|
352
|
+
y1 = Math.max(ay, by, cy);
|
|
353
|
+
|
|
354
|
+
// make sure we don't have other points inside the potential ear
|
|
355
|
+
let p = c.next;
|
|
356
|
+
while (p !== a) {
|
|
357
|
+
if (
|
|
358
|
+
p.x >= x0 &&
|
|
359
|
+
p.x <= x1 &&
|
|
360
|
+
p.y >= y0 &&
|
|
361
|
+
p.y <= y1 &&
|
|
362
|
+
!(ax === p.x && ay === p.y) &&
|
|
363
|
+
pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
|
|
364
|
+
area(p.prev, p, p.next) >= 0
|
|
365
|
+
)
|
|
366
|
+
return false;
|
|
367
|
+
p = p.next;
|
|
368
|
+
}
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
220
371
|
|
|
221
|
-
|
|
372
|
+
/** @param {Node} ear @param {number} minX @param {number} minY @param {number} invSize @returns {boolean} */
|
|
373
|
+
function isEarHashed(ear, minX, minY, invSize) {
|
|
374
|
+
// reflex check is hoisted into the earcutLinked caller (see isEar)
|
|
375
|
+
const a = ear.prev,
|
|
376
|
+
b = ear,
|
|
377
|
+
c = ear.next,
|
|
378
|
+
ax = a.x,
|
|
379
|
+
bx = b.x,
|
|
380
|
+
cx = c.x,
|
|
381
|
+
ay = a.y,
|
|
382
|
+
by = b.y,
|
|
383
|
+
cy = c.y,
|
|
384
|
+
x0 = Math.min(ax, bx, cx), // triangle bbox
|
|
385
|
+
y0 = Math.min(ay, by, cy),
|
|
386
|
+
x1 = Math.max(ax, bx, cx),
|
|
387
|
+
y1 = Math.max(ay, by, cy),
|
|
388
|
+
minZ = zOrder(x0, y0, minX, minY, invSize), // z-order range for the current triangle bbox;
|
|
389
|
+
maxZ = zOrder(x1, y1, minX, minY, invSize);
|
|
390
|
+
|
|
391
|
+
let p = ear.prevZ;
|
|
392
|
+
while (p && p.z >= minZ) {
|
|
393
|
+
// look for points inside the triangle in decreasing z-order
|
|
394
|
+
if (
|
|
395
|
+
p.x >= x0 &&
|
|
396
|
+
p.x <= x1 &&
|
|
397
|
+
p.y >= y0 &&
|
|
398
|
+
p.y <= y1 &&
|
|
399
|
+
p !== c &&
|
|
400
|
+
!(ax === p.x && ay === p.y) &&
|
|
401
|
+
pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
|
|
402
|
+
area(p.prev, p, p.next) >= 0
|
|
403
|
+
)
|
|
404
|
+
return false;
|
|
405
|
+
p = p.prevZ;
|
|
406
|
+
}
|
|
407
|
+
let n = ear.nextZ;
|
|
408
|
+
while (n && n.z <= maxZ) {
|
|
409
|
+
// look for points in increasing z-order
|
|
410
|
+
if (
|
|
411
|
+
n.x >= x0 &&
|
|
412
|
+
n.x <= x1 &&
|
|
413
|
+
n.y >= y0 &&
|
|
414
|
+
n.y <= y1 &&
|
|
415
|
+
n !== c &&
|
|
416
|
+
!(ax === n.x && ay === n.y) &&
|
|
417
|
+
pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
|
|
418
|
+
area(n.prev, n, n.next) >= 0
|
|
419
|
+
)
|
|
420
|
+
return false;
|
|
421
|
+
n = n.nextZ;
|
|
222
422
|
}
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
223
425
|
|
|
224
|
-
|
|
426
|
+
// go through all polygon nodes and cure small local self-intersections
|
|
427
|
+
/** @param {Node} start @param {number[]} triangles @returns {Node} */
|
|
428
|
+
function cureLocalIntersections(start, triangles) {
|
|
429
|
+
let p = start;
|
|
430
|
+
let cured = false;
|
|
431
|
+
do {
|
|
432
|
+
const a = p.prev,
|
|
433
|
+
b = p.next.next;
|
|
225
434
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// try filtering points and slicing again
|
|
229
|
-
if (!pass) {
|
|
230
|
-
earcutLinked(filterPoints(ear), triangles, minX, minY, invSize, 1);
|
|
435
|
+
if (intersects(a, p, p.next, b, false) && locallyInside(a, b) && locallyInside(b, a)) {
|
|
436
|
+
triangles.push(a.i, p.i, b.i);
|
|
231
437
|
|
|
232
|
-
//
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
earcutLinked(ear, triangles, minX, minY, invSize, 2);
|
|
438
|
+
// remove two nodes involved
|
|
439
|
+
removeNode(p);
|
|
440
|
+
removeNode(p.next);
|
|
236
441
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
splitEarcut(ear, triangles, minX, minY, invSize);
|
|
442
|
+
p = start = b;
|
|
443
|
+
cured = true;
|
|
240
444
|
}
|
|
445
|
+
p = p.next;
|
|
446
|
+
} while (p !== start);
|
|
241
447
|
|
|
242
|
-
|
|
243
|
-
}
|
|
448
|
+
return cured ? filterPoints(p) : p;
|
|
244
449
|
}
|
|
245
|
-
}
|
|
246
450
|
|
|
247
|
-
//
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (
|
|
272
|
-
p.x >= x0 &&
|
|
273
|
-
p.x <= x1 &&
|
|
274
|
-
p.y >= y0 &&
|
|
275
|
-
p.y <= y1 &&
|
|
276
|
-
pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
|
|
277
|
-
area(p.prev, p, p.next) >= 0
|
|
278
|
-
)
|
|
279
|
-
return false;
|
|
280
|
-
p = p.next;
|
|
451
|
+
// try splitting polygon into two and triangulate them independently
|
|
452
|
+
/** @param {Node} start */
|
|
453
|
+
function splitEarcut(start) {
|
|
454
|
+
// look for a valid diagonal that divides the polygon into two
|
|
455
|
+
let a = start;
|
|
456
|
+
do {
|
|
457
|
+
let b = a.next.next;
|
|
458
|
+
while (b !== a.prev) {
|
|
459
|
+
if (a.i !== b.i && isValidDiagonal(a, b)) {
|
|
460
|
+
// split the polygon in two by the diagonal
|
|
461
|
+
let c = splitPolygon(a, b);
|
|
462
|
+
|
|
463
|
+
// filter colinear points around the cuts
|
|
464
|
+
a = filterPoints(a, a.next);
|
|
465
|
+
c = filterPoints(c, c.next);
|
|
466
|
+
|
|
467
|
+
// queue both halves for earcut, a on top so it's triangulated first
|
|
468
|
+
splitStack.push(c, a);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
b = b.next;
|
|
472
|
+
}
|
|
473
|
+
a = a.next;
|
|
474
|
+
} while (a !== start);
|
|
281
475
|
}
|
|
282
476
|
|
|
283
|
-
|
|
284
|
-
|
|
477
|
+
// true only while eliminateHoles merges holes, so removeNode keeps the block index live (growBlock)
|
|
478
|
+
let indexActive = false;
|
|
479
|
+
|
|
480
|
+
// link every hole into the outer loop, producing a single-ring polygon without holes
|
|
481
|
+
/** @param {ArrayLike<number>} data @param {ArrayLike<number>} holeIndices @param {Node} outerNode @param {number} dim @returns {Node} */
|
|
482
|
+
function eliminateHoles(data, holeIndices, outerNode, dim, areas, plane) {
|
|
483
|
+
const queue = holeQueue;
|
|
484
|
+
|
|
485
|
+
for (let i = 0, len = holeIndices.length; i < len; i++) {
|
|
486
|
+
const start = holeIndices[i] * dim;
|
|
487
|
+
const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
|
|
488
|
+
const list = /** @type {Node} */ (
|
|
489
|
+
linkedList(data, start, end, dim, false, areas && areas[i + 1], plane)
|
|
490
|
+
);
|
|
491
|
+
if (!list) continue;
|
|
492
|
+
if (list === list.next) steiners.add(list);
|
|
493
|
+
queue.push(getLeftmost(list));
|
|
494
|
+
}
|
|
285
495
|
|
|
286
|
-
|
|
287
|
-
const a = ear.prev;
|
|
288
|
-
const b = ear;
|
|
289
|
-
const c = ear.next;
|
|
496
|
+
queue.sort(compareXYSlope);
|
|
290
497
|
|
|
291
|
-
|
|
498
|
+
// block-bbox index for findHoleBridge, grown append-only as holes merge (see notes
|
|
499
|
+
// above buildBlockIndex). Seed it with the outer ring, then append each merged hole.
|
|
500
|
+
buildBlockIndex(data.length / dim, holeIndices.length);
|
|
501
|
+
indexSegment(outerNode, outerNode);
|
|
292
502
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
503
|
+
// process holes from left to right; indexActive lets removeNode keep block bboxes live as
|
|
504
|
+
// filterPoints heals edges during merges (see growBlock)
|
|
505
|
+
indexActive = true;
|
|
506
|
+
for (let i = 0; i < queue.length; i++) {
|
|
507
|
+
outerNode = eliminateHole(queue[i], outerNode);
|
|
508
|
+
}
|
|
509
|
+
indexActive = false;
|
|
299
510
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
const x1 = ax > bx ? (ax > cx ? ax : cx) : bx > cx ? bx : cx;
|
|
304
|
-
const y1 = ay > by ? (ay > cy ? ay : cy) : by > cy ? by : cy;
|
|
511
|
+
// collapse collinear/coincident points across the whole merged ring once before clipping
|
|
512
|
+
return filterPoints(outerNode);
|
|
513
|
+
}
|
|
305
514
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
515
|
+
/** @param {Node} a @param {Node} b @returns {number} */
|
|
516
|
+
function compareXYSlope(a, b) {
|
|
517
|
+
// when the left-most point of 2 holes meet at a vertex, sort the holes counterclockwise so that when we find
|
|
518
|
+
// the bridge to the outer shell is always the point that they meet at.
|
|
519
|
+
return (
|
|
520
|
+
a.x - b.x ||
|
|
521
|
+
a.y - b.y ||
|
|
522
|
+
(a.next.y - a.y) / (a.next.x - a.x) - (b.next.y - b.y) / (b.next.x - b.x)
|
|
523
|
+
);
|
|
524
|
+
}
|
|
309
525
|
|
|
310
|
-
|
|
311
|
-
|
|
526
|
+
// find a bridge between vertices that connects hole with an outer ring and link it
|
|
527
|
+
/** @param {Node} hole @param {Node} outerNode @returns {Node} */
|
|
528
|
+
function eliminateHole(hole, outerNode) {
|
|
529
|
+
const bridge = findHoleBridge(hole, outerNode);
|
|
530
|
+
if (!bridge) {
|
|
531
|
+
return outerNode;
|
|
532
|
+
}
|
|
312
533
|
|
|
313
|
-
|
|
314
|
-
while (p && p.z >= minZ && n && n.z <= maxZ) {
|
|
315
|
-
if (
|
|
316
|
-
p.x >= x0 &&
|
|
317
|
-
p.x <= x1 &&
|
|
318
|
-
p.y >= y0 &&
|
|
319
|
-
p.y <= y1 &&
|
|
320
|
-
p !== a &&
|
|
321
|
-
p !== c &&
|
|
322
|
-
pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
|
|
323
|
-
area(p.prev, p, p.next) >= 0
|
|
324
|
-
)
|
|
325
|
-
return false;
|
|
326
|
-
p = p.prevZ;
|
|
534
|
+
const bridgeReverse = splitPolygon(bridge, hole);
|
|
327
535
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
n !== c &&
|
|
335
|
-
pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
|
|
336
|
-
area(n.prev, n, n.next) >= 0
|
|
337
|
-
)
|
|
338
|
-
return false;
|
|
339
|
-
n = n.nextZ;
|
|
340
|
-
}
|
|
536
|
+
// index the merged-in segment before filtering: in ring order the splice runs
|
|
537
|
+
// bridge -> hole -> bridgeReverse -> bridge2 -> (bridge's old next), covering the
|
|
538
|
+
// hole's edges and both new slit edges. filterPoints below only drops collinear /
|
|
539
|
+
// coincident points, so these bboxes stay valid (conservative) supersets.
|
|
540
|
+
const bridge2 = bridgeReverse.next;
|
|
541
|
+
indexSegment(bridge, bridge2.next);
|
|
341
542
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
p.x >= x0 &&
|
|
346
|
-
p.x <= x1 &&
|
|
347
|
-
p.y >= y0 &&
|
|
348
|
-
p.y <= y1 &&
|
|
349
|
-
p !== a &&
|
|
350
|
-
p !== c &&
|
|
351
|
-
pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
|
|
352
|
-
area(p.prev, p, p.next) >= 0
|
|
353
|
-
)
|
|
354
|
-
return false;
|
|
355
|
-
p = p.prevZ;
|
|
356
|
-
}
|
|
543
|
+
// heal collinear/coincident points around the two new slit edges, but leave bridge2 alone: dropping it as collinear
|
|
544
|
+
// would leave bridge lying on the healed edge, where a later hole could bridge into it from outside its sector
|
|
545
|
+
filterPoints(bridgeReverse.prev, bridgeReverse);
|
|
357
546
|
|
|
358
|
-
|
|
359
|
-
|
|
547
|
+
// likewise, if bridge lies strictly between its prev and hole, healing would drop it and leave bridge2 on the healed
|
|
548
|
+
// edge; skip the heal then (a spike at bridge, e.g. a hole touching an outer edge, still needs it)
|
|
549
|
+
const a = bridge.prev;
|
|
360
550
|
if (
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
n.y >= y0 &&
|
|
364
|
-
n.y <= y1 &&
|
|
365
|
-
n !== a &&
|
|
366
|
-
n !== c &&
|
|
367
|
-
pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
|
|
368
|
-
area(n.prev, n, n.next) >= 0
|
|
551
|
+
area(a, bridge, hole) === 0 &&
|
|
552
|
+
(a.x - bridge.x) * (hole.x - bridge.x) + (a.y - bridge.y) * (hole.y - bridge.y) < 0
|
|
369
553
|
)
|
|
370
|
-
return
|
|
371
|
-
n = n.nextZ;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return true;
|
|
375
|
-
}
|
|
554
|
+
return bridge;
|
|
376
555
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
let p = start;
|
|
380
|
-
do {
|
|
381
|
-
const a = p.prev;
|
|
382
|
-
const b = p.next.next;
|
|
383
|
-
|
|
384
|
-
if (
|
|
385
|
-
!equals(a, b) &&
|
|
386
|
-
intersects(a, p, p.next, b) &&
|
|
387
|
-
locallyInside(a, b) &&
|
|
388
|
-
locallyInside(b, a)
|
|
389
|
-
) {
|
|
390
|
-
triangles.push(a.i);
|
|
391
|
-
triangles.push(p.i);
|
|
392
|
-
triangles.push(b.i);
|
|
393
|
-
|
|
394
|
-
// remove two nodes involved
|
|
395
|
-
removeNode(p);
|
|
396
|
-
removeNode(p.next);
|
|
397
|
-
|
|
398
|
-
p = start = b;
|
|
399
|
-
}
|
|
400
|
-
p = p.next;
|
|
401
|
-
} while (p !== start);
|
|
556
|
+
return filterPoints(bridge, bridge.next);
|
|
557
|
+
}
|
|
402
558
|
|
|
403
|
-
|
|
404
|
-
|
|
559
|
+
// Block-bbox index for findHoleBridge (issue #183): one [minX,minY,maxX,maxY] bbox per K
|
|
560
|
+
// consecutive ring edges, in a flat Float64Array, so the leftward-ray scan can skip whole
|
|
561
|
+
// blocks in O(1) instead of walking the entire merged ring. Grown append-only — the outer
|
|
562
|
+
// ring seeds it, then each merged hole appends a segment (head node, stop node, K-blocks
|
|
563
|
+
// over head..stop); independent segments, not a ring tiling, since splices land mid-ring.
|
|
564
|
+
// Buffers are sized once from the input upper bound and reused across calls.
|
|
565
|
+
//
|
|
566
|
+
// filterPoints only drops collinear/coincident points, so a stale bbox stays a conservative
|
|
567
|
+
// superset of its live edges (never a false skip); the scan skips dead nodes (p.prev.next !==
|
|
568
|
+
// p) and lazily advances a dead stop. Blocks are scanned in append (not ring) order, so the
|
|
569
|
+
// chosen bridge can differ from the un-indexed code — a different but equally valid result.
|
|
570
|
+
const K = 16; // edges per block
|
|
571
|
+
|
|
572
|
+
let blockBBox = new Float64Array(0); // [minX,minY,maxX,maxY] per block
|
|
573
|
+
let numBlocks = 0;
|
|
574
|
+
/** @type {Node[]} */
|
|
575
|
+
const blockHead = []; // first node of each block's segment
|
|
576
|
+
/** @type {Node[]} */
|
|
577
|
+
const blockStop = []; // node just past each block's segment (exclusive walk bound)
|
|
578
|
+
|
|
579
|
+
/** @param {number} maxNodes @param {number} numHoles */
|
|
580
|
+
function buildBlockIndex(maxNodes, numHoles) {
|
|
581
|
+
// upper bound: every input node indexed once, +2 bridge nodes per hole, plus a partial
|
|
582
|
+
// trailing block per appended segment (outer ring + one per hole)
|
|
583
|
+
const maxBlocks = Math.ceil((maxNodes + 2 * numHoles) / K) + numHoles + 2;
|
|
584
|
+
if (blockBBox.length < maxBlocks * 4) blockBBox = new Float64Array(maxBlocks * 4);
|
|
585
|
+
numBlocks = 0;
|
|
586
|
+
}
|
|
405
587
|
|
|
406
|
-
//
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
//
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
588
|
+
// index the ring run head..stop (exclusive) as ceil(len / K) blocks; head === stop means
|
|
589
|
+
// the whole ring. each block's bbox covers both endpoints of every edge it owns.
|
|
590
|
+
/** @param {Node} head @param {Node} stop */
|
|
591
|
+
function indexSegment(head, stop) {
|
|
592
|
+
let p = head;
|
|
593
|
+
do {
|
|
594
|
+
const b = numBlocks++;
|
|
595
|
+
blockHead[b] = p;
|
|
596
|
+
let minX = p.x,
|
|
597
|
+
minY = p.y,
|
|
598
|
+
maxX = p.x,
|
|
599
|
+
maxY = p.y;
|
|
600
|
+
let k = 0;
|
|
601
|
+
do {
|
|
602
|
+
const c = p.next; // edge p->c; bbox must bound both endpoints
|
|
603
|
+
p.z = b; // reuse z as the owning block during eliminateHoles (see growBlock)
|
|
604
|
+
if (c.x < minX) minX = c.x;
|
|
605
|
+
if (c.x > maxX) maxX = c.x;
|
|
606
|
+
if (c.y < minY) minY = c.y;
|
|
607
|
+
if (c.y > maxY) maxY = c.y;
|
|
608
|
+
p = c;
|
|
609
|
+
} while (++k < K && p !== stop);
|
|
610
|
+
blockStop[b] = p;
|
|
611
|
+
const g = b * 4;
|
|
612
|
+
blockBBox[g] = minX;
|
|
613
|
+
blockBBox[g + 1] = minY;
|
|
614
|
+
blockBBox[g + 2] = maxX;
|
|
615
|
+
blockBBox[g + 3] = maxY;
|
|
616
|
+
} while (p !== stop);
|
|
617
|
+
}
|
|
431
618
|
|
|
432
|
-
//
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
)
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
let i;
|
|
444
|
-
let len;
|
|
445
|
-
let start;
|
|
446
|
-
let end;
|
|
447
|
-
let list;
|
|
448
|
-
|
|
449
|
-
for (i = 0, len = holeIndices.length; i < len; i++) {
|
|
450
|
-
start = holeIndices[i] * dim;
|
|
451
|
-
end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
|
|
452
|
-
list = linkedList(data, start, end, dim, false, areas && areas[i + 1], plane);
|
|
453
|
-
if (!list) continue;
|
|
454
|
-
if (list === list.next) list.steiner = true;
|
|
455
|
-
queue.push(getLeftmost(list));
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
queue.sort(compareX);
|
|
459
|
-
|
|
460
|
-
// process holes from left to right
|
|
461
|
-
for (i = 0; i < queue.length; i++) {
|
|
462
|
-
outerNode = eliminateHole(queue[i], outerNode);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
return outerNode;
|
|
466
|
-
}
|
|
619
|
+
// when filterPoints heals an edge head->tail (removing the collinear node between them), the
|
|
620
|
+
// healed edge can extend past head's frozen block bbox if its old far endpoint lived in another
|
|
621
|
+
// block; grow head's block bbox to cover tail so the leftward-ray prune can't false-skip it.
|
|
622
|
+
/** @param {Node} head @param {Node} tail */
|
|
623
|
+
function growBlock(head, tail) {
|
|
624
|
+
const g = head.z * 4;
|
|
625
|
+
if (tail.x < blockBBox[g]) blockBBox[g] = tail.x;
|
|
626
|
+
if (tail.y < blockBBox[g + 1]) blockBBox[g + 1] = tail.y;
|
|
627
|
+
if (tail.x > blockBBox[g + 2]) blockBBox[g + 2] = tail.x;
|
|
628
|
+
if (tail.y > blockBBox[g + 3]) blockBBox[g + 3] = tail.y;
|
|
629
|
+
}
|
|
467
630
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
631
|
+
/** @param {number} b @returns {Node} */
|
|
632
|
+
function liveBlockStop(b) {
|
|
633
|
+
let stop = blockStop[b];
|
|
634
|
+
while (stop.prev.next !== stop) stop = stop.next;
|
|
635
|
+
blockStop[b] = stop;
|
|
636
|
+
return stop;
|
|
637
|
+
}
|
|
471
638
|
|
|
472
|
-
//
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
639
|
+
// the block's head node can be removed by filterPoints during merges; advance it to the next
|
|
640
|
+
// live node so the walk doesn't start on (and immediately terminate at) a dead node. For the
|
|
641
|
+
// single full-ring seed block (head === stop) the same forward advance keeps them equal, so the
|
|
642
|
+
// do-while still laps the whole ring instead of collapsing to an empty walk.
|
|
643
|
+
/** @param {number} b @returns {Node} */
|
|
644
|
+
function liveBlockHead(b) {
|
|
645
|
+
let head = blockHead[b];
|
|
646
|
+
while (head.prev.next !== head) head = head.next;
|
|
647
|
+
blockHead[b] = head;
|
|
648
|
+
return head;
|
|
477
649
|
}
|
|
478
650
|
|
|
479
|
-
|
|
651
|
+
// David Eberly's algorithm for finding a bridge between hole and outer polygon
|
|
652
|
+
/** @param {Node} hole @param {Node} outerNode @returns {Node | null} */
|
|
653
|
+
function findHoleBridge(hole, outerNode) {
|
|
654
|
+
let p = outerNode;
|
|
655
|
+
const hx = hole.x;
|
|
656
|
+
const hy = hole.y;
|
|
657
|
+
let qx = -Infinity;
|
|
658
|
+
/** @type {Node | undefined} */
|
|
659
|
+
let m;
|
|
660
|
+
|
|
661
|
+
// find a segment intersected by a ray from the hole's leftmost point to the left;
|
|
662
|
+
// segment's endpoint with lesser x will be potential connection point
|
|
663
|
+
// unless they intersect at a vertex, then choose the vertex
|
|
664
|
+
if (equals(hole, p)) return p;
|
|
665
|
+
|
|
666
|
+
// scan blocks; skip any whose bbox can't hold a crossing that beats qx and lies left
|
|
667
|
+
// of hx (the prune Morton order can't express — explicit per-axis [minY,maxY]/[minX,maxX])
|
|
668
|
+
for (let b = 0, g = 0; b < numBlocks; b++, g += 4) {
|
|
669
|
+
if (
|
|
670
|
+
hy < blockBBox[g + 1] ||
|
|
671
|
+
hy > blockBBox[g + 3] ||
|
|
672
|
+
blockBBox[g] > hx ||
|
|
673
|
+
blockBBox[g + 2] <= qx
|
|
674
|
+
)
|
|
675
|
+
continue;
|
|
480
676
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
return filterPoints(bridge, bridge.next);
|
|
484
|
-
}
|
|
677
|
+
// ensure the walk's exclusive bound is live so we don't overrun into other blocks
|
|
678
|
+
const stop = liveBlockStop(b);
|
|
485
679
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
if (x <= hx && x > qx) {
|
|
500
|
-
qx = x;
|
|
501
|
-
m = p.x < p.next.x ? p : p.next;
|
|
502
|
-
if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
|
|
503
|
-
}
|
|
680
|
+
p = liveBlockHead(b);
|
|
681
|
+
do {
|
|
682
|
+
if (equals(hole, p.next)) return p.next;
|
|
683
|
+
else if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
|
|
684
|
+
const x = p.x + ((hy - p.y) * (p.next.x - p.x)) / (p.next.y - p.y);
|
|
685
|
+
if (x <= hx && x > qx) {
|
|
686
|
+
qx = x;
|
|
687
|
+
m = p.x < p.next.x ? p : p.next;
|
|
688
|
+
if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
p = p.next;
|
|
692
|
+
} while (p !== stop);
|
|
504
693
|
}
|
|
505
|
-
p = p.next;
|
|
506
|
-
} while (p !== outerNode);
|
|
507
694
|
|
|
508
|
-
|
|
695
|
+
if (!m) return null;
|
|
509
696
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
697
|
+
// look for points inside the triangle of hole point, segment intersection and endpoint;
|
|
698
|
+
// if there are no points found, we have a valid connection;
|
|
699
|
+
// otherwise choose the point of the minimum angle with the ray as connection point
|
|
513
700
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
701
|
+
const mx = m.x;
|
|
702
|
+
const my = m.y;
|
|
703
|
+
const tminY = Math.min(hy, my); // the triangle's y span; x span is [mx, hx]
|
|
704
|
+
const tmaxY = Math.max(hy, my);
|
|
705
|
+
let tanMin = Infinity;
|
|
519
706
|
|
|
520
|
-
|
|
707
|
+
// scan the same blocks; skip any whose bbox can't overlap the triangle's [mx,hx]×[tminY,tmaxY] box
|
|
708
|
+
for (let b = 0, g = 0; b < numBlocks; b++, g += 4) {
|
|
709
|
+
if (
|
|
710
|
+
blockBBox[g + 2] < mx ||
|
|
711
|
+
blockBBox[g] > hx ||
|
|
712
|
+
blockBBox[g + 3] < tminY ||
|
|
713
|
+
blockBBox[g + 1] > tmaxY
|
|
714
|
+
)
|
|
715
|
+
continue;
|
|
521
716
|
|
|
522
|
-
|
|
523
|
-
if (
|
|
524
|
-
hx >= p.x &&
|
|
525
|
-
p.x >= mx &&
|
|
526
|
-
hx !== p.x &&
|
|
527
|
-
pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)
|
|
528
|
-
) {
|
|
529
|
-
tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
|
|
717
|
+
const stop = liveBlockStop(b);
|
|
530
718
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
(
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
719
|
+
p = liveBlockHead(b);
|
|
720
|
+
do {
|
|
721
|
+
if (
|
|
722
|
+
hx >= p.x &&
|
|
723
|
+
p.x >= mx &&
|
|
724
|
+
hx !== p.x &&
|
|
725
|
+
pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)
|
|
726
|
+
) {
|
|
727
|
+
const tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
|
|
728
|
+
|
|
729
|
+
// if hole point sits on p's horizontal edge (T-junction touch): the bridge runs
|
|
730
|
+
// along that edge — locallyInside rejects it as collinear, but it's valid
|
|
731
|
+
if (
|
|
732
|
+
(locallyInside(p, hole) || (p.y === hy && p.next.y === hy && p.next.x > hx)) &&
|
|
733
|
+
(tan < tanMin ||
|
|
734
|
+
(tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))
|
|
735
|
+
) {
|
|
736
|
+
m = p;
|
|
737
|
+
tanMin = tan;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
p = p.next;
|
|
742
|
+
} while (p !== stop);
|
|
539
743
|
}
|
|
540
744
|
|
|
541
|
-
|
|
542
|
-
}
|
|
745
|
+
return m;
|
|
746
|
+
}
|
|
543
747
|
|
|
544
|
-
|
|
545
|
-
}
|
|
748
|
+
// whether sector in vertex m contains sector in vertex p in the same coordinates
|
|
749
|
+
/** @param {Node} m @param {Node} p @returns {boolean} */
|
|
750
|
+
function sectorContainsSector(m, p) {
|
|
751
|
+
return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
|
|
752
|
+
}
|
|
546
753
|
|
|
547
|
-
//
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
754
|
+
// scratch buffers reused across calls and grown on demand: two node-ref arrays that
|
|
755
|
+
// ping-pong during the radix passes, plus parallel z-value arrays so the passes read
|
|
756
|
+
// z from contiguous memory instead of dereferencing each node. 256-entry histogram for
|
|
757
|
+
// 8-bit digits; the small histogram keeps per-call setup cheap (most rings are short)
|
|
758
|
+
/** @type {Node[]} */
|
|
759
|
+
const sortArr = [];
|
|
760
|
+
/** @type {Node[]} */
|
|
761
|
+
let sortBuf = [];
|
|
762
|
+
let zArr = new Uint32Array(0);
|
|
763
|
+
let zBuf = new Uint32Array(0);
|
|
764
|
+
const counts = new Uint32Array(256);
|
|
765
|
+
|
|
766
|
+
// interlink polygon nodes in z-order: collect into an array, sort by z, relink
|
|
767
|
+
/** @param {Node} start @param {number} minX @param {number} minY @param {number} invSize */
|
|
768
|
+
function indexCurve(start, minX, minY, invSize) {
|
|
769
|
+
let p = start;
|
|
770
|
+
let n = 0;
|
|
771
|
+
do {
|
|
772
|
+
// always (re)compute: z may still hold a block index left over from eliminateHoles
|
|
773
|
+
p.z = zOrder(p.x, p.y, minX, minY, invSize);
|
|
774
|
+
sortArr[n++] = p;
|
|
775
|
+
p = p.next;
|
|
776
|
+
} while (p !== start);
|
|
551
777
|
|
|
552
|
-
|
|
553
|
-
function indexCurve(start, minX, minY, invSize) {
|
|
554
|
-
let p = start;
|
|
555
|
-
do {
|
|
556
|
-
if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
|
|
557
|
-
p.prevZ = p.prev;
|
|
558
|
-
p.nextZ = p.next;
|
|
559
|
-
p = p.next;
|
|
560
|
-
} while (p !== start);
|
|
778
|
+
sortNodes(n);
|
|
561
779
|
|
|
562
|
-
|
|
563
|
-
|
|
780
|
+
/** @type {Node | null} */
|
|
781
|
+
let prev = null;
|
|
782
|
+
for (let i = 0; i < n; i++) {
|
|
783
|
+
const node = sortArr[i];
|
|
784
|
+
node.prevZ = prev;
|
|
785
|
+
if (prev) prev.nextZ = node;
|
|
786
|
+
prev = node;
|
|
787
|
+
}
|
|
788
|
+
/** @type {Node} */ (prev).nextZ = null;
|
|
564
789
|
|
|
565
|
-
|
|
566
|
-
|
|
790
|
+
// drop the node refs but keep the capacity: setting length to 0 leaves sortBuf unresized
|
|
791
|
+
// (its growth is gated on zArr), so the radix scatter regrows it out of order every call
|
|
792
|
+
sortArr.fill(/** @type {Node} */ (/** @type {unknown} */ (null)), 0, n);
|
|
793
|
+
sortBuf.fill(/** @type {Node} */ (/** @type {unknown} */ (null)), 0, n);
|
|
794
|
+
}
|
|
567
795
|
|
|
568
|
-
//
|
|
569
|
-
//
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
do {
|
|
582
|
-
p = list;
|
|
583
|
-
list = null;
|
|
584
|
-
tail = null;
|
|
585
|
-
numMerges = 0;
|
|
586
|
-
|
|
587
|
-
while (p) {
|
|
588
|
-
numMerges++;
|
|
589
|
-
q = p;
|
|
590
|
-
pSize = 0;
|
|
591
|
-
for (i = 0; i < inSize; i++) {
|
|
592
|
-
pSize++;
|
|
593
|
-
q = q.nextZ;
|
|
594
|
-
if (!q) break;
|
|
595
|
-
}
|
|
596
|
-
qSize = inSize;
|
|
597
|
-
|
|
598
|
-
while (pSize > 0 || (qSize > 0 && q)) {
|
|
599
|
-
if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
|
|
600
|
-
e = p;
|
|
601
|
-
p = p.nextZ;
|
|
602
|
-
pSize--;
|
|
603
|
-
} else {
|
|
604
|
-
e = q;
|
|
605
|
-
q = q.nextZ;
|
|
606
|
-
qSize--;
|
|
796
|
+
// sort the first n nodes of sortArr by z, in place: insertion sort for small n (cheaper
|
|
797
|
+
// than histogram setup), else LSD radix in four 8-bit passes (covering z's 30 bits)
|
|
798
|
+
/** @param {number} n */
|
|
799
|
+
function sortNodes(n) {
|
|
800
|
+
if (n <= 32) {
|
|
801
|
+
for (let i = 1; i < n; i++) {
|
|
802
|
+
const node = sortArr[i],
|
|
803
|
+
z = node.z;
|
|
804
|
+
let j = i - 1;
|
|
805
|
+
while (j >= 0 && sortArr[j].z > z) {
|
|
806
|
+
sortArr[j + 1] = sortArr[j];
|
|
807
|
+
j--;
|
|
607
808
|
}
|
|
809
|
+
sortArr[j + 1] = node;
|
|
810
|
+
}
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
608
813
|
|
|
609
|
-
|
|
610
|
-
|
|
814
|
+
if (zArr.length < n) {
|
|
815
|
+
zArr = new Uint32Array(n);
|
|
816
|
+
zBuf = new Uint32Array(n);
|
|
817
|
+
sortBuf = new Array(n);
|
|
818
|
+
}
|
|
819
|
+
for (let i = 0; i < n; i++) zArr[i] = sortArr[i].z;
|
|
611
820
|
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
821
|
+
// even pass count lands the sorted result back in sortArr
|
|
822
|
+
radixPass(n, sortArr, zArr, sortBuf, zBuf, 0);
|
|
823
|
+
radixPass(n, sortBuf, zBuf, sortArr, zArr, 8);
|
|
824
|
+
radixPass(n, sortArr, zArr, sortBuf, zBuf, 16);
|
|
825
|
+
radixPass(n, sortBuf, zBuf, sortArr, zArr, 24);
|
|
826
|
+
}
|
|
615
827
|
|
|
616
|
-
|
|
828
|
+
// one LSD radix pass: stably scatter the first n nodes (and their z) from src to dst,
|
|
829
|
+
// bucketed by the 8-bit digit of z at the given bit shift
|
|
830
|
+
/** @param {number} n @param {Node[]} src @param {Uint32Array} srcZ @param {Node[]} dst @param {Uint32Array} dstZ @param {number} shift */
|
|
831
|
+
function radixPass(n, src, srcZ, dst, dstZ, shift) {
|
|
832
|
+
counts.fill(0);
|
|
833
|
+
for (let i = 0; i < n; i++) counts[(srcZ[i] >>> shift) & 0xff]++;
|
|
834
|
+
// turn per-bucket counts into start offsets (prefix sum)
|
|
835
|
+
let sum = 0;
|
|
836
|
+
for (let b = 0; b < 256; b++) {
|
|
837
|
+
const c = counts[b];
|
|
838
|
+
counts[b] = sum;
|
|
839
|
+
sum += c;
|
|
617
840
|
}
|
|
841
|
+
for (let i = 0; i < n; i++) {
|
|
842
|
+
const z = srcZ[i];
|
|
843
|
+
const pos = counts[(z >>> shift) & 0xff]++;
|
|
844
|
+
dst[pos] = src[i];
|
|
845
|
+
dstZ[pos] = z;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
618
848
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
849
|
+
// z-order of a point given coords and inverse of the longer side of data bbox
|
|
850
|
+
/** @param {number} x @param {number} y @param {number} minX @param {number} minY @param {number} invSize @returns {number} */
|
|
851
|
+
function zOrder(x, y, minX, minY, invSize) {
|
|
852
|
+
// coords are transformed into non-negative 15-bit integer range
|
|
853
|
+
x = ((x - minX) * invSize) | 0;
|
|
854
|
+
y = ((y - minY) * invSize) | 0;
|
|
622
855
|
|
|
623
|
-
|
|
624
|
-
|
|
856
|
+
x = (x | (x << 8)) & 0x00ff00ff;
|
|
857
|
+
x = (x | (x << 4)) & 0x0f0f0f0f;
|
|
858
|
+
x = (x | (x << 2)) & 0x33333333;
|
|
859
|
+
x = (x | (x << 1)) & 0x55555555;
|
|
625
860
|
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
y = ((y - minY) * invSize) | 0;
|
|
861
|
+
y = (y | (y << 8)) & 0x00ff00ff;
|
|
862
|
+
y = (y | (y << 4)) & 0x0f0f0f0f;
|
|
863
|
+
y = (y | (y << 2)) & 0x33333333;
|
|
864
|
+
y = (y | (y << 1)) & 0x55555555;
|
|
631
865
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
x = (x | (x << 2)) & 0x33333333;
|
|
635
|
-
x = (x | (x << 1)) & 0x55555555;
|
|
866
|
+
return x | (y << 1);
|
|
867
|
+
}
|
|
636
868
|
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
869
|
+
// find the leftmost node of a polygon ring
|
|
870
|
+
/** @param {Node} start @returns {Node} */
|
|
871
|
+
function getLeftmost(start) {
|
|
872
|
+
let p = start,
|
|
873
|
+
leftmost = start;
|
|
874
|
+
do {
|
|
875
|
+
if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
|
|
876
|
+
p = p.next;
|
|
877
|
+
} while (p !== start);
|
|
641
878
|
|
|
642
|
-
|
|
643
|
-
}
|
|
879
|
+
return leftmost;
|
|
880
|
+
}
|
|
644
881
|
|
|
645
|
-
//
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
882
|
+
// check if a point lies within a convex triangle
|
|
883
|
+
/** @param {number} ax @param {number} ay @param {number} bx @param {number} by @param {number} cx @param {number} cy @param {number} px @param {number} py @returns {boolean} */
|
|
884
|
+
function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
|
|
885
|
+
return (
|
|
886
|
+
(cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
|
|
887
|
+
(ax - px) * (by - py) >= (bx - px) * (ay - py) &&
|
|
888
|
+
(bx - px) * (cy - py) >= (cx - px) * (by - py)
|
|
889
|
+
);
|
|
890
|
+
}
|
|
653
891
|
|
|
654
|
-
|
|
655
|
-
}
|
|
892
|
+
// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
|
|
893
|
+
/** @param {Node} a @param {Node} b @returns {boolean} true when the diagonal is valid */
|
|
894
|
+
function isValidDiagonal(a, b) {
|
|
895
|
+
const zeroLength = equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0; // degenerate case
|
|
896
|
+
return (
|
|
897
|
+
a.next.i !== b.i &&
|
|
898
|
+
(zeroLength ||
|
|
899
|
+
(locallyInside(a, b) &&
|
|
900
|
+
locallyInside(b, a) && // // locally visible
|
|
901
|
+
(area(a.prev, a, b.prev) !== 0 || area(a, b.prev, b) !== 0))) && // no opposite-facing sectors
|
|
902
|
+
!intersectsPolygon(a, b) &&
|
|
903
|
+
(zeroLength || middleInside(a, b))
|
|
904
|
+
); // doesn't intersect other edges, diagonal inside polygon
|
|
905
|
+
}
|
|
656
906
|
|
|
657
|
-
//
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
(
|
|
661
|
-
|
|
662
|
-
(bx - px) * (cy - py) >= (cx - px) * (by - py)
|
|
663
|
-
);
|
|
664
|
-
}
|
|
907
|
+
// signed area of a triangle
|
|
908
|
+
/** @param {Node} p @param {Node} q @param {Node} r @returns {number} */
|
|
909
|
+
function area(p, q, r) {
|
|
910
|
+
return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
|
|
911
|
+
}
|
|
665
912
|
|
|
666
|
-
// check if
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
!intersectsPolygon(a, b) && // dones't intersect other edges
|
|
672
|
-
((locallyInside(a, b) &&
|
|
673
|
-
locallyInside(b, a) &&
|
|
674
|
-
middleInside(a, b) && // locally visible
|
|
675
|
-
(area(a.prev, a, b.prev) || area(a, b.prev, b))) || // does not create opposite-facing sectors
|
|
676
|
-
(equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0))
|
|
677
|
-
); // special zero-length case
|
|
678
|
-
}
|
|
913
|
+
// check if two points are equal
|
|
914
|
+
/** @param {Node} p1 @param {Node} p2 @returns {boolean} */
|
|
915
|
+
function equals(p1, p2) {
|
|
916
|
+
return p1.x === p2.x && p1.y === p2.y;
|
|
917
|
+
}
|
|
679
918
|
|
|
680
|
-
//
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
919
|
+
// check if two segments intersect; by default includes collinear boundary touches
|
|
920
|
+
/** @param {Node} p1 @param {Node} q1 @param {Node} p2 @param {Node} q2 @param {boolean} [includeBoundary] @returns {boolean} */
|
|
921
|
+
function intersects(p1, q1, p2, q2, includeBoundary = true) {
|
|
922
|
+
const o1 = area(p1, q1, p2);
|
|
923
|
+
const o2 = area(p1, q1, q2);
|
|
924
|
+
const o3 = area(p2, q2, p1);
|
|
925
|
+
const o4 = area(p2, q2, q1);
|
|
684
926
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
return p1.x === p2.x && p1.y === p2.y;
|
|
688
|
-
}
|
|
927
|
+
if (((o1 > 0 && o2 < 0) || (o1 < 0 && o2 > 0)) && ((o3 > 0 && o4 < 0) || (o3 < 0 && o4 > 0)))
|
|
928
|
+
return true;
|
|
689
929
|
|
|
690
|
-
|
|
691
|
-
function intersects(p1, q1, p2, q2) {
|
|
692
|
-
const o1 = sign(area(p1, q1, p2));
|
|
693
|
-
const o2 = sign(area(p1, q1, q2));
|
|
694
|
-
const o3 = sign(area(p2, q2, p1));
|
|
695
|
-
const o4 = sign(area(p2, q2, q1));
|
|
930
|
+
if (!includeBoundary) return false;
|
|
696
931
|
|
|
697
|
-
|
|
932
|
+
if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
|
|
933
|
+
if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
|
|
934
|
+
if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
|
|
935
|
+
if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
|
|
698
936
|
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
|
|
702
|
-
if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
|
|
937
|
+
return false;
|
|
938
|
+
}
|
|
703
939
|
|
|
704
|
-
|
|
705
|
-
}
|
|
940
|
+
// for collinear points p, q, r, check if point q lies on segment pr
|
|
941
|
+
/** @param {Node} p @param {Node} q @param {Node} r @returns {boolean} */
|
|
942
|
+
function onSegment(p, q, r) {
|
|
943
|
+
return (
|
|
944
|
+
q.x <= Math.max(p.x, r.x) &&
|
|
945
|
+
q.x >= Math.min(p.x, r.x) &&
|
|
946
|
+
q.y <= Math.max(p.y, r.y) &&
|
|
947
|
+
q.y >= Math.min(p.y, r.y)
|
|
948
|
+
);
|
|
949
|
+
}
|
|
706
950
|
|
|
707
|
-
//
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
951
|
+
// check if a polygon diagonal intersects any polygon segments
|
|
952
|
+
/** @param {Node} a @param {Node} b @returns {boolean} */
|
|
953
|
+
function intersectsPolygon(a, b) {
|
|
954
|
+
// diagonal bbox; an edge whose bbox can't overlap it can't intersect it, so
|
|
955
|
+
// skip the orientation test for those (the common case — the diagonal is short)
|
|
956
|
+
const minX = Math.min(a.x, b.x);
|
|
957
|
+
const maxX = Math.max(a.x, b.x);
|
|
958
|
+
const minY = Math.min(a.y, b.y);
|
|
959
|
+
const maxY = Math.max(a.y, b.y);
|
|
960
|
+
|
|
961
|
+
let p = a;
|
|
962
|
+
do {
|
|
963
|
+
const n = p.next;
|
|
964
|
+
if (
|
|
965
|
+
(p.x > maxX && n.x > maxX) ||
|
|
966
|
+
(p.x < minX && n.x < minX) ||
|
|
967
|
+
(p.y > maxY && n.y > maxY) ||
|
|
968
|
+
(p.y < minY && n.y < minY)
|
|
969
|
+
) {
|
|
970
|
+
p = n;
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
if (p.i !== a.i && n.i !== a.i && p.i !== b.i && n.i !== b.i && intersects(p, n, a, b))
|
|
974
|
+
return true;
|
|
975
|
+
p = n;
|
|
976
|
+
} while (p !== a);
|
|
716
977
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
}
|
|
978
|
+
return false;
|
|
979
|
+
}
|
|
720
980
|
|
|
721
|
-
// check if a polygon diagonal
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
p.i !== b.i &&
|
|
729
|
-
p.next.i !== b.i &&
|
|
730
|
-
intersects(p, p.next, a, b)
|
|
731
|
-
)
|
|
732
|
-
return true;
|
|
733
|
-
p = p.next;
|
|
734
|
-
} while (p !== a);
|
|
981
|
+
// check if a polygon diagonal is locally inside the polygon
|
|
982
|
+
/** @param {Node} a @param {Node} b @returns {boolean} */
|
|
983
|
+
function locallyInside(a, b) {
|
|
984
|
+
return area(a.prev, a, a.next) < 0
|
|
985
|
+
? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0
|
|
986
|
+
: area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
|
|
987
|
+
}
|
|
735
988
|
|
|
736
|
-
|
|
737
|
-
}
|
|
989
|
+
// check if the middle point of a polygon diagonal is inside the polygon
|
|
990
|
+
/** @param {Node} a @param {Node} b @returns {boolean} */
|
|
991
|
+
function middleInside(a, b) {
|
|
992
|
+
let p = a;
|
|
993
|
+
let inside = false;
|
|
994
|
+
const px = (a.x + b.x) / 2;
|
|
995
|
+
const py = (a.y + b.y) / 2;
|
|
996
|
+
do {
|
|
997
|
+
const n = p.next;
|
|
998
|
+
if (p.y > py !== n.y > py && px < ((n.x - p.x) * (py - p.y)) / (n.y - p.y) + p.x)
|
|
999
|
+
inside = !inside;
|
|
1000
|
+
p = n;
|
|
1001
|
+
} while (p !== a);
|
|
1002
|
+
|
|
1003
|
+
return inside;
|
|
1004
|
+
}
|
|
738
1005
|
|
|
739
|
-
//
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
1006
|
+
// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
|
|
1007
|
+
// if one belongs to the outer ring and another to a hole, it merges it into a single ring
|
|
1008
|
+
/** @param {Node} a @param {Node} b @returns {Node} */
|
|
1009
|
+
function splitPolygon(a, b) {
|
|
1010
|
+
const a2 = createNode(a.i, a.x, a.y),
|
|
1011
|
+
b2 = createNode(b.i, b.x, b.y),
|
|
1012
|
+
an = a.next,
|
|
1013
|
+
bp = b.prev;
|
|
745
1014
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
let p = a;
|
|
749
|
-
let inside = false;
|
|
750
|
-
const px = (a.x + b.x) / 2;
|
|
751
|
-
const py = (a.y + b.y) / 2;
|
|
752
|
-
do {
|
|
753
|
-
if (
|
|
754
|
-
p.y > py !== p.next.y > py &&
|
|
755
|
-
p.next.y !== p.y &&
|
|
756
|
-
px < ((p.next.x - p.x) * (py - p.y)) / (p.next.y - p.y) + p.x
|
|
757
|
-
)
|
|
758
|
-
inside = !inside;
|
|
759
|
-
p = p.next;
|
|
760
|
-
} while (p !== a);
|
|
1015
|
+
a.next = b;
|
|
1016
|
+
b.prev = a;
|
|
761
1017
|
|
|
762
|
-
|
|
763
|
-
|
|
1018
|
+
a2.next = an;
|
|
1019
|
+
an.prev = a2;
|
|
764
1020
|
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
function splitPolygon(a, b) {
|
|
768
|
-
const a2 = allocateVertex(a.i, a.x, a.y);
|
|
769
|
-
const b2 = allocateVertex(b.i, b.x, b.y);
|
|
770
|
-
const an = a.next;
|
|
771
|
-
const bp = b.prev;
|
|
1021
|
+
b2.next = a2;
|
|
1022
|
+
a2.prev = b2;
|
|
772
1023
|
|
|
773
|
-
|
|
774
|
-
|
|
1024
|
+
bp.next = b2;
|
|
1025
|
+
b2.prev = bp;
|
|
775
1026
|
|
|
776
|
-
|
|
777
|
-
|
|
1027
|
+
return b2;
|
|
1028
|
+
}
|
|
778
1029
|
|
|
779
|
-
|
|
780
|
-
|
|
1030
|
+
// create a node and optionally link it with previous one (in a circular doubly linked list)
|
|
1031
|
+
/** @param {number} i @param {number} x @param {number} y @param {Node | null} last @returns {Node} */
|
|
1032
|
+
function insertNode(i, x, y, last) {
|
|
1033
|
+
const p = createNode(i, x, y);
|
|
781
1034
|
|
|
782
|
-
|
|
783
|
-
|
|
1035
|
+
if (!last) {
|
|
1036
|
+
p.prev = p;
|
|
1037
|
+
p.next = p;
|
|
1038
|
+
} else {
|
|
1039
|
+
p.next = last.next;
|
|
1040
|
+
p.prev = last;
|
|
1041
|
+
last.next.prev = p;
|
|
1042
|
+
last.next = p;
|
|
1043
|
+
}
|
|
1044
|
+
return p;
|
|
1045
|
+
}
|
|
784
1046
|
|
|
785
|
-
|
|
786
|
-
|
|
1047
|
+
/** @param {Node} p */
|
|
1048
|
+
function removeNode(p) {
|
|
1049
|
+
p.next.prev = p.prev;
|
|
1050
|
+
p.prev.next = p.next;
|
|
787
1051
|
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
const p = allocateVertex(i, x, y);
|
|
1052
|
+
if (p.prevZ) p.prevZ.nextZ = p.nextZ;
|
|
1053
|
+
if (p.nextZ) p.nextZ.prevZ = p.prevZ;
|
|
791
1054
|
|
|
792
|
-
|
|
793
|
-
p.prev
|
|
794
|
-
p.next = p;
|
|
795
|
-
} else {
|
|
796
|
-
p.next = last.next;
|
|
797
|
-
p.prev = last;
|
|
798
|
-
last.next.prev = p;
|
|
799
|
-
last.next = p;
|
|
1055
|
+
// keep the hole-bridge index's block bboxes covering the healed prev->next edge
|
|
1056
|
+
if (indexActive) growBlock(p.prev, p.next);
|
|
800
1057
|
}
|
|
801
|
-
return p;
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
function removeNode(p) {
|
|
805
|
-
p.next.prev = p.prev;
|
|
806
|
-
p.prev.next = p.next;
|
|
807
1058
|
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
1059
|
+
/** @param {number} i @param {number} x @param {number} y @returns {Node} */
|
|
1060
|
+
function createNode(i, x, y) {
|
|
1061
|
+
let vertex = vertexPool[vertexPoolIndex];
|
|
1062
|
+
if (!vertex) vertexPool[vertexPoolIndex] = vertex = new Vertex(i, x, y);
|
|
1063
|
+
vertexPoolIndex++;
|
|
1064
|
+
vertex.i = i;
|
|
1065
|
+
vertex.x = x;
|
|
1066
|
+
vertex.y = y;
|
|
1067
|
+
vertex.prev = vertex.next = vertex.prevZ = vertex.nextZ = null;
|
|
1068
|
+
vertex.z = 0;
|
|
1069
|
+
return vertex;
|
|
1070
|
+
}
|
|
811
1071
|
|
|
812
|
-
|
|
813
|
-
const vertexPool: Vertex[] = [];
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
1072
|
+
/** Reusable nodes; caller arrays and output triangles are never retained. */
|
|
1073
|
+
const vertexPool: Vertex[] = [];
|
|
1074
|
+
/** Next free node in this call. */
|
|
1075
|
+
let vertexPoolIndex = 0;
|
|
1076
|
+
/** Leftmost hole vertices awaiting bridging. */
|
|
1077
|
+
const holeQueue: Vertex[] = [];
|
|
818
1078
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
1079
|
+
return (
|
|
1080
|
+
positions: NumericArray,
|
|
1081
|
+
holeIndices: NumericArray | undefined,
|
|
1082
|
+
dim: number,
|
|
1083
|
+
areas: NumericArray | undefined,
|
|
1084
|
+
plane: Plane2D,
|
|
1085
|
+
options: EarcutOptions
|
|
1086
|
+
): number[] => {
|
|
1087
|
+
try {
|
|
1088
|
+
balanceTriangles = options.triangulation === 'balanced';
|
|
1089
|
+
return triangulate(positions, holeIndices, dim, areas, plane);
|
|
1090
|
+
} finally {
|
|
1091
|
+
// Also restore state when an input getter throws during ring construction.
|
|
1092
|
+
vertexPoolIndex = 0;
|
|
1093
|
+
indexActive = false;
|
|
1094
|
+
filteredOut = false;
|
|
1095
|
+
steiners.clear();
|
|
1096
|
+
splitStack.length = holeQueue.length = blockHead.length = blockStop.length = 0;
|
|
1097
|
+
numBlocks = 0;
|
|
1098
|
+
// Bound retained storage after a one-off large polygon, including numeric indexes.
|
|
1099
|
+
if (vertexPool.length > 65536 || blockBBox.length > 65536 * 4 || zArr.length > 65536) {
|
|
1100
|
+
vertexPool.length = sortArr.length = sortBuf.length = 0;
|
|
1101
|
+
blockBBox = new Float64Array(0);
|
|
1102
|
+
zArr = new Uint32Array(0);
|
|
1103
|
+
zBuf = new Uint32Array(0);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
830
1107
|
}
|
|
831
|
-
|
|
1108
|
+
/** A pooled vertex with polygon-ring and spatial-index links. */
|
|
832
1109
|
class Vertex {
|
|
833
|
-
|
|
1110
|
+
/** Vertex index, independent of coordinate stride. */
|
|
834
1111
|
i: number;
|
|
835
1112
|
|
|
836
|
-
|
|
1113
|
+
/** First coordinate in the selected projection plane. */
|
|
837
1114
|
x: number;
|
|
1115
|
+
/** Second coordinate in the selected projection plane. */
|
|
838
1116
|
y: number;
|
|
839
1117
|
|
|
840
|
-
|
|
1118
|
+
/** Previous node in the polygon ring. */
|
|
841
1119
|
prev: Vertex = null;
|
|
1120
|
+
/** Next node in the polygon ring. */
|
|
842
1121
|
next: Vertex = null;
|
|
843
1122
|
|
|
844
|
-
|
|
1123
|
+
/** Z-order value, or owning block while bridging holes. */
|
|
845
1124
|
z: number = 0;
|
|
846
1125
|
|
|
847
|
-
|
|
1126
|
+
/** Previous node in z-order. */
|
|
848
1127
|
prevZ: Vertex = null;
|
|
1128
|
+
/** Next node in z-order. */
|
|
849
1129
|
nextZ: Vertex = null;
|
|
850
1130
|
|
|
851
|
-
|
|
852
|
-
steiner: boolean = false;
|
|
853
|
-
|
|
1131
|
+
/** Initialize a node; links are assigned before the first read. */
|
|
854
1132
|
constructor(i: number, x: number, y: number) {
|
|
855
1133
|
this.i = i;
|
|
856
1134
|
this.x = x;
|