@indispec/fs-compat 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +188 -2
- package/package.json +46 -3
- package/src/engine/assembly.js +481 -0
- package/src/engine/csg.js +580 -0
- package/src/engine/interp.js +1713 -0
- package/src/engine/lex.js +166 -0
- package/src/engine/parse.js +539 -0
- package/src/engine/project.js +409 -0
- package/src/engine/regions.js +923 -0
- package/src/engine/trace.js +211 -0
- package/src/engine/values.js +494 -0
- package/src/index.d.ts +746 -0
- package/src/index.js +108 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* csg.js — the lazy SDF/CSG kernel.
|
|
3
|
+
*
|
|
4
|
+
* Solids are expression trees of signed-distance functions. In the browser
|
|
5
|
+
* pipeline the trees are never *sampled* — glsl.js inlines them as GLSL
|
|
6
|
+
* and the GPU sphere-traces them — so the sdf() sampling methods of the
|
|
7
|
+
* original are deliberately not ported. Everything needed to build, transform,
|
|
8
|
+
* boolean and bound the trees is: construction + bbox().
|
|
9
|
+
*
|
|
10
|
+
* Points are plain [x, y, z] arrays; distances are millimetres, negative
|
|
11
|
+
* inside. Geometry classes: Circle2/Rect2/Poly2 (2D profiles), Box/Prism/
|
|
12
|
+
* Revolve/Union/Diff/Isect/Moved (solid nodes), Solid/Scene (bodies).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { FsError } from './values.js'
|
|
16
|
+
|
|
17
|
+
// -- tiny vector helpers (3d, on plain arrays) ------------------------------
|
|
18
|
+
|
|
19
|
+
const v3 = {
|
|
20
|
+
add: (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]],
|
|
21
|
+
sub: (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
|
|
22
|
+
scale: (a, s) => [a[0] * s, a[1] * s, a[2] * s],
|
|
23
|
+
addScaled: (a, b, s) => [a[0] + b[0] * s, a[1] + b[1] * s, a[2] + b[2] * s],
|
|
24
|
+
cross: (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
|
|
25
|
+
a[0] * b[1] - a[1] * b[0]],
|
|
26
|
+
// sqrt-of-sum (not Math.hypot, which scales and can differ in the last
|
|
27
|
+
// ulp — numpy computes sqrt(dot(x, x)) and the GLSL text must match)
|
|
28
|
+
norm: (a) => Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]),
|
|
29
|
+
// 0-seeded like numpy's dot (np.dot / BLAS ddot): 0 + t0 + t1 + t2, which
|
|
30
|
+
// also turns -0 components into +0 exactly the way numpy does
|
|
31
|
+
dot: (a, b) => 0 + a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
|
|
32
|
+
normalized: (a) => {
|
|
33
|
+
const n = Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2])
|
|
34
|
+
return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n]
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const min3 = (a, b) => [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2])]
|
|
39
|
+
const max3 = (a, b) => [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.max(a[2], b[2])]
|
|
40
|
+
const extend = (lo, hi, p) => {
|
|
41
|
+
for (let i = 0; i < 3; i++) {
|
|
42
|
+
if (p[i] < lo[i]) lo[i] = p[i]
|
|
43
|
+
if (p[i] > hi[i]) hi[i] = p[i]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// 2D profiles (plane coordinates, mm)
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
export class Circle2 {
|
|
52
|
+
constructor(c, r) {
|
|
53
|
+
this.c = [c[0], c[1]]
|
|
54
|
+
this.r = +r
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
bbox() {
|
|
58
|
+
return [[this.c[0] - this.r, this.c[1] - this.r],
|
|
59
|
+
[this.c[0] + this.r, this.c[1] + this.r]]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
toString() { return `circle([${this.c}], r=${this.r})` }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class Rect2 {
|
|
66
|
+
constructor(lo, hi) {
|
|
67
|
+
this.lo = [Math.min(lo[0], hi[0]), Math.min(lo[1], hi[1])]
|
|
68
|
+
this.hi = [Math.max(lo[0], hi[0]), Math.max(lo[1], hi[1])]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
bbox() { return [this.lo, this.hi] }
|
|
72
|
+
|
|
73
|
+
toString() { return `rect(${this.lo}..${this.hi})` }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class Poly2 {
|
|
77
|
+
/** General closed polygon (winding-number sign, exact edge distance). */
|
|
78
|
+
constructor(pts) {
|
|
79
|
+
this.pts = pts.map((p) => [p[0], p[1]])
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
bbox() {
|
|
83
|
+
const lo = [Infinity, Infinity]
|
|
84
|
+
const hi = [-Infinity, -Infinity]
|
|
85
|
+
for (const [x, y] of this.pts) {
|
|
86
|
+
if (x < lo[0]) lo[0] = x
|
|
87
|
+
if (y < lo[1]) lo[1] = y
|
|
88
|
+
if (x > hi[0]) hi[0] = x
|
|
89
|
+
if (y > hi[1]) hi[1] = y
|
|
90
|
+
}
|
|
91
|
+
return [lo, hi]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
toString() { return `poly(${this.pts.length} pts)` }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
// Solid nodes
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
export class Node {}
|
|
102
|
+
|
|
103
|
+
export class Box extends Node {
|
|
104
|
+
constructor(lo, hi) {
|
|
105
|
+
super()
|
|
106
|
+
this.lo = [Math.min(lo[0], hi[0]), Math.min(lo[1], hi[1]), Math.min(lo[2], hi[2])]
|
|
107
|
+
this.hi = [Math.max(lo[0], hi[0]), Math.max(lo[1], hi[1]), Math.max(lo[2], hi[2])]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
bbox() { return [this.lo, this.hi] }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export class Prism extends Node {
|
|
114
|
+
/** Extrude a union of 2D profiles along the plane normal, w0..w1. */
|
|
115
|
+
constructor(plane, profiles, w0, w1) {
|
|
116
|
+
super()
|
|
117
|
+
this.plane = plane
|
|
118
|
+
this.profiles = [...profiles]
|
|
119
|
+
this.w0 = Math.min(w0, w1)
|
|
120
|
+
this.w1 = Math.max(w0, w1)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
bbox() {
|
|
124
|
+
const lo = [Infinity, Infinity, Infinity]
|
|
125
|
+
const hi = [-Infinity, -Infinity, -Infinity]
|
|
126
|
+
for (const pr of this.profiles) {
|
|
127
|
+
const [plo, phi] = pr.bbox()
|
|
128
|
+
for (const u of [plo[0], phi[0]]) {
|
|
129
|
+
for (const vv of [plo[1], phi[1]]) {
|
|
130
|
+
for (const w of [this.w0, this.w1]) {
|
|
131
|
+
extend(lo, hi, this.plane.toWorld([[u, vv, w]])[0])
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return [lo, hi]
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export class Revolve extends Node {
|
|
141
|
+
/**
|
|
142
|
+
* Surface of revolution: a 2D profile spun about an axis in its plane.
|
|
143
|
+
*
|
|
144
|
+
* The profile may be a closed polyline or a circle. A circle whose plane
|
|
145
|
+
* contains the axis spins into a torus, which is what `pipeLib` uses for
|
|
146
|
+
* radiused pipe elbows; its centre maps into (r, z) unchanged in radius
|
|
147
|
+
* because the mapping is an isometry on any plane that contains the axis.
|
|
148
|
+
*/
|
|
149
|
+
constructor(plane, profile, axisO, axisD, angle = 2 * Math.PI) {
|
|
150
|
+
super()
|
|
151
|
+
this.plane = plane
|
|
152
|
+
this.axisO = [axisO[0], axisO[1], axisO[2]]
|
|
153
|
+
this.axisD = v3.normalized([axisD[0], axisD[1], axisD[2]])
|
|
154
|
+
this.poly = this.#toRz(profile)
|
|
155
|
+
this.angle = angle
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#toRz(profile) {
|
|
159
|
+
// Express a sketch profile in the axis' (radius, height) frame.
|
|
160
|
+
if (profile instanceof Circle2) {
|
|
161
|
+
const wc = this.plane.toWorld([[profile.c[0], profile.c[1], 0.0]])[0]
|
|
162
|
+
const [r, z] = this.#rzPoint(wc)
|
|
163
|
+
return new Circle2([r, z], profile.r)
|
|
164
|
+
}
|
|
165
|
+
if (!(profile instanceof Poly2)) {
|
|
166
|
+
throw new FsError(`cannot revolve a ${profile?.constructor?.name ?? 'unknown'} profile`)
|
|
167
|
+
}
|
|
168
|
+
const wpts = this.plane.toWorld(profile.pts.map((p) => [p[0], p[1], 0.0]))
|
|
169
|
+
return new Poly2(wpts.map((p) => this.#rzPoint(p)))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#rzPoint(P) {
|
|
173
|
+
const rel = v3.sub(P, this.axisO)
|
|
174
|
+
const z = v3.dot(rel, this.axisD)
|
|
175
|
+
const radial = v3.sub(rel, v3.scale(this.axisD, z))
|
|
176
|
+
// sqrt-of-sum, matching numpy's norm (see v3.norm note above)
|
|
177
|
+
return [Math.sqrt(radial[0] * radial[0] + radial[1] * radial[1]
|
|
178
|
+
+ radial[2] * radial[2]), z]
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
bbox() {
|
|
182
|
+
const [plo, phi] = this.poly.bbox()
|
|
183
|
+
const rmax = Math.max(Math.abs(plo[0]), Math.abs(phi[0]))
|
|
184
|
+
const zlo = plo[1]
|
|
185
|
+
const zhi = phi[1]
|
|
186
|
+
// conservative box around the axis
|
|
187
|
+
const o = this.axisO
|
|
188
|
+
const d = this.axisD
|
|
189
|
+
let perp = [-d[1], d[0], 0.0]
|
|
190
|
+
if (v3.norm(perp) < 1e-9) perp = [1.0, 0.0, 0.0]
|
|
191
|
+
perp = v3.normalized(perp)
|
|
192
|
+
const other = v3.cross(d, perp)
|
|
193
|
+
const lo = [Infinity, Infinity, Infinity]
|
|
194
|
+
const hi = [-Infinity, -Infinity, -Infinity]
|
|
195
|
+
for (const zz of [zlo, zhi]) {
|
|
196
|
+
for (const sx of [-1, 1]) {
|
|
197
|
+
for (const sy of [-1, 1]) {
|
|
198
|
+
// o + zz*d + rmax*(sx*perp + sy*other), same op order as numpy
|
|
199
|
+
const p = [
|
|
200
|
+
o[0] + zz * d[0] + rmax * (sx * perp[0] + sy * other[0]),
|
|
201
|
+
o[1] + zz * d[1] + rmax * (sx * perp[1] + sy * other[1]),
|
|
202
|
+
o[2] + zz * d[2] + rmax * (sx * perp[2] + sy * other[2]),
|
|
203
|
+
]
|
|
204
|
+
extend(lo, hi, p)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return [lo, hi]
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export class Union extends Node {
|
|
213
|
+
constructor(a, b) { super(); this.a = a; this.b = b }
|
|
214
|
+
|
|
215
|
+
bbox() {
|
|
216
|
+
const [alo, ahi] = this.a.bbox()
|
|
217
|
+
const [blo, bhi] = this.b.bbox()
|
|
218
|
+
return [min3(alo, blo), max3(ahi, bhi)]
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export class Diff extends Node {
|
|
223
|
+
constructor(a, b) { super(); this.a = a; this.b = b }
|
|
224
|
+
|
|
225
|
+
bbox() { return this.a.bbox() }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export class Isect extends Node {
|
|
229
|
+
constructor(a, b) { super(); this.a = a; this.b = b }
|
|
230
|
+
|
|
231
|
+
bbox() {
|
|
232
|
+
const [alo, ahi] = this.a.bbox()
|
|
233
|
+
const [blo, bhi] = this.b.bbox()
|
|
234
|
+
return [max3(alo, blo), min3(ahi, bhi)]
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export class Moved extends Node {
|
|
239
|
+
constructor(node, xf) { super(); this.node = node; this.xf = xf }
|
|
240
|
+
|
|
241
|
+
bbox() {
|
|
242
|
+
const [lo, hi] = this.node.bbox()
|
|
243
|
+
let wlo = [Infinity, Infinity, Infinity]
|
|
244
|
+
let whi = [-Infinity, -Infinity, -Infinity]
|
|
245
|
+
for (const x of [lo[0], hi[0]]) {
|
|
246
|
+
for (const y of [lo[1], hi[1]]) {
|
|
247
|
+
for (const z of [lo[2], hi[2]]) {
|
|
248
|
+
const p = this.xf.apply([[x, y, z]])[0]
|
|
249
|
+
extend(wlo, whi, p)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return [wlo, whi]
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* A fillet applied to a body's edges (a kernel operation node).
|
|
259
|
+
*
|
|
260
|
+
* An SDF tree cannot express a round analytically, so this node is a marker: the exact-geometry
|
|
261
|
+
* backend translates `node` and rounds the selected edges with the OCCT kernel. `direction`
|
|
262
|
+
* optionally restricts the operation to edges parallel to a world direction (`null` = every edge).
|
|
263
|
+
* The bounding box delegates to the child — a fillet never grows the envelope.
|
|
264
|
+
*/
|
|
265
|
+
export class Fillet extends Node {
|
|
266
|
+
/**
|
|
267
|
+
* @param {Node} node Body to fillet.
|
|
268
|
+
* @param {number} radius Fillet radius in millimetres.
|
|
269
|
+
* @param {number[] | null} direction World direction of the edges to fillet, or null for all.
|
|
270
|
+
*/
|
|
271
|
+
constructor(node, radius, direction = null) {
|
|
272
|
+
super()
|
|
273
|
+
this.node = node
|
|
274
|
+
this.radius = radius
|
|
275
|
+
this.direction = direction
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
bbox() { return this.node.bbox() }
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Extrude a planar face of an existing body (a kernel operation node).
|
|
283
|
+
*
|
|
284
|
+
* The profile is a **face of `node`**, not a sketch: the face is resolved as exact B-rep by the
|
|
285
|
+
* backend, so its boundary (including inner wires, i.e. holes) and arcs are exact. The node records
|
|
286
|
+
* a *geometric anchor* — a world point on the face plus its outward normal — because B-rep face ids
|
|
287
|
+
* only hold within one evaluation (docs/CAD.md §7.3): every evaluation re-resolves the anchor
|
|
288
|
+
* against the translated body. A point that no longer lies on a planar face is a typed kernel
|
|
289
|
+
* error, never a silent wrong face.
|
|
290
|
+
*
|
|
291
|
+
* `w0`/`w1` are signed distances along the resolved outward normal (mm), so an inward extrude is a
|
|
292
|
+
* negative window. The SDF path cannot express a face profile, so this node is a marker: the exact
|
|
293
|
+
* backend resolves the face and builds the prism with OCCT.
|
|
294
|
+
*/
|
|
295
|
+
export class FaceExtrude extends Node {
|
|
296
|
+
/**
|
|
297
|
+
* @param {Node} node Body whose planar face is extruded.
|
|
298
|
+
* @param {number[]} point World point on the face (mm) — the anchor.
|
|
299
|
+
* @param {number[]} normal Outward unit normal of the face at the anchor.
|
|
300
|
+
* @param {number} w0 Start offset along the outward normal (mm).
|
|
301
|
+
* @param {number} w1 End offset along the outward normal (mm).
|
|
302
|
+
*/
|
|
303
|
+
constructor(node, point, normal, w0, w1) {
|
|
304
|
+
super()
|
|
305
|
+
this.node = node
|
|
306
|
+
this.point = [point[0], point[1], point[2]]
|
|
307
|
+
const n = v3.normalized([normal[0], normal[1], normal[2]])
|
|
308
|
+
this.normal = n[0] === 0 && n[1] === 0 && n[2] === 0 ? [0, 0, 1] : n
|
|
309
|
+
this.w0 = w0
|
|
310
|
+
this.w1 = w1
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
bbox() {
|
|
314
|
+
// Conservative envelope: the body's box grown by the window on every side, so framing includes
|
|
315
|
+
// the added material whichever way the resolved face normal points.
|
|
316
|
+
const [lo, hi] = this.node.bbox()
|
|
317
|
+
const reach = Math.max(Math.abs(this.w0), Math.abs(this.w1))
|
|
318
|
+
return [
|
|
319
|
+
[lo[0] - reach, lo[1] - reach, lo[2] - reach],
|
|
320
|
+
[hi[0] + reach, hi[1] + reach, hi[2] + reach],
|
|
321
|
+
]
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* A chamfer applied to a body's edges (a kernel operation node).
|
|
327
|
+
*
|
|
328
|
+
* Same contract as {@link Fillet}: a marker node translated by the OCCT backend, with an optional
|
|
329
|
+
* world direction to restrict which edges are cut.
|
|
330
|
+
*/
|
|
331
|
+
export class Chamfer extends Node {
|
|
332
|
+
/**
|
|
333
|
+
* @param {Node} node Body to chamfer.
|
|
334
|
+
* @param {number} distance Chamfer distance in millimetres.
|
|
335
|
+
* @param {number[] | null} direction World direction of the edges to chamfer, or null for all.
|
|
336
|
+
*/
|
|
337
|
+
constructor(node, distance, direction = null) {
|
|
338
|
+
super()
|
|
339
|
+
this.node = node
|
|
340
|
+
this.distance = distance
|
|
341
|
+
this.direction = direction
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
bbox() { return this.node.bbox() }
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* A shell applied to a body (a kernel operation node).
|
|
349
|
+
*
|
|
350
|
+
* Like {@link Fillet}, this is a marker: the SDF tree cannot express a hollowed solid with a wall
|
|
351
|
+
* thickness, so the exact-geometry backend translates `node` and hollows it with OCCT. `opening`
|
|
352
|
+
* names the planar face group removed to open the shell: `top`/`bottom` (+Z/−Z) or `x`/`y`
|
|
353
|
+
* (the +X/+Y side). The bounding box delegates to the child — a shell never grows the envelope.
|
|
354
|
+
*/
|
|
355
|
+
export class Shell extends Node {
|
|
356
|
+
/**
|
|
357
|
+
* @param {Node} node Body to hollow.
|
|
358
|
+
* @param {number} thickness Wall thickness in millimetres.
|
|
359
|
+
* @param {string} opening Face group to remove (`top`, `bottom`, `x` or `y`).
|
|
360
|
+
*/
|
|
361
|
+
constructor(node, thickness, opening = 'top') {
|
|
362
|
+
super()
|
|
363
|
+
this.node = node
|
|
364
|
+
this.thickness = thickness
|
|
365
|
+
this.opening = opening
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
bbox() { return this.node.bbox() }
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* A loft through two or more sketch profiles (a kernel operation node).
|
|
373
|
+
*
|
|
374
|
+
* An SDF tree cannot express the ruled/approximated surface between sections, so this node is a
|
|
375
|
+
* marker: the exact-geometry backend builds each section's planar face and lofts through their
|
|
376
|
+
* outer wires with OCCT. Sections keep their own plane, so profiles can sit on parallel planes at
|
|
377
|
+
* different offsets (the ordinary "two sketches, two heights" case) or on different world planes.
|
|
378
|
+
*/
|
|
379
|
+
export class Loft extends Node {
|
|
380
|
+
/**
|
|
381
|
+
* @param {Array<{ plane: object, profiles: object[] }>} sections Sections in loft order; each
|
|
382
|
+
* carries the sketch plane and the (single) closed profile used for that section.
|
|
383
|
+
*/
|
|
384
|
+
constructor(sections) {
|
|
385
|
+
super()
|
|
386
|
+
this.sections = sections
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
bbox() {
|
|
390
|
+
const lo = [Infinity, Infinity, Infinity]
|
|
391
|
+
const hi = [-Infinity, -Infinity, -Infinity]
|
|
392
|
+
for (const section of this.sections) {
|
|
393
|
+
for (const profile of section.profiles) {
|
|
394
|
+
const [plo, phi] = profile.bbox()
|
|
395
|
+
for (const u of [plo[0], phi[0]]) {
|
|
396
|
+
for (const v of [plo[1], phi[1]]) extend(lo, hi, section.plane.toWorld([[u, v, 0]])[0])
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return [lo, hi]
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* A sweep of one sketch profile along another sketch's path (a kernel operation node).
|
|
406
|
+
*
|
|
407
|
+
* The profile is a closed section on its own sketch plane; the path is the path sketch's chained
|
|
408
|
+
* contour as 2D points in that sketch's plane (a closed loop — the sketch model closes any chained
|
|
409
|
+
* contour). The SDF tree cannot express a pipe/loop solid, so this is a marker node translated by
|
|
410
|
+
* the exact kernel's pipe-shell algorithm.
|
|
411
|
+
*/
|
|
412
|
+
export class Sweep extends Node {
|
|
413
|
+
/**
|
|
414
|
+
* @param {object} profile Closed section profile (`Profile2`).
|
|
415
|
+
* @param {import('./values.js').Plane} plane Plane carrying the profile.
|
|
416
|
+
* @param {Array<[number, number]>} path Path points in `pathPlane` coordinates.
|
|
417
|
+
* @param {import('./values.js').Plane} pathPlane Plane carrying the path.
|
|
418
|
+
*/
|
|
419
|
+
constructor(profile, plane, path, pathPlane) {
|
|
420
|
+
super()
|
|
421
|
+
this.profile = profile
|
|
422
|
+
this.plane = plane
|
|
423
|
+
this.path = path
|
|
424
|
+
this.pathPlane = pathPlane
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
bbox() {
|
|
428
|
+
const lo = [Infinity, Infinity, Infinity]
|
|
429
|
+
const hi = [-Infinity, -Infinity, -Infinity]
|
|
430
|
+
const [plo, phi] = this.profile.bbox()
|
|
431
|
+
// Conservative envelope: every path point grown by the profile's half-diagonal, so framing
|
|
432
|
+
// includes the swept section even before exact evaluation.
|
|
433
|
+
const radius = Math.hypot(phi[0] - plo[0], phi[1] - plo[1]) / 2
|
|
434
|
+
for (const [u, v] of this.path) {
|
|
435
|
+
const world = this.pathPlane.toWorld([[u, v, 0]])[0]
|
|
436
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
437
|
+
lo[axis] = Math.min(lo[axis], world[axis] - radius)
|
|
438
|
+
hi[axis] = Math.max(hi[axis], world[axis] + radius)
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return [lo, hi]
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Scalar signed-distance evaluation of a tree at one point (mm, negative
|
|
447
|
+
* inside). The render paths never sample SDFs — the GPU traces generated
|
|
448
|
+
* GLSL and the display path uses clips — but CPU baking (glb export) and
|
|
449
|
+
* future tooling need point evaluation. Ported from fscsg.py's formulas.
|
|
450
|
+
*/
|
|
451
|
+
export function sdfAt(node, p) {
|
|
452
|
+
if (node instanceof Box) {
|
|
453
|
+
const c = [(node.lo[0] + node.hi[0]) / 2, (node.lo[1] + node.hi[1]) / 2,
|
|
454
|
+
(node.lo[2] + node.hi[2]) / 2]
|
|
455
|
+
const h = [(node.hi[0] - node.lo[0]) / 2, (node.hi[1] - node.lo[1]) / 2,
|
|
456
|
+
(node.hi[2] - node.lo[2]) / 2]
|
|
457
|
+
const dx = [Math.abs(p[0] - c[0]) - h[0], Math.abs(p[1] - c[1]) - h[1],
|
|
458
|
+
Math.abs(p[2] - c[2]) - h[2]]
|
|
459
|
+
const outside = Math.hypot(Math.max(dx[0], 0), Math.max(dx[1], 0), Math.max(dx[2], 0))
|
|
460
|
+
const inside = Math.min(Math.max(dx[0], dx[1], dx[2]), 0)
|
|
461
|
+
return outside + inside
|
|
462
|
+
}
|
|
463
|
+
if (node instanceof Prism) {
|
|
464
|
+
const [u, v, w] = node.plane.toLocal([p])[0]
|
|
465
|
+
let d2 = Infinity
|
|
466
|
+
for (const pr of node.profiles) d2 = Math.min(d2, sdf2At(pr, u, v))
|
|
467
|
+
const dw = Math.max(node.w0 - w, w - node.w1)
|
|
468
|
+
return Math.max(d2, dw)
|
|
469
|
+
}
|
|
470
|
+
if (node instanceof Revolve) {
|
|
471
|
+
const rel = v3.sub(p, node.axisO)
|
|
472
|
+
const z = v3.dot(rel, node.axisD)
|
|
473
|
+
const radial = v3.sub(rel, v3.scale(node.axisD, z))
|
|
474
|
+
const r = Math.sqrt(radial[0] * radial[0] + radial[1] * radial[1]
|
|
475
|
+
+ radial[2] * radial[2])
|
|
476
|
+
return sdf2At(node.poly, r, z)
|
|
477
|
+
}
|
|
478
|
+
if (node instanceof Moved) {
|
|
479
|
+
const inv = node.xf.inverse()
|
|
480
|
+
return sdfAt(node.node, inv.apply([p])[0])
|
|
481
|
+
}
|
|
482
|
+
if (node instanceof Union) return Math.min(sdfAt(node.a, p), sdfAt(node.b, p))
|
|
483
|
+
if (node instanceof Diff) return Math.max(sdfAt(node.a, p), -sdfAt(node.b, p))
|
|
484
|
+
if (node instanceof Isect) return Math.max(sdfAt(node.a, p), sdfAt(node.b, p))
|
|
485
|
+
throw new Error(`sdf: unsupported node ${node?.constructor?.name}`)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** 2D profile SDF at (u, v), negative inside (from fscsg.py). */
|
|
489
|
+
function sdf2At(pr, u, v) {
|
|
490
|
+
if (pr instanceof Circle2) {
|
|
491
|
+
return Math.hypot(u - pr.c[0], v - pr.c[1]) - pr.r
|
|
492
|
+
}
|
|
493
|
+
if (pr instanceof Rect2) {
|
|
494
|
+
const c = [(pr.lo[0] + pr.hi[0]) / 2, (pr.lo[1] + pr.hi[1]) / 2]
|
|
495
|
+
const h = [(pr.hi[0] - pr.lo[0]) / 2, (pr.hi[1] - pr.lo[1]) / 2]
|
|
496
|
+
const dx = Math.abs(u - c[0]) - h[0]
|
|
497
|
+
const dy = Math.abs(v - c[1]) - h[1]
|
|
498
|
+
return Math.hypot(Math.max(dx, 0), Math.max(dy, 0)) + Math.min(Math.max(dx, dy), 0)
|
|
499
|
+
}
|
|
500
|
+
if (pr instanceof Poly2) {
|
|
501
|
+
const pts = pr.pts
|
|
502
|
+
const n = pts.length
|
|
503
|
+
let d = Infinity
|
|
504
|
+
let s = 1
|
|
505
|
+
for (let i = 0; i < n; i++) {
|
|
506
|
+
const a = pts[i]
|
|
507
|
+
const b = pts[(i + 1) % n]
|
|
508
|
+
const ex = b[0] - a[0]
|
|
509
|
+
const ey = b[1] - a[1]
|
|
510
|
+
const wx = u - a[0]
|
|
511
|
+
const wy = v - a[1]
|
|
512
|
+
const ee = ex * ex + ey * ey
|
|
513
|
+
const t = ee > 0 ? Math.min(1, Math.max(0, (wx * ex + wy * ey) / ee)) : 0
|
|
514
|
+
const px = wx - ex * t
|
|
515
|
+
const py = wy - ey * t
|
|
516
|
+
d = Math.min(d, Math.hypot(px, py))
|
|
517
|
+
if ((v >= a[1]) !== (v >= b[1]) && u < a[0] + ex * (v - a[1]) / (ey === 0 ? 1e-30 : ey)) {
|
|
518
|
+
s = -s
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return s * d
|
|
522
|
+
}
|
|
523
|
+
throw new Error(`sdf2: unsupported profile ${pr?.constructor?.name}`)
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// ---------------------------------------------------------------------------
|
|
527
|
+
// Scene
|
|
528
|
+
// ---------------------------------------------------------------------------
|
|
529
|
+
|
|
530
|
+
export class Solid {
|
|
531
|
+
/** A named, coloured body. Mutated in place by booleans, like Onshape. */
|
|
532
|
+
constructor(node, name = null, color = null, kind = 'solid') {
|
|
533
|
+
this.node = node
|
|
534
|
+
this.name = name
|
|
535
|
+
this.color = color
|
|
536
|
+
this.alive = true
|
|
537
|
+
this.kind = kind
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
bbox() { return this.node.bbox() }
|
|
541
|
+
|
|
542
|
+
toString() { return `<Solid ${this.name ?? 'unnamed'} ${this.kind}>` }
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export class Scene {
|
|
546
|
+
constructor() {
|
|
547
|
+
this.solids = []
|
|
548
|
+
this.sketches = {} // id path -> Sketch
|
|
549
|
+
this.registry = {} // id path -> [Solid] created by that operation
|
|
550
|
+
this.notes = []
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
add(solid) {
|
|
554
|
+
this.solids.push(solid)
|
|
555
|
+
return solid
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
alive(includeRef = true) {
|
|
559
|
+
let out = this.solids.filter((s) => s.alive)
|
|
560
|
+
if (!includeRef) {
|
|
561
|
+
out = out.filter((s) => !/volume/i.test(s.name ?? ''))
|
|
562
|
+
}
|
|
563
|
+
return out
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
bbox(includeRef = false) {
|
|
567
|
+
const solids = this.alive(includeRef)
|
|
568
|
+
if (!solids.length) return [[0, 0, 0], [1, 1, 1]]
|
|
569
|
+
const lo = [Infinity, Infinity, Infinity]
|
|
570
|
+
const hi = [-Infinity, -Infinity, -Infinity]
|
|
571
|
+
for (const s of solids) {
|
|
572
|
+
const [slo, shi] = s.bbox()
|
|
573
|
+
for (let i = 0; i < 3; i++) {
|
|
574
|
+
if (slo[i] < lo[i]) lo[i] = slo[i]
|
|
575
|
+
if (shi[i] > hi[i]) hi[i] = shi[i]
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return [lo, hi]
|
|
579
|
+
}
|
|
580
|
+
}
|