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

261 lines
11 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Subtract / Intersect as a tool: the body to change (+) and the bodies to
// change it with (−), picked in turn; keep either side if wanted.
import type { BooleanFeature, Vec3 } from "../doc/types";
import { add, scale } from "../geom";
import type { PanelItem } from "../ui/ui";
import { DimWidget } from "./annotate";
import { FeatureOp, type Slot } from "./featureOp";
import { bodyCenter } from "./ops";
import type { AppContext } from "./tool";
const cap = (s: string) => s[0].toUpperCase() + s.slice(1);
export class BooleanOp extends FeatureOp<BooleanFeature> {
readonly id = "boolean";
private marks: HTMLDivElement[] = [];
private centers = new Map<string, Vec3>();
private hook = () => this.placeMarks();
constructor(app: AppContext, op: "subtract" | "intersect", target = "", tools: string[] = [], existing?: BooleanFeature) {
super(app, { ...app.store.newIdentity("boolean", cap(op)), type: "boolean", op, target, tools: tools.filter((t) => t !== target) }, existing);
const sub = this.feature.op === "subtract";
// Not this subtract's own result: with "Keep modified body" on, its preview
// shows beside the original, and a tap on it made it one of its own tools.
const own = (b: string) => b === this.feature.id || b.startsWith(`${this.feature.id}:`);
const bodyOf = (hit: { kind: string; body?: string }) => {
const b = hit.kind === "body" || hit.kind === "face" || hit.kind === "edge" || hit.kind === "vertex" ? hit.body ?? null : null;
return b && !own(b) ? b : null;
};
const targetSlot: Slot = {
label: sub ? "Remove from" : "Body",
describe: () => (this.feature.target ? this.nameOf(this.feature.target) : "Tap a body"),
filled: () => !!this.feature.target,
pick: { profiles: false, planes: false, axes: false },
shows: () => (this.feature.target ? [{ kind: "body", body: this.feature.target }] : []),
accept: (hit) => {
const b = bodyOf(hit);
if (!b) return false;
this.feature.target = b;
this.feature.tools = this.feature.tools.filter((t) => t !== b);
// (Worked out again for the bodies as now picked.)
this.feature.whole = undefined;
return true;
},
};
const toolsSlot: Slot = {
label: sub ? "Subtract" : "With",
multi: true,
describe: () => (this.feature.tools.length ? `${this.feature.tools.length} bod${this.feature.tools.length > 1 ? "ies" : "y"}` : "Tap bodies"),
filled: () => this.feature.tools.length > 0,
pick: { profiles: false, planes: false, axes: false },
shows: () => this.feature.tools.map((body) => ({ kind: "body" as const, body })),
accept: (hit) => {
const b = bodyOf(hit);
if (!b || b === this.feature.target) return false;
const t = this.feature.tools;
this.feature.tools = t.includes(b) ? t.filter((x) => x !== b) : [...t, b];
this.feature.whole = undefined;
return true;
},
};
this.slots = [targetSlot, toolsSlot];
}
/** Names seen (while its preview is being made, a body can be missing from the list). */
private names = new Map<string, string>();
private nameOf(id: string): string {
const name = this.app.store.doc.names?.[id] ?? this.app.view.bodyList.find((b) => b.id === id)?.name ?? this.names.get(id);
if (name) this.names.set(id, name);
return name ?? "Body";
}
protected hint(): string {
return this.feature.op === "subtract"
? "Tap the body to cut, then the bodies to cut away from it."
: "Tap a body, then the bodies it overlaps. Only the part they share is kept.";
}
/** Which bodies give way to the clearance, named after the bodies picked. */
private sides(): { id: string; label: string }[] {
const t = this.feature.tools;
return [
{ id: "body", label: this.feature.target ? this.nameOf(this.feature.target) : "Body" },
{ id: "tools", label: t.length === 1 ? this.nameOf(t[0]) : "The others" },
{ id: "all", label: t.length > 1 ? "All" : "Both" },
];
}
private setSide(id: string): void {
this.feature.cutFrom = id === "body" ? undefined : (id as "tools" | "all");
this.render();
this.update();
}
private setClearance(v: number): void {
if (!(v >= 0)) return;
this.feature.clearance = v || undefined;
this.app.ui.updatePanelValues([v]);
this.dim?.setValue(v);
this.update();
}
protected items(): PanelItem[] {
const f = this.feature;
const sub = f.op === "subtract";
const gap: PanelItem[] = sub
? [
{ ...this.num("Clearance", "clearance", "length", { min: 0, onInput: (v) => this.setClearance(v), inBox: true }), value: f.clearance ?? 0 } as PanelItem,
{ type: "choice", label: "Cut from", options: this.sides(), value: f.cutFrom ?? "body", onChange: (id) => this.setSide(id) },
]
: [];
// The others give way (or all do): every body stays, changed.
if (sub && f.cutFrom) return gap;
return [
...gap,
{ type: "toggle", label: "Keep modified body", value: !!f.keepTarget, onChange: (v) => ((f.keepTarget = v || undefined), this.render(), this.update()) },
{ type: "toggle", label: sub ? "Keep subtracted bodies" : "Keep intersecting bodies", value: !!f.keepTools, onChange: (v) => ((f.keepTools = v || undefined), this.render(), this.update()) },
];
}
enter(): void {
super.enter();
this.app.view.frameHooks.add(this.hook);
this.placeMarks();
}
exit(): void {
this.dim?.dispose();
this.dim = null;
this.app.view.frameHooks.delete(this.hook);
for (const m of this.marks) m.remove();
this.marks = [];
super.exit();
}
/** The clearance, typed between the body and the first body it's subtracted with. */
private dim: DimWidget | null = null;
/** The names in its drop-down (made again when the bodies picked change). */
private dimSides = "";
protected onDraftChanged(): void {
this.placeMarks();
const f = this.feature;
const a = this.centers.get(f.target), b = f.tools[0] ? this.centers.get(f.tools[0]) : undefined;
const sides = this.sides();
const key = JSON.stringify(sides);
if (f.op !== "subtract" || !a || !b || key !== this.dimSides) {
this.dim?.dispose();
this.dim = null;
}
if (f.op !== "subtract" || !a || !b) return;
this.dimSides = key;
this.dim ??= new DimWidget(this.app.view, {
noLine: true,
value: f.clearance ?? 0,
modes: sides,
mode: f.cutFrom ?? "body",
onMode: (id) => this.setSide(id),
onInput: (v, text) => {
if (!(v >= 0)) return;
this.typedInto("clearance", text);
this.setClearance(v);
},
onCommit: () => this.done(),
});
const [p, q] = this.boxAt(scale(add(a, b), 0.5));
this.boxPoint = p;
this.dim.set(p, q);
this.dim.setMode(f.cutFrom ?? "body");
this.dim.setValue(f.clearance ?? 0);
// (The signs again, clear of the box just made.)
this.placeMarks();
}
/**
* The sign on `body` turned the other way: a "−" body becomes the one
* changed, the one that was takes its place among the others; the "+" body
* swaps with the first of the others.
*/
private swap(body: string): void {
const f = this.feature;
if (!f.target || !f.tools.length) return;
const other = body === f.target ? f.tools[0] : body;
if (!f.tools.includes(other)) return;
f.tools = f.tools.map((t) => (t === other ? f.target : t));
f.target = other;
f.whole = undefined;
this.render();
this.update();
this.placeMarks();
}
/** "+" on the body that's changed, "−" on each body it's changed with. */
/**
* Where the clearance box goes: between the two bodies, or - when that's
* over a body (a tray's middle, its pockets still to be tapped) - further
* down the screen, clear of them. (It sat on the pockets; its number pad,
* opened by a tap meant for one, covered half the tray.)
*/
private boxPoint: Vec3 | null = null;
private boxAt(mid: Vec3): [Vec3, Vec3] {
const view = this.app.view;
const s = view.toScreen(mid);
if (!s) return [mid, mid];
const over = (y: number) => [-90, 0, 90].some((dx) => view.pick(s[0] + dx, y, { edgeTolPx: 24, planes: false }));
if (!over(s[1])) return [mid, mid];
let y = s[1];
while (y < view.height - 160 && over(y)) y += 20;
if (over(y)) return [mid, mid];
const w = view.worldUnder(s[0], y).toArray() as Vec3;
return [w, w];
}
private placeMarks(): void {
const want: [string, string][] = [];
if (this.feature.target) want.push([this.feature.target, "+"]);
// Many taken away (eight pockets out of a tray): only the first keeps its
// sign - the rest, pushed aside to clear each other, landed on bodies still
// to be tapped, and a tap meant for a pocket swapped the tray out instead.
for (const t of this.feature.tools.length > 2 ? this.feature.tools.slice(0, 1) : this.feature.tools) want.push([t, "−"]);
while (this.marks.length > want.length) this.marks.pop()!.remove();
while (this.marks.length < want.length) {
const m = document.createElement("div");
m.className = "bool-mark";
// A tap on a sign turns it the other way round: picked in the wrong order, fixed in one tap.
m.addEventListener("pointerdown", (e) => e.stopPropagation());
m.addEventListener("click", (e) => {
e.stopPropagation();
if (m.dataset.body) this.swap(m.dataset.body);
});
document.body.append(m);
this.marks.push(m);
}
const r = this.app.view.canvas.getBoundingClientRect();
// (Bodies sharing a middle - a peg through a plate - get their signs side by side, each one tappable.)
const placed: [number, number][] = [];
// (And clear of the clearance's box, which sits between the first two bodies.)
const a = this.centers.get(this.feature.target), b = this.feature.tools[0] ? this.centers.get(this.feature.tools[0]) : undefined;
const box = this.dim && a && b ? this.app.view.toScreen(this.boxPoint ?? scale(add(a, b), 0.5)) : null;
want.forEach(([body, sign], i) => {
const m = this.marks[i];
m.dataset.body = body;
m.title = sign === "+" ? "Changed - tap to change it with this one instead" : "Changed with - tap to change this one instead";
m.classList.toggle("minus", sign !== "+");
// (Where the body was: in the preview a subtracted body is used up.)
if (this.app.view.bodyList.some((b) => b.id === body)) this.centers.set(body, bodyCenter(this.app, [body]));
const at = this.centers.get(body);
const c = at ? this.app.view.toScreen(at) : null;
m.hidden = !c;
if (!c) return;
let x = c[0], y = c[1];
if (box && Math.abs(box[0] - x) < 100 && Math.abs(box[1] - y) < 36) y = box[1] - 36;
while (placed.some(([px, py]) => Math.abs(px - x) < 30 && Math.abs(py - y) < 30)) x += 32;
placed.push([x, y]);
m.style.transform = `translate(${Math.round(r.left + x - 14)}px, ${Math.round(r.top + y - 14)}px)`;
});
}
}