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