fast-mesh-wasm 0.4.0 → 0.5.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 (47) hide show
  1. package/README.md +51 -3
  2. package/dist/{api-BUy7FRx7.js → api-4kz45-Mq.js} +2 -3
  3. package/dist/{client-Bpjy-eFY.js → client-C61Kku4i.js} +102 -24
  4. package/dist/{client-D-O5-tAg.d.ts → client-tP6R9foI.d.ts} +19 -8
  5. package/dist/{colormap-DxGMYVfs.js → colormap-Dr5V3bvs.js} +1 -2
  6. package/dist/{core-Cke_kZ2e.js → core-4OcWKu3r.js} +2 -2
  7. package/dist/{core-D2X9GPfK.d.ts → core-CJ9D6U5A.d.ts} +1 -3
  8. package/dist/{engine-D6MpGb9i.js → engine-X1Awlc5y.js} +58 -28
  9. package/dist/io.d.ts +294 -0
  10. package/dist/io.js +1395 -0
  11. package/dist/{mesh-zj7-wJBw.js → mesh-CWvrjO8S.js} +3 -4
  12. package/dist/{mesh-BUVxv3jo.d.ts → mesh-D3fv56nF.d.ts} +1 -3
  13. package/dist/node.d.ts +3 -5
  14. package/dist/node.js +4 -5
  15. package/dist/{protocol-CK6_3aqo.d.ts → protocol-BGQAB0op.d.ts} +16 -3
  16. package/dist/{protocol-CAUYm1gd.js → protocol-Ci9LLiwO.js} +7 -6
  17. package/dist/r3f.d.ts +22 -6
  18. package/dist/r3f.js +28 -19
  19. package/dist/{react-CTADh3NN.js → react-DzrtrzZy.js} +4 -6
  20. package/dist/react.d.ts +4 -6
  21. package/dist/react.js +1 -1
  22. package/dist/{scene-D6m9XM3O.js → scene-BnkCS0sv.js} +668 -14
  23. package/dist/{scene-like-DJKQP5uf.d.ts → scene-like-BzkDNHCy.d.ts} +502 -42
  24. package/dist/{snapshot-Cf6NgDl7.js → snapshot-BkbGPspK.js} +1 -2
  25. package/dist/{store-QWRDXlAn.d.ts → store-BKFzSykN.d.ts} +3 -9
  26. package/dist/{store-Ml3TQGsR.js → store-CEd2y2KS.js} +7 -9
  27. package/dist/store.d.ts +2 -4
  28. package/dist/store.js +4 -5
  29. package/dist/three.d.ts +2 -6
  30. package/dist/three.js +4 -6
  31. package/dist/{views-Dh3aWc2q.js → views-CUBMOK0U.js} +5 -8
  32. package/dist/wasm/fast_mesh_wasm.d.ts +86 -6
  33. package/dist/wasm/fast_mesh_wasm.js +192 -6
  34. package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
  35. package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +9 -1
  36. package/dist/wasm-threads/fast_mesh_wasm.d.ts +86 -6
  37. package/dist/wasm-threads/fast_mesh_wasm.js +192 -6
  38. package/dist/wasm-threads/fast_mesh_wasm_bg.wasm +0 -0
  39. package/dist/wasm-threads/fast_mesh_wasm_bg.wasm.d.ts +9 -1
  40. package/dist/web.d.ts +3 -5
  41. package/dist/web.js +4 -5
  42. package/dist/worker-engine.d.ts +1 -7
  43. package/dist/worker-engine.js +1 -1
  44. package/dist/worker-entry.js +2 -3
  45. package/dist/worker.d.ts +2 -2
  46. package/dist/worker.js +1 -1
  47. package/package.json +16 -4
@@ -1,7 +1,7 @@
1
- import { a as release, c as wasmMemory, i as finalizer, n as call, r as ensureInitialized, t as FastMeshError } from "./core-Cke_kZ2e.js";
2
- import { a as FieldHandle, c as rotationMatrix, i as encodeRule, l as targetsOf, n as meshViews, o as MeshHandle, r as copyRule, s as SectionHandle, t as ViewGuard } from "./views-Dh3aWc2q.js";
1
+ import { a as release, c as wasmMemory, i as finalizer, n as call, r as ensureInitialized, t as FastMeshError } from "./core-4OcWKu3r.js";
2
+ import { a as FieldHandle, c as rotationMatrix, i as encodeRule, l as targetsOf, n as meshViews, o as MeshHandle, r as copyRule, s as SectionHandle, t as ViewGuard } from "./views-CUBMOK0U.js";
3
3
  import { WasmScene } from "./wasm/fast_mesh_wasm.js";
4
- //#region src/changes.ts
4
+ // src/changes.ts
5
5
  const ADDED = 1;
6
6
  const REMOVED = 2;
7
7
  const PLACEMENT = 4;
@@ -56,8 +56,246 @@ function decodeChanges(msg) {
56
56
  if (at !== msg.length) throw new Error("fast-mesh-wasm: trailing words in change message");
57
57
  return out;
58
58
  }
59
- //#endregion
60
- //#region src/scene.ts
59
+ // src/selection.ts
60
+ function invalid$1(message) {
61
+ return new FastMeshError("INVALID_ARGUMENT", message);
62
+ }
63
+ function stale(message) {
64
+ return new FastMeshError("STALE_SELECTION", message);
65
+ }
66
+ /** Checks a spec's shape; the Scene methods check the mesh and the stamp. */
67
+ function checkSpec(spec) {
68
+ if (spec === null || typeof spec !== "object") throw invalid$1("spec must be an object");
69
+ switch (spec.kind) {
70
+ case "brush": {
71
+ const c = spec.center;
72
+ if (c?.length !== 3 || ![
73
+ c[0],
74
+ c[1],
75
+ c[2]
76
+ ].every(Number.isFinite)) throw invalid$1("brush center must be 3 finite numbers");
77
+ if (typeof spec.radius !== "number" || !(spec.radius >= 0)) throw invalid$1("brush radius must be a number, zero or greater");
78
+ if (spec.space !== void 0 && spec.space !== "local" && spec.space !== "world") throw invalid$1("brush space must be \"local\" or \"world\"");
79
+ return;
80
+ }
81
+ case "lasso": {
82
+ const p = spec.polygon;
83
+ const n = Array.isArray(p) ? p.length : p instanceof Float32Array ? p.length : -1;
84
+ if (n < 6 || n % 2 !== 0) throw invalid$1("lasso polygon needs at least 3 [x, y] corners");
85
+ const pts = p;
86
+ for (let k = 0; k < n; k++) if (!Number.isFinite(pts[k])) throw invalid$1("lasso polygon must hold finite numbers");
87
+ if (spec.axis !== void 0 && spec.axis !== "xy" && spec.axis !== "xz" && spec.axis !== "yz") throw invalid$1("lasso axis must be \"xy\", \"xz\" or \"yz\"");
88
+ return;
89
+ }
90
+ case "component":
91
+ if (!Number.isInteger(spec.seed) || spec.seed < 0 || spec.seed >= 2 ** 32) throw new FastMeshError("INVALID_INDEX", "component seed is not a vertex id");
92
+ return;
93
+ default: throw invalid$1(`unknown select kind "${spec.kind}"`);
94
+ }
95
+ }
96
+ /**
97
+ * Builds adjacency from index triples, skipping removed slots (`0, 0, 0`).
98
+ * `slots` is the vertex slot count.
99
+ */
100
+ function buildTopology(indices, slots) {
101
+ const sets = Array.from({ length: slots }, () => /* @__PURE__ */ new Set());
102
+ const used = new Uint8Array(slots);
103
+ const live = [];
104
+ const link = (a, b) => {
105
+ if (a !== b && a < slots && b < slots) {
106
+ sets[a].add(b);
107
+ sets[b].add(a);
108
+ }
109
+ };
110
+ for (let t = 0; t * 3 + 2 < indices.length; t++) {
111
+ const a = indices[t * 3];
112
+ const b = indices[t * 3 + 1];
113
+ const c = indices[t * 3 + 2];
114
+ if (a === 0 && b === 0 && c === 0) continue;
115
+ if (a >= slots || b >= slots || c >= slots) continue;
116
+ live.push(t);
117
+ used[a] = 1;
118
+ used[b] = 1;
119
+ used[c] = 1;
120
+ link(a, b);
121
+ link(b, c);
122
+ link(c, a);
123
+ }
124
+ return {
125
+ neighbors: sets.map((s) => [...s].sort((x, y) => x - y)),
126
+ used,
127
+ liveTriangles: Uint32Array.from(live)
128
+ };
129
+ }
130
+ /** Every used vertex within `radius` of `center` (squared distances). */
131
+ function brushPick(positions, used, center, radius) {
132
+ const out = [];
133
+ const r2 = radius * radius;
134
+ for (let v = 0; v < used.length; v++) {
135
+ if (used[v] === 0) continue;
136
+ const dx = positions[v * 3] - center[0];
137
+ const dy = positions[v * 3 + 1] - center[1];
138
+ const dz = positions[v * 3 + 2] - center[2];
139
+ if (dx * dx + dy * dy + dz * dz <= r2) out.push(v);
140
+ }
141
+ return out;
142
+ }
143
+ /** Even-odd point-in-polygon over flat `[x0, y0, ...]`. */
144
+ function insidePolygon(polygon, x, y) {
145
+ let inside = false;
146
+ const n = polygon.length / 2;
147
+ for (let i = 0, j = n - 1; i < n; j = i++) {
148
+ const xi = polygon[i * 2];
149
+ const yi = polygon[i * 2 + 1];
150
+ const xj = polygon[j * 2];
151
+ const yj = polygon[j * 2 + 1];
152
+ if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) inside = !inside;
153
+ }
154
+ return inside;
155
+ }
156
+ /** Every used vertex whose `axis` projection falls inside `polygon`. */
157
+ function lassoPick(positions, used, polygon, axis) {
158
+ const pick = axis === "xy" ? [0, 1] : axis === "xz" ? [0, 2] : [1, 2];
159
+ const out = [];
160
+ for (let v = 0; v < used.length; v++) {
161
+ if (used[v] === 0) continue;
162
+ if (insidePolygon(polygon, positions[v * 3 + pick[0]], positions[v * 3 + pick[1]])) out.push(v);
163
+ }
164
+ return out;
165
+ }
166
+ /**
167
+ * The connected component holding `seed` (over triangle edges). Throws
168
+ * `INVALID_ARGUMENT` when the seed is out of range or not alive.
169
+ */
170
+ function componentOf(topology, seed) {
171
+ if (!Number.isInteger(seed) || seed < 0 || seed >= topology.used.length) throw new FastMeshError("INVALID_INDEX", "component seed is not a vertex id");
172
+ if (topology.used[seed] === 0) throw invalid$1("component seed is not on a live triangle");
173
+ const seen = new Uint8Array(topology.used.length);
174
+ const queue = [seed];
175
+ seen[seed] = 1;
176
+ while (queue.length > 0) {
177
+ const v = queue.pop();
178
+ for (const w of topology.neighbors[v]) if (seen[w] === 0) {
179
+ seen[w] = 1;
180
+ queue.push(w);
181
+ }
182
+ }
183
+ const out = [];
184
+ for (let v = 0; v < seen.length; v++) if (seen[v] === 1) out.push(v);
185
+ return out;
186
+ }
187
+ /**
188
+ * Grows `ids` by `steps` rings of edge neighbours (staying on live
189
+ * vertices). `steps` 0 returns `ids`.
190
+ */
191
+ function growIds(topology, ids, steps) {
192
+ if (!Number.isInteger(steps) || steps < 0) throw invalid$1("steps must be a non-negative integer");
193
+ const inSet = new Uint8Array(topology.used.length);
194
+ for (const v of ids) if (v < inSet.length) inSet[v] = 1;
195
+ let frontier = [...ids].filter((v) => v < inSet.length);
196
+ for (let s = 0; s < steps; s++) {
197
+ const next = [];
198
+ for (const v of frontier) for (const w of topology.neighbors[v]) if (inSet[w] === 0) {
199
+ inSet[w] = 1;
200
+ next.push(w);
201
+ }
202
+ if (next.length === 0) break;
203
+ frontier = next;
204
+ }
205
+ const out = [];
206
+ for (let v = 0; v < inSet.length; v++) if (inSet[v] === 1) out.push(v);
207
+ return out;
208
+ }
209
+ /**
210
+ * Shrinks `ids` by `steps` rings: each step drops the members with a live
211
+ * neighbour outside the set. Interior selections (no boundary) survive
212
+ * unchanged.
213
+ */
214
+ function shrinkIds(topology, ids, steps) {
215
+ if (!Number.isInteger(steps) || steps < 0) throw invalid$1("steps must be a non-negative integer");
216
+ let current = new Set([...ids].filter((v) => v < topology.used.length));
217
+ for (let s = 0; s < steps; s++) {
218
+ const peel = [];
219
+ for (const v of current) if (topology.neighbors[v].some((w) => !current.has(w))) peel.push(v);
220
+ if (peel.length === 0) break;
221
+ for (const v of peel) current.delete(v);
222
+ }
223
+ return [...current].sort((a, b) => a - b);
224
+ }
225
+ /** Throws `STALE_SELECTION` when `sel` is not for `mesh` or predates its stamp. */
226
+ function checkSelection(sel, mesh, stamp) {
227
+ if (sel === null || typeof sel !== "object" || sel.mesh !== mesh) throw invalid$1("the selection is not for this mesh");
228
+ if (!Number.isInteger(sel.stamp) || sel.stamp !== stamp) throw stale("the selection predates the mesh's latest topology edit; reselect it");
229
+ }
230
+ /**
231
+ * The inverse of a column-major 4x4 (a placement's `matrix4`), for
232
+ * world-space brushes. Throws `INVALID_ARGUMENT` when not invertible.
233
+ */
234
+ function invertMatrix4(m) {
235
+ const inv = /* @__PURE__ */ new Float64Array(16);
236
+ const a00 = m[0];
237
+ const a01 = m[1];
238
+ const a02 = m[2];
239
+ const a03 = m[3];
240
+ const a10 = m[4];
241
+ const a11 = m[5];
242
+ const a12 = m[6];
243
+ const a13 = m[7];
244
+ const a20 = m[8];
245
+ const a21 = m[9];
246
+ const a22 = m[10];
247
+ const a23 = m[11];
248
+ const a30 = m[12];
249
+ const a31 = m[13];
250
+ const a32 = m[14];
251
+ const a33 = m[15];
252
+ const b00 = a00 * a11 - a01 * a10;
253
+ const b01 = a00 * a12 - a02 * a10;
254
+ const b02 = a00 * a13 - a03 * a10;
255
+ const b03 = a01 * a12 - a02 * a11;
256
+ const b04 = a01 * a13 - a03 * a11;
257
+ const b05 = a02 * a13 - a03 * a12;
258
+ const b06 = a20 * a31 - a21 * a30;
259
+ const b07 = a20 * a32 - a22 * a30;
260
+ const b08 = a20 * a33 - a23 * a30;
261
+ const b09 = a21 * a32 - a22 * a31;
262
+ const b10 = a21 * a33 - a23 * a31;
263
+ const b11 = a22 * a33 - a23 * a32;
264
+ const det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
265
+ if (det === 0 || !Number.isFinite(det)) throw invalid$1("the mesh placement is not invertible");
266
+ const s = 1 / det;
267
+ inv[0] = (a11 * b11 - a12 * b10 + a13 * b09) * s;
268
+ inv[1] = (a02 * b10 - a01 * b11 - a03 * b09) * s;
269
+ inv[2] = (a31 * b05 - a32 * b04 + a33 * b03) * s;
270
+ inv[3] = (a22 * b04 - a21 * b05 - a23 * b03) * s;
271
+ inv[4] = (a12 * b08 - a10 * b11 - a13 * b07) * s;
272
+ inv[5] = (a00 * b11 - a02 * b08 + a03 * b07) * s;
273
+ inv[6] = (a32 * b02 - a30 * b05 - a33 * b01) * s;
274
+ inv[7] = (a20 * b05 - a22 * b02 + a23 * b01) * s;
275
+ inv[8] = (a10 * b10 - a11 * b08 + a13 * b06) * s;
276
+ inv[9] = (a01 * b08 - a00 * b10 - a03 * b06) * s;
277
+ inv[10] = (a30 * b04 - a31 * b02 + a33 * b00) * s;
278
+ inv[11] = (a21 * b02 - a20 * b04 - a23 * b00) * s;
279
+ inv[12] = (a11 * b07 - a10 * b09 - a12 * b06) * s;
280
+ inv[13] = (a00 * b09 - a01 * b07 + a02 * b06) * s;
281
+ inv[14] = (a31 * b01 - a30 * b03 - a32 * b00) * s;
282
+ inv[15] = (a20 * b03 - a21 * b01 + a22 * b00) * s;
283
+ return inv;
284
+ }
285
+ /** Applies a column-major 4x4 to a point. */
286
+ function transformPoint(m, p) {
287
+ const x = p[0];
288
+ const y = p[1];
289
+ const z = p[2];
290
+ const w = m[3] * x + m[7] * y + m[11] * z + m[15];
291
+ if (w === 0 || !Number.isFinite(w)) throw invalid$1("the mesh placement does not map the brush centre");
292
+ return [
293
+ (m[0] * x + m[4] * y + m[8] * z + m[12]) / w,
294
+ (m[1] * x + m[5] * y + m[9] * z + m[13]) / w,
295
+ (m[2] * x + m[6] * y + m[10] * z + m[14]) / w
296
+ ];
297
+ }
298
+ // src/scene.ts
61
299
  /**
62
300
  * The `WasmScene` class new scenes wrap: the single-threaded glue's, unless
63
301
  * the Worker entry started the threaded glue ({@link useWasmScene}).
@@ -113,6 +351,17 @@ function vec3At(a, at) {
113
351
  function invalid(message) {
114
352
  return new FastMeshError("INVALID_ARGUMENT", message);
115
353
  }
354
+ /**
355
+ * The mean stretch of a column-major 4x4's linear part: the cube root of
356
+ * its determinant. World-space brush radii divide by it, exact for rigid
357
+ * placements and a documented approximation under non-uniform scale.
358
+ */
359
+ function meanStretch(m) {
360
+ const det = m[0] * (m[5] * m[10] - m[6] * m[9]) - m[4] * (m[1] * m[10] - m[2] * m[9]) + m[8] * (m[1] * m[6] - m[2] * m[5]);
361
+ const s = Math.cbrt(Math.abs(det));
362
+ if (s === 0 || !Number.isFinite(s)) throw invalid("the mesh placement is not invertible");
363
+ return s;
364
+ }
116
365
  const U32_LIMIT = 2 ** 32;
117
366
  const EDIT_CODES = {
118
367
  setPositions: 0,
@@ -202,8 +451,23 @@ const REPAIR_CODES = {
202
451
  removeDegenerate: 1,
203
452
  removeSmallComponents: 2,
204
453
  orient: 3,
205
- fillHoles: 4
454
+ fillHoles: 4,
455
+ inflate: 5,
456
+ smooth: 6,
457
+ remesh: 7,
458
+ bridgeLoops: 8,
459
+ grab: 9,
460
+ relax: 10,
461
+ offset: 11,
462
+ shell: 12,
463
+ simplify: 13,
464
+ subdivide: 14
206
465
  };
466
+ const FALLOFF_CODES = {
467
+ linear: 0,
468
+ smooth: 1
469
+ };
470
+ const SUBDIVIDE_METHOD_CODES = { loop: 0 };
207
471
  /** An optional number for the binding: NaN when unset; an explicit NaN is rejected. */
208
472
  function optional(v, what) {
209
473
  if (v === void 0) return NaN;
@@ -213,6 +477,16 @@ function optional(v, what) {
213
477
  function flagOf(v, fallback) {
214
478
  return v ?? fallback ? 1 : 0;
215
479
  }
480
+ /** A brush center as 3 values; anything else is `INVALID_ARGUMENT`. */
481
+ function centerOf(center, values) {
482
+ const c = center;
483
+ if (c?.length !== 3 || ![
484
+ c[0],
485
+ c[1],
486
+ c[2]
487
+ ].every(Number.isFinite)) throw invalid("center must be 3 finite numbers");
488
+ values.push(c[0], c[1], c[2]);
489
+ }
216
490
  /** A loop id as the binding's `minVertex, len, hash low, hash high`. */
217
491
  function encodeLoopId(id, out) {
218
492
  const { minVertex, len, hash } = id ?? {};
@@ -247,6 +521,46 @@ function encodeRepair(ops) {
247
521
  values.push(optional(o.maxEdges, "maxEdges"), optional(o.maxPerimeter, "maxPerimeter"), optional(o.maxArea, "maxArea"), count, flagOf(o.allowUnclean, false), flagOf(o.fair, true), optional(o.dpLimit, "dpLimit"), optional(o.maxDihedral, "maxDihedral"), optional(o.maxRefineVertices, "maxRefineVertices"));
248
522
  break;
249
523
  }
524
+ case "inflate":
525
+ centerOf(o.center, values);
526
+ values.push(optional(o.radius, "radius"), codeIn(FALLOFF_CODES, o.falloff, "falloff"), optional(o.strength, "strength"));
527
+ break;
528
+ case "smooth":
529
+ case "relax":
530
+ centerOf(o.center, values);
531
+ values.push(optional(o.radius, "radius"), codeIn(FALLOFF_CODES, o.falloff, "falloff"), optional(o.strength, "strength"), optional(o.cycles, "cycles"));
532
+ break;
533
+ case "remesh":
534
+ centerOf(o.center, values);
535
+ values.push(optional(o.radius, "radius"), optional(o.targetLength, "targetLength"), optional(o.maxRounds, "maxRounds"), flagOf(o.splitOnly, false));
536
+ break;
537
+ case "bridgeLoops":
538
+ if (!Array.isArray(o.loops) || o.loops.length !== 2) throw invalid("loops must be an array of two loop ids");
539
+ values.push(optional(o.maxEdges, "maxEdges"));
540
+ for (const id of o.loops) encodeLoopId(id, loops);
541
+ break;
542
+ case "grab": {
543
+ centerOf(o.center, values);
544
+ values.push(optional(o.radius, "radius"), codeIn(FALLOFF_CODES, o.falloff, "falloff"), optional(o.strength, "strength"));
545
+ const d = o.direction;
546
+ if (d?.length !== 3 || ![
547
+ d[0],
548
+ d[1],
549
+ d[2]
550
+ ].every(Number.isFinite)) throw invalid("direction must be 3 finite numbers");
551
+ values.push(d[0], d[1], d[2]);
552
+ break;
553
+ }
554
+ case "offset":
555
+ values.push(optional(o.distance, "distance"));
556
+ break;
557
+ case "shell":
558
+ values.push(optional(o.thickness, "thickness"));
559
+ break;
560
+ case "simplify":
561
+ values.push(optional(o.targetTriangles, "targetTriangles"), flagOf(o.preserveBoundary, true));
562
+ break;
563
+ case "subdivide": values.push(o.method === void 0 ? NaN : codeIn(SUBDIVIDE_METHOD_CODES, o.method, "method"), optional(o.levels, "levels"));
250
564
  }
251
565
  }
252
566
  return {
@@ -343,6 +657,48 @@ function decodeReport(op, r) {
343
657
  reason: FAIL_REASONS[r.next()]
344
658
  }))
345
659
  };
660
+ case "inflate": return {
661
+ moved: r.next(),
662
+ maxDisplacement: r.next()
663
+ };
664
+ case "smooth":
665
+ case "relax": return {
666
+ moved: r.next(),
667
+ maxDisplacement: r.next()
668
+ };
669
+ case "remesh": return {
670
+ splits: r.next(),
671
+ collapses: r.next(),
672
+ flips: r.next(),
673
+ rounds: r.next()
674
+ };
675
+ case "bridgeLoops": return { trianglesAdded: r.next() };
676
+ case "grab": return {
677
+ moved: r.next(),
678
+ maxDisplacement: r.next()
679
+ };
680
+ case "offset": return {
681
+ moved: r.next(),
682
+ maxDisplacement: r.next(),
683
+ degenerateRemoved: r.next(),
684
+ verticesRemoved: r.next()
685
+ };
686
+ case "shell": return {
687
+ verticesAdded: r.next(),
688
+ trianglesAdded: r.next(),
689
+ loopsClosed: r.next(),
690
+ degenerateRemoved: r.next(),
691
+ verticesRemoved: r.next()
692
+ };
693
+ case "simplify": return {
694
+ collapsed: r.next(),
695
+ trianglesRemoved: r.next(),
696
+ verticesRemoved: r.next()
697
+ };
698
+ case "subdivide": return {
699
+ verticesAdded: r.next(),
700
+ trianglesAdded: r.next()
701
+ };
346
702
  }
347
703
  }
348
704
  /** An analysis result; see `encode_analysis` in the Rust crate. */
@@ -565,17 +921,99 @@ var Scene = class {
565
921
  this.#removed.set(key, mesh);
566
922
  }
567
923
  /**
568
- * Sets a mesh's placement: world = rotation · local + translation, where
569
- * local is relative to the mesh origin. A quaternion is normalised and
570
- * converted in double precision.
924
+ * Joins `source` into `target`: copies the source's geometry into the
925
+ * target (keeping its world shape), bridges `options.loopA` to the copied
926
+ * `options.loopB`, and removes the source. The copy and the bridge undo as
927
+ * one step; the removal is permanent. A drag of the source ends first,
928
+ * recording its moves as one step, so they survive a failed join.
929
+ *
930
+ * @throws `UNKNOWN_MESH` for a removed handle; `INVALID_ARGUMENT` for one
931
+ * mesh twice or loops that cannot bridge; `STALE_LOOPS` for a loop that is
932
+ * not there. A failed join changes nothing.
933
+ */
934
+ join(target, source, options) {
935
+ const ta = this.#id(target);
936
+ const sb = this.#id(source);
937
+ const loops = [];
938
+ encodeLoopId(options.loopA, loops);
939
+ encodeLoopId(options.loopB, loops);
940
+ const maxEdges = optional(options.maxEdges, "maxEdges");
941
+ if (this.#drag?.mesh === source) this.#drag.end();
942
+ const r = call(() => this.#scene().join(ta.index, ta.generation, sb.index, sb.generation, Uint32Array.from(loops), maxEdges));
943
+ const key = `${sb.index}:${sb.generation}`;
944
+ this.#meshes.delete(key);
945
+ this.#removed.set(key, source);
946
+ return {
947
+ verticesAdded: r[0],
948
+ trianglesAdded: r[1],
949
+ bridgeTrianglesAdded: r[2]
950
+ };
951
+ }
952
+ /**
953
+ * Cuts `mesh` with a world `plane`: the `+` side
954
+ * (`signedDistance >= 0`) stays on the mesh and the `-` side is added as
955
+ * a new mesh with the same placement, so the world shape is kept.
956
+ * Cutting a closed mesh with `cap` (the default) yields two closed
957
+ * meshes, capped with flat, unfaired patches.
958
+ *
959
+ * The cut undoes in one step; the added mesh stays, like the removed
960
+ * source of {@link Scene.join}.
571
961
  *
572
- * @throws `INVALID_ARGUMENT` when the rotation is not a proper rotation or a quaternion is zero.
962
+ * @throws `UNKNOWN_MESH` for a removed handle; `INVALID_ARGUMENT` when
963
+ * the plane does not cut the mesh or a rim cannot be capped. A failed cut
964
+ * changes nothing.
965
+ */
966
+ cutByPlane(mesh, plane, options) {
967
+ const { index, generation } = this.#id(mesh);
968
+ const p = this.#plane(plane);
969
+ const r = call(() => this.#scene().cut_by_plane(index, generation, p, options?.cap ?? true));
970
+ return {
971
+ kept: this.#meshHandle(r[0], r[1]),
972
+ removed: this.#meshHandle(r[2], r[3]),
973
+ keptTriangles: r[4],
974
+ removedTriangles: r[5],
975
+ capTriangles: r[6]
976
+ };
977
+ }
978
+ /**
979
+ * Combines `source` into `target` in the world: the union, subtraction
980
+ * (`target` minus `source`) or intersection. Returns `target`, now
981
+ * holding the result with its placement; `source` is unchanged.
982
+ *
983
+ * Both meshes must be closed and manifold. The boolean undoes in one
984
+ * step. Combining a mesh with itself is a no-op for `"union"` and
985
+ * `"intersect"`, and clears it for `"subtract"`.
986
+ *
987
+ * @throws `UNKNOWN_MESH` for a removed handle; `INVALID_ARGUMENT` for an
988
+ * input that is not closed, manifold and consistently wound. A failed
989
+ * boolean changes nothing.
990
+ */
991
+ boolean(target, source, op) {
992
+ const ta = this.#id(target);
993
+ const sb = this.#id(source);
994
+ const code = codeIn({
995
+ union: 0,
996
+ subtract: 1,
997
+ intersect: 2
998
+ }, op, "op");
999
+ const r = call(() => this.#scene().boolean(ta.index, ta.generation, sb.index, sb.generation, code));
1000
+ return this.#meshHandle(r[0], r[1]);
1001
+ }
1002
+ /**
1003
+ * Sets a mesh's placement: world = rotation · (scale · local) + translation,
1004
+ * where local is relative to the mesh origin. A quaternion is normalised
1005
+ * and converted in double precision. The scale defaults to `[1, 1, 1]`
1006
+ * (rigid) and must be 3 finite positive numbers.
1007
+ *
1008
+ * @throws `INVALID_ARGUMENT` when the rotation is not a proper rotation, the
1009
+ * scale is not 3 finite positive numbers, or a quaternion is zero.
573
1010
  */
574
1011
  setPlacement(mesh, placement) {
575
1012
  const { index, generation } = this.#id(mesh);
576
1013
  const r = Float64Array.from(rotationMatrix(placement.rotation));
577
1014
  const t = Float64Array.from(placement.translation);
578
- call(() => this.#scene().set_placement(index, generation, r, t));
1015
+ const s = placement.scale === void 0 ? void 0 : Float64Array.from(placement.scale);
1016
+ call(() => this.#scene().set_placement(index, generation, r, t, s));
579
1017
  }
580
1018
  /** A mesh's placement, with the rotation as a row-major matrix. */
581
1019
  placement(mesh) {
@@ -587,13 +1025,19 @@ var Scene = class {
587
1025
  p[9],
588
1026
  p[10],
589
1027
  p[11]
1028
+ ],
1029
+ scale: [
1030
+ p[12],
1031
+ p[13],
1032
+ p[14]
590
1033
  ]
591
1034
  };
592
1035
  }
593
1036
  /**
594
1037
  * The column-major world matrix of the local render coordinates,
595
1038
  * `placement · translate(origin)`, ready for renderers (convert to `f32`
596
- * yourself).
1039
+ * yourself). The rotation columns carry the scale; normals need the
1040
+ * inverse-transpose (three.js' normal matrix does this from the matrix).
597
1041
  */
598
1042
  matrix4(mesh) {
599
1043
  const { index, generation } = this.#id(mesh);
@@ -857,13 +1301,24 @@ var Scene = class {
857
1301
  t[1],
858
1302
  t[2]
859
1303
  ].every(Number.isFinite)) throw invalid("translation must be 3 finite numbers");
1304
+ const s = placement.scale;
1305
+ if (s !== void 0 && (s.length !== 3 || ![
1306
+ s[0],
1307
+ s[1],
1308
+ s[2]
1309
+ ].every(Number.isFinite))) throw invalid("scale must be 3 finite numbers");
860
1310
  pending = {
861
1311
  rotation,
862
1312
  translation: [
863
1313
  t[0],
864
1314
  t[1],
865
1315
  t[2]
866
- ]
1316
+ ],
1317
+ ...s === void 0 ? {} : { scale: [
1318
+ s[0],
1319
+ s[1],
1320
+ s[2]
1321
+ ] }
867
1322
  };
868
1323
  },
869
1324
  end: () => {
@@ -946,6 +1401,27 @@ var Scene = class {
946
1401
  const si = options.selfIntersection ?? false;
947
1402
  return decodeAnalysis(new Reader(call(() => this.#scene().analyze(index, generation, tolerance, si, maxListed))));
948
1403
  }
1404
+ /**
1405
+ * Scalar measurements of a mesh in world coordinates: its surface area
1406
+ * (always) and, when watertight, its enclosed volume. An open or
1407
+ * non-manifold mesh still gets its area; only `volume` is `null`, with
1408
+ * `volumeReason` saying why. A read-only call: it leaves every view
1409
+ * valid.
1410
+ */
1411
+ measure(mesh) {
1412
+ const { index, generation } = this.#id(mesh);
1413
+ const r = call(() => this.#peek().measure(index, generation));
1414
+ const closed = r[2] === 1;
1415
+ return {
1416
+ area: r[0],
1417
+ volume: Number.isNaN(r[1]) ? null : r[1],
1418
+ closed,
1419
+ volumeReason: closed ? null : r[3] === 2 ? "non-manifold" : "open",
1420
+ boundaryLoops: r[4],
1421
+ nonManifoldEdges: r[5],
1422
+ orientationConflicts: r[6]
1423
+ };
1424
+ }
949
1425
  repair(mesh, op) {
950
1426
  const { index, generation } = this.#id(mesh);
951
1427
  const ops = Array.isArray(op) ? op : [op];
@@ -1049,6 +1525,154 @@ var Scene = class {
1049
1525
  return out;
1050
1526
  }
1051
1527
  /**
1528
+ * Selects vertices of a mesh: a sphere brush, a lasso polygon, or the
1529
+ * connected component holding a seed vertex. The result is a snapshot
1530
+ * (ascending ids plus the mesh's content stamp); any edit, repair, compact,
1531
+ * undo or redo of the mesh invalidates it, and the selection methods then
1532
+ * throw `STALE_SELECTION` instead of silently remapping ids. Placement
1533
+ * changes keep selections valid. Only vertices a live triangle uses can
1534
+ * be selected (dead or never-used slots never are).
1535
+ *
1536
+ * @throws `UNKNOWN_MESH` for a removed mesh; `INVALID_ARGUMENT` for a bad
1537
+ * spec; `INVALID_INDEX` for an out-of-range component seed.
1538
+ */
1539
+ select(mesh, spec) {
1540
+ checkSpec(spec);
1541
+ this.#id(mesh);
1542
+ const views = this.views(mesh);
1543
+ const slots = this.vertexSlots(mesh);
1544
+ const positions = views.positions().slice();
1545
+ const topology = buildTopology(views.indices().slice(), slots);
1546
+ let ids;
1547
+ switch (spec.kind) {
1548
+ case "brush": {
1549
+ let center = spec.center;
1550
+ let radius = spec.radius;
1551
+ if (spec.space === "world") {
1552
+ center = transformPoint(invertMatrix4(this.matrix4(mesh)), spec.center);
1553
+ radius = spec.radius / meanStretch(this.matrix4(mesh));
1554
+ }
1555
+ ids = brushPick(positions, topology.used, center, radius);
1556
+ break;
1557
+ }
1558
+ case "lasso":
1559
+ ids = lassoPick(positions, topology.used, spec.polygon, spec.axis ?? "xy");
1560
+ break;
1561
+ case "component": ids = componentOf(topology, spec.seed);
1562
+ }
1563
+ return {
1564
+ mesh,
1565
+ vertices: Uint32Array.from(ids),
1566
+ stamp: meshAdds(this).stamp(mesh)
1567
+ };
1568
+ }
1569
+ /**
1570
+ * Whether `sel` is still usable: its mesh is alive and no topology edit
1571
+ * landed since it was taken. Never throws.
1572
+ */
1573
+ selectionAlive(sel) {
1574
+ try {
1575
+ if (sel === null || typeof sel !== "object" || !(sel.vertices instanceof Uint32Array)) return false;
1576
+ this.#id(sel.mesh);
1577
+ return Number.isInteger(sel.stamp) && sel.stamp === meshAdds(this).stamp(sel.mesh);
1578
+ } catch {
1579
+ return false;
1580
+ }
1581
+ }
1582
+ /** Reads the mesh's topology and checks `sel` against its stamp. */
1583
+ #selectionTopology(sel) {
1584
+ this.#id(sel.mesh);
1585
+ checkSelection(sel, sel.mesh, meshAdds(this).stamp(sel.mesh));
1586
+ return buildTopology(this.views(sel.mesh).indices().slice(), this.vertexSlots(sel.mesh));
1587
+ }
1588
+ /**
1589
+ * Grows a selection by `steps` rings of edge neighbours (staying on live
1590
+ * vertices). The result carries the current stamp, so a fresh selection
1591
+ * can be grown right after an edit. `steps` 0 returns an equal copy.
1592
+ *
1593
+ * @throws `STALE_SELECTION` when the mesh changed since the selection;
1594
+ * `UNKNOWN_MESH` for a removed mesh.
1595
+ */
1596
+ selectGrow(sel, steps = 1) {
1597
+ const topology = this.#selectionTopology(sel);
1598
+ return {
1599
+ mesh: sel.mesh,
1600
+ vertices: Uint32Array.from(growIds(topology, sel.vertices, steps)),
1601
+ stamp: meshAdds(this).stamp(sel.mesh)
1602
+ };
1603
+ }
1604
+ /**
1605
+ * Shrinks a selection by `steps` rings, dropping the members with a live
1606
+ * neighbour outside the set. Interior selections (no boundary) survive
1607
+ * unchanged; shrinking past the core gives an empty (still valid)
1608
+ * selection.
1609
+ *
1610
+ * @throws `STALE_SELECTION` when the mesh changed since the selection;
1611
+ * `UNKNOWN_MESH` for a removed mesh.
1612
+ */
1613
+ selectShrink(sel, steps = 1) {
1614
+ const topology = this.#selectionTopology(sel);
1615
+ return {
1616
+ mesh: sel.mesh,
1617
+ vertices: Uint32Array.from(shrinkIds(topology, sel.vertices, steps)),
1618
+ stamp: meshAdds(this).stamp(sel.mesh)
1619
+ };
1620
+ }
1621
+ /**
1622
+ * Inverts a selection: every live vertex outside it. The result carries
1623
+ * the current stamp.
1624
+ *
1625
+ * @throws `STALE_SELECTION` when the mesh changed since the selection;
1626
+ * `UNKNOWN_MESH` for a removed mesh.
1627
+ */
1628
+ selectInvert(sel) {
1629
+ const topology = this.#selectionTopology(sel);
1630
+ const inSet = new Set(sel.vertices);
1631
+ const out = [];
1632
+ for (let v = 0; v < topology.used.length; v++) if (topology.used[v] === 1 && !inSet.has(v)) out.push(v);
1633
+ return {
1634
+ mesh: sel.mesh,
1635
+ vertices: Uint32Array.from(out),
1636
+ stamp: meshAdds(this).stamp(sel.mesh)
1637
+ };
1638
+ }
1639
+ /**
1640
+ * Isolates a selection: removes the triangles fully outside it (no
1641
+ * selected vertex), one undo step. Boundary triangles with at least one
1642
+ * selected vertex stay. An empty selection selects nothing to keep and
1643
+ * throws `INVALID_ARGUMENT` rather than clearing the mesh.
1644
+ *
1645
+ * @throws `STALE_SELECTION` when the mesh changed since the selection;
1646
+ * `UNKNOWN_MESH` for a removed mesh.
1647
+ */
1648
+ isolate(mesh, sel) {
1649
+ this.#id(mesh);
1650
+ checkSelection(sel, mesh, meshAdds(this).stamp(mesh));
1651
+ if (sel.vertices.length === 0) throw invalid("isolate needs a non-empty selection");
1652
+ const inSet = new Set(sel.vertices);
1653
+ const indices = this.views(mesh).indices().slice();
1654
+ const drop = [];
1655
+ for (let t = 0; t * 3 + 2 < indices.length; t++) {
1656
+ const a = indices[t * 3];
1657
+ const b = indices[t * 3 + 1];
1658
+ const c = indices[t * 3 + 2];
1659
+ if (a === 0 && b === 0 && c === 0) continue;
1660
+ if (!inSet.has(a) && !inSet.has(b) && !inSet.has(c)) drop.push(t);
1661
+ }
1662
+ if (drop.length > 0) this.edit(mesh, [{
1663
+ op: "removeTriangles",
1664
+ triangles: Uint32Array.from(drop)
1665
+ }]);
1666
+ return {
1667
+ removedTriangles: drop.length,
1668
+ selection: {
1669
+ mesh,
1670
+ vertices: sel.vertices.slice(),
1671
+ stamp: meshAdds(this).stamp(mesh)
1672
+ }
1673
+ };
1674
+ }
1675
+ /**
1052
1676
  * Adds a signed distance field: one value per vertex slot of `on`, the
1053
1677
  * distance to `to` as placed (negative inside), `Infinity` beyond
1054
1678
  * `maxDistance`, NaN for dead or unused vertices. With several meshes in
@@ -1061,6 +1685,37 @@ var Scene = class {
1061
1685
  addDistanceField(desc) {
1062
1686
  return this.#addField(desc, false);
1063
1687
  }
1688
+ /**
1689
+ * Adds a per-vertex wall-thickness field: one value per vertex slot of
1690
+ * `on`, the distance to the opposite surface along the vertex's smooth
1691
+ * normal, in world units. Open sheets and rims read NaN, hits beyond
1692
+ * `maxDistance` read `Infinity`. It follows edits and placements by the
1693
+ * mesh modes, like normals, and colours like any field.
1694
+ *
1695
+ * @throws `INVALID_ARGUMENT` when `maxDistance` is negative or NaN, or the name is taken.
1696
+ */
1697
+ addThicknessField(desc) {
1698
+ const s = this.#scene();
1699
+ const ids = this.#flatIds([desc.on]);
1700
+ if (typeof desc.name !== "string") throw invalid("name must be a string");
1701
+ const raw = call(() => s.add_thickness_field(desc.name, ids, desc.maxDistance));
1702
+ return this.#rawHandle(this.#fields, () => new FieldHandle(), raw);
1703
+ }
1704
+ /**
1705
+ * Adds a per-vertex curvature field: one value per vertex slot of `on`,
1706
+ * the signed mean curvature there, in 1/world-units (positive on domes,
1707
+ * negative in bowls, zero on flats). It follows edits and placements by
1708
+ * the mesh modes, like normals, and colours like any field.
1709
+ *
1710
+ * @throws `INVALID_ARGUMENT` when the name is taken.
1711
+ */
1712
+ addCurvatureField(desc) {
1713
+ const s = this.#scene();
1714
+ const ids = this.#flatIds([desc.on]);
1715
+ if (typeof desc.name !== "string") throw invalid("name must be a string");
1716
+ const raw = call(() => s.add_curvature_field(desc.name, ids));
1717
+ return this.#rawHandle(this.#fields, () => new FieldHandle(), raw);
1718
+ }
1064
1719
  /** `addDistanceField`, or with `deferred` its internal variant that computes nothing yet. */
1065
1720
  #addField(desc, deferred) {
1066
1721
  const s = this.#scene();
@@ -1338,5 +1993,4 @@ var Scene = class {
1338
1993
  };
1339
1994
  }
1340
1995
  };
1341
- //#endregion
1342
1996
  export { useWasmScene as i, fieldSteps as n, meshAdds as r, Scene as t };