fast-mesh-wasm 0.5.0 → 0.5.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/README.md +91 -3
- package/dist/{api-BUy7FRx7.js → api-4kz45-Mq.js} +2 -3
- package/dist/{client-CE6RvmvB.js → client-BIPPznBZ.js} +108 -24
- package/dist/{client-pv0Fwvl4.d.ts → client-Uo7hHvD8.d.ts} +25 -11
- package/dist/{colormap-DxGMYVfs.js → colormap-Dr5V3bvs.js} +1 -2
- package/dist/{core-Cke_kZ2e.js → core-4OcWKu3r.js} +2 -2
- package/dist/{core-D2X9GPfK.d.ts → core-CJ9D6U5A.d.ts} +1 -3
- package/dist/{engine-DLQAL9GT.js → engine-BS8i5U8J.js} +41 -11
- package/dist/io.d.ts +306 -0
- package/dist/io.js +1479 -0
- package/dist/{mesh-RtPVmPQy.d.ts → mesh-CqAIuOxU.d.ts} +15 -6
- package/dist/{mesh-zj7-wJBw.js → mesh-hPWBQPaN.js} +31 -8
- package/dist/node.d.ts +3 -5
- package/dist/node.js +4 -5
- package/dist/{protocol-z-jeCUGO.d.ts → protocol-BHAI0xiA.d.ts} +25 -7
- package/dist/{protocol-DkhGNP0h.js → protocol-Ci9LLiwO.js} +1 -2
- package/dist/r3f.d.ts +22 -6
- package/dist/r3f.js +28 -19
- package/dist/{react-CTADh3NN.js → react-DzrtrzZy.js} +4 -6
- package/dist/react.d.ts +4 -6
- package/dist/react.js +1 -1
- package/dist/{scene-like-YcY08oii.d.ts → scene-like-CavrKbpQ.d.ts} +590 -47
- package/dist/{scene-DyYdEQdy.js → scene-rsfM06St.js} +711 -21
- package/dist/{snapshot-Cf6NgDl7.js → snapshot-BkbGPspK.js} +1 -2
- package/dist/{store-C5nzCpxy.d.ts → store-CZwml1K3.d.ts} +5 -10
- package/dist/{store-Z6A2HPih.js → store-m9l0Al-m.js} +9 -12
- package/dist/store.d.ts +2 -4
- package/dist/store.js +4 -5
- package/dist/three.d.ts +2 -6
- package/dist/three.js +4 -6
- package/dist/{views-Dh3aWc2q.js → views-CdOArjcB.js} +6 -8
- package/dist/wasm/fast_mesh_wasm.d.ts +113 -8
- package/dist/wasm/fast_mesh_wasm.js +267 -8
- package/dist/wasm/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm/fast_mesh_wasm_bg.wasm.d.ts +13 -1
- package/dist/wasm-threads/fast_mesh_wasm.d.ts +113 -8
- package/dist/wasm-threads/fast_mesh_wasm.js +268 -9
- package/dist/wasm-threads/fast_mesh_wasm_bg.wasm +0 -0
- package/dist/wasm-threads/fast_mesh_wasm_bg.wasm.d.ts +13 -1
- package/dist/web.d.ts +3 -5
- package/dist/web.js +4 -5
- package/dist/worker-engine.d.ts +1 -7
- package/dist/worker-engine.js +1 -1
- package/dist/worker-entry.js +2 -3
- package/dist/worker.d.ts +2 -2
- package/dist/worker.js +1 -1
- 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-
|
|
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-
|
|
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-CdOArjcB.js";
|
|
3
3
|
import { WasmScene } from "./wasm/fast_mesh_wasm.js";
|
|
4
|
-
|
|
4
|
+
// src/changes.ts
|
|
5
5
|
const ADDED = 1;
|
|
6
6
|
const REMOVED = 2;
|
|
7
7
|
const PLACEMENT = 4;
|
|
@@ -10,6 +10,8 @@ const POSITIONS_ALL = 16;
|
|
|
10
10
|
const NORMALS_ALL = 32;
|
|
11
11
|
const COLORS_ALL = 64;
|
|
12
12
|
const INDICES_ALL = 128;
|
|
13
|
+
const UVS = 256;
|
|
14
|
+
const UVS_ALL = 512;
|
|
13
15
|
/** Decodes `[record_count, record...]`. */
|
|
14
16
|
function decodeChanges(msg) {
|
|
15
17
|
let at = 0;
|
|
@@ -49,6 +51,7 @@ function decodeChanges(msg) {
|
|
|
49
51
|
normals: ranges((flags & NORMALS_ALL) !== 0),
|
|
50
52
|
colors: ranges((flags & COLORS_ALL) !== 0),
|
|
51
53
|
indices: ranges((flags & INDICES_ALL) !== 0),
|
|
54
|
+
...(flags & UVS) !== 0 ? { uvs: ranges((flags & UVS_ALL) !== 0) } : {},
|
|
52
55
|
fields: ids(),
|
|
53
56
|
sections: ids()
|
|
54
57
|
});
|
|
@@ -56,8 +59,246 @@ function decodeChanges(msg) {
|
|
|
56
59
|
if (at !== msg.length) throw new Error("fast-mesh-wasm: trailing words in change message");
|
|
57
60
|
return out;
|
|
58
61
|
}
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
// src/selection.ts
|
|
63
|
+
function invalid$1(message) {
|
|
64
|
+
return new FastMeshError("INVALID_ARGUMENT", message);
|
|
65
|
+
}
|
|
66
|
+
function stale(message) {
|
|
67
|
+
return new FastMeshError("STALE_SELECTION", message);
|
|
68
|
+
}
|
|
69
|
+
/** Checks a spec's shape; the Scene methods check the mesh and the stamp. */
|
|
70
|
+
function checkSpec(spec) {
|
|
71
|
+
if (spec === null || typeof spec !== "object") throw invalid$1("spec must be an object");
|
|
72
|
+
switch (spec.kind) {
|
|
73
|
+
case "brush": {
|
|
74
|
+
const c = spec.center;
|
|
75
|
+
if (c?.length !== 3 || ![
|
|
76
|
+
c[0],
|
|
77
|
+
c[1],
|
|
78
|
+
c[2]
|
|
79
|
+
].every(Number.isFinite)) throw invalid$1("brush center must be 3 finite numbers");
|
|
80
|
+
if (typeof spec.radius !== "number" || !(spec.radius >= 0)) throw invalid$1("brush radius must be a number, zero or greater");
|
|
81
|
+
if (spec.space !== void 0 && spec.space !== "local" && spec.space !== "world") throw invalid$1("brush space must be \"local\" or \"world\"");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
case "lasso": {
|
|
85
|
+
const p = spec.polygon;
|
|
86
|
+
const n = Array.isArray(p) ? p.length : p instanceof Float32Array ? p.length : -1;
|
|
87
|
+
if (n < 6 || n % 2 !== 0) throw invalid$1("lasso polygon needs at least 3 [x, y] corners");
|
|
88
|
+
const pts = p;
|
|
89
|
+
for (let k = 0; k < n; k++) if (!Number.isFinite(pts[k])) throw invalid$1("lasso polygon must hold finite numbers");
|
|
90
|
+
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\"");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
case "component":
|
|
94
|
+
if (!Number.isInteger(spec.seed) || spec.seed < 0 || spec.seed >= 2 ** 32) throw new FastMeshError("INVALID_INDEX", "component seed is not a vertex id");
|
|
95
|
+
return;
|
|
96
|
+
default: throw invalid$1(`unknown select kind "${spec.kind}"`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Builds adjacency from index triples, skipping removed slots (`0, 0, 0`).
|
|
101
|
+
* `slots` is the vertex slot count.
|
|
102
|
+
*/
|
|
103
|
+
function buildTopology(indices, slots) {
|
|
104
|
+
const sets = Array.from({ length: slots }, () => /* @__PURE__ */ new Set());
|
|
105
|
+
const used = new Uint8Array(slots);
|
|
106
|
+
const live = [];
|
|
107
|
+
const link = (a, b) => {
|
|
108
|
+
if (a !== b && a < slots && b < slots) {
|
|
109
|
+
sets[a].add(b);
|
|
110
|
+
sets[b].add(a);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
for (let t = 0; t * 3 + 2 < indices.length; t++) {
|
|
114
|
+
const a = indices[t * 3];
|
|
115
|
+
const b = indices[t * 3 + 1];
|
|
116
|
+
const c = indices[t * 3 + 2];
|
|
117
|
+
if (a === 0 && b === 0 && c === 0) continue;
|
|
118
|
+
if (a >= slots || b >= slots || c >= slots) continue;
|
|
119
|
+
live.push(t);
|
|
120
|
+
used[a] = 1;
|
|
121
|
+
used[b] = 1;
|
|
122
|
+
used[c] = 1;
|
|
123
|
+
link(a, b);
|
|
124
|
+
link(b, c);
|
|
125
|
+
link(c, a);
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
neighbors: sets.map((s) => [...s].sort((x, y) => x - y)),
|
|
129
|
+
used,
|
|
130
|
+
liveTriangles: Uint32Array.from(live)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** Every used vertex within `radius` of `center` (squared distances). */
|
|
134
|
+
function brushPick(positions, used, center, radius) {
|
|
135
|
+
const out = [];
|
|
136
|
+
const r2 = radius * radius;
|
|
137
|
+
for (let v = 0; v < used.length; v++) {
|
|
138
|
+
if (used[v] === 0) continue;
|
|
139
|
+
const dx = positions[v * 3] - center[0];
|
|
140
|
+
const dy = positions[v * 3 + 1] - center[1];
|
|
141
|
+
const dz = positions[v * 3 + 2] - center[2];
|
|
142
|
+
if (dx * dx + dy * dy + dz * dz <= r2) out.push(v);
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
/** Even-odd point-in-polygon over flat `[x0, y0, ...]`. */
|
|
147
|
+
function insidePolygon(polygon, x, y) {
|
|
148
|
+
let inside = false;
|
|
149
|
+
const n = polygon.length / 2;
|
|
150
|
+
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
151
|
+
const xi = polygon[i * 2];
|
|
152
|
+
const yi = polygon[i * 2 + 1];
|
|
153
|
+
const xj = polygon[j * 2];
|
|
154
|
+
const yj = polygon[j * 2 + 1];
|
|
155
|
+
if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) inside = !inside;
|
|
156
|
+
}
|
|
157
|
+
return inside;
|
|
158
|
+
}
|
|
159
|
+
/** Every used vertex whose `axis` projection falls inside `polygon`. */
|
|
160
|
+
function lassoPick(positions, used, polygon, axis) {
|
|
161
|
+
const pick = axis === "xy" ? [0, 1] : axis === "xz" ? [0, 2] : [1, 2];
|
|
162
|
+
const out = [];
|
|
163
|
+
for (let v = 0; v < used.length; v++) {
|
|
164
|
+
if (used[v] === 0) continue;
|
|
165
|
+
if (insidePolygon(polygon, positions[v * 3 + pick[0]], positions[v * 3 + pick[1]])) out.push(v);
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* The connected component holding `seed` (over triangle edges). Throws
|
|
171
|
+
* `INVALID_ARGUMENT` when the seed is out of range or not alive.
|
|
172
|
+
*/
|
|
173
|
+
function componentOf(topology, seed) {
|
|
174
|
+
if (!Number.isInteger(seed) || seed < 0 || seed >= topology.used.length) throw new FastMeshError("INVALID_INDEX", "component seed is not a vertex id");
|
|
175
|
+
if (topology.used[seed] === 0) throw invalid$1("component seed is not on a live triangle");
|
|
176
|
+
const seen = new Uint8Array(topology.used.length);
|
|
177
|
+
const queue = [seed];
|
|
178
|
+
seen[seed] = 1;
|
|
179
|
+
while (queue.length > 0) {
|
|
180
|
+
const v = queue.pop();
|
|
181
|
+
for (const w of topology.neighbors[v]) if (seen[w] === 0) {
|
|
182
|
+
seen[w] = 1;
|
|
183
|
+
queue.push(w);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const out = [];
|
|
187
|
+
for (let v = 0; v < seen.length; v++) if (seen[v] === 1) out.push(v);
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Grows `ids` by `steps` rings of edge neighbours (staying on live
|
|
192
|
+
* vertices). `steps` 0 returns `ids`.
|
|
193
|
+
*/
|
|
194
|
+
function growIds(topology, ids, steps) {
|
|
195
|
+
if (!Number.isInteger(steps) || steps < 0) throw invalid$1("steps must be a non-negative integer");
|
|
196
|
+
const inSet = new Uint8Array(topology.used.length);
|
|
197
|
+
for (const v of ids) if (v < inSet.length) inSet[v] = 1;
|
|
198
|
+
let frontier = [...ids].filter((v) => v < inSet.length);
|
|
199
|
+
for (let s = 0; s < steps; s++) {
|
|
200
|
+
const next = [];
|
|
201
|
+
for (const v of frontier) for (const w of topology.neighbors[v]) if (inSet[w] === 0) {
|
|
202
|
+
inSet[w] = 1;
|
|
203
|
+
next.push(w);
|
|
204
|
+
}
|
|
205
|
+
if (next.length === 0) break;
|
|
206
|
+
frontier = next;
|
|
207
|
+
}
|
|
208
|
+
const out = [];
|
|
209
|
+
for (let v = 0; v < inSet.length; v++) if (inSet[v] === 1) out.push(v);
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Shrinks `ids` by `steps` rings: each step drops the members with a live
|
|
214
|
+
* neighbour outside the set. Interior selections (no boundary) survive
|
|
215
|
+
* unchanged.
|
|
216
|
+
*/
|
|
217
|
+
function shrinkIds(topology, ids, steps) {
|
|
218
|
+
if (!Number.isInteger(steps) || steps < 0) throw invalid$1("steps must be a non-negative integer");
|
|
219
|
+
let current = new Set([...ids].filter((v) => v < topology.used.length));
|
|
220
|
+
for (let s = 0; s < steps; s++) {
|
|
221
|
+
const peel = [];
|
|
222
|
+
for (const v of current) if (topology.neighbors[v].some((w) => !current.has(w))) peel.push(v);
|
|
223
|
+
if (peel.length === 0) break;
|
|
224
|
+
for (const v of peel) current.delete(v);
|
|
225
|
+
}
|
|
226
|
+
return [...current].sort((a, b) => a - b);
|
|
227
|
+
}
|
|
228
|
+
/** Throws `STALE_SELECTION` when `sel` is not for `mesh` or predates its stamp. */
|
|
229
|
+
function checkSelection(sel, mesh, stamp) {
|
|
230
|
+
if (sel === null || typeof sel !== "object" || sel.mesh !== mesh) throw invalid$1("the selection is not for this mesh");
|
|
231
|
+
if (!Number.isInteger(sel.stamp) || sel.stamp !== stamp) throw stale("the selection predates the mesh's latest topology edit; reselect it");
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The inverse of a column-major 4x4 (a placement's `matrix4`), for
|
|
235
|
+
* world-space brushes. Throws `INVALID_ARGUMENT` when not invertible.
|
|
236
|
+
*/
|
|
237
|
+
function invertMatrix4(m) {
|
|
238
|
+
const inv = /* @__PURE__ */ new Float64Array(16);
|
|
239
|
+
const a00 = m[0];
|
|
240
|
+
const a01 = m[1];
|
|
241
|
+
const a02 = m[2];
|
|
242
|
+
const a03 = m[3];
|
|
243
|
+
const a10 = m[4];
|
|
244
|
+
const a11 = m[5];
|
|
245
|
+
const a12 = m[6];
|
|
246
|
+
const a13 = m[7];
|
|
247
|
+
const a20 = m[8];
|
|
248
|
+
const a21 = m[9];
|
|
249
|
+
const a22 = m[10];
|
|
250
|
+
const a23 = m[11];
|
|
251
|
+
const a30 = m[12];
|
|
252
|
+
const a31 = m[13];
|
|
253
|
+
const a32 = m[14];
|
|
254
|
+
const a33 = m[15];
|
|
255
|
+
const b00 = a00 * a11 - a01 * a10;
|
|
256
|
+
const b01 = a00 * a12 - a02 * a10;
|
|
257
|
+
const b02 = a00 * a13 - a03 * a10;
|
|
258
|
+
const b03 = a01 * a12 - a02 * a11;
|
|
259
|
+
const b04 = a01 * a13 - a03 * a11;
|
|
260
|
+
const b05 = a02 * a13 - a03 * a12;
|
|
261
|
+
const b06 = a20 * a31 - a21 * a30;
|
|
262
|
+
const b07 = a20 * a32 - a22 * a30;
|
|
263
|
+
const b08 = a20 * a33 - a23 * a30;
|
|
264
|
+
const b09 = a21 * a32 - a22 * a31;
|
|
265
|
+
const b10 = a21 * a33 - a23 * a31;
|
|
266
|
+
const b11 = a22 * a33 - a23 * a32;
|
|
267
|
+
const det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
|
268
|
+
if (det === 0 || !Number.isFinite(det)) throw invalid$1("the mesh placement is not invertible");
|
|
269
|
+
const s = 1 / det;
|
|
270
|
+
inv[0] = (a11 * b11 - a12 * b10 + a13 * b09) * s;
|
|
271
|
+
inv[1] = (a02 * b10 - a01 * b11 - a03 * b09) * s;
|
|
272
|
+
inv[2] = (a31 * b05 - a32 * b04 + a33 * b03) * s;
|
|
273
|
+
inv[3] = (a22 * b04 - a21 * b05 - a23 * b03) * s;
|
|
274
|
+
inv[4] = (a12 * b08 - a10 * b11 - a13 * b07) * s;
|
|
275
|
+
inv[5] = (a00 * b11 - a02 * b08 + a03 * b07) * s;
|
|
276
|
+
inv[6] = (a32 * b02 - a30 * b05 - a33 * b01) * s;
|
|
277
|
+
inv[7] = (a20 * b05 - a22 * b02 + a23 * b01) * s;
|
|
278
|
+
inv[8] = (a10 * b10 - a11 * b08 + a13 * b06) * s;
|
|
279
|
+
inv[9] = (a01 * b08 - a00 * b10 - a03 * b06) * s;
|
|
280
|
+
inv[10] = (a30 * b04 - a31 * b02 + a33 * b00) * s;
|
|
281
|
+
inv[11] = (a21 * b02 - a20 * b04 - a23 * b00) * s;
|
|
282
|
+
inv[12] = (a11 * b07 - a10 * b09 - a12 * b06) * s;
|
|
283
|
+
inv[13] = (a00 * b09 - a01 * b07 + a02 * b06) * s;
|
|
284
|
+
inv[14] = (a31 * b01 - a30 * b03 - a32 * b00) * s;
|
|
285
|
+
inv[15] = (a20 * b03 - a21 * b01 + a22 * b00) * s;
|
|
286
|
+
return inv;
|
|
287
|
+
}
|
|
288
|
+
/** Applies a column-major 4x4 to a point. */
|
|
289
|
+
function transformPoint(m, p) {
|
|
290
|
+
const x = p[0];
|
|
291
|
+
const y = p[1];
|
|
292
|
+
const z = p[2];
|
|
293
|
+
const w = m[3] * x + m[7] * y + m[11] * z + m[15];
|
|
294
|
+
if (w === 0 || !Number.isFinite(w)) throw invalid$1("the mesh placement does not map the brush centre");
|
|
295
|
+
return [
|
|
296
|
+
(m[0] * x + m[4] * y + m[8] * z + m[12]) / w,
|
|
297
|
+
(m[1] * x + m[5] * y + m[9] * z + m[13]) / w,
|
|
298
|
+
(m[2] * x + m[6] * y + m[10] * z + m[14]) / w
|
|
299
|
+
];
|
|
300
|
+
}
|
|
301
|
+
// src/scene.ts
|
|
61
302
|
/**
|
|
62
303
|
* The `WasmScene` class new scenes wrap: the single-threaded glue's, unless
|
|
63
304
|
* the Worker entry started the threaded glue ({@link useWasmScene}).
|
|
@@ -113,13 +354,25 @@ function vec3At(a, at) {
|
|
|
113
354
|
function invalid(message) {
|
|
114
355
|
return new FastMeshError("INVALID_ARGUMENT", message);
|
|
115
356
|
}
|
|
357
|
+
/**
|
|
358
|
+
* The mean stretch of a column-major 4x4's linear part: the cube root of
|
|
359
|
+
* its determinant. World-space brush radii divide by it, exact for rigid
|
|
360
|
+
* placements and a documented approximation under non-uniform scale.
|
|
361
|
+
*/
|
|
362
|
+
function meanStretch(m) {
|
|
363
|
+
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]);
|
|
364
|
+
const s = Math.cbrt(Math.abs(det));
|
|
365
|
+
if (s === 0 || !Number.isFinite(s)) throw invalid("the mesh placement is not invertible");
|
|
366
|
+
return s;
|
|
367
|
+
}
|
|
116
368
|
const U32_LIMIT = 2 ** 32;
|
|
117
369
|
const EDIT_CODES = {
|
|
118
370
|
setPositions: 0,
|
|
119
371
|
addVertices: 1,
|
|
120
372
|
addTriangles: 2,
|
|
121
373
|
removeTriangles: 3,
|
|
122
|
-
removeVertices: 4
|
|
374
|
+
removeVertices: 4,
|
|
375
|
+
setUvs: 5
|
|
123
376
|
};
|
|
124
377
|
/** An id list as the binding's `n, r, ids..., (slot, op, i)...`; see `crate::edit`. */
|
|
125
378
|
function encodeIds(list, what, words) {
|
|
@@ -177,7 +430,16 @@ function encodeEdit(ops) {
|
|
|
177
430
|
case "removeTriangles":
|
|
178
431
|
encodeIds(o.triangles, `${what}: triangles`, words);
|
|
179
432
|
break;
|
|
180
|
-
case "removeVertices":
|
|
433
|
+
case "removeVertices":
|
|
434
|
+
encodeIds(o.vertices, `${what}: vertices`, words);
|
|
435
|
+
break;
|
|
436
|
+
case "setUvs": {
|
|
437
|
+
encodeIds(o.vertices, `${what}: vertices`, words);
|
|
438
|
+
const uvs = Float64Array.from(o.uvs ?? []);
|
|
439
|
+
if (uvs.length !== 2 * o.vertices.length) throw invalid(`${what}: uvs need 2 values per vertex`);
|
|
440
|
+
values.push(uvs);
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
181
443
|
}
|
|
182
444
|
});
|
|
183
445
|
const out = new Uint32Array(words.reduce((n, w) => n + w.length, 0));
|
|
@@ -206,13 +468,30 @@ const REPAIR_CODES = {
|
|
|
206
468
|
inflate: 5,
|
|
207
469
|
smooth: 6,
|
|
208
470
|
remesh: 7,
|
|
209
|
-
bridgeLoops: 8
|
|
471
|
+
bridgeLoops: 8,
|
|
472
|
+
grab: 9,
|
|
473
|
+
relax: 10,
|
|
474
|
+
offset: 11,
|
|
475
|
+
shell: 12,
|
|
476
|
+
simplify: 13,
|
|
477
|
+
subdivide: 14
|
|
210
478
|
};
|
|
479
|
+
/** The op code of an {@link AutoRepairRequest}; see `crate::edit` in the Rust crate. */
|
|
480
|
+
const AUTO_REPAIR_CODE = 15;
|
|
211
481
|
const FALLOFF_CODES = {
|
|
212
482
|
linear: 0,
|
|
213
483
|
smooth: 1
|
|
214
484
|
};
|
|
485
|
+
const SUBDIVIDE_METHOD_CODES = { loop: 0 };
|
|
215
486
|
/** An optional number for the binding: NaN when unset; an explicit NaN is rejected. */
|
|
487
|
+
/** The `status` codes of `WasmScene.register`, in order. */
|
|
488
|
+
const REGISTER_STATUSES = [
|
|
489
|
+
"converged",
|
|
490
|
+
"maxIterations",
|
|
491
|
+
"underconstrained",
|
|
492
|
+
"tooFewCorrespondences",
|
|
493
|
+
"diverged"
|
|
494
|
+
];
|
|
216
495
|
function optional(v, what) {
|
|
217
496
|
if (v === void 0) return NaN;
|
|
218
497
|
if (typeof v !== "number" || Number.isNaN(v)) throw invalid(`${what} must be a number`);
|
|
@@ -238,6 +517,9 @@ function encodeLoopId(id, out) {
|
|
|
238
517
|
for (const v of [minVertex, len]) if (!Number.isInteger(v) || v < 0 || v >= U32_LIMIT) throw invalid("a loop id's minVertex and len must be u32 integers");
|
|
239
518
|
out.push(minVertex, len, Number(hash & 4294967295n), Number(hash >> 32n));
|
|
240
519
|
}
|
|
520
|
+
function isAutoRepair(op) {
|
|
521
|
+
return op?.kind === "auto";
|
|
522
|
+
}
|
|
241
523
|
/** A repair op list as the binding's `(ops, loops)`; see `crate::edit` in the Rust crate. */
|
|
242
524
|
function encodeRepair(ops) {
|
|
243
525
|
const values = [];
|
|
@@ -270,17 +552,41 @@ function encodeRepair(ops) {
|
|
|
270
552
|
values.push(optional(o.radius, "radius"), codeIn(FALLOFF_CODES, o.falloff, "falloff"), optional(o.strength, "strength"));
|
|
271
553
|
break;
|
|
272
554
|
case "smooth":
|
|
555
|
+
case "relax":
|
|
273
556
|
centerOf(o.center, values);
|
|
274
557
|
values.push(optional(o.radius, "radius"), codeIn(FALLOFF_CODES, o.falloff, "falloff"), optional(o.strength, "strength"), optional(o.cycles, "cycles"));
|
|
275
558
|
break;
|
|
276
559
|
case "remesh":
|
|
277
560
|
centerOf(o.center, values);
|
|
278
|
-
values.push(optional(o.radius, "radius"), optional(o.targetLength, "targetLength"), optional(o.maxRounds, "maxRounds"));
|
|
561
|
+
values.push(optional(o.radius, "radius"), optional(o.targetLength, "targetLength"), optional(o.maxRounds, "maxRounds"), flagOf(o.splitOnly, false));
|
|
279
562
|
break;
|
|
280
563
|
case "bridgeLoops":
|
|
281
564
|
if (!Array.isArray(o.loops) || o.loops.length !== 2) throw invalid("loops must be an array of two loop ids");
|
|
282
565
|
values.push(optional(o.maxEdges, "maxEdges"));
|
|
283
566
|
for (const id of o.loops) encodeLoopId(id, loops);
|
|
567
|
+
break;
|
|
568
|
+
case "grab": {
|
|
569
|
+
centerOf(o.center, values);
|
|
570
|
+
values.push(optional(o.radius, "radius"), codeIn(FALLOFF_CODES, o.falloff, "falloff"), optional(o.strength, "strength"));
|
|
571
|
+
const d = o.direction;
|
|
572
|
+
if (d?.length !== 3 || ![
|
|
573
|
+
d[0],
|
|
574
|
+
d[1],
|
|
575
|
+
d[2]
|
|
576
|
+
].every(Number.isFinite)) throw invalid("direction must be 3 finite numbers");
|
|
577
|
+
values.push(d[0], d[1], d[2]);
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
case "offset":
|
|
581
|
+
values.push(optional(o.distance, "distance"));
|
|
582
|
+
break;
|
|
583
|
+
case "shell":
|
|
584
|
+
values.push(optional(o.thickness, "thickness"));
|
|
585
|
+
break;
|
|
586
|
+
case "simplify":
|
|
587
|
+
values.push(optional(o.targetTriangles, "targetTriangles"), flagOf(o.preserveBoundary, true));
|
|
588
|
+
break;
|
|
589
|
+
case "subdivide": values.push(o.method === void 0 ? NaN : codeIn(SUBDIVIDE_METHOD_CODES, o.method, "method"), optional(o.levels, "levels"));
|
|
284
590
|
}
|
|
285
591
|
}
|
|
286
592
|
return {
|
|
@@ -326,7 +632,11 @@ var Reader = class {
|
|
|
326
632
|
return out;
|
|
327
633
|
}
|
|
328
634
|
};
|
|
329
|
-
const SKIP_REASONS = [
|
|
635
|
+
const SKIP_REASONS = [
|
|
636
|
+
"bounds",
|
|
637
|
+
"unclean",
|
|
638
|
+
"uvSeam"
|
|
639
|
+
];
|
|
330
640
|
const FAIL_REASONS = [
|
|
331
641
|
"projectionFolds",
|
|
332
642
|
"noValidTriangulation",
|
|
@@ -381,7 +691,8 @@ function decodeReport(op, r) {
|
|
|
381
691
|
moved: r.next(),
|
|
382
692
|
maxDisplacement: r.next()
|
|
383
693
|
};
|
|
384
|
-
case "smooth":
|
|
694
|
+
case "smooth":
|
|
695
|
+
case "relax": return {
|
|
385
696
|
moved: r.next(),
|
|
386
697
|
maxDisplacement: r.next()
|
|
387
698
|
};
|
|
@@ -392,8 +703,44 @@ function decodeReport(op, r) {
|
|
|
392
703
|
rounds: r.next()
|
|
393
704
|
};
|
|
394
705
|
case "bridgeLoops": return { trianglesAdded: r.next() };
|
|
706
|
+
case "grab": return {
|
|
707
|
+
moved: r.next(),
|
|
708
|
+
maxDisplacement: r.next()
|
|
709
|
+
};
|
|
710
|
+
case "offset": return {
|
|
711
|
+
moved: r.next(),
|
|
712
|
+
maxDisplacement: r.next(),
|
|
713
|
+
degenerateRemoved: r.next(),
|
|
714
|
+
verticesRemoved: r.next()
|
|
715
|
+
};
|
|
716
|
+
case "shell": return {
|
|
717
|
+
verticesAdded: r.next(),
|
|
718
|
+
trianglesAdded: r.next(),
|
|
719
|
+
loopsClosed: r.next(),
|
|
720
|
+
degenerateRemoved: r.next(),
|
|
721
|
+
verticesRemoved: r.next()
|
|
722
|
+
};
|
|
723
|
+
case "simplify": return {
|
|
724
|
+
collapsed: r.next(),
|
|
725
|
+
trianglesRemoved: r.next(),
|
|
726
|
+
verticesRemoved: r.next()
|
|
727
|
+
};
|
|
728
|
+
case "subdivide": return {
|
|
729
|
+
verticesAdded: r.next(),
|
|
730
|
+
trianglesAdded: r.next()
|
|
731
|
+
};
|
|
395
732
|
}
|
|
396
733
|
}
|
|
734
|
+
/** An auto repair's result, stage by stage in the order they ran; see `crate::edit` in the Rust crate. */
|
|
735
|
+
function decodeAutoReport(r) {
|
|
736
|
+
const ops = Object.keys(REPAIR_CODES);
|
|
737
|
+
const stages = {};
|
|
738
|
+
while (r.at < r.data.length) {
|
|
739
|
+
const op = ops[r.next()];
|
|
740
|
+
stages[op] = decodeReport(op, r);
|
|
741
|
+
}
|
|
742
|
+
return stages;
|
|
743
|
+
}
|
|
397
744
|
/** An analysis result; see `encode_analysis` in the Rust crate. */
|
|
398
745
|
function decodeAnalysis(r) {
|
|
399
746
|
const loops = r.many(() => ({
|
|
@@ -589,7 +936,8 @@ var Scene = class {
|
|
|
589
936
|
* identity, so {@link Scene.matrix4} maps the local coordinates back to
|
|
590
937
|
* where they were given.
|
|
591
938
|
*
|
|
592
|
-
* @throws `INVALID_ARGUMENT` for bad lengths or options
|
|
939
|
+
* @throws `INVALID_ARGUMENT` for bad lengths or options (`uvs` not 2 finite
|
|
940
|
+
* values per vertex included), `INVALID_INDEX` for an index out of range.
|
|
593
941
|
*/
|
|
594
942
|
addMesh(positions, indices, options = {}) {
|
|
595
943
|
const s = this.#scene();
|
|
@@ -597,7 +945,13 @@ var Scene = class {
|
|
|
597
945
|
const origin = options.origin === void 0 ? void 0 : Float64Array.from(options.origin);
|
|
598
946
|
const p = toFloat64(positions);
|
|
599
947
|
const i = toUint32(indices);
|
|
600
|
-
const
|
|
948
|
+
const normals = options.normals ?? true;
|
|
949
|
+
if (options.uvs === void 0) {
|
|
950
|
+
const id = call(() => s.add_mesh(p, i, origin, mode, normals));
|
|
951
|
+
return this.#meshHandle(id[0], id[1]);
|
|
952
|
+
}
|
|
953
|
+
const uvs = options.uvs instanceof Float32Array ? options.uvs : Float32Array.from(options.uvs);
|
|
954
|
+
const id = call(() => s.add_mesh_with_uvs(p, i, uvs, origin, mode, normals));
|
|
601
955
|
return this.#meshHandle(id[0], id[1]);
|
|
602
956
|
}
|
|
603
957
|
/**
|
|
@@ -643,17 +997,130 @@ var Scene = class {
|
|
|
643
997
|
};
|
|
644
998
|
}
|
|
645
999
|
/**
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
1000
|
+
* Cuts `mesh` with a world `plane`: the `+` side
|
|
1001
|
+
* (`signedDistance >= 0`) stays on the mesh and the `-` side is added as
|
|
1002
|
+
* a new mesh with the same placement, so the world shape is kept.
|
|
1003
|
+
* Cutting a closed mesh with `cap` (the default) yields two closed
|
|
1004
|
+
* meshes, capped with flat, unfaired patches.
|
|
649
1005
|
*
|
|
650
|
-
*
|
|
1006
|
+
* The cut undoes in one step; the added mesh stays, like the removed
|
|
1007
|
+
* source of {@link Scene.join}.
|
|
1008
|
+
*
|
|
1009
|
+
* @throws `UNKNOWN_MESH` for a removed handle; `INVALID_ARGUMENT` when
|
|
1010
|
+
* the plane does not cut the mesh or a rim cannot be capped. A failed cut
|
|
1011
|
+
* changes nothing.
|
|
1012
|
+
*/
|
|
1013
|
+
cutByPlane(mesh, plane, options) {
|
|
1014
|
+
const { index, generation } = this.#id(mesh);
|
|
1015
|
+
const p = this.#plane(plane);
|
|
1016
|
+
const r = call(() => this.#scene().cut_by_plane(index, generation, p, options?.cap ?? true));
|
|
1017
|
+
return {
|
|
1018
|
+
kept: this.#meshHandle(r[0], r[1]),
|
|
1019
|
+
removed: this.#meshHandle(r[2], r[3]),
|
|
1020
|
+
keptTriangles: r[4],
|
|
1021
|
+
removedTriangles: r[5],
|
|
1022
|
+
capTriangles: r[6]
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Combines `source` into `target` in the world: the union, subtraction
|
|
1027
|
+
* (`target` minus `source`) or intersection. Returns `target`, now
|
|
1028
|
+
* holding the result with its placement; `source` is unchanged.
|
|
1029
|
+
*
|
|
1030
|
+
* Both meshes must be closed and manifold. The boolean undoes in one
|
|
1031
|
+
* step. Combining a mesh with itself is a no-op for `"union"` and
|
|
1032
|
+
* `"intersect"`, and clears it for `"subtract"`.
|
|
1033
|
+
*
|
|
1034
|
+
* @throws `UNKNOWN_MESH` for a removed handle; `INVALID_ARGUMENT` for an
|
|
1035
|
+
* input that is not closed, manifold and consistently wound. A failed
|
|
1036
|
+
* boolean changes nothing.
|
|
1037
|
+
*/
|
|
1038
|
+
boolean(target, source, op) {
|
|
1039
|
+
const ta = this.#id(target);
|
|
1040
|
+
const sb = this.#id(source);
|
|
1041
|
+
const code = codeIn({
|
|
1042
|
+
union: 0,
|
|
1043
|
+
subtract: 1,
|
|
1044
|
+
intersect: 2
|
|
1045
|
+
}, op, "op");
|
|
1046
|
+
const r = call(() => this.#scene().boolean(ta.index, ta.generation, sb.index, sb.generation, code));
|
|
1047
|
+
return this.#meshHandle(r[0], r[1]);
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Rigidly aligns the mesh `moving` to the mesh `fixed` by point-to-plane ICP,
|
|
1051
|
+
* with the closest points found through `fixed`'s BVH and trimmed by
|
|
1052
|
+
* `options.overlap`. A drag of either mesh ends first, recording its moves
|
|
1053
|
+
* as one step.
|
|
1054
|
+
*
|
|
1055
|
+
* **The initial guess is the current placements.** ICP finds the nearest
|
|
1056
|
+
* fit, so put the meshes roughly in place first with `setPlacement`.
|
|
1057
|
+
*
|
|
1058
|
+
* **Applying.** By default a converged result becomes `moving`'s placement
|
|
1059
|
+
* as one undo step (`applied` in the result); `apply: false` only returns
|
|
1060
|
+
* it. A result that did not converge is never applied, so a failed alignment
|
|
1061
|
+
* does not move anything; `placement` still holds the best estimate, for
|
|
1062
|
+
* `setPlacement` if it will do.
|
|
1063
|
+
*
|
|
1064
|
+
* **Scales.** Only rotation and translation are solved. `moving`'s scale is
|
|
1065
|
+
* applied to its points and kept. `fixed`'s scale, uniform or not, is
|
|
1066
|
+
* followed exactly on a copy baked through it, so each such call costs a BVH
|
|
1067
|
+
* build. All distances are in world units.
|
|
1068
|
+
*
|
|
1069
|
+
* @throws `UNKNOWN_MESH` for a removed handle; `INVALID_ARGUMENT` for one
|
|
1070
|
+
* mesh twice, a mesh without triangles or an invalid option; `STALE_BVH` for
|
|
1071
|
+
* a stale Manual `fixed`. Nothing changes on a throw.
|
|
1072
|
+
*/
|
|
1073
|
+
register(moving, fixed, options = {}) {
|
|
1074
|
+
const m = this.#id(moving);
|
|
1075
|
+
const f = this.#id(fixed);
|
|
1076
|
+
const maxIterations = optional(options.maxIterations, "maxIterations");
|
|
1077
|
+
const tolerance = optional(options.tolerance, "tolerance");
|
|
1078
|
+
const overlap = optional(options.overlap, "overlap");
|
|
1079
|
+
const maxDistance = optional(options.maxDistance, "maxDistance");
|
|
1080
|
+
const maxSamples = optional(options.maxSamples, "maxSamples");
|
|
1081
|
+
const apply = options.apply ?? true;
|
|
1082
|
+
if (this.#drag?.mesh === moving || this.#drag?.mesh === fixed) this.#drag.end();
|
|
1083
|
+
const r = call(() => this.#scene().register(m.index, m.generation, f.index, f.generation, maxIterations, tolerance, overlap, maxDistance, maxSamples, apply));
|
|
1084
|
+
const status = REGISTER_STATUSES[r[17]];
|
|
1085
|
+
return {
|
|
1086
|
+
placement: {
|
|
1087
|
+
rotation: Array.from(r.subarray(0, 9)),
|
|
1088
|
+
translation: [
|
|
1089
|
+
r[9],
|
|
1090
|
+
r[10],
|
|
1091
|
+
r[11]
|
|
1092
|
+
],
|
|
1093
|
+
scale: [
|
|
1094
|
+
r[12],
|
|
1095
|
+
r[13],
|
|
1096
|
+
r[14]
|
|
1097
|
+
]
|
|
1098
|
+
},
|
|
1099
|
+
rmse: r[15],
|
|
1100
|
+
iterations: r[16],
|
|
1101
|
+
converged: status === "converged",
|
|
1102
|
+
status,
|
|
1103
|
+
applied: r[18] === 1,
|
|
1104
|
+
samples: r[19],
|
|
1105
|
+
inliers: r[20],
|
|
1106
|
+
conditioning: r[21]
|
|
1107
|
+
};
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Sets a mesh's placement: world = rotation · (scale · local) + translation,
|
|
1111
|
+
* where local is relative to the mesh origin. A quaternion is normalised
|
|
1112
|
+
* and converted in double precision. The scale defaults to `[1, 1, 1]`
|
|
1113
|
+
* (rigid) and must be 3 finite positive numbers.
|
|
1114
|
+
*
|
|
1115
|
+
* @throws `INVALID_ARGUMENT` when the rotation is not a proper rotation, the
|
|
1116
|
+
* scale is not 3 finite positive numbers, or a quaternion is zero.
|
|
651
1117
|
*/
|
|
652
1118
|
setPlacement(mesh, placement) {
|
|
653
1119
|
const { index, generation } = this.#id(mesh);
|
|
654
1120
|
const r = Float64Array.from(rotationMatrix(placement.rotation));
|
|
655
1121
|
const t = Float64Array.from(placement.translation);
|
|
656
|
-
|
|
1122
|
+
const s = placement.scale === void 0 ? void 0 : Float64Array.from(placement.scale);
|
|
1123
|
+
call(() => this.#scene().set_placement(index, generation, r, t, s));
|
|
657
1124
|
}
|
|
658
1125
|
/** A mesh's placement, with the rotation as a row-major matrix. */
|
|
659
1126
|
placement(mesh) {
|
|
@@ -665,13 +1132,19 @@ var Scene = class {
|
|
|
665
1132
|
p[9],
|
|
666
1133
|
p[10],
|
|
667
1134
|
p[11]
|
|
1135
|
+
],
|
|
1136
|
+
scale: [
|
|
1137
|
+
p[12],
|
|
1138
|
+
p[13],
|
|
1139
|
+
p[14]
|
|
668
1140
|
]
|
|
669
1141
|
};
|
|
670
1142
|
}
|
|
671
1143
|
/**
|
|
672
1144
|
* The column-major world matrix of the local render coordinates,
|
|
673
1145
|
* `placement · translate(origin)`, ready for renderers (convert to `f32`
|
|
674
|
-
* yourself).
|
|
1146
|
+
* yourself). The rotation columns carry the scale; normals need the
|
|
1147
|
+
* inverse-transpose (three.js' normal matrix does this from the matrix).
|
|
675
1148
|
*/
|
|
676
1149
|
matrix4(mesh) {
|
|
677
1150
|
const { index, generation } = this.#id(mesh);
|
|
@@ -783,6 +1256,7 @@ var Scene = class {
|
|
|
783
1256
|
normals: c.normals,
|
|
784
1257
|
colors: c.colors,
|
|
785
1258
|
indices: c.indices,
|
|
1259
|
+
...c.uvs === void 0 ? {} : { uvs: c.uvs },
|
|
786
1260
|
fieldsChanged: c.fields.map((f) => this.#rawHandle(this.#fields, () => new FieldHandle(), f)),
|
|
787
1261
|
sectionsChanged: c.sections.map((x) => this.#rawHandle(this.#sections, () => new SectionHandle(), x))
|
|
788
1262
|
}));
|
|
@@ -935,13 +1409,24 @@ var Scene = class {
|
|
|
935
1409
|
t[1],
|
|
936
1410
|
t[2]
|
|
937
1411
|
].every(Number.isFinite)) throw invalid("translation must be 3 finite numbers");
|
|
1412
|
+
const s = placement.scale;
|
|
1413
|
+
if (s !== void 0 && (s.length !== 3 || ![
|
|
1414
|
+
s[0],
|
|
1415
|
+
s[1],
|
|
1416
|
+
s[2]
|
|
1417
|
+
].every(Number.isFinite))) throw invalid("scale must be 3 finite numbers");
|
|
938
1418
|
pending = {
|
|
939
1419
|
rotation,
|
|
940
1420
|
translation: [
|
|
941
1421
|
t[0],
|
|
942
1422
|
t[1],
|
|
943
1423
|
t[2]
|
|
944
|
-
]
|
|
1424
|
+
],
|
|
1425
|
+
...s === void 0 ? {} : { scale: [
|
|
1426
|
+
s[0],
|
|
1427
|
+
s[1],
|
|
1428
|
+
s[2]
|
|
1429
|
+
] }
|
|
945
1430
|
};
|
|
946
1431
|
},
|
|
947
1432
|
end: () => {
|
|
@@ -1024,8 +1509,34 @@ var Scene = class {
|
|
|
1024
1509
|
const si = options.selfIntersection ?? false;
|
|
1025
1510
|
return decodeAnalysis(new Reader(call(() => this.#scene().analyze(index, generation, tolerance, si, maxListed))));
|
|
1026
1511
|
}
|
|
1512
|
+
/**
|
|
1513
|
+
* Scalar measurements of a mesh in world coordinates: its surface area
|
|
1514
|
+
* (always) and, when watertight, its enclosed volume. An open or
|
|
1515
|
+
* non-manifold mesh still gets its area; only `volume` is `null`, with
|
|
1516
|
+
* `volumeReason` saying why. A read-only call: it leaves every view
|
|
1517
|
+
* valid.
|
|
1518
|
+
*/
|
|
1519
|
+
measure(mesh) {
|
|
1520
|
+
const { index, generation } = this.#id(mesh);
|
|
1521
|
+
const r = call(() => this.#peek().measure(index, generation));
|
|
1522
|
+
const closed = r[2] === 1;
|
|
1523
|
+
return {
|
|
1524
|
+
area: r[0],
|
|
1525
|
+
volume: Number.isNaN(r[1]) ? null : r[1],
|
|
1526
|
+
closed,
|
|
1527
|
+
volumeReason: closed ? null : r[3] === 2 ? "non-manifold" : "open",
|
|
1528
|
+
boundaryLoops: r[4],
|
|
1529
|
+
nonManifoldEdges: r[5],
|
|
1530
|
+
orientationConflicts: r[6]
|
|
1531
|
+
};
|
|
1532
|
+
}
|
|
1027
1533
|
repair(mesh, op) {
|
|
1028
1534
|
const { index, generation } = this.#id(mesh);
|
|
1535
|
+
if (isAutoRepair(op)) {
|
|
1536
|
+
const values = Float64Array.of(AUTO_REPAIR_CODE, optional(op.weldTolerance, "weldTolerance"), optional(op.minTriangles, "minTriangles"), optional(op.maxEdges, "maxEdges"), op.fair === void 0 ? NaN : flagOf(op.fair, true));
|
|
1537
|
+
const noLoops = /* @__PURE__ */ new Uint32Array();
|
|
1538
|
+
return decodeAutoReport(new Reader(call(() => this.#scene().repair(index, generation, values, noLoops))));
|
|
1539
|
+
}
|
|
1029
1540
|
const ops = Array.isArray(op) ? op : [op];
|
|
1030
1541
|
const { values, loops } = encodeRepair(ops);
|
|
1031
1542
|
const r = new Reader(call(() => this.#scene().repair(index, generation, values, loops)));
|
|
@@ -1127,6 +1638,154 @@ var Scene = class {
|
|
|
1127
1638
|
return out;
|
|
1128
1639
|
}
|
|
1129
1640
|
/**
|
|
1641
|
+
* Selects vertices of a mesh: a sphere brush, a lasso polygon, or the
|
|
1642
|
+
* connected component holding a seed vertex. The result is a snapshot
|
|
1643
|
+
* (ascending ids plus the mesh's content stamp); any edit, repair, compact,
|
|
1644
|
+
* undo or redo of the mesh invalidates it, and the selection methods then
|
|
1645
|
+
* throw `STALE_SELECTION` instead of silently remapping ids. Placement
|
|
1646
|
+
* changes keep selections valid. Only vertices a live triangle uses can
|
|
1647
|
+
* be selected (dead or never-used slots never are).
|
|
1648
|
+
*
|
|
1649
|
+
* @throws `UNKNOWN_MESH` for a removed mesh; `INVALID_ARGUMENT` for a bad
|
|
1650
|
+
* spec; `INVALID_INDEX` for an out-of-range component seed.
|
|
1651
|
+
*/
|
|
1652
|
+
select(mesh, spec) {
|
|
1653
|
+
checkSpec(spec);
|
|
1654
|
+
this.#id(mesh);
|
|
1655
|
+
const views = this.views(mesh);
|
|
1656
|
+
const slots = this.vertexSlots(mesh);
|
|
1657
|
+
const positions = views.positions().slice();
|
|
1658
|
+
const topology = buildTopology(views.indices().slice(), slots);
|
|
1659
|
+
let ids;
|
|
1660
|
+
switch (spec.kind) {
|
|
1661
|
+
case "brush": {
|
|
1662
|
+
let center = spec.center;
|
|
1663
|
+
let radius = spec.radius;
|
|
1664
|
+
if (spec.space === "world") {
|
|
1665
|
+
center = transformPoint(invertMatrix4(this.matrix4(mesh)), spec.center);
|
|
1666
|
+
radius = spec.radius / meanStretch(this.matrix4(mesh));
|
|
1667
|
+
}
|
|
1668
|
+
ids = brushPick(positions, topology.used, center, radius);
|
|
1669
|
+
break;
|
|
1670
|
+
}
|
|
1671
|
+
case "lasso":
|
|
1672
|
+
ids = lassoPick(positions, topology.used, spec.polygon, spec.axis ?? "xy");
|
|
1673
|
+
break;
|
|
1674
|
+
case "component": ids = componentOf(topology, spec.seed);
|
|
1675
|
+
}
|
|
1676
|
+
return {
|
|
1677
|
+
mesh,
|
|
1678
|
+
vertices: Uint32Array.from(ids),
|
|
1679
|
+
stamp: meshAdds(this).stamp(mesh)
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* Whether `sel` is still usable: its mesh is alive and no topology edit
|
|
1684
|
+
* landed since it was taken. Never throws.
|
|
1685
|
+
*/
|
|
1686
|
+
selectionAlive(sel) {
|
|
1687
|
+
try {
|
|
1688
|
+
if (sel === null || typeof sel !== "object" || !(sel.vertices instanceof Uint32Array)) return false;
|
|
1689
|
+
this.#id(sel.mesh);
|
|
1690
|
+
return Number.isInteger(sel.stamp) && sel.stamp === meshAdds(this).stamp(sel.mesh);
|
|
1691
|
+
} catch {
|
|
1692
|
+
return false;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
/** Reads the mesh's topology and checks `sel` against its stamp. */
|
|
1696
|
+
#selectionTopology(sel) {
|
|
1697
|
+
this.#id(sel.mesh);
|
|
1698
|
+
checkSelection(sel, sel.mesh, meshAdds(this).stamp(sel.mesh));
|
|
1699
|
+
return buildTopology(this.views(sel.mesh).indices().slice(), this.vertexSlots(sel.mesh));
|
|
1700
|
+
}
|
|
1701
|
+
/**
|
|
1702
|
+
* Grows a selection by `steps` rings of edge neighbours (staying on live
|
|
1703
|
+
* vertices). The result carries the current stamp, so a fresh selection
|
|
1704
|
+
* can be grown right after an edit. `steps` 0 returns an equal copy.
|
|
1705
|
+
*
|
|
1706
|
+
* @throws `STALE_SELECTION` when the mesh changed since the selection;
|
|
1707
|
+
* `UNKNOWN_MESH` for a removed mesh.
|
|
1708
|
+
*/
|
|
1709
|
+
selectGrow(sel, steps = 1) {
|
|
1710
|
+
const topology = this.#selectionTopology(sel);
|
|
1711
|
+
return {
|
|
1712
|
+
mesh: sel.mesh,
|
|
1713
|
+
vertices: Uint32Array.from(growIds(topology, sel.vertices, steps)),
|
|
1714
|
+
stamp: meshAdds(this).stamp(sel.mesh)
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1717
|
+
/**
|
|
1718
|
+
* Shrinks a selection by `steps` rings, dropping the members with a live
|
|
1719
|
+
* neighbour outside the set. Interior selections (no boundary) survive
|
|
1720
|
+
* unchanged; shrinking past the core gives an empty (still valid)
|
|
1721
|
+
* selection.
|
|
1722
|
+
*
|
|
1723
|
+
* @throws `STALE_SELECTION` when the mesh changed since the selection;
|
|
1724
|
+
* `UNKNOWN_MESH` for a removed mesh.
|
|
1725
|
+
*/
|
|
1726
|
+
selectShrink(sel, steps = 1) {
|
|
1727
|
+
const topology = this.#selectionTopology(sel);
|
|
1728
|
+
return {
|
|
1729
|
+
mesh: sel.mesh,
|
|
1730
|
+
vertices: Uint32Array.from(shrinkIds(topology, sel.vertices, steps)),
|
|
1731
|
+
stamp: meshAdds(this).stamp(sel.mesh)
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
/**
|
|
1735
|
+
* Inverts a selection: every live vertex outside it. The result carries
|
|
1736
|
+
* the current stamp.
|
|
1737
|
+
*
|
|
1738
|
+
* @throws `STALE_SELECTION` when the mesh changed since the selection;
|
|
1739
|
+
* `UNKNOWN_MESH` for a removed mesh.
|
|
1740
|
+
*/
|
|
1741
|
+
selectInvert(sel) {
|
|
1742
|
+
const topology = this.#selectionTopology(sel);
|
|
1743
|
+
const inSet = new Set(sel.vertices);
|
|
1744
|
+
const out = [];
|
|
1745
|
+
for (let v = 0; v < topology.used.length; v++) if (topology.used[v] === 1 && !inSet.has(v)) out.push(v);
|
|
1746
|
+
return {
|
|
1747
|
+
mesh: sel.mesh,
|
|
1748
|
+
vertices: Uint32Array.from(out),
|
|
1749
|
+
stamp: meshAdds(this).stamp(sel.mesh)
|
|
1750
|
+
};
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Isolates a selection: removes the triangles fully outside it (no
|
|
1754
|
+
* selected vertex), one undo step. Boundary triangles with at least one
|
|
1755
|
+
* selected vertex stay. An empty selection selects nothing to keep and
|
|
1756
|
+
* throws `INVALID_ARGUMENT` rather than clearing the mesh.
|
|
1757
|
+
*
|
|
1758
|
+
* @throws `STALE_SELECTION` when the mesh changed since the selection;
|
|
1759
|
+
* `UNKNOWN_MESH` for a removed mesh.
|
|
1760
|
+
*/
|
|
1761
|
+
isolate(mesh, sel) {
|
|
1762
|
+
this.#id(mesh);
|
|
1763
|
+
checkSelection(sel, mesh, meshAdds(this).stamp(mesh));
|
|
1764
|
+
if (sel.vertices.length === 0) throw invalid("isolate needs a non-empty selection");
|
|
1765
|
+
const inSet = new Set(sel.vertices);
|
|
1766
|
+
const indices = this.views(mesh).indices().slice();
|
|
1767
|
+
const drop = [];
|
|
1768
|
+
for (let t = 0; t * 3 + 2 < indices.length; t++) {
|
|
1769
|
+
const a = indices[t * 3];
|
|
1770
|
+
const b = indices[t * 3 + 1];
|
|
1771
|
+
const c = indices[t * 3 + 2];
|
|
1772
|
+
if (a === 0 && b === 0 && c === 0) continue;
|
|
1773
|
+
if (!inSet.has(a) && !inSet.has(b) && !inSet.has(c)) drop.push(t);
|
|
1774
|
+
}
|
|
1775
|
+
if (drop.length > 0) this.edit(mesh, [{
|
|
1776
|
+
op: "removeTriangles",
|
|
1777
|
+
triangles: Uint32Array.from(drop)
|
|
1778
|
+
}]);
|
|
1779
|
+
return {
|
|
1780
|
+
removedTriangles: drop.length,
|
|
1781
|
+
selection: {
|
|
1782
|
+
mesh,
|
|
1783
|
+
vertices: sel.vertices.slice(),
|
|
1784
|
+
stamp: meshAdds(this).stamp(mesh)
|
|
1785
|
+
}
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1130
1789
|
* Adds a signed distance field: one value per vertex slot of `on`, the
|
|
1131
1790
|
* distance to `to` as placed (negative inside), `Infinity` beyond
|
|
1132
1791
|
* `maxDistance`, NaN for dead or unused vertices. With several meshes in
|
|
@@ -1139,6 +1798,37 @@ var Scene = class {
|
|
|
1139
1798
|
addDistanceField(desc) {
|
|
1140
1799
|
return this.#addField(desc, false);
|
|
1141
1800
|
}
|
|
1801
|
+
/**
|
|
1802
|
+
* Adds a per-vertex wall-thickness field: one value per vertex slot of
|
|
1803
|
+
* `on`, the distance to the opposite surface along the vertex's smooth
|
|
1804
|
+
* normal, in world units. Open sheets and rims read NaN, hits beyond
|
|
1805
|
+
* `maxDistance` read `Infinity`. It follows edits and placements by the
|
|
1806
|
+
* mesh modes, like normals, and colours like any field.
|
|
1807
|
+
*
|
|
1808
|
+
* @throws `INVALID_ARGUMENT` when `maxDistance` is negative or NaN, or the name is taken.
|
|
1809
|
+
*/
|
|
1810
|
+
addThicknessField(desc) {
|
|
1811
|
+
const s = this.#scene();
|
|
1812
|
+
const ids = this.#flatIds([desc.on]);
|
|
1813
|
+
if (typeof desc.name !== "string") throw invalid("name must be a string");
|
|
1814
|
+
const raw = call(() => s.add_thickness_field(desc.name, ids, desc.maxDistance));
|
|
1815
|
+
return this.#rawHandle(this.#fields, () => new FieldHandle(), raw);
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Adds a per-vertex curvature field: one value per vertex slot of `on`,
|
|
1819
|
+
* the signed mean curvature there, in 1/world-units (positive on domes,
|
|
1820
|
+
* negative in bowls, zero on flats). It follows edits and placements by
|
|
1821
|
+
* the mesh modes, like normals, and colours like any field.
|
|
1822
|
+
*
|
|
1823
|
+
* @throws `INVALID_ARGUMENT` when the name is taken.
|
|
1824
|
+
*/
|
|
1825
|
+
addCurvatureField(desc) {
|
|
1826
|
+
const s = this.#scene();
|
|
1827
|
+
const ids = this.#flatIds([desc.on]);
|
|
1828
|
+
if (typeof desc.name !== "string") throw invalid("name must be a string");
|
|
1829
|
+
const raw = call(() => s.add_curvature_field(desc.name, ids));
|
|
1830
|
+
return this.#rawHandle(this.#fields, () => new FieldHandle(), raw);
|
|
1831
|
+
}
|
|
1142
1832
|
/** `addDistanceField`, or with `deferred` its internal variant that computes nothing yet. */
|
|
1143
1833
|
#addField(desc, deferred) {
|
|
1144
1834
|
const s = this.#scene();
|
|
@@ -1260,6 +1950,7 @@ var Scene = class {
|
|
|
1260
1950
|
},
|
|
1261
1951
|
pushPositions: (token, chunk) => call(() => scene.#scene().add_mesh_push_positions(token, chunk)),
|
|
1262
1952
|
pushIndices: (token, chunk) => call(() => scene.#scene().add_mesh_push_indices(token, chunk)),
|
|
1953
|
+
pushUvs: (token, chunk) => call(() => scene.#scene().add_mesh_push_uvs(token, chunk)),
|
|
1263
1954
|
step: (token, budget) => call(() => scene.#scene().add_mesh_step(token, budget)),
|
|
1264
1955
|
buffers: (token) => {
|
|
1265
1956
|
const [pp, pl, np, nl, ip, il] = call(() => scene.#peek().add_mesh_buffers(token));
|
|
@@ -1416,5 +2107,4 @@ var Scene = class {
|
|
|
1416
2107
|
};
|
|
1417
2108
|
}
|
|
1418
2109
|
};
|
|
1419
|
-
//#endregion
|
|
1420
2110
|
export { useWasmScene as i, fieldSteps as n, meshAdds as r, Scene as t };
|