@entro314labs/iso-engine 0.0.0-stage → 0.5.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 +165 -2
- package/THIRD_PARTY_NOTICES.md +39 -0
- package/dist/engine/anim.js +225 -0
- package/dist/engine/behaviors.js +105 -0
- package/dist/engine/diagram.js +80 -0
- package/dist/engine/draw.js +117 -0
- package/dist/engine/export.js +81 -0
- package/dist/engine/figure.js +35 -0
- package/dist/engine/global.js +4 -0
- package/dist/engine/index.d.ts +278 -0
- package/dist/engine/index.js +164 -0
- package/dist/engine/math.js +190 -0
- package/dist/engine/node.js +290 -0
- package/dist/engine/paths.js +76 -0
- package/dist/engine/prefabs/architecture.js +136 -0
- package/dist/engine/prefabs/basics.js +131 -0
- package/dist/engine/prefabs/devices.js +295 -0
- package/dist/engine/prefabs/icons.js +49 -0
- package/dist/engine/prefabs/index.js +12 -0
- package/dist/engine/prefabs/logistics.js +156 -0
- package/dist/engine/scene.js +466 -0
- package/dist/engine/shapes.js +781 -0
- package/dist/engine/sort.js +153 -0
- package/dist/engine/spec.js +81 -0
- package/dist/engine/style.js +87 -0
- package/dist/engine.css +1 -0
- package/dist/figure.css +93 -0
- package/dist/figure.d.ts +22 -0
- package/dist/figure.js +73 -0
- package/dist/filters.d.ts +9 -0
- package/dist/filters.js +22 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/iso.global.js +7990 -0
- package/dist/kernel.d.ts +102 -0
- package/dist/kernel.js +172 -0
- package/dist/loops.d.ts +49 -0
- package/dist/loops.js +64 -0
- package/dist/patterns.d.ts +44 -0
- package/dist/patterns.js +97 -0
- package/dist/theme.css +19 -0
- package/dist/types.d.ts +86 -0
- package/dist/types.js +1 -0
- package/package.json +50 -4
- package/skills/iso-engine/SKILL.md +143 -0
- package/skills/iso-engine/examples/engine.html +42 -0
- package/skills/iso-engine/examples/figure.html +51 -0
- package/skills/iso-engine/examples/loop.html +68 -0
- package/skills/iso-engine/examples/loop.js +36 -0
- package/skills/iso-engine/examples/matcher.js +134 -0
- package/skills/iso-engine/examples/spec.html +42 -0
- package/skills/iso-engine/references/animation.md +116 -0
- package/skills/iso-engine/references/api.md +207 -0
- package/skills/iso-engine/references/composition.md +66 -0
- package/skills/iso-engine/references/diagrams.md +68 -0
- package/skills/iso-engine/references/figures.md +359 -0
- package/skills/iso-engine/references/loops.md +126 -0
- package/skills/iso-engine/references/prefabs.md +103 -0
- package/skills/iso-engine/references/spec.md +66 -0
- package/skills/iso-engine/references/style.md +76 -0
- package/skills/iso-engine/references/troubleshooting.md +49 -0
- package/skills/iso-engine/scripts/build.mjs +35 -0
|
@@ -0,0 +1,781 @@
|
|
|
1
|
+
/* Primitives. All geometry is in world units (1 unit ≈ 1 px at zoom 1).
|
|
2
|
+
Conventions
|
|
3
|
+
- box / slab / flat rect: `at` = min corner [x, y, z] (or `center` = [cx, cy, z] footprint centre)
|
|
4
|
+
- cylinder / cone / tube / sphere: `at` = centre of the base [cx, cy, z] (sphere: centre)
|
|
5
|
+
- every shape accepts: id, material, color, class, glow, layer, hidden, style, anchor
|
|
6
|
+
Faces of solids: top (+z), left (+y, screen-left), right (+x, screen-right). */
|
|
7
|
+
import { Node } from './node.js';
|
|
8
|
+
import { DEG, EPS, fmt, cssClass, num, dashArray, textAnchor, fontWeight, rrect, isConvex, signedArea, insetConvex, rotatePts, circlePts, v3, box3 } from './math.js';
|
|
9
|
+
import { applyFrame, frameVec } from './draw.js';
|
|
10
|
+
|
|
11
|
+
/* ───────────────────────── Face drawer ─────────────────────────
|
|
12
|
+
2D drawing on a planar face: u → along U, v → along V (world unit vectors), origin o (world). */
|
|
13
|
+
export class Face {
|
|
14
|
+
constructor(d, o, U, V, w, h) { this.d = d; this.o = o; this.U = U; this.V = V; this.w = w; this.h = h; }
|
|
15
|
+
/* a face given in the node's local coordinates (applies the draw frame) */
|
|
16
|
+
static local(d, o, U, V, w, h) { return new Face(d, d.w(o[0], o[1], o[2]), frameVec(d.frame, v3.norm(U)), frameVec(d.frame, v3.norm(V)), w, h); }
|
|
17
|
+
pt(u, v) { const o = this.o, U = this.U, V = this.V; return [o[0] + U[0] * u + V[0] * v, o[1] + U[1] * u + V[1] * v, o[2] + U[2] * u + V[2] * v]; }
|
|
18
|
+
_shape(pts, close, opt = {}) {
|
|
19
|
+
const d = this.d, path = d.dw(pts.map((p) => this.pt(p[0], p[1])), close);
|
|
20
|
+
const fill = opt.fill, line = opt.line === undefined ? 'ln-soft' : opt.line;
|
|
21
|
+
if (fill) d.fill(path, fill);
|
|
22
|
+
if (line) d.line(path, line, opt.dash ? ' stroke-dasharray="' + dashArray(opt.dash) + '"' : undefined);
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
rect(u, v, w, h, opt = {}) { return this._shape(rrect(u, v, w, h, opt.r || 0), true, opt); }
|
|
26
|
+
poly(pts, opt = {}) { return this._shape(pts, opt.close !== false, opt); }
|
|
27
|
+
line(u1, v1, u2, v2, cls = 'ln-soft') { this.d.line(this.d.dw([this.pt(u1, v1), this.pt(u2, v2)]), cls); return this; }
|
|
28
|
+
circle(u, v, r, opt = {}) { return this._shape(circlePts(u, v, r, opt.n || Math.max(12, Math.round(r * 3))), true, opt); }
|
|
29
|
+
/* evenly spaced lines: vents, slits */
|
|
30
|
+
hlines(u, v, w, h, n, cls = 'ln-soft') { for (let i = 0; i < n; i++) { const y = n === 1 ? v : v + (h * i) / (n - 1); this.line(u, y, u + w, y, cls); } return this; }
|
|
31
|
+
vlines(u, v, w, h, n, cls = 'ln-soft') { for (let i = 0; i < n; i++) { const x = n === 1 ? u : u + (w * i) / (n - 1); this.line(x, v, x, v + h, cls); } return this; }
|
|
32
|
+
/* a grid of cells (windows, keys, panels); fn(i, j) may return per-cell options or false */
|
|
33
|
+
grid(u, v, w, h, cols, rows, opt = {}) {
|
|
34
|
+
const gap = opt.gap ?? 2, cw = (w - gap * (cols - 1)) / cols, ch = (h - gap * (rows - 1)) / rows;
|
|
35
|
+
for (let j = 0; j < rows; j++) for (let i = 0; i < cols; i++) {
|
|
36
|
+
let o = opt;
|
|
37
|
+
if (opt.each) { const r = opt.each(i, j); if (r === false) continue; if (r) o = { ...opt, ...r }; }
|
|
38
|
+
this.rect(u + i * (cw + gap), v + j * (ch + gap), cw, ch, o);
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
/* SVG text laid on the face */
|
|
43
|
+
text(str, u, v, opt = {}) {
|
|
44
|
+
const anchor = textAnchor(opt.anchor), pad = opt.pad ?? 1;
|
|
45
|
+
/* room on this face for the anchor: labels never run past the face they are drawn on */
|
|
46
|
+
const room = opt.max ?? (!this.w ? 0 : anchor === 'middle' ? 2 * Math.min(u, this.w - u) - 2 * pad : anchor === 'end' ? u - pad : this.w - u - pad);
|
|
47
|
+
const fit = opt.fit === false ? { str: String(str), size: opt.size || 8 } : fitText(str, opt.size || 8, opt.cls || 'tx', opt.spacing, room, opt.minSize);
|
|
48
|
+
const size = fit.size; str = fit.str;
|
|
49
|
+
const m = this.d.planeMatrix(this.pt(u, v), this.U, this.V);
|
|
50
|
+
const ls = opt.spacing !== undefined ? ' letter-spacing="' + num(opt.spacing) + '"' : '';
|
|
51
|
+
const w = fontWeight(opt.weight) ? ' font-weight="' + fontWeight(opt.weight) + '"' : '';
|
|
52
|
+
this.d.raw('<text class="' + cssClass(opt.cls || 'tx') + '" transform="' + m + '" font-size="' + fmt(size) + '" text-anchor="' + anchor + '"' + ls + w + (opt.attrs || '') + '>' + escText(str) + '</text>');
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/* raw SVG content in face units, optional extra transform (e.g. 'translate(4 4) scale(2)') */
|
|
56
|
+
svg(markup, opt = {}) {
|
|
57
|
+
const m = this.d.planeMatrix(this.pt(opt.u || 0, opt.v || 0), this.U, this.V);
|
|
58
|
+
this.d.raw('<g transform="' + m + (opt.transform ? ' ' + opt.transform : '') + '"' + (opt.cls ? ' class="' + cssClass(opt.cls) + '"' : '') + '>' + markup + '</g>');
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
/* sub-face offset by (u, v) */
|
|
62
|
+
sub(u, v, w, h) { return new Face(this.d, this.pt(u, v), this.U, this.V, w ?? this.w - u, h ?? this.h - v); }
|
|
63
|
+
/* matrix string for custom content */
|
|
64
|
+
matrix(u = 0, v = 0) { return this.d.planeMatrix(this.pt(u, v), this.U, this.V); }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* Fit a line of mono text into `avail` world units: estimate its width (mono ≈ 0.62 em per char, tracked labels
|
|
68
|
+
≈ 0.72 em, plus explicit letter-spacing), shrink the size to fit, and below `min` truncate with an ellipsis.
|
|
69
|
+
Returns the string and size to draw. `avail` ≤ 0 or not finite means "no limit". */
|
|
70
|
+
export function fitText(str, size, cls, spacing, avail, min = 3) {
|
|
71
|
+
str = String(str);
|
|
72
|
+
min = Math.min(min, size); // never truncate text that was asked to be smaller than the floor
|
|
73
|
+
if (!(avail > 0) || !Number.isFinite(avail)) return { str, size };
|
|
74
|
+
const em = (cls && cls.includes('tx-label') ? 0.72 : 0.62);
|
|
75
|
+
const width = (txt, sz) => txt.length * (sz * em + (spacing || 0));
|
|
76
|
+
if (width(str, size) <= avail) return { str, size };
|
|
77
|
+
const fitted = avail / (str.length * (em + (spacing || 0) / size));
|
|
78
|
+
if (fitted >= min) return { str, size: fitted };
|
|
79
|
+
let out = str;
|
|
80
|
+
while (out.length > 1 && width(out + '…', min) > avail) out = out.slice(0, -1);
|
|
81
|
+
return { str: out + '…', size: min };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function escText(s) { return String(s).replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c])); }
|
|
85
|
+
|
|
86
|
+
const TONES = { top: 'f-top', left: 'f-left', right: 'f-right', bevel: 'f-bevel' };
|
|
87
|
+
const tones = (o) => (o.tone ? { ...TONES, ...o.tone } : TONES);
|
|
88
|
+
const VIEW_N = (S) => [1, 1, 2 * S];
|
|
89
|
+
|
|
90
|
+
/* ───────────────────────── Prism (extruded plan polygon) ───────────────────────── */
|
|
91
|
+
export class Prism extends Node {
|
|
92
|
+
get kind() { return 'prism'; }
|
|
93
|
+
/* local plan polygon (with kinds), base z, height, chamfer */
|
|
94
|
+
plan() {
|
|
95
|
+
const o = this.opts, at = o.at || [0, 0, 0];
|
|
96
|
+
const pts = (o.points || []).map((p) => [p[0] + at[0], p[1] + at[1]]);
|
|
97
|
+
pts.kinds = o.kinds || pts.map(() => 'c');
|
|
98
|
+
return { poly: pts, z: (o.z ?? at[2]) || 0, h: o.h ?? 10, c: o.chamfer || 0 };
|
|
99
|
+
}
|
|
100
|
+
lbox() {
|
|
101
|
+
const { poly, z, h } = this.plan();
|
|
102
|
+
const b = box3.empty();
|
|
103
|
+
for (const p of poly) { box3.addPt(b, p[0], p[1], z); box3.addPt(b, p[0], p[1], z + h); }
|
|
104
|
+
return b;
|
|
105
|
+
}
|
|
106
|
+
aabb(frame) {
|
|
107
|
+
if (!frame.rot) return super.aabb(frame);
|
|
108
|
+
const { poly, z, h } = this.plan(), b = box3.empty();
|
|
109
|
+
for (const p of poly) { const w = applyFrame(frame, p[0], p[1], z); box3.addPt(b, w[0], w[1], w[2]); box3.addPt(b, w[0], w[1], w[2] + h); }
|
|
110
|
+
return b;
|
|
111
|
+
}
|
|
112
|
+
/* inner top outline for the chamfer (world plan). Box overrides with a rounded inset. */
|
|
113
|
+
insetPlan(W, c) { return insetConvex(W, c); }
|
|
114
|
+
draw(d) {
|
|
115
|
+
const { poly, z, h, c: c0 } = this.plan();
|
|
116
|
+
if (poly.length < 3) return;
|
|
117
|
+
const T = tones(this.opts);
|
|
118
|
+
const W = poly.map((p) => { const w = d.w(p[0], p[1], z); return [w[0], w[1]]; });
|
|
119
|
+
W.kinds = poly.kinds;
|
|
120
|
+
const Z0 = d.w(0, 0, z)[2], Z1 = Z0 + h;
|
|
121
|
+
const convex = isConvex(W);
|
|
122
|
+
const c = convex ? Math.min(c0, h) : 0;
|
|
123
|
+
const Zs = Z1 - c, N = W.length, ccw = signedArea(W) > 0;
|
|
124
|
+
const kinds = W.kinds || [];
|
|
125
|
+
const edges = [];
|
|
126
|
+
for (let i = 0; i < N; i++) {
|
|
127
|
+
const a = W[i], b = W[(i + 1) % N];
|
|
128
|
+
const dx = b[0] - a[0], dy = b[1] - a[1];
|
|
129
|
+
const nx = ccw ? dy : -dy, ny = ccw ? -dx : dx;
|
|
130
|
+
edges.push({ i, visible: nx + ny > EPS * Math.hypot(dx, dy) * 10, right: nx > ny });
|
|
131
|
+
}
|
|
132
|
+
const P = (q, zz) => d.P(W[q][0], W[q][1], zz);
|
|
133
|
+
if (h > EPS) {
|
|
134
|
+
if (convex) {
|
|
135
|
+
let start = -1;
|
|
136
|
+
for (let i = 0; i < N; i++) if (edges[i].visible && !edges[(i + N - 1) % N].visible) { start = i; break; }
|
|
137
|
+
if (start >= 0) {
|
|
138
|
+
const run = [];
|
|
139
|
+
for (let j = 0; j < N; j++) { const e = edges[(start + j) % N]; if (e.visible) run.push(e); else break; }
|
|
140
|
+
const groups = []; let cur = null;
|
|
141
|
+
for (const e of run) { if (!cur || cur.right !== e.right) groups.push((cur = { right: e.right, edges: [] })); cur.edges.push(e.i); }
|
|
142
|
+
for (const g of groups) {
|
|
143
|
+
const idx = g.edges.concat([(g.edges[g.edges.length - 1] + 1) % N]);
|
|
144
|
+
const top = idx.map((q) => P(q, Zs)), bot = idx.map((q) => P(q, Z0)).reverse();
|
|
145
|
+
d.fill(d.d(top.concat(bot), true), g.right ? T.right : T.left);
|
|
146
|
+
}
|
|
147
|
+
const chain = run.map((e) => e.i); chain.push((chain[chain.length - 1] + 1) % N);
|
|
148
|
+
d.line(d.d(chain.map((q) => P(q, Z0))), 'ln');
|
|
149
|
+
chain.forEach((q, ci) => {
|
|
150
|
+
const end = ci === 0 || ci === chain.length - 1, k = kinds[q];
|
|
151
|
+
if (end || k === 'c') d.line(d.d([P(q, Z0), P(q, Zs)]), 'ln');
|
|
152
|
+
else if (k === 't') d.line(d.d([P(q, Z0), P(q, Zs)]), 'ln-soft');
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
const walls = edges.filter((e) => e.visible).map((e) => {
|
|
157
|
+
const a = W[e.i], b = W[(e.i + 1) % N];
|
|
158
|
+
return { e, key: a[0] + a[1] + b[0] + b[1] };
|
|
159
|
+
}).sort((p, q) => p.key - q.key);
|
|
160
|
+
for (const { e } of walls) {
|
|
161
|
+
const i = e.i, j = (i + 1) % N;
|
|
162
|
+
d.fill(d.d([P(i, Z1), P(j, Z1), P(j, Z0), P(i, Z0)], true), e.right ? T.right : T.left);
|
|
163
|
+
d.line(d.d([P(i, Z0), P(j, Z0)]), 'ln');
|
|
164
|
+
const prevVis = edges[(i + N - 1) % N].visible, nextVis = edges[j].visible;
|
|
165
|
+
if (!prevVis || kinds[i] === 'c') d.line(d.d([P(i, Z0), P(i, Z1)]), 'ln');
|
|
166
|
+
if (!nextVis || kinds[j] === 'c') d.line(d.d([P(j, Z0), P(j, Z1)]), 'ln');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
let topPoly = W;
|
|
171
|
+
if (c > EPS) {
|
|
172
|
+
const inner = this.insetPlan(W, c);
|
|
173
|
+
const outerD = d.d(W.map((p) => d.P(p[0], p[1], Zs)), true);
|
|
174
|
+
const innerD = d.d(inner.map((p) => d.P(p[0], p[1], Z1)), true);
|
|
175
|
+
d.fill(outerD + innerD, T.bevel, ' fill-rule="evenodd"');
|
|
176
|
+
d.line(outerD, 'ln');
|
|
177
|
+
topPoly = inner;
|
|
178
|
+
}
|
|
179
|
+
if (this.opts.top !== false) d.both(d.d(topPoly.map((p) => d.P(p[0], p[1], Z1)), true), T.top, 'ln');
|
|
180
|
+
this.drawFaces(d);
|
|
181
|
+
}
|
|
182
|
+
drawFaces(_d) {}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/* ───────────────────────── Box ───────────────────────── */
|
|
186
|
+
export class Box extends Prism {
|
|
187
|
+
get kind() { return 'box'; }
|
|
188
|
+
dims() {
|
|
189
|
+
const o = this.opts, s = o.size || [o.w ?? 20, o.d ?? 20, o.h ?? 20];
|
|
190
|
+
let [x, y, z] = o.at || [0, 0, 0];
|
|
191
|
+
if (o.center) { x = o.center[0] - s[0] / 2; y = o.center[1] - s[1] / 2; z = o.center[2] || 0; }
|
|
192
|
+
if (o.z !== undefined) z = o.z;
|
|
193
|
+
return { x, y, z, w: s[0], dd: s[1], h: s[2] };
|
|
194
|
+
}
|
|
195
|
+
plan() {
|
|
196
|
+
const o = this.opts, { x, y, z, w, dd, h } = this.dims();
|
|
197
|
+
let poly = rrect(x, y, w, dd, o.r || 0, o.n);
|
|
198
|
+
if (o.rot) poly = rotatePts(poly, x + w / 2, y + dd / 2, o.rot);
|
|
199
|
+
return { poly, z, h, c: o.chamfer || 0 };
|
|
200
|
+
}
|
|
201
|
+
insetPlan(W, c) {
|
|
202
|
+
const o = this.opts;
|
|
203
|
+
if (!o.r || o.rot) return insetConvex(W, c);
|
|
204
|
+
const { x, y, w, dd } = this.dims();
|
|
205
|
+
const f = this.frame;
|
|
206
|
+
const inner = rrect(x + c, y + c, w - 2 * c, dd - 2 * c, Math.max((o.r || 0) - c, 0.6), o.n);
|
|
207
|
+
return inner.map((p) => { const q = applyFrame(f, p[0], p[1], 0); return [q[0], q[1]]; });
|
|
208
|
+
}
|
|
209
|
+
/* face drawers in world space; available to callers via box.face(d, 'left') */
|
|
210
|
+
face(d, which) {
|
|
211
|
+
const { x, y, z, w, dd, h } = this.dims(), f = this.frame, o = this.opts;
|
|
212
|
+
const rot = o.rot || 0;
|
|
213
|
+
const L = (px, py, pz) => {
|
|
214
|
+
let X = px, Y = py;
|
|
215
|
+
if (rot) { const cx = x + w / 2, cy = y + dd / 2, c = Math.cos(rot * DEG), s = Math.sin(rot * DEG); X = cx + (px - cx) * c - (py - cy) * s; Y = cy + (px - cx) * s + (py - cy) * c; }
|
|
216
|
+
return applyFrame(f, X, Y, pz);
|
|
217
|
+
};
|
|
218
|
+
const R = (v) => { let r = v; if (rot) { const c = Math.cos(rot * DEG), s = Math.sin(rot * DEG); r = [v[0] * c - v[1] * s, v[0] * s + v[1] * c, v[2]]; } return frameVec(f, r); };
|
|
219
|
+
const top = z + h;
|
|
220
|
+
if (which === 'left') return new Face(d, L(x, y + dd, top), R([1, 0, 0]), R([0, 0, -1]), w, h);
|
|
221
|
+
if (which === 'right') return new Face(d, L(x + w, y + dd, top), R([0, -1, 0]), R([0, 0, -1]), dd, h);
|
|
222
|
+
if (which === 'top') return new Face(d, L(x, y, top), R([1, 0, 0]), R([0, 1, 0]), w, dd);
|
|
223
|
+
if (which === 'back-left') return new Face(d, L(x + w, y, top), R([-1, 0, 0]), R([0, 0, -1]), w, h);
|
|
224
|
+
if (which === 'back-right') return new Face(d, L(x, y, top), R([0, 1, 0]), R([0, 0, -1]), dd, h);
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
drawFaces(d) {
|
|
228
|
+
const o = this.opts;
|
|
229
|
+
for (const k of ['left', 'right', 'top']) if (typeof o[k] === 'function') o[k](this.face(d, k), this);
|
|
230
|
+
if (typeof o.faces === 'function') o.faces((k) => this.face(d, k), this);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* ───────────────────────── Cylinder / cone / frustum ───────────────────────── */
|
|
235
|
+
export class Cylinder extends Node {
|
|
236
|
+
get kind() { return 'cylinder'; }
|
|
237
|
+
g() {
|
|
238
|
+
const o = this.opts, at = o.at || [0, 0, 0];
|
|
239
|
+
const r = o.r ?? 10, rt = o.rTop ?? r;
|
|
240
|
+
return { cx: at[0], cy: at[1], z: (o.z ?? at[2]) || 0, r, rt, h: o.h ?? 10, c: o.chamfer || 0 };
|
|
241
|
+
}
|
|
242
|
+
lbox() { const { cx, cy, z, r, rt, h } = this.g(), R = Math.max(r, rt); return [cx - R, cy - R, z, cx + R, cy + R, z + h]; }
|
|
243
|
+
draw(d) {
|
|
244
|
+
const { cx: lx, cy: ly, z, r, rt, h } = this.g();
|
|
245
|
+
const T = tones(this.opts), o = this.opts;
|
|
246
|
+
const [cx, cy, Z0] = d.w(lx, ly, z);
|
|
247
|
+
const c = Math.min(this.g().c, rt * 0.9, h);
|
|
248
|
+
const Z1 = Z0 + h, Zs = Z1 - c;
|
|
249
|
+
const S = d.view.S;
|
|
250
|
+
const q = h > EPS ? (-2 * S * (r - rt)) / (h * Math.SQRT2) : (r > rt ? -2 : 2);
|
|
251
|
+
let t1 = -45, t2 = 135, full = false;
|
|
252
|
+
if (q <= -1) full = true;
|
|
253
|
+
else if (q < 1) { const a = Math.asin(q) / DEG; t1 = a - 45; t2 = 135 - a; }
|
|
254
|
+
const rS = rt + (r - rt) * (c / (h || 1)); // radius at Zs on a frustum
|
|
255
|
+
const pt = (rr, zz, t) => d.hpt(cx, cy, zz, rr, t);
|
|
256
|
+
const M = (p) => 'M' + fmt(p[0]) + ' ' + fmt(p[1]);
|
|
257
|
+
const Lp = (p) => 'L' + fmt(p[0]) + ' ' + fmt(p[1]);
|
|
258
|
+
if (h > EPS && q < 1) {
|
|
259
|
+
if (full) {
|
|
260
|
+
/* flat cone/frustum seen from above: the whole flank shows; two tones split through the apex */
|
|
261
|
+
const top = rS > EPS ? null : d.P(cx, cy, Z1);
|
|
262
|
+
for (const [a, b, cls] of [[-135, 45, T.right], [45, 225, T.left]]) {
|
|
263
|
+
let s = M(pt(r, Z0, a)) + d.harc(cx, cy, Z0, r, a, b);
|
|
264
|
+
s += top ? Lp(top) + 'Z' : Lp(pt(rS, Zs, b)) + d.harc(cx, cy, Zs, rS, b, a) + 'Z';
|
|
265
|
+
d.fill(s, cls);
|
|
266
|
+
}
|
|
267
|
+
d.line(d.hcircle(cx, cy, Z0, r), 'ln');
|
|
268
|
+
if (top) { d.line(d.d([pt(r, Z0, -45), top]), 'ln-soft'); d.line(d.d([pt(r, Z0, 135), top]), 'ln-soft'); }
|
|
269
|
+
} else {
|
|
270
|
+
/* two tones split where the surface faces the viewer (45°) */
|
|
271
|
+
const parts = [[t1, Math.min(45, t2), T.right], [Math.max(45, t1), t2, T.left]];
|
|
272
|
+
for (const [a, b, cls] of parts) {
|
|
273
|
+
if (b - a < 0.01) continue;
|
|
274
|
+
let s = M(pt(r, Z0, a)) + d.harc(cx, cy, Z0, r, a, b);
|
|
275
|
+
if (rS > EPS) s += Lp(pt(rS, Zs, b)) + d.harc(cx, cy, Zs, rS, b, a) + 'Z'; else s += Lp(d.P(cx, cy, Z1)) + 'Z';
|
|
276
|
+
d.fill(s, cls);
|
|
277
|
+
}
|
|
278
|
+
d.line(M(pt(r, Z0, t1)) + d.harc(cx, cy, Z0, r, t1, t2), 'ln');
|
|
279
|
+
const apex = rS > EPS ? null : d.P(cx, cy, Z1);
|
|
280
|
+
d.line(d.d([pt(r, Z0, t1), apex || pt(rS, Zs, t1)]), 'ln');
|
|
281
|
+
d.line(d.d([pt(r, Z0, t2), apex || pt(rS, Zs, t2)]), 'ln');
|
|
282
|
+
/* horizontal bands and vertical ribs on the visible side */
|
|
283
|
+
if (o.bands) for (const bz of o.bands) {
|
|
284
|
+
const f = (bz - z) / h, rb = r + (rt - r) * f;
|
|
285
|
+
d.line(M(pt(rb, Z0 + bz - z, t1)) + d.harc(cx, cy, Z0 + bz - z, rb, t1, t2), o.bandClass || 'ln-soft');
|
|
286
|
+
}
|
|
287
|
+
if (o.ribs) {
|
|
288
|
+
const n = o.ribs, a0 = o.ribStart || 0;
|
|
289
|
+
for (let i = 0; i < n; i++) {
|
|
290
|
+
let a = a0 + (360 * i) / n; a = ((a - t1) % 360 + 360) % 360 + t1;
|
|
291
|
+
if (a > t1 + 0.5 && a < t2 - 0.5) d.line(d.d([pt(r, Z0 + (o.ribFrom ?? 0), a), pt(r + (rt - r) * ((o.ribTo ?? h) / h), Z0 + (o.ribTo ?? h), a)]), o.ribClass || 'ln-soft');
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (rt > EPS) {
|
|
297
|
+
if (c > EPS) {
|
|
298
|
+
d.fill(d.hcircle(cx, cy, Zs, rS) + d.hcircle(cx, cy, Z1, rt - c), T.bevel, ' fill-rule="evenodd"');
|
|
299
|
+
d.line(d.hcircle(cx, cy, Zs, rS), 'ln');
|
|
300
|
+
if (o.top !== false) d.both(d.hcircle(cx, cy, Z1, rt - c), T.top, 'ln');
|
|
301
|
+
} else if (o.top !== false) d.both(d.hcircle(cx, cy, Z1, rt), T.top, 'ln');
|
|
302
|
+
if (o.rings) for (const rr of o.rings) d.line(d.hcircle(cx, cy, Z1, rr), 'ln-soft');
|
|
303
|
+
}
|
|
304
|
+
if (typeof o.topFace === 'function') {
|
|
305
|
+
const f = new Face(d, [cx, cy, Z1], [1, 0, 0], [0, 1, 0], rt * 2, rt * 2);
|
|
306
|
+
o.topFace(f, this);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* ───────────────────────── Tube segment (ring wall) ─────────────────────────
|
|
312
|
+
One angular piece of a hollow cylinder. `tube()` splits a ring into segments so that
|
|
313
|
+
objects inside the courtyard sort correctly between its far and near halves. */
|
|
314
|
+
function rangeIntersect(a0, a1, v0, v1) {
|
|
315
|
+
/* intersect [a0,a1] with the periodic interval [v0,v1] (degrees); returns [start, end, period shift] */
|
|
316
|
+
const out = [];
|
|
317
|
+
for (const k of [-360, 0, 360]) {
|
|
318
|
+
const s = Math.max(a0, v0 + k), e = Math.min(a1, v1 + k);
|
|
319
|
+
if (e - s > 0.01) out.push([s, e, k]);
|
|
320
|
+
}
|
|
321
|
+
return out;
|
|
322
|
+
}
|
|
323
|
+
export class TubeSegment extends Node {
|
|
324
|
+
get kind() { return 'tube-segment'; }
|
|
325
|
+
g() {
|
|
326
|
+
const o = this.opts, at = o.at || [0, 0, 0];
|
|
327
|
+
return { cx: at[0], cy: at[1], z: at[2] || 0, r: o.r, ri: o.ri, h: o.h, a0: o.a0, a1: o.a1 };
|
|
328
|
+
}
|
|
329
|
+
lbox() {
|
|
330
|
+
const { cx, cy, z, r, ri, h, a0, a1 } = this.g();
|
|
331
|
+
const b = box3.empty();
|
|
332
|
+
const angs = [a0, a1];
|
|
333
|
+
for (let a = Math.ceil(a0 / 90) * 90; a <= a1; a += 90) angs.push(a);
|
|
334
|
+
for (const a of angs) for (const rr of [r, ri]) box3.addPt(b, cx + rr * Math.cos(a * DEG), cy + rr * Math.sin(a * DEG), z);
|
|
335
|
+
b[5] = z + h;
|
|
336
|
+
return b;
|
|
337
|
+
}
|
|
338
|
+
draw(d) {
|
|
339
|
+
const { cx: lx, cy: ly, z, r, ri, h, a0, a1 } = this.g(), o = this.opts, T = tones(o);
|
|
340
|
+
const [cx, cy, Z0] = d.w(lx, ly, z), Z1 = Z0 + h;
|
|
341
|
+
const pt = (rr, zz, t) => d.hpt(cx, cy, zz, rr, t);
|
|
342
|
+
const M = (p) => 'M' + fmt(p[0]) + ' ' + fmt(p[1]);
|
|
343
|
+
const Lp = (p) => 'L' + fmt(p[0]) + ' ' + fmt(p[1]);
|
|
344
|
+
const wall = (rr, s, e, cls) => M(pt(rr, Z0, s)) + d.harc(cx, cy, Z0, rr, s, e) + Lp(pt(rr, Z1, e)) + d.harc(cx, cy, Z1, rr, e, s) + 'Z';
|
|
345
|
+
/* inner wall: the far half faces the viewer */
|
|
346
|
+
if (ri > EPS) for (const [s, e, k] of rangeIntersect(a0, a1, 135, 315)) {
|
|
347
|
+
for (const [s2, e2, cls] of [[s, Math.min(e, 225 + k), T.right], [Math.max(s, 225 + k), e, T.left]]) {
|
|
348
|
+
if (e2 - s2 > 0.01) d.fill(wall(ri, s2, e2), cls);
|
|
349
|
+
}
|
|
350
|
+
d.line(M(pt(ri, Z0, s)) + d.harc(cx, cy, Z0, ri, s, e), 'ln-soft');
|
|
351
|
+
for (const sil of [135, 315, 135 + 360, 315 - 360]) if (sil > a0 + 0.01 && sil < a1 - 0.01) d.line(d.d([pt(ri, Z0, sil), pt(ri, Z1, sil)]), 'ln');
|
|
352
|
+
if (o.innerBands) for (const bz of o.innerBands) d.line(M(pt(ri, Z0 + bz, s)) + d.harc(cx, cy, Z0 + bz, ri, s, e), 'ln-soft');
|
|
353
|
+
}
|
|
354
|
+
/* outer wall: the near half */
|
|
355
|
+
for (const [s, e, k] of rangeIntersect(a0, a1, -45, 135)) {
|
|
356
|
+
for (const [s2, e2, cls] of [[s, Math.min(e, 45 + k), T.right], [Math.max(s, 45 + k), e, T.left]]) {
|
|
357
|
+
if (e2 - s2 > 0.01) d.fill(wall(r, s2, e2), cls);
|
|
358
|
+
}
|
|
359
|
+
d.line(M(pt(r, Z0, s)) + d.harc(cx, cy, Z0, r, s, e), 'ln');
|
|
360
|
+
for (const sil of [-45, 135, 315, 495]) if (sil > a0 + 0.01 && sil < a1 - 0.01) d.line(d.d([pt(r, Z0, sil), pt(r, Z1, sil)]), 'ln');
|
|
361
|
+
if (o.bands) for (const bz of o.bands) d.line(M(pt(r, Z0 + bz, s)) + d.harc(cx, cy, Z0 + bz, r, s, e), o.bandClass || 'ln-soft');
|
|
362
|
+
if (o.ribs) {
|
|
363
|
+
const step = 360 / o.ribs;
|
|
364
|
+
for (let a = Math.ceil(s / step) * step; a < e; a += step) if (a > s + 0.3) d.line(d.d([pt(r, Z0 + (o.ribFrom ?? 0), a), pt(r, Z0 + (o.ribTo ?? h), a)]), o.ribClass || 'ln-soft');
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/* top annulus piece: fill without radial seams, arcs stroked */
|
|
368
|
+
const top = M(pt(r, Z1, a0)) + d.harc(cx, cy, Z1, r, a0, a1) + (ri > EPS ? Lp(pt(ri, Z1, a1)) + d.harc(cx, cy, Z1, ri, a1, a0) : Lp(d.P(cx, cy, Z1))) + 'Z';
|
|
369
|
+
d.fill(top, T.top);
|
|
370
|
+
d.line(M(pt(r, Z1, a0)) + d.harc(cx, cy, Z1, r, a0, a1), 'ln');
|
|
371
|
+
if (ri > EPS) d.line(M(pt(ri, Z1, a0)) + d.harc(cx, cy, Z1, ri, a0, a1), 'ln');
|
|
372
|
+
if (o.topRings) for (const rr of o.topRings) d.line(M(pt(rr, Z1, a0)) + d.harc(cx, cy, Z1, rr, a0, a1), 'ln-soft');
|
|
373
|
+
if (o.topRadials) {
|
|
374
|
+
const step = 360 / o.topRadials;
|
|
375
|
+
for (let a = Math.ceil(a0 / step) * step; a < a1; a += step) d.line(d.d([pt(ri, Z1, a), pt(r, Z1, a)]), 'ln-soft');
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/* ───────────────────────── Sphere ───────────────────────── */
|
|
381
|
+
export class Sphere extends Node {
|
|
382
|
+
get kind() { return 'sphere'; }
|
|
383
|
+
lbox() { const o = this.opts, [x, y, z] = o.at || [0, 0, 0], r = o.r ?? 10; return [x - r, y - r, z - r, x + r, y + r, z + r]; }
|
|
384
|
+
draw(d) {
|
|
385
|
+
const o = this.opts, [x, y, z] = o.at || [0, 0, 0], r = o.r ?? 10, T = tones(o);
|
|
386
|
+
const w = d.w(x, y, z), c = d.P(w[0], w[1], w[2]);
|
|
387
|
+
const { C, S } = d.view, rx = r * Math.SQRT2 * C, ry = r * Math.sqrt(2 * S * S + 1);
|
|
388
|
+
d.P(w[0], w[1], w[2] + r); d.P(w[0] + r, w[1] - r, w[2]); d.P(w[0] - r, w[1] + r, w[2]); d.P(w[0], w[1], w[2] - r);
|
|
389
|
+
const circ = (k, dx = 0, dy = 0) => {
|
|
390
|
+
const a = rx * k, b = ry * k, X = c[0] + dx, Y = c[1] + dy;
|
|
391
|
+
return 'M' + fmt(X - a) + ' ' + fmt(Y) + 'A' + fmt(a) + ' ' + fmt(b) + ' 0 1 0 ' + fmt(X + a) + ' ' + fmt(Y) + 'A' + fmt(a) + ' ' + fmt(b) + ' 0 1 0 ' + fmt(X - a) + ' ' + fmt(Y) + 'Z';
|
|
392
|
+
};
|
|
393
|
+
d.fill(circ(1), T.right);
|
|
394
|
+
/* soft lit cap toward the top-left */
|
|
395
|
+
d.fill(circ(0.78, -rx * 0.14, -ry * 0.16), T.top, ' opacity=".7"');
|
|
396
|
+
d.line(circ(1), 'ln');
|
|
397
|
+
if (o.equator) d.line('M' + fmt(c[0] - rx) + ' ' + fmt(c[1]) + d.harc(w[0], w[1], w[2], r, 135, 315), 'ln-soft');
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/* ───────────────────────── Convex polyhedron ───────────────────────── */
|
|
402
|
+
export class Hull extends Node {
|
|
403
|
+
get kind() { return 'hull'; }
|
|
404
|
+
verts() { const o = this.opts, at = o.at || [0, 0, 0]; return (o.vertices || []).map((v) => [v[0] + at[0], v[1] + at[1], v[2] + at[2]]); }
|
|
405
|
+
lbox() { const b = box3.empty(); for (const v of this.verts()) box3.addPt(b, v[0], v[1], v[2]); return b; }
|
|
406
|
+
aabb(frame) {
|
|
407
|
+
const b = box3.empty();
|
|
408
|
+
for (const v of this.verts()) { const w = applyFrame(frame, v[0], v[1], v[2]); box3.addPt(b, w[0], w[1], w[2]); }
|
|
409
|
+
return b;
|
|
410
|
+
}
|
|
411
|
+
draw(d) {
|
|
412
|
+
const o = this.opts, T = tones(o);
|
|
413
|
+
const V = this.verts().map((v) => d.w(v[0], v[1], v[2]));
|
|
414
|
+
const cen = V.reduce((a, v) => v3.add(a, v), [0, 0, 0]).map((s) => s / V.length);
|
|
415
|
+
const view = VIEW_N(d.view.S);
|
|
416
|
+
const all = (o.faces || []).map((f, fi) => {
|
|
417
|
+
const p = f.map((i) => V[i]);
|
|
418
|
+
let n = [0, 0, 0];
|
|
419
|
+
for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length]; n = v3.add(n, [(a[1] - b[1]) * (a[2] + b[2]), (a[2] - b[2]) * (a[0] + b[0]), (a[0] - b[0]) * (a[1] + b[1])]); }
|
|
420
|
+
const fc = p.reduce((a, v) => v3.add(a, v), [0, 0, 0]).map((s) => s / p.length);
|
|
421
|
+
if (v3.dot(n, v3.sub(fc, cen)) < 0) n = v3.mul(n, -1);
|
|
422
|
+
n = v3.norm(n);
|
|
423
|
+
const visible = v3.dot(n, view) > 1e-6;
|
|
424
|
+
const cls = n[2] >= Math.max(n[0], n[1]) - 1e-6 ? T.top : n[1] > n[0] ? T.left : T.right;
|
|
425
|
+
return { f, p, cls, fi, n, visible };
|
|
426
|
+
});
|
|
427
|
+
const faces = all.filter((f) => f.visible);
|
|
428
|
+
for (const f of faces) d.fill(d.dw(f.p, true), (o.faceTones && o.faceTones[f.fi]) || f.cls);
|
|
429
|
+
/* outlines: silhouette edges and sharp creases only, so rounded profiles read as smooth */
|
|
430
|
+
const edges = new Map();
|
|
431
|
+
all.forEach((F) => F.f.forEach((a, i) => {
|
|
432
|
+
const b = F.f[(i + 1) % F.f.length], k = a < b ? a + ',' + b : b + ',' + a;
|
|
433
|
+
const e = edges.get(k); if (e) e.push(F); else edges.set(k, [F]);
|
|
434
|
+
}));
|
|
435
|
+
const crease = Math.cos(((o.crease ?? 28) * Math.PI) / 180);
|
|
436
|
+
let ln = '';
|
|
437
|
+
for (const [k, fs] of edges) {
|
|
438
|
+
const vis = fs.filter((F) => F.visible);
|
|
439
|
+
if (!vis.length) continue;
|
|
440
|
+
const sharp = fs.length < 2 || vis.length === 1 || v3.dot(fs[0].n, fs[1].n) < crease;
|
|
441
|
+
if (!sharp) continue;
|
|
442
|
+
const [a, b] = k.split(',').map(Number);
|
|
443
|
+
ln += d.dw([V[a], V[b]]);
|
|
444
|
+
}
|
|
445
|
+
d.line(ln, 'ln');
|
|
446
|
+
if (typeof o.onFaces === 'function') o.onFaces(d, faces);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/* A slab with a rounded-rectangle profile standing on a plane: phones, screens, doors, signs.
|
|
451
|
+
plane 'left': the front faces +y, width runs +x, height +z, thickness goes −y.
|
|
452
|
+
plane 'right': the front faces +x, width runs −y, height +z, thickness goes −x.
|
|
453
|
+
plane 'top': lying flat, front faces +z, width +x, depth +y, thickness goes −z.
|
|
454
|
+
at = bottom-left corner of the front face (for 'top': the back-left corner of the top face). */
|
|
455
|
+
const PANEL_AXES = {
|
|
456
|
+
left: { U: [1, 0, 0], V: [0, 0, -1], N: [0, 1, 0] },
|
|
457
|
+
right: { U: [0, -1, 0], V: [0, 0, -1], N: [1, 0, 0] },
|
|
458
|
+
top: { U: [1, 0, 0], V: [0, 1, 0], N: [0, 0, 1] },
|
|
459
|
+
};
|
|
460
|
+
export class Panel extends Hull {
|
|
461
|
+
get kind() { return 'panel'; }
|
|
462
|
+
geo() {
|
|
463
|
+
const o = this.opts, ax = PANEL_AXES[o.plane || 'left'], at = o.at || [0, 0, 0];
|
|
464
|
+
const w = o.w ?? 20, h = o.h ?? 30, t = o.t ?? 2;
|
|
465
|
+
const tl = o.plane === 'top' ? at : [at[0], at[1], at[2] + h];
|
|
466
|
+
return { ax, w, h, t, tl };
|
|
467
|
+
}
|
|
468
|
+
verts() {
|
|
469
|
+
const o = this.opts, { ax, w, h, t, tl } = this.geo();
|
|
470
|
+
const prof = rrect(0, 0, w, h, o.r ?? 2, o.n || 4);
|
|
471
|
+
const P = (u, v, k) => [tl[0] + ax.U[0] * u + ax.V[0] * v - ax.N[0] * k, tl[1] + ax.U[1] * u + ax.V[1] * v - ax.N[1] * k, tl[2] + ax.U[2] * u + ax.V[2] * v - ax.N[2] * k];
|
|
472
|
+
return prof.map((p) => P(p[0], p[1], 0)).concat(prof.map((p) => P(p[0], p[1], t)));
|
|
473
|
+
}
|
|
474
|
+
draw(d) {
|
|
475
|
+
const o = this.opts, n = rrect(0, 0, this.geo().w, this.geo().h, o.r ?? 2, o.n || 4).length;
|
|
476
|
+
const faces = [Array.from({ length: n }, (_, i) => i), Array.from({ length: n }, (_, i) => 2 * n - 1 - i)];
|
|
477
|
+
for (let i = 0; i < n; i++) faces.push([i, (i + 1) % n, n + ((i + 1) % n), n + i]);
|
|
478
|
+
this.opts.faces = faces;
|
|
479
|
+
super.draw(d);
|
|
480
|
+
if (typeof o.front === 'function') o.front(this.face(d), this);
|
|
481
|
+
}
|
|
482
|
+
/* Face drawer on the front, (0,0) at the top-left corner */
|
|
483
|
+
face(d) {
|
|
484
|
+
const { ax, w, h, tl } = this.geo();
|
|
485
|
+
return new Face(d, d.w(...tl), frameVec(d.frame, ax.U), frameVec(d.frame, ax.V), w, h);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/* Parallelepiped from an origin and three edge vectors (tilted slabs: laptop lids, signs, ramps). */
|
|
490
|
+
export function slabVertices(origin, u, v, w) {
|
|
491
|
+
const o = origin, A = (...vs) => vs.reduce((s, x) => v3.add(s, x), o);
|
|
492
|
+
return [A(), A(u), A(u, v), A(v), A(w), A(u, w), A(u, v, w), A(v, w)];
|
|
493
|
+
}
|
|
494
|
+
export const SLAB_FACES = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [3, 2, 6, 7], [0, 3, 7, 4], [1, 2, 6, 5]];
|
|
495
|
+
|
|
496
|
+
/* ───────────────────────── Flat shapes on a horizontal plane ───────────────────────── */
|
|
497
|
+
export class Flat extends Node {
|
|
498
|
+
get kind() { return 'flat'; }
|
|
499
|
+
g() {
|
|
500
|
+
const o = this.opts, at = o.at || [0, 0, 0];
|
|
501
|
+
return { x: at[0], y: at[1], z: (o.z ?? at[2]) || 0 };
|
|
502
|
+
}
|
|
503
|
+
outline() {
|
|
504
|
+
const o = this.opts, { x, y } = this.g();
|
|
505
|
+
if (o.shape === 'poly') return (o.points || []).map((p) => [p[0] + x, p[1] + y]);
|
|
506
|
+
const s = o.size || [o.w ?? 20, o.d ?? 20];
|
|
507
|
+
let pts = rrect(x, y, s[0], s[1], o.r || 0, o.n);
|
|
508
|
+
if (o.rot) pts = rotatePts(pts, x + s[0] / 2, y + s[1] / 2, o.rot);
|
|
509
|
+
return pts;
|
|
510
|
+
}
|
|
511
|
+
lbox() {
|
|
512
|
+
const o = this.opts, { x, y, z } = this.g();
|
|
513
|
+
if (o.shape === 'circle' || o.shape === 'ring') { const r = o.r ?? 10; return [x - r, y - r, z, x + r, y + r, z]; }
|
|
514
|
+
const b = box3.empty(); for (const p of this.outline()) box3.addPt(b, p[0], p[1], z); return b;
|
|
515
|
+
}
|
|
516
|
+
draw(d) {
|
|
517
|
+
const o = this.opts, { x, y, z } = this.g();
|
|
518
|
+
const fill = o.fill === undefined ? null : o.fill, line = o.line === undefined ? 'ln-soft' : o.line;
|
|
519
|
+
const dash = o.dash ? ' stroke-dasharray="' + dashArray(o.dash) + '"' : undefined;
|
|
520
|
+
let path;
|
|
521
|
+
if (o.shape === 'circle' || o.shape === 'ring') {
|
|
522
|
+
const [cx, cy, Z] = d.w(x, y, z);
|
|
523
|
+
path = d.hcircle(cx, cy, Z, o.r ?? 10);
|
|
524
|
+
if (o.shape === 'ring') {
|
|
525
|
+
const inner = d.hcircle(cx, cy, Z, o.ri ?? (o.r ?? 10) / 2);
|
|
526
|
+
if (fill) d.fill(path + inner, fill, ' fill-rule="evenodd"');
|
|
527
|
+
if (line) { d.line(path, line, dash); d.line(inner, line, dash); }
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
} else path = d.dl(this.outline().map((p) => [p[0], p[1], z]), true);
|
|
531
|
+
if (fill) d.fill(path, fill);
|
|
532
|
+
if (line) d.line(path, line, dash);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/* ───────────────────────── Lines, arrows, helices ───────────────────────── */
|
|
537
|
+
function arrowHead(d, a, b, size, cls) {
|
|
538
|
+
/* triangle lying in the plane that contains the segment and is most visible */
|
|
539
|
+
let dir = v3.sub(b, a); const L = v3.len(dir); if (L < EPS) return;
|
|
540
|
+
dir = v3.mul(dir, 1 / L);
|
|
541
|
+
let perp = Math.abs(dir[2]) > 0.9 ? v3.norm([1, -1, 0]) : v3.norm([-dir[1], dir[0], 0]);
|
|
542
|
+
const base = v3.sub(b, v3.mul(dir, size));
|
|
543
|
+
const p1 = v3.add(base, v3.mul(perp, size * 0.45)), p2 = v3.sub(base, v3.mul(perp, size * 0.45));
|
|
544
|
+
d.fill(d.dw([b, p1, p2], true), cls);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export class Line extends Node {
|
|
548
|
+
get kind() { return 'line'; }
|
|
549
|
+
pts() {
|
|
550
|
+
const o = this.opts, at = o.at || [0, 0, 0];
|
|
551
|
+
let pts = o.path ? o.path.points : o.points || [];
|
|
552
|
+
return pts.map((p) => [p[0] + at[0], p[1] + at[1], (p[2] || 0) + at[2]]);
|
|
553
|
+
}
|
|
554
|
+
lbox() { const b = box3.empty(); for (const p of this.pts()) box3.addPt(b, p[0], p[1], p[2]); return box3.valid(b) ? b : null; }
|
|
555
|
+
aabb(frame) { const b = box3.empty(); for (const p of this.pts()) { const w = applyFrame(frame, p[0], p[1], p[2]); box3.addPt(b, w[0], w[1], w[2]); } return box3.valid(b) ? b : null; }
|
|
556
|
+
draw(d) {
|
|
557
|
+
const o = this.opts;
|
|
558
|
+
let pts = this.pts();
|
|
559
|
+
if (pts.length < 2) return;
|
|
560
|
+
const cls = o.line || o.cls || 'ln';
|
|
561
|
+
/* drawing on: cut the polyline at the revealed length (dash tricks don't survive non-scaling strokes
|
|
562
|
+
or the flow dash); the line's own dash and flow wait until it is complete */
|
|
563
|
+
const revealing = o.reveal !== undefined && o.reveal < 1;
|
|
564
|
+
if (revealing) {
|
|
565
|
+
pts = cutPolyline(pts, Math.max(0, o.reveal));
|
|
566
|
+
if (pts.length < 2) return;
|
|
567
|
+
}
|
|
568
|
+
pts = pts.map((p) => d.w(p[0], p[1], p[2]));
|
|
569
|
+
let extra = '';
|
|
570
|
+
if (o.dash && !revealing) extra += ' stroke-dasharray="' + dashArray(o.dash) + '"';
|
|
571
|
+
if (o.flow && !revealing) extra += ' data-flow="' + num(o.flow) + '"';
|
|
572
|
+
if (o.width) extra += ' stroke-width="' + num(o.width) + '"';
|
|
573
|
+
d.line(d.dw(pts, o.closed && !revealing), cls, extra || undefined);
|
|
574
|
+
if (revealing) return;
|
|
575
|
+
const as = o.arrowSize || 5;
|
|
576
|
+
if (o.arrow === 'end' || o.arrow === 'both' || o.arrow === true) arrowHead(d, pts[pts.length - 2], pts[pts.length - 1], as, o.arrowClass || 'f-ink');
|
|
577
|
+
if (o.arrow === 'start' || o.arrow === 'both') arrowHead(d, pts[1], pts[0], as, o.arrowClass || 'f-ink');
|
|
578
|
+
if (o.dots) for (const p of [pts[0], pts[pts.length - 1]]) d.both(d.hcircle(p[0], p[1], p[2], o.dots), 'f-ink', null);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/* The first fraction `f` (0..1) of a polyline, by length. */
|
|
583
|
+
function cutPolyline(pts, f) {
|
|
584
|
+
const seg = [];
|
|
585
|
+
let total = 0;
|
|
586
|
+
for (let i = 1; i < pts.length; i++) { const l = Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1], pts[i][2] - pts[i - 1][2]); seg.push(l); total += l; }
|
|
587
|
+
let left = total * f;
|
|
588
|
+
if (left <= EPS) return [];
|
|
589
|
+
const out = [pts[0]];
|
|
590
|
+
for (let i = 1; i < pts.length; i++) {
|
|
591
|
+
if (left >= seg[i - 1]) { out.push(pts[i]); left -= seg[i - 1]; continue; }
|
|
592
|
+
const k = left / (seg[i - 1] || 1), a = pts[i - 1], b = pts[i];
|
|
593
|
+
out.push([a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k]);
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
return out;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/* Orthogonal route on a horizontal plane with rounded corners. */
|
|
600
|
+
export function routePoints(from, to, opt = {}) {
|
|
601
|
+
const z = opt.z || 0, r = opt.radius ?? 6, mode = opt.route || 'auto';
|
|
602
|
+
const [x0, y0] = from, [x1, y1] = to;
|
|
603
|
+
let pts;
|
|
604
|
+
if (Array.isArray(mode)) pts = [[x0, y0], ...mode, [x1, y1]];
|
|
605
|
+
else if (mode === 'straight' || (Math.abs(x1 - x0) < EPS || Math.abs(y1 - y0) < EPS)) pts = [[x0, y0], [x1, y1]];
|
|
606
|
+
else {
|
|
607
|
+
const xf = mode === 'x' ? true : mode === 'y' ? false : mode === 'xyx' || mode === 'yxy' ? null : Math.abs(x1 - x0) >= Math.abs(y1 - y0);
|
|
608
|
+
if (mode === 'xyx') { const mx = (x0 + x1) / 2; pts = [[x0, y0], [mx, y0], [mx, y1], [x1, y1]]; }
|
|
609
|
+
else if (mode === 'yxy') { const my = (y0 + y1) / 2; pts = [[x0, y0], [x0, my], [x1, my], [x1, y1]]; }
|
|
610
|
+
else pts = xf ? [[x0, y0], [x1, y0], [x1, y1]] : [[x0, y0], [x0, y1], [x1, y1]];
|
|
611
|
+
}
|
|
612
|
+
return filletPolyline(pts.map((p) => [p[0], p[1], p[2] ?? z]), r);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/* Round the corners of a polyline with arcs of radius r (in 3D, works in any plane). */
|
|
616
|
+
export function filletPolyline(pts, r, closed = false) {
|
|
617
|
+
if (r <= 0 || pts.length < 3) return pts.map((p) => p.slice());
|
|
618
|
+
const out = [pts[0].slice()];
|
|
619
|
+
for (let i = 1; i < pts.length - 1; i++) {
|
|
620
|
+
const a = pts[i - 1], b = pts[i], c = pts[i + 1];
|
|
621
|
+
const u = v3.sub(a, b), w = v3.sub(c, b), lu = v3.len(u), lw = v3.len(w);
|
|
622
|
+
if (lu < EPS || lw < EPS) continue;
|
|
623
|
+
const un = v3.mul(u, 1 / lu), wn = v3.mul(w, 1 / lw);
|
|
624
|
+
const cos = clampN(v3.dot(un, wn), -1, 1), ang = Math.acos(cos);
|
|
625
|
+
if (ang > Math.PI - 1e-3) { out.push(b.slice()); continue; }
|
|
626
|
+
const t = Math.min(r / Math.tan(ang / 2), lu / 2, lw / 2);
|
|
627
|
+
const p0 = v3.add(b, v3.mul(un, t)), p1 = v3.add(b, v3.mul(wn, t));
|
|
628
|
+
const n = 8;
|
|
629
|
+
for (let k = 0; k <= n; k++) {
|
|
630
|
+
const s = k / n;
|
|
631
|
+
/* quadratic bezier approximates the fillet well enough at these sizes */
|
|
632
|
+
const q = v3.add(v3.add(v3.mul(p0, (1 - s) * (1 - s)), v3.mul(b, 2 * s * (1 - s))), v3.mul(p1, s * s));
|
|
633
|
+
out.push(q);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
out.push(pts[pts.length - 1].slice());
|
|
637
|
+
return out;
|
|
638
|
+
}
|
|
639
|
+
const clampN = (v, a, b) => Math.max(a, Math.min(b, v));
|
|
640
|
+
|
|
641
|
+
/* Flat strip along a polyline (roads, paths, belts). */
|
|
642
|
+
export class Ribbon extends Node {
|
|
643
|
+
get kind() { return 'ribbon'; }
|
|
644
|
+
pts() { const o = this.opts; return (o.path ? o.path.points : o.points || []).map((p) => [p[0], p[1], p[2] ?? o.z ?? 0]); }
|
|
645
|
+
outline() {
|
|
646
|
+
const o = this.opts, P = this.pts(), hw = (o.width ?? 10) / 2, L = [], R = [];
|
|
647
|
+
for (let i = 0; i < P.length; i++) {
|
|
648
|
+
const a = P[Math.max(0, i - 1)], b = P[Math.min(P.length - 1, i + 1)];
|
|
649
|
+
let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx /= l; dy /= l;
|
|
650
|
+
L.push([P[i][0] - dy * hw, P[i][1] + dx * hw, P[i][2]]);
|
|
651
|
+
R.push([P[i][0] + dy * hw, P[i][1] - dx * hw, P[i][2]]);
|
|
652
|
+
}
|
|
653
|
+
return { L, R };
|
|
654
|
+
}
|
|
655
|
+
lbox() { const b = box3.empty(), { L, R } = this.outline(); for (const p of L.concat(R)) box3.addPt(b, p[0], p[1], p[2]); return box3.valid(b) ? b : null; }
|
|
656
|
+
aabb(frame) { const b = box3.empty(), { L, R } = this.outline(); for (const p of L.concat(R)) { const w = applyFrame(frame, p[0], p[1], p[2]); box3.addPt(b, w[0], w[1], w[2]); } return box3.valid(b) ? b : null; }
|
|
657
|
+
draw(d) {
|
|
658
|
+
const o = this.opts, { L, R } = this.outline();
|
|
659
|
+
if (L.length < 2) return;
|
|
660
|
+
d.fill(d.dl(L.concat(R.slice().reverse()), true), o.fill || 'f-road');
|
|
661
|
+
if (o.line !== false) { d.line(d.dl(L), o.line || 'ln-soft'); d.line(d.dl(R), o.line || 'ln-soft'); }
|
|
662
|
+
if (o.center) d.line(d.dl(this.pts()), 'ln-soft', ' stroke-dasharray="' + (o.center === true ? '4 4' : dashArray(o.center)) + '"' + (o.flow ? ' data-flow="' + num(o.flow) + '"' : ''));
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
export class Helix extends Node {
|
|
667
|
+
get kind() { return 'helix'; }
|
|
668
|
+
lbox() { const o = this.opts, b = box3.empty(); for (const p of [o.a, o.b]) box3.addPt(b, p[0], p[1], p[2]); return [b[0] - o.r, b[1] - o.r, b[2] - o.r, b[3] + o.r, b[4] + o.r, b[5] + o.r]; }
|
|
669
|
+
draw(d) {
|
|
670
|
+
const o = this.opts, a = d.w(...o.a), b = d.w(...o.b);
|
|
671
|
+
let dd = v3.sub(b, a); const L = v3.len(dd); dd = v3.mul(dd, 1 / L);
|
|
672
|
+
let e1 = v3.norm([dd[1], -dd[0], 0]);
|
|
673
|
+
if (!isFinite(e1[0]) || v3.len([dd[1], -dd[0], 0]) < 1e-6) e1 = [1, 0, 0];
|
|
674
|
+
const e2 = v3.cross(dd, e1);
|
|
675
|
+
const n = o.n || Math.round(o.turns * 12), pts = [];
|
|
676
|
+
for (let i = 0; i <= n; i++) {
|
|
677
|
+
const t = i / n, th = o.turns * 2 * Math.PI * t;
|
|
678
|
+
pts.push(v3.add(v3.add(a, v3.mul(dd, L * t)), v3.add(v3.mul(e1, o.r * Math.cos(th)), v3.mul(e2, o.r * Math.sin(th)))));
|
|
679
|
+
}
|
|
680
|
+
d.line(d.dw(pts), o.line || 'ln');
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/* ───────────────────────── Text and 2D content on a plane ───────────────────────── */
|
|
685
|
+
const PLANES = {
|
|
686
|
+
top: [[1, 0, 0], [0, 1, 0]],
|
|
687
|
+
left: [[1, 0, 0], [0, 0, -1]], // the +y facing plane, reads left → right going down-right
|
|
688
|
+
right: [[0, -1, 0], [0, 0, -1]], // the +x facing plane, reads left → right going up-right
|
|
689
|
+
'top-y': [[0, -1, 0], [1, 0, 0]], // top plane, text running along −y (up-right)
|
|
690
|
+
};
|
|
691
|
+
export class Text extends Node {
|
|
692
|
+
get kind() { return 'text'; }
|
|
693
|
+
lbox() {
|
|
694
|
+
const o = this.opts, [x, y, z] = o.at || [0, 0, 0], size = o.size || 8;
|
|
695
|
+
const len = String(o.text ?? '').length * size * 0.62;
|
|
696
|
+
const pl = PLANES[o.plane || 'screen'];
|
|
697
|
+
if (!pl) return [x, y, z, x, y, z];
|
|
698
|
+
const [U] = pl, b = box3.empty();
|
|
699
|
+
const s = o.anchor === 'middle' ? -0.5 : o.anchor === 'end' ? -1 : 0;
|
|
700
|
+
box3.addPt(b, x + U[0] * len * s, y + U[1] * len * s, z);
|
|
701
|
+
box3.addPt(b, x + U[0] * len * (s + 1), y + U[1] * len * (s + 1), z);
|
|
702
|
+
return b;
|
|
703
|
+
}
|
|
704
|
+
draw(d) {
|
|
705
|
+
const o = this.opts, [x, y, z] = o.at || [0, 0, 0];
|
|
706
|
+
const pl = PLANES[o.plane || 'screen'];
|
|
707
|
+
const cls = cssClass(o.cls || (o.plane === 'screen' || !o.plane ? 'tx tx-label' : 'tx'));
|
|
708
|
+
const anchor = textAnchor(o.anchor);
|
|
709
|
+
const ls = o.spacing !== undefined ? ' letter-spacing="' + num(o.spacing) + '"' : '';
|
|
710
|
+
const wt = fontWeight(o.weight) ? ' font-weight="' + fontWeight(o.weight) + '"' : '';
|
|
711
|
+
let str = String(o.text ?? '');
|
|
712
|
+
let size = o.size || 8;
|
|
713
|
+
/* fit the whole text first, so a typewriter reveal keeps one size instead of shrinking letter by letter */
|
|
714
|
+
if (o.max && !str.includes('\n')) { const f = fitText(str, size, cls, o.spacing, o.max, o.minSize); str = f.str; size = f.size; }
|
|
715
|
+
if (o.chars !== undefined) str = str.slice(0, Math.max(0, Math.round(o.chars)));
|
|
716
|
+
const lines = str.split('\n');
|
|
717
|
+
const lh = (o.lineHeight || 1.3) * size;
|
|
718
|
+
let tsp = lines.length === 1 ? escText(lines[0]) : lines.map((l, i) => '<tspan x="0" dy="' + (i ? fmt(lh) : 0) + '">' + escText(l) + '</tspan>').join('');
|
|
719
|
+
if (!pl) {
|
|
720
|
+
const w = d.w(x, y, z), p = d.P(w[0], w[1], w[2]);
|
|
721
|
+
const dx = o.dx || 0, dy = o.dy || 0;
|
|
722
|
+
/* estimate the text box so fitting includes it (mono ≈ 0.62em per char, labels are tracked) */
|
|
723
|
+
const tw = Math.max(...lines.map((l) => l.length)) * size * (cls.includes('tx-label') ? 0.72 : 0.62);
|
|
724
|
+
const x0 = p[0] + dx - (anchor === 'middle' ? tw / 2 : anchor === 'end' ? tw : 0), y1 = p[1] + dy;
|
|
725
|
+
const b = d.b;
|
|
726
|
+
b[0] = Math.min(b[0], x0); b[2] = Math.max(b[2], x0 + tw); b[1] = Math.min(b[1], y1 - size); b[3] = Math.max(b[3], y1 + lh * (lines.length - 1) + size * 0.3);
|
|
727
|
+
d.raw('<text class="' + cls + '" transform="translate(' + fmt(p[0] + dx) + ' ' + fmt(p[1] + dy) + ')" font-size="' + fmt(size) + '" text-anchor="' + anchor + '"' + ls + wt + '>' + tsp + '</text>');
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
const U = frameVec(d.frame, pl[0]), V = frameVec(d.frame, pl[1]);
|
|
731
|
+
const f = new Face(d, d.w(x, y, z), U, V, 0, 0);
|
|
732
|
+
const m = f.matrix(0, 0);
|
|
733
|
+
d.raw('<text class="' + cls + '" transform="' + m + '" font-size="' + fmt(size) + '" text-anchor="' + anchor + '"' + ls + wt + '>' + tsp + '</text>');
|
|
734
|
+
d.P(...f.pt(String(o.text).length * size * 0.6, 0));
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/* A rectangle of 2D content on a plane: panes of glass, signs, screens, decals.
|
|
739
|
+
draw(face) receives a Face whose (0,0) is `at` and whose size is w × h. */
|
|
740
|
+
export class Pane extends Node {
|
|
741
|
+
get kind() { return 'pane'; }
|
|
742
|
+
basis(frame) {
|
|
743
|
+
const o = this.opts, pl = PLANES[o.plane || 'left'];
|
|
744
|
+
return { U: frameVec(frame, o.u || pl[0]), V: frameVec(frame, o.v || pl[1]) };
|
|
745
|
+
}
|
|
746
|
+
corners() {
|
|
747
|
+
const o = this.opts, at = o.at || [0, 0, 0], pl = PLANES[o.plane || 'left'];
|
|
748
|
+
const U = o.u || pl[0], V = o.v || pl[1], w = o.w ?? 20, h = o.h ?? 20;
|
|
749
|
+
const pt = (u, v) => [at[0] + U[0] * u + V[0] * v, at[1] + U[1] * u + V[1] * v, at[2] + U[2] * u + V[2] * v];
|
|
750
|
+
return [pt(0, 0), pt(w, 0), pt(w, h), pt(0, h)];
|
|
751
|
+
}
|
|
752
|
+
lbox() { const b = box3.empty(); for (const p of this.corners()) box3.addPt(b, p[0], p[1], p[2]); return b; }
|
|
753
|
+
aabb(frame) { const b = box3.empty(); for (const p of this.corners()) { const w = applyFrame(frame, p[0], p[1], p[2]); box3.addPt(b, w[0], w[1], w[2]); } return b; }
|
|
754
|
+
draw(d) {
|
|
755
|
+
const o = this.opts, at = o.at || [0, 0, 0], { U, V } = this.basis(d.frame);
|
|
756
|
+
const f = new Face(d, d.w(at[0], at[1], at[2]), U, V, o.w ?? 20, o.h ?? 20);
|
|
757
|
+
if (o.fill !== false || o.line !== false) {
|
|
758
|
+
f.rect(0, 0, f.w, f.h, { r: o.r || 0, fill: o.fill === undefined ? 'f-glass' : o.fill, line: o.line === undefined ? 'ln' : o.line });
|
|
759
|
+
}
|
|
760
|
+
if (typeof o.draw === 'function') o.draw(f, this);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/* Lines on the floor. */
|
|
765
|
+
export class Grid extends Node {
|
|
766
|
+
get kind() { return 'grid'; }
|
|
767
|
+
lbox() { const o = this.opts, [x, y, z] = o.at || [0, 0, 0], [w, dd] = o.size || [100, 100]; return [x, y, z, x + w, y + dd, z]; }
|
|
768
|
+
draw(d) {
|
|
769
|
+
const o = this.opts, [x, y, z] = o.at || [0, 0, 0], [w, dd] = o.size || [100, 100], st = o.step || 10;
|
|
770
|
+
const cls = o.line || 'ln-faint';
|
|
771
|
+
for (let i = 0; i <= w + 1e-6; i += st) d.line(d.dl([[x + i, y, z], [x + i, y + dd, z]]), cls);
|
|
772
|
+
for (let j = 0; j <= dd + 1e-6; j += st) d.line(d.dl([[x, y + j, z], [x + w, y + j, z]]), cls);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/* Escape hatch: draw anything with the Draw context. fn(d, node) */
|
|
777
|
+
export class Custom extends Node {
|
|
778
|
+
get kind() { return 'custom'; }
|
|
779
|
+
lbox() { return this.opts.bounds || null; }
|
|
780
|
+
draw(d) { if (this.opts.draw) this.opts.draw(d, this); }
|
|
781
|
+
}
|