B-Rep-Koerper: Drehen/Skalieren/Spiegeln ueber transform-Spec (truck builder::transformed)

This commit is contained in:
karim committed 2026-10-23 13:33:58 +02:00
1 parent cc9ad85552
commit fad8a1bb06
5 files changed
+83 -16

No files matched your search

+2 -1
View File
@@ -111,7 +111,8 @@ export type SolidSpec =
| { kind: "cone"; cx: number; cy: number; z0: number; r: number; height: number }
| { kind: "torus"; center: [number, number, number]; major: number; minor: number }
| { kind: "boolean"; op: "union" | "difference" | "intersection"; a: SolidSpec; b: SolidSpec }
| { kind: "translate"; v: [number, number, number]; solid: SolidSpec };
| { kind: "translate"; v: [number, number, number]; solid: SolidSpec }
| { kind: "transform"; m: number[]; solid: SolidSpec };
/** Tesselliert einen B-Rep-Körper (Sehnenabweichung `tol` Meter). */
export async function buildSolidMesh(spec: SolidSpec, tol = 0.01): Promise<ExtrudedMesh> {
+19
View File
@@ -1,3 +1,4 @@
import { transformSolid } from "./extrudedSolidSpec";
import { describe, expect, it } from "vitest";
import type { ExtrudedSolid } from "../model/types";
import { extrudedSolidSpec, needsBrepPath } from "./extrudedSolidSpec";
@@ -48,3 +49,21 @@ describe("extrudedSolidSpec", () => {
});
});
});
describe("transformSolid", () => {
const body = {
id: "b", type: "extrudedSolid", points: [{ x: 0, y: 0 }, { x: 2, y: 0 }, { x: 2, y: 1 }, { x: 0, y: 1 }], height: 3,
brep: { spec: { kind: "cuboid", min: [0, 0, 0], max: [2, 1, 3] }, origin: { x: 0, y: 0 } },
} as never;
it("Drehung um 90° wickelt die Spezifikation in transform", () => {
const r = transformSolid(body, (p) => ({ x: -p.y, y: p.x }));
const sp = r.brep!.spec as { kind: string; m: number[] };
expect(sp.kind).toBe("transform");
expect(sp.m.slice(0, 2)).toEqual([0, -1]);
expect(r.brep!.origin).toEqual(r.points[0]);
});
it("Skalierung skaliert auch die Hoehe", () => {
const r = transformSolid(body, (p) => ({ x: p.x * 2, y: p.y * 2 }), 2);
expect(r.height).toBe(6);
});
});
+26
View File
@@ -74,3 +74,29 @@ export function extrudedSolidSpec(solid: ExtrudedSolid, baseZ: number): SolidSpe
}
return null;
}
/**
* Wendet eine Grundriss-Abbildung `fn` (Drehung/Skalierung/Spiegelung/Verschiebung, um Z)
* auf einen Körper an. B-Rep-Körper erhalten ihre Spezifikation als `transform` um die
* bisherige Verschiebung gewickelt (Z bleibt erhalten); der Ursprung wandert auf den
* neuen ersten Umrisspunkt, damit die Verschiebung wieder 0 ist.
*/
export function transformSolid(
solid: ExtrudedSolid,
fn: (p: { x: number; y: number }) => { x: number; y: number },
scale = 1,
): ExtrudedSolid {
const points = solid.points.map(fn);
const circle = solid.circle ? { ...solid.circle, center: fn(solid.circle.center), r: solid.circle.r * scale } : undefined;
const base: ExtrudedSolid = { ...solid, points, circle };
if (!solid.brep) return base;
const o = fn({ x: 0, y: 0 });
const ex = fn({ x: 1, y: 0 });
const ey = fn({ x: 0, y: 1 });
const p0 = solid.points[0];
const d: [number, number, number] = [p0 ? p0.x - solid.brep.origin.x : 0, p0 ? p0.y - solid.brep.origin.y : 0, 0];
const inner = { kind: "translate", v: d, solid: solid.brep.spec };
const m = [ex.x - o.x, ey.x - o.x, 0, o.x, ex.y - o.y, ey.y - o.y, 0, o.y, 0, 0, scale, 0];
// z-Skalierung gleich der Grundriss-Skalierung (gleichmässig); Spiegelung/Drehung lassen Z.
return { ...base, height: solid.height * scale, brep: { ...solid.brep, spec: { kind: "transform", m, solid: inner }, origin: points[0] ?? solid.brep.origin } };
}
+3 -15
View File
@@ -9,6 +9,7 @@
// Bezeichner englisch, UI-Text via t() (CONVENTIONS.md). Reine Funktionen + Geometrie;
// die App hält den State und wendet `commitTransform` immutabel an.
import { transformSolid } from "../plan/extrudedSolidSpec";
import type {
Ceiling,
Column,
@@ -711,15 +712,7 @@ export function commitTransform(
// nutzt) müssen mitziehen — sonst weicht das 3D-Volumen sichtbar von
// der Grundriss-Kontur ab (nur `center` mitzuführen genügt nicht).
const f1 = op === "scale" ? scaleFactor(pts, 1) : 1;
extrudedSolids = extrudedSolids.map((s) =>
s.id === sel.extrudedSolidId
? {
...s,
points: s.points.map(fn),
circle: s.circle ? { ...s.circle, center: fn(s.circle.center), r: s.circle.r * f1 } : undefined,
}
: s,
);
extrudedSolids = extrudedSolids.map((s) => (s.id === sel.extrudedSolidId ? transformSolid(s, fn, f1) : s));
}
if (sel.columnId) {
columns = columns.map((c) => (c.id === sel.columnId && !c.locked ? mvColumn(c, fn) : c));
@@ -775,12 +768,7 @@ export function commitTransform(
case "extrudedSolid": {
const s = item.solid;
const f = op === "scale" ? scaleFactor(pts, t) : 1;
newSolids.push({
...s,
id: uniqueId("extrude"),
points: s.points.map(fn),
circle: s.circle ? { ...s.circle, center: fn(s.circle.center), r: s.circle.r * f } : undefined,
});
newSolids.push({ ...transformSolid(s, fn, f), id: uniqueId("extrude") });
break;
}
}