formacad 0.0.0-stage → 0.2.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 +98 -2
- package/build/cli/src/forma.d.ts +2 -0
- package/build/cli/src/forma.js +546 -0
- package/build/cli/src/forma.js.map +1 -0
- package/build/cli/src/mcp.d.ts +1 -0
- package/build/cli/src/mcp.js +495 -0
- package/build/cli/src/mcp.js.map +1 -0
- package/build/cli/src/preview.d.ts +31 -0
- package/build/cli/src/preview.js +427 -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 +10 -0
- package/build/core/src/export2d.js +127 -0
- package/build/core/src/export2d.js.map +1 -0
- package/build/core/src/geometry.d.ts +93 -0
- package/build/core/src/geometry.js +946 -0
- package/build/core/src/geometry.js.map +1 -0
- package/build/core/src/gltf.d.ts +6 -0
- package/build/core/src/gltf.js +154 -0
- package/build/core/src/gltf.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 +12 -0
- package/build/core/src/importers.js +1025 -0
- package/build/core/src/importers.js.map +1 -0
- package/build/core/src/index.d.ts +92 -0
- package/build/core/src/index.js +227 -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 +178 -0
- package/build/core/src/interp.js +3012 -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/modeling.d.ts +18 -0
- package/build/core/src/modeling.js +483 -0
- package/build/core/src/modeling.js.map +1 -0
- package/build/core/src/parser.d.ts +46 -0
- package/build/core/src/parser.js +478 -0
- package/build/core/src/parser.js.map +1 -0
- package/build/core/src/scad/colors.d.ts +1 -0
- package/build/core/src/scad/colors.js +37 -0
- package/build/core/src/scad/colors.js.map +1 -0
- package/build/core/src/scad/convert.d.ts +33 -0
- package/build/core/src/scad/convert.js +2842 -0
- package/build/core/src/scad/convert.js.map +1 -0
- package/build/core/src/scad/parser.d.ts +239 -0
- package/build/core/src/scad/parser.js +578 -0
- package/build/core/src/scad/parser.js.map +1 -0
- package/build/core/src/scad/sources.d.ts +39 -0
- package/build/core/src/scad/sources.js +171 -0
- package/build/core/src/scad/sources.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/surface.d.ts +21 -0
- package/build/core/src/surface.js +253 -0
- package/build/core/src/surface.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 +146 -0
- package/build/core/src/values.js +365 -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 +59 -4
|
@@ -0,0 +1,946 @@
|
|
|
1
|
+
// Geometry kernel: polygons, BSP-tree booleans (after csg.js), primitives, 2D outlines,
|
|
2
|
+
// convex hulls and mesh utilities. A faithful port of the Python reference engine.
|
|
3
|
+
import { FormaError } from './errors.js';
|
|
4
|
+
import { manifoldDifference, manifoldIntersection, manifoldUnion } from './manifold.js';
|
|
5
|
+
export const EPS = 1e-5;
|
|
6
|
+
const COPLANAR = 0, FRONT = 1, BACK = 2, SPANNING = 3;
|
|
7
|
+
/** Python's round(x, nd) for keys: correctly rounded decimal, as a string (−0 folded into 0). */
|
|
8
|
+
export function roundKey(x, nd) {
|
|
9
|
+
const s = x.toFixed(nd);
|
|
10
|
+
return /^-0\.?0*$/.test(s) ? s.slice(1) : s;
|
|
11
|
+
}
|
|
12
|
+
/** Python's round(x, nd) as a number. */
|
|
13
|
+
export function pyRoundTo(x, nd) {
|
|
14
|
+
return Number(x.toFixed(nd));
|
|
15
|
+
}
|
|
16
|
+
/** Python's round(x) for integers: halves go to the even neighbour. */
|
|
17
|
+
export function pyRound(x) {
|
|
18
|
+
const f = Math.floor(x);
|
|
19
|
+
const diff = x - f;
|
|
20
|
+
if (diff > 0.5)
|
|
21
|
+
return f + 1;
|
|
22
|
+
if (diff < 0.5)
|
|
23
|
+
return f;
|
|
24
|
+
return f % 2 === 0 ? f : f + 1;
|
|
25
|
+
}
|
|
26
|
+
// ---- polygons ----------------------------------------------------------------------
|
|
27
|
+
export function newell(v) {
|
|
28
|
+
let nx = 0, ny = 0, nz = 0;
|
|
29
|
+
const L = v.length;
|
|
30
|
+
for (let i = 0; i < L; i++) {
|
|
31
|
+
const [x1, y1, z1] = v[i];
|
|
32
|
+
const [x2, y2, z2] = v[(i + 1) % L];
|
|
33
|
+
nx += (y1 - y2) * (z1 + z2);
|
|
34
|
+
ny += (z1 - z2) * (x1 + x2);
|
|
35
|
+
nz += (x1 - x2) * (y1 + y2);
|
|
36
|
+
}
|
|
37
|
+
const ln = Math.sqrt(nx * nx + ny * ny + nz * nz);
|
|
38
|
+
if (ln < 1e-12)
|
|
39
|
+
return [null, 0];
|
|
40
|
+
const n = [nx / ln, ny / ln, nz / ln];
|
|
41
|
+
let cx = 0, cy = 0, cz = 0;
|
|
42
|
+
for (const p of v) {
|
|
43
|
+
cx += p[0];
|
|
44
|
+
cy += p[1];
|
|
45
|
+
cz += p[2];
|
|
46
|
+
}
|
|
47
|
+
cx /= L;
|
|
48
|
+
cy /= L;
|
|
49
|
+
cz /= L;
|
|
50
|
+
return [n, n[0] * cx + n[1] * cy + n[2] * cz];
|
|
51
|
+
}
|
|
52
|
+
export class Poly {
|
|
53
|
+
v;
|
|
54
|
+
n;
|
|
55
|
+
w;
|
|
56
|
+
c;
|
|
57
|
+
constructor(v, c = null, n = null, w = 0) {
|
|
58
|
+
this.v = v;
|
|
59
|
+
this.c = c;
|
|
60
|
+
if (n === null)
|
|
61
|
+
[n, w] = newell(v);
|
|
62
|
+
this.n = n;
|
|
63
|
+
this.w = w;
|
|
64
|
+
}
|
|
65
|
+
flipped() {
|
|
66
|
+
const n = this.n;
|
|
67
|
+
return new Poly(this.v.slice().reverse(), this.c, [-n[0], -n[1], -n[2]], -this.w);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function makePoly(v, c = null) {
|
|
71
|
+
const clean = [];
|
|
72
|
+
for (const p of v) {
|
|
73
|
+
const q = clean[clean.length - 1];
|
|
74
|
+
if (!q || Math.max(Math.abs(p[0] - q[0]), Math.abs(p[1] - q[1]), Math.abs(p[2] - q[2])) > 1e-9)
|
|
75
|
+
clean.push(p);
|
|
76
|
+
}
|
|
77
|
+
while (clean.length > 1) {
|
|
78
|
+
const a = clean[0], b = clean[clean.length - 1];
|
|
79
|
+
if (Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1]), Math.abs(a[2] - b[2])) <= 1e-9)
|
|
80
|
+
clean.pop();
|
|
81
|
+
else
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
if (clean.length < 3)
|
|
85
|
+
return null;
|
|
86
|
+
const p = new Poly(clean, c);
|
|
87
|
+
return p.n !== null ? p : null;
|
|
88
|
+
}
|
|
89
|
+
function keep(list) {
|
|
90
|
+
return list.filter((p) => p !== null);
|
|
91
|
+
}
|
|
92
|
+
export function split(n, w, p, cf, cb, f, b) {
|
|
93
|
+
const [nx, ny, nz] = n;
|
|
94
|
+
const vs = p.v;
|
|
95
|
+
const L = vs.length;
|
|
96
|
+
const types = new Array(L);
|
|
97
|
+
let ptype = 0;
|
|
98
|
+
for (let i = 0; i < L; i++) {
|
|
99
|
+
const v = vs[i];
|
|
100
|
+
const t = nx * v[0] + ny * v[1] + nz * v[2] - w;
|
|
101
|
+
const ty = t < -EPS ? BACK : t > EPS ? FRONT : COPLANAR;
|
|
102
|
+
ptype |= ty;
|
|
103
|
+
types[i] = ty;
|
|
104
|
+
}
|
|
105
|
+
if (ptype === COPLANAR) {
|
|
106
|
+
(nx * p.n[0] + ny * p.n[1] + nz * p.n[2] > 0 ? cf : cb).push(p);
|
|
107
|
+
}
|
|
108
|
+
else if (ptype === FRONT) {
|
|
109
|
+
f.push(p);
|
|
110
|
+
}
|
|
111
|
+
else if (ptype === BACK) {
|
|
112
|
+
b.push(p);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const fv = [], bv = [];
|
|
116
|
+
for (let i = 0; i < L; i++) {
|
|
117
|
+
const j = (i + 1) % L;
|
|
118
|
+
const ti = types[i], tj = types[j];
|
|
119
|
+
const vi = vs[i], vj = vs[j];
|
|
120
|
+
if (ti !== BACK)
|
|
121
|
+
fv.push(vi);
|
|
122
|
+
if (ti !== FRONT)
|
|
123
|
+
bv.push(vi);
|
|
124
|
+
if ((ti | tj) === SPANNING) {
|
|
125
|
+
const di = nx * vi[0] + ny * vi[1] + nz * vi[2];
|
|
126
|
+
const dj = nx * vj[0] + ny * vj[1] + nz * vj[2];
|
|
127
|
+
const t = (w - di) / (dj - di);
|
|
128
|
+
const m = [vi[0] + (vj[0] - vi[0]) * t, vi[1] + (vj[1] - vi[1]) * t, vi[2] + (vj[2] - vi[2]) * t];
|
|
129
|
+
fv.push(m);
|
|
130
|
+
bv.push(m);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (fv.length >= 3)
|
|
134
|
+
f.push(new Poly(fv, p.c, p.n, p.w));
|
|
135
|
+
if (bv.length >= 3)
|
|
136
|
+
b.push(new Poly(bv, p.c, p.n, p.w));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// ---- BSP tree ------------------------------------------------------------------------
|
|
140
|
+
export class BspNode {
|
|
141
|
+
n = null;
|
|
142
|
+
w = 0;
|
|
143
|
+
front = null;
|
|
144
|
+
back = null;
|
|
145
|
+
polys = [];
|
|
146
|
+
constructor(polys) {
|
|
147
|
+
if (polys && polys.length)
|
|
148
|
+
this.build(polys);
|
|
149
|
+
}
|
|
150
|
+
nodes() {
|
|
151
|
+
const out = [];
|
|
152
|
+
const stack = [this];
|
|
153
|
+
while (stack.length) {
|
|
154
|
+
const nd = stack.pop();
|
|
155
|
+
out.push(nd);
|
|
156
|
+
if (nd.front)
|
|
157
|
+
stack.push(nd.front);
|
|
158
|
+
if (nd.back)
|
|
159
|
+
stack.push(nd.back);
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
build(polys) {
|
|
164
|
+
const stack = [[this, polys]];
|
|
165
|
+
while (stack.length) {
|
|
166
|
+
const [nd, ps] = stack.pop();
|
|
167
|
+
if (!ps.length)
|
|
168
|
+
continue;
|
|
169
|
+
if (nd.n === null) {
|
|
170
|
+
nd.n = ps[0].n;
|
|
171
|
+
nd.w = ps[0].w;
|
|
172
|
+
}
|
|
173
|
+
const f = [], b = [];
|
|
174
|
+
for (const p of ps)
|
|
175
|
+
split(nd.n, nd.w, p, nd.polys, nd.polys, f, b);
|
|
176
|
+
if (f.length) {
|
|
177
|
+
if (nd.front === null)
|
|
178
|
+
nd.front = new BspNode();
|
|
179
|
+
stack.push([nd.front, f]);
|
|
180
|
+
}
|
|
181
|
+
if (b.length) {
|
|
182
|
+
if (nd.back === null)
|
|
183
|
+
nd.back = new BspNode();
|
|
184
|
+
stack.push([nd.back, b]);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
clipPolygons(polys) {
|
|
189
|
+
if (this.n === null)
|
|
190
|
+
return polys.slice();
|
|
191
|
+
const out = [];
|
|
192
|
+
const stack = [[this, polys]];
|
|
193
|
+
while (stack.length) {
|
|
194
|
+
const [nd, ps] = stack.pop();
|
|
195
|
+
if (nd.n === null) {
|
|
196
|
+
for (const p of ps)
|
|
197
|
+
out.push(p);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const f = [], b = [];
|
|
201
|
+
for (const p of ps)
|
|
202
|
+
split(nd.n, nd.w, p, f, b, f, b);
|
|
203
|
+
if (f.length) {
|
|
204
|
+
if (nd.front)
|
|
205
|
+
stack.push([nd.front, f]);
|
|
206
|
+
else
|
|
207
|
+
for (const p of f)
|
|
208
|
+
out.push(p);
|
|
209
|
+
}
|
|
210
|
+
if (b.length && nd.back)
|
|
211
|
+
stack.push([nd.back, b]);
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
clipTo(bsp) {
|
|
216
|
+
for (const nd of this.nodes())
|
|
217
|
+
nd.polys = bsp.clipPolygons(nd.polys);
|
|
218
|
+
}
|
|
219
|
+
invert() {
|
|
220
|
+
for (const nd of this.nodes()) {
|
|
221
|
+
nd.polys = nd.polys.map((p) => p.flipped());
|
|
222
|
+
if (nd.n !== null) {
|
|
223
|
+
nd.n = [-nd.n[0], -nd.n[1], -nd.n[2]];
|
|
224
|
+
nd.w = -nd.w;
|
|
225
|
+
}
|
|
226
|
+
const t = nd.front;
|
|
227
|
+
nd.front = nd.back;
|
|
228
|
+
nd.back = t;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
allPolys() {
|
|
232
|
+
const out = [];
|
|
233
|
+
for (const nd of this.nodes())
|
|
234
|
+
for (const p of nd.polys)
|
|
235
|
+
out.push(p);
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
export function bboxOf(polys) {
|
|
240
|
+
let lx = Infinity, ly = Infinity, lz = Infinity, hx = -Infinity, hy = -Infinity, hz = -Infinity;
|
|
241
|
+
let any = false;
|
|
242
|
+
for (const q of polys) {
|
|
243
|
+
for (const p of q.v) {
|
|
244
|
+
any = true;
|
|
245
|
+
if (p[0] < lx)
|
|
246
|
+
lx = p[0];
|
|
247
|
+
if (p[1] < ly)
|
|
248
|
+
ly = p[1];
|
|
249
|
+
if (p[2] < lz)
|
|
250
|
+
lz = p[2];
|
|
251
|
+
if (p[0] > hx)
|
|
252
|
+
hx = p[0];
|
|
253
|
+
if (p[1] > hy)
|
|
254
|
+
hy = p[1];
|
|
255
|
+
if (p[2] > hz)
|
|
256
|
+
hz = p[2];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return any ? [[lx, ly, lz], [hx, hy, hz]] : null;
|
|
260
|
+
}
|
|
261
|
+
export function boxesOverlap(a, b) {
|
|
262
|
+
if (a === null || b === null)
|
|
263
|
+
return false;
|
|
264
|
+
for (let k = 0; k < 3; k++) {
|
|
265
|
+
if (!(a[0][k] <= b[1][k] + 1e-6 && b[0][k] <= a[1][k] + 1e-6))
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
/** n-way union: uses Manifold WASM when possible, falling back to BSP for non-manifold inputs. */
|
|
271
|
+
export function csgUnion(input) {
|
|
272
|
+
const sets = input.filter((s) => s.length);
|
|
273
|
+
if (!sets.length)
|
|
274
|
+
return [];
|
|
275
|
+
if (sets.length === 1)
|
|
276
|
+
return sets[0].slice();
|
|
277
|
+
const m = manifoldUnion(sets);
|
|
278
|
+
if (m !== null)
|
|
279
|
+
return m;
|
|
280
|
+
const boxes = sets.map(bboxOf);
|
|
281
|
+
const bsps = sets.map(() => null);
|
|
282
|
+
const out = [];
|
|
283
|
+
sets.forEach((s, k) => {
|
|
284
|
+
let ps = s;
|
|
285
|
+
for (let i = 0; i < sets.length; i++) {
|
|
286
|
+
if (i === k || !boxesOverlap(boxes[i], boxes[k]))
|
|
287
|
+
continue;
|
|
288
|
+
if (bsps[i] === null)
|
|
289
|
+
bsps[i] = new BspNode(sets[i].slice());
|
|
290
|
+
const bsp = bsps[i];
|
|
291
|
+
ps = bsp.clipPolygons(ps);
|
|
292
|
+
if (i < k) { // drop faces coplanar with an earlier piece (avoids duplicates)
|
|
293
|
+
ps = bsp.clipPolygons(ps.map((p) => p.flipped())).map((p) => p.flipped());
|
|
294
|
+
}
|
|
295
|
+
if (!ps.length)
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
for (const p of ps)
|
|
299
|
+
out.push(p);
|
|
300
|
+
});
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
export function csgSubtract(A, B) {
|
|
304
|
+
if (!A.length || !B.length || !boxesOverlap(bboxOf(A), bboxOf(B)))
|
|
305
|
+
return A.slice();
|
|
306
|
+
const m = manifoldDifference(A, [B]);
|
|
307
|
+
if (m !== null)
|
|
308
|
+
return m;
|
|
309
|
+
const a = new BspNode(A.slice()), b = new BspNode(B.slice());
|
|
310
|
+
a.invert();
|
|
311
|
+
a.clipTo(b);
|
|
312
|
+
b.clipTo(a);
|
|
313
|
+
b.invert();
|
|
314
|
+
b.clipTo(a);
|
|
315
|
+
b.invert();
|
|
316
|
+
a.build(b.allPolys());
|
|
317
|
+
a.invert();
|
|
318
|
+
return a.allPolys();
|
|
319
|
+
}
|
|
320
|
+
/** Subtract multiple cutters from A in a single Manifold pass (skipping non-overlapping cutters). */
|
|
321
|
+
export function csgSubtractMany(A, cutters) {
|
|
322
|
+
if (!A.length)
|
|
323
|
+
return [];
|
|
324
|
+
const boxA = bboxOf(A);
|
|
325
|
+
const active = cutters.filter((c) => c.length && boxesOverlap(boxA, bboxOf(c)));
|
|
326
|
+
if (!active.length)
|
|
327
|
+
return A.slice();
|
|
328
|
+
const m = manifoldDifference(A, active);
|
|
329
|
+
if (m !== null)
|
|
330
|
+
return m;
|
|
331
|
+
const combined = active.length === 1 ? active[0] : csgUnion(active);
|
|
332
|
+
return csgSubtract(A, combined);
|
|
333
|
+
}
|
|
334
|
+
export function csgIntersect(A, B) {
|
|
335
|
+
if (!A.length || !B.length || !boxesOverlap(bboxOf(A), bboxOf(B)))
|
|
336
|
+
return [];
|
|
337
|
+
const m = manifoldIntersection([A, B]);
|
|
338
|
+
if (m !== null)
|
|
339
|
+
return m;
|
|
340
|
+
const a = new BspNode(A.slice()), b = new BspNode(B.slice());
|
|
341
|
+
a.invert();
|
|
342
|
+
b.clipTo(a);
|
|
343
|
+
b.invert();
|
|
344
|
+
a.clipTo(b);
|
|
345
|
+
b.clipTo(a);
|
|
346
|
+
a.build(b.allPolys());
|
|
347
|
+
a.invert();
|
|
348
|
+
return a.allPolys();
|
|
349
|
+
}
|
|
350
|
+
/** N-way intersection in a single Manifold pass. */
|
|
351
|
+
export function csgIntersectMany(sets) {
|
|
352
|
+
if (!sets.length)
|
|
353
|
+
return [];
|
|
354
|
+
if (sets.length === 1)
|
|
355
|
+
return sets[0].slice();
|
|
356
|
+
let accBox = bboxOf(sets[0]);
|
|
357
|
+
if (!accBox)
|
|
358
|
+
return [];
|
|
359
|
+
for (let i = 1; i < sets.length; i++) {
|
|
360
|
+
const b = bboxOf(sets[i]);
|
|
361
|
+
if (!boxesOverlap(accBox, b))
|
|
362
|
+
return [];
|
|
363
|
+
accBox = [
|
|
364
|
+
[Math.max(accBox[0][0], b[0][0]), Math.max(accBox[0][1], b[0][1]), Math.max(accBox[0][2], b[0][2])],
|
|
365
|
+
[Math.min(accBox[1][0], b[1][0]), Math.min(accBox[1][1], b[1][1]), Math.min(accBox[1][2], b[1][2])],
|
|
366
|
+
];
|
|
367
|
+
}
|
|
368
|
+
const m = manifoldIntersection(sets);
|
|
369
|
+
if (m !== null)
|
|
370
|
+
return m;
|
|
371
|
+
let acc = sets[0];
|
|
372
|
+
for (let i = 1; i < sets.length; i++) {
|
|
373
|
+
acc = csgIntersect(acc, sets[i]);
|
|
374
|
+
if (!acc.length)
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
return acc;
|
|
378
|
+
}
|
|
379
|
+
export const QUALITY = {
|
|
380
|
+
draft: [3.0, 8, 24], normal: [1.5, 12, 40], fine: [0.6, 16, 96],
|
|
381
|
+
};
|
|
382
|
+
export const quality = { name: 'normal', fs: 1.5, lo: 12, hi: 40 };
|
|
383
|
+
export function setQuality(name) {
|
|
384
|
+
const [fs, lo, hi] = QUALITY[name];
|
|
385
|
+
Object.assign(quality, { name, fs, lo, hi });
|
|
386
|
+
}
|
|
387
|
+
export function segmentsFor(r, override = null) {
|
|
388
|
+
if (override !== null)
|
|
389
|
+
return Math.max(3, Math.trunc(override));
|
|
390
|
+
let s = Math.ceil((2 * Math.PI * r) / quality.fs);
|
|
391
|
+
s = Math.max(quality.lo, Math.min(quality.hi, s));
|
|
392
|
+
return s + (s % 2);
|
|
393
|
+
}
|
|
394
|
+
export function primSphere(r, seg) {
|
|
395
|
+
const rings = Math.max(Math.floor(seg / 2), 2);
|
|
396
|
+
const pt = (i, j) => {
|
|
397
|
+
const phi = (Math.PI * i) / rings;
|
|
398
|
+
const th = (2 * Math.PI * (j % seg)) / seg;
|
|
399
|
+
return [r * Math.sin(phi) * Math.cos(th), r * Math.sin(phi) * Math.sin(th), r * Math.cos(phi)];
|
|
400
|
+
};
|
|
401
|
+
const out = [];
|
|
402
|
+
for (let i = 0; i < rings; i++) {
|
|
403
|
+
for (let j = 0; j < seg; j++) {
|
|
404
|
+
const p = makePoly([pt(i, j), pt(i + 1, j), pt(i + 1, j + 1), pt(i, j + 1)]);
|
|
405
|
+
if (p)
|
|
406
|
+
out.push(p);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return out;
|
|
410
|
+
}
|
|
411
|
+
export function primCone(r1, r2, h, seg, z0 = 0) {
|
|
412
|
+
const ring = (r, z) => {
|
|
413
|
+
const out = [];
|
|
414
|
+
for (let j = 0; j < seg; j++) {
|
|
415
|
+
out.push([r * Math.cos((2 * Math.PI * j) / seg), r * Math.sin((2 * Math.PI * j) / seg), z]);
|
|
416
|
+
}
|
|
417
|
+
return out;
|
|
418
|
+
};
|
|
419
|
+
const bot = ring(r1, z0), top = ring(r2, z0 + h);
|
|
420
|
+
const out = [];
|
|
421
|
+
if (r1 > EPS)
|
|
422
|
+
out.push(makePoly(bot.slice().reverse()));
|
|
423
|
+
if (r2 > EPS)
|
|
424
|
+
out.push(makePoly(top));
|
|
425
|
+
for (let j = 0; j < seg; j++) {
|
|
426
|
+
const k = (j + 1) % seg;
|
|
427
|
+
out.push(makePoly([bot[j], bot[k], top[k], top[j]]));
|
|
428
|
+
}
|
|
429
|
+
return keep(out);
|
|
430
|
+
}
|
|
431
|
+
export function primBox(sx, sy, sz, z0) {
|
|
432
|
+
const cx = 0, cy = 0, cz = z0 + sz / 2;
|
|
433
|
+
const faces = [[0, 4, 6, 2], [1, 3, 7, 5], [0, 1, 5, 4], [2, 6, 7, 3], [0, 2, 3, 1], [4, 5, 7, 6]];
|
|
434
|
+
const corner = (i) => [
|
|
435
|
+
cx + (sx / 2) * (i & 1 ? 1 : -1), cy + (sy / 2) * (i & 2 ? 1 : -1), cz + (sz / 2) * (i & 4 ? 1 : -1),
|
|
436
|
+
];
|
|
437
|
+
return keep(faces.map((f) => makePoly(f.map(corner))));
|
|
438
|
+
}
|
|
439
|
+
export function primTorus(R, t, seg, tseg) {
|
|
440
|
+
const pt = (i, j) => {
|
|
441
|
+
const th = (2 * Math.PI * (i % seg)) / seg;
|
|
442
|
+
const ph = (2 * Math.PI * (j % tseg)) / tseg;
|
|
443
|
+
const rr = R + t * Math.cos(ph);
|
|
444
|
+
return [rr * Math.cos(th), rr * Math.sin(th), t * Math.sin(ph)];
|
|
445
|
+
};
|
|
446
|
+
const out = [];
|
|
447
|
+
for (let i = 0; i < seg; i++) {
|
|
448
|
+
for (let j = 0; j < tseg; j++) {
|
|
449
|
+
const p = makePoly([pt(i, j), pt(i + 1, j), pt(i + 1, j + 1), pt(i, j + 1)]);
|
|
450
|
+
if (p)
|
|
451
|
+
out.push(p);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
// ---- matrices --------------------------------------------------------------------------
|
|
457
|
+
export const I3 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
|
|
458
|
+
export function matMul(a, b) {
|
|
459
|
+
const r = (i, j) => a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
|
|
460
|
+
return [[r(0, 0), r(0, 1), r(0, 2)], [r(1, 0), r(1, 1), r(1, 2)], [r(2, 0), r(2, 1), r(2, 2)]];
|
|
461
|
+
}
|
|
462
|
+
export function matDet(m) {
|
|
463
|
+
return m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
|
|
464
|
+
- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
|
|
465
|
+
+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
|
|
466
|
+
}
|
|
467
|
+
export function rotX(a) {
|
|
468
|
+
const c = Math.cos(a), s = Math.sin(a);
|
|
469
|
+
return [[1, 0, 0], [0, c, -s], [0, s, c]];
|
|
470
|
+
}
|
|
471
|
+
export function rotY(a) {
|
|
472
|
+
const c = Math.cos(a), s = Math.sin(a);
|
|
473
|
+
return [[c, 0, s], [0, 1, 0], [-s, 0, c]];
|
|
474
|
+
}
|
|
475
|
+
export function rotZ(a) {
|
|
476
|
+
const c = Math.cos(a), s = Math.sin(a);
|
|
477
|
+
return [[c, -s, 0], [s, c, 0], [0, 0, 1]];
|
|
478
|
+
}
|
|
479
|
+
export function rotAxis(axis, ang) {
|
|
480
|
+
let [x, y, z] = axis;
|
|
481
|
+
const ln = Math.sqrt(x * x + y * y + z * z);
|
|
482
|
+
x /= ln;
|
|
483
|
+
y /= ln;
|
|
484
|
+
z /= ln;
|
|
485
|
+
const c = Math.cos(ang), s = Math.sin(ang), C = 1 - c;
|
|
486
|
+
return [
|
|
487
|
+
[c + x * x * C, x * y * C - z * s, x * z * C + y * s],
|
|
488
|
+
[y * x * C + z * s, c + y * y * C, y * z * C - x * s],
|
|
489
|
+
[z * x * C - y * s, z * y * C + x * s, c + z * z * C],
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
// ---- 2D outlines ---------------------------------------------------------------------------
|
|
493
|
+
/** 2D shapes are thin prisms (z = 0..1); cutters are stretched to z = -1..2 so caps never coincide. */
|
|
494
|
+
export function stretchZ(polys) {
|
|
495
|
+
return keep(polys.map((p) => makePoly(p.v.map(([x, y, z]) => [x, y, 3 * z - 1]), p.c)));
|
|
496
|
+
}
|
|
497
|
+
export function cross2(o, a, b) {
|
|
498
|
+
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
499
|
+
}
|
|
500
|
+
const at = (arr, i) => arr[i < 0 ? arr.length + i : i];
|
|
501
|
+
export function cleanOutline(pts) {
|
|
502
|
+
const out = [];
|
|
503
|
+
for (const p of pts) {
|
|
504
|
+
const q = out[out.length - 1];
|
|
505
|
+
if (!q || Math.abs(p[0] - q[0]) > 1e-9 || Math.abs(p[1] - q[1]) > 1e-9)
|
|
506
|
+
out.push(p);
|
|
507
|
+
}
|
|
508
|
+
if (out.length > 1 && Math.abs(out[0][0] - out[out.length - 1][0]) <= 1e-9
|
|
509
|
+
&& Math.abs(out[0][1] - out[out.length - 1][1]) <= 1e-9)
|
|
510
|
+
out.pop();
|
|
511
|
+
let changed = true;
|
|
512
|
+
while (changed && out.length >= 3) {
|
|
513
|
+
changed = false;
|
|
514
|
+
for (let i = 0; i < out.length; i++) {
|
|
515
|
+
if (Math.abs(cross2(at(out, i - 1), out[i], out[(i + 1) % out.length])) < 1e-12) {
|
|
516
|
+
out.splice(i, 1);
|
|
517
|
+
changed = true;
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
function pointInTri(p, a, b, c) {
|
|
525
|
+
return cross2(a, b, p) >= -1e-12 && cross2(b, c, p) >= -1e-12 && cross2(c, a, p) >= -1e-12;
|
|
526
|
+
}
|
|
527
|
+
export function earClip(pts) {
|
|
528
|
+
const idx = pts.map((_, i) => i);
|
|
529
|
+
const tris = [];
|
|
530
|
+
while (idx.length > 3) {
|
|
531
|
+
let found = false;
|
|
532
|
+
for (let k = 0; k < idx.length; k++) {
|
|
533
|
+
const ia = at(idx, k - 1), ib = idx[k], ic = idx[(k + 1) % idx.length];
|
|
534
|
+
const a = pts[ia], b = pts[ib], c = pts[ic];
|
|
535
|
+
if (cross2(a, b, c) <= 1e-12)
|
|
536
|
+
continue;
|
|
537
|
+
if (idx.some((j) => j !== ia && j !== ib && j !== ic && pointInTri(pts[j], a, b, c)))
|
|
538
|
+
continue;
|
|
539
|
+
tris.push([a, b, c]);
|
|
540
|
+
idx.splice(k, 1);
|
|
541
|
+
found = true;
|
|
542
|
+
break;
|
|
543
|
+
}
|
|
544
|
+
if (!found)
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
tris.push(idx.map((i) => pts[i]));
|
|
548
|
+
return tris;
|
|
549
|
+
}
|
|
550
|
+
export function signedArea(o) {
|
|
551
|
+
let s = 0;
|
|
552
|
+
for (let i = 0; i < o.length; i++)
|
|
553
|
+
s += at(o, i - 1)[0] * o[i][1] - o[i][0] * at(o, i - 1)[1];
|
|
554
|
+
return s / 2;
|
|
555
|
+
}
|
|
556
|
+
export function prism2d(input, what, pos) {
|
|
557
|
+
const pts = cleanOutline(input.map(([x, y]) => [x, y]));
|
|
558
|
+
if (pts.length < 3)
|
|
559
|
+
throw new FormaError(`${what} needs at least 3 points that are not on one line`, pos);
|
|
560
|
+
const area = signedArea(pts);
|
|
561
|
+
if (Math.abs(area) < 1e-9) {
|
|
562
|
+
throw new FormaError(`${what} has no area — are all points on one line, or does the outline cross itself?`, pos);
|
|
563
|
+
}
|
|
564
|
+
if (area < 0)
|
|
565
|
+
pts.reverse();
|
|
566
|
+
const n = pts.length;
|
|
567
|
+
let convex = true;
|
|
568
|
+
for (let i = 0; i < n; i++)
|
|
569
|
+
if (cross2(at(pts, i - 1), pts[i], pts[(i + 1) % n]) < -1e-12)
|
|
570
|
+
convex = false;
|
|
571
|
+
const tris = convex ? [pts] : earClip(pts);
|
|
572
|
+
if (tris === null)
|
|
573
|
+
throw new FormaError(`${what}: the outline crosses itself — check the order of the points`, pos);
|
|
574
|
+
const out = [];
|
|
575
|
+
for (const t of tris) {
|
|
576
|
+
out.push(makePoly(t.map(([x, y]) => [x, y, 1])));
|
|
577
|
+
out.push(makePoly(t.slice().reverse().map(([x, y]) => [x, y, 0])));
|
|
578
|
+
}
|
|
579
|
+
for (let i = 0; i < n; i++) {
|
|
580
|
+
const [x1, y1] = pts[i], [x2, y2] = pts[(i + 1) % n];
|
|
581
|
+
out.push(makePoly([[x1, y1, 0], [x2, y2, 0], [x2, y2, 1], [x1, y1, 1]]));
|
|
582
|
+
}
|
|
583
|
+
return keep(out);
|
|
584
|
+
}
|
|
585
|
+
export function sliceZ(polys, n) {
|
|
586
|
+
const done = [];
|
|
587
|
+
let cur = polys;
|
|
588
|
+
for (let k = 1; k < n; k++) {
|
|
589
|
+
const front = [];
|
|
590
|
+
for (const p of cur)
|
|
591
|
+
split([0, 0, 1], k / n, p, done, done, front, done);
|
|
592
|
+
cur = front;
|
|
593
|
+
}
|
|
594
|
+
for (const p of cur)
|
|
595
|
+
done.push(p);
|
|
596
|
+
return done;
|
|
597
|
+
}
|
|
598
|
+
export function fan(v) {
|
|
599
|
+
const out = [];
|
|
600
|
+
for (let i = 1; i < v.length - 1; i++)
|
|
601
|
+
out.push([v[0], v[i], v[i + 1]]);
|
|
602
|
+
return out;
|
|
603
|
+
}
|
|
604
|
+
export function pointInOutline(p, poly) {
|
|
605
|
+
const [x, y] = p;
|
|
606
|
+
let inside = false;
|
|
607
|
+
for (let i = 0; i < poly.length; i++) {
|
|
608
|
+
const [x1, y1] = at(poly, i - 1), [x2, y2] = poly[i];
|
|
609
|
+
if ((y1 > y) !== (y2 > y) && x < ((x2 - x1) * (y - y1)) / (y2 - y1) + x1)
|
|
610
|
+
inside = !inside;
|
|
611
|
+
}
|
|
612
|
+
return inside;
|
|
613
|
+
}
|
|
614
|
+
/** Quickhull. Returns outward triangles, or null if the points are flat. */
|
|
615
|
+
export function convexHull3d(points) {
|
|
616
|
+
const seen = new Map();
|
|
617
|
+
for (const [x, y, z] of points) {
|
|
618
|
+
const p = [pyRoundTo(x, 9), pyRoundTo(y, 9), pyRoundTo(z, 9)];
|
|
619
|
+
seen.set(p.join(','), p);
|
|
620
|
+
}
|
|
621
|
+
const pts = [...seen.values()];
|
|
622
|
+
if (pts.length < 4)
|
|
623
|
+
return null;
|
|
624
|
+
let size = 0;
|
|
625
|
+
for (let k = 0; k < 3; k++) {
|
|
626
|
+
let lo = Infinity, hi = -Infinity;
|
|
627
|
+
for (const p of pts) {
|
|
628
|
+
lo = Math.min(lo, p[k]);
|
|
629
|
+
hi = Math.max(hi, p[k]);
|
|
630
|
+
}
|
|
631
|
+
size = Math.max(size, hi - lo);
|
|
632
|
+
}
|
|
633
|
+
const eps = Math.max(size, 1e-6) * 1e-9;
|
|
634
|
+
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
635
|
+
const crs = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
636
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
637
|
+
const argmax = (xs, f) => {
|
|
638
|
+
let best = xs[0], bv = f(xs[0]);
|
|
639
|
+
for (const x of xs) {
|
|
640
|
+
const v = f(x);
|
|
641
|
+
if (v > bv) {
|
|
642
|
+
best = x;
|
|
643
|
+
bv = v;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return best;
|
|
647
|
+
};
|
|
648
|
+
const ext = [];
|
|
649
|
+
for (let k = 0; k < 3; k++) {
|
|
650
|
+
ext.push(argmax(pts, (p) => -p[k]));
|
|
651
|
+
ext.push(argmax(pts, (p) => p[k]));
|
|
652
|
+
}
|
|
653
|
+
const pairs = [];
|
|
654
|
+
for (const x of ext)
|
|
655
|
+
for (const y of ext)
|
|
656
|
+
pairs.push([x, y]);
|
|
657
|
+
const [a, b] = argmax(pairs, ([x, y]) => dot(sub(x, y), sub(x, y)));
|
|
658
|
+
const ab = sub(b, a);
|
|
659
|
+
const c = argmax(pts, (p) => { const q = crs(ab, sub(p, a)); return dot(q, q); });
|
|
660
|
+
const nrm = crs(ab, sub(c, a));
|
|
661
|
+
const ln = Math.sqrt(dot(nrm, nrm));
|
|
662
|
+
if (ln < eps * Math.max(size, 1e-6))
|
|
663
|
+
return null;
|
|
664
|
+
const d = argmax(pts, (p) => Math.abs(dot(nrm, sub(p, a))));
|
|
665
|
+
if (Math.abs(dot(nrm, sub(d, a))) / ln < eps * 10)
|
|
666
|
+
return null;
|
|
667
|
+
const edges = new Map();
|
|
668
|
+
const faces = [];
|
|
669
|
+
const ek = (i, j) => i + ',' + j;
|
|
670
|
+
const make = (i, j, k) => {
|
|
671
|
+
const n = crs(sub(pts[j], pts[i]), sub(pts[k], pts[i]));
|
|
672
|
+
const l = Math.sqrt(dot(n, n)) || 1;
|
|
673
|
+
const nn = [n[0] / l, n[1] / l, n[2] / l];
|
|
674
|
+
const f = { v: [i, j, k], n: nn, w: dot(nn, pts[i]), out: [], alive: true };
|
|
675
|
+
edges.set(ek(i, j), f);
|
|
676
|
+
edges.set(ek(j, k), f);
|
|
677
|
+
edges.set(ek(k, i), f);
|
|
678
|
+
faces.push(f);
|
|
679
|
+
return f;
|
|
680
|
+
};
|
|
681
|
+
const dist = (f, p) => dot(f.n, p) - f.w;
|
|
682
|
+
const ia = pts.indexOf(a), ib = pts.indexOf(b), ic = pts.indexOf(c), id = pts.indexOf(d);
|
|
683
|
+
const tet = [ia, ib, ic, id];
|
|
684
|
+
const centroid = [0, 1, 2].map((k) => tet.reduce((s, i) => s + pts[i][k], 0) / 4);
|
|
685
|
+
const start = [];
|
|
686
|
+
for (let [i, j, k] of [[ia, ib, ic], [ia, ic, id], [ia, id, ib], [ib, id, ic]]) {
|
|
687
|
+
const n = crs(sub(pts[j], pts[i]), sub(pts[k], pts[i]));
|
|
688
|
+
if (dot(n, sub(centroid, pts[i])) > 0)
|
|
689
|
+
[j, k] = [k, j];
|
|
690
|
+
start.push(make(i, j, k));
|
|
691
|
+
}
|
|
692
|
+
for (let idx = 0; idx < pts.length; idx++) {
|
|
693
|
+
if (tet.includes(idx))
|
|
694
|
+
continue;
|
|
695
|
+
for (const f of start) {
|
|
696
|
+
if (dist(f, pts[idx]) > eps) {
|
|
697
|
+
f.out.push(idx);
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
const stack = start.filter((f) => f.out.length);
|
|
703
|
+
while (stack.length) {
|
|
704
|
+
const f = stack.pop();
|
|
705
|
+
if (!f.alive || !f.out.length)
|
|
706
|
+
continue;
|
|
707
|
+
const pIdx = argmax(f.out, (i) => dist(f, pts[i]));
|
|
708
|
+
const p = pts[pIdx];
|
|
709
|
+
const visible = [f];
|
|
710
|
+
const isVisible = new Set([f]);
|
|
711
|
+
for (let q = 0; q < visible.length; q++) {
|
|
712
|
+
const [i, j, k] = visible[q].v;
|
|
713
|
+
for (const [e0, e1] of [[i, j], [j, k], [k, i]]) {
|
|
714
|
+
const h = edges.get(ek(e1, e0));
|
|
715
|
+
if (h && h.alive && !isVisible.has(h) && dist(h, p) > eps) {
|
|
716
|
+
isVisible.add(h);
|
|
717
|
+
visible.push(h);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
const horizon = [];
|
|
722
|
+
const orphans = [];
|
|
723
|
+
for (const g of visible) {
|
|
724
|
+
const [i, j, k] = g.v;
|
|
725
|
+
for (const e of [[i, j], [j, k], [k, i]]) {
|
|
726
|
+
const h = edges.get(ek(e[1], e[0]));
|
|
727
|
+
if (!h || !isVisible.has(h))
|
|
728
|
+
horizon.push(e);
|
|
729
|
+
}
|
|
730
|
+
for (const o of g.out)
|
|
731
|
+
orphans.push(o);
|
|
732
|
+
}
|
|
733
|
+
for (const g of visible) {
|
|
734
|
+
g.alive = false;
|
|
735
|
+
const [i, j, k] = g.v;
|
|
736
|
+
for (const [e0, e1] of [[i, j], [j, k], [k, i]]) {
|
|
737
|
+
if (edges.get(ek(e0, e1)) === g)
|
|
738
|
+
edges.delete(ek(e0, e1));
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
const created = horizon.map(([i, j]) => make(i, j, pIdx));
|
|
742
|
+
for (const idx of orphans) {
|
|
743
|
+
if (idx === pIdx)
|
|
744
|
+
continue;
|
|
745
|
+
for (const nf of created) {
|
|
746
|
+
if (dist(nf, pts[idx]) > eps) {
|
|
747
|
+
nf.out.push(idx);
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
for (const nf of created)
|
|
753
|
+
if (nf.out.length)
|
|
754
|
+
stack.push(nf);
|
|
755
|
+
}
|
|
756
|
+
return faces.filter((f) => f.alive).map((f) => f.v.map((i) => pts[i]));
|
|
757
|
+
}
|
|
758
|
+
export function convexHull2d(points) {
|
|
759
|
+
const seen = new Map();
|
|
760
|
+
for (const [x, y] of points) {
|
|
761
|
+
const p = [pyRoundTo(x, 9), pyRoundTo(y, 9)];
|
|
762
|
+
seen.set(p.join(','), p);
|
|
763
|
+
}
|
|
764
|
+
const pts = [...seen.values()].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
765
|
+
if (pts.length < 3)
|
|
766
|
+
return pts;
|
|
767
|
+
const lower = [], upper = [];
|
|
768
|
+
for (const p of pts) {
|
|
769
|
+
while (lower.length >= 2 && cross2(lower[lower.length - 2], lower[lower.length - 1], p) <= 0)
|
|
770
|
+
lower.pop();
|
|
771
|
+
lower.push(p);
|
|
772
|
+
}
|
|
773
|
+
for (const p of pts.slice().reverse()) {
|
|
774
|
+
while (upper.length >= 2 && cross2(upper[upper.length - 2], upper[upper.length - 1], p) <= 0)
|
|
775
|
+
upper.pop();
|
|
776
|
+
upper.push(p);
|
|
777
|
+
}
|
|
778
|
+
return lower.slice(0, -1).concat(upper.slice(0, -1));
|
|
779
|
+
}
|
|
780
|
+
// ---- mesh utilities ---------------------------------------------------------------------------
|
|
781
|
+
const vkey = (v) => v[0] + ',' + v[1] + ',' + v[2];
|
|
782
|
+
/** How many edges have no matching opposite edge (0 for a closed, printable mesh). */
|
|
783
|
+
export function openEdges(polys) {
|
|
784
|
+
const key = (v) => roundKey(v[0], 4) + ',' + roundKey(v[1], 4) + ',' + roundKey(v[2], 4);
|
|
785
|
+
const edges = new Map();
|
|
786
|
+
for (const p of polys) {
|
|
787
|
+
const vs = p.v.map(key);
|
|
788
|
+
for (let i = 0; i < vs.length; i++) {
|
|
789
|
+
const a = vs[i], b = vs[(i + 1) % vs.length];
|
|
790
|
+
if (a !== b)
|
|
791
|
+
edges.set(a + '|' + b, (edges.get(a + '|' + b) ?? 0) + 1);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
let bad = 0;
|
|
795
|
+
for (const [k, n] of edges) {
|
|
796
|
+
const [a, b] = k.split('|');
|
|
797
|
+
if ((edges.get(b + '|' + a) ?? 0) !== n)
|
|
798
|
+
bad++;
|
|
799
|
+
}
|
|
800
|
+
return bad;
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* After booleans a vertex of one face can sit in the middle of a neighbour's edge (a T-junction).
|
|
804
|
+
* Every vertex lying on an edge is inserted into that edge; changed faces are re-triangulated
|
|
805
|
+
* around their centre so no zero-area triangles appear.
|
|
806
|
+
*/
|
|
807
|
+
export function fixTJunctions(polys) {
|
|
808
|
+
const lineKey = (a, b) => {
|
|
809
|
+
let d = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
|
|
810
|
+
const ln = Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
|
|
811
|
+
if (ln < 1e-9)
|
|
812
|
+
return [null, null];
|
|
813
|
+
d = [d[0] / ln, d[1] / ln, d[2] / ln];
|
|
814
|
+
for (const c of d) {
|
|
815
|
+
if (Math.abs(c) > 1e-9) {
|
|
816
|
+
if (c < 0)
|
|
817
|
+
d = [-d[0], -d[1], -d[2]];
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
const t = a[0] * d[0] + a[1] * d[1] + a[2] * d[2];
|
|
822
|
+
const c = [a[0] - t * d[0], a[1] - t * d[1], a[2] - t * d[2]];
|
|
823
|
+
return [[roundKey(d[0], 4), roundKey(d[1], 4), roundKey(d[2], 4),
|
|
824
|
+
roundKey(c[0], 3), roundKey(c[1], 3), roundKey(c[2], 3)].join(','), d];
|
|
825
|
+
};
|
|
826
|
+
const lines = new Map();
|
|
827
|
+
for (const p of polys) {
|
|
828
|
+
const v = p.v;
|
|
829
|
+
for (let i = 0; i < v.length; i++) {
|
|
830
|
+
const a = v[i], b = v[(i + 1) % v.length];
|
|
831
|
+
const [key, d] = lineKey(a, b);
|
|
832
|
+
if (key === null)
|
|
833
|
+
continue;
|
|
834
|
+
let entry = lines.get(key);
|
|
835
|
+
if (!entry) {
|
|
836
|
+
entry = { d: d, pts: new Map() };
|
|
837
|
+
lines.set(key, entry);
|
|
838
|
+
}
|
|
839
|
+
for (const q of [a, b]) {
|
|
840
|
+
entry.pts.set(vkey(q), [q, q[0] * d[0] + q[1] * d[1] + q[2] * d[2]]);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
const rows = new Map();
|
|
845
|
+
for (const [key, entry] of lines) {
|
|
846
|
+
const row = [...entry.pts.values()].sort((x, y) => x[1] - y[1]);
|
|
847
|
+
const index = new Map();
|
|
848
|
+
for (const [q, t] of row)
|
|
849
|
+
if (!index.has(vkey(q)))
|
|
850
|
+
index.set(vkey(q), t);
|
|
851
|
+
rows.set(key, { row, index });
|
|
852
|
+
}
|
|
853
|
+
const out = [];
|
|
854
|
+
for (const p of polys) {
|
|
855
|
+
const v = p.v;
|
|
856
|
+
const nv = [];
|
|
857
|
+
let changed = false;
|
|
858
|
+
for (let i = 0; i < v.length; i++) {
|
|
859
|
+
const a = v[i], b = v[(i + 1) % v.length];
|
|
860
|
+
nv.push(a);
|
|
861
|
+
const [key] = lineKey(a, b);
|
|
862
|
+
if (key === null)
|
|
863
|
+
continue;
|
|
864
|
+
const { row, index } = rows.get(key);
|
|
865
|
+
const ta = index.get(vkey(a)), tb = index.get(vkey(b));
|
|
866
|
+
const lo = Math.min(ta, tb), hi = Math.max(ta, tb);
|
|
867
|
+
const mids = row.filter(([, t]) => lo + 1e-7 < t && t < hi - 1e-7).map(([q]) => q);
|
|
868
|
+
if (mids.length) {
|
|
869
|
+
if (ta > tb)
|
|
870
|
+
mids.reverse();
|
|
871
|
+
for (const m of mids)
|
|
872
|
+
nv.push(m);
|
|
873
|
+
changed = true;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
if (!changed) {
|
|
877
|
+
out.push(p);
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
let cx = 0, cy = 0, cz = 0;
|
|
881
|
+
for (const q of nv) {
|
|
882
|
+
cx += q[0];
|
|
883
|
+
cy += q[1];
|
|
884
|
+
cz += q[2];
|
|
885
|
+
}
|
|
886
|
+
const centre = [cx / nv.length, cy / nv.length, cz / nv.length];
|
|
887
|
+
for (let i = 0; i < nv.length; i++)
|
|
888
|
+
out.push(new Poly([centre, nv[i], nv[(i + 1) % nv.length]], p.c, p.n, p.w));
|
|
889
|
+
}
|
|
890
|
+
return out;
|
|
891
|
+
}
|
|
892
|
+
export function volume(polys) {
|
|
893
|
+
let vol = 0;
|
|
894
|
+
for (const p of polys) {
|
|
895
|
+
const v = p.v;
|
|
896
|
+
const a = v[0];
|
|
897
|
+
for (let i = 1; i < v.length - 1; i++) {
|
|
898
|
+
const b = v[i], c = v[i + 1];
|
|
899
|
+
vol += a[0] * (b[1] * c[2] - b[2] * c[1]) - a[1] * (b[0] * c[2] - b[2] * c[0]) + a[2] * (b[0] * c[1] - b[1] * c[0]);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return vol / 6;
|
|
903
|
+
}
|
|
904
|
+
export function triangleCount(polys) {
|
|
905
|
+
let n = 0;
|
|
906
|
+
for (const p of polys)
|
|
907
|
+
n += p.v.length - 2;
|
|
908
|
+
return n;
|
|
909
|
+
}
|
|
910
|
+
/** Binary STL. */
|
|
911
|
+
export function stlBytes(polys) {
|
|
912
|
+
const n = triangleCount(polys);
|
|
913
|
+
const buf = new ArrayBuffer(84 + 50 * n);
|
|
914
|
+
const bytes = new Uint8Array(buf);
|
|
915
|
+
const header = 'Forma STL'.padEnd(80, ' ');
|
|
916
|
+
for (let i = 0; i < 80; i++)
|
|
917
|
+
bytes[i] = header.charCodeAt(i);
|
|
918
|
+
const dv = new DataView(buf);
|
|
919
|
+
dv.setUint32(80, n, true);
|
|
920
|
+
let o = 84;
|
|
921
|
+
for (const p of polys) {
|
|
922
|
+
const v = p.v;
|
|
923
|
+
const [nx, ny, nz] = p.n;
|
|
924
|
+
const [x0, y0, z0] = v[0];
|
|
925
|
+
for (let i = 1; i < v.length - 1; i++) {
|
|
926
|
+
const [x1, y1, z1] = v[i];
|
|
927
|
+
const [x2, y2, z2] = v[i + 1];
|
|
928
|
+
dv.setFloat32(o, nx, true);
|
|
929
|
+
dv.setFloat32(o + 4, ny, true);
|
|
930
|
+
dv.setFloat32(o + 8, nz, true);
|
|
931
|
+
dv.setFloat32(o + 12, x0, true);
|
|
932
|
+
dv.setFloat32(o + 16, y0, true);
|
|
933
|
+
dv.setFloat32(o + 20, z0, true);
|
|
934
|
+
dv.setFloat32(o + 24, x1, true);
|
|
935
|
+
dv.setFloat32(o + 28, y1, true);
|
|
936
|
+
dv.setFloat32(o + 32, z1, true);
|
|
937
|
+
dv.setFloat32(o + 36, x2, true);
|
|
938
|
+
dv.setFloat32(o + 40, y2, true);
|
|
939
|
+
dv.setFloat32(o + 44, z2, true);
|
|
940
|
+
dv.setUint16(o + 48, 0, true);
|
|
941
|
+
o += 50;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return bytes;
|
|
945
|
+
}
|
|
946
|
+
//# sourceMappingURL=geometry.js.map
|