337c802689
Memory. The kernel build frees nothing when most of its things are deleted (one do-nothing function stands in for their destructors): a deleted handle still counted as a user of its shape, so no body the engine made was ever freed, and each tool kept what it had worked out. Now each is told to let go before it is deleted (release.ts): shape handles emptied, explorers pointed at nothing, booleans cleared and given a last tiny job so they drop their bodies. The validity check, which left over half a megabyte behind per check of a 35-face part, is pointed at nothing before it goes. A body's copy is written out and read back (the kernel's copier keeps both bodies for good). Rebuilding a hole 1500 times took the engine from 100 MB to 595 MB, now to 199 MB; a face pushed on a 44-face plate 400 times, 1871 MB, now 811 MB; a shell on it 200 times broke the engine, now it doesn't. Meshing. A body is meshed a few hundred faces at a time (meshing.ts): the kernel's mesher kept working notes on every face and edge at once - a body of 4 500 faces took 1.3 GB, one of 40 000 the 4 GB a browser allows. Same triangles, same order. STL is written from that mesh, of a copy of the body (the print mesh no longer stays on the model). A face's own triangles are made for that face alone, not by meshing its whole body. STL and 3MF meshes in. A closed mesh comes in as one solid, its flat triangles merged (the reader hands back loose triangles; every file came in as loose faces until Repaired, a 12-triangle block too). A binary file is told from a text one by its length (a blank heading and round sizes made the reader take it for text: a 10 mm cube out of a 3MF couldn't be read). Up to 250 000 triangles; more is refused at once with the count (it used to run the engine out of memory). A broken-down engine is no longer taken for an unreadable file. Sweep. Facing the same way along a path that runs in the profile's own plane never returned; a profile that changes size or turns left a sweep in pieces at a sharp corner: both refused in words. Joins and cuts made by a step go through one checked boolean. An unknown mode is a damaged step, not a step that does nothing. Tests: gridloft (340), gridio (51: up to a face or plane, STEP and STL out and back, meshes in, split by a body, copies, memory over 400 rebuilds). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
108 lines
5.7 KiB
TypeScript
108 lines
5.7 KiB
TypeScript
// Shared pieces of the tests: the geometry engine started once, pass/fail
|
|
// counting, and quick ways to write sketches, shapes and steps.
|
|
//
|
|
// import { check, done, sketch, rect, circle, extrude, XY, after } from "./kit";
|
|
// const fs = [sketch("s", XY(), [rect("r", 0, 0, 10, 10)]), extrude("E", "s", 5)];
|
|
// const { body, err } = await after(fs, "E");
|
|
// check("a 10 x 10 x 5 block", !err && Math.abs(volume(body) - 500) < 1e-6);
|
|
// done();
|
|
import opencascade from "replicad-opencascadejs";
|
|
import { measureVolume, setOC, type Shape3D } from "replicad";
|
|
import { debugBodies, healthReport, regenerate } from "../src/kernel/engine";
|
|
import { arrangement, regionRef } from "../src/sketch/regions";
|
|
import type { Feature, PlaneDef, SketchEntity, Vec3 } from "../src/doc/types";
|
|
import type { BodyHealth, RegenResult } from "../src/kernel/protocol";
|
|
|
|
setOC(await (opencascade as unknown as () => Promise<any>)());
|
|
|
|
// ------------------------------------------------------------------ results
|
|
|
|
let failed = 0;
|
|
let passed = 0;
|
|
|
|
/** One check: printed as ok or FAIL, with what was found when it fails (or always, if given). */
|
|
export function check(name: string, ok: boolean, info: unknown = ""): boolean {
|
|
if (ok) passed++;
|
|
else failed++;
|
|
console.log(ok ? "ok " : "FAIL", name, typeof info === "string" ? info : JSON.stringify(info));
|
|
return ok;
|
|
}
|
|
|
|
/** Near enough: |a - b| <= tol. */
|
|
export const near = (a: number, b: number, tol = 1e-6) => Math.abs(a - b) <= tol;
|
|
|
|
/** The end of a test file: the count, and the exit code. */
|
|
export function done(): never {
|
|
console.log(failed ? `${failed} failed, ${passed} passed` : `all ${passed} passed`);
|
|
process.exit(failed ? 1 : 0);
|
|
}
|
|
|
|
// ------------------------------------------------------------------ planes
|
|
|
|
export const XY = (z = 0): PlaneDef => ({ origin: [0, 0, z], xDir: [1, 0, 0], normal: [0, 0, 1] });
|
|
/** Seen from the front: x to the right, z up. */
|
|
export const XZ = (y = 0): PlaneDef => ({ origin: [0, y, 0], xDir: [1, 0, 0], normal: [0, -1, 0] });
|
|
export const YZ = (x = 0): PlaneDef => ({ origin: [x, 0, 0], xDir: [0, 1, 0], normal: [1, 0, 0] });
|
|
export const plane = (origin: Vec3, normal: Vec3, xDir: Vec3): PlaneDef => ({ origin, normal, xDir });
|
|
|
|
// ------------------------------------------------------------------ sketches
|
|
|
|
export const rect = (id: string, x0: number, y0: number, x1: number, y1: number): SketchEntity =>
|
|
({ id, kind: "polyline", points: [[x0, y0], [x1, y0], [x1, y1], [x0, y1]], closed: true }) as SketchEntity;
|
|
export const circle = (id: string, x: number, y: number, r: number): SketchEntity => ({ id, kind: "circle", center: [x, y], radius: r }) as SketchEntity;
|
|
export const line = (id: string, a: [number, number], b: [number, number]): SketchEntity => ({ id, kind: "line", a, b }) as SketchEntity;
|
|
export const poly = (id: string, points: [number, number][], closed = true): SketchEntity => ({ id, kind: "polyline", points, closed }) as SketchEntity;
|
|
|
|
export const sketch = (id: string, plane: PlaneDef, entities: SketchEntity[], name = id): Feature => ({ id, name, type: "sketch", plane, entities }) as Feature;
|
|
|
|
/** Every closed shape of the sketch's curves (or those `pick` keeps). */
|
|
export function profile(sketchId: string, entities: SketchEntity[], pick?: (regions: ReturnType<typeof regionRef>[]) => ReturnType<typeof regionRef>[]) {
|
|
const regions = arrangement(entities).regions.map(regionRef);
|
|
return { kind: "profile" as const, sketch: sketchId, regions: pick ? pick(regions) : regions };
|
|
}
|
|
|
|
/** The entities of a sketch step made above. */
|
|
const entitiesOf = (fs: Feature[], id: string) => (fs.find((f) => f.id === id) as { entities: SketchEntity[] } | undefined)?.entities ?? [];
|
|
|
|
// ------------------------------------------------------------------ steps
|
|
|
|
/** An extrude of the sketch's shapes (all of them, given its entities or the steps holding it). */
|
|
export function extrude(id: string, sk: Feature | string, distance: number, mode: "new" | "join" | "cut" | "intersect" | "auto" = "new", extra: Record<string, unknown> = {}): Feature {
|
|
const sid = typeof sk === "string" ? sk : sk.id;
|
|
const es = typeof sk === "string" ? [] : (sk as { entities: SketchEntity[] }).entities;
|
|
return { id, name: id, type: "extrude", source: profile(sid, es), distance, mode, ...extra } as Feature;
|
|
}
|
|
|
|
/** A block w x d x h from the origin (a sketch and its extrude). */
|
|
export function block(id: string, w: number, d: number, h: number, at: Vec3 = [0, 0, 0]): Feature[] {
|
|
const s = sketch(`${id}-s`, XY(at[2]), [rect(`${id}-r`, at[0], at[1], at[0] + w, at[1] + d)]);
|
|
return [s, extrude(id, s, h)];
|
|
}
|
|
|
|
/** A cylinder of radius r and height h standing on the XY plane at (x, y). */
|
|
export function cylinder(id: string, r: number, h: number, x = 0, y = 0, z = 0): Feature[] {
|
|
const s = sketch(`${id}-s`, XY(z), [circle(`${id}-c`, x, y, r)]);
|
|
return [s, extrude(id, s, h)];
|
|
}
|
|
|
|
// ------------------------------------------------------------------ results
|
|
|
|
/** Rebuild the steps: the result, the solid of one body (by its id), and the first error. */
|
|
export async function after(fs: Feature[], bodyId?: string): Promise<{ r: RegenResult; body: Shape3D | undefined; err?: string; errors: string[] }> {
|
|
const r = await regenerate(fs);
|
|
const bodies = debugBodies();
|
|
const body = bodyId ? bodies.get(bodyId) : [...bodies.values()][0];
|
|
const errors = r.errors.map((e) => `${e.featureId}: ${e.message}`);
|
|
return { r, body, err: r.errors[0]?.message, errors };
|
|
}
|
|
|
|
/** The solids of the last rebuild, by body id. */
|
|
export const bodies = () => debugBodies();
|
|
|
|
export const volume = (s: Shape3D | undefined) => (s ? measureVolume(s) : 0);
|
|
|
|
/** A body's health: valid, closed, solids, faces, volume, box. */
|
|
export const health = (s: Shape3D, id = "body"): BodyHealth => healthReport(id, id, s);
|
|
|
|
export { entitiesOf };
|