@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,268 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
const F = '3MF';
|
|
3
|
+
const arc = (c, radius, from, to) => ({
|
|
4
|
+
from,
|
|
5
|
+
to,
|
|
6
|
+
c,
|
|
7
|
+
radius,
|
|
8
|
+
at: (z) => Math.sqrt(Math.max(0, radius * radius - (z - c) * (z - c))),
|
|
9
|
+
});
|
|
10
|
+
/** The upper envelope of the sources over their spans, which touch or overlap, closed to the axis at both ends. */
|
|
11
|
+
function envelope(sources, frustum) {
|
|
12
|
+
const low = Math.min(...sources.map((s) => s.from)), high = Math.max(...sources.map((s) => s.to)), size = Math.max(high - low, ...sources.map((s) => Math.max(s.at(s.from), s.at(s.to), s.radius ?? 0))), tiny = size * 1e-9, heights = sources.flatMap((s) => [s.from, s.to]), arcs = sources.filter((s) => s.radius !== undefined);
|
|
13
|
+
// Where two curves cross, the envelope changes from one to the other.
|
|
14
|
+
for (const [i, a] of arcs.entries()) {
|
|
15
|
+
if (frustum) {
|
|
16
|
+
const { slope: k, r0 } = frustum, c = a.c, qa = 1 + k * k, qb = 2 * (r0 * k - c), qc = r0 * r0 + c * c - a.radius * a.radius, d = qb * qb - 4 * qa * qc;
|
|
17
|
+
if (d >= 0)
|
|
18
|
+
heights.push((-qb - Math.sqrt(d)) / (2 * qa), (-qb + Math.sqrt(d)) / (2 * qa));
|
|
19
|
+
}
|
|
20
|
+
for (const b of arcs.slice(i + 1))
|
|
21
|
+
if (a.c !== b.c)
|
|
22
|
+
heights.push((a.c * a.c - b.c * b.c - a.radius ** 2 + b.radius ** 2) / (2 * (a.c - b.c)));
|
|
23
|
+
}
|
|
24
|
+
const cuts = heights
|
|
25
|
+
.filter((z) => z >= low && z <= high)
|
|
26
|
+
.sort((a, b) => a - b)
|
|
27
|
+
.filter((z, i, all) => !i || z - all[i - 1] > tiny);
|
|
28
|
+
if (high - cuts[cuts.length - 1] > 0)
|
|
29
|
+
cuts[cuts.length - 1] = high;
|
|
30
|
+
const pieces = [];
|
|
31
|
+
let previous, end = 0;
|
|
32
|
+
for (let i = 0; i + 1 < cuts.length; i++) {
|
|
33
|
+
const z0 = cuts[i], z1 = cuts[i + 1], middle = (z0 + z1) / 2;
|
|
34
|
+
let top;
|
|
35
|
+
for (const s of sources)
|
|
36
|
+
if (s.from <= middle && s.to >= middle && (!top || s.at(middle) > top.at(middle)))
|
|
37
|
+
top = s;
|
|
38
|
+
if (!top)
|
|
39
|
+
continue;
|
|
40
|
+
const r0 = top.at(z0), r1 = top.at(z1), last = pieces[pieces.length - 1];
|
|
41
|
+
// A step of the radius at a height is a flat ring.
|
|
42
|
+
if (Math.abs(r0 - end) > tiny)
|
|
43
|
+
pieces.push({ z0, r0: end, z1: z0, r1: r0 });
|
|
44
|
+
if (top === previous && last.z1 === z0 && Math.abs(r0 - end) <= tiny) {
|
|
45
|
+
last.z1 = z1;
|
|
46
|
+
last.r1 = r1;
|
|
47
|
+
}
|
|
48
|
+
else
|
|
49
|
+
pieces.push({ z0, r0, z1, r1, ...(top.radius === undefined ? {} : { c: top.c, radius: top.radius }) });
|
|
50
|
+
previous = top;
|
|
51
|
+
end = r1;
|
|
52
|
+
}
|
|
53
|
+
if (end > tiny)
|
|
54
|
+
pieces.push({ z0: high, r0: end, z1: high, r1: 0 });
|
|
55
|
+
return pieces;
|
|
56
|
+
}
|
|
57
|
+
function solid(origin, axis, length, sources, frustum) {
|
|
58
|
+
const profile = envelope(sources, frustum), low = [Infinity, Infinity, Infinity], high = [-Infinity, -Infinity, -Infinity];
|
|
59
|
+
let from = Infinity, to = -Infinity, reach = 0;
|
|
60
|
+
for (const s of sources) {
|
|
61
|
+
from = Math.min(from, s.from);
|
|
62
|
+
to = Math.max(to, s.to);
|
|
63
|
+
reach = Math.max(reach, s.radius ?? Math.max(s.at(s.from), s.at(s.to)));
|
|
64
|
+
}
|
|
65
|
+
for (let k = 0; k < 3; k++)
|
|
66
|
+
for (const z of [from, to]) {
|
|
67
|
+
low[k] = Math.min(low[k], origin[k] + axis[k] * z - reach);
|
|
68
|
+
high[k] = Math.max(high[k], origin[k] + axis[k] * z + reach);
|
|
69
|
+
}
|
|
70
|
+
return { origin, axis, length, profile, low, high };
|
|
71
|
+
}
|
|
72
|
+
const point = (vertices, v) => {
|
|
73
|
+
if (!(Number.isInteger(v) && v >= 0 && v * 3 + 2 < vertices.length))
|
|
74
|
+
throw new LoaderError(F, 'range', `lattice vertex ${v} is outside 0..${vertices.length / 3 - 1}`);
|
|
75
|
+
return [vertices[v * 3], vertices[v * 3 + 1], vertices[v * 3 + 2]];
|
|
76
|
+
};
|
|
77
|
+
/** Every beam and ball of a lattice as a solid of revolution. */
|
|
78
|
+
function solidsOf(vertices, lattice) {
|
|
79
|
+
const parts = [];
|
|
80
|
+
const active = lattice.activeBeams ?? lattice.beams.map((_, i) => i);
|
|
81
|
+
for (let n = 0; n < active.length; n++) {
|
|
82
|
+
const at = active[n], beam = lattice.beams[at], a = point(vertices, beam.v1), b = point(vertices, beam.v2), d = [b[0] - a[0], b[1] - a[1], b[2] - a[2]], length = Math.hypot(d[0], d[1], d[2]), { r1, r2, cap1, cap2 } = beam;
|
|
83
|
+
if (!(length > 0 && r1 > 0 && r2 > 0))
|
|
84
|
+
throw new LoaderError(F, 'range', `beam ${at} needs two distinct ends and positive radii`);
|
|
85
|
+
const slope = (r2 - r1) / length, sources = [{ from: 0, to: length, at: (z) => r1 + slope * z }];
|
|
86
|
+
if (cap1 !== 'butt')
|
|
87
|
+
sources.push(arc(0, r1, -r1, cap1 === 'sphere' ? r1 : 0));
|
|
88
|
+
if (cap2 !== 'butt')
|
|
89
|
+
sources.push(arc(length, r2, cap2 === 'sphere' ? length - r2 : length, length + r2));
|
|
90
|
+
parts.push({
|
|
91
|
+
kind: 'beam',
|
|
92
|
+
index: at,
|
|
93
|
+
solid: solid(a, d.map((v) => v / length), length, sources, { slope, r0: r1 }),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
for (const [at, ball] of (lattice.effectiveBalls ?? []).entries()) {
|
|
97
|
+
if (!(ball.radius > 0))
|
|
98
|
+
throw new LoaderError(F, 'range', `ball ${at} needs a positive radius`);
|
|
99
|
+
parts.push({
|
|
100
|
+
kind: 'ball',
|
|
101
|
+
index: at,
|
|
102
|
+
solid: solid(point(vertices, ball.vertex), [0, 0, 1], 0, [arc(0, ball.radius, -ball.radius, ball.radius)]),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return parts;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The surface of a lattice in the vertices' coordinates: one closed, outward triangle shell for each active beam, in
|
|
109
|
+
* their order, then one for each effective ball. The shells overlap where the lattice joins, which the 3MF positive
|
|
110
|
+
* fill rule reads as their union; `batchBooleanMeshes` of `@margoengine/mesh-edit` unions them where one surface is
|
|
111
|
+
* needed. A clipping mesh is not applied here (`parse3MFEvaluated` applies it).
|
|
112
|
+
*
|
|
113
|
+
* @throws LoaderError for a vertex index outside the vertices, a beam without length or radius, a bad option, or more
|
|
114
|
+
* triangles than `maxTriangles`.
|
|
115
|
+
*/
|
|
116
|
+
export function mesh3MFBeamLattice(vertices, lattice, options = {}) {
|
|
117
|
+
const { segments = 16, maxTriangles = 4_000_000 } = options;
|
|
118
|
+
if (!(Number.isInteger(segments) && segments >= 3 && segments <= 256))
|
|
119
|
+
throw new LoaderError(F, 'range', 'segments must be an integer from 3 to 256');
|
|
120
|
+
if (!(Number.isSafeInteger(maxTriangles) && maxTriangles > 0))
|
|
121
|
+
throw new LoaderError(F, 'range', 'maxTriangles must be a positive integer');
|
|
122
|
+
const turn = Array.from({ length: segments }, (_, j) => {
|
|
123
|
+
const angle = (2 * Math.PI * j) / segments;
|
|
124
|
+
return [Math.cos(angle), Math.sin(angle)];
|
|
125
|
+
}), positions = [], along = [], indices = [], parts = [];
|
|
126
|
+
for (const part of solidsOf(vertices, lattice)) {
|
|
127
|
+
const { origin, axis, length, profile } = part.solid,
|
|
128
|
+
// A unit vector across the axis, from the world axis it leans on least.
|
|
129
|
+
least = [0, 1, 2].reduce((m, k) => (Math.abs(axis[k]) < Math.abs(axis[m]) ? k : m), 0), e = [0, 0, 0];
|
|
130
|
+
e[least] = 1;
|
|
131
|
+
const dot = axis[least], e1 = e.map((v, k) => v - dot * axis[k]), size = Math.hypot(e1[0], e1[1], e1[2]);
|
|
132
|
+
for (let k = 0; k < 3; k++)
|
|
133
|
+
e1[k] /= size;
|
|
134
|
+
const e2 = [
|
|
135
|
+
axis[1] * e1[2] - axis[2] * e1[1],
|
|
136
|
+
axis[2] * e1[0] - axis[0] * e1[2],
|
|
137
|
+
axis[0] * e1[1] - axis[1] * e1[0],
|
|
138
|
+
];
|
|
139
|
+
// The profile as points of height and radius.
|
|
140
|
+
const widest = Math.max(...profile.map((p) => p.radius ?? Math.max(p.r0, p.r1))), tiny = widest * 1e-6, line = [];
|
|
141
|
+
const add = (z, r) => {
|
|
142
|
+
const radius = r > tiny ? r : 0, last = line[line.length - 1];
|
|
143
|
+
if (last && Math.abs(last[0] - z) <= tiny && Math.abs(last[1] - radius) <= tiny)
|
|
144
|
+
return;
|
|
145
|
+
line.push([z, radius]);
|
|
146
|
+
};
|
|
147
|
+
add(profile[0].z0, 0);
|
|
148
|
+
for (const { z0, r0, z1, r1, c, radius } of profile) {
|
|
149
|
+
add(z0, r0);
|
|
150
|
+
if (radius !== undefined) {
|
|
151
|
+
const angle = (z, r) => Math.atan2(r, z - c), from = angle(z0, r0), to = angle(z1, r1), steps = Math.max(1, Math.ceil((Math.abs(from - to) / (2 * Math.PI)) * segments - 1e-6));
|
|
152
|
+
for (let i = 1; i < steps; i++) {
|
|
153
|
+
const t = from + ((to - from) * i) / steps;
|
|
154
|
+
add(c + radius * Math.cos(t), radius * Math.sin(t));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
add(z1, r1);
|
|
158
|
+
}
|
|
159
|
+
add(profile[profile.length - 1].z1, 0);
|
|
160
|
+
const triangleStart = indices.length / 3, rings = [];
|
|
161
|
+
for (const [z, r] of line) {
|
|
162
|
+
const ring = [];
|
|
163
|
+
for (let j = 0; j < (r ? segments : 1); j++) {
|
|
164
|
+
ring.push(positions.length / 3);
|
|
165
|
+
for (let k = 0; k < 3; k++)
|
|
166
|
+
positions.push(origin[k] + axis[k] * z + r * (turn[j][0] * e1[k] + turn[j][1] * e2[k]));
|
|
167
|
+
along.push(length ? Math.min(1, Math.max(0, z / length)) : 0);
|
|
168
|
+
}
|
|
169
|
+
rings.push(ring);
|
|
170
|
+
}
|
|
171
|
+
for (let i = 0; i + 1 < rings.length; i++) {
|
|
172
|
+
const a = rings[i], b = rings[i + 1];
|
|
173
|
+
if (a.length === 1 && b.length === 1)
|
|
174
|
+
continue;
|
|
175
|
+
for (let j = 0; j < segments; j++) {
|
|
176
|
+
const k = (j + 1) % segments;
|
|
177
|
+
if (a.length > 1)
|
|
178
|
+
indices.push(a[j], a[k], b[b.length > 1 ? k : 0]);
|
|
179
|
+
if (b.length > 1)
|
|
180
|
+
indices.push(a[a.length > 1 ? j : 0], b[k], b[j]);
|
|
181
|
+
}
|
|
182
|
+
if (indices.length / 3 > maxTriangles)
|
|
183
|
+
throw new LoaderError(F, 'limit', `the beam lattice exceeds ${maxTriangles} triangles; lower segments`);
|
|
184
|
+
}
|
|
185
|
+
parts.push({
|
|
186
|
+
kind: part.kind,
|
|
187
|
+
index: part.index,
|
|
188
|
+
triangleStart,
|
|
189
|
+
triangleCount: indices.length / 3 - triangleStart,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
positions: Float32Array.from(positions),
|
|
194
|
+
indices: Uint32Array.from(indices),
|
|
195
|
+
along: Float32Array.from(along),
|
|
196
|
+
parts,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** The signed distance from a point to one solid of revolution, negative inside. */
|
|
200
|
+
function distanceTo({ origin, axis, profile }, x, y, z) {
|
|
201
|
+
const dx = x - origin[0], dy = y - origin[1], dz = z - origin[2], h = dx * axis[0] + dy * axis[1] + dz * axis[2], r = Math.hypot(dx - h * axis[0], dy - h * axis[1], dz - h * axis[2]);
|
|
202
|
+
let best = Infinity, top = -1;
|
|
203
|
+
for (const { z0, r0, z1, r1, c, radius } of profile) {
|
|
204
|
+
if (radius === undefined) {
|
|
205
|
+
const ez = z1 - z0, er = r1 - r0, t = Math.min(1, Math.max(0, ((h - z0) * ez + (r - r0) * er) / (ez * ez + er * er || 1)));
|
|
206
|
+
best = Math.min(best, Math.hypot(h - z0 - t * ez, r - r0 - t * er));
|
|
207
|
+
if (h >= z0 && h <= z1 && ez > 0)
|
|
208
|
+
top = Math.max(top, r0 + (er * (h - z0)) / ez);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const away = Math.hypot(h - c, r),
|
|
212
|
+
// The nearest point of the circle, when it lies on this arc.
|
|
213
|
+
on = away > 0 ? c + ((h - c) * radius) / away : c;
|
|
214
|
+
if (on >= z0 && on <= z1)
|
|
215
|
+
best = Math.min(best, Math.abs(away - radius));
|
|
216
|
+
else
|
|
217
|
+
best = Math.min(best, Math.hypot(h - z0, r - r0), Math.hypot(h - z1, r - r1));
|
|
218
|
+
if (h >= z0 && h <= z1)
|
|
219
|
+
top = Math.max(top, Math.sqrt(Math.max(0, radius * radius - (h - c) ** 2)));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return r <= top ? -best : best;
|
|
223
|
+
}
|
|
224
|
+
function tree(solids) {
|
|
225
|
+
const low = [0, 1, 2].map((k) => solids.reduce((m, s) => Math.min(m, s.low[k]), Infinity)), high = [0, 1, 2].map((k) => solids.reduce((m, s) => Math.max(m, s.high[k]), -Infinity));
|
|
226
|
+
if (solids.length === 1)
|
|
227
|
+
return { low, high, solid: solids[0] };
|
|
228
|
+
const k = [0, 1, 2].reduce((m, i) => (high[i] - low[i] > high[m] - low[m] ? i : m), 0), sorted = [...solids].sort((a, b) => a.low[k] + a.high[k] - b.low[k] - b.high[k]), half = sorted.length >> 1;
|
|
229
|
+
return { low, high, left: tree(sorted.slice(0, half)), right: tree(sorted.slice(half)) };
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The signed distance to a lattice's beams and balls, negative inside. It is exact outside the lattice and its sign is
|
|
233
|
+
* exact everywhere; inside, where beams overlap, it is the distance to the surface of the beam or ball the point lies
|
|
234
|
+
* deepest in, which is never more than the distance to the lattice's surface. Infinite for a lattice with nothing in
|
|
235
|
+
* it. A clipping mesh is not applied here.
|
|
236
|
+
*
|
|
237
|
+
* @throws LoaderError for a vertex index outside the vertices or a beam without length or radius.
|
|
238
|
+
*/
|
|
239
|
+
export function threeMFBeamDistance(vertices, lattice) {
|
|
240
|
+
const solids = solidsOf(vertices, lattice).map((p) => p.solid);
|
|
241
|
+
if (!solids.length)
|
|
242
|
+
return () => Infinity;
|
|
243
|
+
const root = tree(solids);
|
|
244
|
+
const away = ({ low, high }, x, y, z) => Math.hypot(Math.max(low[0] - x, 0, x - high[0]), Math.max(low[1] - y, 0, y - high[1]), Math.max(low[2] - z, 0, z - high[2]));
|
|
245
|
+
return (x, y, z) => {
|
|
246
|
+
let best = Infinity;
|
|
247
|
+
const visit = (node, near) => {
|
|
248
|
+
// A solid is no nearer than its box; one whose box holds the point may be deeper than the best so far.
|
|
249
|
+
if (near > 0 && near >= best)
|
|
250
|
+
return;
|
|
251
|
+
if (node.solid) {
|
|
252
|
+
best = Math.min(best, distanceTo(node.solid, x, y, z));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const a = away(node.left, x, y, z), b = away(node.right, x, y, z);
|
|
256
|
+
if (a <= b) {
|
|
257
|
+
visit(node.left, a);
|
|
258
|
+
visit(node.right, b);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
visit(node.right, b);
|
|
262
|
+
visit(node.left, a);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
visit(root, away(root, x, y, z));
|
|
266
|
+
return best;
|
|
267
|
+
};
|
|
268
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ThreeMFBooleanOptions, ThreeMFBooleanShape } from './threemf-boolean.js';
|
|
2
|
+
import type { ThreeMFExtension } from './threemf.js';
|
|
3
|
+
/**
|
|
4
|
+
* The boolean reader's hooks. `others` says that hooks before these turn displacement meshes, level sets and lattices
|
|
5
|
+
* into triangles, so such an object may be a base.
|
|
6
|
+
*/
|
|
7
|
+
export declare function booleanStage(options: ThreeMFBooleanOptions, others?: boolean): {
|
|
8
|
+
hooks: ThreeMFExtension;
|
|
9
|
+
shapes: ThreeMFBooleanShape[];
|
|
10
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { booleanMesh } from './threemf-csg.js';
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { limits, throwIfAborted } from './util.js';
|
|
4
|
+
const F = '3MF';
|
|
5
|
+
const OPERATIONS = ['union', 'difference', 'intersection'];
|
|
6
|
+
const bad = (message) => {
|
|
7
|
+
throw new LoaderError(F, 'syntax', message);
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The boolean reader's hooks. `others` says that hooks before these turn displacement meshes, level sets and lattices
|
|
11
|
+
* into triangles, so such an object may be a base.
|
|
12
|
+
*/
|
|
13
|
+
export function booleanStage(options, others = false) {
|
|
14
|
+
const evaluate = options.evaluate !== false;
|
|
15
|
+
const shapes = [], listed = new Map(),
|
|
16
|
+
/** Objects with a shape of another extension, which no operand may have. */
|
|
17
|
+
extended = new Map(), current = new Map();
|
|
18
|
+
let built = 0;
|
|
19
|
+
const hooks = {
|
|
20
|
+
booleans: true,
|
|
21
|
+
welded: true,
|
|
22
|
+
// A base may be another extension's shape.
|
|
23
|
+
levelSets: true,
|
|
24
|
+
element(model, name, a) {
|
|
25
|
+
if (name === 'object')
|
|
26
|
+
current.set(model, a.id);
|
|
27
|
+
else if (name === 'displacementmesh' || name === 'beamlattice' || name === 'levelset') {
|
|
28
|
+
let ids = extended.get(model);
|
|
29
|
+
if (!ids)
|
|
30
|
+
extended.set(model, (ids = new Set()));
|
|
31
|
+
ids.add(current.get(model));
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
instance(model, object, m, scale) {
|
|
35
|
+
if (evaluate && object.levelSet && !others)
|
|
36
|
+
throw new LoaderError(F, 'unsupported', `object ${object.id.slice(0, 32)} is a volumetric level set; parse3MFBoolean meshes none (parse3MFEvaluated from @margoengine/loaders/threemf-evaluate does; evaluate: false reads the shapes)`);
|
|
37
|
+
const found = listed.get(model)?.get(object.id);
|
|
38
|
+
if (!found)
|
|
39
|
+
return;
|
|
40
|
+
found.instances.push(m.map((v) => v * scale));
|
|
41
|
+
built++;
|
|
42
|
+
},
|
|
43
|
+
hasGeometry: () => built > 0,
|
|
44
|
+
async ready(_read, { models, partPath }) {
|
|
45
|
+
const root = models.values().next().value;
|
|
46
|
+
// A part that another names is finished before the part that names it.
|
|
47
|
+
for (const model of [...models.values()].reverse()) {
|
|
48
|
+
const own = new Map(), order = [...model.objects.keys()], modelPath = partPath(model);
|
|
49
|
+
listed.set(model, own);
|
|
50
|
+
for (const object of model.objects.values()) {
|
|
51
|
+
const { id, boolean: data } = object;
|
|
52
|
+
if (!data)
|
|
53
|
+
continue;
|
|
54
|
+
const label = `boolean shape ${id.slice(0, 32)}`;
|
|
55
|
+
if (!OPERATIONS.includes(data.operation))
|
|
56
|
+
bad(`${label} has the unknown operation ${data.operation.slice(0, 32)}`);
|
|
57
|
+
if (object.mesh || object.components.length)
|
|
58
|
+
bad(`${label} also has a mesh or components`);
|
|
59
|
+
if (object.pid !== undefined || object.pindex !== undefined)
|
|
60
|
+
bad(`${label} takes no pid or pindex`);
|
|
61
|
+
if (!data.operands.length)
|
|
62
|
+
bad(`${label} needs a boolean element`);
|
|
63
|
+
const operand = (reference, isBase) => {
|
|
64
|
+
const { objectId, path, transform } = reference;
|
|
65
|
+
if (path !== undefined && model !== root)
|
|
66
|
+
bad(`${label}: only the root model part may name another part`);
|
|
67
|
+
const owner = path ? models.get(path.replace(/\\/g, '/').replace(/^\/+/, '').toLowerCase()) : model, target = owner.objects.get(objectId), what = `${label}: object ${objectId.slice(0, 32)}`;
|
|
68
|
+
// Defined before it is named, so no shape reads itself.
|
|
69
|
+
if (!target || (owner === model && order.indexOf(objectId) >= order.indexOf(id)))
|
|
70
|
+
bad(`${what} is not defined before it`);
|
|
71
|
+
if (target.type !== 'model' || target.components.length || target.alternatives.length)
|
|
72
|
+
bad(`${what} must be a model shape without components or alternatives`);
|
|
73
|
+
if (!isBase && (!target.mesh || target.boolean || extended.get(owner)?.has(objectId)))
|
|
74
|
+
bad(`${what} must be a plain triangle mesh`);
|
|
75
|
+
const mesh = target.boolean ? undefined : target.mesh, ownerPath = partPath(owner);
|
|
76
|
+
return {
|
|
77
|
+
objectId,
|
|
78
|
+
...(ownerPath === undefined ? {} : { modelPath: ownerPath }),
|
|
79
|
+
transform,
|
|
80
|
+
...(mesh ? { mesh: { positions: mesh.vertices.result(), indices: mesh.triangles.result() } } : {}),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const shape = {
|
|
84
|
+
objectId: id,
|
|
85
|
+
...(modelPath === undefined ? {} : { modelPath }),
|
|
86
|
+
...(object.name === undefined ? {} : { name: object.name }),
|
|
87
|
+
operation: data.operation,
|
|
88
|
+
base: operand(data.base, true),
|
|
89
|
+
operands: data.operands.map((o) => operand(o, false)),
|
|
90
|
+
instances: [],
|
|
91
|
+
};
|
|
92
|
+
own.set(id, shape);
|
|
93
|
+
shapes.push(shape);
|
|
94
|
+
if (!evaluate)
|
|
95
|
+
continue;
|
|
96
|
+
throwIfAborted(options.signal, F);
|
|
97
|
+
const inputs = [data.base, ...data.operands].map(({ objectId, path, transform }) => {
|
|
98
|
+
const owner = path ? models.get(path.replace(/\\/g, '/').replace(/^\/+/, '').toLowerCase()) : model, target = owner.objects.get(objectId);
|
|
99
|
+
if ((!others && extended.get(owner)?.has(objectId)) || !target.mesh)
|
|
100
|
+
throw new LoaderError(F, 'unsupported', `${label}: its base ${objectId.slice(0, 32)} is a displacement mesh, a level set or another extension's shape, which parse3MFBoolean does not evaluate (parse3MFEvaluated from @margoengine/loaders/threemf-evaluate does; evaluate: false reads the shapes)`);
|
|
101
|
+
return { model: owner, object: target, transform };
|
|
102
|
+
});
|
|
103
|
+
// Later shapes read this one as a mesh.
|
|
104
|
+
object.mesh = await booleanMesh(model, id, shape.operation, inputs, limits(options, F).maxTriangles);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
bucket: () => '',
|
|
109
|
+
corner() { },
|
|
110
|
+
group() { },
|
|
111
|
+
};
|
|
112
|
+
return { hooks, shapes };
|
|
113
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type ThreeMFOptions, type ThreeMFResult } from './threemf.js';
|
|
2
|
+
import type { ThreeMFVolumeSamples } from './threemf-volumetric.js';
|
|
3
|
+
import type { BinaryInput } from './util.js';
|
|
4
|
+
declare const OPERATIONS: readonly ['union', 'difference', 'intersection'];
|
|
5
|
+
/** An object a boolean shape reads. */
|
|
6
|
+
export interface ThreeMFBooleanOperand {
|
|
7
|
+
objectId: string;
|
|
8
|
+
/** Its model part, when not the root. */
|
|
9
|
+
modelPath?: string;
|
|
10
|
+
/** Twelve numbers as a 3MF transform writes them, applied before the operation. */
|
|
11
|
+
transform: Float64Array;
|
|
12
|
+
/** Its triangles in its own coordinates and the model's unit; absent for a shape without them. */
|
|
13
|
+
mesh?: {
|
|
14
|
+
positions: Float32Array;
|
|
15
|
+
indices: Uint32Array;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* With `parse3MFEvaluated`, when the object has volume data: reads it at points in the object's own coordinates,
|
|
19
|
+
* which `transform` takes to the shape's.
|
|
20
|
+
*/
|
|
21
|
+
sample?(positions: ArrayLike<number>): Promise<ThreeMFVolumeSamples>;
|
|
22
|
+
}
|
|
23
|
+
/** A boolean shape: `operands` applied to `base` in order. */
|
|
24
|
+
export interface ThreeMFBooleanShape {
|
|
25
|
+
objectId: string;
|
|
26
|
+
modelPath?: string;
|
|
27
|
+
name?: string;
|
|
28
|
+
operation: (typeof OPERATIONS)[number];
|
|
29
|
+
base: ThreeMFBooleanOperand;
|
|
30
|
+
operands: ThreeMFBooleanOperand[];
|
|
31
|
+
/**
|
|
32
|
+
* Each place the build puts the shape: twelve numbers as a 3MF transform writes them, from the shape's coordinates to
|
|
33
|
+
* the result's units. None for a shape that only another shape reads.
|
|
34
|
+
*/
|
|
35
|
+
instances: Float64Array[];
|
|
36
|
+
}
|
|
37
|
+
/** Options of {@link parse3MFBoolean}. */
|
|
38
|
+
export interface ThreeMFBooleanOptions extends ThreeMFOptions {
|
|
39
|
+
/** Evaluate each boolean shape and add its surface to the result. Default true. */
|
|
40
|
+
evaluate?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** The result of {@link parse3MFBoolean}. */
|
|
43
|
+
export interface ThreeMFBooleanResult extends ThreeMFResult {
|
|
44
|
+
/** Every boolean shape, each after the shapes it reads. */
|
|
45
|
+
shapes: ThreeMFBooleanShape[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parses a 3MF package with the boolean operations extension. Each boolean shape is evaluated exactly by
|
|
49
|
+
* `@margoengine/mesh-edit` and its surface joins the result's triangles where the build places it, with the colours of
|
|
50
|
+
* the surfaces it came from; the shapes are returned beside them with their operands' triangles and placements. A base
|
|
51
|
+
* that is a displacement mesh or a level set is not evaluated here (`parse3MFEvaluated` evaluates one).
|
|
52
|
+
*
|
|
53
|
+
* @throws LoaderError for what the extension rules out (an unknown operation, a property on a shape, a base that is not
|
|
54
|
+
* a `model` shape or has alternatives, an operand that is not a plain `model` mesh, a shape that reads itself or a later
|
|
55
|
+
* object), an input that is not a closed solid, a base this entry does not evaluate, a built level set, and what
|
|
56
|
+
* `parse3MF` throws.
|
|
57
|
+
*/
|
|
58
|
+
export declare function parse3MFBoolean(input: BinaryInput, options?: ThreeMFBooleanOptions): Promise<ThreeMFBooleanResult>;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Reads the 3MF Consortium's Boolean Operations Extension (https://github.com/3MFConsortium/spec_booleans): the shapes
|
|
2
|
+
// made by union, difference and intersection, and the meshes they name.
|
|
3
|
+
import { read3MF } from './threemf.js';
|
|
4
|
+
import { booleanStage } from './threemf-boolean-read.js';
|
|
5
|
+
const OPERATIONS = ['union', 'difference', 'intersection'];
|
|
6
|
+
/**
|
|
7
|
+
* Parses a 3MF package with the boolean operations extension. Each boolean shape is evaluated exactly by
|
|
8
|
+
* `@margoengine/mesh-edit` and its surface joins the result's triangles where the build places it, with the colours of
|
|
9
|
+
* the surfaces it came from; the shapes are returned beside them with their operands' triangles and placements. A base
|
|
10
|
+
* that is a displacement mesh or a level set is not evaluated here (`parse3MFEvaluated` evaluates one).
|
|
11
|
+
*
|
|
12
|
+
* @throws LoaderError for what the extension rules out (an unknown operation, a property on a shape, a base that is not
|
|
13
|
+
* a `model` shape or has alternatives, an operand that is not a plain `model` mesh, a shape that reads itself or a later
|
|
14
|
+
* object), an input that is not a closed solid, a base this entry does not evaluate, a built level set, and what
|
|
15
|
+
* `parse3MF` throws.
|
|
16
|
+
*/
|
|
17
|
+
export async function parse3MFBoolean(input, options = {}) {
|
|
18
|
+
const stage = booleanStage(options), read = await read3MF(input, options, stage.hooks);
|
|
19
|
+
return { ...read, shapes: stage.shapes };
|
|
20
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ThreeMFMesh, type ThreeMFModel, type ThreeMFObject } from './threemf.js';
|
|
2
|
+
/** One input of a boolean shape: an object with triangles, and the transform applied to it first. */
|
|
3
|
+
export interface BooleanInput {
|
|
4
|
+
model: ThreeMFModel;
|
|
5
|
+
object: ThreeMFObject;
|
|
6
|
+
transform: ArrayLike<number>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The triangles of `inputs[0] operation inputs[1..]` in the shape's coordinates and the model's unit, with its colours
|
|
10
|
+
* added to `model`.
|
|
11
|
+
*
|
|
12
|
+
* @throws LoaderError for an input that is not a closed, outward solid, and for more than `maxTriangles`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function booleanMesh(model: ThreeMFModel, id: string, operation: 'union' | 'difference' | 'intersection', inputs: readonly BooleanInput[], maxTriangles: number): Promise<ThreeMFMesh>;
|