formacad 0.0.0-stage → 0.1.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 +200 -0
- package/README.md +87 -2
- package/build/cli/src/forma.d.ts +2 -0
- package/build/cli/src/forma.js +285 -0
- package/build/cli/src/forma.js.map +1 -0
- package/build/cli/src/preview.d.ts +3 -0
- package/build/cli/src/preview.js +268 -0
- package/build/cli/src/preview.js.map +1 -0
- package/build/core/fonts/Roboto-Bold.ttf +0 -0
- package/build/core/fonts/Roboto-Regular.ttf +0 -0
- package/build/core/src/bevel.d.ts +41 -0
- package/build/core/src/bevel.js +548 -0
- package/build/core/src/bevel.js.map +1 -0
- package/build/core/src/difflib.d.ts +3 -0
- package/build/core/src/difflib.js +94 -0
- package/build/core/src/difflib.js.map +1 -0
- package/build/core/src/errors.d.ts +9 -0
- package/build/core/src/errors.js +14 -0
- package/build/core/src/errors.js.map +1 -0
- package/build/core/src/export2d.d.ts +7 -0
- package/build/core/src/export2d.js +79 -0
- package/build/core/src/export2d.js.map +1 -0
- package/build/core/src/geometry.d.ts +92 -0
- package/build/core/src/geometry.js +946 -0
- package/build/core/src/geometry.js.map +1 -0
- package/build/core/src/host.d.ts +28 -0
- package/build/core/src/host.js +113 -0
- package/build/core/src/host.js.map +1 -0
- package/build/core/src/importers.d.ts +11 -0
- package/build/core/src/importers.js +711 -0
- package/build/core/src/importers.js.map +1 -0
- package/build/core/src/index.d.ts +81 -0
- package/build/core/src/index.js +155 -0
- package/build/core/src/index.js.map +1 -0
- package/build/core/src/inflate.d.ts +15 -0
- package/build/core/src/inflate.js +230 -0
- package/build/core/src/inflate.js.map +1 -0
- package/build/core/src/interp.d.ts +142 -0
- package/build/core/src/interp.js +2026 -0
- package/build/core/src/interp.js.map +1 -0
- package/build/core/src/lexer.d.ts +14 -0
- package/build/core/src/lexer.js +217 -0
- package/build/core/src/lexer.js.map +1 -0
- package/build/core/src/manifold.d.ts +26 -0
- package/build/core/src/manifold.js +687 -0
- package/build/core/src/manifold.js.map +1 -0
- package/build/core/src/parser.d.ts +46 -0
- package/build/core/src/parser.js +408 -0
- package/build/core/src/parser.js.map +1 -0
- package/build/core/src/std.d.ts +3 -0
- package/build/core/src/std.js +47 -0
- package/build/core/src/std.js.map +1 -0
- package/build/core/src/threemf.d.ts +4 -0
- package/build/core/src/threemf.js +127 -0
- package/build/core/src/threemf.js.map +1 -0
- package/build/core/src/ttf.d.ts +39 -0
- package/build/core/src/ttf.js +473 -0
- package/build/core/src/ttf.js.map +1 -0
- package/build/core/src/values.d.ts +116 -0
- package/build/core/src/values.js +294 -0
- package/build/core/src/values.js.map +1 -0
- package/build/core/src/xml.d.ts +13 -0
- package/build/core/src/xml.js +152 -0
- package/build/core/src/xml.js.map +1 -0
- package/build/core/std/electronics.forma +120 -0
- package/build/core/std/enclosures.forma +76 -0
- package/build/core/std/fasteners.forma +170 -0
- package/build/core/std/gridfinity.forma +182 -0
- package/build/core/std/joints.forma +120 -0
- package/build/core/std/mechanics.forma +276 -0
- package/build/core/std/patterns.forma +138 -0
- package/build/core/std/threads.forma +152 -0
- package/package.json +58 -4
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
// Manifold WASM geometry backend for fast, guaranteed-manifold 3D booleans and convex hulls.
|
|
2
|
+
// Loads `manifold-3d` in Node.js or from `../../vendor/manifold-3d/` in browsers / Web Workers.
|
|
3
|
+
// Falls back cleanly to null (letting geometry.ts use the pure-JS BSP kernel) if WASM is unavailable
|
|
4
|
+
// or if an input mesh is non-manifold (e.g., an unclosed imported STL/OBJ).
|
|
5
|
+
import { Poly } from './geometry.js';
|
|
6
|
+
let wasm = null;
|
|
7
|
+
const colorToId = new Map();
|
|
8
|
+
const idToColor = new Map();
|
|
9
|
+
async function loadManifold() {
|
|
10
|
+
try {
|
|
11
|
+
let factory;
|
|
12
|
+
let wasmUrl;
|
|
13
|
+
const isNode = Boolean(globalThis.process?.versions?.node);
|
|
14
|
+
if (isNode) {
|
|
15
|
+
const mod = await import('manifold-3d');
|
|
16
|
+
factory = mod.default;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const jsUrl = new URL('../../vendor/manifold-3d/manifold.js', import.meta.url).href;
|
|
20
|
+
wasmUrl = new URL('../../vendor/manifold-3d/manifold.wasm', import.meta.url).href;
|
|
21
|
+
const mod = await import(/* @vite-ignore */ jsUrl);
|
|
22
|
+
factory = mod.default;
|
|
23
|
+
}
|
|
24
|
+
const inst = await factory(wasmUrl ? { locateFile: () => wasmUrl } : undefined);
|
|
25
|
+
inst.setup();
|
|
26
|
+
return inst;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// Top-level await ensures Manifold is ready before any synchronous `build()` call in ESM.
|
|
33
|
+
wasm = await loadManifold();
|
|
34
|
+
export function isManifoldReady() {
|
|
35
|
+
return wasm !== null;
|
|
36
|
+
}
|
|
37
|
+
function getColorId(color) {
|
|
38
|
+
let id = colorToId.get(color);
|
|
39
|
+
if (id === undefined) {
|
|
40
|
+
id = wasm.Manifold.reserveIDs(1);
|
|
41
|
+
colorToId.set(color, id);
|
|
42
|
+
idToColor.set(id, color);
|
|
43
|
+
}
|
|
44
|
+
return id;
|
|
45
|
+
}
|
|
46
|
+
/** Convert Forma Poly[] to a Manifold, preserving per-polygon colors via originalID runs. */
|
|
47
|
+
function polysToManifold(polys) {
|
|
48
|
+
if (!wasm || !polys.length)
|
|
49
|
+
return null;
|
|
50
|
+
// Deduplicate vertices at float32 precision and group triangles by color.
|
|
51
|
+
const vertMap = new Map();
|
|
52
|
+
const verts = [];
|
|
53
|
+
const byColor = new Map();
|
|
54
|
+
const addVert = (v) => {
|
|
55
|
+
const x = Math.fround(v[0]) || 0;
|
|
56
|
+
const y = Math.fround(v[1]) || 0;
|
|
57
|
+
const z = Math.fround(v[2]) || 0;
|
|
58
|
+
const key = x + ',' + y + ',' + z;
|
|
59
|
+
let idx = vertMap.get(key);
|
|
60
|
+
if (idx === undefined) {
|
|
61
|
+
idx = verts.length / 3;
|
|
62
|
+
vertMap.set(key, idx);
|
|
63
|
+
verts.push(x, y, z);
|
|
64
|
+
}
|
|
65
|
+
return idx;
|
|
66
|
+
};
|
|
67
|
+
let totalTriIndices = 0;
|
|
68
|
+
for (let pi = 0; pi < polys.length; pi++) {
|
|
69
|
+
const p = polys[pi];
|
|
70
|
+
const vs = p.v;
|
|
71
|
+
const len = vs.length;
|
|
72
|
+
if (len < 3)
|
|
73
|
+
continue;
|
|
74
|
+
let bucket = byColor.get(p.c);
|
|
75
|
+
if (!bucket) {
|
|
76
|
+
bucket = [];
|
|
77
|
+
byColor.set(p.c, bucket);
|
|
78
|
+
}
|
|
79
|
+
const i0 = addVert(vs[0]);
|
|
80
|
+
for (let i = 1; i < len - 1; i++) {
|
|
81
|
+
const i1 = addVert(vs[i]);
|
|
82
|
+
const i2 = addVert(vs[i + 1]);
|
|
83
|
+
if (i0 !== i1 && i1 !== i2 && i2 !== i0) {
|
|
84
|
+
bucket.push(i0, i1, i2);
|
|
85
|
+
totalTriIndices += 3;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (totalTriIndices === 0)
|
|
90
|
+
return null;
|
|
91
|
+
const vertProperties = new Float32Array(verts);
|
|
92
|
+
const triVerts = new Uint32Array(totalTriIndices);
|
|
93
|
+
const runCount = byColor.size;
|
|
94
|
+
const runIndex = new Uint32Array(runCount + 1);
|
|
95
|
+
const runOriginalID = new Uint32Array(runCount);
|
|
96
|
+
let offset = 0;
|
|
97
|
+
let r = 0;
|
|
98
|
+
for (const [color, tris] of byColor) {
|
|
99
|
+
runIndex[r] = offset;
|
|
100
|
+
runOriginalID[r] = getColorId(color);
|
|
101
|
+
triVerts.set(tris, offset);
|
|
102
|
+
offset += tris.length;
|
|
103
|
+
r++;
|
|
104
|
+
}
|
|
105
|
+
runIndex[runCount] = offset;
|
|
106
|
+
const mesh = new wasm.Mesh({
|
|
107
|
+
numProp: 3,
|
|
108
|
+
vertProperties,
|
|
109
|
+
triVerts,
|
|
110
|
+
runIndex,
|
|
111
|
+
runOriginalID,
|
|
112
|
+
});
|
|
113
|
+
mesh.merge();
|
|
114
|
+
let m;
|
|
115
|
+
try {
|
|
116
|
+
m = wasm.Manifold.ofMesh(mesh);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (m.status() !== 'NoError') {
|
|
122
|
+
m.delete();
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return m;
|
|
126
|
+
}
|
|
127
|
+
/** Convert a Manifold back to Forma Poly[], restoring per-triangle colors from runOriginalID. */
|
|
128
|
+
function manifoldToPolys(m) {
|
|
129
|
+
if (m.isEmpty())
|
|
130
|
+
return [];
|
|
131
|
+
const mesh = m.getMesh();
|
|
132
|
+
const vp = mesh.vertProperties;
|
|
133
|
+
const tv = mesh.triVerts;
|
|
134
|
+
const stride = mesh.numProp;
|
|
135
|
+
const out = [];
|
|
136
|
+
const pushRange = (start, end, color) => {
|
|
137
|
+
for (let t = start; t < end; t += 3) {
|
|
138
|
+
const a0 = tv[t] * stride;
|
|
139
|
+
const b0 = tv[t + 1] * stride;
|
|
140
|
+
const c0 = tv[t + 2] * stride;
|
|
141
|
+
const ax = vp[a0], ay = vp[a0 + 1], az = vp[a0 + 2];
|
|
142
|
+
const bx = vp[b0], by = vp[b0 + 1], bz = vp[b0 + 2];
|
|
143
|
+
const cx = vp[c0], cy = vp[c0 + 1], cz = vp[c0 + 2];
|
|
144
|
+
const ux = bx - ax, uy = by - ay, uz = bz - az;
|
|
145
|
+
const vx = cx - ax, vy = cy - ay, vz = cz - az;
|
|
146
|
+
const nx = uy * vz - uz * vy;
|
|
147
|
+
const ny = uz * vx - ux * vz;
|
|
148
|
+
const nz = ux * vy - uy * vx;
|
|
149
|
+
const ln = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
150
|
+
if (ln < 1e-12)
|
|
151
|
+
continue;
|
|
152
|
+
const inv = 1 / ln;
|
|
153
|
+
const nnx = nx * inv, nny = ny * inv, nnz = nz * inv;
|
|
154
|
+
const w = nnx * ax + nny * ay + nnz * az;
|
|
155
|
+
out.push(new Poly([[ax, ay, az], [bx, by, bz], [cx, cy, cz]], color, [nnx, nny, nnz], w));
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
if (mesh.runOriginalID && mesh.runOriginalID.length > 0 && mesh.runIndex && mesh.runIndex.length > mesh.runOriginalID.length) {
|
|
159
|
+
for (let r = 0; r < mesh.runOriginalID.length; r++) {
|
|
160
|
+
const color = idToColor.get(mesh.runOriginalID[r]) ?? null;
|
|
161
|
+
pushRange(mesh.runIndex[r], mesh.runIndex[r + 1], color);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
pushRange(0, tv.length, null);
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
function disposeAll(list) {
|
|
170
|
+
for (const m of list) {
|
|
171
|
+
try {
|
|
172
|
+
m.delete();
|
|
173
|
+
}
|
|
174
|
+
catch { /* already deleted */ }
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** N-way boolean union using Manifold. Returns null if Manifold is unavailable or any input is non-manifold. */
|
|
178
|
+
export function manifoldUnion(sets) {
|
|
179
|
+
if (!wasm)
|
|
180
|
+
return null;
|
|
181
|
+
const allocated = [];
|
|
182
|
+
try {
|
|
183
|
+
const inputs = [];
|
|
184
|
+
for (const s of sets) {
|
|
185
|
+
if (!s.length)
|
|
186
|
+
continue;
|
|
187
|
+
const m = polysToManifold(s);
|
|
188
|
+
if (!m)
|
|
189
|
+
return null;
|
|
190
|
+
allocated.push(m);
|
|
191
|
+
inputs.push(m);
|
|
192
|
+
}
|
|
193
|
+
if (!inputs.length)
|
|
194
|
+
return [];
|
|
195
|
+
if (inputs.length === 1)
|
|
196
|
+
return sets.find((s) => s.length).slice();
|
|
197
|
+
const res = wasm.Manifold.union(inputs);
|
|
198
|
+
allocated.push(res);
|
|
199
|
+
if (res.status() !== 'NoError')
|
|
200
|
+
return null;
|
|
201
|
+
return manifoldToPolys(res);
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
disposeAll(allocated);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** N-way boolean difference (base minus all cutters) in one Manifold pass. */
|
|
208
|
+
export function manifoldDifference(base, cutters) {
|
|
209
|
+
if (!wasm)
|
|
210
|
+
return null;
|
|
211
|
+
const allocated = [];
|
|
212
|
+
try {
|
|
213
|
+
const mBase = polysToManifold(base);
|
|
214
|
+
if (!mBase)
|
|
215
|
+
return null;
|
|
216
|
+
allocated.push(mBase);
|
|
217
|
+
const list = [mBase];
|
|
218
|
+
for (const c of cutters) {
|
|
219
|
+
if (!c.length)
|
|
220
|
+
continue;
|
|
221
|
+
const mc = polysToManifold(c);
|
|
222
|
+
if (!mc)
|
|
223
|
+
return null;
|
|
224
|
+
allocated.push(mc);
|
|
225
|
+
list.push(mc);
|
|
226
|
+
}
|
|
227
|
+
if (list.length === 1)
|
|
228
|
+
return base.slice();
|
|
229
|
+
const res = wasm.Manifold.difference(list);
|
|
230
|
+
allocated.push(res);
|
|
231
|
+
if (res.status() !== 'NoError')
|
|
232
|
+
return null;
|
|
233
|
+
return manifoldToPolys(res);
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
disposeAll(allocated);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** N-way boolean intersection in one Manifold pass. */
|
|
240
|
+
export function manifoldIntersection(sets) {
|
|
241
|
+
if (!wasm)
|
|
242
|
+
return null;
|
|
243
|
+
const allocated = [];
|
|
244
|
+
try {
|
|
245
|
+
const inputs = [];
|
|
246
|
+
for (const s of sets) {
|
|
247
|
+
if (!s.length)
|
|
248
|
+
return [];
|
|
249
|
+
const m = polysToManifold(s);
|
|
250
|
+
if (!m)
|
|
251
|
+
return null;
|
|
252
|
+
allocated.push(m);
|
|
253
|
+
inputs.push(m);
|
|
254
|
+
}
|
|
255
|
+
if (!inputs.length)
|
|
256
|
+
return [];
|
|
257
|
+
if (inputs.length === 1)
|
|
258
|
+
return sets[0].slice();
|
|
259
|
+
const res = wasm.Manifold.intersection(inputs);
|
|
260
|
+
allocated.push(res);
|
|
261
|
+
if (res.status() !== 'NoError')
|
|
262
|
+
return null;
|
|
263
|
+
return manifoldToPolys(res);
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
disposeAll(allocated);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/** 3D convex hull of a point cloud using Manifold's C++ Quickhull. Returns null if flat or unavailable. */
|
|
270
|
+
export function manifoldHull3d(points) {
|
|
271
|
+
if (!wasm || points.length < 4)
|
|
272
|
+
return null;
|
|
273
|
+
let m = null;
|
|
274
|
+
try {
|
|
275
|
+
m = wasm.Manifold.hull(points);
|
|
276
|
+
if (m.status() !== 'NoError' || m.isEmpty())
|
|
277
|
+
return null;
|
|
278
|
+
return manifoldToPolys(m);
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
if (m)
|
|
285
|
+
try {
|
|
286
|
+
m.delete();
|
|
287
|
+
}
|
|
288
|
+
catch { /* ignore */ }
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** Helper: convert a Manifold CrossSection to Forma's 2D thin prism (z = 0..1). */
|
|
292
|
+
function crossSectionToPrism(cs, color) {
|
|
293
|
+
if (cs.isEmpty())
|
|
294
|
+
return [];
|
|
295
|
+
let extruded = null;
|
|
296
|
+
try {
|
|
297
|
+
extruded = cs.extrude(1);
|
|
298
|
+
if (extruded.status() !== 'NoError' || extruded.isEmpty())
|
|
299
|
+
return [];
|
|
300
|
+
const polys = manifoldToPolys(extruded);
|
|
301
|
+
if (color !== null)
|
|
302
|
+
for (const p of polys)
|
|
303
|
+
p.c = color;
|
|
304
|
+
return polys;
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
if (extruded)
|
|
308
|
+
try {
|
|
309
|
+
extruded.delete();
|
|
310
|
+
}
|
|
311
|
+
catch { /* ignore */ }
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function uniformColor(polys) {
|
|
315
|
+
if (!polys.length)
|
|
316
|
+
return null;
|
|
317
|
+
const c = polys[0].c;
|
|
318
|
+
if (c === null)
|
|
319
|
+
return null;
|
|
320
|
+
for (let i = 1; i < polys.length; i++)
|
|
321
|
+
if (polys[i].c !== c)
|
|
322
|
+
return null;
|
|
323
|
+
return c;
|
|
324
|
+
}
|
|
325
|
+
/** 2D offset (grow/shrink) of a Forma 2D shape (z = 0..1 prism) via Manifold CrossSection (Clipper2). */
|
|
326
|
+
export function manifoldOffset2d(polys, delta, join, miterLimit, circularSegments) {
|
|
327
|
+
if (!wasm)
|
|
328
|
+
return null;
|
|
329
|
+
if (!polys.length)
|
|
330
|
+
return [];
|
|
331
|
+
if (Math.abs(delta) < 1e-12)
|
|
332
|
+
return polys.slice();
|
|
333
|
+
let m = null;
|
|
334
|
+
let cs = null;
|
|
335
|
+
let off = null;
|
|
336
|
+
let simp = null;
|
|
337
|
+
try {
|
|
338
|
+
m = polysToManifold(polys);
|
|
339
|
+
if (!m)
|
|
340
|
+
return null;
|
|
341
|
+
cs = m.slice(0.5);
|
|
342
|
+
if (cs.isEmpty())
|
|
343
|
+
return [];
|
|
344
|
+
const joinType = join === 'miter' ? 'Miter' : join === 'square' ? 'Square' : 'Round';
|
|
345
|
+
off = cs.offset(delta, joinType, miterLimit, circularSegments);
|
|
346
|
+
simp = off.simplify(1e-6);
|
|
347
|
+
return crossSectionToPrism(simp, uniformColor(polys));
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
finally {
|
|
353
|
+
if (simp)
|
|
354
|
+
try {
|
|
355
|
+
simp.delete();
|
|
356
|
+
}
|
|
357
|
+
catch { /* ignore */ }
|
|
358
|
+
if (off)
|
|
359
|
+
try {
|
|
360
|
+
off.delete();
|
|
361
|
+
}
|
|
362
|
+
catch { /* ignore */ }
|
|
363
|
+
if (cs)
|
|
364
|
+
try {
|
|
365
|
+
cs.delete();
|
|
366
|
+
}
|
|
367
|
+
catch { /* ignore */ }
|
|
368
|
+
if (m)
|
|
369
|
+
try {
|
|
370
|
+
m.delete();
|
|
371
|
+
}
|
|
372
|
+
catch { /* ignore */ }
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/** 3D -> 2D horizontal cross-section slice at height z. Returns a Forma 2D prism (z = 0..1). */
|
|
376
|
+
export function manifoldSlice(polys, z) {
|
|
377
|
+
if (!wasm)
|
|
378
|
+
return null;
|
|
379
|
+
if (!polys.length)
|
|
380
|
+
return [];
|
|
381
|
+
let m = null;
|
|
382
|
+
let cs = null;
|
|
383
|
+
try {
|
|
384
|
+
m = polysToManifold(polys);
|
|
385
|
+
if (!m)
|
|
386
|
+
return null;
|
|
387
|
+
cs = m.slice(z);
|
|
388
|
+
return crossSectionToPrism(cs, uniformColor(polys));
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
finally {
|
|
394
|
+
if (cs)
|
|
395
|
+
try {
|
|
396
|
+
cs.delete();
|
|
397
|
+
}
|
|
398
|
+
catch { /* ignore */ }
|
|
399
|
+
if (m)
|
|
400
|
+
try {
|
|
401
|
+
m.delete();
|
|
402
|
+
}
|
|
403
|
+
catch { /* ignore */ }
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
/** 3D -> 2D silhouette projection onto the XY plane. Returns a Forma 2D prism (z = 0..1). */
|
|
407
|
+
export function manifoldProject(polys) {
|
|
408
|
+
if (!wasm)
|
|
409
|
+
return null;
|
|
410
|
+
if (!polys.length)
|
|
411
|
+
return [];
|
|
412
|
+
let m = null;
|
|
413
|
+
let cs = null;
|
|
414
|
+
try {
|
|
415
|
+
m = polysToManifold(polys);
|
|
416
|
+
if (!m)
|
|
417
|
+
return null;
|
|
418
|
+
cs = m.project();
|
|
419
|
+
return crossSectionToPrism(cs, uniformColor(polys));
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
finally {
|
|
425
|
+
if (cs)
|
|
426
|
+
try {
|
|
427
|
+
cs.delete();
|
|
428
|
+
}
|
|
429
|
+
catch { /* ignore */ }
|
|
430
|
+
if (m)
|
|
431
|
+
try {
|
|
432
|
+
m.delete();
|
|
433
|
+
}
|
|
434
|
+
catch { /* ignore */ }
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/** Minkowski sum of 2 or more shapes (3D or 2D thin prisms). */
|
|
438
|
+
export function manifoldMinkowski(sets, is2d) {
|
|
439
|
+
if (!wasm)
|
|
440
|
+
return null;
|
|
441
|
+
const nonEmpty = sets.filter((s) => s.length > 0);
|
|
442
|
+
if (!nonEmpty.length)
|
|
443
|
+
return [];
|
|
444
|
+
if (nonEmpty.length === 1)
|
|
445
|
+
return nonEmpty[0].slice();
|
|
446
|
+
const allocated = [];
|
|
447
|
+
let cs = null;
|
|
448
|
+
try {
|
|
449
|
+
const first = polysToManifold(nonEmpty[0]);
|
|
450
|
+
if (!first)
|
|
451
|
+
return null;
|
|
452
|
+
let acc = first;
|
|
453
|
+
allocated.push(acc);
|
|
454
|
+
for (let i = 1; i < nonEmpty.length; i++) {
|
|
455
|
+
const next = polysToManifold(nonEmpty[i]);
|
|
456
|
+
if (!next)
|
|
457
|
+
return null;
|
|
458
|
+
allocated.push(next);
|
|
459
|
+
const sum = acc.minkowskiSum(next);
|
|
460
|
+
allocated.push(sum);
|
|
461
|
+
if (sum.status() !== 'NoError')
|
|
462
|
+
return null;
|
|
463
|
+
acc = sum;
|
|
464
|
+
}
|
|
465
|
+
const color = uniformColor(nonEmpty[0]);
|
|
466
|
+
if (is2d) {
|
|
467
|
+
// N prisms of z = 0..1 summed have z = 0..N; slice at N/2 to recover exact 2D Minkowski sum.
|
|
468
|
+
cs = acc.slice(nonEmpty.length / 2);
|
|
469
|
+
return crossSectionToPrism(cs, color);
|
|
470
|
+
}
|
|
471
|
+
const out = manifoldToPolys(acc);
|
|
472
|
+
if (color !== null)
|
|
473
|
+
for (const p of out)
|
|
474
|
+
p.c = color;
|
|
475
|
+
return out;
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
finally {
|
|
481
|
+
if (cs)
|
|
482
|
+
try {
|
|
483
|
+
cs.delete();
|
|
484
|
+
}
|
|
485
|
+
catch { /* ignore */ }
|
|
486
|
+
disposeAll(allocated);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
/** Extract 2D contours (Vec2[][], outer CCW / hole CW) from a 2D prism or 3D projection. */
|
|
490
|
+
export function manifoldContours2d(polys, is2d) {
|
|
491
|
+
if (!wasm)
|
|
492
|
+
return null;
|
|
493
|
+
if (!polys.length)
|
|
494
|
+
return [];
|
|
495
|
+
let m = null;
|
|
496
|
+
let cs = null;
|
|
497
|
+
try {
|
|
498
|
+
m = polysToManifold(polys);
|
|
499
|
+
if (!m)
|
|
500
|
+
return null;
|
|
501
|
+
cs = is2d ? m.slice(0.5) : m.project();
|
|
502
|
+
if (cs.isEmpty())
|
|
503
|
+
return [];
|
|
504
|
+
return cs.toPolygons();
|
|
505
|
+
}
|
|
506
|
+
catch {
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
finally {
|
|
510
|
+
if (cs)
|
|
511
|
+
try {
|
|
512
|
+
cs.delete();
|
|
513
|
+
}
|
|
514
|
+
catch { /* ignore */ }
|
|
515
|
+
if (m)
|
|
516
|
+
try {
|
|
517
|
+
m.delete();
|
|
518
|
+
}
|
|
519
|
+
catch { /* ignore */ }
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/** 2D corner fillet on any Forma 2D shape via Manifold CrossSection inward-miter + outward-round offset. */
|
|
523
|
+
export function manifoldFillet2d(polys, r, circularSegments) {
|
|
524
|
+
if (!wasm)
|
|
525
|
+
return null;
|
|
526
|
+
if (!polys.length)
|
|
527
|
+
return [];
|
|
528
|
+
let m = null;
|
|
529
|
+
let cs = null;
|
|
530
|
+
let shrunk = null;
|
|
531
|
+
let shrunkSimp = null;
|
|
532
|
+
let grown = null;
|
|
533
|
+
let finalCs = null;
|
|
534
|
+
try {
|
|
535
|
+
m = polysToManifold(polys);
|
|
536
|
+
if (!m)
|
|
537
|
+
return null;
|
|
538
|
+
cs = m.slice(0.5);
|
|
539
|
+
if (cs.isEmpty())
|
|
540
|
+
return [];
|
|
541
|
+
shrunk = cs.offset(-r, 'Miter', 2, circularSegments);
|
|
542
|
+
if (shrunk.isEmpty())
|
|
543
|
+
return [];
|
|
544
|
+
shrunkSimp = shrunk.simplify(1e-6);
|
|
545
|
+
grown = shrunkSimp.offset(r, 'Round', 2, circularSegments);
|
|
546
|
+
finalCs = grown.simplify(1e-6);
|
|
547
|
+
return crossSectionToPrism(finalCs, uniformColor(polys));
|
|
548
|
+
}
|
|
549
|
+
catch {
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
finally {
|
|
553
|
+
if (finalCs)
|
|
554
|
+
try {
|
|
555
|
+
finalCs.delete();
|
|
556
|
+
}
|
|
557
|
+
catch { /* ignore */ }
|
|
558
|
+
if (grown)
|
|
559
|
+
try {
|
|
560
|
+
grown.delete();
|
|
561
|
+
}
|
|
562
|
+
catch { /* ignore */ }
|
|
563
|
+
if (shrunkSimp)
|
|
564
|
+
try {
|
|
565
|
+
shrunkSimp.delete();
|
|
566
|
+
}
|
|
567
|
+
catch { /* ignore */ }
|
|
568
|
+
if (shrunk)
|
|
569
|
+
try {
|
|
570
|
+
shrunk.delete();
|
|
571
|
+
}
|
|
572
|
+
catch { /* ignore */ }
|
|
573
|
+
if (cs)
|
|
574
|
+
try {
|
|
575
|
+
cs.delete();
|
|
576
|
+
}
|
|
577
|
+
catch { /* ignore */ }
|
|
578
|
+
if (m)
|
|
579
|
+
try {
|
|
580
|
+
m.delete();
|
|
581
|
+
}
|
|
582
|
+
catch { /* ignore */ }
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/** 2D corner chamfer on any Forma 2D shape: bevels sharp corners (> 25 deg turn) by `size` along each adjacent edge. */
|
|
586
|
+
export function manifoldChamfer2d(polys, size) {
|
|
587
|
+
if (!wasm)
|
|
588
|
+
return null;
|
|
589
|
+
if (!polys.length)
|
|
590
|
+
return [];
|
|
591
|
+
let m = null;
|
|
592
|
+
let cs = null;
|
|
593
|
+
let outCs = null;
|
|
594
|
+
try {
|
|
595
|
+
m = polysToManifold(polys);
|
|
596
|
+
if (!m)
|
|
597
|
+
return null;
|
|
598
|
+
cs = m.slice(0.5);
|
|
599
|
+
if (cs.isEmpty())
|
|
600
|
+
return [];
|
|
601
|
+
const rings = cs.toPolygons();
|
|
602
|
+
const newRings = [];
|
|
603
|
+
for (const ring of rings) {
|
|
604
|
+
const n = ring.length;
|
|
605
|
+
if (n < 3)
|
|
606
|
+
continue;
|
|
607
|
+
const out = [];
|
|
608
|
+
for (let i = 0; i < n; i++) {
|
|
609
|
+
const prev = ring[(i - 1 + n) % n];
|
|
610
|
+
const cur = ring[i];
|
|
611
|
+
const next = ring[(i + 1) % n];
|
|
612
|
+
const ux = prev[0] - cur[0], uy = prev[1] - cur[1];
|
|
613
|
+
const vx = next[0] - cur[0], vy = next[1] - cur[1];
|
|
614
|
+
const lu = Math.hypot(ux, uy), lv = Math.hypot(vx, vy);
|
|
615
|
+
if (lu < 1e-9 || lv < 1e-9)
|
|
616
|
+
continue;
|
|
617
|
+
const dot = (ux * vx + uy * vy) / (lu * lv);
|
|
618
|
+
if (dot > -0.906) {
|
|
619
|
+
const d = Math.min(size, lu * 0.499, lv * 0.499);
|
|
620
|
+
out.push([cur[0] + (ux / lu) * d, cur[1] + (uy / lu) * d]);
|
|
621
|
+
out.push([cur[0] + (vx / lv) * d, cur[1] + (vy / lv) * d]);
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
out.push(cur);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (out.length >= 3)
|
|
628
|
+
newRings.push(out);
|
|
629
|
+
}
|
|
630
|
+
if (!newRings.length)
|
|
631
|
+
return [];
|
|
632
|
+
outCs = wasm.CrossSection.ofPolygons(newRings, 'EvenOdd');
|
|
633
|
+
return crossSectionToPrism(outCs, uniformColor(polys));
|
|
634
|
+
}
|
|
635
|
+
catch {
|
|
636
|
+
return null;
|
|
637
|
+
}
|
|
638
|
+
finally {
|
|
639
|
+
if (outCs)
|
|
640
|
+
try {
|
|
641
|
+
outCs.delete();
|
|
642
|
+
}
|
|
643
|
+
catch { /* ignore */ }
|
|
644
|
+
if (cs)
|
|
645
|
+
try {
|
|
646
|
+
cs.delete();
|
|
647
|
+
}
|
|
648
|
+
catch { /* ignore */ }
|
|
649
|
+
if (m)
|
|
650
|
+
try {
|
|
651
|
+
m.delete();
|
|
652
|
+
}
|
|
653
|
+
catch { /* ignore */ }
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/** Convert 2D polygon contours (e.g. from TrueType glyphs) into a Forma 2D prism (z = 0..1) via Clipper2. */
|
|
657
|
+
export function manifoldContoursToPrism(contours, fillRule = 'NonZero') {
|
|
658
|
+
if (!wasm)
|
|
659
|
+
return null;
|
|
660
|
+
if (!contours.length)
|
|
661
|
+
return [];
|
|
662
|
+
let cs = null;
|
|
663
|
+
let simp = null;
|
|
664
|
+
try {
|
|
665
|
+
cs = wasm.CrossSection.ofPolygons(contours, fillRule);
|
|
666
|
+
if (cs.isEmpty())
|
|
667
|
+
return [];
|
|
668
|
+
simp = cs.simplify(1e-5);
|
|
669
|
+
return crossSectionToPrism(simp, null);
|
|
670
|
+
}
|
|
671
|
+
catch {
|
|
672
|
+
return null;
|
|
673
|
+
}
|
|
674
|
+
finally {
|
|
675
|
+
if (simp)
|
|
676
|
+
try {
|
|
677
|
+
simp.delete();
|
|
678
|
+
}
|
|
679
|
+
catch { /* ignore */ }
|
|
680
|
+
if (cs)
|
|
681
|
+
try {
|
|
682
|
+
cs.delete();
|
|
683
|
+
}
|
|
684
|
+
catch { /* ignore */ }
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
//# sourceMappingURL=manifold.js.map
|