@es3d/es3d 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +86 -86
  3. package/docs/SPEC.md +417 -417
  4. package/package.json +1 -1
  5. package/scripts/es3d-cli.mjs +377 -377
  6. package/src/cache/geometry-store.js +392 -392
  7. package/src/cache/mesh-bundle.js +123 -123
  8. package/src/cache/sha256.js +73 -73
  9. package/src/format/ast.js +238 -238
  10. package/src/format/colors.js +93 -93
  11. package/src/format/compact-parser.js +171 -171
  12. package/src/format/compact-tokenizer.js +165 -165
  13. package/src/format/errors.js +247 -247
  14. package/src/format/json.js +55 -55
  15. package/src/format/limits.js +14 -14
  16. package/src/format/parse.js +92 -92
  17. package/src/format/schema.js +617 -617
  18. package/src/format/serializer.js +333 -333
  19. package/src/format/validator.js +330 -330
  20. package/src/format/value-error.js +12 -12
  21. package/src/format/value-format.js +314 -314
  22. package/src/format/values.js +459 -459
  23. package/src/format/xml-parser.js +136 -136
  24. package/src/format/xml-tokenizer.js +237 -237
  25. package/src/geometry/contour.js +86 -86
  26. package/src/geometry/extrude.js +175 -175
  27. package/src/geometry/gear.js +85 -85
  28. package/src/geometry/geometry-cache.js +159 -159
  29. package/src/geometry/lod-error.js +178 -178
  30. package/src/geometry/mesh.js +458 -458
  31. package/src/geometry/primitives.js +299 -299
  32. package/src/geometry/registry.js +645 -645
  33. package/src/geometry/revolve.js +136 -136
  34. package/src/geometry/sweep.js +441 -441
  35. package/src/index.js +47 -47
  36. package/src/io/compression.js +67 -67
  37. package/src/io/convert.js +236 -236
  38. package/src/io/download.js +13 -13
  39. package/src/io/external-scene.js +127 -127
  40. package/src/io/file-loader.js +28 -28
  41. package/src/io/gltf.js +630 -630
  42. package/src/io/mesh-import.js +104 -104
  43. package/src/io/obj.js +352 -352
  44. package/src/io/step-brep.js +439 -439
  45. package/src/io/step-exact.js +1149 -1149
  46. package/src/io/step.js +1418 -1418
  47. package/src/io/stl.js +193 -193
  48. package/src/io/svg-import.js +787 -787
  49. package/src/material/materials.js +154 -154
  50. package/src/math/mat4.js +475 -475
  51. package/src/math/random.js +30 -30
  52. package/src/math/trig.js +65 -65
  53. package/src/math/vec3.js +81 -81
  54. package/src/optimize/optimizer.js +579 -579
  55. package/src/path/path-flatten.js +322 -322
  56. package/src/path/path-parser.js +332 -332
  57. package/src/path/shapes.js +129 -129
  58. package/src/path/triangulate.js +517 -517
  59. package/src/pipeline.js +74 -74
  60. package/src/render/camera.js +168 -168
  61. package/src/render/frame-builder.js +362 -362
  62. package/src/render/geometry-pool.js +149 -149
  63. package/src/render/lighting.js +149 -149
  64. package/src/render/orbit-controls.js +237 -237
  65. package/src/render/renderer.js +258 -249
  66. package/src/render/scroll-timeline.js +104 -104
  67. package/src/render/shaders/glsl.js +285 -285
  68. package/src/render/shaders/wgsl.js +252 -252
  69. package/src/render/stage.js +364 -352
  70. package/src/render/texture-cache.js +40 -40
  71. package/src/render/textures.js +223 -223
  72. package/src/render/webgl2/webgl2-backend.js +586 -532
  73. package/src/render/webgpu/webgpu-backend.js +482 -480
  74. package/src/scene/flatten.js +754 -754
  75. package/src/scene/picking.js +161 -161
  76. package/src/scene/scene-graph.js +320 -320
  77. package/src/transform/transform.js +211 -211
  78. package/src/wasm/kernels-binary.js +3 -3
  79. package/src/wasm/kernels.js +385 -385
@@ -1,517 +1,517 @@
1
- // Polygon triangulation with holes.
2
- //
3
- // Ear clipping on a doubly linked vertex list with hole elimination by
4
- // bridging (David Eberly's method), z-order curve acceleration for large
5
- // polygons and the classic fallbacks for degenerate input (curing local
6
- // self-intersections, splitting the polygon). The algorithm follows the
7
- // structure popularised by Mapbox "earcut" (ISC license, Vladimir Agafonkin).
8
- //
9
- // Input: flat coordinate array [x0,y0,x1,y1,...] and hole start indices (in
10
- // vertices). Output: triangle vertex indices, counter-clockwise (y up).
11
-
12
- import { triangulateWasm } from '../wasm/kernels.js';
13
-
14
- export function triangulate(data, holeIndices = null) {
15
- const w = triangulateWasm(data, holeIndices);
16
- if (w) return w;
17
- const dim = 2;
18
- const hasHoles = holeIndices && holeIndices.length;
19
- const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
20
- let outerNode = linkedList(data, 0, outerLen, dim, true);
21
- const triangles = [];
22
- if (!outerNode || outerNode.next === outerNode.prev) return triangles;
23
-
24
- if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
25
-
26
- let minX = 0, minY = 0, invSize = 0;
27
- if (data.length > 80 * dim) {
28
- minX = Infinity;
29
- minY = Infinity;
30
- let maxX = -Infinity, maxY = -Infinity;
31
- for (let i = 0; i < outerLen; i += dim) {
32
- const x = data[i], y = data[i + 1];
33
- if (x < minX) minX = x;
34
- if (y < minY) minY = y;
35
- if (x > maxX) maxX = x;
36
- if (y > maxY) maxY = y;
37
- }
38
- invSize = Math.max(maxX - minX, maxY - minY);
39
- invSize = invSize !== 0 ? 32767 / invSize : 0;
40
- }
41
- earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
42
- return triangles;
43
- }
44
-
45
- class Node {
46
- constructor(i, x, y) {
47
- this.i = i;
48
- this.x = x;
49
- this.y = y;
50
- this.prev = null;
51
- this.next = null;
52
- this.z = 0;
53
- this.prevZ = null;
54
- this.nextZ = null;
55
- this.steiner = false;
56
- }
57
- }
58
-
59
- // keeps the class's hidden class alive between triangulations (see MeshBuilder.shapeKeeper);
60
- // fractional coordinates, so its fields have the representation real nodes use
61
- Node.shapeKeeper = new Node(0, 0.5, 0.5);
62
-
63
- function linkedList(data, start, end, dim, clockwise) {
64
- let last = null;
65
- if (clockwise === signedArea(data, start, end, dim) > 0) {
66
- for (let i = start; i < end; i += dim) last = insertNode(i / dim, data[i], data[i + 1], last);
67
- } else {
68
- for (let i = end - dim; i >= start; i -= dim) last = insertNode(i / dim, data[i], data[i + 1], last);
69
- }
70
- if (last && equals(last, last.next)) {
71
- removeNode(last);
72
- last = last.next;
73
- }
74
- return last;
75
- }
76
-
77
- function filterPoints(start, end) {
78
- if (!start) return start;
79
- if (!end) end = start;
80
- let p = start;
81
- let again;
82
- do {
83
- again = false;
84
- if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
85
- removeNode(p);
86
- p = end = p.prev;
87
- if (p === p.next) break;
88
- again = true;
89
- } else {
90
- p = p.next;
91
- }
92
- } while (again || p !== end);
93
- return end;
94
- }
95
-
96
- function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
97
- if (!ear) return;
98
- if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
99
- let stop = ear;
100
- while (ear.prev !== ear.next) {
101
- const prev = ear.prev;
102
- const next = ear.next;
103
- if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
104
- triangles.push(prev.i, ear.i, next.i);
105
- removeNode(ear);
106
- // skipping the next vertex leads to fewer sliver triangles
107
- ear = next.next;
108
- stop = next.next;
109
- continue;
110
- }
111
- ear = next;
112
- if (ear === stop) {
113
- if (!pass) {
114
- earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
115
- } else if (pass === 1) {
116
- ear = cureLocalIntersections(filterPoints(ear), triangles);
117
- earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
118
- } else if (pass === 2) {
119
- splitEarcut(ear, triangles, dim, minX, minY, invSize);
120
- }
121
- break;
122
- }
123
- }
124
- }
125
-
126
- function isEar(ear) {
127
- const a = ear.prev, b = ear, c = ear.next;
128
- if (area(a, b, c) >= 0) return false; // reflex
129
- const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
130
- const x0 = Math.min(ax, bx, cx), y0 = Math.min(ay, by, cy);
131
- const x1 = Math.max(ax, bx, cx), y1 = Math.max(ay, by, cy);
132
- let p = c.next;
133
- while (p !== a) {
134
- if (
135
- p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
136
- pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) &&
137
- area(p.prev, p, p.next) >= 0
138
- ) return false;
139
- p = p.next;
140
- }
141
- return true;
142
- }
143
-
144
- function isEarHashed(ear, minX, minY, invSize) {
145
- const a = ear.prev, b = ear, c = ear.next;
146
- if (area(a, b, c) >= 0) return false;
147
- const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
148
- const x0 = Math.min(ax, bx, cx), y0 = Math.min(ay, by, cy);
149
- const x1 = Math.max(ax, bx, cx), y1 = Math.max(ay, by, cy);
150
- const minZ = zOrder(x0, y0, minX, minY, invSize);
151
- const maxZ = zOrder(x1, y1, minX, minY, invSize);
152
- let p = ear.prevZ;
153
- let n = ear.nextZ;
154
- while (p && p.z >= minZ && n && n.z <= maxZ) {
155
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
156
- pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
157
- p = p.prevZ;
158
- if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
159
- pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
160
- n = n.nextZ;
161
- }
162
- while (p && p.z >= minZ) {
163
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
164
- pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
165
- p = p.prevZ;
166
- }
167
- while (n && n.z <= maxZ) {
168
- if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
169
- pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
170
- n = n.nextZ;
171
- }
172
- return true;
173
- }
174
-
175
- function cureLocalIntersections(start, triangles) {
176
- let p = start;
177
- do {
178
- const a = p.prev, b = p.next.next;
179
- if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
180
- triangles.push(a.i, p.i, b.i);
181
- removeNode(p);
182
- removeNode(p.next);
183
- p = start = b;
184
- }
185
- p = p.next;
186
- } while (p !== start);
187
- return filterPoints(p);
188
- }
189
-
190
- function splitEarcut(start, triangles, dim, minX, minY, invSize) {
191
- let a = start;
192
- do {
193
- let b = a.next.next;
194
- while (b !== a.prev) {
195
- if (a.i !== b.i && isValidDiagonal(a, b)) {
196
- let c = splitPolygon(a, b);
197
- a = filterPoints(a, a.next);
198
- c = filterPoints(c, c.next);
199
- earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
200
- earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
201
- return;
202
- }
203
- b = b.next;
204
- }
205
- a = a.next;
206
- } while (a !== start);
207
- }
208
-
209
- function eliminateHoles(data, holeIndices, outerNode, dim) {
210
- const queue = [];
211
- for (let i = 0, len = holeIndices.length; i < len; i++) {
212
- const start = holeIndices[i] * dim;
213
- const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
214
- const list = linkedList(data, start, end, dim, false);
215
- if (!list) continue;
216
- if (list === list.next) list.steiner = true;
217
- queue.push(getLeftmost(list));
218
- }
219
- queue.sort(compareXYSlope);
220
- for (let i = 0; i < queue.length; i++) outerNode = eliminateHole(queue[i], outerNode);
221
- return outerNode;
222
- }
223
-
224
- function compareXYSlope(a, b) {
225
- let result = a.x - b.x;
226
- if (result === 0) {
227
- result = a.y - b.y;
228
- if (result === 0) {
229
- const aSlope = (a.next.y - a.y) / (a.next.x - a.x);
230
- const bSlope = (b.next.y - b.y) / (b.next.x - b.x);
231
- result = aSlope - bSlope;
232
- }
233
- }
234
- return result;
235
- }
236
-
237
- function eliminateHole(hole, outerNode) {
238
- const bridge = findHoleBridge(hole, outerNode);
239
- if (!bridge) return outerNode;
240
- const bridgeReverse = splitPolygon(bridge, hole);
241
- filterPoints(bridgeReverse, bridgeReverse.next);
242
- return filterPoints(bridge, bridge.next);
243
- }
244
-
245
- function findHoleBridge(hole, outerNode) {
246
- let p = outerNode;
247
- const hx = hole.x, hy = hole.y;
248
- let qx = -Infinity;
249
- let m = null;
250
- if (equals(hole, p)) return p;
251
- do {
252
- if (equals(hole, p.next)) return p.next;
253
- if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
254
- const x = p.x + ((hy - p.y) * (p.next.x - p.x)) / (p.next.y - p.y);
255
- if (x <= hx && x > qx) {
256
- qx = x;
257
- m = p.x < p.next.x ? p : p.next;
258
- if (x === hx) return m;
259
- }
260
- }
261
- p = p.next;
262
- } while (p !== outerNode);
263
- if (!m) return null;
264
- const stop = m;
265
- const mx = m.x, my = m.y;
266
- let tanMin = Infinity;
267
- p = m;
268
- do {
269
- if (hx >= p.x && p.x >= mx && hx !== p.x &&
270
- pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
271
- const tan = Math.abs(hy - p.y) / (hx - p.x);
272
- if (locallyInside(p, hole) &&
273
- (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
274
- m = p;
275
- tanMin = tan;
276
- }
277
- }
278
- p = p.next;
279
- } while (p !== stop);
280
- return m;
281
- }
282
-
283
- function sectorContainsSector(m, p) {
284
- return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
285
- }
286
-
287
- function indexCurve(start, minX, minY, invSize) {
288
- let p = start;
289
- do {
290
- if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
291
- p.prevZ = p.prev;
292
- p.nextZ = p.next;
293
- p = p.next;
294
- } while (p !== start);
295
- p.prevZ.nextZ = null;
296
- p.prevZ = null;
297
- sortLinked(p);
298
- }
299
-
300
- function sortLinked(list) {
301
- let numMerges;
302
- let inSize = 1;
303
- do {
304
- let p = list;
305
- let e;
306
- list = null;
307
- let tail = null;
308
- numMerges = 0;
309
- while (p) {
310
- numMerges++;
311
- let q = p;
312
- let pSize = 0;
313
- for (let i = 0; i < inSize; i++) {
314
- pSize++;
315
- q = q.nextZ;
316
- if (!q) break;
317
- }
318
- let qSize = inSize;
319
- while (pSize > 0 || (qSize > 0 && q)) {
320
- if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
321
- e = p;
322
- p = p.nextZ;
323
- pSize--;
324
- } else {
325
- e = q;
326
- q = q.nextZ;
327
- qSize--;
328
- }
329
- if (tail) tail.nextZ = e;
330
- else list = e;
331
- e.prevZ = tail;
332
- tail = e;
333
- }
334
- p = q;
335
- }
336
- tail.nextZ = null;
337
- inSize *= 2;
338
- } while (numMerges > 1);
339
- return list;
340
- }
341
-
342
- function zOrder(x, y, minX, minY, invSize) {
343
- x = ((x - minX) * invSize) | 0;
344
- y = ((y - minY) * invSize) | 0;
345
- x = (x | (x << 8)) & 0x00ff00ff;
346
- x = (x | (x << 4)) & 0x0f0f0f0f;
347
- x = (x | (x << 2)) & 0x33333333;
348
- x = (x | (x << 1)) & 0x55555555;
349
- y = (y | (y << 8)) & 0x00ff00ff;
350
- y = (y | (y << 4)) & 0x0f0f0f0f;
351
- y = (y | (y << 2)) & 0x33333333;
352
- y = (y | (y << 1)) & 0x55555555;
353
- return x | (y << 1);
354
- }
355
-
356
- function getLeftmost(start) {
357
- let p = start;
358
- let leftmost = start;
359
- do {
360
- if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
361
- p = p.next;
362
- } while (p !== start);
363
- return leftmost;
364
- }
365
-
366
- function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
367
- return (
368
- (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
369
- (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
370
- (bx - px) * (cy - py) >= (cx - px) * (by - py)
371
- );
372
- }
373
-
374
- function pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, px, py) {
375
- return !(ax === px && ay === py) && pointInTriangle(ax, ay, bx, by, cx, cy, px, py);
376
- }
377
-
378
- function isValidDiagonal(a, b) {
379
- return (
380
- a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
381
- ((locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) &&
382
- (area(a.prev, a, b.prev) || area(a, b.prev, b))) ||
383
- (equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0))
384
- );
385
- }
386
-
387
- function area(p, q, r) {
388
- return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
389
- }
390
-
391
- function equals(p1, p2) {
392
- return p1.x === p2.x && p1.y === p2.y;
393
- }
394
-
395
- function intersects(p1, q1, p2, q2) {
396
- const o1 = sign(area(p1, q1, p2));
397
- const o2 = sign(area(p1, q1, q2));
398
- const o3 = sign(area(p2, q2, p1));
399
- const o4 = sign(area(p2, q2, q1));
400
- if (o1 !== o2 && o3 !== o4) return true;
401
- if (o1 === 0 && onSegment(p1, p2, q1)) return true;
402
- if (o2 === 0 && onSegment(p1, q2, q1)) return true;
403
- if (o3 === 0 && onSegment(p2, p1, q2)) return true;
404
- if (o4 === 0 && onSegment(p2, q1, q2)) return true;
405
- return false;
406
- }
407
-
408
- function onSegment(p, q, r) {
409
- return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
410
- }
411
-
412
- function sign(num) {
413
- return num > 0 ? 1 : num < 0 ? -1 : 0;
414
- }
415
-
416
- function intersectsPolygon(a, b) {
417
- let p = a;
418
- do {
419
- if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && intersects(p, p.next, a, b)) return true;
420
- p = p.next;
421
- } while (p !== a);
422
- return false;
423
- }
424
-
425
- function locallyInside(a, b) {
426
- return area(a.prev, a, a.next) < 0
427
- ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0
428
- : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
429
- }
430
-
431
- function middleInside(a, b) {
432
- let p = a;
433
- let inside = false;
434
- const px = (a.x + b.x) / 2, py = (a.y + b.y) / 2;
435
- do {
436
- if (p.y > py !== p.next.y > py && p.next.y !== p.y && px < ((p.next.x - p.x) * (py - p.y)) / (p.next.y - p.y) + p.x) inside = !inside;
437
- p = p.next;
438
- } while (p !== a);
439
- return inside;
440
- }
441
-
442
- function splitPolygon(a, b) {
443
- const a2 = new Node(a.i, a.x, a.y);
444
- const b2 = new Node(b.i, b.x, b.y);
445
- const an = a.next;
446
- const bp = b.prev;
447
- a.next = b;
448
- b.prev = a;
449
- a2.next = an;
450
- an.prev = a2;
451
- b2.next = a2;
452
- a2.prev = b2;
453
- bp.next = b2;
454
- b2.prev = bp;
455
- return b2;
456
- }
457
-
458
- function insertNode(i, x, y, last) {
459
- const p = new Node(i, x, y);
460
- if (!last) {
461
- p.prev = p;
462
- p.next = p;
463
- } else {
464
- p.next = last.next;
465
- p.prev = last;
466
- last.next.prev = p;
467
- last.next = p;
468
- }
469
- return p;
470
- }
471
-
472
- function removeNode(p) {
473
- p.next.prev = p.prev;
474
- p.prev.next = p.next;
475
- if (p.prevZ) p.prevZ.nextZ = p.nextZ;
476
- if (p.nextZ) p.nextZ.prevZ = p.prevZ;
477
- }
478
-
479
- function signedArea(data, start, end, dim) {
480
- let sum = 0;
481
- for (let i = start, j = end - dim; i < end; i += dim) {
482
- sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
483
- j = i;
484
- }
485
- return sum;
486
- }
487
-
488
- /**
489
- * Relative difference between the triangulated area and the polygon area.
490
- * Large values indicate self-intersecting or otherwise malformed input.
491
- */
492
- export function triangulationError(data, holeIndices, triangles) {
493
- const polyArea = (start, end) => {
494
- let s = 0;
495
- for (let i = start, j = end - 2; i < end; i += 2) {
496
- s += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
497
- j = i;
498
- }
499
- return Math.abs(s / 2);
500
- };
501
- const outerEnd = holeIndices && holeIndices.length ? holeIndices[0] * 2 : data.length;
502
- let expected = polyArea(0, outerEnd);
503
- if (holeIndices) {
504
- for (let i = 0; i < holeIndices.length; i++) {
505
- const s = holeIndices[i] * 2;
506
- const e = i < holeIndices.length - 1 ? holeIndices[i + 1] * 2 : data.length;
507
- expected -= polyArea(s, e);
508
- }
509
- }
510
- let actual = 0;
511
- for (let i = 0; i < triangles.length; i += 3) {
512
- const a = triangles[i] * 2, b = triangles[i + 1] * 2, c = triangles[i + 2] * 2;
513
- actual += Math.abs((data[a] - data[c]) * (data[b + 1] - data[a + 1]) - (data[a] - data[b]) * (data[c + 1] - data[a + 1])) / 2;
514
- }
515
- if (expected === 0 && actual === 0) return 0;
516
- return Math.abs(expected - actual) / Math.max(Math.abs(expected), 1e-12);
517
- }
1
+ // Polygon triangulation with holes.
2
+ //
3
+ // Ear clipping on a doubly linked vertex list with hole elimination by
4
+ // bridging (David Eberly's method), z-order curve acceleration for large
5
+ // polygons and the classic fallbacks for degenerate input (curing local
6
+ // self-intersections, splitting the polygon). The algorithm follows the
7
+ // structure popularised by Mapbox "earcut" (ISC license, Vladimir Agafonkin).
8
+ //
9
+ // Input: flat coordinate array [x0,y0,x1,y1,...] and hole start indices (in
10
+ // vertices). Output: triangle vertex indices, counter-clockwise (y up).
11
+
12
+ import { triangulateWasm } from '../wasm/kernels.js';
13
+
14
+ export function triangulate(data, holeIndices = null) {
15
+ const w = triangulateWasm(data, holeIndices);
16
+ if (w) return w;
17
+ const dim = 2;
18
+ const hasHoles = holeIndices && holeIndices.length;
19
+ const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
20
+ let outerNode = linkedList(data, 0, outerLen, dim, true);
21
+ const triangles = [];
22
+ if (!outerNode || outerNode.next === outerNode.prev) return triangles;
23
+
24
+ if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
25
+
26
+ let minX = 0, minY = 0, invSize = 0;
27
+ if (data.length > 80 * dim) {
28
+ minX = Infinity;
29
+ minY = Infinity;
30
+ let maxX = -Infinity, maxY = -Infinity;
31
+ for (let i = 0; i < outerLen; i += dim) {
32
+ const x = data[i], y = data[i + 1];
33
+ if (x < minX) minX = x;
34
+ if (y < minY) minY = y;
35
+ if (x > maxX) maxX = x;
36
+ if (y > maxY) maxY = y;
37
+ }
38
+ invSize = Math.max(maxX - minX, maxY - minY);
39
+ invSize = invSize !== 0 ? 32767 / invSize : 0;
40
+ }
41
+ earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
42
+ return triangles;
43
+ }
44
+
45
+ class Node {
46
+ constructor(i, x, y) {
47
+ this.i = i;
48
+ this.x = x;
49
+ this.y = y;
50
+ this.prev = null;
51
+ this.next = null;
52
+ this.z = 0;
53
+ this.prevZ = null;
54
+ this.nextZ = null;
55
+ this.steiner = false;
56
+ }
57
+ }
58
+
59
+ // keeps the class's hidden class alive between triangulations (see MeshBuilder.shapeKeeper);
60
+ // fractional coordinates, so its fields have the representation real nodes use
61
+ Node.shapeKeeper = new Node(0, 0.5, 0.5);
62
+
63
+ function linkedList(data, start, end, dim, clockwise) {
64
+ let last = null;
65
+ if (clockwise === signedArea(data, start, end, dim) > 0) {
66
+ for (let i = start; i < end; i += dim) last = insertNode(i / dim, data[i], data[i + 1], last);
67
+ } else {
68
+ for (let i = end - dim; i >= start; i -= dim) last = insertNode(i / dim, data[i], data[i + 1], last);
69
+ }
70
+ if (last && equals(last, last.next)) {
71
+ removeNode(last);
72
+ last = last.next;
73
+ }
74
+ return last;
75
+ }
76
+
77
+ function filterPoints(start, end) {
78
+ if (!start) return start;
79
+ if (!end) end = start;
80
+ let p = start;
81
+ let again;
82
+ do {
83
+ again = false;
84
+ if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
85
+ removeNode(p);
86
+ p = end = p.prev;
87
+ if (p === p.next) break;
88
+ again = true;
89
+ } else {
90
+ p = p.next;
91
+ }
92
+ } while (again || p !== end);
93
+ return end;
94
+ }
95
+
96
+ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
97
+ if (!ear) return;
98
+ if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
99
+ let stop = ear;
100
+ while (ear.prev !== ear.next) {
101
+ const prev = ear.prev;
102
+ const next = ear.next;
103
+ if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
104
+ triangles.push(prev.i, ear.i, next.i);
105
+ removeNode(ear);
106
+ // skipping the next vertex leads to fewer sliver triangles
107
+ ear = next.next;
108
+ stop = next.next;
109
+ continue;
110
+ }
111
+ ear = next;
112
+ if (ear === stop) {
113
+ if (!pass) {
114
+ earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
115
+ } else if (pass === 1) {
116
+ ear = cureLocalIntersections(filterPoints(ear), triangles);
117
+ earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
118
+ } else if (pass === 2) {
119
+ splitEarcut(ear, triangles, dim, minX, minY, invSize);
120
+ }
121
+ break;
122
+ }
123
+ }
124
+ }
125
+
126
+ function isEar(ear) {
127
+ const a = ear.prev, b = ear, c = ear.next;
128
+ if (area(a, b, c) >= 0) return false; // reflex
129
+ const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
130
+ const x0 = Math.min(ax, bx, cx), y0 = Math.min(ay, by, cy);
131
+ const x1 = Math.max(ax, bx, cx), y1 = Math.max(ay, by, cy);
132
+ let p = c.next;
133
+ while (p !== a) {
134
+ if (
135
+ p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
136
+ pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) &&
137
+ area(p.prev, p, p.next) >= 0
138
+ ) return false;
139
+ p = p.next;
140
+ }
141
+ return true;
142
+ }
143
+
144
+ function isEarHashed(ear, minX, minY, invSize) {
145
+ const a = ear.prev, b = ear, c = ear.next;
146
+ if (area(a, b, c) >= 0) return false;
147
+ const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
148
+ const x0 = Math.min(ax, bx, cx), y0 = Math.min(ay, by, cy);
149
+ const x1 = Math.max(ax, bx, cx), y1 = Math.max(ay, by, cy);
150
+ const minZ = zOrder(x0, y0, minX, minY, invSize);
151
+ const maxZ = zOrder(x1, y1, minX, minY, invSize);
152
+ let p = ear.prevZ;
153
+ let n = ear.nextZ;
154
+ while (p && p.z >= minZ && n && n.z <= maxZ) {
155
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
156
+ pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
157
+ p = p.prevZ;
158
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
159
+ pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
160
+ n = n.nextZ;
161
+ }
162
+ while (p && p.z >= minZ) {
163
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
164
+ pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
165
+ p = p.prevZ;
166
+ }
167
+ while (n && n.z <= maxZ) {
168
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
169
+ pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
170
+ n = n.nextZ;
171
+ }
172
+ return true;
173
+ }
174
+
175
+ function cureLocalIntersections(start, triangles) {
176
+ let p = start;
177
+ do {
178
+ const a = p.prev, b = p.next.next;
179
+ if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
180
+ triangles.push(a.i, p.i, b.i);
181
+ removeNode(p);
182
+ removeNode(p.next);
183
+ p = start = b;
184
+ }
185
+ p = p.next;
186
+ } while (p !== start);
187
+ return filterPoints(p);
188
+ }
189
+
190
+ function splitEarcut(start, triangles, dim, minX, minY, invSize) {
191
+ let a = start;
192
+ do {
193
+ let b = a.next.next;
194
+ while (b !== a.prev) {
195
+ if (a.i !== b.i && isValidDiagonal(a, b)) {
196
+ let c = splitPolygon(a, b);
197
+ a = filterPoints(a, a.next);
198
+ c = filterPoints(c, c.next);
199
+ earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
200
+ earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
201
+ return;
202
+ }
203
+ b = b.next;
204
+ }
205
+ a = a.next;
206
+ } while (a !== start);
207
+ }
208
+
209
+ function eliminateHoles(data, holeIndices, outerNode, dim) {
210
+ const queue = [];
211
+ for (let i = 0, len = holeIndices.length; i < len; i++) {
212
+ const start = holeIndices[i] * dim;
213
+ const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
214
+ const list = linkedList(data, start, end, dim, false);
215
+ if (!list) continue;
216
+ if (list === list.next) list.steiner = true;
217
+ queue.push(getLeftmost(list));
218
+ }
219
+ queue.sort(compareXYSlope);
220
+ for (let i = 0; i < queue.length; i++) outerNode = eliminateHole(queue[i], outerNode);
221
+ return outerNode;
222
+ }
223
+
224
+ function compareXYSlope(a, b) {
225
+ let result = a.x - b.x;
226
+ if (result === 0) {
227
+ result = a.y - b.y;
228
+ if (result === 0) {
229
+ const aSlope = (a.next.y - a.y) / (a.next.x - a.x);
230
+ const bSlope = (b.next.y - b.y) / (b.next.x - b.x);
231
+ result = aSlope - bSlope;
232
+ }
233
+ }
234
+ return result;
235
+ }
236
+
237
+ function eliminateHole(hole, outerNode) {
238
+ const bridge = findHoleBridge(hole, outerNode);
239
+ if (!bridge) return outerNode;
240
+ const bridgeReverse = splitPolygon(bridge, hole);
241
+ filterPoints(bridgeReverse, bridgeReverse.next);
242
+ return filterPoints(bridge, bridge.next);
243
+ }
244
+
245
+ function findHoleBridge(hole, outerNode) {
246
+ let p = outerNode;
247
+ const hx = hole.x, hy = hole.y;
248
+ let qx = -Infinity;
249
+ let m = null;
250
+ if (equals(hole, p)) return p;
251
+ do {
252
+ if (equals(hole, p.next)) return p.next;
253
+ if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
254
+ const x = p.x + ((hy - p.y) * (p.next.x - p.x)) / (p.next.y - p.y);
255
+ if (x <= hx && x > qx) {
256
+ qx = x;
257
+ m = p.x < p.next.x ? p : p.next;
258
+ if (x === hx) return m;
259
+ }
260
+ }
261
+ p = p.next;
262
+ } while (p !== outerNode);
263
+ if (!m) return null;
264
+ const stop = m;
265
+ const mx = m.x, my = m.y;
266
+ let tanMin = Infinity;
267
+ p = m;
268
+ do {
269
+ if (hx >= p.x && p.x >= mx && hx !== p.x &&
270
+ pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
271
+ const tan = Math.abs(hy - p.y) / (hx - p.x);
272
+ if (locallyInside(p, hole) &&
273
+ (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
274
+ m = p;
275
+ tanMin = tan;
276
+ }
277
+ }
278
+ p = p.next;
279
+ } while (p !== stop);
280
+ return m;
281
+ }
282
+
283
+ function sectorContainsSector(m, p) {
284
+ return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
285
+ }
286
+
287
+ function indexCurve(start, minX, minY, invSize) {
288
+ let p = start;
289
+ do {
290
+ if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
291
+ p.prevZ = p.prev;
292
+ p.nextZ = p.next;
293
+ p = p.next;
294
+ } while (p !== start);
295
+ p.prevZ.nextZ = null;
296
+ p.prevZ = null;
297
+ sortLinked(p);
298
+ }
299
+
300
+ function sortLinked(list) {
301
+ let numMerges;
302
+ let inSize = 1;
303
+ do {
304
+ let p = list;
305
+ let e;
306
+ list = null;
307
+ let tail = null;
308
+ numMerges = 0;
309
+ while (p) {
310
+ numMerges++;
311
+ let q = p;
312
+ let pSize = 0;
313
+ for (let i = 0; i < inSize; i++) {
314
+ pSize++;
315
+ q = q.nextZ;
316
+ if (!q) break;
317
+ }
318
+ let qSize = inSize;
319
+ while (pSize > 0 || (qSize > 0 && q)) {
320
+ if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
321
+ e = p;
322
+ p = p.nextZ;
323
+ pSize--;
324
+ } else {
325
+ e = q;
326
+ q = q.nextZ;
327
+ qSize--;
328
+ }
329
+ if (tail) tail.nextZ = e;
330
+ else list = e;
331
+ e.prevZ = tail;
332
+ tail = e;
333
+ }
334
+ p = q;
335
+ }
336
+ tail.nextZ = null;
337
+ inSize *= 2;
338
+ } while (numMerges > 1);
339
+ return list;
340
+ }
341
+
342
+ function zOrder(x, y, minX, minY, invSize) {
343
+ x = ((x - minX) * invSize) | 0;
344
+ y = ((y - minY) * invSize) | 0;
345
+ x = (x | (x << 8)) & 0x00ff00ff;
346
+ x = (x | (x << 4)) & 0x0f0f0f0f;
347
+ x = (x | (x << 2)) & 0x33333333;
348
+ x = (x | (x << 1)) & 0x55555555;
349
+ y = (y | (y << 8)) & 0x00ff00ff;
350
+ y = (y | (y << 4)) & 0x0f0f0f0f;
351
+ y = (y | (y << 2)) & 0x33333333;
352
+ y = (y | (y << 1)) & 0x55555555;
353
+ return x | (y << 1);
354
+ }
355
+
356
+ function getLeftmost(start) {
357
+ let p = start;
358
+ let leftmost = start;
359
+ do {
360
+ if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
361
+ p = p.next;
362
+ } while (p !== start);
363
+ return leftmost;
364
+ }
365
+
366
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
367
+ return (
368
+ (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
369
+ (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
370
+ (bx - px) * (cy - py) >= (cx - px) * (by - py)
371
+ );
372
+ }
373
+
374
+ function pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, px, py) {
375
+ return !(ax === px && ay === py) && pointInTriangle(ax, ay, bx, by, cx, cy, px, py);
376
+ }
377
+
378
+ function isValidDiagonal(a, b) {
379
+ return (
380
+ a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
381
+ ((locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) &&
382
+ (area(a.prev, a, b.prev) || area(a, b.prev, b))) ||
383
+ (equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0))
384
+ );
385
+ }
386
+
387
+ function area(p, q, r) {
388
+ return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
389
+ }
390
+
391
+ function equals(p1, p2) {
392
+ return p1.x === p2.x && p1.y === p2.y;
393
+ }
394
+
395
+ function intersects(p1, q1, p2, q2) {
396
+ const o1 = sign(area(p1, q1, p2));
397
+ const o2 = sign(area(p1, q1, q2));
398
+ const o3 = sign(area(p2, q2, p1));
399
+ const o4 = sign(area(p2, q2, q1));
400
+ if (o1 !== o2 && o3 !== o4) return true;
401
+ if (o1 === 0 && onSegment(p1, p2, q1)) return true;
402
+ if (o2 === 0 && onSegment(p1, q2, q1)) return true;
403
+ if (o3 === 0 && onSegment(p2, p1, q2)) return true;
404
+ if (o4 === 0 && onSegment(p2, q1, q2)) return true;
405
+ return false;
406
+ }
407
+
408
+ function onSegment(p, q, r) {
409
+ return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
410
+ }
411
+
412
+ function sign(num) {
413
+ return num > 0 ? 1 : num < 0 ? -1 : 0;
414
+ }
415
+
416
+ function intersectsPolygon(a, b) {
417
+ let p = a;
418
+ do {
419
+ if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && intersects(p, p.next, a, b)) return true;
420
+ p = p.next;
421
+ } while (p !== a);
422
+ return false;
423
+ }
424
+
425
+ function locallyInside(a, b) {
426
+ return area(a.prev, a, a.next) < 0
427
+ ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0
428
+ : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
429
+ }
430
+
431
+ function middleInside(a, b) {
432
+ let p = a;
433
+ let inside = false;
434
+ const px = (a.x + b.x) / 2, py = (a.y + b.y) / 2;
435
+ do {
436
+ if (p.y > py !== p.next.y > py && p.next.y !== p.y && px < ((p.next.x - p.x) * (py - p.y)) / (p.next.y - p.y) + p.x) inside = !inside;
437
+ p = p.next;
438
+ } while (p !== a);
439
+ return inside;
440
+ }
441
+
442
+ function splitPolygon(a, b) {
443
+ const a2 = new Node(a.i, a.x, a.y);
444
+ const b2 = new Node(b.i, b.x, b.y);
445
+ const an = a.next;
446
+ const bp = b.prev;
447
+ a.next = b;
448
+ b.prev = a;
449
+ a2.next = an;
450
+ an.prev = a2;
451
+ b2.next = a2;
452
+ a2.prev = b2;
453
+ bp.next = b2;
454
+ b2.prev = bp;
455
+ return b2;
456
+ }
457
+
458
+ function insertNode(i, x, y, last) {
459
+ const p = new Node(i, x, y);
460
+ if (!last) {
461
+ p.prev = p;
462
+ p.next = p;
463
+ } else {
464
+ p.next = last.next;
465
+ p.prev = last;
466
+ last.next.prev = p;
467
+ last.next = p;
468
+ }
469
+ return p;
470
+ }
471
+
472
+ function removeNode(p) {
473
+ p.next.prev = p.prev;
474
+ p.prev.next = p.next;
475
+ if (p.prevZ) p.prevZ.nextZ = p.nextZ;
476
+ if (p.nextZ) p.nextZ.prevZ = p.prevZ;
477
+ }
478
+
479
+ function signedArea(data, start, end, dim) {
480
+ let sum = 0;
481
+ for (let i = start, j = end - dim; i < end; i += dim) {
482
+ sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
483
+ j = i;
484
+ }
485
+ return sum;
486
+ }
487
+
488
+ /**
489
+ * Relative difference between the triangulated area and the polygon area.
490
+ * Large values indicate self-intersecting or otherwise malformed input.
491
+ */
492
+ export function triangulationError(data, holeIndices, triangles) {
493
+ const polyArea = (start, end) => {
494
+ let s = 0;
495
+ for (let i = start, j = end - 2; i < end; i += 2) {
496
+ s += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
497
+ j = i;
498
+ }
499
+ return Math.abs(s / 2);
500
+ };
501
+ const outerEnd = holeIndices && holeIndices.length ? holeIndices[0] * 2 : data.length;
502
+ let expected = polyArea(0, outerEnd);
503
+ if (holeIndices) {
504
+ for (let i = 0; i < holeIndices.length; i++) {
505
+ const s = holeIndices[i] * 2;
506
+ const e = i < holeIndices.length - 1 ? holeIndices[i + 1] * 2 : data.length;
507
+ expected -= polyArea(s, e);
508
+ }
509
+ }
510
+ let actual = 0;
511
+ for (let i = 0; i < triangles.length; i += 3) {
512
+ const a = triangles[i] * 2, b = triangles[i + 1] * 2, c = triangles[i + 2] * 2;
513
+ actual += Math.abs((data[a] - data[c]) * (data[b + 1] - data[a + 1]) - (data[a] - data[b]) * (data[c + 1] - data[a + 1])) / 2;
514
+ }
515
+ if (expected === 0 && actual === 0) return 0;
516
+ return Math.abs(expected - actual) / Math.max(Math.abs(expected), 1e-12);
517
+ }