Skip to content

Pocket Watch ​

Pocket Watch, rendered in the brepjs playground
Showcase · 522 lines▶ Run it in the playground

An open-face pocket watch with a hand-wound movement of the 16.5 ligne class: 36.6 mm, 17 jewels, 18,000 vibrations per hour. Each tooth count is from a published teardown, so the gear ratios are true, and the three hands show the time that the train gives them. The time parameter runs the watch. The screws have the miniature thread of ISO 1501, and in the exploded view each one turns out of its tapped hole. The bridges, the escapement shapes, the winding parts, the dial and the case are a display model.

Code ​

This is the full program. The playground runs it in your browser without a change.

ts
import {
  box,
  chamfer,
  compound,
  cut,
  cutAll,
  cylinder,
  edgeFinder,
  extrude,
  face,
  fillet,
  fuseAll,
  getBounds,
  interpolateCurve,
  intersect,
  line,
  loft,
  makeExternalGear,
  polygon,
  revolve,
  rotate,
  threePointArc,
  torus,
  translate,
  unwrap,
  wire,
} from 'brepjs/quick';
import { color } from 'brepjs/playground';

// Open-face pocket watch with a hand-wound movement of the 16.5 ligne class: 36.6 mm, 17 jewels, 18,000
// vibrations per hour. Each tooth count is from a published teardown of this class, so each ratio is true, and
// the three hands show the time that the train gives them. The arbor layout comes from the centre distances of
// those gears. The bridges, the escapement shapes, the winding parts, the dial and the case are a display model.
// Units are millimetres. Z = 0 is the top of the main plate, and the dial side is below it. 12 o'clock is at +X.
const TAU = 2 * Math.PI;
const R = 18.3; // 36.6 mm movement

type P2 = [number, number];
type P3 = [number, number, number];
type Shape = ReturnType<typeof cylinder>;
type Tone = 'plate' | 'bridge' | 'brass' | 'steel' | 'blue' | 'ruby' | 'dial' | 'ink' | 'case' | 'glass';
type Mobile = 'barrel' | 'centre' | 'third' | 'fourth' | 'escape' | 'fork' | 'balance' | 'minute' | 'hour' | 'set1' | 'set2';
interface Part {
  tone: Tone;
  shape: Shape;
  lift: number; // level in the exploded view: -5 crystal, -4 hands, -3 dial, -1 dial side, 0 plate, 1 wheels, 2 balance, 3 bridges, 4 top, 6 case back
  on?: Mobile; // the mobile that the part turns with
}
const LIFT = 4; // millimetres between two levels of the exploded view

// Kernel shapes live in WASM memory. Every shape goes through own(). freeing() runs a build and then frees them all.
const owned: { delete(): void }[] = [];
const own = <T extends { delete(): void }>(s: T): T => {
  owned.push(s);
  return s;
};
const freeing = <T,>(build: () => T): T => { try { return build(); } finally { owned.splice(0).forEach((s) => s.delete()); } }; // also when the build throws

// ---- the gears: wheel teeth and module, pinion leaves. A pinion has the module of the wheel that drives it.
const BARREL = { teeth: 88, module: 0.191 };
const CENTRE = { teeth: 80, module: 0.151, leaves: 12 };
const THIRD = { teeth: 60, module: 0.138, leaves: 10 };
const FOURTH = { teeth: 70, module: 0.116, leaves: 8 };
const ESCAPE = { teeth: 15, leaves: 7, phase: (15.5 * Math.PI) / 180 }; // phase: the angle of the wheel on its arbor, which puts a tooth against a pallet stone
const MOTION = { cannon: 12, minute: 36, minutePinion: 10, hour: 40, module: 0.214, hourModule: 0.2054 }; // 12 + 36 and 10 + 40 teeth on one centre distance
const WINDING = { ratchet: 56, crown: 35, module: 0.238, setting: 16 };
// ---- the screws: miniature thread S for timepieces (ISO 1501). minor is the minor diameter of the thread in the hole.
type Thread = { major: number; pitch: number; minor: number };
const S06: Thread = { major: 0.6, pitch: 0.15, minor: 0.456 };
const S08: Thread = { major: 0.8, pitch: 0.2, minor: 0.608 };
const PLAY = 0.012; // radial play between a screw thread and the thread in its hole

const centres = (module: number, z1: number, z2: number): number => (module * (z1 + z2)) / 2;
const polar = (o: P2, r: number, a: number): P2 => [o[0] + r * Math.cos(a), o[1] + r * Math.sin(a)];
// The point at r1 from a and r2 from b, on the left (side = 1) or the right (side = -1) of the line a to b.
const meet = (a: P2, r1: number, b: P2, r2: number, side: 1 | -1): P2 => {
  const d = Math.hypot(b[0] - a[0], b[1] - a[1]);
  const t = (r1 * r1 - r2 * r2 + d * d) / (2 * d);
  const h = side * Math.sqrt(r1 * r1 - t * t);
  const u: P2 = [(b[0] - a[0]) / d, (b[1] - a[1]) / d];
  return [a[0] + u[0] * t - u[1] * h, a[1] + u[1] * t + u[0] * h];
};

// ---- the arbors. The stem is on +X, and the small seconds is opposite it.
const AT_CENTRE: P2 = [0, 0];
const AT_CROWN: P2 = [13, 1.6]; // over the stem
const AT_BARREL = meet(AT_CENTRE, centres(BARREL.module, BARREL.teeth, CENTRE.leaves), AT_CROWN, centres(WINDING.module, WINDING.ratchet, WINDING.crown), 1);
const AT_FOURTH: P2 = [-10.6, 0];
const AT_THIRD = meet(AT_CENTRE, centres(CENTRE.module, CENTRE.teeth, THIRD.leaves), AT_FOURTH, centres(THIRD.module, THIRD.teeth, FOURTH.leaves), -1);
const AT_ESCAPE = polar(AT_FOURTH, centres(FOURTH.module, FOURTH.teeth, ESCAPE.leaves), (-62 * Math.PI) / 180);
const AT_BALANCE: P2 = [1, -10.3];
const TO_BALANCE = Math.atan2(AT_BALANCE[1] - AT_ESCAPE[1], AT_BALANCE[0] - AT_ESCAPE[0]);
const AT_PALLET = polar(AT_ESCAPE, 3.66, TO_BALANCE);
const COCK_FOOT = polar([0, 0], R - 2.2, (-38 * Math.PI) / 180);
const STUD_DIR = Math.atan2(COCK_FOOT[1] - AT_BALANCE[1], COCK_FOOT[0] - AT_BALANCE[0]); // from the balance to the foot of its cock
const HAIRSPRING = { r0: 0.75, r1: 4.4, turns: 11.5 };
const DIAL = { radius: 17.6, face: -1.9, back: -1.5, numerals: 13.5, seconds: 4.1 };
const START = { hours: 10, minutes: 8, seconds: 42 }; // the time on the dial at time = 0
const CASE = { crown: 24.8 }; // where the stem ends and the crown starts
const AT_MINUTE = polar(AT_CENTRE, centres(MOTION.module, MOTION.cannon, MOTION.minute), (-50 * Math.PI) / 180);
const AT_SET2: P2 = [9.6, -2.6];
const AT_SET1 = meet(AT_MINUTE, centres(MOTION.module, MOTION.minute, WINDING.setting), AT_SET2, MOTION.module * WINDING.setting, 1);
const ARBOR: Record<Mobile, P2> = {
  barrel: AT_BARREL, centre: AT_CENTRE, third: AT_THIRD, fourth: AT_FOURTH, escape: AT_ESCAPE, fork: AT_PALLET,
  balance: AT_BALANCE, minute: AT_MINUTE, hour: AT_CENTRE, set1: AT_SET1, set2: AT_SET2,
};

// ---- levels above the plate
const Z = {
  centreWheel: [0.25, 0.4],
  barrelTeeth: [0.5, 0.8],
  thirdWheel: [0.88, 1.03],
  fourthWheel: [1.35, 1.5],
  escapeWheel: [0.6, 0.72],
  bridge: [2.45, 3.15],
  plate: [-1.35, 0], // 4.5 mm from the dial side of the plate to the top of the bridges
} as const;

const at = (s: Shape, [x, y]: P2, z = 0, turn = 0): Shape => {
  const turned = turn === 0 ? s : (own(rotate(s, (turn * 360) / TAU)) as Shape);
  return own(translate(turned, [x, y, z]) as Shape);
};
const disc = (r: number, z0: number, z1: number, c: P2 = [0, 0]): Shape => own(cylinder(r, z1 - z0, { at: [c[0], c[1], z0] }));
const ring = (r0: number, r1: number, z0: number, z1: number): Shape => own(unwrap(cut(disc(r1, z0, z1), disc(r0, z0 - 0.1, z1 + 0.1)))) as Shape;
const prism = (pts: P2[], z0: number, z1: number): Shape =>
  own(unwrap(extrude(own(unwrap(polygon(pts.map(([x, y]): P3 => [x, y, z0])))), z1 - z0))) as Shape;
const fused = (parts: Shape[]): Shape => own(unwrap(fuseAll(parts))) as Shape;
const minus = (s: Shape, tools: Shape[]): Shape => own(unwrap(cutAll(s, tools))) as Shape;

// An involute gear from z0 to z1. A pinion gets a positive profile shift and its wheel the same negative shift,
// so the centre distance stays at module * (z1 + z2) / 2.
const gear = (teeth: number, module: number, [z0, z1]: readonly [number, number], shift = 0, bore = 0): Shape => {
  const g = unwrap(makeExternalGear({ teeth, moduleSize: module, thickness: z1 - z0, shift, bore, flankThinning: module * 0.05, samples: 6 }));
  return own(translate(own(g.solid), [0, 0, z0]) as Shape);
};

// A wheel: a toothed rim, a hub and arms.
const wheel = (teeth: number, module: number, z: readonly [number, number], shift: number, arms: number, hub: number): Shape => {
  const rim = (teeth * module) / 2 - 3.2 * module - 0.25;
  const gap = 0.22 / ((rim + hub) / 2); // half the angle of one arm
  const windows = Array.from({ length: arms }, (_, i) => {
    const a0 = (i * TAU) / arms + gap;
    const a1 = ((i + 1) * TAU) / arms - gap;
    const am = (a0 + a1) / 2;
    const p = (r: number, a: number): P3 => [r * Math.cos(a), r * Math.sin(a), z[0] - 0.05];
    const edges = [
      own(threePointArc(p(rim, a0), p(rim, am), p(rim, a1))),
      own(line(p(rim, a1), p(hub, a1))),
      own(threePointArc(p(hub, a1), p(hub, am), p(hub, a0))),
      own(line(p(hub, a0), p(rim, a0))),
    ];
    return own(unwrap(extrude(own(unwrap(face(own(unwrap(wire(edges))) as never))), z[1] - z[0] + 0.1))) as Shape;
  });
  return minus(gear(teeth, module, z, shift, 0.6), windows);
};

// A flat spiral strip: the mainspring and the hairspring.
const spiral = (r0: number, r1: number, turns: number, thick: number, z0: number, z1: number): Shape => {
  const n = Math.ceil(turns * 20);
  const side = (off: number): P3[] =>
    Array.from({ length: n + 1 }, (_, i): P3 => {
      const a = (i / n) * turns * TAU;
      const r = r0 + ((r1 - r0) * i) / n + off;
      return [r * Math.cos(a), r * Math.sin(a), z0];
    });
  const inner = side(-thick / 2);
  const outer = side(thick / 2).reverse();
  const edges = [
    own(unwrap(interpolateCurve(inner))),
    own(line(inner[n] as P3, outer[0] as P3)),
    own(unwrap(interpolateCurve(outer))),
    own(line(outer[n] as P3, inner[0] as P3)),
  ];
  return own(unwrap(extrude(own(unwrap(face(own(unwrap(wire(edges))) as never))), z1 - z0))) as Shape;
};

// A helical thread tooth about +Z, from z = from to z = to. Its crest passes the +X side at z = ref.
// tip > core is the thread of a screw, tip < core the thread in a hole. The loft is smooth: no facets.
const helixTooth = (t: Thread, core: number, tip: number, from: number, to: number, ref: number): Shape => {
  const outward = tip > core;
  const root = outward ? core / 0.998 : core * 0.998; // a hair clear of the core or the bore
  const crest = outward ? tip : tip / 0.998;
  const half = 0.42 * t.pitch;
  const n = Math.max(2, Math.round(((to - from) / t.pitch) * 12)); // 12 sections in each turn
  const sections = Array.from({ length: n + 1 }, (_, j) => {
    const z = from + ((to - from) * j) / n;
    const a = TAU * ((z - ref) / t.pitch);
    const p = (r: number, dz: number): P3 => [r * Math.cos(a), r * Math.sin(a), z + dz];
    const pts = [p(root, -half), p(crest, 0), p(root, half)];
    return own(unwrap(wire(pts.map((q, i) => own(line(q, pts[(i + 1) % 3]!))))));
  });
  return own(unwrap(loft(sections, { ruled: false }))) as Shape;
};
// A slotted screw along Z, and its thread. The head sits on z = 0. The shank is the thread root, and the thread
// is on the last part of it.
const slotted = (t: Thread, r: number, length: number, threaded: number): [Shape, Shape] => {
  const profile: P2[] = [[0, -length], [t.minor / 2, -length], [t.minor / 2, 0], [r, 0], [r, 0.3], [0, 0.3]];
  const body = own(unwrap(revolve(own(unwrap(polygon(profile.map(([x, z]): P3 => [x, 0, z])))))));
  const slot = own(translate(own(box(2 * r + 0.2, 0.2, 0.2)), [-r - 0.1, -0.1, 0.16]));
  return [own(unwrap(cut(body, slot))) as Shape, helixTooth(t, t.minor / 2, t.major / 2, 0.42 * t.pitch - length, threaded - length - 0.42 * t.pitch, 0)];
};
// The thread in a hole, from z = 0 down to a depth. It is half a pitch from the thread of a screw on the same axis.
const tapped = (t: Thread, depth: number): Shape => helixTooth(t, t.major / 2 + PLAY, t.minor / 2 + PLAY, 0.42 * t.pitch - depth, -0.42 * t.pitch, t.pitch / 2);
const bore = (t: Thread): number => t.major / 2 + PLAY; // the radius of a hole that gets a thread

// A pierced jewel, flush with the face at z1.
const jewel = (c: P2, z0: number, z1: number, r = 0.55): Shape => at(ring(0.13, r, z0, z1), c);

// The angle of a driven gear that puts a tooth space on the tooth of its driver, on the line of centres.
const meshAngle = (driver: P2, driverAngle: number, zDriver: number, driven: P2, zDriven: number): number => {
  const phi = Math.atan2(driven[1] - driver[1], driven[0] - driver[0]);
  const phase = (phi - driverAngle) / (TAU / zDriver);
  return phi + Math.PI - (TAU / zDriven) * (0.5 - phase);
};

// The angle of each mobile after a running time in seconds. The escapement lets one tooth flank go at each vibration, so
// the train moves in 5 steps each second. The balance makes 2.5 oscillations each second.
function angles(time: number): Record<Mobile, number> {
  const beat = Math.floor(time * 5 + 1e-9) / 5;
  const centre = (beat / 3600) * TAU;
  const third = meshAngle(AT_CENTRE, centre, CENTRE.teeth, AT_THIRD, THIRD.leaves);
  const fourth = meshAngle(AT_THIRD, third, THIRD.teeth, AT_FOURTH, FOURTH.leaves);
  const minute = meshAngle(AT_CENTRE, centre, MOTION.cannon, AT_MINUTE, MOTION.minute);
  const set1 = meshAngle(AT_MINUTE, minute, MOTION.minute, AT_SET1, WINDING.setting);
  const swing = Math.sin(time * 2.5 * TAU);
  return {
    centre, third, fourth, minute, set1,
    barrel: meshAngle(AT_CENTRE, centre, CENTRE.leaves, AT_BARREL, BARREL.teeth),
    escape: meshAngle(AT_FOURTH, fourth, FOURTH.teeth, AT_ESCAPE, ESCAPE.leaves),
    hour: meshAngle(AT_MINUTE, minute, MOTION.minutePinion, AT_CENTRE, MOTION.hour),
    set2: meshAngle(AT_SET1, set1, WINDING.setting, AT_SET2, WINDING.setting),
    balance: ((270 * Math.PI) / 180) * swing,
    fork: TO_BALANCE - ((swing >= 0 ? 1 : -1) * 7 * Math.PI) / 180, // the balance pushes the fork the other way
  };
}

// All parts of the movement after a running time in seconds.
function movementParts(time = 0, explode = 0): Part[] {
  const A = angles(time);
  const parts: Part[] = [];
  const add = (tone: Tone, lift: number, ...shapes: Shape[]): void => {
    for (const shape of shapes) parts.push({ tone, shape, lift });
  };
  const turn = (on: Mobile, tone: Tone, lift: number, ...shapes: Shape[]): void => {
    for (const shape of shapes) parts.push({ tone, lift, on, shape: at(shape, ARBOR[on], 0, A[on]) });
  };

  // ---- where the screws are: three on the barrel bridge, two on the train bridge, one on the cock, two on the
  // pallet bridge and one on the click. A bridge screw goes through a foot of its bridge into the plate.
  const [g0, g1] = Z.bridge;
  const barrelScrews: P2[] = [[-2.4, 15.6], [13.9, 9.4], [15.8, -3.4]];
  const trainScrews: P2[] = [[-13.2, 8.2], [-14.6, -5.2]];
  const cockScrew = polar(COCK_FOOT, 0.9, STUD_DIR);
  const palletScrews = [-3.85, -5.05].map((d) => polar(AT_PALLET, d, TO_BALANCE + Math.PI / 2));
  const clickAt = polar(AT_BARREL, 8.75, (150 * Math.PI) / 180);
  const through = (t: Thread, c: P2, z0: number, z1: number): Shape => disc(t.major / 2 + 0.05, z0, z1, c); // a clearance hole

  // ---- main plate: the dial side has a recess for the motion works
  const [p0, p1] = Z.plate;
  const tappedHoles = [...[...barrelScrews, ...trainScrews, cockScrew].map((c) => disc(bore(S08), -1.0, 0.1, c)), ...palletScrews.map((c) => disc(bore(S06), -0.66, 0.1, c))];
  const recess = [disc(4.9, p0 - 0.1, -0.7, AT_CENTRE), disc(4.7, p0 - 0.1, -0.7, AT_MINUTE), disc(2.3, p0 - 0.1, -0.7, AT_SET1), disc(2.3, p0 - 0.1, -0.7, AT_SET2)];
  const pivots = [...[AT_CENTRE, AT_THIRD, AT_FOURTH, AT_ESCAPE, AT_PALLET].map((c) => disc(0.55, -0.3, 0.1, c)), disc(0.6, -0.62, 0.1, AT_BALANCE)];
  const stemSlot = own(translate(own(box(R - 9.4, 3.7, 1.5)), [9.5, -1.85, -1.05])) as Shape;
  add('plate', 0, minus(disc(R, p0, p1), [...recess, ...pivots, ...tappedHoles, stemSlot, disc(1.0, p0 - 0.1, p1 + 0.1, AT_BARREL), disc(0.5, p0 - 0.1, p1 + 0.1, AT_CENTRE), disc(0.2, p0 - 0.1, p1 + 0.1, AT_FOURTH)]));

  // ---- barrel: 88 teeth on the drum, the mainspring inside, the arbor up to the ratchet wheel
  const [b0, b1] = Z.barrelTeeth;
  const drum = minus(fused([gear(BARREL.teeth, BARREL.module, Z.barrelTeeth, -0.3), disc(7.75, b0, 2.3)]), [disc(7.2, b0 + 0.2, 2.4), disc(0.95, b0 - 0.1, b0 + 0.3)]);
  turn('barrel', 'brass', 1, drum);
  turn('barrel', 'brass', 1.5, ring(0.95, 7.2, 2.3, 2.42));
  turn('barrel', 'blue', 1.25, spiral(1.75, 7.05, 19.53, 0.165, b0 + 0.25, b0 + 1.75)); // 1.5 x 0.165 x 540 mm
  add('steel', 1, at(minus(fused([disc(0.9, 0.05, 3.2), disc(1.6, b0 + 0.25, 2.3)]), [disc(bore(S08), 2.05, 3.3)]), AT_BARREL)); // tapped for the ratchet screw

  // ---- going train. Each mobile is a wheel and the pinion that the wheel before it drives.
  const mobile = (on: Mobile, wheelShape: Shape, pinion: Shape, zArbor: readonly [number, number]): void => {
    turn(on, 'brass', 1, wheelShape);
    turn(on, 'steel', 1, fused([pinion, disc(0.12, zArbor[0], zArbor[1]), disc(0.3, zArbor[0] + 0.25, zArbor[1] - 0.45)]));
  };
  mobile('centre', wheel(CENTRE.teeth, CENTRE.module, Z.centreWheel, -0.35, 5, 1.1), gear(CENTRE.leaves, BARREL.module, [0.42, 1.05], 0.3), [-0.25, 3.0]);
  mobile('third', wheel(THIRD.teeth, THIRD.module, Z.thirdWheel, -0.4, 5, 0.8), gear(THIRD.leaves, CENTRE.module, [0.12, 0.55], 0.35), [-0.25, 3.0]);
  mobile('fourth', wheel(FOURTH.teeth, FOURTH.module, Z.fourthWheel, -0.3, 5, 0.7), gear(FOURTH.leaves, THIRD.module, [0.75, 1.12], 0.4), [-0.25, 3.0]);
  turn('fourth', 'steel', 1, disc(0.1, -2.14, -0.25)); // the fourth arbor goes through the plate and the dial to the seconds hand

  // Escape wheel: 15 club teeth that lean in the direction of rotation. The tooth shape is a display shape.
  const [e0, e1] = Z.escapeWheel;
  const club = Array.from({ length: ESCAPE.teeth }, (_, i) => {
    const a = ESCAPE.phase - (i * TAU) / ESCAPE.teeth;
    const deg = -Math.PI / 180;
    return [polar([0, 0], 2.4, a), polar([0, 0], 3.17, a + 11 * deg), polar([0, 0], 3.1, a + 15 * deg), polar([0, 0], 2.88, a + 13.4 * deg), polar([0, 0], 2.4, a + 6 * deg)];
  }).flat();
  const escapeWheel = minus(prism(club, e0, e1), [0, 1, 2, 3].map((i) => own(rotate(prism([polar([0, 0], 0.75, 0.35), polar([0, 0], 1.95, 0.16), polar([0, 0], 1.95, TAU / 4 - 0.16), polar([0, 0], 0.75, TAU / 4 - 0.35)], e0 - 0.05, e1 + 0.05), i * 90)) as Shape));
  mobile('escape', minus(escapeWheel, [disc(0.3, e0 - 0.1, e1 + 0.1)]), gear(ESCAPE.leaves, FOURTH.module, [1.25, 1.6], 0.3), [-0.25, 3.0]);

  // ---- pallet fork and its two stones (display shape)
  const forkLen = Math.hypot(AT_BALANCE[0] - AT_PALLET[0], AT_BALANCE[1] - AT_PALLET[1]);
  const lever = prism([[-0.3, -0.42], [forkLen - 1.9, -0.3], [forkLen - 1.25, -0.75], [forkLen - 1.0, -0.28], [forkLen - 1.45, 0], [forkLen - 1.0, 0.28], [forkLen - 1.25, 0.75], [forkLen - 1.9, 0.3], [-0.3, 0.42]], e0, e1);
  const arms = prism([[0.45, -0.35], [-0.15, -1.95], [-0.45, -1.8], [-0.2, 0], [-0.45, 1.8], [-0.15, 1.95], [0.45, 0.35]], e0, e1);
  const stones = [-1, 1].map((s) => own(translate(own(box(0.75, 0.3, e1 - e0 + 0.06)), [-1.05, s * 1.6 - 0.15, e0 - 0.03])) as Shape);
  turn('fork', 'steel', 1, minus(fused([lever, arms, disc(0.12, -0.25, 1.15)]), stones));
  turn('fork', 'ruby', 1, ...stones);

  // ---- balance: a smooth rim on three arms, the roller with its impulse jewel, and the hairspring. The inner
  // end of the hairspring turns with the balance. The outer end stays at its stud, so the coils open and close.
  const rim = fused([ring(6.25, 7.025, 1.3, 1.75), ring(0.32, 0.9, 1.3, 1.75), ...[0, 120, 240].map((d) => own(rotate(own(translate(own(box(5.7, 0.7, 0.3)), [0.7, -0.35, 1.3])), d)) as Shape)]);
  turn('balance', 'brass', 2, rim);
  turn('balance', 'steel', 2, fused([disc(0.1, -0.42, 2.95), disc(0.32, 0.8, 2.3), disc(1.05, 0.82, 0.95), disc(HAIRSPRING.r0 - 0.03, 1.98, 2.24)]));
  turn('balance', 'ruby', 2, disc(0.14, 0.5, 0.82, polar([0, 0], 0.82, TO_BALANCE + Math.PI)));
  const coils = HAIRSPRING.turns - A.balance / TAU;
  add('blue', 2, at(spiral(HAIRSPRING.r0, HAIRSPRING.r1, coils, 0.04, 2.0, 2.22), AT_BALANCE, 0, STUD_DIR - coils * TAU));

  // ---- bridges. Each one is a slab on a wall at the rim of the plate, with a foot under each screw.
  const rounded = (s: Shape, r: number): Shape => own(unwrap(fillet(s, edgeFinder().inDirection('Z').findAll(s), r))) as Shape;
  const bevel = (s: Shape, z: number): Shape => {
    const onTop = (e: Parameters<typeof getBounds>[0]) => {
      const b = getBounds(e);
      return Math.abs(b.zMin - z) < 1e-6 && Math.abs(b.zMax - z) < 1e-6;
    };
    return own(unwrap(chamfer(s, edgeFinder().when(onTop).findAll(s), 0.1))) as Shape;
  };
  const slab = (outline: P2[], holes: Shape[], screws: P2[]): Shape => {
    const top = bevel(own(unwrap(intersect(rounded(prism(outline, g0, g1), 0.9), disc(R - 0.15, g0, g1)))) as Shape, g1);
    const wall = own(unwrap(intersect(prism(outline, 0, g0), ring(R - 1.5, R - 0.15, 0, g0)))) as Shape;
    return minus(fused([top, wall, ...screws.map((c) => disc(1.0, 0, g0 + 0.1, c))]), [...holes, ...screws.map((c) => through(S08, c, -0.1, g1 + 0.1))]);
  };
  const stemHole = own(cylinder(1.0, 4, { at: [R - 3, 0, 0.9], axis: [1, 0, 0] }));
  const barrelBridge = slab(
    [[-4, 20], [-4, 3.2], [-2.2, -1.4], [0.8, -2.6], [10, -2.6], [13.2, -4.9], [20, -4.9], [20, 20]],
    [disc(7.15, 2.9, g1 + 0.1, AT_BARREL), disc(4.6, 2.9, g1 + 0.1, AT_CROWN), disc(1.0, g0 - 0.1, g1, AT_BARREL), disc(0.55, g0 - 0.1, g1 + 0.1, AT_CENTRE), stemHole, disc(8.9, -0.1, g0 - 0.02, AT_BARREL), own(translate(own(box(1.2, 2.6, 0.6)), [12.3, -1.3, g0 - 0.1])) as Shape,
      disc(bore(S08), g0 - 0.1, 2.95, AT_CROWN), disc(bore(S06), g1 - 0.66, g1 + 0.1, clickAt)], // tapped for the crown wheel screw and the click screw
    barrelScrews,
  );
  const trainBridge = slab(
    [[-20, 10.5], [-4.6, 10.5], [-4.6, 3.4], [-2.9, -1.0], [-4.9, -2.2], [-12.5, -8], [-20, -8]],
    [AT_THIRD, AT_FOURTH, AT_ESCAPE].map((c) => disc(0.55, g0 - 0.1, g1 + 0.1, c)),
    trainScrews,
  );
  add('bridge', 3, barrelBridge, trainBridge);

  // Balance cock: a foot at the rim and an arm to the balance staff.
  const foot = COCK_FOOT;
  const armDir = STUD_DIR;
  const side = (c: P2, w: number, s: 1 | -1): P2 => polar(c, w, armDir + (s * Math.PI) / 2);
  const head = Array.from({ length: 13 }, (_, i): P2 => polar(AT_BALANCE, 1.9, armDir - (130 * Math.PI) / 180 - (i * 100 * Math.PI) / 180 / 12));
  const cockTop = prism([side(AT_BALANCE, 1.5, 1), side(foot, 2.6, 1), side(polar(foot, 2.6, armDir), 2.6, 1), side(polar(foot, 2.6, armDir), 2.6, -1), side(foot, 2.6, -1), side(AT_BALANCE, 1.5, -1), ...head], g0, g1);
  const cock = minus(fused([bevel(own(unwrap(intersect(cockTop, disc(R - 0.15, g0, g1)))) as Shape, g1), own(unwrap(intersect(prism([side(polar(foot, -1.6, armDir), 2.6, 1), side(polar(foot, 2.6, armDir), 2.6, 1), side(polar(foot, 2.6, armDir), 2.6, -1), side(polar(foot, -1.6, armDir), 2.6, -1)], 0, g0), disc(R - 0.15, 0, g0)))) as Shape]), [disc(0.7, g0 - 0.1, g1 + 0.1, AT_BALANCE), through(S08, cockScrew, -0.1, g1 + 0.1)]);
  add('bridge', 3, cock);
  add('steel', 3, disc(0.22, 2.0, g0, polar(AT_BALANCE, HAIRSPRING.r1 + 0.25, STUD_DIR))); // the stud that holds the hairspring
  // Regulator: a pointer on the cock (display shape).
  add('steel', 3, at(minus(fused([disc(1.25, g1, g1 + 0.12), prism([[0, -0.3], [4.6, -0.12], [4.6, 0.12], [0, 0.3]], g1, g1 + 0.12)]), [disc(0.75, g1 - 0.1, g1 + 0.2)]), AT_BALANCE, 0, armDir + 0.12));

  // Pallet bridge: a low bar under the balance rim. Its foot is on the side away from the centre wheel.
  const across = TO_BALANCE + Math.PI / 2;
  const bar = (z0: number, z1: number, from: number, to: number): Shape =>
    prism([polar(polar(AT_PALLET, from, across), 1.0, TO_BALANCE), polar(polar(AT_PALLET, to, across), 1.0, TO_BALANCE), polar(polar(AT_PALLET, to, across), -1.0, TO_BALANCE), polar(polar(AT_PALLET, from, across), -1.0, TO_BALANCE)], z0, z1);
  add('bridge', 2.5, minus(fused([bar(0.95, 1.2, -5.7, 1.0), bar(0, 0.95, -5.7, -3.2)]), [disc(0.45, 0.9, 1.25, AT_PALLET), ...palletScrews.map((c) => through(S06, c, -0.1, 1.3))]));

  // ---- 17 jewels: a pair for each of five arbors, hole and cap jewels for the balance, the impulse jewel, two stones
  for (const c of [AT_CENTRE, AT_THIRD, AT_FOURTH, AT_ESCAPE]) {
    add('ruby', 3, jewel(c, g1 - 0.3, g1));
    add('ruby', 0, jewel(c, -0.3, 0));
  }
  add('ruby', 2.5, jewel(AT_PALLET, 0.95, 1.2, 0.45));
  add('ruby', 0, jewel(AT_PALLET, -0.3, 0));
  add('ruby', 3, jewel(AT_BALANCE, g0 + 0.05, g0 + 0.3, 0.6), at(disc(0.7, g1 - 0.2, g1), AT_BALANCE));
  add('ruby', 0, jewel(AT_BALANCE, -0.3, 0, 0.6), at(disc(0.6, -0.62, -0.42), AT_BALANCE));

  // ---- winding: ratchet wheel on the barrel arbor, crown wheel, click, stem and crown
  add('steel', 4, at(gear(WINDING.ratchet, WINDING.module, [2.92, 3.2], 0, 1.8), AT_BARREL));
  const crownA = meshAngle(AT_BARREL, 0, WINDING.ratchet, AT_CROWN, WINDING.crown);
  add('steel', 4, at(minus(gear(WINDING.crown, WINDING.module, [2.92, 3.2]), [disc(0.6, 2.8, 3.3)]), AT_CROWN, 0, crownA));
  add('steel', 4, at(minus(prism([[-0.6, -0.5], [2.1, -0.25], [2.3, 0.35], [-0.6, 0.5]], g1, g1 + 0.25), [through(S06, [0, 0], g1 - 0.1, g1 + 0.35)]), clickAt, 0, (-65 * Math.PI) / 180));
  // The stem carries the winding pinion and the sliding pinion. Their tooth counts are not published.
  const onStem = (g: Shape, x: number): Shape => own(translate(own(rotate(g, 90, { axis: [0, 1, 0] })), [x, 0, 0.9])) as Shape;
  add('steel', 1, fused([own(cylinder(0.6, CASE.crown - 8.5, { at: [8.5, 0, 0.9], axis: [1, 0, 0] })), onStem(gear(16, 0.2, [0, 0.9], 0, 0), 12.45), onStem(gear(12, 0.19, [0, 1.3], 0, 0), 9.5)]));

  // ---- motion works on the dial side: 12 to 1 from the cannon pinion to the hour wheel
  turn('centre', 'steel', -1, fused([gear(MOTION.cannon, MOTION.module, [-0.9, -0.72], 0.2), ring(0.5, 0.75, -2.75, -0.72)]));
  turn('minute', 'brass', -1, gear(MOTION.minute, MOTION.module, [-0.9, -0.76], -0.2, 0.5));
  turn('minute', 'steel', -1, fused([gear(MOTION.minutePinion, MOTION.hourModule, [-1.2, -0.92], 0.35), disc(0.25, -1.2, -0.7)]));
  turn('hour', 'brass', -1.5, minus(fused([gear(MOTION.hour, MOTION.hourModule, [-1.2, -1.0], -0.35), disc(1.15, -2.35, -1.0)]), [disc(0.8, -2.45, -0.9)]));
  turn('set1', 'steel', -1, gear(WINDING.setting, MOTION.module, [-0.9, -0.76], 0.2));
  turn('set2', 'steel', -1, gear(WINDING.setting, MOTION.module, [-0.9, -0.76], -0.2));

  // ---- screws. Each one has a slot, a shank and a true thread, and its hole has the mating thread. In the
  // exploded view a screw turns out of its thread, one turn for each pitch. into is the level of the tapped part.
  const fasten = (t: Thread, [body, thread]: [Shape, Shape], nut: Shape, c: P2, z: number, grip: number, tone: Tone, into: number): void => {
    const slotAt = (c[0] * 7 + c[1] * 3) % TAU;
    const turned = (slotAt + (((4 - into) * explode * LIFT) / t.pitch) * TAU) % TAU;
    add('blue', 4, at(body, c, z, turned), at(thread, c, z, turned));
    add(tone, into, at(nut, c, z - grip, (slotAt - (grip / t.pitch) * TAU) % TAU));
  };
  const bridgeScrew = slotted(S08, 0.75, g1 + 0.8, 0.8);
  const wheelScrew = slotted(S08, 0.95, 1.05, 0.7);
  const palletScrew = slotted(S06, 0.5, 1.8, 0.6);
  const clickScrew = slotted(S06, 0.5, 0.9, 0.65);
  const [deep, shallow, small] = [tapped(S08, 0.7), tapped(S08, 0.45), tapped(S06, 0.6)];
  for (const c of [...barrelScrews, ...trainScrews, cockScrew]) fasten(S08, bridgeScrew, deep, c, g1, g1, 'plate', 0);
  for (const c of palletScrews) fasten(S06, palletScrew, small, c, 1.2, 1.2, 'plate', 0);
  fasten(S08, wheelScrew, deep, AT_BARREL, 3.2, 0.35, 'steel', 1); // the ratchet wheel, on the barrel arbor
  fasten(S08, wheelScrew, shallow, AT_CROWN, 3.2, 0.3, 'bridge', 3); // the crown wheel, on the barrel bridge
  fasten(S06, clickScrew, small, clickAt, g1 + 0.25, 0.25, 'bridge', 3);

  // Gold settings round the four train jewels of the bridges, and the scale of the regulator on the cock.
  add('brass', 3, ...[AT_CENTRE, AT_THIRD, AT_FOURTH, AT_ESCAPE].map((c) => at(ring(0.55, 0.85, g1, g1 + 0.04), c)));
  add('ink', 3, ...[-3, -2, -1, 0, 1, 2, 3].map((i) => at(own(translate(own(box(i === 0 ? 0.55 : 0.35, 0.07, 0.02)), [4.75, -0.035, g1])) as Shape, AT_BALANCE, 0, armDir + 0.12 + i * 0.09)));
  // Setting parts under the dial: the setting lever, the yoke and its spring (display shapes).
  add('steel', -1, prism([[14.5, 1.2], [16.5, 2.2], [16.2, 4.2], [12.5, 3.2], [11.2, 5.0], [10.4, 4.4], [12.0, 2.0]], -1.49, -1.37));
  add('steel', -1, prism([[10.2, -1.0], [13.8, -1.6], [14.6, -4.2], [13.4, -4.6], [12.6, -2.6], [9.8, -2.4], [8.2, -5.2], [7.4, -4.6], [9.0, -1.4]], -1.49, -1.37));
  add('steel', -1, prism([[15.6, -5.6], [16.4, -5.2], [14.6, -0.6], [13.9, -0.9]], -1.49, -1.37));

  // ---- dial: a white disc with a recess for the small seconds. The marks are thin solids on its face.
  const { face: d0, back: d1 } = DIAL;
  add('dial', -3, minus(disc(DIAL.radius, d0, d1), [disc(1.3, d0 - 0.1, d1 + 0.1), disc(0.25, d0 - 0.1, d1 + 0.1, AT_FOURTH), disc(DIAL.seconds, d0 - 0.1, d0 + 0.08, AT_FOURTH)]));
  // A mark from a to b on a plane of the dial, as a flat bar.
  const mark = (a: P2, b: P2, width: number, z = d0): Shape =>
    at(own(translate(own(box(Math.hypot(b[0] - a[0], b[1] - a[1]), width, 0.03)), [0, -width / 2, z - 0.03])) as Shape, a, 0, Math.atan2(b[1] - a[1], b[0] - a[0]));
  const toSeconds = Math.atan2(-AT_FOURTH[1], -AT_FOURTH[0]); // on the small dial, 60 is toward the centre of the watch
  const marks: Shape[] = [
    ...Array.from({ length: 60 }, (_, i) => mark(polar([0, 0], i % 5 === 0 ? 15.92 : 16.35, (i * TAU) / 60), polar([0, 0], 16.88, (i * TAU) / 60), i % 5 === 0 ? 0.24 : 0.1)),
    ring(16.9, 16.99, d0 - 0.03, d0),
    ring(15.81, 15.9, d0 - 0.03, d0),
    ...Array.from({ length: 60 }, (_, i) => mark(polar(AT_FOURTH, i % 5 === 0 ? 3.1 : 3.5, toSeconds + (i * TAU) / 60), polar(AT_FOURTH, 3.79, toSeconds + (i * TAU) / 60), i % 5 === 0 ? 0.12 : 0.05, d0 + 0.08)),
    at(ring(3.8, 3.86, d0 + 0.05, d0 + 0.08), AT_FOURTH),
  ];
  // Roman numerals with their feet toward the centre. A numeral is a row of strokes between two thin bars.
  // The small seconds takes the place of VI.
  const half = 1.45;
  const GLYPH: Record<string, { strokes: [number, number, number, number, number][]; advance: number }> = {
    I: { strokes: [[0.4, half, 0.4, -half, 0.42]], advance: 0.8 },
    V: { strokes: [[0.1, half, 0.75, -half, 0.42], [1.4, half, 0.75, -half, 0.16]], advance: 1.5 },
    X: { strokes: [[0.1, half, 1.4, -half, 0.42], [1.4, half, 0.1, -half, 0.16]], advance: 1.5 },
  };
  ['XII', 'I', 'II', 'III', 'IIII', 'V', '', 'VII', 'VIII', 'IX', 'X', 'XI'].forEach((numeral, hour) => {
    if (numeral === '') return;
    const angle = (hour * TAU) / 12;
    const width = [...numeral].reduce((sum, c) => sum + (GLYPH[c] as (typeof GLYPH)[string]).advance, 0);
    // u goes to the right of a reader at the dial side, v goes away from the centre.
    const point = (u: number, v: number): P2 => polar(polar([0, 0], DIAL.numerals + v, angle), u, angle + TAU / 4);
    let u = -width / 2;
    const strokes: Shape[] = [];
    for (const c of numeral) {
      const glyph = GLYPH[c] as (typeof GLYPH)[string];
      for (const [u0, v0, u1, v1, w] of glyph.strokes) strokes.push(mark(point(u + u0, v0), point(u + u1, v1), w));
      u += glyph.advance;
    }
    for (const v of [half, -half]) strokes.push(mark(point(-width / 2 - 0.12, v), point(width / 2 + 0.12, v), 0.13));
    marks.push(fused(strokes));
  });
  add('ink', -3, ...marks);

  // ---- hands. Each hand sits on its mobile at the angle that shows the start time at time = 0.
  const A0 = angles(0);
  const clock = { fourth: START.seconds / 60, centre: (START.minutes + START.seconds / 60) / 60, hour: (START.hours + START.minutes / 60 + START.seconds / 3600) / 12 };
  const hand = (on: 'fourth' | 'centre' | 'hour', outline: P2[], hub: number, hole: number, z0: number, z1: number): void => {
    const zero = on === 'fourth' ? toSeconds : 0;
    const both = [...outline, ...outline.map(([x, y]): P2 => [x, -y]).reverse()];
    const shape = minus(fused([prism(both, z0, z1), disc(hub, z0, z1)]), [disc(hole, z0 - 0.1, z1 + 0.1)]);
    turn(on, 'blue', -4, own(rotate(shape, ((zero + clock[on] * TAU - A0[on]) * 360) / TAU)) as Shape);
  };
  hand('hour', [[-2.0, 0.3], [0, 0.85], [6.8, 0.55], [7.7, 1.25], [10.4, 0.02]], 1.7, 1.15, -2.3, -2.18);
  hand('centre', [[-2.4, 0.24], [0, 0.62], [11.5, 0.4], [15.6, 0.02]], 1.3, 0.75, -2.7, -2.58);
  hand('fourth', [[-1.2, 0.1], [0, 0.26], [3.5, 0.02]], 0.32, 0.1, -2.1, -2.02);

  // ---- case: a band with the pendant, a bezel with the crystal, a back, the crown and the bow (display shapes)
  const lathe = (profile: P2[]): Shape => own(unwrap(revolve(own(unwrap(polygon(profile.map(([r, z]): P3 => [r, 0, z]))))))) as Shape;
  const pendant = fused([own(cylinder(2.3, 3.6, { at: [21.0, 0, 0.9], axis: [1, 0, 0] })), own(cylinder(2.7, 0.5, { at: [24.1, 0, 0.9], axis: [1, 0, 0] }))]);
  const band = fused([lathe([[18.45, -2.2], [20.3, -2.2], [21.3, -0.8], [21.5, 0.9], [21.3, 2.6], [20.3, 4.0], [18.45, 4.0]]), pendant]);
  add('case', 0, minus(band, [own(cylinder(0.8, 9, { at: [17, 0, 0.9], axis: [1, 0, 0] }))]));
  add('case', -5, lathe([[17.3, -2.2], [20.3, -2.2], [19.8, -3.1], [18.3, -3.6], [17.3, -3.6]]));
  add('glass', -5, disc(17.3, -3.5, -2.95));
  add('case', 6, fused([lathe([[0.03, 4.75], [14, 4.6], [18.45, 4.0], [20.3, 4.0], [19.9, 4.7], [16, 5.4], [8, 5.85], [0.03, 5.95]]), disc(0.06, 4.76, 5.94)]));
  add('case', 0, own(translate(own(rotate(gear(26, 0.22, [0, 2.8], 0, 0), 90, { axis: [0, 1, 0] })), [CASE.crown, 0, 0.9])) as Shape); // the gear teeth are the grip of the crown
  add('case', 0, minus(own(torus(5.86, 0.65, { at: [28.2, 0, 0.9] })), [own(cylinder(2.7, 6, { at: [20, 0, 0.9], axis: [1, 0, 0] }))]));

  return parts;
}

interface WatchParams {
  time?: number; // seconds of running
  explode?: number; // 0 = assembled, 1 = the levels lift apart
}

const watch = ({ time = 0, explode = 0 }: WatchParams = {}) => freeing(() => {
  const parts = movementParts(time, explode);
  const tones: Tone[] = ['plate', 'bridge', 'brass', 'steel', 'blue', 'ruby', 'dial', 'ink', 'case', 'glass'];
  const built = Object.fromEntries(
    tones.map((tone) => [
      tone,
      compound(parts.filter((p) => p.tone === tone).map((p) => (explode > 0 ? (own(translate(p.shape, [0, 0, p.lift * explode * LIFT])) as Shape) : p.shape))) as Shape,
    ]),
  ) as Record<Tone, Shape>;
  const fourthTurnsPerHour = (CENTRE.teeth / THIRD.leaves) * (THIRD.teeth / FOURTH.leaves);
  return {
    ...built,
    solids: parts.length,
    jewels: parts.filter((p) => p.tone === 'ruby').length,
    beatsPerHour: fourthTurnsPerHour * (FOURTH.teeth / ESCAPE.leaves) * ESCAPE.teeth * 2,
    hoursPerBarrelTurn: BARREL.teeth / CENTRE.leaves,
  };
});

// Show the dial side. Try watch({ time: 1800 }) for the pose after half an hour, or watch({ explode: 1 }).
const movement = watch();
// Turn the dial up. The crystal (movement.glass) stays out of the view, because the viewer draws solid colours.
const show = (s: Shape, css: string) => {
  const turned = rotate(s, 180, { axis: [1, 0, 0] });
  s.delete();
  return color(turned, css);
};
movement.glass.delete();
console.log(movement.solids, 'solids,', movement.jewels, 'jewels,', movement.beatsPerHour, 'vibrations per hour, one barrel turn in', movement.hoursPerBarrelTurn.toFixed(2), 'h');
export default [
  show(movement.plate, '#d9d6cf'),
  show(movement.bridge, '#e9e6de'),
  show(movement.brass, '#d8a943'),
  show(movement.steel, '#a9b1ba'),
  show(movement.blue, '#2f4fa8'),
  show(movement.ruby, '#c2185b'),
  show(movement.dial, '#f4f1e6'),
  show(movement.ink, '#16181c'),
  show(movement.case, '#cfd2d6'),
];

Functions used ​

box chamfer compound cut cutAll cylinder edgeFinder extrude face fillet fuseAll getBounds interpolateCurve intersect line loft makeExternalGear polygon revolve rotate threePointArc torus translate unwrap wire color

▶ Run Pocket Watch in the playground

Released under the Apache 2.0 License.