@margoengine/loaders 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +1666 -1198
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-common.js +1 -1
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae.d.ts +9 -4
- package/dist/dae.js +16 -5
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +42 -1
- package/dist/dxf.js +226 -15
- package/dist/engine.d.ts +5 -0
- package/dist/engine.js +3 -0
- package/dist/gcode.d.ts +37 -0
- package/dist/gcode.js +91 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/mtl.d.ts +8 -3
- package/dist/mtl.js +91 -10
- package/dist/nrrd.d.ts +12 -5
- package/dist/nrrd.js +68 -26
- package/dist/obj.d.ts +3 -1
- package/dist/obj.js +7 -1
- package/dist/render.d.ts +9 -4
- package/dist/render.js +31 -3
- package/dist/shared/asset-url.js +82 -82
- package/dist/shared/crc32.d.ts +2 -2
- package/dist/shared/crc32.js +47 -47
- package/dist/shared/errors.js +36 -36
- package/dist/shared/inflate.js +284 -284
- package/dist/shared/normals.js +26 -26
- package/dist/shared/scene-scope.js +54 -54
- package/dist/shared/trailing-data.js +45 -42
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.js +10 -145
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +89 -2
- package/dist/threemf-materials.js +194 -11
- package/dist/threemf-mesh.d.ts +21 -6
- package/dist/threemf-mesh.js +266 -45
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +73 -4
- package/dist/threemf.js +235 -41
- package/dist/types.d.ts +18 -0
- package/dist/workers/usd.js +1 -1
- package/package.json +291 -246
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
const F = '3MF';
|
|
3
|
+
const SIZE = { scalar: 1, vector: 3, matrix: 16, resourceid: 0 };
|
|
4
|
+
const MAX_STEPS = 100_000;
|
|
5
|
+
const bad = (message) => {
|
|
6
|
+
throw new LoaderError(F, 'syntax', message);
|
|
7
|
+
};
|
|
8
|
+
/** A voxel index outside `0..size - 1` under a tile style. */
|
|
9
|
+
const tiled = (style, i, size) => {
|
|
10
|
+
if (style === 'clamp')
|
|
11
|
+
return Math.min(size - 1, Math.max(i, 0));
|
|
12
|
+
const turns = Math.floor(i / size);
|
|
13
|
+
return style === 'mirror' && turns % 2 !== 0 ? (turns + 1) * size - i - 1 : i - turns * size;
|
|
14
|
+
};
|
|
15
|
+
function sampler(f, voxels, pos, out) {
|
|
16
|
+
const { columns, rows, sheets, data } = voxels, rgba = new Float64Array(4), [tileU, tileV, tileW] = f.tile, color = out.get('color'), channels = ['red', 'green', 'blue', 'alpha'].map((name) => out.get(name));
|
|
17
|
+
/** A coordinate brought into 0..1 by its tile style, so a far point keeps its precision. */
|
|
18
|
+
const wrapped = (style, s) => style === 'clamp'
|
|
19
|
+
? Math.min(1, Math.max(0, s))
|
|
20
|
+
: style === 'mirror'
|
|
21
|
+
? 1 - Math.abs(s - 2 * Math.floor(s / 2) - 1)
|
|
22
|
+
: s - Math.floor(s);
|
|
23
|
+
return () => {
|
|
24
|
+
if (!Number.isFinite(pos[0] + pos[1] + pos[2]))
|
|
25
|
+
rgba.fill(NaN);
|
|
26
|
+
else {
|
|
27
|
+
// Voxel centres are at halves; rows run down as v runs up (section 3.2).
|
|
28
|
+
const c = wrapped(tileU, pos[0]) * columns - 0.5, r = (1 - wrapped(tileV, pos[1])) * rows - 0.5, s = wrapped(tileW, pos[2]) * sheets - 0.5;
|
|
29
|
+
const add = (i, j, k, weight) => {
|
|
30
|
+
const at = ((tiled(tileW, k, sheets) * rows + tiled(tileV, i, rows)) * columns + tiled(tileU, j, columns)) * 4;
|
|
31
|
+
for (let n = 0; n < 4; n++)
|
|
32
|
+
rgba[n] += (data[at + n] / 65535) * weight;
|
|
33
|
+
};
|
|
34
|
+
rgba.fill(0);
|
|
35
|
+
if (f.filter === 'nearest') {
|
|
36
|
+
// The nearest centre; halfway between two, the one of lower index.
|
|
37
|
+
const lower = (x) => Math.ceil(x - 0.5 - 1e-9);
|
|
38
|
+
add(lower(r), lower(c), lower(s), 1);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const i = Math.floor(r), j = Math.floor(c), k = Math.floor(s);
|
|
42
|
+
for (let n = 0; n < 8; n++) {
|
|
43
|
+
const di = (n >> 1) & 1, dj = n & 1, dk = n >> 2, weight = (di ? r - i : 1 - r + i) * (dj ? c - j : 1 - c + j) * (dk ? s - k : 1 - s + k);
|
|
44
|
+
add(i + di, j + dj, k + dk, weight);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (let n = 0; n < 4; n++)
|
|
49
|
+
channels[n][0] = rgba[n] * f.scale + f.offset;
|
|
50
|
+
for (let n = 0; n < 3; n++)
|
|
51
|
+
color[n] = channels[n][0];
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const UNARY = {
|
|
55
|
+
sin: Math.sin,
|
|
56
|
+
cos: Math.cos,
|
|
57
|
+
tan: Math.tan,
|
|
58
|
+
asin: Math.asin,
|
|
59
|
+
acos: Math.acos,
|
|
60
|
+
atan: Math.atan,
|
|
61
|
+
abs: Math.abs,
|
|
62
|
+
sqrt: Math.sqrt,
|
|
63
|
+
exp: Math.exp,
|
|
64
|
+
log: Math.log,
|
|
65
|
+
log2: Math.log2,
|
|
66
|
+
log10: Math.log10,
|
|
67
|
+
cosh: Math.cosh,
|
|
68
|
+
sinh: Math.sinh,
|
|
69
|
+
tanh: Math.tanh,
|
|
70
|
+
round: (a) => Math.sign(a) * Math.round(Math.abs(a)),
|
|
71
|
+
ceil: Math.ceil,
|
|
72
|
+
floor: Math.floor,
|
|
73
|
+
sign: Math.sign,
|
|
74
|
+
fract: (a) => a - Math.floor(a),
|
|
75
|
+
};
|
|
76
|
+
const BINARY = {
|
|
77
|
+
addition: (a, b) => a + b,
|
|
78
|
+
subtraction: (a, b) => a - b,
|
|
79
|
+
multiplication: (a, b) => a * b,
|
|
80
|
+
division: (a, b) => a / b,
|
|
81
|
+
atan2: Math.atan2,
|
|
82
|
+
min: Math.min,
|
|
83
|
+
max: Math.max,
|
|
84
|
+
fmod: (a, b) => a % b,
|
|
85
|
+
mod: (a, b) => a - b * Math.floor(a / b),
|
|
86
|
+
// Math.pow gives 1 for an undefined base to the power 0.
|
|
87
|
+
pow: (a, b) => (a !== a || b !== b ? NaN : a ** b),
|
|
88
|
+
};
|
|
89
|
+
/** Writes the inverse of a row-major 4 x 4 matrix, or NaN for one without. Gauss-Jordan with row pivoting. */
|
|
90
|
+
function invert(m, out) {
|
|
91
|
+
const a = Array.from({ length: 4 }, (_, r) => [
|
|
92
|
+
...m.subarray(r * 4, r * 4 + 4),
|
|
93
|
+
...[0, 1, 2, 3].map((c) => +(r === c)),
|
|
94
|
+
]);
|
|
95
|
+
for (let c = 0; c < 4; c++) {
|
|
96
|
+
let pivot = c;
|
|
97
|
+
for (let r = c + 1; r < 4; r++)
|
|
98
|
+
if (Math.abs(a[r][c]) > Math.abs(a[pivot][c]))
|
|
99
|
+
pivot = r;
|
|
100
|
+
if (!(Math.abs(a[pivot][c]) > 0))
|
|
101
|
+
return void out.fill(NaN);
|
|
102
|
+
[a[c], a[pivot]] = [a[pivot], a[c]];
|
|
103
|
+
const scale = a[c][c];
|
|
104
|
+
for (let k = 0; k < 8; k++)
|
|
105
|
+
a[c][k] /= scale;
|
|
106
|
+
for (let r = 0; r < 4; r++) {
|
|
107
|
+
if (r === c)
|
|
108
|
+
continue;
|
|
109
|
+
const factor = a[r][c];
|
|
110
|
+
for (let k = 0; k < 8; k++)
|
|
111
|
+
a[r][k] -= factor * a[c][k];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for (let r = 0; r < 4; r++)
|
|
115
|
+
for (let c = 0; c < 4; c++)
|
|
116
|
+
out[r * 4 + c] = Number.isFinite(a[r][4 + c]) ? a[r][4 + c] : NaN;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Compiles function `id` of a scope over `inputs`, whose buffers the caller writes before each `run`. `outputs` holds
|
|
120
|
+
* the function's outputs by name.
|
|
121
|
+
*
|
|
122
|
+
* @throws LoaderError for a missing or mistyped input, a node this evaluator does not know, and functions that call
|
|
123
|
+
* themselves.
|
|
124
|
+
*/
|
|
125
|
+
export function compileField(scope, id, inputs) {
|
|
126
|
+
const steps = [], budget = { steps: MAX_STEPS };
|
|
127
|
+
const outputs = inline(scope, id, inputs, steps, [], budget);
|
|
128
|
+
return {
|
|
129
|
+
outputs,
|
|
130
|
+
run: () => {
|
|
131
|
+
for (const step of steps)
|
|
132
|
+
step();
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function inline(scope, id, inputs, steps, calls, budget) {
|
|
137
|
+
const f = scope.functions.get(id) ?? bad(`function ${id.slice(0, 32)} is not defined`), label = `function ${id.slice(0, 32)}`;
|
|
138
|
+
if (calls.includes(id) || calls.length > 64)
|
|
139
|
+
bad(`${label} calls itself`);
|
|
140
|
+
const outputs = new Map();
|
|
141
|
+
if (f.kind === 'image') {
|
|
142
|
+
const pos = inputs.get('pos');
|
|
143
|
+
if (!(pos instanceof Float64Array) || pos.length !== 3)
|
|
144
|
+
bad(`${label} needs the vector input pos`);
|
|
145
|
+
const out = new Map(['color', 'red', 'green', 'blue', 'alpha'].map((n, i) => [n, new Float64Array(i ? 1 : 3)]));
|
|
146
|
+
steps.push(sampler(f, scope.voxels(f.imageId), pos, out));
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
const values = new Map();
|
|
150
|
+
for (const { name, type } of f.inputs) {
|
|
151
|
+
const value = inputs.get(name);
|
|
152
|
+
if (value === undefined || (type === 'resourceid') !== (typeof value === 'string'))
|
|
153
|
+
bad(`${label} is called without its ${type} input ${name.slice(0, 64)}`);
|
|
154
|
+
if (value instanceof Float64Array && value.length !== SIZE[type])
|
|
155
|
+
bad(`${label} is called with another type for ${name.slice(0, 64)}`);
|
|
156
|
+
values.set(`inputs.${name}`, value);
|
|
157
|
+
}
|
|
158
|
+
const read = (port) => values.get(port.ref) ?? bad(`${label}: ${String(port.ref).slice(0, 64)} is not an earlier output`);
|
|
159
|
+
for (const node of f.nodes) {
|
|
160
|
+
if (--budget.steps < 0)
|
|
161
|
+
throw new LoaderError(F, 'limit', `${label} expands to more than ${MAX_STEPS} nodes`);
|
|
162
|
+
compileNode(scope, node, label, read, values, steps, [...calls, id], budget);
|
|
163
|
+
}
|
|
164
|
+
for (const port of f.outputs)
|
|
165
|
+
outputs.set(port.name, read(port));
|
|
166
|
+
return outputs;
|
|
167
|
+
}
|
|
168
|
+
function compileNode(scope, node, label, read, values, steps, calls, budget) {
|
|
169
|
+
const { op, attributes } = node, what = `${label}: node ${node.id.slice(0, 64)} (${op.slice(0, 32)})`, port = (name) => node.inputs.find((p) => p.name === name) ?? bad(`${what} needs the input ${name}`), value = (name, size) => {
|
|
170
|
+
const v = read(port(name));
|
|
171
|
+
if (!(v instanceof Float64Array) || (size !== undefined && v.length !== size))
|
|
172
|
+
bad(`${what}: input ${name} has the wrong type`);
|
|
173
|
+
return v;
|
|
174
|
+
}, resource = (name) => {
|
|
175
|
+
const v = read(port(name));
|
|
176
|
+
return typeof v === 'string' ? v : bad(`${what}: input ${name} is not a resource`);
|
|
177
|
+
};
|
|
178
|
+
const store = (name, v) => void values.set(`${node.id}.${name}`, v);
|
|
179
|
+
/** A buffer for the output `name`, or the node's only output. */
|
|
180
|
+
const result = (size, name) => {
|
|
181
|
+
const out = name === undefined ? node.outputs[0] : node.outputs.find((p) => p.name === name), buffer = new Float64Array(size);
|
|
182
|
+
if (!out) {
|
|
183
|
+
if (name === undefined)
|
|
184
|
+
bad(`${what} has no output`);
|
|
185
|
+
return buffer;
|
|
186
|
+
}
|
|
187
|
+
if (SIZE[out.type] !== size)
|
|
188
|
+
bad(`${what}: output ${out.name.slice(0, 64)} has the wrong type`);
|
|
189
|
+
store(out.name, buffer);
|
|
190
|
+
return buffer;
|
|
191
|
+
};
|
|
192
|
+
const defined = (x) => (Number.isFinite(x) ? x : NaN);
|
|
193
|
+
const numbers = (list) => list.map((text) => {
|
|
194
|
+
const n = Number(text);
|
|
195
|
+
return text?.trim() && Number.isFinite(n) ? n : bad(`${what}: invalid constant`);
|
|
196
|
+
});
|
|
197
|
+
if (Object.hasOwn(UNARY, op)) {
|
|
198
|
+
const apply = UNARY[op], a = value('A'), out = result(a.length);
|
|
199
|
+
steps.push(() => {
|
|
200
|
+
for (let i = 0; i < out.length; i++)
|
|
201
|
+
out[i] = defined(apply(a[i]));
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
else if (Object.hasOwn(BINARY, op)) {
|
|
205
|
+
const apply = BINARY[op], a = value('A'), b = value('B', a.length), out = result(a.length);
|
|
206
|
+
steps.push(() => {
|
|
207
|
+
for (let i = 0; i < out.length; i++)
|
|
208
|
+
out[i] = defined(apply(a[i], b[i]));
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
else if (op === 'constant' || op === 'constvec' || op === 'constmat') {
|
|
212
|
+
const list = op === 'constant'
|
|
213
|
+
? [attributes.value]
|
|
214
|
+
: op === 'constvec'
|
|
215
|
+
? [attributes.x, attributes.y, attributes.z]
|
|
216
|
+
: (attributes.matrix ?? '').trim().split(/\s+/), size = op === 'constant' ? 1 : op === 'constvec' ? 3 : 16;
|
|
217
|
+
if (list.length !== size)
|
|
218
|
+
bad(`${what}: invalid constant`);
|
|
219
|
+
result(size).set(numbers(list));
|
|
220
|
+
}
|
|
221
|
+
else if (op === 'constresourceid') {
|
|
222
|
+
const out = node.outputs[0] ?? bad(`${what} has no output`);
|
|
223
|
+
store(out.name, attributes.value ?? attributes.resourceid ?? bad(`${what} needs a value`));
|
|
224
|
+
}
|
|
225
|
+
else if (op === 'composevector' || op === 'composematrix') {
|
|
226
|
+
const names = op === 'composevector'
|
|
227
|
+
? ['x', 'y', 'z']
|
|
228
|
+
: Array.from({ length: 16 }, (_, i) => `m${Math.floor(i / 4)}${i % 4}`), from = names.map((name) => value(name, 1)), out = result(names.length);
|
|
229
|
+
steps.push(() => from.forEach((v, i) => (out[i] = v[0])));
|
|
230
|
+
}
|
|
231
|
+
else if (op === 'vectorfromscalar') {
|
|
232
|
+
const a = value('A', 1), out = result(3);
|
|
233
|
+
steps.push(() => void out.fill(a[0]));
|
|
234
|
+
}
|
|
235
|
+
else if (op === 'decomposevector') {
|
|
236
|
+
const a = value('A', 3), out = ['x', 'y', 'z'].map((name) => result(1, name));
|
|
237
|
+
steps.push(() => out.forEach((v, i) => (v[0] = a[i])));
|
|
238
|
+
}
|
|
239
|
+
else if (op === 'matrixfromcolumns' || op === 'matrixfromrows') {
|
|
240
|
+
const from = ['A', 'B', 'C', 'D'].map((name) => value(name, 3)), rows = op === 'matrixfromrows', out = result(16);
|
|
241
|
+
steps.push(() => {
|
|
242
|
+
for (let r = 0; r < 4; r++)
|
|
243
|
+
for (let c = 0; c < 4; c++)
|
|
244
|
+
out[r * 4 + c] = (rows ? c < 3 : r < 3) ? (rows ? from[r][c] : from[c][r]) : +(r === 3 && c === 3);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
else if (op === 'dot') {
|
|
248
|
+
const a = value('A', 3), b = value('B', 3), out = result(1);
|
|
249
|
+
steps.push(() => (out[0] = defined(a[0] * b[0] + a[1] * b[1] + a[2] * b[2])));
|
|
250
|
+
}
|
|
251
|
+
else if (op === 'cross') {
|
|
252
|
+
const a = value('A', 3), b = value('B', 3), out = result(3);
|
|
253
|
+
steps.push(() => {
|
|
254
|
+
for (let i = 0; i < 3; i++) {
|
|
255
|
+
const j = (i + 1) % 3, k = (i + 2) % 3;
|
|
256
|
+
out[i] = defined(a[j] * b[k] - a[k] * b[j]);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
else if (op === 'length') {
|
|
261
|
+
const a = value('A', 3), out = result(1);
|
|
262
|
+
steps.push(() => (out[0] = defined(Math.hypot(a[0], a[1], a[2]))));
|
|
263
|
+
}
|
|
264
|
+
else if (op === 'matrixvectormultiplication' || op === 'matvecmultiplication') {
|
|
265
|
+
const m = value('A', 16), v = value('B', 3), out = result(3);
|
|
266
|
+
steps.push(() => {
|
|
267
|
+
for (let r = 0; r < 3; r++)
|
|
268
|
+
out[r] = defined(m[r * 4] * v[0] + m[r * 4 + 1] * v[1] + m[r * 4 + 2] * v[2] + m[r * 4 + 3]);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
else if (op === 'transpose') {
|
|
272
|
+
const m = value('A', 16), out = result(16);
|
|
273
|
+
steps.push(() => {
|
|
274
|
+
for (let i = 0; i < 16; i++)
|
|
275
|
+
out[i] = m[(i % 4) * 4 + Math.floor(i / 4)];
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
else if (op === 'inverse') {
|
|
279
|
+
const m = value('A', 16), out = result(16);
|
|
280
|
+
steps.push(() => invert(m, out));
|
|
281
|
+
}
|
|
282
|
+
else if (op === 'clamp') {
|
|
283
|
+
const a = value('A'), low = value('min', a.length), high = value('max', a.length), out = result(a.length);
|
|
284
|
+
steps.push(() => {
|
|
285
|
+
for (let i = 0; i < out.length; i++)
|
|
286
|
+
out[i] = Math.max(low[i], Math.min(a[i], high[i]));
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else if (op === 'select') {
|
|
290
|
+
const a = value('A'), b = value('B', a.length), c = value('C', a.length), d = value('D', a.length), out = result(a.length);
|
|
291
|
+
steps.push(() => {
|
|
292
|
+
for (let i = 0; i < out.length; i++)
|
|
293
|
+
out[i] = a[i] !== a[i] || b[i] !== b[i] ? NaN : a[i] < b[i] ? c[i] : d[i];
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
else if (op === 'mesh' || op === 'unsignedmesh') {
|
|
297
|
+
const pos = value('pos', 3), distance = scope.distance(resource('mesh'), op === 'mesh'), out = result(1);
|
|
298
|
+
steps.push(() => (out[0] = Number.isFinite(pos[0] + pos[1] + pos[2]) ? defined(distance(pos[0], pos[1], pos[2])) : NaN));
|
|
299
|
+
}
|
|
300
|
+
else if (op === 'beamlattice') {
|
|
301
|
+
// The distance is exact in any band, so the node's accuraterange asks for nothing more.
|
|
302
|
+
const pos = value('pos', 3), distance = scope.lattice(resource('beamlattice')), out = result(1);
|
|
303
|
+
steps.push(() => (out[0] = Number.isFinite(pos[0] + pos[1] + pos[2]) ? defined(distance(pos[0], pos[1], pos[2])) : NaN));
|
|
304
|
+
}
|
|
305
|
+
else if (op === 'functioncall') {
|
|
306
|
+
const called = resource('functionID'), given = new Map(node.inputs.filter((p) => p.name !== 'functionID').map((p) => [p.name, read(p)])), returned = inline(scope, called, given, steps, calls, budget);
|
|
307
|
+
for (const { name, type } of node.outputs) {
|
|
308
|
+
const v = returned.get(name);
|
|
309
|
+
if (v === undefined || (v instanceof Float64Array && v.length !== SIZE[type]))
|
|
310
|
+
bad(`${what}: function ${called.slice(0, 32)} has no ${type} output ${name.slice(0, 64)}`);
|
|
311
|
+
store(name, v);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
else if (op === 'functiongradient' || op === 'normalizedistance') {
|
|
315
|
+
const called = resource('functionID'), { scalaroutput, vectorinput } = attributes;
|
|
316
|
+
if (!scalaroutput || !vectorinput)
|
|
317
|
+
bad(`${what} needs scalaroutput and vectorinput`);
|
|
318
|
+
const at = value(vectorinput, 3), step = value('step', 1), moved = new Float64Array(3), given = new Map(node.inputs.filter((p) => p.name !== 'functionID' && p.name !== 'step').map((p) => [p.name, read(p)])), own = [];
|
|
319
|
+
given.set(vectorinput, moved);
|
|
320
|
+
const f = inline(scope, called, given, own, calls, budget).get(scalaroutput);
|
|
321
|
+
if (!(f instanceof Float64Array) || f.length !== 1)
|
|
322
|
+
bad(`${what}: function ${called.slice(0, 32)} has no scalar output ${scalaroutput.slice(0, 64)}`);
|
|
323
|
+
const scalar = f, gradient = new Float64Array(3);
|
|
324
|
+
const sample = (axis, by) => {
|
|
325
|
+
moved.set(at);
|
|
326
|
+
if (axis >= 0)
|
|
327
|
+
moved[axis] += by;
|
|
328
|
+
for (const run of own)
|
|
329
|
+
run();
|
|
330
|
+
return scalar[0];
|
|
331
|
+
};
|
|
332
|
+
// Central differences, the step at least 1e-8.
|
|
333
|
+
const differentiate = () => {
|
|
334
|
+
const h = Math.max(step[0], 1e-8);
|
|
335
|
+
for (let k = 0; k < 3; k++)
|
|
336
|
+
gradient[k] = defined((sample(k, h) - sample(k, -h)) / (2 * h));
|
|
337
|
+
return Math.hypot(gradient[0], gradient[1], gradient[2]);
|
|
338
|
+
};
|
|
339
|
+
if (op === 'functiongradient') {
|
|
340
|
+
const direction = result(3, 'vector'), raw = result(3, 'gradient'), magnitude = result(1, 'magnitude');
|
|
341
|
+
steps.push(() => {
|
|
342
|
+
const length = differentiate();
|
|
343
|
+
raw.set(gradient);
|
|
344
|
+
magnitude[0] = length;
|
|
345
|
+
for (let k = 0; k < 3; k++)
|
|
346
|
+
direction[k] = defined(gradient[k] / length);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
const out = result(1);
|
|
351
|
+
steps.push(() => {
|
|
352
|
+
const length = differentiate();
|
|
353
|
+
out[0] = defined(sample(-1, 0) / Math.max(length, 1e-8));
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
throw new LoaderError(F, 'unsupported', `${what}: this node is not evaluated; tessellate level sets elsewhere`);
|
|
359
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ThreeMFEvaluatedLattice, ThreeMFEvaluateOptions } from './threemf-evaluate.js';
|
|
2
|
+
import type { ThreeMFBeamLattice } from './threemf-lattice.js';
|
|
3
|
+
import { type ThreeMFExtension, type ThreeMFMesh, type ThreeMFModel } from './threemf.js';
|
|
4
|
+
/** The lattice step of the evaluating reader. */
|
|
5
|
+
export declare function lattices(options: ThreeMFEvaluateOptions): {
|
|
6
|
+
hooks: ThreeMFExtension;
|
|
7
|
+
add(model: ThreeMFModel, own: ReadonlyMap<string, ThreeMFBeamLattice>, surfaces: ReadonlyMap<string, ThreeMFMesh>, modelPath: string | undefined): Promise<void>;
|
|
8
|
+
result: () => ThreeMFEvaluatedLattice[];
|
|
9
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Adds a 3MF beam lattice's triangles to its mesh object, for `parse3MFEvaluated`: the shells of
|
|
2
|
+
// `mesh3MFBeamLattice`, cut by the clipping mesh when the lattice names one, with the properties the beam lattice
|
|
3
|
+
// extension gives them (https://github.com/3MFConsortium/spec_beamlattice, chapter 2: a beam's property runs along its
|
|
4
|
+
// axis, a surface takes that of the nearest point of the axis, and a clipped surface takes the clipping mesh's).
|
|
5
|
+
import { mesh3MFBeamLattice } from './threemf-beams.js';
|
|
6
|
+
import { booleanMesh } from './threemf-csg.js';
|
|
7
|
+
import { cornerProperty, } from './threemf.js';
|
|
8
|
+
import { LoaderError } from './types.js';
|
|
9
|
+
import { floats, limits, uints } from './util.js';
|
|
10
|
+
const F = '3MF';
|
|
11
|
+
const NONE = 0xffffffff;
|
|
12
|
+
const IDENTITY = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0];
|
|
13
|
+
/** A lattice's shells as a mesh with the properties of their beams and balls. */
|
|
14
|
+
function shells(model, object, lattice, vertices, gradients, options) {
|
|
15
|
+
const made = mesh3MFBeamLattice(vertices, lattice, options), out = { vertices: floats(), triangles: uints(), pid: uints(), p: uints(), flat: [] }, blended = [];
|
|
16
|
+
let blendedAt = -1;
|
|
17
|
+
for (let v = 0; v < made.positions.length; v += 3)
|
|
18
|
+
out.vertices.push3(made.positions[v], made.positions[v + 1], made.positions[v + 2]);
|
|
19
|
+
for (const { kind, index, triangleStart, triangleCount } of made.parts) {
|
|
20
|
+
const beam = kind === 'beam' ? lattice.beams[index] : undefined, ball = kind === 'ball' ? lattice.effectiveBalls[index] : undefined, pid = (beam ?? ball).pid, first = beam ? beam.p1 : ball.property, second = beam ? (beam.p2 ?? first) : first, group = pid === undefined ? undefined : model.properties.get(pid), a = first === undefined ? undefined : group?.[first], b = second === undefined ? undefined : group?.[second], at = pid === undefined ? NONE : model.propertyIds.indexOf(pid),
|
|
21
|
+
// Only a colour group blends; a base material holds for the whole beam.
|
|
22
|
+
blend = a && b && gradients.has(pid) && String(a) !== String(b);
|
|
23
|
+
for (let t = triangleStart; t < triangleStart + triangleCount; t++) {
|
|
24
|
+
const corners = [made.indices[t * 3], made.indices[t * 3 + 1], made.indices[t * 3 + 2]];
|
|
25
|
+
out.triangles.push3(corners[0], corners[1], corners[2]);
|
|
26
|
+
out.flat.push(a);
|
|
27
|
+
if (blend) {
|
|
28
|
+
if (blendedAt < 0) {
|
|
29
|
+
blendedAt = model.propertyIds.length;
|
|
30
|
+
const id = `\0lattice ${object.id}`;
|
|
31
|
+
model.propertyIds.push(id);
|
|
32
|
+
model.properties.set(id, blended);
|
|
33
|
+
}
|
|
34
|
+
for (const v of corners) {
|
|
35
|
+
const s = made.along[v];
|
|
36
|
+
blended.push([0, 1, 2, 3].map((k) => a[k] + (b[k] - a[k]) * s));
|
|
37
|
+
}
|
|
38
|
+
out.pid.push(blendedAt);
|
|
39
|
+
out.p.push3(blended.length - 3, blended.length - 2, blended.length - 1);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// A property the colour groups do not hold, such as a texture's, gives the surface none here.
|
|
43
|
+
out.pid.push(at < 0 ? NONE - 1 : at);
|
|
44
|
+
const index = first ?? NONE;
|
|
45
|
+
out.p.push3(index, index, index);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/** One mesh of two, each triangle keeping its group colour. */
|
|
52
|
+
function appended(model, object, meshes) {
|
|
53
|
+
const out = { vertices: floats(), triangles: uints(), pid: uints(), p: uints(), flat: [] };
|
|
54
|
+
for (const mesh of meshes) {
|
|
55
|
+
const base = out.vertices.length / 3, count = mesh.vertices.length / 3, source = mesh.vertices.array;
|
|
56
|
+
for (let v = 0; v < count; v++)
|
|
57
|
+
out.vertices.push3(source[v * 3], source[v * 3 + 1], source[v * 3 + 2]);
|
|
58
|
+
for (let t = 0; t < mesh.triangles.length / 3; t++) {
|
|
59
|
+
const [a, b, c] = mesh.triangles.array.subarray(t * 3, t * 3 + 3),
|
|
60
|
+
// An index past its own mesh stays past the joined one, for the reader to refuse.
|
|
61
|
+
at = (v) => (v < count ? base + v : NONE);
|
|
62
|
+
out.triangles.push3(at(a), at(b), at(c));
|
|
63
|
+
out.pid.push(mesh.pid.array[t]);
|
|
64
|
+
out.p.push3(mesh.p.array[t * 3], mesh.p.array[t * 3 + 1], mesh.p.array[t * 3 + 2]);
|
|
65
|
+
const property = cornerProperty(model, object, mesh, t, 0);
|
|
66
|
+
out.flat.push(mesh.flat ? mesh.flat[t] : property && model.properties.get(property.id)?.[property.index]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
/** The lattice step of the evaluating reader. */
|
|
72
|
+
export function lattices(options) {
|
|
73
|
+
/** The colour groups of each model part, whose properties blend along a beam. */
|
|
74
|
+
const gradients = new Map(), found = [];
|
|
75
|
+
const hooks = {
|
|
76
|
+
element(model, name, a) {
|
|
77
|
+
if (name !== 'colorgroup' || !a.id)
|
|
78
|
+
return;
|
|
79
|
+
let ids = gradients.get(model);
|
|
80
|
+
if (!ids)
|
|
81
|
+
gradients.set(model, (ids = new Set()));
|
|
82
|
+
ids.add(a.id);
|
|
83
|
+
},
|
|
84
|
+
bucket: () => '',
|
|
85
|
+
corner() { },
|
|
86
|
+
group() { },
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
hooks,
|
|
90
|
+
async add(model, own, surfaces, modelPath) {
|
|
91
|
+
const { maxTriangles } = limits(options, F);
|
|
92
|
+
for (const [objectId, lattice] of own) {
|
|
93
|
+
const object = model.objects.get(objectId);
|
|
94
|
+
found.push({ objectId, ...(modelPath === undefined ? {} : { modelPath }), lattice });
|
|
95
|
+
let made = shells(model, object, lattice, object.mesh.vertices.result(), gradients.get(model) ?? new Set(), {
|
|
96
|
+
segments: options.beamSegments,
|
|
97
|
+
maxTriangles,
|
|
98
|
+
});
|
|
99
|
+
if (!made.triangles.length)
|
|
100
|
+
continue;
|
|
101
|
+
if (lattice.clippingMode !== 'none') {
|
|
102
|
+
const clip = model.objects.get(lattice.clippingMesh), surface = surfaces.get(lattice.clippingMesh);
|
|
103
|
+
if (!clip || !surface || clip === object)
|
|
104
|
+
throw new LoaderError(F, 'range', `the lattice of object ${objectId.slice(0, 32)} names no clipping mesh`);
|
|
105
|
+
made = await booleanMesh(model, `${objectId} lattice`, lattice.clippingMode === 'inside' ? 'intersection' : 'difference', [
|
|
106
|
+
{ model, object: { ...object, mesh: made }, transform: IDENTITY },
|
|
107
|
+
{ model, object: { ...clip, mesh: surface }, transform: IDENTITY },
|
|
108
|
+
], maxTriangles);
|
|
109
|
+
}
|
|
110
|
+
object.mesh = appended(model, object, [object.mesh, made]);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
result: () => found,
|
|
114
|
+
};
|
|
115
|
+
}
|