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

+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 } };
}