Files
superGeometry 8de2367a0c Stress tests: a phone stand, an ice cube tray, a headphone hook, a spanner, a planter, a door handle
== A phone stand (plane at angle, offset edge, through-all cut) ==

A phone stand built by code and by hand on a phone, the two matching:
a base, a back leaning at 75 degrees on a plane through its top edge, a
lip raised from an Offset Edge line, a cable hole cut through all, the
lip chamfered and the back's top rounded. What it turned up:

- A join that only touches along an edge (the back stood on the base's
  edge) made two solids meeting at an edge and was called sound. Health
  now counts pinched edges (more than two faces round them) and such a
  body is not healthy; the join says "only touches the body along an
  edge" and to let them overlap.
- A rectangle's height typed 93 came out 92.999999999999 (the solver's
  float noise, shown, saved and built). Values a hair off a clean one
  are put on it when the constraints stay as well met.
- Cut with Through all on a sketch on a face went out into the air and
  cut nothing: with no way picked yet, a through-all cut now goes into
  the body (Flip direction still turns it).
- Once a chamfer's preview showed, its edge was gone from the view, so
  tapping it again could not un-pick it: a tap near a picked edge's own
  line now drops it.

== An ice cube tray (draft, two-way pattern, shell, lettering) ==

An ice cube tray built by code and by hand on a phone, the two the same:
one pocket pulled down with a 6 degree draft as a body of its own,
patterned four along and two across, all eight subtracted from the
block, the block shelled from below (walls 2 thick round every pocket),
the rim rounded, ICE typed on the front and raised. What it turned up:

- Subtract's clearance box sat between the first two bodies: over the
  middle of the tray, on the pockets still to be tapped, and a tap meant
  for one opened its number pad over half the tray. It moves down the
  screen, clear of the bodies.
- With many bodies taken away, the signs (to swap the bodies round)
  were pushed aside onto bodies still to be tapped, and a tap meant for
  a pocket swapped the tray out. Past two, only the first keeps a sign.
- A pattern running, its first body picked, the view's taps go to its
  direction: four pockets to pattern across could only be had by
  starting again from Items. Now a body tapped in Items joins the
  running pattern (or leaves it).

== A headphone hook (symmetric, up to a plane, thread) ==

A headphone hook built by code and by hand on a phone, the two the
same to the face: a C clamp pulled Symmetric, an arm pulled Up to a
plane 50 out, a lip raised and rounded, a screw hole cut up through the
jaw and threaded M7. What it turned up:

- With Symmetric on, the box still read Distance, and 30 typed for a
  clamp 30 wide made it 30 each way, 60 in all. The box reads "Each
  side" and the hint says to type half the whole width.
- Up to a plane with the distance box left at 0: Done did nothing.
  The distance only has to be set when the ends are a distance.
- Fillet: tapping a picked edge to drop it took the picked edge even
  when the tap was on another edge nearer; now the nearer real edge
  wins.
- The radius dot catches taps for some 40 px round it, so an edge
  near it could not be picked or dropped. A tap on it that doesn't
  move, away from the dot itself, now goes to the model.

== A spanner (pieces pulled up at once, polygon, corner rounds) ==

A spanner built by code and by hand on a phone, the two the same to the
face: two circles and a bar pulled up 6 as one, an open jaw cut through
all, a hexagon 10 across flats cut through the ring end, the inside
corners rounded and the hexagon chamfered. What it turned up:

- New project kept the last project's view: left looking up from below,
  a tap meant for the top picked the bottom face. A new project starts
  from the default view.
- Through all with Auto, on a sketch on a body's face, went up off the
  face: a tower 354 high was added instead of the jaw cut. Auto on a
  body's face now goes into it, as a cut does.
- The Polygon's Values only took a radius to a corner: a hex 10 across
  its flats meant typing 5.7735. It takes Across flats too, kept as the
  radius dimension that makes it.

== A planter (loft square to round, shell, circular sketch pattern) ==

A planter built by code and by hand on a phone, the two the same to the
face: lofted from a square foot to a round mouth on an Offset Plane,
shelled open at the mouth, four drain holes (one circle, patterned round
in the sketch) cut through the floor, the rim rounded. What it turned up:

- The loft leaves the mouth's circle in five pieces, where the square's
  corners run up to it. A tap on the rim picked one quarter, and its
  fillet could not be made ("would damage the body"). A tap on a piece
  of a circle now takes the whole circle, and a tap drops it whole.

== A door handle (sweep along line, arc, line; sketch mirror; countersinks) ==

A lever door handle built by code and by hand on a phone, the two the
same to the face: a plate with rounded corners, two screw holes (one
circle mirrored in the sketch) cut through and countersunk, a round bar
swept up from the plate along a line, a centre arc and a line, its end
rounded. What it turned up:

- Mirror (Copy) applied on the first tap on the axis, but kept the shape
  picked and its card still read "Apply (1)", so a second tap made the
  same copy again, on top of the first. A mirrored copy now lets go of
  the shape, and a copy exactly on a curve already there is not made
  ("Those copies are there already").

Health: a body where more than two faces meet on one edge (pieces
touching only along a line) is no longer called clean. The drill's
housing halves have one such seam each, where the grip runs into the
wall and the inside and outside meet; the drill test allows that one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 20:54:00 +02:00

120 lines
8.7 KiB
TypeScript

// Run: npx tsx tests/spanner.test.ts [out.json] - a spanner: an open jaw at
// one end, a hex ring at the other, a handle between. The shape is drawn as a
// beginner would, out of overlapping pieces (two circles and a bar) and
// pulled up all at once; the jaw cut through all with a rectangle; a hexagon
// (the Polygon tool) cut through the ring end; the inside corners where the
// bar meets the heads rounded; the hexagon's top edges chamfered. Each step
// held to its size and soundness.
import { readFileSync, writeFileSync } from "node:fs";
import { makeBox, type Shape3D } from "replicad";
import { after, bodies, check, circle, done, health, near, rect, sketch, XY } from "./kit";
import { measureVolume } from "../src/kernel/ocFree";
import { arrangement, regionRef } from "../src/sketch/regions";
import type { EdgeRef, Feature, SketchEntity, Vec3 } from "../src/doc/types";
const vol = (s: Shape3D) => Math.abs(measureVolume(s));
const body = (id: string) => bodies().get(id) as Shape3D;
const profileOf = (sk: string, es: SketchEntity[]) => ({ kind: "profile" as const, sketch: sk, regions: arrangement(es).regions.map(regionRef) });
const unit = (v: number[]): Vec3 => { const l = Math.hypot(v[0], v[1], v[2]) || 1; return [v[0] / l, v[1] / l, v[2] / l]; };
const edgesOf = (id: string, test: (m: Vec3, d: Vec3) => boolean): EdgeRef[] =>
body(id).edges.filter((e) => test(e.pointAt(0.5).toTuple() as Vec3, unit(e.tangentAt(0.5).toTuple()))).map((e) => ({ body: id, mid: e.pointAt(0.5).toTuple() as Vec3, dir: e.tangentAt(0.5).toTuple() as Vec3, length: e.length }));
/** Is there material round this point (a tiny box kept by an intersect)? */
const inside = (s: Shape3D, p: Vec3) => {
const probe = makeBox([p[0] - 0.05, p[1] - 0.05, p[2] - 0.05], [p[0] + 0.05, p[1] + 0.05, p[2] + 0.05]);
return Math.abs(measureVolume(s.intersect(probe) as Shape3D)) > 1e-6;
};
const T = 6; // thick
const JX = -70, JR = 16; // the jaw end: a head 32 across
const RX = 70, RR = 13; // the ring end: 26 across
const BAR = 6; // the bar: 12 wide (6 each side)
const JAW = 13; // the jaw's opening (13 across flats)
const HEX = 10; // the ring's hexagon: 10 across flats
const ROUND = 3, CHAMF = 0.5;
const hexR = HEX / Math.sqrt(3); // its corners' radius
// ----------------------------------------------------------------- the outline
// Two circles and a bar over both, every piece pulled up 6 at once.
const outEs = [circle("j", JX, 0, JR), circle("r", RX, 0, RR), rect("b", JX, -BAR, RX, BAR)];
const steps: Feature[] = [sketch("Outline", XY(), outEs), { id: "Spanner", name: "Spanner", type: "extrude", source: profileOf("Outline", outEs), distance: T, mode: "new" } as Feature];
let { errors } = await after(steps);
check("the outline: no errors, one body", !errors.length && bodies().size === 1, errors);
// The area: two discs and the bar between them (less what of it is in the discs).
// (The strip |y| < 6 over half a disc: the bar runs from each disc's centre inwards.)
const seg = (r: number) => r * r * Math.asin(BAR / r) + BAR * Math.sqrt(r * r - BAR * BAR);
const inJ = seg(JR), inR = seg(RR);
const area = Math.PI * JR * JR + Math.PI * RR * RR + 2 * BAR * (RX - JX) - inJ - inR;
const vOut = vol(body("Spanner"));
check("the outline: the discs and the bar, 6 thick", near(vOut, area * T, 1e-3), [vOut, area * T]);
// -------------------------------------------------------------------- the jaw
// A rectangle 13 across from inside the head out past its end, cut through all.
const jawEs = [rect("w", JX - JR - 4, -JAW / 2, JX + 2, JAW / 2)];
steps.push(sketch("Jaw", XY(T), jawEs), { id: "JawCut", name: "Extrude", type: "extrude", source: profileOf("Jaw", jawEs), distance: -1, extent: "through", mode: "cut", target: "Spanner" } as Feature);
({ errors } = await after(steps));
check("the jaw: no errors", !errors.length, errors);
const vJaw = vol(body("Spanner"));
check("the jaw: open (no material in its mouth), the handle still there", !inside(body("Spanner"), [JX - JR + 1, 0, T / 2]) && !inside(body("Spanner"), [JX, 0, T / 2]) && inside(body("Spanner"), [0, 0, T / 2]) && inside(body("Spanner"), [JX, JAW / 2 + 1, T / 2]), vOut - vJaw);
check("the jaw: one body still", bodies().size === 1 && health(body("Spanner"), "s").solids === 1, health(body("Spanner"), "s").solids);
// Auto (as a beginner leaves it), Through all into the body from its top face: a cut, the same.
{
const auto = steps.map((f) => (f.id === "JawCut" ? ({ ...f, mode: "auto" } as Feature) : f));
const { errors } = await after(auto);
check("the jaw with Auto, through all into the body: the same cut", !errors.length && near(vol(body("Spanner")), vJaw, 1e-6), [errors, vol(body("Spanner")), vJaw]);
await after(steps);
}
// -------------------------------------------------------------------- the ring
// A hexagon 10 across flats in the ring end, cut through all.
const hexEs = [{ id: "h", kind: "polygon", center: [RX, 0], radius: hexR, sides: 6, angle: 0 } as unknown as SketchEntity];
steps.push(sketch("Hex", XY(T), hexEs), { id: "HexCut", name: "Extrude", type: "extrude", source: profileOf("Hex", hexEs), distance: -1, extent: "through", mode: "cut", target: "Spanner" } as Feature);
({ errors } = await after(steps));
check("the ring: no errors", !errors.length, errors);
const vHex = vol(body("Spanner"));
check("the ring: a hexagon 10 across flats out of it, 6 deep", near(vJaw - vHex, ((3 * Math.sqrt(3)) / 2) * hexR * hexR * T, 1e-3), vJaw - vHex);
// ------------------------------------------------------------------- the edges
// The four inside corners where the bar meets the heads, rounded 3.
const xJ = JX + Math.sqrt(JR * JR - BAR * BAR), xR = RX - Math.sqrt(RR * RR - BAR * BAR);
const corners = edgesOf("Spanner", (m, d) => Math.abs(d[2]) > 0.99 && near(Math.abs(m[1]), BAR, 1e-6) && (near(m[0], xJ, 1e-6) || near(m[0], xR, 1e-6)));
steps.push({ id: "Corners", name: "Fillet", type: "fillet", edges: corners, radius: ROUND } as Feature);
({ errors } = await after(steps));
check("the corners: four, rounded, no errors", !errors.length && corners.length === 4, [errors, corners.length]);
const vRound = vol(body("Spanner"));
check("the corners: filled in a little (more material, not less)", vRound > vHex && vRound - vHex < 4 * ROUND * ROUND * T, vRound - vHex);
// The hexagon's six top edges, chamfered 0.5.
const hexTop = edgesOf("Spanner", (m) => near(m[2], T, 1e-6) && near(Math.hypot(m[0] - RX, m[1]), HEX / 2, 1e-6));
steps.push({ id: "HexEdges", name: "Chamfer", type: "chamfer", edges: hexTop, radius: CHAMF } as Feature);
({ errors } = await after(steps));
check("the hexagon's top: six edges chamfered, no errors", !errors.length && hexTop.length === 6, [errors, hexTop.length]);
const vCham = vol(body("Spanner"));
check("the hexagon's top: 0.5 x 0.5 off round it (6 sides, a little more at the corners)", vRound - vCham > 0.5 * CHAMF * CHAMF * 6 * hexR && vRound - vCham < 0.5 * CHAMF * CHAMF * 6 * (hexR + 1), vRound - vCham);
const h = health(body("Spanner"), "Spanner");
check("the spanner: sound, one body", h.healthy && bodies().size === 1, h.healthy ? `${Math.round(h.volume)} mm³` : h);
const codeVol = vol(body("Spanner"));
const codeFaces = body("Spanner").faces.length;
const codeBox = body("Spanner").boundingBox.bounds;
console.log("outline", vOut.toFixed(1), "jaw", vJaw.toFixed(1), "hex", vHex.toFixed(1), "round", vRound.toFixed(1), "final", codeVol.toFixed(1), "faces", codeFaces);
// ----------------------------------------------- the spanner built by hand
// On a phone-sized screen (tests/fixtures/spanner-hand.json): the two circles
// and the bar typed in Values, All shapes pulled up 6; on the head's top a
// rectangle typed, cut Through all (Auto: it went up into a tower 354 high,
// before); on the ring's top the Polygon tool, 10 typed Across flats (it
// only took a corner radius, before); the four inside corners picked from
// two sides and rounded 3; the hexagon's six top edges chamfered 0.5. It
// comes out the same.
{
const hand = JSON.parse(readFileSync(new URL("./fixtures/spanner-hand.json", import.meta.url), "utf8")).features as Feature[];
const { errors } = await after(hand);
check("by hand: no errors", errors.length === 0, errors);
const H = [...bodies().values()].map((s) => s as Shape3D);
check("by hand: one body, sound", H.length === 1 && health(H[0], "hand").healthy, H.length);
check("by hand: the same spanner (to 0.1 %)", H.length === 1 && Math.abs(vol(H[0]) - codeVol) < codeVol * 1e-3, `${H[0] ? vol(H[0]).toFixed(1) : "-"} / ${codeVol.toFixed(1)}`);
check("by hand: as many faces, the same box", H[0]?.faces.length === codeFaces && H[0].boundingBox.bounds.flat().every((x, i) => near(x, codeBox.flat()[i], 1e-3)), [H[0]?.faces.length, codeFaces, H[0]?.boundingBox.bounds]);
}
if (process.argv[2]) writeFileSync(process.argv[2], JSON.stringify({ format: "super", version: 2, name: "Spanner", features: steps }));
done();