@es3d/es3d 0.0.0-stage → 1.0.0
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/LICENSE +201 -0
- package/README.md +86 -3
- package/docs/REFERENCE.md +581 -0
- package/docs/SPEC.md +417 -0
- package/package.json +50 -4
- package/scripts/es3d-cli.mjs +377 -0
- package/src/cache/geometry-store.js +392 -0
- package/src/cache/mesh-bundle.js +123 -0
- package/src/cache/sha256.js +73 -0
- package/src/format/ast.js +238 -0
- package/src/format/colors.js +93 -0
- package/src/format/compact-parser.js +171 -0
- package/src/format/compact-tokenizer.js +165 -0
- package/src/format/errors.js +247 -0
- package/src/format/json.js +55 -0
- package/src/format/limits.js +14 -0
- package/src/format/parse.js +92 -0
- package/src/format/schema.js +617 -0
- package/src/format/serializer.js +333 -0
- package/src/format/validator.js +330 -0
- package/src/format/value-error.js +12 -0
- package/src/format/value-format.js +314 -0
- package/src/format/values.js +459 -0
- package/src/format/xml-parser.js +136 -0
- package/src/format/xml-tokenizer.js +237 -0
- package/src/geometry/contour.js +86 -0
- package/src/geometry/extrude.js +175 -0
- package/src/geometry/gear.js +85 -0
- package/src/geometry/geometry-cache.js +159 -0
- package/src/geometry/lod-error.js +178 -0
- package/src/geometry/mesh.js +458 -0
- package/src/geometry/primitives.js +299 -0
- package/src/geometry/registry.js +645 -0
- package/src/geometry/revolve.js +136 -0
- package/src/geometry/sweep.js +441 -0
- package/src/index.js +47 -0
- package/src/io/compression.js +67 -0
- package/src/io/convert.js +236 -0
- package/src/io/download.js +13 -0
- package/src/io/external-scene.js +127 -0
- package/src/io/file-loader.js +28 -0
- package/src/io/gltf.js +630 -0
- package/src/io/mesh-import.js +104 -0
- package/src/io/obj.js +352 -0
- package/src/io/step-brep.js +439 -0
- package/src/io/step-exact.js +1149 -0
- package/src/io/step.js +1418 -0
- package/src/io/stl.js +193 -0
- package/src/io/svg-import.js +787 -0
- package/src/material/materials.js +154 -0
- package/src/math/mat4.js +475 -0
- package/src/math/random.js +30 -0
- package/src/math/trig.js +65 -0
- package/src/math/vec3.js +81 -0
- package/src/optimize/optimizer.js +579 -0
- package/src/path/path-flatten.js +322 -0
- package/src/path/path-parser.js +332 -0
- package/src/path/shapes.js +129 -0
- package/src/path/triangulate.js +517 -0
- package/src/pipeline.js +74 -0
- package/src/render/camera.js +168 -0
- package/src/render/frame-builder.js +362 -0
- package/src/render/geometry-pool.js +149 -0
- package/src/render/lighting.js +149 -0
- package/src/render/orbit-controls.js +237 -0
- package/src/render/renderer.js +249 -0
- package/src/render/scroll-timeline.js +104 -0
- package/src/render/shaders/glsl.js +285 -0
- package/src/render/shaders/wgsl.js +252 -0
- package/src/render/stage.js +352 -0
- package/src/render/texture-cache.js +40 -0
- package/src/render/textures.js +223 -0
- package/src/render/webgl2/webgl2-backend.js +532 -0
- package/src/render/webgpu/webgpu-backend.js +480 -0
- package/src/scene/animation.js +171 -0
- package/src/scene/camera-constraints.js +117 -0
- package/src/scene/flatten.js +754 -0
- package/src/scene/picking.js +161 -0
- package/src/scene/scene-graph.js +320 -0
- package/src/transform/transform.js +211 -0
- package/src/wasm/kernels-binary.js +3 -0
- package/src/wasm/kernels.js +385 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Contours -> fill regions (polygons with holes) following SVG fill rules.
|
|
2
|
+
//
|
|
3
|
+
// Closed contours are organised into a containment tree. A contour's interior
|
|
4
|
+
// (minus its children) is filled when its nesting depth is even ("evenodd") or
|
|
5
|
+
// when the accumulated winding number is non-zero ("nonzero", SVG default).
|
|
6
|
+
// Side walls for extrusions/sweeps exist exactly where the fill state changes
|
|
7
|
+
// across a contour; walls are oriented with the filled side on the left so the
|
|
8
|
+
// right-hand perpendicular of each edge is the outward normal.
|
|
9
|
+
|
|
10
|
+
import { signedArea, reverseContour } from './path-flatten.js';
|
|
11
|
+
import { triangulate, triangulationError } from './triangulate.js';
|
|
12
|
+
|
|
13
|
+
function pointInPolygon(points, x, y) {
|
|
14
|
+
let inside = false;
|
|
15
|
+
const n = points.length / 2;
|
|
16
|
+
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
17
|
+
const xi = points[i * 2], yi = points[i * 2 + 1];
|
|
18
|
+
const xj = points[j * 2], yj = points[j * 2 + 1];
|
|
19
|
+
if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside;
|
|
20
|
+
}
|
|
21
|
+
return inside;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function bbox(points) {
|
|
25
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
26
|
+
for (let i = 0; i < points.length; i += 2) {
|
|
27
|
+
const x = points[i], y = points[i + 1];
|
|
28
|
+
if (x < minX) minX = x;
|
|
29
|
+
if (y < minY) minY = y;
|
|
30
|
+
if (x > maxX) maxX = x;
|
|
31
|
+
if (y > maxY) maxY = y;
|
|
32
|
+
}
|
|
33
|
+
return { minX, minY, maxX, maxY };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A sample point strictly inside the contour near its first vertex (robust against shared vertices). */
|
|
37
|
+
function samplePoint(points) {
|
|
38
|
+
const n = points.length / 2;
|
|
39
|
+
// centroid of a small triangle at a convex-ish vertex works for most inputs;
|
|
40
|
+
// fall back to the first vertex.
|
|
41
|
+
for (let k = 0; k < Math.min(n, 8); k++) {
|
|
42
|
+
const i0 = ((k - 1 + n) % n) * 2, i1 = k * 2, i2 = ((k + 1) % n) * 2;
|
|
43
|
+
const cx = (points[i0] + points[i1] + points[i2]) / 3;
|
|
44
|
+
const cy = (points[i0 + 1] + points[i1 + 1] + points[i2 + 1]) / 3;
|
|
45
|
+
if (pointInPolygon(points, cx, cy)) return [cx, cy];
|
|
46
|
+
}
|
|
47
|
+
return [points[0], points[1]];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {{points: Float64Array, smooth: Uint8Array}[]} contours 2D contours (treated as closed)
|
|
52
|
+
* @param {'nonzero'|'evenodd'} fillRule
|
|
53
|
+
*/
|
|
54
|
+
export function buildRegions(contours, fillRule = 'nonzero') {
|
|
55
|
+
const items = [];
|
|
56
|
+
for (const c of contours) {
|
|
57
|
+
if (c.points.length < 6) continue;
|
|
58
|
+
const a = signedArea(c.points);
|
|
59
|
+
if (Math.abs(a) < 1e-14) continue;
|
|
60
|
+
items.push({ c, area: a, abs: Math.abs(a), box: bbox(c.points), parent: -1, depth: 0, winding: 0, children: [] });
|
|
61
|
+
}
|
|
62
|
+
items.sort((p, q) => q.abs - p.abs);
|
|
63
|
+
for (let i = 0; i < items.length; i++) {
|
|
64
|
+
const it = items[i];
|
|
65
|
+
const [sx, sy] = samplePoint(it.c.points);
|
|
66
|
+
for (let j = i - 1; j >= 0; j--) {
|
|
67
|
+
const cand = items[j];
|
|
68
|
+
const b = cand.box;
|
|
69
|
+
if (sx < b.minX || sx > b.maxX || sy < b.minY || sy > b.maxY) continue;
|
|
70
|
+
if (pointInPolygon(cand.c.points, sx, sy)) {
|
|
71
|
+
it.parent = j;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const sgn = it.area > 0 ? 1 : -1;
|
|
76
|
+
if (it.parent >= 0) {
|
|
77
|
+
const p = items[it.parent];
|
|
78
|
+
it.depth = p.depth + 1;
|
|
79
|
+
it.winding = p.winding + sgn;
|
|
80
|
+
p.children.push(i);
|
|
81
|
+
} else {
|
|
82
|
+
it.depth = 0;
|
|
83
|
+
it.winding = sgn;
|
|
84
|
+
}
|
|
85
|
+
it.filled = fillRule === 'evenodd' ? it.depth % 2 === 0 : it.winding !== 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A contour is a wall where the fill state differs on its two sides.
|
|
89
|
+
for (const it of items) it.isWall = it.filled !== (it.parent >= 0 ? items[it.parent].filled : false);
|
|
90
|
+
const oriented = (it, wantCcw) => {
|
|
91
|
+
const c = (signedArea(it.c.points) > 0) === wantCcw ? it.c : reverseContour(it.c);
|
|
92
|
+
return { ...c, isWall: it.isWall };
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const regions = [];
|
|
96
|
+
const walls = [];
|
|
97
|
+
for (let i = 0; i < items.length; i++) {
|
|
98
|
+
const it = items[i];
|
|
99
|
+
if (it.filled) {
|
|
100
|
+
regions.push({ outer: oriented(it, true), holes: it.children.map((k) => oriented(items[k], false)) });
|
|
101
|
+
}
|
|
102
|
+
if (it.isWall) walls.push(oriented(it, it.filled));
|
|
103
|
+
}
|
|
104
|
+
return { regions, walls };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Triangulate a region; returns {positions: Float64Array (2D), smooth, indices, error}.
|
|
109
|
+
* `error` is the relative area mismatch (large = self-intersecting input).
|
|
110
|
+
*/
|
|
111
|
+
export function triangulateRegion(region) {
|
|
112
|
+
let total = region.outer.points.length;
|
|
113
|
+
for (const h of region.holes) total += h.points.length;
|
|
114
|
+
const data = new Float64Array(total);
|
|
115
|
+
const smooth = new Uint8Array(total / 2);
|
|
116
|
+
data.set(region.outer.points, 0);
|
|
117
|
+
smooth.set(region.outer.smooth, 0);
|
|
118
|
+
const holeIndices = [];
|
|
119
|
+
let off = region.outer.points.length;
|
|
120
|
+
for (const h of region.holes) {
|
|
121
|
+
holeIndices.push(off / 2);
|
|
122
|
+
data.set(h.points, off);
|
|
123
|
+
smooth.set(h.smooth, off / 2);
|
|
124
|
+
off += h.points.length;
|
|
125
|
+
}
|
|
126
|
+
const indices = triangulate(data, holeIndices.length ? holeIndices : null);
|
|
127
|
+
const error = triangulationError(data, holeIndices.length ? holeIndices : null, indices);
|
|
128
|
+
return { positions: data, smooth, indices, holeIndices, error };
|
|
129
|
+
}
|
|
@@ -0,0 +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
|
+
}
|