Files
DOSSIER/src/plan/voids.ts
T

233 lines
9.3 KiB
TypeScript
Raw 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.
// Abzugskörper (`ExtrudedSolid.operation === "subtract"`): Aussparungen, Durchbrüche und
// Nischen, die sich von Wänden, Decken und Stützen abziehen — in ALLEN abgeleiteten
// Ansichten aus demselben Modell:
//
// • Grundriss: Poché-Polygone der getroffenen Bauteile minus Körper-Grundfläche, sofern der
// Körper die Schnitthöhe (Wände/Stützen) bzw. die Platte (Decken) durchdringt.
// • Schnitt: Cut-Polygone minus Körper-Schnitt (Grundfläche ∩ Schnittspur × Höhenbereich), im Kern
// (`core/src/plan/section.rs`).
// • 3D/STEP: Boolean-Differenz (truck, Mesh-CSG-Fallback), s. `toWalls3d/voids3d.ts`
// bzw. `exportStep.ts`.
//
// Abzugskörper sind senkrechte Prismen (Profil × Höhe, ohne Verjüngung/B-Rep) — damit bleiben
// Grundriss und Schnitt exakt analytisch (ein Prisma schneidet eine waagrechte Ebene in seiner
// Grundfläche und eine senkrechte Ebene in Rechtecken).
//
// Polygone mit Loch (Körper ganz im Bauteil) werden über einen Steg an den Aussenring
// gehängt (Schlüsselloch); die beiden Stegkanten sind nicht gestrichen (`noStrokeEdges`).
import type { Pair } from "../geometry/polygonBool";
import { core } from "../engine/coreWasm";
import type { ExtrudedSolid, Project, Vec2 } from "../model/types";
import { getFloor } from "../model/types";
import { ceilingVerticalExtent, columnVerticalExtent } from "../model/wall";
import type { Primitive } from "./generatePlan/shared";
import type { SolidSpec } from "../engine/truckSolid";
import type { RWall } from "./toWalls3d/shared";
export interface VoidPrism {
id: string;
pts: Vec2[];
z0: number;
z1: number;
}
const EPS = 1e-7;
/** Ob ein Körper als Abzugskörper wirkt (Prisma mit gültigem Profil). */
export function isVoidSolid(s: ExtrudedSolid): boolean {
return s.operation === "subtract" && !s.brep && (s.taper ?? 0) < 1e-6 && s.points.length >= 3 && s.height > 0;
}
/** UK des Körpers (Geschoss-UK + `baseOffset`). */
export function solidBaseZ(project: Project, s: ExtrudedSolid): number {
let base = 0;
try {
base = getFloor(project, s.levelId).baseElevation ?? 0;
} catch {
base = 0;
}
return base + (s.baseOffset ?? 0);
}
/** Alle Abzugskörper des Projekts (Welt-Höhen). */
export function voidPrisms(project: Project): VoidPrism[] {
const out: VoidPrism[] = [];
for (const s of project.extrudedSolids ?? []) {
if (!isVoidSolid(s)) continue;
const z0 = solidBaseZ(project, s);
out.push({ id: s.id, pts: s.points, z0, z1: z0 + s.height });
}
return out;
}
// ── Polygon-Booleans (Rust-Kern, `voids.rs`) ─────────────────────────────────
const ring = (pts: Vec2[]): Pair[] => pts.map((p) => [p.x, p.y] as Pair);
/** Ringliste flach für den Kern (`[n, (k, x, y …) × n]`). */
function encodeRings(rings: Pair[][]): Float64Array {
const b: number[] = [rings.length];
for (const r of rings) {
b.push(r.length);
for (const p of r) b.push(p[0], p[1]);
}
return Float64Array.from(b);
}
/**
* Polygon (Ring `pts`, optional nicht gestrichene Kanten) minus Schneider. Liefert die
* Rest-Polygone mit übertragenen `noStrokeEdges` (eine Kante bleibt ungestrichen, wenn sie
* auf einer ungestrichenen Kante des Originals liegt; Stege sind immer ungestrichen).
* `null` = keine Überschneidung (Original unverändert lassen).
*/
export function subtractRings(
pts: Pair[],
cutters: Pair[][],
noStroke: number[] = [],
): Array<{ pts: Pair[]; noStrokeEdges: number[] }> | null {
if (pts.length < 3 || cutters.length === 0) return null;
const f = core().voids_subtract_rings_flat(
encodeRings([pts]).subarray(1),
encodeRings(cutters),
Float64Array.from([noStroke.length, ...noStroke]),
);
if (f.length === 0 || f[0] < 0) return null;
const out: Array<{ pts: Pair[]; noStrokeEdges: number[] }> = [];
let i = 1;
for (let k = 0; k < f[0]; k++) {
const n = f[i++];
const rp = new Array<Pair>(n);
for (let j = 0; j < n; j++, i += 2) rp[j] = [f[i], f[i + 1]];
const m = f[i++];
const ns = new Array<number>(m);
for (let j = 0; j < m; j++) ns[j] = f[i++];
out.push({ pts: rp, noStrokeEdges: ns });
}
return out;
}
// ── Grundriss ────────────────────────────────────────────────────────────────
/** Teile der Strecke a→b ausserhalb aller Ringe, als Parameter-Intervalle [t0, t1]. */
export function segmentOutside(a: Vec2, b: Vec2, cutters: Pair[][]): Array<[number, number]> {
const f = core().voids_segment_outside_flat(Float64Array.from([a.x, a.y, b.x, b.y]), encodeRings(cutters));
const out: Array<[number, number]> = [];
for (let k = 0; k < (f[0] ?? 0); k++) out.push([f[1 + 2 * k], f[2 + 2 * k]]);
return out;
}
/** Linien-Klassen der Wand-Poché, die ein Abzugskörper auf Schnitthöhe unterbricht. */
const WALL_LINE_CLS = new Set(["wall-outline", "layer-joint", "layer-joint-l"]);
/**
* Zieht die Abzugskörper von den Bauteil-Polygonen eines Grundrisses ab (in place).
* Wände: wenn der Körper die Schnitthöhe `cutZ` durchdringt. Stützen: ebenso, über ihren
* Höhenbereich. Decken: wenn der Körper die Platte durchdringt (Deckendurchbruch). Liefert, ob
* Abzugskörper da waren (dann kann sich die Liste verändert haben).
*/
export function subtractVoidsInPlan(prims: Primitive[], project: Project, cutZ: number): boolean {
const voids = voidPrisms(project);
if (voids.length === 0) return false;
const ceilings = new Map((project.ceilings ?? []).map((c) => [c.id, c]));
const columns = new Map((project.columns ?? []).map((c) => [c.id, c]));
const cuttersFor = (z0: number, z1: number): Pair[][] =>
voids.filter((v) => v.z0 < z1 - EPS && v.z1 > z0 + EPS).map((v) => ring(v.pts));
const wallCutters = cuttersFor(cutZ - 1e-4, cutZ + 1e-4);
const out: Primitive[] = [];
for (const pr of prims) {
if (pr.kind === "line" && WALL_LINE_CLS.has(pr.cls) && wallCutters.length) {
const parts = segmentOutside(pr.a, pr.b, wallCutters);
const whole = parts.length === 1 && parts[0][0] === 0 && parts[0][1] === 1;
if (whole) out.push(pr);
else
for (const [t0, t1] of parts) {
const at = (t: number) => ({ x: pr.a.x + (pr.b.x - pr.a.x) * t, y: pr.a.y + (pr.b.y - pr.a.y) * t });
out.push({ ...pr, a: at(t0), b: at(t1) });
}
continue;
}
if (pr.kind !== "polygon" || pr.extrudedSolidId) {
out.push(pr);
continue;
}
let cutters: Pair[][] | null = null;
if (pr.wallId) cutters = wallCutters;
else if (pr.ceilingId) {
const c = ceilings.get(pr.ceilingId);
if (c) {
const { zBottom, zTop } = ceilingVerticalExtent(project, c);
cutters = cuttersFor(zBottom, zTop);
}
} else if (pr.columnId) {
const c = columns.get(pr.columnId);
if (c) {
const { zBottom, zTop } = columnVerticalExtent(project, c);
cutters = zBottom <= cutZ && zTop >= cutZ ? wallCutters : cuttersFor(zBottom, zTop);
}
}
if (!cutters || cutters.length === 0) {
out.push(pr);
continue;
}
const rest = subtractRings(ring(pr.pts), cutters, pr.noStrokeEdges ?? []);
if (rest === null) {
out.push(pr);
continue;
}
for (const r of rest) {
out.push({
...pr,
pts: r.pts.map(([x, y]) => ({ x, y })),
noStrokeEdges: r.noStrokeEdges.length ? r.noStrokeEdges : undefined,
});
}
}
prims.length = 0;
for (const p of out) prims.push(p);
return true;
}
// ── 3D / B-Rep ───────────────────────────────────────────────────────────────
export type Box2 = { x0: number; y0: number; x1: number; y1: number };
export const bbox = (pts: Array<{ x: number; y: number }>): Box2 => ({
x0: Math.min(...pts.map((p) => p.x)),
y0: Math.min(...pts.map((p) => p.y)),
x1: Math.max(...pts.map((p) => p.x)),
y1: Math.max(...pts.map((p) => p.y)),
});
const boxHit = (a: Box2, b: Box2) => a.x0 < b.x1 && b.x0 < a.x1 && a.y0 < b.y1 && b.y0 < a.y1;
export function wallCorners(w: RWall): Array<{ x: number; y: number }> {
const dx = w.end[0] - w.start[0];
const dy = w.end[1] - w.start[1];
const len = Math.hypot(dx, dy) || 1;
const nx = (-dy / len) * (w.thickness / 2);
const ny = (dx / len) * (w.thickness / 2);
return [
{ x: w.start[0] + nx, y: w.start[1] + ny },
{ x: w.end[0] + nx, y: w.end[1] + ny },
{ x: w.end[0] - nx, y: w.end[1] - ny },
{ x: w.start[0] - nx, y: w.start[1] - ny },
];
}
const voidSpec = (v: VoidPrism): SolidSpec => ({
kind: "extrude",
points: v.pts.flatMap((p) => [p.x, p.y]),
height: v.z1 - v.z0,
z0: v.z0,
});
/** Bauteil minus alle Körper (verkettete Differenzen). */
export function subtractSpec(base: SolidSpec, voids: VoidPrism[]): SolidSpec {
return voids.reduce<SolidSpec>((acc, v) => ({ kind: "boolean", op: "difference", a: acc, b: voidSpec(v) }), base);
}
/** Abzugskörper, die ein Prisma (Grundriss-Hülle + Höhenbereich) treffen. */
export function hittingVoids(voids: VoidPrism[], foot: Box2, z0: number, z1: number): VoidPrism[] {
return voids.filter((v) => v.z0 < z1 - 1e-6 && v.z1 > z0 + 1e-6 && boxHit(bbox(v.pts), foot));
}