@rulecad/gears 0.3.5 → 0.4.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/tooth.mjs CHANGED
@@ -5,8 +5,21 @@ import { grid, analyse, positions } from '@rulecad/fea';
5
5
  import { gearModel, spurProfile, lewisFactor } from './core.mjs';
6
6
 
7
7
  const fmt = (x, d = 1) => String(+x.toFixed(d));
8
- const inside = ([x, y], pts) => { let c = false; for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) { const [xi, yi] = pts[i], [xj, yj] = pts[j]; if ((yi > y) !== (yj > y) && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) c = !c; } return c; };
9
- const toSeg = ([px, py], [ax, ay], [bx, by]) => { const dx = bx - ax, dy = by - ay, t = Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy || 1))); return Math.hypot(px - ax - t * dx, py - ay - t * dy); };
8
+ const inside = ([x, y], pts) => {
9
+ let c = false;
10
+ for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) {
11
+ const [xi, yi] = pts[i],
12
+ [xj, yj] = pts[j];
13
+ if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) c = !c;
14
+ }
15
+ return c;
16
+ };
17
+ const toSeg = ([px, py], [ax, ay], [bx, by]) => {
18
+ const dx = bx - ax,
19
+ dy = by - ay,
20
+ t = Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy || 1)));
21
+ return Math.hypot(px - ax - t * dx, py - ay - t * dy);
22
+ };
10
23
 
11
24
  /**
12
25
  * A tooth's root stress by finite elements, at a torque on the gear.
@@ -15,58 +28,131 @@ const toSeg = ([px, py], [ax, ay], [bx, by]) => { const dx = bx - ax, dy = by -
15
28
  * cells: across a module (16)
16
29
  */
17
30
  export function toothStress(p, { width, torque, material, cells = 32 } = {}) {
18
- const fine = solveTooth(p, { width, torque, material, cells }), coarse = solveTooth(p, { width, torque, material, cells: Math.round(cells * 0.75) });
19
- const change = fine.peak / coarse.peak - 1, out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
20
- const { peak, lewis, h, nx, ny, atRoot } = fine, name = material.name ?? 'the material', n = material.strength ? material.strength / peak : null;
21
- f(n == null ? 'info' : n >= 2 ? 'pass' : n >= 1 ? 'warn' : 'fail', `a tooth analysed (${nx} × ${ny} cells of ${fmt(h, 3)} mm): ${fmt(peak)} MPa at the root of its loaded side, ${fmt(atRoot, 2)} mm above the root circle, ${fmt(peak / lewis, 2)}× Lewis's ${fmt(lewis)} MPa (the fillet's concentration)${n == null ? '' : n >= 1 ? `: ${fmt(n)}× under ${name}'s ${material.strength} MPa` : `: over ${name}'s ${material.strength} MPa, it breaks`}`, 'gear.fe', { peak, lewis });
22
- if (Math.abs(change) > 0.05) f('warn', `the peak changed ${fmt(change * 100)}% from cells a third bigger: not settled; more cells`, 'gear.fe.coarse', { change });
31
+ const fine = solveTooth(p, { width, torque, material, cells }),
32
+ coarse = solveTooth(p, { width, torque, material, cells: Math.round(cells * 0.75) });
33
+ const change = fine.peak / coarse.peak - 1,
34
+ out = [],
35
+ f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
36
+ const { peak, lewis, h, nx, ny, atRoot } = fine,
37
+ name = material.name ?? 'the material',
38
+ n = material.strength ? material.strength / peak : null;
39
+ f(
40
+ n == null ? 'info' : n >= 2 ? 'pass' : n >= 1 ? 'warn' : 'fail',
41
+ `a tooth analysed (${nx} × ${ny} cells of ${fmt(h, 3)} mm): ${fmt(peak)} MPa at the root of its loaded side, ${fmt(atRoot, 2)} mm above the root circle, ${fmt(peak / lewis, 2)}× Lewis's ${fmt(lewis)} MPa (the fillet's concentration)${n == null ? '' : n >= 1 ? `: ${fmt(n)}× under ${name}'s ${material.strength} MPa` : `: over ${name}'s ${material.strength} MPa, it breaks`}`,
42
+ 'gear.fe',
43
+ { peak, lewis },
44
+ );
45
+ if (Math.abs(change) > 0.05)
46
+ f(
47
+ 'warn',
48
+ `the peak changed ${fmt(change * 100)}% from cells a third bigger: not settled; more cells`,
49
+ 'gear.fe.coarse',
50
+ { change },
51
+ );
23
52
  return { peak, at: fine.at, lewis, ratio: peak / lewis, cells: [nx, ny], change, findings: out };
24
53
  }
25
54
 
26
55
  function solveTooth(p, { width, torque, material, cells }) {
27
- const g = gearModel(p), m = g.module, z = g.teeth, a = (g.pressureAngle * Math.PI) / 180;
28
- if (!(width > 0) || !(torque > 0)) throw new Error(`toothStress: width and torque are sizes above 0 (got ${width} and ${torque})`);
29
- if (!(material?.E > 0)) throw new Error('toothStress: the material needs a modulus E (MPa) and a Poisson\'s ratio nu');
56
+ const g = gearModel(p),
57
+ m = g.module,
58
+ z = g.teeth,
59
+ a = (g.pressureAngle * Math.PI) / 180;
60
+ if (!(width > 0) || !(torque > 0))
61
+ throw new Error(`toothStress: width and torque are sizes above 0 (got ${width} and ${torque})`);
62
+ if (!(material?.E > 0)) throw new Error("toothStress: the material needs a modulus E (MPa) and a Poisson's ratio nu");
30
63
  // the tooth on +x between the middles of its spaces, and the rim under it two and a half modules deep
31
- const loop = spurProfile(p).loops[0], half = Math.PI / z, rf = g.root / 2, rr = rf - 2.5 * m;
32
- const tooth = loop.filter(([x, y]) => Math.abs(Math.atan2(y, x)) <= half + 1e-9 && x > 0).sort((u, v) => Math.atan2(u[1], u[0]) - Math.atan2(v[1], v[0]));
33
- const arc = Array.from({ length: 13 }, (_, i) => { const t = half - (2 * half * i) / 12; return [rr * Math.cos(t), rr * Math.sin(t)]; });
64
+ const loop = spurProfile(p).loops[0],
65
+ half = Math.PI / z,
66
+ rf = g.root / 2,
67
+ rr = rf - 2.5 * m;
68
+ const tooth = loop
69
+ .filter(([x, y]) => Math.abs(Math.atan2(y, x)) <= half + 1e-9 && x > 0)
70
+ .sort((u, v) => Math.atan2(u[1], u[0]) - Math.atan2(v[1], v[0]));
71
+ const arc = Array.from({ length: 13 }, (_, i) => {
72
+ const t = half - (2 * half * i) / 12;
73
+ return [rr * Math.cos(t), rr * Math.sin(t)];
74
+ });
34
75
  const poly = [...tooth, ...arc];
35
76
  // a grid over it, each cell as much solid as its 3 × 3 samples say
36
- const h = m / cells, x0 = Math.min(...poly.map((q) => q[0])), y0 = Math.min(...poly.map((q) => q[1]));
37
- const nx = Math.ceil((Math.max(...poly.map((q) => q[0])) - x0) / h), ny = Math.ceil((Math.max(...poly.map((q) => q[1])) - y0) / h);
38
- const G = grid({ nx, ny, h }), density = new Float64Array(G.cells);
39
- for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) {
40
- let n = 0;
41
- for (let s = 0; s < 3; s++) for (let t = 0; t < 3; t++) if (inside([x0 + (i + (s + 0.5) / 3) * h, y0 + (j + (t + 0.5) / 3) * h], poly)) n++;
42
- density[j * nx + i] = n / 9;
43
- }
77
+ const h = m / cells,
78
+ x0 = Math.min(...poly.map((q) => q[0])),
79
+ y0 = Math.min(...poly.map((q) => q[1]));
80
+ const nx = Math.ceil((Math.max(...poly.map((q) => q[0])) - x0) / h),
81
+ ny = Math.ceil((Math.max(...poly.map((q) => q[1])) - y0) / h);
82
+ const G = grid({ nx, ny, h }),
83
+ density = new Float64Array(G.cells);
84
+ for (let j = 0; j < ny; j++)
85
+ for (let i = 0; i < nx; i++) {
86
+ let n = 0;
87
+ for (let s = 0; s < 3; s++)
88
+ for (let t = 0; t < 3; t++) if (inside([x0 + (i + (s + 0.5) / 3) * h, y0 + (j + (t + 0.5) / 3) * h], poly)) n++;
89
+ density[j * nx + i] = n / 9;
90
+ }
44
91
  // held: the rim's cut edges (the two radial cuts and the arc under them)
45
- const at = positions(G).map(([x, y]) => [x + x0, y + y0]), fixed = new Uint8Array(G.dofs);
46
- const cutA = [[rf * Math.cos(half), rf * Math.sin(half)], [rr * Math.cos(half), rr * Math.sin(half)]], cutB = [[rf * Math.cos(-half), rf * Math.sin(-half)], [rr * Math.cos(-half), rr * Math.sin(-half)]];
92
+ const at = positions(G).map(([x, y]) => [x + x0, y + y0]),
93
+ fixed = new Uint8Array(G.dofs);
94
+ const cutA = [
95
+ [rf * Math.cos(half), rf * Math.sin(half)],
96
+ [rr * Math.cos(half), rr * Math.sin(half)],
97
+ ],
98
+ cutB = [
99
+ [rf * Math.cos(-half), rf * Math.sin(-half)],
100
+ [rr * Math.cos(-half), rr * Math.sin(-half)],
101
+ ];
47
102
  for (let n = 0; n < G.nodes; n++) {
48
- const q = at[n], r = Math.hypot(...q), near = toSeg(q, ...cutA) < 0.75 * h || toSeg(q, ...cutB) < 0.75 * h || (Math.abs(r - rr) < 0.75 * h && Math.abs(Math.atan2(q[1], q[0])) <= half);
103
+ const q = at[n],
104
+ r = Math.hypot(...q),
105
+ near =
106
+ toSeg(q, ...cutA) < 0.75 * h ||
107
+ toSeg(q, ...cutB) < 0.75 * h ||
108
+ (Math.abs(r - rr) < 0.75 * h && Math.abs(Math.atan2(q[1], q[0])) <= half);
49
109
  if (near) fixed[2 * n] = fixed[2 * n + 1] = 1;
50
110
  }
51
111
  // the load: the mate's push on the lower flank near the tip, along the flank's normal (the line of action), F_t / cos α
52
- const Ft = torque / (g.pitch / 2), Fn = Ft / Math.cos(a), ra = g.tip / 2;
112
+ const Ft = torque / (g.pitch / 2),
113
+ Fn = Ft / Math.cos(a),
114
+ ra = g.tip / 2;
53
115
  const flank = tooth.filter(([x, y]) => y < 0 && Math.hypot(x, y) < ra - 0.2 * m && Math.hypot(x, y) > ra - 0.6 * m);
54
- const k = Math.floor(flank.length / 2), P = flank[k] ?? tooth[1], Q = flank[Math.min(k + 1, flank.length - 1)] ?? tooth[2];
55
- const tx = Q[0] - P[0], ty = Q[1] - P[1], tl = Math.hypot(tx, ty) || 1, nIn = [-ty / tl, tx / tl]; // (inward: the tooth is on the left of its outline)
56
- const load = new Float64Array(G.dofs), touch = [];
116
+ const k = Math.floor(flank.length / 2),
117
+ P = flank[k] ?? tooth[1],
118
+ Q = flank[Math.min(k + 1, flank.length - 1)] ?? tooth[2];
119
+ const tx = Q[0] - P[0],
120
+ ty = Q[1] - P[1],
121
+ tl = Math.hypot(tx, ty) || 1,
122
+ nIn = [-ty / tl, tx / tl]; // (inward: the tooth is on the left of its outline)
123
+ const load = new Float64Array(G.dofs),
124
+ touch = [];
57
125
  for (let n = 0; n < G.nodes; n++) if (Math.hypot(at[n][0] - P[0], at[n][1] - P[1]) < 0.15 * m) touch.push(n);
58
126
  if (!touch.length) throw new Error('toothStress: no node near the load: more cells');
59
- for (const n of touch) { load[2 * n] = (Fn * nIn[0]) / touch.length; load[2 * n + 1] = (Fn * nIn[1]) / touch.length; }
60
- const r = analyse({ grid: G, density, material: { E: material.E, nu: material.nu }, thickness: width, fixed, loads: [load], method: 'direct' });
127
+ for (const n of touch) {
128
+ load[2 * n] = (Fn * nIn[0]) / touch.length;
129
+ load[2 * n + 1] = (Fn * nIn[1]) / touch.length;
130
+ }
131
+ const r = analyse({
132
+ grid: G,
133
+ density,
134
+ material: { E: material.E, nu: material.nu },
135
+ thickness: width,
136
+ fixed,
137
+ loads: [load],
138
+ method: 'direct',
139
+ });
61
140
  // the peak on the loaded side (where the root is in tension, as Lewis and the strength mean), away from where the
62
141
  // load goes in (a point load's own stress is a property of the model, not the tooth)
63
- let peak = 0, where = null;
64
- for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) {
65
- const e = j * nx + i; if (density[e] < 0.5) continue;
66
- const c = [x0 + (i + 0.5) * h, y0 + (j + 0.5) * h];
67
- if (c[1] > 0 || Math.hypot(c[0] - P[0], c[1] - P[1]) < 0.6 * m) continue;
68
- if (r.stress[0][e] > peak) { peak = r.stress[0][e]; where = c; }
69
- }
142
+ let peak = 0,
143
+ where = null;
144
+ for (let j = 0; j < ny; j++)
145
+ for (let i = 0; i < nx; i++) {
146
+ const e = j * nx + i;
147
+ if (density[e] < 0.5) continue;
148
+ const c = [x0 + (i + 0.5) * h, y0 + (j + 0.5) * h];
149
+ if (c[1] > 0 || Math.hypot(c[0] - P[0], c[1] - P[1]) < 0.6 * m) continue;
150
+ // (the stress at the part's surface, fea's analyse().surface: the cells' centres, half a cell inside, read it low)
151
+ if (r.surface[0][e] > peak) {
152
+ peak = r.surface[0][e];
153
+ where = c;
154
+ }
155
+ }
70
156
  const lewis = Ft / (width * m * lewisFactor(z));
71
157
  return { peak, at: where, lewis, h, nx, ny, atRoot: Math.hypot(...where) - rf };
72
158
  }
package/worm.mjs CHANGED
@@ -10,7 +10,10 @@ const { mat4 } = jscad.maths;
10
10
  const DEG = Math.PI / 180;
11
11
  const fmt = (x, d = 3) => String(+x.toFixed(d));
12
12
  const isNum = (x) => typeof x === 'number' && Number.isFinite(x);
13
- function size(what, k, v) { if (!(isNum(v) && v > 0)) throw new Error(`${what}: ${k} is a size above 0 (got ${JSON.stringify(v)})`); return v; }
13
+ function size(what, k, v) {
14
+ if (!(isNum(v) && v > 0)) throw new Error(`${what}: ${k} is a size above 0 (got ${JSON.stringify(v)})`);
15
+ return v;
16
+ }
14
17
 
15
18
  /**
16
19
  * A worm's numbers. module: its axial module (the wheel's transverse module); starts: threads round it (1 to 4);
@@ -21,19 +24,41 @@ function size(what, k, v) { if (!(isNum(v) && v > 0)) throw new Error(`${what}:
21
24
  export function wormModel(p) {
22
25
  const what = 'wormModel';
23
26
  if (!p || typeof p !== 'object') throw new Error(`${what}: give it { module, starts, diameter }`);
24
- const m = size(what, 'module', p.module), starts = p.starts ?? 1;
25
- if (!(Number.isInteger(starts) && starts >= 1 && starts <= 4)) throw new Error(`${what}: starts is a whole number from 1 to 4 (got ${JSON.stringify(p.starts)})`);
27
+ const m = size(what, 'module', p.module),
28
+ starts = p.starts ?? 1;
29
+ if (!(Number.isInteger(starts) && starts >= 1 && starts <= 4))
30
+ throw new Error(`${what}: starts is a whole number from 1 to 4 (got ${JSON.stringify(p.starts)})`);
26
31
  const d = p.diameter === undefined ? 10 * m : size(what, 'diameter', p.diameter);
27
- if (d < 2 * 1.25 * m + 4 * m) throw new Error(`${what}: a pitch diameter of ${d} mm leaves under two modules of core under the threads (at least ${fmt(6.5 * m)} mm)`);
28
- const mu = p.friction ?? 0.25, axialPitch = Math.PI * m, lead = starts * axialPitch;
29
- const gamma = Math.atan(lead / (Math.PI * d)), phi = Math.atan(mu), alpha = (p.pressureAngle ?? 20) * DEG;
32
+ if (d < 2 * 1.25 * m + 4 * m)
33
+ throw new Error(
34
+ `${what}: a pitch diameter of ${d} mm leaves under two modules of core under the threads (at least ${fmt(6.5 * m)} mm)`,
35
+ );
36
+ const mu = p.friction ?? 0.25,
37
+ axialPitch = Math.PI * m,
38
+ lead = starts * axialPitch;
39
+ const gamma = Math.atan(lead / (Math.PI * d)),
40
+ phi = Math.atan(mu),
41
+ alpha = (p.pressureAngle ?? 20) * DEG;
30
42
  // efficiency driving from the worm, through the normal pressure angle: tan γ (cos αn − μ tan γ) / (cos αn tan γ + μ)
31
43
  const an = Math.atan(Math.tan(alpha) * Math.cos(gamma));
32
44
  const efficiency = (Math.tan(gamma) * (Math.cos(an) - mu * Math.tan(gamma))) / (Math.cos(an) * Math.tan(gamma) + mu);
33
45
  return {
34
- kind: 'worm', module: m, starts, pitch: d, tip: d + 2 * m, root: d - 2.5 * m, axialPitch, lead, hand: p.hand ?? 'right',
35
- pressureAngle: alpha / DEG, backlash: p.backlash ?? 0.15, leadAngle: gamma / DEG, friction: mu, frictionAngle: phi / DEG,
36
- locks: gamma < phi, efficiency,
46
+ kind: 'worm',
47
+ module: m,
48
+ starts,
49
+ pitch: d,
50
+ tip: d + 2 * m,
51
+ root: d - 2.5 * m,
52
+ axialPitch,
53
+ lead,
54
+ hand: p.hand ?? 'right',
55
+ pressureAngle: alpha / DEG,
56
+ backlash: p.backlash ?? 0.15,
57
+ leadAngle: gamma / DEG,
58
+ friction: mu,
59
+ frictionAngle: phi / DEG,
60
+ locks: gamma < phi,
61
+ efficiency,
37
62
  };
38
63
  }
39
64
 
@@ -42,26 +67,62 @@ export function wormModel(p) {
42
67
  * @param {{ module: number, starts?: number, diameter?: number, length: number, bore?: number, pressureAngle?: number, backlash?: number, hand?: 'right' | 'left' }} p
43
68
  */
44
69
  export function worm(p) {
45
- const w = wormModel(p), length = size('worm', 'length', p.length), m = w.module, r = w.pitch / 2;
70
+ const w = wormModel(p),
71
+ length = size('worm', 'length', p.length),
72
+ m = w.module,
73
+ r = w.pitch / 2;
46
74
  // the thread's axial section: the basic rack's tooth, its pitch line on the pitch radius (heights above it outward)
47
75
  const teeth = Math.max(2, w.starts * 2);
48
- const [loop] = rackProfile({ module: m, teeth, pressureAngle: w.pressureAngle, backlash: w.backlash, height: m }).loops;
49
- const p0 = w.axialPitch, top = loop.slice(2).reverse().filter(([x]) => x >= p0 && x <= 2 * p0); // one pitch of the top outline, along x
50
- const heightAt = (u) => { // the rack's height above its pitch line at u along it (one pitch repeating)
76
+ const [loop] = rackProfile({
77
+ module: m,
78
+ teeth,
79
+ pressureAngle: w.pressureAngle,
80
+ backlash: w.backlash,
81
+ height: m,
82
+ }).loops;
83
+ const p0 = w.axialPitch,
84
+ top = loop
85
+ .slice(2)
86
+ .reverse()
87
+ .filter(([x]) => x >= p0 && x <= 2 * p0); // one pitch of the top outline, along x
88
+ const heightAt = (u) => {
89
+ // the rack's height above its pitch line at u along it (one pitch repeating)
51
90
  const x = p0 + (((u % p0) + p0) % p0);
52
- for (let i = 1; i < top.length; i++) if (top[i][0] >= x) { const [a, b] = [top[i - 1], top[i]]; return a[1] + ((b[1] - a[1]) * (x - a[0])) / (b[0] - a[0] || 1); }
91
+ for (let i = 1; i < top.length; i++)
92
+ if (top[i][0] >= x) {
93
+ const [a, b] = [top[i - 1], top[i]];
94
+ return a[1] + ((b[1] - a[1]) * (x - a[0])) / (b[0] - a[0] || 1);
95
+ }
53
96
  return top.at(-1)[1];
54
97
  };
55
98
  // the section across the axis at z = 0: a point at angle θ is on the thread's axial section at u = −lead θ / 2π
56
- const sense = w.hand === 'left' ? -1 : 1, n = 90 * w.starts;
57
- const section = Array.from({ length: n }, (_, i) => { const t = (2 * Math.PI * i) / n, rho = r + heightAt((-sense * w.lead * t) / (2 * Math.PI)); return [rho * Math.cos(t), rho * Math.sin(t)]; });
58
- const hole = p.bore ? Array.from({ length: 48 }, (_, i) => { const t = (-2 * Math.PI * i) / 48; return [(p.bore / 2) * Math.cos(t), (p.bore / 2) * Math.sin(t)]; }) : null;
59
- if (p.bore && !(p.bore < w.root - 2 * m)) throw new Error(`worm: a bore of ${p.bore} mm leaves under a module of wall under the threads`);
99
+ const sense = w.hand === 'left' ? -1 : 1,
100
+ n = 90 * w.starts;
101
+ const section = Array.from({ length: n }, (_, i) => {
102
+ const t = (2 * Math.PI * i) / n,
103
+ rho = r + heightAt((-sense * w.lead * t) / (2 * Math.PI));
104
+ return [rho * Math.cos(t), rho * Math.sin(t)];
105
+ });
106
+ const hole = p.bore
107
+ ? Array.from({ length: 48 }, (_, i) => {
108
+ const t = (-2 * Math.PI * i) / 48;
109
+ return [(p.bore / 2) * Math.cos(t), (p.bore / 2) * Math.sin(t)];
110
+ })
111
+ : null;
112
+ if (p.bore && !(p.bore < w.root - 2 * m))
113
+ throw new Error(`worm: a bore of ${p.bore} mm leaves under a module of wall under the threads`);
60
114
  // up the axis the section turns: a turn for each lead, the thread's own helix
61
- const steps = Math.max(2, Math.ceil((length / w.lead) * 72)), slices = [];
115
+ const steps = Math.max(2, Math.ceil((length / w.lead) * 72)),
116
+ slices = [];
62
117
  for (let k = 0; k <= steps; k++) {
63
- const z = (length * k) / steps, a = (sense * 2 * Math.PI * z) / w.lead, c = Math.cos(a), s = Math.sin(a), at = ([x, y]) => [x * c - y * s, x * s + y * c];
64
- const sides = [section, ...(hole ? [hole] : [])].flatMap((pts) => pts.map((q, i) => [at(q), at(pts[(i + 1) % pts.length])]));
118
+ const z = (length * k) / steps,
119
+ a = (sense * 2 * Math.PI * z) / w.lead,
120
+ c = Math.cos(a),
121
+ s = Math.sin(a),
122
+ at = ([x, y]) => [x * c - y * s, x * s + y * c];
123
+ const sides = [section, ...(hole ? [hole] : [])].flatMap((pts) =>
124
+ pts.map((q, i) => [at(q), at(pts[(i + 1) % pts.length])]),
125
+ );
65
126
  slices.push(slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, z]), slice.fromSides(sides)));
66
127
  }
67
128
  return extrudeFromSlices({ numberOfSlices: slices.length, repair: false, callback: (t, i) => slices[i] }, slices[0]);
@@ -73,7 +134,18 @@ export function worm(p) {
73
134
  */
74
135
  export function wormWheel(wormSpec, p) {
75
136
  const w = wormModel(wormSpec);
76
- return helical({ module: w.module, teeth: p.teeth, width: p.width, bore: p.bore, shift: p.shift, pressureAngle: w.pressureAngle, backlash: w.backlash, helix: w.leadAngle, hand: w.hand, chamfer: p.chamfer });
137
+ return helical({
138
+ module: w.module,
139
+ teeth: p.teeth,
140
+ width: p.width,
141
+ bore: p.bore,
142
+ shift: p.shift,
143
+ pressureAngle: w.pressureAngle,
144
+ backlash: w.backlash,
145
+ helix: w.leadAngle,
146
+ hand: w.hand,
147
+ chamfer: p.chamfer,
148
+ });
77
149
  }
78
150
 
79
151
  /**
@@ -81,12 +153,50 @@ export function wormWheel(wormSpec, p) {
81
153
  * whether it locks, and how efficient it is. @param {object} wormSpec @param {{ teeth: number, shift?: number }} wheel
82
154
  */
83
155
  export function wormMesh(wormSpec, wheel) {
84
- const w = wormModel(wormSpec), g = gearModel({ module: w.module, teeth: wheel.teeth, shift: wheel.shift ?? 0, pressureAngle: w.pressureAngle, helix: w.leadAngle, hand: w.hand });
85
- const out = [], f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
156
+ const w = wormModel(wormSpec),
157
+ g = gearModel({
158
+ module: w.module,
159
+ teeth: wheel.teeth,
160
+ shift: wheel.shift ?? 0,
161
+ pressureAngle: w.pressureAngle,
162
+ helix: w.leadAngle,
163
+ hand: w.hand,
164
+ });
165
+ const out = [],
166
+ f = (level, text, code, values) => out.push({ level, text, code, ...(values ? { values } : {}) });
86
167
  const ratio = w.starts / g.teeth;
87
- f('info', `${g.teeth / w.starts} turns of the worm for one of the wheel; lead angle ${fmt(w.leadAngle, 2)}°`, 'worm.ratio', { ratio });
88
- if (w.locks) f('pass', `it locks: the lead angle (${fmt(w.leadAngle, 2)}°) is under the friction angle (${fmt(w.frictionAngle, 1)}° at μ ${w.friction}), so the wheel can't turn the worm; ${fmt(w.efficiency * 100, 0)}% efficient driving`, 'worm.locks', { locks: true });
89
- else f('info', `it doesn't lock: the lead angle (${fmt(w.leadAngle, 2)}°) is over the friction angle (${fmt(w.frictionAngle, 1)}°), so a load on the wheel can turn the worm back; ${fmt(w.efficiency * 100, 0)}% efficient driving`, 'worm.locks', { locks: false });
90
- if (w.leadAngle > 25) f('warn', `a lead angle of ${fmt(w.leadAngle, 1)}° is steep for a worm: the wheel's teeth lean as much, and print poorly past about 45° at their tips`, 'worm.lead');
91
- return { centre: (w.pitch + g.pitch) / 2 + (wheel.shift ?? 0) * w.module, ratio, locks: w.locks, efficiency: w.efficiency, leadAngle: w.leadAngle, findings: out };
168
+ f(
169
+ 'info',
170
+ `${g.teeth / w.starts} turns of the worm for one of the wheel; lead angle ${fmt(w.leadAngle, 2)}°`,
171
+ 'worm.ratio',
172
+ { ratio },
173
+ );
174
+ if (w.locks)
175
+ f(
176
+ 'pass',
177
+ `it locks: the lead angle (${fmt(w.leadAngle, 2)}°) is under the friction angle (${fmt(w.frictionAngle, 1)}° at μ ${w.friction}), so the wheel can't turn the worm; ${fmt(w.efficiency * 100, 0)}% efficient driving`,
178
+ 'worm.locks',
179
+ { locks: true },
180
+ );
181
+ else
182
+ f(
183
+ 'info',
184
+ `it doesn't lock: the lead angle (${fmt(w.leadAngle, 2)}°) is over the friction angle (${fmt(w.frictionAngle, 1)}°), so a load on the wheel can turn the worm back; ${fmt(w.efficiency * 100, 0)}% efficient driving`,
185
+ 'worm.locks',
186
+ { locks: false },
187
+ );
188
+ if (w.leadAngle > 25)
189
+ f(
190
+ 'warn',
191
+ `a lead angle of ${fmt(w.leadAngle, 1)}° is steep for a worm: the wheel's teeth lean as much, and print poorly past about 45° at their tips`,
192
+ 'worm.lead',
193
+ );
194
+ return {
195
+ centre: (w.pitch + g.pitch) / 2 + (wheel.shift ?? 0) * w.module,
196
+ ratio,
197
+ locks: w.locks,
198
+ efficiency: w.efficiency,
199
+ leadAngle: w.leadAngle,
200
+ findings: out,
201
+ };
92
202
  }