@rulecad/cad 0.8.0 → 0.10.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.
Files changed (102) hide show
  1. package/AGENTS.md +45 -16
  2. package/CHANGELOG.md +724 -0
  3. package/EXAMPLES.md +61 -54
  4. package/README.md +376 -98
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2664 -541
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1883 -435
  9. package/lib/beams.mjs +256 -52
  10. package/lib/belts.mjs +66 -15
  11. package/lib/board-work.mjs +12 -0
  12. package/lib/bought.mjs +309 -53
  13. package/lib/buildpage.mjs +179 -29
  14. package/lib/cache-file.mjs +105 -0
  15. package/lib/carried.mjs +201 -0
  16. package/lib/clone.mjs +194 -42
  17. package/lib/code-text.mjs +28 -0
  18. package/lib/design-imports.mjs +127 -0
  19. package/lib/design.mjs +871 -147
  20. package/lib/designshapes.mjs +142 -0
  21. package/lib/drive.mjs +187 -63
  22. package/lib/edit.mjs +922 -192
  23. package/lib/errors.mjs +16 -3
  24. package/lib/estimates.mjs +51 -0
  25. package/lib/explain.mjs +62 -13
  26. package/lib/fasten.mjs +2708 -507
  27. package/lib/fe-key.mjs +29 -0
  28. package/lib/fittings.mjs +721 -122
  29. package/lib/fixings.mjs +38 -13
  30. package/lib/framebuild.mjs +790 -175
  31. package/lib/framecheck.mjs +1901 -368
  32. package/lib/framegen.mjs +108 -22
  33. package/lib/frameheat.mjs +225 -42
  34. package/lib/frameprint.mjs +513 -108
  35. package/lib/frames.mjs +86 -18
  36. package/lib/framesize.mjs +719 -120
  37. package/lib/frametip.mjs +235 -0
  38. package/lib/gait.mjs +135 -23
  39. package/lib/grip.mjs +62 -13
  40. package/lib/guides.mjs +1403 -230
  41. package/lib/host.mjs +129 -23
  42. package/lib/inplace.mjs +171 -36
  43. package/lib/jigs.mjs +360 -78
  44. package/lib/joins.mjs +697 -118
  45. package/lib/jsonedit.mjs +88 -26
  46. package/lib/kept.mjs +84 -0
  47. package/lib/kit.mjs +439 -69
  48. package/lib/layout.mjs +2 -1
  49. package/lib/links.mjs +149 -42
  50. package/lib/list.mjs +334 -64
  51. package/lib/live-guard.mjs +68 -0
  52. package/lib/live-shape.mjs +96 -24
  53. package/lib/live.mjs +189 -59
  54. package/lib/loads.mjs +597 -136
  55. package/lib/machine.mjs +372 -74
  56. package/lib/magnets.mjs +162 -44
  57. package/lib/measured.mjs +1080 -117
  58. package/lib/mesh.mjs +526 -119
  59. package/lib/meshcmd.mjs +215 -60
  60. package/lib/motors.mjs +1002 -214
  61. package/lib/motorsvg.mjs +114 -32
  62. package/lib/names.mjs +18 -1
  63. package/lib/optimise.mjs +991 -287
  64. package/lib/order.mjs +515 -114
  65. package/lib/panels.mjs +482 -94
  66. package/lib/parts.mjs +81 -23
  67. package/lib/pins.mjs +163 -44
  68. package/lib/plates.mjs +223 -35
  69. package/lib/player.mjs +37 -9
  70. package/lib/power.mjs +64 -13
  71. package/lib/preview.mjs +83 -16
  72. package/lib/profilemeasure.mjs +572 -129
  73. package/lib/rules.mjs +17 -6
  74. package/lib/sandbox-hooks.mjs +179 -27
  75. package/lib/sandbox-lock.mjs +225 -0
  76. package/lib/sandbox-preload.mjs +2 -2
  77. package/lib/sandbox-run.mjs +122 -25
  78. package/lib/sandbox.mjs +667 -75
  79. package/lib/schema.mjs +42 -11
  80. package/lib/seats.mjs +227 -49
  81. package/lib/services.mjs +261 -52
  82. package/lib/sheet.mjs +963 -179
  83. package/lib/slotnuts.mjs +213 -34
  84. package/lib/targets/gist.mjs +4 -1
  85. package/lib/targets.mjs +22 -4
  86. package/lib/task.mjs +376 -79
  87. package/lib/versions.mjs +285 -39
  88. package/lib/wires.mjs +228 -45
  89. package/lib/workspace.mjs +200 -14
  90. package/package.json +19 -19
  91. package/schemas/assembly.schema.json +2 -2
  92. package/schemas/project.schema.json +6 -0
  93. package/templates/coupons/MEASURE.md +109 -0
  94. package/templates/coupons/assemblies/fixings-test.json +20 -0
  95. package/templates/coupons/assemblies/slot-test.json +11 -0
  96. package/templates/coupons/objects/fixings-coupon.js +88 -0
  97. package/templates/coupons/objects/fixings-coupon.json +26 -0
  98. package/templates/coupons/objects/holds-coupon.js +130 -0
  99. package/templates/coupons/objects/holds-coupon.json +42 -0
  100. package/templates/coupons/objects/slot-coupon.js +89 -0
  101. package/templates/coupons/objects/slot-coupon.json +29 -0
  102. package/templates/coupons/project.json +10 -0
package/lib/animate.mjs CHANGED
@@ -9,7 +9,8 @@ const STEP = 0.06; // seconds a frame
9
9
 
10
10
  const fmt = (n) => +n.toFixed(2);
11
11
  const pathOf = (outlines) => outlines.map((o) => `M${o.map(([x, y]) => `${fmt(x)} ${fmt(-y)}`).join('L')}Z`).join('');
12
- const escape = (s) => String(s).replace(/[<>&"]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' })[c]);
12
+ const escape = (s) =>
13
+ String(s).replace(/[<>&"]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' })[c]);
13
14
 
14
15
  /**
15
16
  * @param {{ frames: { from: string, to: string, t: number, parts: Record<string, number[][][]> }[], joints: Record<string, object> }} mechanism
@@ -19,48 +20,112 @@ const escape = (s) => String(s).replace(/[<>&"]/g, (c) => ({ '<': '&lt;', '>': '
19
20
  export function sequenceSvg(mechanism, { title = '' } = {}) {
20
21
  const frames = mechanism.frames || [];
21
22
  if (!frames.length) throw new Error('the mechanism has no sequence to animate: give its lamina a sequence of states');
22
- const flexures = new Set(Object.keys(mechanism.joints || {})), kind = (n) => (n === 'ground' ? 'ground' : flexures.has(n) ? 'flexure' : 'body');
23
+ const flexures = new Set(Object.keys(mechanism.joints || {})),
24
+ kind = (n) => (n === 'ground' ? 'ground' : flexures.has(n) ? 'flexure' : 'body');
23
25
  // The view: everything every frame reaches, with a margin and a line for the caption
24
- let lo = [Infinity, Infinity], hi = [-Infinity, -Infinity];
25
- for (const fr of frames) for (const outs of Object.values(fr.parts)) for (const o of outs) for (const [x, y] of o) { lo = [Math.min(lo[0], x), Math.min(lo[1], -y)]; hi = [Math.max(hi[0], x), Math.max(hi[1], -y)]; }
26
- const pad = Math.max(hi[0] - lo[0], hi[1] - lo[1]) * 0.05, caption = pad * 2;
26
+ let lo = [Infinity, Infinity],
27
+ hi = [-Infinity, -Infinity];
28
+ for (const fr of frames)
29
+ for (const outs of Object.values(fr.parts))
30
+ for (const o of outs)
31
+ for (const [x, y] of o) {
32
+ lo = [Math.min(lo[0], x), Math.min(lo[1], -y)];
33
+ hi = [Math.max(hi[0], x), Math.max(hi[1], -y)];
34
+ }
35
+ const pad = Math.max(hi[0] - lo[0], hi[1] - lo[1]) * 0.05,
36
+ caption = pad * 2;
27
37
  // The force curves of the steps a hand drives, a chart each, in a row under the drawing
28
- const charts = (mechanism.curves || []).flatMap((tr) => tr.curve.filter((c) => c.points.length > 1).map((c) => ({ from: tr.from, to: tr.to, ...c })));
38
+ const charts = (mechanism.curves || []).flatMap((tr) =>
39
+ tr.curve.filter((c) => c.points.length > 1).map((c) => ({ from: tr.from, to: tr.to, ...c })),
40
+ );
29
41
  const chartH = charts.length ? (hi[1] - lo[1]) * 0.35 : 0;
30
- const vb = [lo[0] - pad, lo[1] - pad - caption, hi[0] - lo[0] + 2 * pad, hi[1] - lo[1] + 2 * pad + caption * 2 + (chartH ? chartH + caption * 1.5 : 0)]; // a band above for the key, below for the step and the charts
42
+ const vb = [
43
+ lo[0] - pad,
44
+ lo[1] - pad - caption,
45
+ hi[0] - lo[0] + 2 * pad,
46
+ hi[1] - lo[1] + 2 * pad + caption * 2 + (chartH ? chartH + caption * 1.5 : 0),
47
+ ]; // a band above for the key, below for the step and the charts
31
48
  // Timing: each frame its share; a state (the first frame of each step, and the last) held longer
32
- const weights = frames.map((fr, i) => (fr.t === 0 || i === frames.length - 1 ? HOLD : 1)), total = weights.reduce((a, b) => a + b, 0);
33
- const starts = []; weights.reduce((at, w) => { starts.push(at / total); return at + w; }, 0);
49
+ const weights = frames.map((fr, i) => (fr.t === 0 || i === frames.length - 1 ? HOLD : 1)),
50
+ total = weights.reduce((a, b) => a + b, 0);
51
+ const starts = [];
52
+ weights.reduce((at, w) => {
53
+ starts.push(at / total);
54
+ return at + w;
55
+ }, 0);
34
56
  const dur = `${fmt(total * STEP)}s`;
35
- const show = (i) => { // visible only during frame i
36
- const a = fmt(starts[i]), b = fmt(i + 1 < frames.length ? starts[i + 1] : 1);
37
- const keys = [...new Set([0, a, b, 1])].sort((x, y) => x - y), values = keys.map((k) => (k >= a && k < b ? 'inline' : 'none'));
57
+ const show = (i) => {
58
+ // visible only during frame i
59
+ const a = fmt(starts[i]),
60
+ b = fmt(i + 1 < frames.length ? starts[i + 1] : 1);
61
+ const keys = [...new Set([0, a, b, 1])].sort((x, y) => x - y),
62
+ values = keys.map((k) => (k >= a && k < b ? 'inline' : 'none'));
38
63
  if (i === frames.length - 1) values[values.length - 1] = 'inline';
39
64
  return `<animate attributeName="display" values="${values.join(';')}" keyTimes="${keys.join(';')}" dur="${dur}" calcMode="discrete" repeatCount="indefinite"/>`;
40
65
  };
41
- const ground = frames[0].parts.ground ? `<path d="${pathOf(frames[0].parts.ground)}" fill="${COLOURS.ground}" fill-rule="evenodd"/>` : '';
42
- const size = fmt(caption * 0.45), textY = fmt(hi[1] + pad + caption * 0.65), keyY = fmt(vb[1] + caption * 0.65);
66
+ const ground = frames[0].parts.ground
67
+ ? `<path d="${pathOf(frames[0].parts.ground)}" fill="${COLOURS.ground}" fill-rule="evenodd"/>`
68
+ : '';
69
+ const size = fmt(caption * 0.45),
70
+ textY = fmt(hi[1] + pad + caption * 0.65),
71
+ keyY = fmt(vb[1] + caption * 0.65);
43
72
  // Each chart: its box, the curve, and where frame i sits on it (if frame i is in its step)
44
- const chartTop = hi[1] + pad + caption * 1.5, cw = vb[2] / Math.max(1, charts.length), inner = (k) => ({ x0: vb[0] + k * cw + pad * 1.5, x1: vb[0] + (k + 1) * cw - pad * 0.5, y0: chartTop + caption * 0.6, y1: chartTop + chartH });
73
+ const chartTop = hi[1] + pad + caption * 1.5,
74
+ cw = vb[2] / Math.max(1, charts.length),
75
+ inner = (k) => ({
76
+ x0: vb[0] + k * cw + pad * 1.5,
77
+ x1: vb[0] + (k + 1) * cw - pad * 0.5,
78
+ y0: chartTop + caption * 0.6,
79
+ y1: chartTop + chartH,
80
+ });
45
81
  const scaled = charts.map((c, k) => {
46
- const b = inner(k), xs = c.points.map(([x]) => Math.abs(x)), fs = c.points.map(([, F]) => F), xm = Math.max(1e-9, ...xs), fmin = Math.min(0, ...fs), fmax = Math.max(1e-9, ...fs);
47
- const at = ([x, F]) => [b.x0 + (Math.abs(x) / xm) * (b.x1 - b.x0), b.y1 - ((F - fmin) / (fmax - fmin)) * (b.y1 - b.y0)];
82
+ const b = inner(k),
83
+ xs = c.points.map(([x]) => Math.abs(x)),
84
+ fs = c.points.map(([, F]) => F),
85
+ xm = Math.max(1e-9, ...xs),
86
+ fmin = Math.min(0, ...fs),
87
+ fmax = Math.max(1e-9, ...fs);
88
+ const at = ([x, F]) => [
89
+ b.x0 + (Math.abs(x) / xm) * (b.x1 - b.x0),
90
+ b.y1 - ((F - fmin) / (fmax - fmin)) * (b.y1 - b.y0),
91
+ ];
48
92
  return { ...c, b, at, fmax, xm };
49
93
  });
50
- const chartArt = scaled.map((c) => {
51
- const pts = c.points.map(c.at), zero = c.at([0, 0])[1];
52
- const small = fmt(Math.min(size * 0.6, (c.b.x1 - c.b.x0) / 22)); // two short lines, sized to fit the chart's width
53
- return `<g font-family="system-ui, sans-serif" fill="#555"><text x="${fmt(c.b.x0)}" y="${fmt(c.b.y0 - caption * 0.45)}" font-size="${small}">${escape(`${c.from} to ${c.to}, ${c.joint}`)}</text><text x="${fmt(c.b.x0)}" y="${fmt(c.b.y0 - caption * 0.1)}" font-size="${small}">${escape(`up to ${c.fmax.toFixed(1)} N over ${c.xm.toFixed(1)} mm`)}</text>
94
+ const chartArt = scaled
95
+ .map((c) => {
96
+ const pts = c.points.map(c.at),
97
+ zero = c.at([0, 0])[1];
98
+ const small = fmt(Math.min(size * 0.6, (c.b.x1 - c.b.x0) / 22)); // two short lines, sized to fit the chart's width
99
+ return `<g font-family="system-ui, sans-serif" fill="#555"><text x="${fmt(c.b.x0)}" y="${fmt(c.b.y0 - caption * 0.45)}" font-size="${small}">${escape(`${c.from} to ${c.to}, ${c.joint}`)}</text><text x="${fmt(c.b.x0)}" y="${fmt(c.b.y0 - caption * 0.1)}" font-size="${small}">${escape(`up to ${c.fmax.toFixed(1)} N over ${c.xm.toFixed(1)} mm`)}</text>
54
100
  <line x1="${fmt(c.b.x0)}" y1="${fmt(zero)}" x2="${fmt(c.b.x1)}" y2="${fmt(zero)}" stroke="#ccc" stroke-width="${fmt(size * 0.04)}"/><line x1="${fmt(c.b.x0)}" y1="${fmt(c.b.y0)}" x2="${fmt(c.b.x0)}" y2="${fmt(c.b.y1)}" stroke="#ccc" stroke-width="${fmt(size * 0.04)}"/>
55
101
  <polyline points="${pts.map(([x, y]) => `${fmt(x)},${fmt(y)}`).join(' ')}" fill="none" stroke="#2f6fb3" stroke-width="${fmt(size * 0.08)}"/></g>`;
56
- }).join('\n');
57
- const markerOf = (fr) => scaled.filter((c) => c.from === fr.from && c.to === fr.to).map((c) => { const p = c.at(c.points[Math.min(c.points.length - 1, Math.round(fr.t * (c.points.length - 1)))]); return `<circle cx="${fmt(p[0])}" cy="${fmt(p[1])}" r="${fmt(size * 0.25)}" fill="#e07a1f"/>`; }).join('');
58
- const body = frames.map((fr, i) => {
59
- const label = fr.label || (fr.t === 0 && fr.from === fr.to ? fr.from : fr.t === 0 ? fr.from : `${fr.from} to ${fr.to}`);
60
- const shapes = Object.entries(fr.parts).filter(([n]) => n !== 'ground').map(([n, o]) => `<path d="${pathOf(o)}" fill="${COLOURS[kind(n)]}" fill-rule="evenodd"/>`).join('');
61
- return `<g display="${i ? 'none' : 'inline'}">${show(i)}${shapes}${markerOf(fr)}<text x="${fmt(vb[0] + pad)}" y="${textY}" font-size="${size}" font-family="system-ui, sans-serif" fill="#333">${escape(label)}</text></g>`;
62
- }).join('\n');
63
- const key = Object.entries(COLOURS).map(([k, c], i) => `<rect x="${fmt(vb[0] + pad + i * size * 5)}" y="${fmt(keyY - size * 0.8)}" width="${size}" height="${size}" fill="${c}"/><text x="${fmt(vb[0] + pad + i * size * 5 + size * 1.3)}" y="${keyY}" font-size="${fmt(size * 0.8)}" font-family="system-ui, sans-serif" fill="#555">${{ ground: 'frame', body: 'moves', flexure: 'bends' }[k]}</text>`).join('');
102
+ })
103
+ .join('\n');
104
+ const markerOf = (fr) =>
105
+ scaled
106
+ .filter((c) => c.from === fr.from && c.to === fr.to)
107
+ .map((c) => {
108
+ const p = c.at(c.points[Math.min(c.points.length - 1, Math.round(fr.t * (c.points.length - 1)))]);
109
+ return `<circle cx="${fmt(p[0])}" cy="${fmt(p[1])}" r="${fmt(size * 0.25)}" fill="#e07a1f"/>`;
110
+ })
111
+ .join('');
112
+ const body = frames
113
+ .map((fr, i) => {
114
+ const label =
115
+ fr.label || (fr.t === 0 && fr.from === fr.to ? fr.from : fr.t === 0 ? fr.from : `${fr.from} to ${fr.to}`);
116
+ const shapes = Object.entries(fr.parts)
117
+ .filter(([n]) => n !== 'ground')
118
+ .map(([n, o]) => `<path d="${pathOf(o)}" fill="${COLOURS[kind(n)]}" fill-rule="evenodd"/>`)
119
+ .join('');
120
+ return `<g display="${i ? 'none' : 'inline'}">${show(i)}${shapes}${markerOf(fr)}<text x="${fmt(vb[0] + pad)}" y="${textY}" font-size="${size}" font-family="system-ui, sans-serif" fill="#333">${escape(label)}</text></g>`;
121
+ })
122
+ .join('\n');
123
+ const key = Object.entries(COLOURS)
124
+ .map(
125
+ ([k, c], i) =>
126
+ `<rect x="${fmt(vb[0] + pad + i * size * 5)}" y="${fmt(keyY - size * 0.8)}" width="${size}" height="${size}" fill="${c}"/><text x="${fmt(vb[0] + pad + i * size * 5 + size * 1.3)}" y="${keyY}" font-size="${fmt(size * 0.8)}" font-family="system-ui, sans-serif" fill="#555">${{ ground: 'frame', body: 'moves', flexure: 'bends' }[k]}</text>`,
127
+ )
128
+ .join('');
64
129
  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${vb.map(fmt).join(' ')}" width="900">
65
130
  <title>${escape(title)}</title>
66
131
  <rect x="${fmt(vb[0])}" y="${fmt(vb[1])}" width="${fmt(vb[2])}" height="${fmt(vb[3])}" fill="#fff"/>
@@ -75,28 +140,79 @@ ${key}
75
140
  // A gait as one animated SVG: the side view (x, z) with each leg's hip, knee and foot drawn as sticks and the ground, and
76
141
  // under it the plan (x, y) with the feet in contact and the support they make. Self-contained, as the lamina sequence is.
77
142
  export function gaitData(s, r) {
78
- return { title: s.a.title || s.name, pattern: s.a.gait.pattern, period: s.gait.period, height: s.gait.height, legs: s.gait.legs.map((l) => l.name), frames: r.frames.map((f) => ({ t: f.t, margin: Number.isFinite(f.margin) ? Number(f.margin.toFixed(3)) : null, feet: f.feet.map((e) => ({ leg: e.leg, stance: e.stance, foot: e.foot, q: e.q, load: e.load })) })) };
143
+ return {
144
+ title: s.a.title || s.name,
145
+ pattern: s.a.gait.pattern,
146
+ period: s.gait.period,
147
+ height: s.gait.height,
148
+ legs: s.gait.legs.map((l) => l.name),
149
+ frames: r.frames.map((f) => ({
150
+ t: f.t,
151
+ margin: Number.isFinite(f.margin) ? Number(f.margin.toFixed(3)) : null,
152
+ feet: f.feet.map((e) => ({ leg: e.leg, stance: e.stance, foot: e.foot, q: e.q, load: e.load })),
153
+ })),
154
+ };
79
155
  }
80
156
 
81
157
  export function gaitSvg(s, r, { title = '' } = {}) {
82
- const gait = s.gait, joints = (l, q) => { const c = fk(l.chain, q); return [c.frames[0].at, c.frames[1].at, c.position]; };
83
- const frames = r.frames.map((f) => ({ ...f, sticks: f.feet.map((e) => { const l = gait.legs.find((x) => x.name === e.leg); return { ...e, pts: joints(l, e.q) }; }) }));
84
- const xs = frames.flatMap((f) => f.sticks.flatMap((e) => e.pts.map((p) => p[0]))), zs = frames.flatMap((f) => f.sticks.flatMap((e) => e.pts.map((p) => p[2])));
158
+ const gait = s.gait,
159
+ joints = (l, q) => {
160
+ const c = fk(l.chain, q);
161
+ return [c.frames[0].at, c.frames[1].at, c.position];
162
+ };
163
+ const frames = r.frames.map((f) => ({
164
+ ...f,
165
+ sticks: f.feet.map((e) => {
166
+ const l = gait.legs.find((x) => x.name === e.leg);
167
+ return { ...e, pts: joints(l, e.q) };
168
+ }),
169
+ }));
170
+ const xs = frames.flatMap((f) => f.sticks.flatMap((e) => e.pts.map((p) => p[0]))),
171
+ zs = frames.flatMap((f) => f.sticks.flatMap((e) => e.pts.map((p) => p[2])));
85
172
  const ys = frames.flatMap((f) => f.sticks.map((e) => e.foot[1]));
86
- const [x0, x1] = [Math.min(...xs) - 40, Math.max(...xs) + 40], [z0, z1] = [Math.min(...zs, -gait.height) - 40, Math.max(...zs, 0) + 40], [y0, y1] = [Math.min(...ys) - 40, Math.max(...ys) + 40];
87
- const side = { w: x1 - x0, h: z1 - z0 }, plan = { w: x1 - x0, h: y1 - y0 }, pad = Math.max(side.w, side.h) * 0.04, cap = pad * 1.6;
88
- const W = side.w + 2 * pad, H = side.h + plan.h + 3 * pad + cap, fx = (x) => fmt(x - x0 + pad), fz = (z) => fmt(z1 - z + pad + cap), fy = (y) => fmt(y - y0 + side.h + 2 * pad + cap);
89
- const hold = 1 / frames.length, dur = `${fmt(gait.period)}s`;
173
+ const [x0, x1] = [Math.min(...xs) - 40, Math.max(...xs) + 40],
174
+ [z0, z1] = [Math.min(...zs, -gait.height) - 40, Math.max(...zs, 0) + 40],
175
+ [y0, y1] = [Math.min(...ys) - 40, Math.max(...ys) + 40];
176
+ const side = { w: x1 - x0, h: z1 - z0 },
177
+ plan = { w: x1 - x0, h: y1 - y0 },
178
+ pad = Math.max(side.w, side.h) * 0.04,
179
+ cap = pad * 1.6;
180
+ const W = side.w + 2 * pad,
181
+ H = side.h + plan.h + 3 * pad + cap,
182
+ fx = (x) => fmt(x - x0 + pad),
183
+ fz = (z) => fmt(z1 - z + pad + cap),
184
+ fy = (y) => fmt(y - y0 + side.h + 2 * pad + cap);
185
+ const hold = 1 / frames.length,
186
+ dur = `${fmt(gait.period)}s`;
90
187
  const stroke = Math.max(side.w, side.h) * 0.012;
91
- const body = frames.map((f, i) => {
92
- const a = fmt(i * hold), b = fmt((i + 1) * hold), keys = [...new Set([0, a, b, 1])].sort((p, q) => p - q), vals = keys.map((k) => (k >= a && k < b ? 'inline' : 'none'));
93
- const sticks = f.sticks.map((e) => `<polyline points="${e.pts.map((p) => `${fx(p[0])},${fz(p[2])}`).join(' ')}" fill="none" stroke="${e.stance ? '#2f6fb3' : '#e07a1f'}" stroke-width="${fmt(stroke)}" stroke-linecap="round" stroke-linejoin="round"/><circle cx="${fx(e.pts[2][0])}" cy="${fz(e.pts[2][2])}" r="${fmt(stroke * 1.1)}" fill="${e.stance ? '#2f6fb3' : '#e07a1f'}"/>`).join('');
94
- const down = f.sticks.filter((e) => e.stance).map((e) => e.foot), pts = hull2D(down.map((p) => [p[0], p[1]]));
95
- const support = pts.length > 1 ? `<polygon points="${pts.map(([x, y]) => `${fx(x)},${fy(y)}`).join(' ')}" fill="#2f6fb333" stroke="#2f6fb3" stroke-width="${fmt(stroke / 2)}"/>` : '';
96
- const feet = f.sticks.map((e) => `<circle cx="${fx(e.foot[0])}" cy="${fy(e.foot[1])}" r="${fmt(stroke)}" fill="${e.stance ? '#2f6fb3' : '#e07a1f'}"/>`).join('');
97
- const text = `${fmt(f.time)} s of ${fmt(gait.period)} s${Number.isFinite(f.margin) ? `, ${fmt(f.margin)} mm of balance to spare` : ''}`;
98
- return `<g display="${i ? 'none' : 'inline'}"><animate attributeName="display" values="${vals.join(';')}" keyTimes="${keys.join(';')}" dur="${dur}" calcMode="discrete" repeatCount="indefinite"/>${sticks}${support}${feet}<text x="${fmt(pad)}" y="${fmt(cap * 0.6)}" font-family="system-ui, sans-serif" font-size="${fmt(pad * 0.7)}" fill="#333">${escape(text)}</text></g>`;
99
- }).join('\n');
188
+ const body = frames
189
+ .map((f, i) => {
190
+ const a = fmt(i * hold),
191
+ b = fmt((i + 1) * hold),
192
+ keys = [...new Set([0, a, b, 1])].sort((p, q) => p - q),
193
+ vals = keys.map((k) => (k >= a && k < b ? 'inline' : 'none'));
194
+ const sticks = f.sticks
195
+ .map(
196
+ (e) =>
197
+ `<polyline points="${e.pts.map((p) => `${fx(p[0])},${fz(p[2])}`).join(' ')}" fill="none" stroke="${e.stance ? '#2f6fb3' : '#e07a1f'}" stroke-width="${fmt(stroke)}" stroke-linecap="round" stroke-linejoin="round"/><circle cx="${fx(e.pts[2][0])}" cy="${fz(e.pts[2][2])}" r="${fmt(stroke * 1.1)}" fill="${e.stance ? '#2f6fb3' : '#e07a1f'}"/>`,
198
+ )
199
+ .join('');
200
+ const down = f.sticks.filter((e) => e.stance).map((e) => e.foot),
201
+ pts = hull2D(down.map((p) => [p[0], p[1]]));
202
+ const support =
203
+ pts.length > 1
204
+ ? `<polygon points="${pts.map(([x, y]) => `${fx(x)},${fy(y)}`).join(' ')}" fill="#2f6fb333" stroke="#2f6fb3" stroke-width="${fmt(stroke / 2)}"/>`
205
+ : '';
206
+ const feet = f.sticks
207
+ .map(
208
+ (e) =>
209
+ `<circle cx="${fx(e.foot[0])}" cy="${fy(e.foot[1])}" r="${fmt(stroke)}" fill="${e.stance ? '#2f6fb3' : '#e07a1f'}"/>`,
210
+ )
211
+ .join('');
212
+ const text = `${fmt(f.time)} s of ${fmt(gait.period)} s${Number.isFinite(f.margin) ? `, ${fmt(f.margin)} mm of balance to spare` : ''}`;
213
+ return `<g display="${i ? 'none' : 'inline'}"><animate attributeName="display" values="${vals.join(';')}" keyTimes="${keys.join(';')}" dur="${dur}" calcMode="discrete" repeatCount="indefinite"/>${sticks}${support}${feet}<text x="${fmt(pad)}" y="${fmt(cap * 0.6)}" font-family="system-ui, sans-serif" font-size="${fmt(pad * 0.7)}" fill="#333">${escape(text)}</text></g>`;
214
+ })
215
+ .join('\n');
100
216
  const ground = `<line x1="${fmt(pad)}" y1="${fz(-gait.height)}" x2="${fmt(W - pad)}" y2="${fz(-gait.height)}" stroke="#555" stroke-width="${fmt(stroke / 2)}"/>`;
101
217
  const tag = `<text x="${fmt(pad)}" y="${fmt(side.h + pad + cap * 1.3)}" font-family="system-ui, sans-serif" font-size="${fmt(pad * 0.7)}" fill="#555">Plan: feet in blue are down, orange in swing; quasi-static, so no momentum is counted</text>`;
102
218
  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${fmt(W)} ${fmt(H)}" width="900">\n<title>${escape(title)}</title>\n<rect width="${fmt(W)}" height="${fmt(H)}" fill="#fff"/>\n${ground}\n${tag}\n${body}\n</svg>\n`;
@@ -105,8 +221,16 @@ export function gaitSvg(s, r, { title = '' } = {}) {
105
221
  const hull2D = (points) => {
106
222
  const p = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
107
223
  if (p.length < 3) return p;
108
- const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]), lo = [], hi = [];
109
- for (const x of p) { while (lo.length >= 2 && cross(lo.at(-2), lo.at(-1), x) <= 0) lo.pop(); lo.push(x); }
110
- for (const x of [...p].reverse()) { while (hi.length >= 2 && cross(hi.at(-2), hi.at(-1), x) <= 0) hi.pop(); hi.push(x); }
224
+ const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]),
225
+ lo = [],
226
+ hi = [];
227
+ for (const x of p) {
228
+ while (lo.length >= 2 && cross(lo.at(-2), lo.at(-1), x) <= 0) lo.pop();
229
+ lo.push(x);
230
+ }
231
+ for (const x of [...p].reverse()) {
232
+ while (hi.length >= 2 && cross(hi.at(-2), hi.at(-1), x) <= 0) hi.pop();
233
+ hi.push(x);
234
+ }
111
235
  return [...lo.slice(0, -1), ...hi.slice(0, -1)];
112
236
  };