835816fef1
Each operation tried over a grid of shapes, places and sizes, every result held to a volume worked out by hand. What that found: - A face with a hole in it pulled out: the hole was closed with a lid (the slab added took the face's outer outline only). Pushed in with a boss standing on it: refused, "would break the body into pieces". The slab now stops at each hole's own walls, and such a face goes the exact way first (a round hole stays one true cylinder). - Shell, where two faces are closer together than two walls (a pocket 1.7 mm from a hole, a hole 2 mm from a side): the pocket's walls were left out without a word (the pocket a window into the hollow), the tube round a hole in the opened face ended a wall and a half short of the opening, and a broken shell came out "repaired" a sixth too light. Shell now takes a result only if it is sound, has a wall behind every face it kept and nothing left deep inside, trying each way in turn; the opening is cut from the hollow's own floor. What still can't be built says why (walls would run into each other; a pocket in the opened face would be left loose). - A join where an edge of one body lay in a face of the other left that edge lying across the face, bounding nothing: taken for a gap, "would tear the body open". Such edges are taken out of every boolean's result. Tests: grid.ts and gridbooleans (65), gridextrude (152), gridfaces (122), gridfillet (126), gridshapes (98), gridshell (48), gridsizes (90). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
70 lines
3.3 KiB
TypeScript
70 lines
3.3 KiB
TypeScript
// Shared pieces of the grid tests (gridbooleans, gridextrude, gridfillet, gridshapes,
|
|
// gridfaces, gridsizes, shellclose): one operation tried over a grid of shapes, places
|
|
// and sizes, each result held to a volume worked out by hand and to being one sound solid.
|
|
//
|
|
// import { build, say, near, faceRef, edgeRefs } from "./grid";
|
|
// const g = await build([...steps]);
|
|
// say(!g.errors.length && !g.unsound.length && near(g.vol.A, 24000), "a block", `${g.errors} ${g.unsound} ${g.vol.A}`);
|
|
import { after, bodies, check, health } from "./kit";
|
|
import { measureArea, type Shape3D } from "replicad";
|
|
import type { Feature, Vec3 } from "../src/doc/types";
|
|
|
|
/** One case: ok or FAIL, with what was found when it fails. */
|
|
export const say = (ok: boolean, name: string, info: string) => check(name, ok, ok ? "" : info);
|
|
|
|
export interface Got {
|
|
/** The steps' errors ("step: message"). */
|
|
errors: string[];
|
|
ids: string[];
|
|
/** Each body's volume. */
|
|
vol: Record<string, number>;
|
|
/** What is wrong with the bodies (invalid, more solids than expected, open edges, hair-thin faces). */
|
|
unsound: string[];
|
|
}
|
|
|
|
/** Build the steps: each body's volume, and whether it is one sound solid (`pieces`: bodies meant to be more, or 0 for any number). */
|
|
export async function build(fs: Feature[], pieces: Record<string, number> = {}): Promise<Got> {
|
|
const { errors } = await after(fs);
|
|
const vol: Record<string, number> = {};
|
|
const unsound: string[] = [];
|
|
for (const [id, s] of bodies()) {
|
|
const h = health(s);
|
|
vol[id] = h.volume;
|
|
const want = pieces[id] ?? 1;
|
|
if (!h.valid) unsound.push(`${id} invalid`);
|
|
if (want && h.solids !== want) unsound.push(`${id}: ${h.solids} solids`);
|
|
if (h.openEdges) unsound.push(`${id}: ${h.openEdges} open edges`);
|
|
if (h.thin) unsound.push(`${id}: ${h.thin} thin faces`);
|
|
}
|
|
return { errors, ids: [...bodies().keys()], vol, unsound };
|
|
}
|
|
|
|
/** The same number, to a part in ten thousand (or `rel`). */
|
|
export const near = (a: number, b: number, rel = 1e-4, abs = 1e-6) => Math.abs(a - b) <= Math.max(abs, rel * Math.max(Math.abs(a), Math.abs(b)));
|
|
|
|
const unit = (v: Vec3): Vec3 => {
|
|
const l = Math.hypot(v[0], v[1], v[2]) || 1;
|
|
return [v[0] / l, v[1] / l, v[2] / l];
|
|
};
|
|
|
|
/**
|
|
* A face of a body, as the last build left it, by a test on its (unit) normal
|
|
* and its middle: the reference a step takes. Build the steps before it first -
|
|
* the faces are those of the last build.
|
|
*/
|
|
export function faceRef(body: string, pick: (n: Vec3, c: Vec3, f: any) => boolean) {
|
|
const s = bodies().get(body) as Shape3D;
|
|
// (replicad's normalAt isn't a unit vector on round faces.)
|
|
const f = s.faces.find((x) => pick(unit(x.normalAt().toTuple() as Vec3), x.center.toTuple() as Vec3, x));
|
|
if (!f) throw new Error(`no such face on ${body}`);
|
|
return { body, center: f.center.toTuple() as Vec3, normal: unit(f.normalAt().toTuple() as Vec3), area: measureArea(f) };
|
|
}
|
|
|
|
/** Edges of a body, as the last build left it, by a test on their middle and direction. */
|
|
export function edgeRefs(body: string, pick: (mid: Vec3, dir: Vec3, e: any) => boolean) {
|
|
const s = bodies().get(body) as Shape3D;
|
|
return s.edges
|
|
.filter((e) => pick(e.pointAt(0.5).toTuple() as Vec3, e.tangentAt(0.5).toTuple() as Vec3, e))
|
|
.map((e) => ({ body, mid: e.pointAt(0.5).toTuple() as Vec3, dir: e.tangentAt(0.5).toTuple() as Vec3, length: e.length as number }));
|
|
}
|