@rulecad/cad 0.7.97 → 0.9.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/AGENTS.md +26 -10
- package/CHANGELOG.md +442 -2
- package/EXAMPLES.md +48 -42
- package/README.md +226 -73
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2253 -514
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1854 -437
- package/lib/beams.mjs +256 -52
- package/lib/belts.mjs +66 -15
- package/lib/board-work.mjs +12 -0
- package/lib/bought.mjs +309 -53
- package/lib/buildpage.mjs +179 -29
- package/lib/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/design.mjs +734 -113
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +900 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2710 -502
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +792 -176
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +100 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +81 -17
- package/lib/framesize.mjs +792 -120
- package/lib/frametip.mjs +235 -0
- package/lib/gait.mjs +135 -23
- package/lib/grip.mjs +62 -13
- package/lib/guides.mjs +1403 -230
- package/lib/host.mjs +149 -18
- package/lib/inplace.mjs +171 -36
- package/lib/jigs.mjs +360 -78
- package/lib/joins.mjs +697 -118
- package/lib/jsonedit.mjs +88 -26
- package/lib/kept.mjs +84 -0
- package/lib/kit.mjs +438 -68
- package/lib/layout.mjs +3 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +326 -64
- package/lib/live-guard.mjs +68 -0
- package/lib/live-shape.mjs +96 -24
- package/lib/live.mjs +189 -59
- package/lib/loads.mjs +579 -130
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +417 -94
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1022 -212
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +41 -0
- package/lib/optimise.mjs +979 -283
- package/lib/order.mjs +519 -112
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +78 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +224 -36
- package/lib/player.mjs +37 -9
- package/lib/power.mjs +64 -13
- package/lib/preview.mjs +83 -16
- package/lib/profilemeasure.mjs +572 -129
- package/lib/rules.mjs +38 -4
- package/lib/sandbox-hooks.mjs +154 -12
- package/lib/sandbox-preload.mjs +2 -2
- package/lib/sandbox-run.mjs +127 -12
- package/lib/sandbox.mjs +632 -35
- package/lib/schema.mjs +43 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +851 -178
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +42 -6
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +113 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +216 -17
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
- package/schemas/object.schema.json +1 -1
- package/schemas/project.schema.json +1 -1
package/lib/drive.mjs
CHANGED
|
@@ -8,7 +8,8 @@ import { trainFrom, axisOf } from './motors.mjs';
|
|
|
8
8
|
|
|
9
9
|
const fmt = (n) => +Number(n).toFixed(3);
|
|
10
10
|
const esc = (s) => String(s).replace(/[<>&"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', '"': '"' })[c]);
|
|
11
|
-
const rowsAt = (W, p) =>
|
|
11
|
+
const rowsAt = (W, p) =>
|
|
12
|
+
[0, 1, 2].map((k) => W[4 * k] * p[0] + W[4 * k + 1] * p[1] + W[4 * k + 2] * p[2] + W[4 * k + 3]);
|
|
12
13
|
const COLOUR = { still: '#c9ccd1', moves: '#2f6fb3', load: '#e07a1f', bought: '#e9eaec' };
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -16,10 +17,17 @@ const COLOUR = { still: '#c9ccd1', moves: '#2f6fb3', load: '#e07a1f', bought: '#
|
|
|
16
17
|
* levels across the axis to section at, and how far to turn so the slowest part moves a readable amount.
|
|
17
18
|
*/
|
|
18
19
|
export function drivePlan(s, { turns } = {}) {
|
|
19
|
-
const motion = s.motion,
|
|
20
|
-
|
|
20
|
+
const motion = s.motion,
|
|
21
|
+
d = (motion.drives || []).find((x) => x.joint);
|
|
22
|
+
const root =
|
|
23
|
+
d?.joint ?? motion.joints.find((j) => !j.follow && motion.joints.some((x) => x.follow?.[0] === j.id))?.id;
|
|
21
24
|
if (!root) return null;
|
|
22
|
-
const load =
|
|
25
|
+
const load =
|
|
26
|
+
(motion.loads || []).find((l) => l.joint)?.joint ??
|
|
27
|
+
motion.joints
|
|
28
|
+
.filter((j) => trainFrom(motion, j.id).root === root && j.id !== root)
|
|
29
|
+
.sort((a, b) => trainFrom(motion, b.id).ratio - trainFrom(motion, a.id).ratio)[0]?.id ??
|
|
30
|
+
root;
|
|
23
31
|
const ratio = Math.abs(trainFrom(motion, load).ratio) || 1;
|
|
24
32
|
const n = turns ?? Math.min(6, Math.max(1, (ratio * 60) / 360)); // the slowest part through about 60°, in one to six turns
|
|
25
33
|
return { root, load, motor: d?.id ?? null, ratio, turns: n };
|
|
@@ -27,91 +35,190 @@ export function drivePlan(s, { turns } = {}) {
|
|
|
27
35
|
|
|
28
36
|
export function driveSvg(s, { turns, title = '' } = {}) {
|
|
29
37
|
const plan = drivePlan(s, { turns });
|
|
30
|
-
if (!plan)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
if (!plan)
|
|
39
|
+
throw new Error(
|
|
40
|
+
`${s.name} has nothing driven to animate: give it a "motor" item that "drives" a joint, or gears that follow one`,
|
|
41
|
+
);
|
|
42
|
+
const motion = s.motion,
|
|
43
|
+
rootJ = motion.joints.find((j) => j.id === plan.root);
|
|
44
|
+
const v0 = jointValues(motion, motion.pose ?? undefined),
|
|
45
|
+
W0 = placeItems(motion, v0);
|
|
46
|
+
const { O, Z } = axisOf(motion, W0, rootJ, mul),
|
|
47
|
+
F = axisFrame(O, Z);
|
|
34
48
|
const indexOf = new Map(s.items.map((it, k) => [it.id, k]));
|
|
35
49
|
const loadItem = motion.joints.find((j) => j.id === plan.load)?.child;
|
|
36
50
|
// Every part, in the axis's frame at rest: what's cut, and what it belongs to
|
|
37
|
-
const parts = s.parts
|
|
38
|
-
|
|
51
|
+
const parts = s.parts
|
|
52
|
+
.map((p) => ({ p, k: indexOf.get(p.item), world: p.triangles.map((t) => t.map(F)) }))
|
|
53
|
+
.filter((x) => x.k != null && x.world.length);
|
|
54
|
+
const span = (x) => {
|
|
55
|
+
let lo = Infinity,
|
|
56
|
+
hi = -Infinity;
|
|
57
|
+
for (const t of x.world)
|
|
58
|
+
for (const q of t) {
|
|
59
|
+
lo = Math.min(lo, q[2]);
|
|
60
|
+
hi = Math.max(hi, q[2]);
|
|
61
|
+
}
|
|
62
|
+
return [lo, hi];
|
|
63
|
+
};
|
|
39
64
|
// Where to cut: through each gear's teeth (the height whose section has the most edges, the middle of that run),
|
|
40
65
|
// rings named in a planetary included, and through the middle of what carries the load
|
|
41
|
-
const geared = new Set([
|
|
66
|
+
const geared = new Set([
|
|
67
|
+
...(motion.meshes || []).flatMap((m) => [m.joint, ...(m.members || [])]),
|
|
68
|
+
...motion.joints.filter((j) => j.follow).flatMap((j) => [j.id, j.follow[0]]),
|
|
69
|
+
]);
|
|
42
70
|
const childOf = new Map(motion.joints.map((j) => [j.id, j.child]));
|
|
43
|
-
const ringItems = new Set(
|
|
71
|
+
const ringItems = new Set(
|
|
72
|
+
(s.a.planetary || [])
|
|
73
|
+
.flatMap((g) => [g.ring, g.output?.ring])
|
|
74
|
+
.filter(Boolean)
|
|
75
|
+
.map((id) => indexOf.get(id)),
|
|
76
|
+
);
|
|
44
77
|
const gearItems = new Set([...[...geared].map((id) => childOf.get(id)), ...ringItems].filter((k) => k != null));
|
|
45
78
|
const teethAt = (x) => {
|
|
46
|
-
const [lo, hi] = span(x),
|
|
47
|
-
|
|
48
|
-
|
|
79
|
+
const [lo, hi] = span(x),
|
|
80
|
+
n = Math.min(160, Math.max(8, Math.round((hi - lo) / 0.25))),
|
|
81
|
+
counts = [];
|
|
82
|
+
for (let i = 1; i < n; i++) {
|
|
83
|
+
const z = lo + ((hi - lo) * i) / n;
|
|
84
|
+
counts.push([z, sliceAt(x.world, z).length]);
|
|
85
|
+
}
|
|
86
|
+
const top = Math.max(...counts.map((c) => c[1])),
|
|
87
|
+
run = counts.filter((c) => c[1] >= top * 0.9).map((c) => c[0]);
|
|
49
88
|
return run.length ? (run[0] + run.at(-1)) / 2 : (lo + hi) / 2;
|
|
50
89
|
};
|
|
51
90
|
const want = [];
|
|
52
|
-
for (const k of gearItems) {
|
|
53
|
-
|
|
91
|
+
for (const k of gearItems) {
|
|
92
|
+
const own = parts.filter((x) => x.k === k && x.p.role === 'part');
|
|
93
|
+
if (own.length) want.push({ z: teethAt({ world: own.flatMap((x) => x.world) }), ring: ringItems.has(k) });
|
|
94
|
+
}
|
|
95
|
+
if (loadItem != null && !gearItems.has(loadItem)) {
|
|
96
|
+
const own = parts.filter((x) => x.k === loadItem && x.p.role === 'part');
|
|
97
|
+
if (own.length) {
|
|
98
|
+
const [lo, hi] = span({ world: own.flatMap((x) => x.world) });
|
|
99
|
+
want.push({ z: (lo + hi) / 2, ring: false });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
54
102
|
want.sort((a, b) => b.ring - a.ring || a.z - b.z);
|
|
55
103
|
const levels = [];
|
|
56
104
|
for (const { z } of want) if (!levels.some((l) => Math.abs(l - z) < 1.5)) levels.push(z);
|
|
57
105
|
const picked = levels.slice(0, 3).sort((a, b) => a - b); // rings first, then the rest, three at most
|
|
58
106
|
// The frames: the root turned through its turns, each item's rigid move in the plane from two points on it
|
|
59
|
-
const N = Math.min(240, Math.max(24, Math.round(plan.turns * 40))),
|
|
60
|
-
|
|
107
|
+
const N = Math.min(240, Math.max(24, Math.round(plan.turns * 40))),
|
|
108
|
+
base = v0[plan.root]?.[0] ?? 0;
|
|
109
|
+
const ks = [...new Set(parts.map((x) => x.k))],
|
|
110
|
+
moves = new Map(ks.map((k) => [k, []]));
|
|
61
111
|
for (let f = 0; f <= N; f++) {
|
|
62
|
-
const W = f
|
|
112
|
+
const W = f
|
|
113
|
+
? placeItems(
|
|
114
|
+
motion,
|
|
115
|
+
jointValues(motion, {
|
|
116
|
+
...Object.fromEntries(Object.entries(v0).filter(([id]) => !motion.joints.find((j) => j.id === id)?.follow)),
|
|
117
|
+
[plan.root]: [base + (360 * plan.turns * f) / N],
|
|
118
|
+
}),
|
|
119
|
+
)
|
|
120
|
+
: W0;
|
|
63
121
|
for (const k of ks) {
|
|
64
|
-
const a = F(rowsAt(W[k], [0, 0, 0])),
|
|
65
|
-
|
|
122
|
+
const a = F(rowsAt(W[k], [0, 0, 0])),
|
|
123
|
+
local = invRows(W0[k]),
|
|
124
|
+
axisLocal = rotOnly(local, Z),
|
|
125
|
+
e = perp(axisLocal);
|
|
126
|
+
const b = F(rowsAt(W[k], e)),
|
|
127
|
+
a0 = F(rowsAt(W0[k], [0, 0, 0])),
|
|
128
|
+
b0 = F(rowsAt(W0[k], e));
|
|
66
129
|
const th = Math.atan2(b[1] - a[1], b[0] - a[0]) - Math.atan2(b0[1] - a0[1], b0[0] - a0[0]);
|
|
67
|
-
const list = moves.get(k),
|
|
68
|
-
|
|
69
|
-
|
|
130
|
+
const list = moves.get(k),
|
|
131
|
+
prev = list.length ? list.at(-1).th : 0;
|
|
132
|
+
let t = th;
|
|
133
|
+
while (t - prev > Math.PI) t -= 2 * Math.PI;
|
|
134
|
+
while (t - prev < -Math.PI) t += 2 * Math.PI; // turned on, not back
|
|
135
|
+
const c = Math.cos(t),
|
|
136
|
+
sn = Math.sin(t);
|
|
70
137
|
list.push({ th: t, tx: a[0] - (c * a0[0] - sn * a0[1]), ty: a[1] - (sn * a0[0] + c * a0[1]) });
|
|
71
138
|
}
|
|
72
139
|
}
|
|
73
|
-
const still = (k) =>
|
|
140
|
+
const still = (k) =>
|
|
141
|
+
moves.get(k).every((m) => Math.abs(m.th) < 1e-6 && Math.abs(m.tx) < 1e-6 && Math.abs(m.ty) < 1e-6);
|
|
74
142
|
// Each panel: the cut through every part that crosses it, at rest; the moving ones animated
|
|
75
|
-
const panels = picked
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
143
|
+
const panels = picked
|
|
144
|
+
.map((z) => {
|
|
145
|
+
const cuts = [];
|
|
146
|
+
for (const x of parts) {
|
|
147
|
+
const outlines = loops(sliceAt(x.world, z)).filter((o) => o.length > 2);
|
|
148
|
+
if (outlines.length)
|
|
149
|
+
cuts.push({
|
|
150
|
+
...x,
|
|
151
|
+
d: outlines.map((o) => `M${o.map(([u, w]) => `${fmt(u)} ${fmt(w)}`).join('L')}Z`).join(''),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const names = [...new Set(cuts.filter((c) => c.p.role === 'part').map((c) => c.p.item))];
|
|
155
|
+
return { z, cuts, names };
|
|
156
|
+
})
|
|
157
|
+
.filter((p) => p.cuts.length);
|
|
84
158
|
if (!panels.length) throw new Error(`${s.name}: no part crosses the planes through its gears`);
|
|
85
159
|
// The view: everything every frame reaches, in each panel
|
|
86
160
|
let r = 0;
|
|
87
|
-
for (const pn of panels)
|
|
88
|
-
|
|
161
|
+
for (const pn of panels)
|
|
162
|
+
for (const c of pn.cuts)
|
|
163
|
+
for (const t of c.world)
|
|
164
|
+
for (const q of t) if (Math.abs(q[2] - pn.z) < 50) r = Math.max(r, Math.hypot(q[0], q[1]));
|
|
165
|
+
const cell = 2 * r * 1.1,
|
|
166
|
+
gap = cell * 0.08,
|
|
167
|
+
cap = cell * 0.16,
|
|
168
|
+
W = panels.length * cell + (panels.length + 1) * gap,
|
|
169
|
+
H = cell + cap * 2 + gap;
|
|
89
170
|
const dur = `${fmt(Math.max(4, Math.min(12, plan.turns * 2)))}s`;
|
|
90
171
|
const anim = (k) => {
|
|
91
172
|
const m = moves.get(k);
|
|
92
|
-
return {
|
|
93
|
-
|
|
173
|
+
return {
|
|
174
|
+
t: `<animateTransform attributeName="transform" type="translate" values="${m.map((x) => `${fmt(x.tx)} ${fmt(x.ty)}`).join(';')}" dur="${dur}" repeatCount="indefinite"/>`,
|
|
175
|
+
r: `<animateTransform attributeName="transform" type="rotate" values="${m.map((x) => fmt((x.th * 180) / Math.PI)).join(';')}" dur="${dur}" repeatCount="indefinite"/>`,
|
|
176
|
+
};
|
|
94
177
|
};
|
|
95
|
-
const colour = (c) =>
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
178
|
+
const colour = (c) =>
|
|
179
|
+
c.p.role !== 'part' ? COLOUR.bought : c.k === loadItem ? COLOUR.load : still(c.k) ? COLOUR.still : COLOUR.moves;
|
|
180
|
+
const art = panels
|
|
181
|
+
.map((pn, i) => {
|
|
182
|
+
const cx = gap + i * (cell + gap) + cell / 2,
|
|
183
|
+
cy = cap + gap + cell / 2;
|
|
184
|
+
const order = [...pn.cuts].sort(
|
|
185
|
+
(a, b) => (a.p.role !== 'part') - (b.p.role !== 'part') || still(b.k) - still(a.k),
|
|
186
|
+
); // bought faint at the back, still parts under moving ones
|
|
187
|
+
const shapes = order
|
|
188
|
+
.map((c) => {
|
|
189
|
+
const fill = `<path d="${c.d}" fill="${colour(c)}" fill-rule="evenodd" stroke="#1a1a1a" stroke-width="${fmt(cell / 900)}"/>`;
|
|
190
|
+
if (still(c.k)) return fill;
|
|
191
|
+
const a = anim(c.k);
|
|
192
|
+
return `<g>${a.t}<g>${a.r}${fill}</g></g>`;
|
|
193
|
+
})
|
|
194
|
+
.join('');
|
|
195
|
+
const size = cap * 0.3,
|
|
196
|
+
room = Math.floor(cell / (size * 0.55)),
|
|
197
|
+
text = `at ${fmt(pn.z)} mm: ${pn.names.join(', ')}`;
|
|
198
|
+
const label = `<text x="${fmt(cx - cell / 2)}" y="${fmt(cap * 0.75)}" font-size="${fmt(size)}" font-family="system-ui, sans-serif" fill="#333">${esc(text.length > room ? `${text.slice(0, room - 1)}…` : text)}</text>`;
|
|
199
|
+
return `${label}<g transform="translate(${fmt(cx)} ${fmt(cy)}) scale(1 -1)">${shapes}</g>`;
|
|
200
|
+
})
|
|
201
|
+
.join('\n');
|
|
202
|
+
const times = (n) => (n === 1 ? 'once' : `${fmt(n)} times`),
|
|
203
|
+
along = `cut across ${plan.root}'s axis`;
|
|
204
|
+
const out =
|
|
205
|
+
plan.ratio !== 1
|
|
206
|
+
? `${along}: ${plan.load} turns ${fmt((360 * plan.turns) / plan.ratio)}° while ${plan.root} turns ${times(plan.turns)} (${fmt(plan.ratio)}:1)`
|
|
207
|
+
: `${along}: ${plan.root} turned ${times(plan.turns)}`;
|
|
208
|
+
const KEY = [
|
|
209
|
+
['moves', 'moves'],
|
|
210
|
+
['load', 'carries the load'],
|
|
211
|
+
['still', 'held still'],
|
|
212
|
+
['bought', 'bought'],
|
|
213
|
+
];
|
|
214
|
+
const ks2 = Math.min(cap * 0.3, (W - 2 * gap) / KEY.reduce((n, [, t]) => n + t.length * 0.55 + 3.2, 0)),
|
|
215
|
+
outSize = Math.min(cap * 0.34, (W - 2 * gap) / (out.length * 0.55));
|
|
113
216
|
let kx = gap;
|
|
114
|
-
const key = KEY.map(([k, t]) => {
|
|
217
|
+
const key = KEY.map(([k, t]) => {
|
|
218
|
+
const x = kx;
|
|
219
|
+
kx += ks2 * (t.length * 0.55 + 3.2);
|
|
220
|
+
return `<rect x="${fmt(x)}" y="${fmt(H - cap * 0.95)}" width="${fmt(ks2 * 1.2)}" height="${fmt(ks2 * 1.2)}" fill="${COLOUR[k]}" stroke="#1a1a1a" stroke-width="${fmt(cell / 900)}"/><text x="${fmt(x + ks2 * 1.6)}" y="${fmt(H - cap * 0.95 + ks2)}" font-size="${fmt(ks2)}" font-family="system-ui, sans-serif" fill="#555">${t}</text>`;
|
|
221
|
+
}).join('');
|
|
115
222
|
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${fmt(W)} ${fmt(H)}" width="${Math.min(1400, 480 * panels.length)}">
|
|
116
223
|
<title>${esc(title || s.a.title || s.name)}</title>
|
|
117
224
|
<rect width="${fmt(W)}" height="${fmt(H)}" fill="#fff"/>
|
|
@@ -123,11 +230,28 @@ ${key}
|
|
|
123
230
|
}
|
|
124
231
|
|
|
125
232
|
// Helpers on the flat row-major 4 × 4 matrices placeItems gives
|
|
126
|
-
function invRows(W) {
|
|
127
|
-
|
|
233
|
+
function invRows(W) {
|
|
234
|
+
// a rigid move's inverse: the turn transposed, the shift turned back
|
|
235
|
+
const R = [
|
|
236
|
+
[W[0], W[1], W[2]],
|
|
237
|
+
[W[4], W[5], W[6]],
|
|
238
|
+
[W[8], W[9], W[10]],
|
|
239
|
+
],
|
|
240
|
+
t = [W[3], W[7], W[11]];
|
|
128
241
|
const o = new Array(16).fill(0);
|
|
129
|
-
for (let i = 0; i < 3; i++) {
|
|
130
|
-
|
|
242
|
+
for (let i = 0; i < 3; i++) {
|
|
243
|
+
for (let j = 0; j < 3; j++) o[4 * i + j] = R[j][i];
|
|
244
|
+
o[4 * i + 3] = -(R[0][i] * t[0] + R[1][i] * t[1] + R[2][i] * t[2]);
|
|
245
|
+
}
|
|
246
|
+
o[15] = 1;
|
|
247
|
+
return o;
|
|
131
248
|
}
|
|
132
249
|
const rotOnly = (W, v) => [0, 1, 2].map((k) => W[4 * k] * v[0] + W[4 * k + 1] * v[1] + W[4 * k + 2] * v[2]);
|
|
133
|
-
const perp = (z) => {
|
|
250
|
+
const perp = (z) => {
|
|
251
|
+
const n = Math.hypot(...z) || 1,
|
|
252
|
+
u = z.map((c) => c / n),
|
|
253
|
+
seed = Math.abs(u[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0];
|
|
254
|
+
const c = [seed[1] * u[2] - seed[2] * u[1], seed[2] * u[0] - seed[0] * u[2], seed[0] * u[1] - seed[1] * u[0]],
|
|
255
|
+
m = Math.hypot(...c);
|
|
256
|
+
return c.map((x) => (x / m) * 10);
|
|
257
|
+
};
|