@es3d/es3d 1.0.0 → 1.0.2
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 -201
- package/README.md +95 -86
- package/docs/SPEC.md +426 -417
- package/package.json +1 -1
- package/scripts/es3d-cli.mjs +390 -377
- package/src/cache/geometry-store.js +392 -392
- package/src/cache/mesh-bundle.js +123 -123
- package/src/cache/sha256.js +73 -73
- package/src/format/ast.js +238 -238
- package/src/format/colors.js +93 -93
- package/src/format/compact-parser.js +171 -171
- package/src/format/compact-tokenizer.js +165 -165
- package/src/format/errors.js +247 -247
- package/src/format/json.js +55 -55
- package/src/format/limits.js +14 -14
- package/src/format/parse.js +132 -92
- package/src/format/schema.js +617 -617
- package/src/format/serializer.js +333 -333
- package/src/format/validator.js +330 -330
- package/src/format/value-error.js +12 -12
- package/src/format/value-format.js +314 -314
- package/src/format/values.js +459 -459
- package/src/format/xml-parser.js +136 -136
- package/src/format/xml-tokenizer.js +237 -237
- package/src/geometry/contour.js +86 -86
- package/src/geometry/extrude.js +175 -175
- package/src/geometry/gear.js +85 -85
- package/src/geometry/geometry-cache.js +159 -159
- package/src/geometry/lod-error.js +178 -178
- package/src/geometry/mesh.js +458 -458
- package/src/geometry/primitives.js +299 -299
- package/src/geometry/registry.js +645 -645
- package/src/geometry/revolve.js +136 -136
- package/src/geometry/sweep.js +441 -441
- package/src/index.js +47 -47
- package/src/io/compression.js +67 -67
- package/src/io/convert.js +236 -236
- package/src/io/download.js +13 -13
- package/src/io/external-scene.js +127 -127
- package/src/io/file-loader.js +28 -28
- package/src/io/gltf-pack.js +1461 -0
- package/src/io/gltf.js +630 -630
- package/src/io/mesh-import.js +104 -104
- package/src/io/obj.js +352 -352
- package/src/io/step-brep.js +439 -439
- package/src/io/step-exact.js +1149 -1149
- package/src/io/step.js +1418 -1418
- package/src/io/stl.js +193 -193
- package/src/io/svg-import.js +787 -787
- package/src/material/materials.js +154 -154
- package/src/math/mat4.js +475 -475
- package/src/math/random.js +30 -30
- package/src/math/trig.js +65 -65
- package/src/math/vec3.js +81 -81
- package/src/optimize/optimizer.js +604 -579
- package/src/path/path-flatten.js +322 -322
- package/src/path/path-parser.js +332 -332
- package/src/path/shapes.js +129 -129
- package/src/path/triangulate.js +517 -517
- package/src/pipeline.js +74 -74
- package/src/render/camera.js +168 -168
- package/src/render/frame-builder.js +362 -362
- package/src/render/geometry-pool.js +149 -149
- package/src/render/lighting.js +149 -149
- package/src/render/orbit-controls.js +237 -237
- package/src/render/renderer.js +258 -249
- package/src/render/scroll-timeline.js +104 -104
- package/src/render/shaders/glsl.js +285 -285
- package/src/render/shaders/wgsl.js +252 -252
- package/src/render/stage.js +365 -352
- package/src/render/texture-cache.js +40 -40
- package/src/render/textures.js +223 -223
- package/src/render/webgl2/webgl2-backend.js +586 -532
- package/src/render/webgpu/webgpu-backend.js +482 -480
- package/src/scene/flatten.js +754 -754
- package/src/scene/picking.js +161 -161
- package/src/scene/scene-graph.js +320 -320
- package/src/transform/transform.js +211 -211
- package/src/wasm/kernels-binary.js +3 -3
- package/src/wasm/kernels.js +385 -385
package/src/path/path-flatten.js
CHANGED
|
@@ -1,322 +1,322 @@
|
|
|
1
|
-
// Flattening of path segments into polylines ("contours").
|
|
2
|
-
//
|
|
3
|
-
// Curves are subdivided adaptively from a geometric tolerance (Wang's formula
|
|
4
|
-
// for Béziers, chord error for arcs), so the same path produces coarser or
|
|
5
|
-
// finer polylines for different levels of detail. Every output point carries a
|
|
6
|
-
// `smooth` flag: true for curve interiors and tangent-continuous joints, false
|
|
7
|
-
// for corners. Geometry generators use the flags to decide where normals must
|
|
8
|
-
// be split (sharp edges) and where they are shared (smooth surfaces).
|
|
9
|
-
|
|
10
|
-
const SMOOTH_JOINT_COS = Math.cos((8 * Math.PI) / 180);
|
|
11
|
-
const MAX_SEGMENTS = 256;
|
|
12
|
-
|
|
13
|
-
function norm2(x, y) {
|
|
14
|
-
const l = Math.hypot(x, y);
|
|
15
|
-
return l > 1e-12 ? [x / l, y / l, 0] : null;
|
|
16
|
-
}
|
|
17
|
-
function norm3(x, y, z) {
|
|
18
|
-
const l = Math.hypot(x, y, z);
|
|
19
|
-
return l > 1e-12 ? [x / l, y / l, z / l] : null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
class ContourBuilder {
|
|
23
|
-
constructor(dim) {
|
|
24
|
-
this.dim = dim;
|
|
25
|
-
this.pts = [];
|
|
26
|
-
this.smooth = [];
|
|
27
|
-
this.firstTan = null;
|
|
28
|
-
this.lastTan = null;
|
|
29
|
-
this.closed = false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get count() {
|
|
33
|
-
return this.smooth.length;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
lastPoint() {
|
|
37
|
-
const d = this.dim;
|
|
38
|
-
const p = this.pts;
|
|
39
|
-
const k = p.length - d;
|
|
40
|
-
return d === 3 ? [p[k], p[k + 1], p[k + 2]] : [p[k], p[k + 1], 0];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
start(x, y, z) {
|
|
44
|
-
this.pts.push(x, y);
|
|
45
|
-
if (this.dim === 3) this.pts.push(z);
|
|
46
|
-
this.smooth.push(0);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Begin a piece: decide whether the joint at the current last point is smooth. */
|
|
50
|
-
joint(tan) {
|
|
51
|
-
if (!tan) return;
|
|
52
|
-
if (this.lastTan) {
|
|
53
|
-
const c = this.lastTan[0] * tan[0] + this.lastTan[1] * tan[1] + this.lastTan[2] * tan[2];
|
|
54
|
-
this.smooth[this.smooth.length - 1] = c >= SMOOTH_JOINT_COS ? 1 : 0;
|
|
55
|
-
} else {
|
|
56
|
-
this.firstTan = tan;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
push(x, y, z, smooth) {
|
|
61
|
-
// skip duplicate points
|
|
62
|
-
const d = this.dim;
|
|
63
|
-
const p = this.pts;
|
|
64
|
-
const k = p.length - d;
|
|
65
|
-
if (Math.abs(p[k] - x) < 1e-12 && Math.abs(p[k + 1] - y) < 1e-12 && (d === 2 || Math.abs(p[k + 2] - z) < 1e-12)) return;
|
|
66
|
-
p.push(x, y);
|
|
67
|
-
if (d === 3) p.push(z);
|
|
68
|
-
this.smooth.push(smooth ? 1 : 0);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
end(tan) {
|
|
72
|
-
if (tan) this.lastTan = tan;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function tangent(dim, x, y, z) {
|
|
77
|
-
return dim === 3 ? norm3(x, y, z) : norm2(x, y);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @param {object[]} segments absolute segments from parsePathData
|
|
82
|
-
* @param {{tolerance:number, dim?:2|3, maxSegments?:number}} opts
|
|
83
|
-
* @returns {{points: Float64Array, smooth: Uint8Array, closed: boolean}[]}
|
|
84
|
-
*/
|
|
85
|
-
export function flattenPath(segments, opts) {
|
|
86
|
-
const dim = opts.dim || 2;
|
|
87
|
-
const tol = Math.max(opts.tolerance, 1e-9);
|
|
88
|
-
const maxSeg = opts.maxSegments || MAX_SEGMENTS;
|
|
89
|
-
const out = [];
|
|
90
|
-
let b = null;
|
|
91
|
-
|
|
92
|
-
const finish = () => {
|
|
93
|
-
if (!b) return;
|
|
94
|
-
if (b.closed) {
|
|
95
|
-
// drop a duplicated end point and resolve the joint at the start point
|
|
96
|
-
const d = dim;
|
|
97
|
-
const p = b.pts;
|
|
98
|
-
const n = b.count;
|
|
99
|
-
if (n > 1) {
|
|
100
|
-
const k = (n - 1) * d;
|
|
101
|
-
const same = Math.abs(p[k] - p[0]) < 1e-9 && Math.abs(p[k + 1] - p[1]) < 1e-9 && (d === 2 || Math.abs(p[k + 2] - p[2]) < 1e-9);
|
|
102
|
-
if (same) {
|
|
103
|
-
p.length -= d;
|
|
104
|
-
b.smooth.pop();
|
|
105
|
-
} else {
|
|
106
|
-
// implicit closing line
|
|
107
|
-
const t = tangent(dim, p[0] - p[k], p[1] - p[k + 1], d === 3 ? p[2] - p[k + 2] : 0);
|
|
108
|
-
b.joint(t);
|
|
109
|
-
b.end(t);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (b.firstTan && b.lastTan) {
|
|
113
|
-
const c = b.lastTan[0] * b.firstTan[0] + b.lastTan[1] * b.firstTan[1] + b.lastTan[2] * b.firstTan[2];
|
|
114
|
-
b.smooth[0] = c >= SMOOTH_JOINT_COS ? 1 : 0;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (b.count >= (b.closed ? 3 : 2)) {
|
|
118
|
-
out.push({ points: Float64Array.from(b.pts), smooth: Uint8Array.from(b.smooth), closed: b.closed });
|
|
119
|
-
} else if (b.count >= 2) {
|
|
120
|
-
// a closed contour with fewer than 3 distinct points is degenerate; keep it open so it can be reported
|
|
121
|
-
out.push({ points: Float64Array.from(b.pts), smooth: Uint8Array.from(b.smooth), closed: false, degenerate: true });
|
|
122
|
-
}
|
|
123
|
-
b = null;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
for (const s of segments) {
|
|
127
|
-
if (s.c === 'M') {
|
|
128
|
-
finish();
|
|
129
|
-
b = new ContourBuilder(dim);
|
|
130
|
-
b.start(s.x, s.y, s.z || 0);
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
if (!b) continue;
|
|
134
|
-
const [px, py, pz] = b.lastPoint();
|
|
135
|
-
switch (s.c) {
|
|
136
|
-
case 'L': {
|
|
137
|
-
const t = tangent(dim, s.x - px, s.y - py, (s.z || 0) - pz);
|
|
138
|
-
if (!t) break;
|
|
139
|
-
b.joint(t);
|
|
140
|
-
b.push(s.x, s.y, s.z || 0, false);
|
|
141
|
-
b.end(t);
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
case 'Q': {
|
|
145
|
-
const x0 = px, y0 = py, z0 = pz;
|
|
146
|
-
const ddx = x0 - 2 * s.x1 + s.x, ddy = y0 - 2 * s.y1 + s.y, ddz = dim === 3 ? z0 - 2 * s.z1 + s.z : 0;
|
|
147
|
-
const M = Math.hypot(ddx, ddy, ddz);
|
|
148
|
-
const n = Math.max(1, Math.min(maxSeg, Math.ceil(Math.sqrt((0.25 * M) / tol))));
|
|
149
|
-
const t0 = tangent(dim, s.x1 - x0, s.y1 - y0, (s.z1 || 0) - z0) || tangent(dim, s.x - x0, s.y - y0, (s.z || 0) - z0);
|
|
150
|
-
const t1 = tangent(dim, s.x - s.x1, s.y - s.y1, (s.z || 0) - (s.z1 || 0)) || t0;
|
|
151
|
-
if (!t0) break;
|
|
152
|
-
b.joint(t0);
|
|
153
|
-
for (let i = 1; i <= n; i++) {
|
|
154
|
-
const t = i / n, mt = 1 - t;
|
|
155
|
-
const a = mt * mt, bb = 2 * mt * t, c = t * t;
|
|
156
|
-
b.push(a * x0 + bb * s.x1 + c * s.x, a * y0 + bb * s.y1 + c * s.y, a * z0 + bb * (s.z1 || 0) + c * (s.z || 0), i < n);
|
|
157
|
-
}
|
|
158
|
-
b.end(t1);
|
|
159
|
-
break;
|
|
160
|
-
}
|
|
161
|
-
case 'C': {
|
|
162
|
-
const x0 = px, y0 = py, z0 = pz;
|
|
163
|
-
const z1 = s.z1 || 0, z2 = s.z2 || 0, z3 = s.z || 0;
|
|
164
|
-
const d1 = Math.hypot(x0 - 2 * s.x1 + s.x2, y0 - 2 * s.y1 + s.y2, dim === 3 ? z0 - 2 * z1 + z2 : 0);
|
|
165
|
-
const d2 = Math.hypot(s.x1 - 2 * s.x2 + s.x, s.y1 - 2 * s.y2 + s.y, dim === 3 ? z1 - 2 * z2 + z3 : 0);
|
|
166
|
-
const M = Math.max(d1, d2);
|
|
167
|
-
const n = Math.max(1, Math.min(maxSeg, Math.ceil(Math.sqrt((0.75 * M) / tol))));
|
|
168
|
-
const t0 =
|
|
169
|
-
tangent(dim, s.x1 - x0, s.y1 - y0, z1 - z0) ||
|
|
170
|
-
tangent(dim, s.x2 - x0, s.y2 - y0, z2 - z0) ||
|
|
171
|
-
tangent(dim, s.x - x0, s.y - y0, z3 - z0);
|
|
172
|
-
const t1 =
|
|
173
|
-
tangent(dim, s.x - s.x2, s.y - s.y2, z3 - z2) ||
|
|
174
|
-
tangent(dim, s.x - s.x1, s.y - s.y1, z3 - z1) ||
|
|
175
|
-
t0;
|
|
176
|
-
if (!t0) break;
|
|
177
|
-
b.joint(t0);
|
|
178
|
-
for (let i = 1; i <= n; i++) {
|
|
179
|
-
const t = i / n, mt = 1 - t;
|
|
180
|
-
const a = mt * mt * mt, bb = 3 * mt * mt * t, c = 3 * mt * t * t, d = t * t * t;
|
|
181
|
-
b.push(
|
|
182
|
-
a * x0 + bb * s.x1 + c * s.x2 + d * s.x,
|
|
183
|
-
a * y0 + bb * s.y1 + c * s.y2 + d * s.y,
|
|
184
|
-
a * z0 + bb * z1 + c * z2 + d * z3,
|
|
185
|
-
i < n,
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
b.end(t1);
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
case 'A': {
|
|
192
|
-
const arc = arcToCenter(px, py, s.rx, s.ry, s.rot, s.large, s.sweep, s.x, s.y);
|
|
193
|
-
if (!arc) {
|
|
194
|
-
const t = tangent(2, s.x - px, s.y - py, 0);
|
|
195
|
-
if (!t) break;
|
|
196
|
-
b.joint(t);
|
|
197
|
-
b.push(s.x, s.y, 0, false);
|
|
198
|
-
b.end(t);
|
|
199
|
-
break;
|
|
200
|
-
}
|
|
201
|
-
const r = Math.max(arc.rx, arc.ry);
|
|
202
|
-
const step = tol >= r ? Math.PI / 2 : 2 * Math.acos(1 - tol / r);
|
|
203
|
-
const n = Math.max(1, Math.min(maxSeg, Math.ceil(Math.abs(arc.dtheta) / step)));
|
|
204
|
-
b.joint(arc.tangentAt(arc.theta1));
|
|
205
|
-
for (let i = 1; i <= n; i++) {
|
|
206
|
-
const th = arc.theta1 + (arc.dtheta * i) / n;
|
|
207
|
-
if (i === n) b.push(s.x, s.y, 0, false);
|
|
208
|
-
else {
|
|
209
|
-
const p = arc.pointAt(th);
|
|
210
|
-
b.push(p[0], p[1], 0, true);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
b.end(arc.tangentAt(arc.theta1 + arc.dtheta));
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
case 'Z':
|
|
217
|
-
b.closed = true;
|
|
218
|
-
finish();
|
|
219
|
-
break;
|
|
220
|
-
default:
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
finish();
|
|
225
|
-
return out;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/** SVG arc endpoint -> center parameterization (SVG 1.1 implementation notes F.6.5). */
|
|
229
|
-
export function arcToCenter(x1, y1, rx, ry, rotDeg, large, sweep, x2, y2) {
|
|
230
|
-
if (x1 === x2 && y1 === y2) return null;
|
|
231
|
-
rx = Math.abs(rx);
|
|
232
|
-
ry = Math.abs(ry);
|
|
233
|
-
if (rx < 1e-12 || ry < 1e-12) return null;
|
|
234
|
-
const phi = (rotDeg * Math.PI) / 180;
|
|
235
|
-
const cosP = Math.cos(phi), sinP = Math.sin(phi);
|
|
236
|
-
const dx2 = (x1 - x2) / 2, dy2 = (y1 - y2) / 2;
|
|
237
|
-
const x1p = cosP * dx2 + sinP * dy2;
|
|
238
|
-
const y1p = -sinP * dx2 + cosP * dy2;
|
|
239
|
-
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
240
|
-
if (lambda > 1) {
|
|
241
|
-
const s = Math.sqrt(lambda);
|
|
242
|
-
rx *= s;
|
|
243
|
-
ry *= s;
|
|
244
|
-
}
|
|
245
|
-
const rx2 = rx * rx, ry2 = ry * ry;
|
|
246
|
-
const num = rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p;
|
|
247
|
-
const den = rx2 * y1p * y1p + ry2 * x1p * x1p;
|
|
248
|
-
let coef = den > 0 ? Math.sqrt(Math.max(0, num / den)) : 0;
|
|
249
|
-
if (large === sweep) coef = -coef;
|
|
250
|
-
const cxp = (coef * rx * y1p) / ry;
|
|
251
|
-
const cyp = (-coef * ry * x1p) / rx;
|
|
252
|
-
const cx = cosP * cxp - sinP * cyp + (x1 + x2) / 2;
|
|
253
|
-
const cy = sinP * cxp + cosP * cyp + (y1 + y2) / 2;
|
|
254
|
-
const ang = (ux, uy, vx, vy) => {
|
|
255
|
-
const a = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
256
|
-
return a;
|
|
257
|
-
};
|
|
258
|
-
const ux = (x1p - cxp) / rx, uy = (y1p - cyp) / ry;
|
|
259
|
-
const vx = (-x1p - cxp) / rx, vy = (-y1p - cyp) / ry;
|
|
260
|
-
const theta1 = Math.atan2(uy, ux);
|
|
261
|
-
let dtheta = ang(ux, uy, vx, vy);
|
|
262
|
-
if (!sweep && dtheta > 0) dtheta -= 2 * Math.PI;
|
|
263
|
-
else if (sweep && dtheta < 0) dtheta += 2 * Math.PI;
|
|
264
|
-
const sign = dtheta >= 0 ? 1 : -1;
|
|
265
|
-
return {
|
|
266
|
-
cx,
|
|
267
|
-
cy,
|
|
268
|
-
rx,
|
|
269
|
-
ry,
|
|
270
|
-
theta1,
|
|
271
|
-
dtheta,
|
|
272
|
-
pointAt(th) {
|
|
273
|
-
const c = Math.cos(th), s = Math.sin(th);
|
|
274
|
-
return [cx + rx * cosP * c - ry * sinP * s, cy + rx * sinP * c + ry * cosP * s];
|
|
275
|
-
},
|
|
276
|
-
tangentAt(th) {
|
|
277
|
-
const c = Math.cos(th), s = Math.sin(th);
|
|
278
|
-
return norm2(sign * (-rx * cosP * s - ry * sinP * c), sign * (-rx * sinP * s + ry * cosP * c));
|
|
279
|
-
},
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/** Polyline points (flat array) -> contour with corner detection by turning angle. */
|
|
284
|
-
export function polylineContour(points, dim, closed) {
|
|
285
|
-
const n = points.length / dim;
|
|
286
|
-
const smooth = new Uint8Array(n);
|
|
287
|
-
return { points: Float64Array.from(points), smooth, closed };
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/** Regular polygon approximation of a circle (all points smooth), counter-clockwise. */
|
|
291
|
-
export function circleContour(r, segments) {
|
|
292
|
-
const pts = new Float64Array(segments * 2);
|
|
293
|
-
for (let i = 0; i < segments; i++) {
|
|
294
|
-
const a = (i / segments) * Math.PI * 2;
|
|
295
|
-
pts[i * 2] = Math.cos(a) * r;
|
|
296
|
-
pts[i * 2 + 1] = Math.sin(a) * r;
|
|
297
|
-
}
|
|
298
|
-
return { points: pts, smooth: new Uint8Array(segments).fill(1), closed: true };
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/** Signed area of a 2D contour (positive = counter-clockwise with y up). */
|
|
302
|
-
export function signedArea(points) {
|
|
303
|
-
let a = 0;
|
|
304
|
-
const n = points.length / 2;
|
|
305
|
-
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
306
|
-
a += (points[j * 2] - points[i * 2]) * (points[j * 2 + 1] + points[i * 2 + 1]);
|
|
307
|
-
}
|
|
308
|
-
return a / 2;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/** Reverse a 2D/3D contour in place-safe manner (returns a new contour). */
|
|
312
|
-
export function reverseContour(c, dim = 2) {
|
|
313
|
-
const n = c.points.length / dim;
|
|
314
|
-
const pts = new Float64Array(c.points.length);
|
|
315
|
-
const sm = new Uint8Array(n);
|
|
316
|
-
for (let i = 0; i < n; i++) {
|
|
317
|
-
const j = n - 1 - i;
|
|
318
|
-
for (let k = 0; k < dim; k++) pts[i * dim + k] = c.points[j * dim + k];
|
|
319
|
-
sm[i] = c.smooth[j];
|
|
320
|
-
}
|
|
321
|
-
return { ...c, points: pts, smooth: sm };
|
|
322
|
-
}
|
|
1
|
+
// Flattening of path segments into polylines ("contours").
|
|
2
|
+
//
|
|
3
|
+
// Curves are subdivided adaptively from a geometric tolerance (Wang's formula
|
|
4
|
+
// for Béziers, chord error for arcs), so the same path produces coarser or
|
|
5
|
+
// finer polylines for different levels of detail. Every output point carries a
|
|
6
|
+
// `smooth` flag: true for curve interiors and tangent-continuous joints, false
|
|
7
|
+
// for corners. Geometry generators use the flags to decide where normals must
|
|
8
|
+
// be split (sharp edges) and where they are shared (smooth surfaces).
|
|
9
|
+
|
|
10
|
+
const SMOOTH_JOINT_COS = Math.cos((8 * Math.PI) / 180);
|
|
11
|
+
const MAX_SEGMENTS = 256;
|
|
12
|
+
|
|
13
|
+
function norm2(x, y) {
|
|
14
|
+
const l = Math.hypot(x, y);
|
|
15
|
+
return l > 1e-12 ? [x / l, y / l, 0] : null;
|
|
16
|
+
}
|
|
17
|
+
function norm3(x, y, z) {
|
|
18
|
+
const l = Math.hypot(x, y, z);
|
|
19
|
+
return l > 1e-12 ? [x / l, y / l, z / l] : null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class ContourBuilder {
|
|
23
|
+
constructor(dim) {
|
|
24
|
+
this.dim = dim;
|
|
25
|
+
this.pts = [];
|
|
26
|
+
this.smooth = [];
|
|
27
|
+
this.firstTan = null;
|
|
28
|
+
this.lastTan = null;
|
|
29
|
+
this.closed = false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get count() {
|
|
33
|
+
return this.smooth.length;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
lastPoint() {
|
|
37
|
+
const d = this.dim;
|
|
38
|
+
const p = this.pts;
|
|
39
|
+
const k = p.length - d;
|
|
40
|
+
return d === 3 ? [p[k], p[k + 1], p[k + 2]] : [p[k], p[k + 1], 0];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
start(x, y, z) {
|
|
44
|
+
this.pts.push(x, y);
|
|
45
|
+
if (this.dim === 3) this.pts.push(z);
|
|
46
|
+
this.smooth.push(0);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Begin a piece: decide whether the joint at the current last point is smooth. */
|
|
50
|
+
joint(tan) {
|
|
51
|
+
if (!tan) return;
|
|
52
|
+
if (this.lastTan) {
|
|
53
|
+
const c = this.lastTan[0] * tan[0] + this.lastTan[1] * tan[1] + this.lastTan[2] * tan[2];
|
|
54
|
+
this.smooth[this.smooth.length - 1] = c >= SMOOTH_JOINT_COS ? 1 : 0;
|
|
55
|
+
} else {
|
|
56
|
+
this.firstTan = tan;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
push(x, y, z, smooth) {
|
|
61
|
+
// skip duplicate points
|
|
62
|
+
const d = this.dim;
|
|
63
|
+
const p = this.pts;
|
|
64
|
+
const k = p.length - d;
|
|
65
|
+
if (Math.abs(p[k] - x) < 1e-12 && Math.abs(p[k + 1] - y) < 1e-12 && (d === 2 || Math.abs(p[k + 2] - z) < 1e-12)) return;
|
|
66
|
+
p.push(x, y);
|
|
67
|
+
if (d === 3) p.push(z);
|
|
68
|
+
this.smooth.push(smooth ? 1 : 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
end(tan) {
|
|
72
|
+
if (tan) this.lastTan = tan;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function tangent(dim, x, y, z) {
|
|
77
|
+
return dim === 3 ? norm3(x, y, z) : norm2(x, y);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {object[]} segments absolute segments from parsePathData
|
|
82
|
+
* @param {{tolerance:number, dim?:2|3, maxSegments?:number}} opts
|
|
83
|
+
* @returns {{points: Float64Array, smooth: Uint8Array, closed: boolean}[]}
|
|
84
|
+
*/
|
|
85
|
+
export function flattenPath(segments, opts) {
|
|
86
|
+
const dim = opts.dim || 2;
|
|
87
|
+
const tol = Math.max(opts.tolerance, 1e-9);
|
|
88
|
+
const maxSeg = opts.maxSegments || MAX_SEGMENTS;
|
|
89
|
+
const out = [];
|
|
90
|
+
let b = null;
|
|
91
|
+
|
|
92
|
+
const finish = () => {
|
|
93
|
+
if (!b) return;
|
|
94
|
+
if (b.closed) {
|
|
95
|
+
// drop a duplicated end point and resolve the joint at the start point
|
|
96
|
+
const d = dim;
|
|
97
|
+
const p = b.pts;
|
|
98
|
+
const n = b.count;
|
|
99
|
+
if (n > 1) {
|
|
100
|
+
const k = (n - 1) * d;
|
|
101
|
+
const same = Math.abs(p[k] - p[0]) < 1e-9 && Math.abs(p[k + 1] - p[1]) < 1e-9 && (d === 2 || Math.abs(p[k + 2] - p[2]) < 1e-9);
|
|
102
|
+
if (same) {
|
|
103
|
+
p.length -= d;
|
|
104
|
+
b.smooth.pop();
|
|
105
|
+
} else {
|
|
106
|
+
// implicit closing line
|
|
107
|
+
const t = tangent(dim, p[0] - p[k], p[1] - p[k + 1], d === 3 ? p[2] - p[k + 2] : 0);
|
|
108
|
+
b.joint(t);
|
|
109
|
+
b.end(t);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (b.firstTan && b.lastTan) {
|
|
113
|
+
const c = b.lastTan[0] * b.firstTan[0] + b.lastTan[1] * b.firstTan[1] + b.lastTan[2] * b.firstTan[2];
|
|
114
|
+
b.smooth[0] = c >= SMOOTH_JOINT_COS ? 1 : 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (b.count >= (b.closed ? 3 : 2)) {
|
|
118
|
+
out.push({ points: Float64Array.from(b.pts), smooth: Uint8Array.from(b.smooth), closed: b.closed });
|
|
119
|
+
} else if (b.count >= 2) {
|
|
120
|
+
// a closed contour with fewer than 3 distinct points is degenerate; keep it open so it can be reported
|
|
121
|
+
out.push({ points: Float64Array.from(b.pts), smooth: Uint8Array.from(b.smooth), closed: false, degenerate: true });
|
|
122
|
+
}
|
|
123
|
+
b = null;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
for (const s of segments) {
|
|
127
|
+
if (s.c === 'M') {
|
|
128
|
+
finish();
|
|
129
|
+
b = new ContourBuilder(dim);
|
|
130
|
+
b.start(s.x, s.y, s.z || 0);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (!b) continue;
|
|
134
|
+
const [px, py, pz] = b.lastPoint();
|
|
135
|
+
switch (s.c) {
|
|
136
|
+
case 'L': {
|
|
137
|
+
const t = tangent(dim, s.x - px, s.y - py, (s.z || 0) - pz);
|
|
138
|
+
if (!t) break;
|
|
139
|
+
b.joint(t);
|
|
140
|
+
b.push(s.x, s.y, s.z || 0, false);
|
|
141
|
+
b.end(t);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case 'Q': {
|
|
145
|
+
const x0 = px, y0 = py, z0 = pz;
|
|
146
|
+
const ddx = x0 - 2 * s.x1 + s.x, ddy = y0 - 2 * s.y1 + s.y, ddz = dim === 3 ? z0 - 2 * s.z1 + s.z : 0;
|
|
147
|
+
const M = Math.hypot(ddx, ddy, ddz);
|
|
148
|
+
const n = Math.max(1, Math.min(maxSeg, Math.ceil(Math.sqrt((0.25 * M) / tol))));
|
|
149
|
+
const t0 = tangent(dim, s.x1 - x0, s.y1 - y0, (s.z1 || 0) - z0) || tangent(dim, s.x - x0, s.y - y0, (s.z || 0) - z0);
|
|
150
|
+
const t1 = tangent(dim, s.x - s.x1, s.y - s.y1, (s.z || 0) - (s.z1 || 0)) || t0;
|
|
151
|
+
if (!t0) break;
|
|
152
|
+
b.joint(t0);
|
|
153
|
+
for (let i = 1; i <= n; i++) {
|
|
154
|
+
const t = i / n, mt = 1 - t;
|
|
155
|
+
const a = mt * mt, bb = 2 * mt * t, c = t * t;
|
|
156
|
+
b.push(a * x0 + bb * s.x1 + c * s.x, a * y0 + bb * s.y1 + c * s.y, a * z0 + bb * (s.z1 || 0) + c * (s.z || 0), i < n);
|
|
157
|
+
}
|
|
158
|
+
b.end(t1);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case 'C': {
|
|
162
|
+
const x0 = px, y0 = py, z0 = pz;
|
|
163
|
+
const z1 = s.z1 || 0, z2 = s.z2 || 0, z3 = s.z || 0;
|
|
164
|
+
const d1 = Math.hypot(x0 - 2 * s.x1 + s.x2, y0 - 2 * s.y1 + s.y2, dim === 3 ? z0 - 2 * z1 + z2 : 0);
|
|
165
|
+
const d2 = Math.hypot(s.x1 - 2 * s.x2 + s.x, s.y1 - 2 * s.y2 + s.y, dim === 3 ? z1 - 2 * z2 + z3 : 0);
|
|
166
|
+
const M = Math.max(d1, d2);
|
|
167
|
+
const n = Math.max(1, Math.min(maxSeg, Math.ceil(Math.sqrt((0.75 * M) / tol))));
|
|
168
|
+
const t0 =
|
|
169
|
+
tangent(dim, s.x1 - x0, s.y1 - y0, z1 - z0) ||
|
|
170
|
+
tangent(dim, s.x2 - x0, s.y2 - y0, z2 - z0) ||
|
|
171
|
+
tangent(dim, s.x - x0, s.y - y0, z3 - z0);
|
|
172
|
+
const t1 =
|
|
173
|
+
tangent(dim, s.x - s.x2, s.y - s.y2, z3 - z2) ||
|
|
174
|
+
tangent(dim, s.x - s.x1, s.y - s.y1, z3 - z1) ||
|
|
175
|
+
t0;
|
|
176
|
+
if (!t0) break;
|
|
177
|
+
b.joint(t0);
|
|
178
|
+
for (let i = 1; i <= n; i++) {
|
|
179
|
+
const t = i / n, mt = 1 - t;
|
|
180
|
+
const a = mt * mt * mt, bb = 3 * mt * mt * t, c = 3 * mt * t * t, d = t * t * t;
|
|
181
|
+
b.push(
|
|
182
|
+
a * x0 + bb * s.x1 + c * s.x2 + d * s.x,
|
|
183
|
+
a * y0 + bb * s.y1 + c * s.y2 + d * s.y,
|
|
184
|
+
a * z0 + bb * z1 + c * z2 + d * z3,
|
|
185
|
+
i < n,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
b.end(t1);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case 'A': {
|
|
192
|
+
const arc = arcToCenter(px, py, s.rx, s.ry, s.rot, s.large, s.sweep, s.x, s.y);
|
|
193
|
+
if (!arc) {
|
|
194
|
+
const t = tangent(2, s.x - px, s.y - py, 0);
|
|
195
|
+
if (!t) break;
|
|
196
|
+
b.joint(t);
|
|
197
|
+
b.push(s.x, s.y, 0, false);
|
|
198
|
+
b.end(t);
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
const r = Math.max(arc.rx, arc.ry);
|
|
202
|
+
const step = tol >= r ? Math.PI / 2 : 2 * Math.acos(1 - tol / r);
|
|
203
|
+
const n = Math.max(1, Math.min(maxSeg, Math.ceil(Math.abs(arc.dtheta) / step)));
|
|
204
|
+
b.joint(arc.tangentAt(arc.theta1));
|
|
205
|
+
for (let i = 1; i <= n; i++) {
|
|
206
|
+
const th = arc.theta1 + (arc.dtheta * i) / n;
|
|
207
|
+
if (i === n) b.push(s.x, s.y, 0, false);
|
|
208
|
+
else {
|
|
209
|
+
const p = arc.pointAt(th);
|
|
210
|
+
b.push(p[0], p[1], 0, true);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
b.end(arc.tangentAt(arc.theta1 + arc.dtheta));
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case 'Z':
|
|
217
|
+
b.closed = true;
|
|
218
|
+
finish();
|
|
219
|
+
break;
|
|
220
|
+
default:
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
finish();
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** SVG arc endpoint -> center parameterization (SVG 1.1 implementation notes F.6.5). */
|
|
229
|
+
export function arcToCenter(x1, y1, rx, ry, rotDeg, large, sweep, x2, y2) {
|
|
230
|
+
if (x1 === x2 && y1 === y2) return null;
|
|
231
|
+
rx = Math.abs(rx);
|
|
232
|
+
ry = Math.abs(ry);
|
|
233
|
+
if (rx < 1e-12 || ry < 1e-12) return null;
|
|
234
|
+
const phi = (rotDeg * Math.PI) / 180;
|
|
235
|
+
const cosP = Math.cos(phi), sinP = Math.sin(phi);
|
|
236
|
+
const dx2 = (x1 - x2) / 2, dy2 = (y1 - y2) / 2;
|
|
237
|
+
const x1p = cosP * dx2 + sinP * dy2;
|
|
238
|
+
const y1p = -sinP * dx2 + cosP * dy2;
|
|
239
|
+
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
240
|
+
if (lambda > 1) {
|
|
241
|
+
const s = Math.sqrt(lambda);
|
|
242
|
+
rx *= s;
|
|
243
|
+
ry *= s;
|
|
244
|
+
}
|
|
245
|
+
const rx2 = rx * rx, ry2 = ry * ry;
|
|
246
|
+
const num = rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p;
|
|
247
|
+
const den = rx2 * y1p * y1p + ry2 * x1p * x1p;
|
|
248
|
+
let coef = den > 0 ? Math.sqrt(Math.max(0, num / den)) : 0;
|
|
249
|
+
if (large === sweep) coef = -coef;
|
|
250
|
+
const cxp = (coef * rx * y1p) / ry;
|
|
251
|
+
const cyp = (-coef * ry * x1p) / rx;
|
|
252
|
+
const cx = cosP * cxp - sinP * cyp + (x1 + x2) / 2;
|
|
253
|
+
const cy = sinP * cxp + cosP * cyp + (y1 + y2) / 2;
|
|
254
|
+
const ang = (ux, uy, vx, vy) => {
|
|
255
|
+
const a = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
256
|
+
return a;
|
|
257
|
+
};
|
|
258
|
+
const ux = (x1p - cxp) / rx, uy = (y1p - cyp) / ry;
|
|
259
|
+
const vx = (-x1p - cxp) / rx, vy = (-y1p - cyp) / ry;
|
|
260
|
+
const theta1 = Math.atan2(uy, ux);
|
|
261
|
+
let dtheta = ang(ux, uy, vx, vy);
|
|
262
|
+
if (!sweep && dtheta > 0) dtheta -= 2 * Math.PI;
|
|
263
|
+
else if (sweep && dtheta < 0) dtheta += 2 * Math.PI;
|
|
264
|
+
const sign = dtheta >= 0 ? 1 : -1;
|
|
265
|
+
return {
|
|
266
|
+
cx,
|
|
267
|
+
cy,
|
|
268
|
+
rx,
|
|
269
|
+
ry,
|
|
270
|
+
theta1,
|
|
271
|
+
dtheta,
|
|
272
|
+
pointAt(th) {
|
|
273
|
+
const c = Math.cos(th), s = Math.sin(th);
|
|
274
|
+
return [cx + rx * cosP * c - ry * sinP * s, cy + rx * sinP * c + ry * cosP * s];
|
|
275
|
+
},
|
|
276
|
+
tangentAt(th) {
|
|
277
|
+
const c = Math.cos(th), s = Math.sin(th);
|
|
278
|
+
return norm2(sign * (-rx * cosP * s - ry * sinP * c), sign * (-rx * sinP * s + ry * cosP * c));
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Polyline points (flat array) -> contour with corner detection by turning angle. */
|
|
284
|
+
export function polylineContour(points, dim, closed) {
|
|
285
|
+
const n = points.length / dim;
|
|
286
|
+
const smooth = new Uint8Array(n);
|
|
287
|
+
return { points: Float64Array.from(points), smooth, closed };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Regular polygon approximation of a circle (all points smooth), counter-clockwise. */
|
|
291
|
+
export function circleContour(r, segments) {
|
|
292
|
+
const pts = new Float64Array(segments * 2);
|
|
293
|
+
for (let i = 0; i < segments; i++) {
|
|
294
|
+
const a = (i / segments) * Math.PI * 2;
|
|
295
|
+
pts[i * 2] = Math.cos(a) * r;
|
|
296
|
+
pts[i * 2 + 1] = Math.sin(a) * r;
|
|
297
|
+
}
|
|
298
|
+
return { points: pts, smooth: new Uint8Array(segments).fill(1), closed: true };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Signed area of a 2D contour (positive = counter-clockwise with y up). */
|
|
302
|
+
export function signedArea(points) {
|
|
303
|
+
let a = 0;
|
|
304
|
+
const n = points.length / 2;
|
|
305
|
+
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
306
|
+
a += (points[j * 2] - points[i * 2]) * (points[j * 2 + 1] + points[i * 2 + 1]);
|
|
307
|
+
}
|
|
308
|
+
return a / 2;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Reverse a 2D/3D contour in place-safe manner (returns a new contour). */
|
|
312
|
+
export function reverseContour(c, dim = 2) {
|
|
313
|
+
const n = c.points.length / dim;
|
|
314
|
+
const pts = new Float64Array(c.points.length);
|
|
315
|
+
const sm = new Uint8Array(n);
|
|
316
|
+
for (let i = 0; i < n; i++) {
|
|
317
|
+
const j = n - 1 - i;
|
|
318
|
+
for (let k = 0; k < dim; k++) pts[i * dim + k] = c.points[j * dim + k];
|
|
319
|
+
sm[i] = c.smooth[j];
|
|
320
|
+
}
|
|
321
|
+
return { ...c, points: pts, smooth: sm };
|
|
322
|
+
}
|