diff --git a/src/plan/generatePlan.ts b/src/plan/generatePlan.ts index eb2da4a8..cb4685fc 100644 --- a/src/plan/generatePlan.ts +++ b/src/plan/generatePlan.ts @@ -929,11 +929,33 @@ export function generateSectionPlan( ceilingId: kind === "slab" && !cp.stairId ? sourceId : undefined, roofId: kind === "roof" ? sourceId : undefined, stairId: cp.stairId, + extrudedSolidId: kind === "body" ? sourceId : undefined, noStrokeEdges: cp.noStrokeEdges, greyed: cp.categoryCode !== undefined && catDisplay(cp.categoryCode).greyed, }); } + // Geländelinie(n): kräftige Linie, über Kategorie filterbar. + for (const pr of output.profiles ?? []) { + if (pr.categoryCode !== undefined) { + if (opts.visibleCodes && !opts.visibleCodes.has(pr.categoryCode)) continue; + if (!catDisplay(pr.categoryCode).render) continue; + } + for (let i = 0; i + 1 < pr.pts.length; i++) { + const [a, b] = [pr.pts[i], pr.pts[i + 1]]; + acc(a[0], a[1]); + acc(b[0], b[1]); + primitives.push({ + kind: "line", + a: { x: a[0], y: a[1] }, + b: { x: b[0], y: b[1] }, + cls: "section-terrain", + weightMm: SECTION_GROUND_MM, + color: SECTION_INK, + }); + } + } + // Verdeckte Kanten zuerst (liegen unter den sichtbaren), gestrichelt — // Standard AUS (VW-üblich): sie legen sich sonst als blasses Liniengewirr // über Poché und Ansichtskanten. Zuschaltbar je Schnitt-/Ansichts-Ebene. diff --git a/src/plan/meshSection.test.ts b/src/plan/meshSection.test.ts new file mode 100644 index 00000000..c5365bbd --- /dev/null +++ b/src/plan/meshSection.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, it } from "vitest"; +import { cutMeshWithPlane, ringArea, ringsToPolygons } from "./meshSection"; + +/** Quader als geschlossenes Netz (12 Dreiecke, aussen orientiert). `inward` kehrt um (Hohlraum). */ +function box(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, inward = false) { + const p = [x0, y0, z0, x1, y0, z0, x1, y1, z0, x0, y1, z0, x0, y0, z1, x1, y0, z1, x1, y1, z1, x0, y1, z1]; + const f = [0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 1, 5, 0, 5, 4, 1, 2, 6, 1, 6, 5, 2, 3, 7, 2, 7, 6, 3, 0, 4, 3, 4, 7]; + return { p, f: inward ? f.map((_, i) => f[i - (i % 3) + [0, 2, 1][i % 3]]) : f }; +} +const merge = (a: ReturnType, b: ReturnType) => ({ p: [...a.p, ...b.p], f: [...a.f, ...b.f.map((i) => i + a.p.length / 3)] }); + +describe("cutMeshWithPlane", () => { + it("senkrechter Schnitt durch einen Quader: ein Ring, Fläche = Breite × Höhe", () => { + const b = box(0, 0, 0, 4, 2, 3); + const r = cutMeshWithPlane(b.p, b.f, { kind: "vertical", point: { x: 0, y: 1 }, dir: { x: 1, y: 0 } }); + expect(r.rings).toHaveLength(1); + expect(r.lines).toHaveLength(0); + expect(Math.abs(ringArea(r.rings[0]))).toBeCloseTo(4 * 3, 9); + }); + + it("waagrechter Schnitt: Grundriss-Ring (x,y)", () => { + const b = box(1, 1, 0, 4, 3, 5); + const r = cutMeshWithPlane(b.p, b.f, { kind: "horizontal", z: 2.5 }); + expect(r.rings).toHaveLength(1); + expect(Math.abs(ringArea(r.rings[0]))).toBeCloseTo(3 * 2, 9); + }); + + it("Ebene exakt auf Ecken/Kanten (Raster!) schliesst trotzdem sauber", () => { + const b = box(0, 0, 0, 4, 2, 3); + for (const y of [0, 2]) { + const r = cutMeshWithPlane(b.p, b.f, { kind: "vertical", point: { x: 0, y }, dir: { x: 1, y: 0 } }); + expect(r.lines).toHaveLength(0); + } + const r = cutMeshWithPlane(b.p, b.f, { kind: "horizontal", z: 3 }); + expect(r.lines).toHaveLength(0); + }); + + it("Ebene schneidet nicht: leer", () => { + const b = box(0, 0, 0, 1, 1, 1); + const r = cutMeshWithPlane(b.p, b.f, { kind: "horizontal", z: 10 }); + expect(r.rings).toEqual([]); + expect(r.lines).toEqual([]); + }); + + it("Hohlkörper: Aussenring + Loch", () => { + const m = merge(box(0, 0, 0, 10, 10, 10), box(3, 3, 3, 7, 7, 7, true)); + const r = cutMeshWithPlane(m.p, m.f, { kind: "horizontal", z: 5 }); + expect(r.rings).toHaveLength(2); + const polys = ringsToPolygons(r.rings); + expect(polys).toHaveLength(1); + expect(polys[0]).toHaveLength(2); + const area = Math.abs(ringArea(polys[0][0])) - Math.abs(ringArea(polys[0][1])); + expect(area).toBeCloseTo(100 - 16, 9); + }); + + it("zwei getrennte Körper: zwei Polygone ohne Löcher", () => { + const m = merge(box(0, 0, 0, 2, 2, 2), box(5, 0, 0, 7, 2, 2)); + const polys = ringsToPolygons(cutMeshWithPlane(m.p, m.f, { kind: "horizontal", z: 1 }).rings); + expect(polys).toHaveLength(2); + expect(polys.every((p) => p.length === 1)).toBe(true); + }); + + it("offenes Netz (Gelände): Linienzug statt Ring, Höhe als v", () => { + // Zwei Dreiecke, die eine geneigte Fläche z = x bilden (x 0..4, y 0..4). + const p = [0, 0, 0, 4, 0, 4, 4, 4, 4, 0, 4, 0]; + const f = [0, 1, 2, 0, 2, 3]; + const r = cutMeshWithPlane(p, f, { kind: "vertical", point: { x: 0, y: 2 }, dir: { x: 1, y: 0 } }); + expect(r.rings).toEqual([]); + expect(r.lines).toHaveLength(1); + const line = r.lines[0]; + const first = line[0]; + const last = line[line.length - 1]; + expect(Math.min(first[0], last[0])).toBeCloseTo(0, 9); + expect(Math.max(first[0], last[0])).toBeCloseTo(4, 9); + // v = z = u auf dieser Fläche + for (const [u, v] of line) expect(v).toBeCloseTo(u, 9); + }); + + it("schräge Schnittrichtung", () => { + const b = box(0, 0, 0, 2, 2, 2); + const s = Math.SQRT1_2; + const r = cutMeshWithPlane(b.p, b.f, { kind: "vertical", point: { x: 0, y: 0 }, dir: { x: s, y: s } }); + expect(r.rings).toHaveLength(1); + // Diagonale des Quaders: Länge 2√2, Höhe 2 + expect(Math.abs(ringArea(r.rings[0]))).toBeCloseTo(2 * Math.SQRT2 * 2, 6); + }); +}); diff --git a/src/plan/meshSection.ts b/src/plan/meshSection.ts new file mode 100644 index 00000000..15d85815 --- /dev/null +++ b/src/plan/meshSection.ts @@ -0,0 +1,223 @@ +// Ebenenschnitt beliebiger Dreiecksnetze (docs/boolean-geometrie-plan.md, Baustein 1 / [G1]). +// +// Schneidet ein Netz (Modell-Koordinaten x, y, z = Höhe — wie `ImportedMesh`/`TerrainMesh`/truck- +// Meshes) mit einer SENKRECHTEN Ebene (Schnitt) oder einer WAAGRECHTEN Ebene (Grundriss auf +// Schnitthöhe) und liefert Linienzüge bzw. Ringe in den Ebenen-Koordinaten: +// • senkrecht: (u, v) wie die Schnitt-Pipeline (u entlang der Ebene, v = Höhe), +// • waagrecht: (x, y). +// Geschlossene Netze (Körper) ergeben geschlossene Ringe → Polygone mit Löchern; offene Netze +// (Gelände) ergeben offene Linienzüge (Geländelinie im Schnitt). +// +// Robustheit: Ecken exakt AUF der Ebene werden einheitlich der „hinteren" Seite zugeschlagen +// (symbolische Störung, `d > 0` ⇒ vorn) — so schliessen sich die Segmente auch bei Netzen, deren +// Ecken genau auf der Schnittlinie liegen (Achsen im Raster!). + +export type Pt2 = [number, number]; + +export interface CutResult { + /** Geschlossene Ringe (Körper). */ + rings: Pt2[][]; + /** Offene Linienzüge (offene Netze, z. B. Gelände). */ + lines: Pt2[][]; +} + +/** Ebene durch `point` (Modell x,y) in Richtung `dir` (Einheitsvektor): senkrechter Schnitt. */ +export interface VerticalPlane { + kind: "vertical"; + point: { x: number; y: number }; + dir: { x: number; y: number }; +} +/** Waagrechte Ebene auf Höhe `z`. */ +export interface HorizontalPlane { + kind: "horizontal"; + z: number; +} +export type CutPlane = VerticalPlane | HorizontalPlane; + +const QUANT = 1e6; +const key = (p: Pt2): string => `${Math.round(p[0] * QUANT)},${Math.round(p[1] * QUANT)}`; + +/** + * Schneidet das Netz mit `plane`. `positions` flach [x,y,z,…], `indices` je 3 ein Dreieck. + * Entartete Segmente (Länge ~0) werden verworfen. + */ +export function cutMeshWithPlane(positions: ArrayLike, indices: ArrayLike, plane: CutPlane): CutResult { + const nv = Math.floor(positions.length / 3); + // Signierter Abstand + 2D-Koordinate je Vertex. + const dist = new Float64Array(nv); + const c0 = new Float64Array(nv); + const c1 = new Float64Array(nv); + for (let i = 0; i < nv; i++) { + const x = positions[i * 3]; + const y = positions[i * 3 + 1]; + const z = positions[i * 3 + 2]; + if (plane.kind === "vertical") { + // Normale der Ebene (im Grundriss): n = (dir.y, −dir.x); u = Position entlang dir. + dist[i] = (x - plane.point.x) * plane.dir.y - (y - plane.point.y) * plane.dir.x; + c0[i] = (x - plane.point.x) * plane.dir.x + (y - plane.point.y) * plane.dir.y; + c1[i] = z; + } else { + dist[i] = z - plane.z; + c0[i] = x; + c1[i] = y; + } + } + + // Kanten-Schnittpunkte cachen (jede Kante gehört zu bis zu 2 Dreiecken → gleicher Punkt). + const edgePt = new Map(); + const edgeKey = (a: number, b: number): number => (a < b ? a * nv + b : b * nv + a); + const crossing = (a: number, b: number): Pt2 => { + const k = edgeKey(a, b); + let p = edgePt.get(k); + if (!p) { + const t = dist[a] / (dist[a] - dist[b]); + p = [c0[a] + (c0[b] - c0[a]) * t, c1[a] + (c1[b] - c1[a]) * t]; + edgePt.set(k, p); + } + return p; + }; + + const segs: Array<[Pt2, Pt2]> = []; + for (let t = 0; t + 2 < indices.length; t += 3) { + const a = indices[t]; + const b = indices[t + 1]; + const c = indices[t + 2]; + const sa = dist[a] > 0; + const sb = dist[b] > 0; + const sc = dist[c] > 0; + if (sa === sb && sb === sc) continue; + // Genau eine Ecke steht allein auf ihrer Seite: die beiden Kanten dort bilden das Segment. + const odd = sa !== sb && sa !== sc ? a : sb !== sa && sb !== sc ? b : c; + const o1 = odd === a ? b : odd === b ? c : a; + const o2 = odd === a ? c : odd === b ? a : b; + const p = crossing(odd, o1); + const q = crossing(odd, o2); + if (Math.abs(p[0] - q[0]) < 1e-9 && Math.abs(p[1] - q[1]) < 1e-9) continue; + // Einheitlicher Umlaufsinn je Seite der Einzelecke → Segmente benachbarter Dreiecke fügen + // sich bei gleichsinnig orientiertem Netz zu gleichsinnigen Ketten. + segs.push(dist[odd] > 0 ? [q, p] : [p, q]); + } + return chain(segs); +} + +/** Verkettet Segmente zu Ringen und Linienzügen. */ +function chain(segs: Array<[Pt2, Pt2]>): CutResult { + // Start-/Endpunkt-Index. + const byStart = new Map(); + const byEnd = new Map(); + segs.forEach((s, i) => { + const ks = key(s[0]); + const ke = key(s[1]); + (byStart.get(ks) ?? byStart.set(ks, []).get(ks)!).push(i); + (byEnd.get(ke) ?? byEnd.set(ke, []).get(ke)!).push(i); + }); + const used = new Array(segs.length).fill(false); + const take = (m: Map, k: string): number => { + const l = m.get(k); + if (!l) return -1; + while (l.length) { + const i = l.pop()!; + if (!used[i]) return i; + } + return -1; + }; + const rings: Pt2[][] = []; + const lines: Pt2[][] = []; + for (let i = 0; i < segs.length; i++) { + if (used[i]) continue; + used[i] = true; + const pts: Pt2[] = [segs[i][0], segs[i][1]]; + const startKey = key(pts[0]); + // vorwärts + for (;;) { + const k = key(pts[pts.length - 1]); + if (k === startKey && pts.length > 2) break; + const n = take(byStart, k); + if (n < 0) break; + used[n] = true; + pts.push(segs[n][1]); + } + if (key(pts[pts.length - 1]) === startKey && pts.length > 3) { + pts.pop(); + rings.push(pts); + continue; + } + // rückwärts verlängern (offener Zug) + for (;;) { + const k = key(pts[0]); + const n = take(byEnd, k); + if (n < 0) break; + used[n] = true; + pts.unshift(segs[n][0]); + if (key(pts[0]) === key(pts[pts.length - 1])) { + pts.shift(); + rings.push(pts); + pts.length = 0; + break; + } + } + if (pts.length >= 2) lines.push(pts); + } + return { rings, lines }; +} + +export const ringArea = (r: Pt2[]): number => { + let a = 0; + for (let i = 0; i < r.length; i++) { + const p = r[i]; + const q = r[(i + 1) % r.length]; + a += p[0] * q[1] - q[0] * p[1]; + } + return a / 2; +}; + +function pointInRing(p: Pt2, r: Pt2[]): boolean { + let inside = false; + for (let i = 0, j = r.length - 1; i < r.length; j = i++) { + const [xi, yi] = r[i]; + const [xj, yj] = r[j]; + if (yi > p[1] !== yj > p[1] && p[0] < ((xj - xi) * (p[1] - yi)) / (yj - yi) + xi) inside = !inside; + } + return inside; +} + +/** Ring mit Löchern (Aussenring zuerst, dann Löcher). */ +export type RingSet = Pt2[][]; + +/** + * Ordnet geschlossene Ringe zu Aussenring + Löchern (Schachtelungstiefe gerade = aussen, ungerade = + * Loch). Unabhängig vom Umlaufsinn. Aussenringe zuerst-gross; Löcher folgen ihrem Aussenring. + */ +export function ringsToPolygons(rings: Pt2[][]): RingSet[] { + const items = rings + .filter((r) => r.length >= 3 && Math.abs(ringArea(r)) > 1e-12) + .map((r) => ({ r, area: Math.abs(ringArea(r)) })) + .sort((a, b) => b.area - a.area); + const depth: number[] = items.map(() => 0); + const parent: number[] = items.map(() => -1); + for (let i = 0; i < items.length; i++) { + for (let j = i - 1; j >= 0; j--) { + // Kandidat j (grösser) enthält i? Test mit einem Punkt von i (nicht auf dem Rand von j). + if (pointInRing(items[i].r[0], items[j].r)) { + parent[i] = j; + depth[i] = depth[j] + 1; + break; + } + } + } + const out: RingSet[] = []; + const idx = new Map(); + items.forEach((it, i) => { + if (depth[i] % 2 === 0) { + idx.set(i, out.length); + out.push([it.r]); + } + }); + items.forEach((it, i) => { + if (depth[i] % 2 === 1) { + const owner = idx.get(parent[i]); + if (owner !== undefined) out[owner].push(it.r); + } + }); + return out; +} diff --git a/src/plan/sectionExtras.test.ts b/src/plan/sectionExtras.test.ts new file mode 100644 index 00000000..cfde7bc2 --- /dev/null +++ b/src/plan/sectionExtras.test.ts @@ -0,0 +1,65 @@ +// Gelände und freie Körper im Schnitt ([G1]): echter Lauf über computeSection (render3d- und +// truck-WASM aus src/engine/pkg*). +import { beforeAll, describe, expect, it } from "vitest"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { setWasmBytesProvider } from "../engine/wasmSource"; +import { computeSection } from "./toSection"; +import { generateSectionPlan } from "./generatePlan"; +import { sampleProject } from "../model/sampleProject"; +import type { ExtrudedSolid, Project, TerrainMesh } from "../model/types"; + +beforeAll(() => { + setWasmBytesProvider(async (name) => + readFileSync(join(process.cwd(), "src", "engine", name === "trucksolid" ? "pkgTruck/trucksolid_bg.wasm" : "pkg3d/render3d_bg.wasm")), + ); +}); + +const section = sampleProject.drawingLevels.find((l) => l.kind === "section")!; + +/** Gelände: Raster 41×41 über −20…+20 m, z = 0.1·x (geneigt). */ +function terrain(): TerrainMesh { + const n = 41; + const positions: number[] = []; + for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) positions.push(i - 20, j - 20, 0.1 * (i - 20)); + const indices: number[] = []; + for (let j = 0; j + 1 < n; j++) + for (let i = 0; i + 1 < n; i++) { + const a = j * n + i; + indices.push(a, a + 1, a + n, a + 1, a + n + 1, a + n); + } + return { id: "t1", type: "terrainMesh", name: "Gelände", positions, indices }; +} + +describe("Schnitt: Gelände und freie Körper", () => { + it("Geländelinie als Linienzug im Schnitt, auf den Gebäudebereich begrenzt", async () => { + const p: Project = { ...sampleProject, context: [...(sampleProject.context ?? []), terrain()] }; + const out = (await computeSection(p, section))!; + expect(out.profiles?.length).toBeGreaterThan(0); + const pts = out.profiles!.flatMap((x) => x.pts); + // Begrenzung: nicht über das ganze 40-m-Gelände, sondern Inhalt ± Rand + const us = out.cutPolygons.flatMap((c) => c.pts.map((q) => q[0])); + expect(Math.min(...pts.map((q) => q[0]))).toBeGreaterThanOrEqual(Math.min(...us) - 6.01); + expect(Math.max(...pts.map((q) => q[0]))).toBeLessThanOrEqual(Math.max(...us) + 6.01); + const plan = generateSectionPlan(out); + expect(plan.primitives.some((q) => q.kind === "line" && q.cls === "section-terrain")).toBe(true); + }, 30_000); + + it("Kugel (B-Rep) wird als Schnittfläche gefüllt, wenn die Ebene sie trifft", async () => { + const floor = sampleProject.drawingLevels.find((l) => l.kind === "floor")!; + // Kugel mitten auf der Schnittebene: Position aus der Schnittlinie des Beispiels + const [px, , pz] = (await import("./toSection")).sectionPlaneFromLevel(section)!.point; + const sphere: ExtrudedSolid = { + id: "ball", type: "extrudedSolid", levelId: floor.id, height: 2, + points: [{ x: px - 1, y: pz - 1 }, { x: px + 1, y: pz - 1 }, { x: px + 1, y: pz + 1 }, { x: px - 1, y: pz + 1 }], + brep: { spec: { kind: "sphere", center: [px, pz, 1], r: 1 }, origin: { x: px - 1, y: pz - 1 } }, + }; + const p: Project = { ...sampleProject, extrudedSolids: [...(sampleProject.extrudedSolids ?? []), sphere] }; + const out = (await computeSection(p, section))!; + const body = out.cutPolygons.filter((c) => c.component.kind === "body"); + expect(body.length).toBeGreaterThan(0); + expect(body[0].sourceId).toBe("ball"); + const plan = generateSectionPlan(out); + expect(plan.primitives.some((q) => q.kind === "polygon" && q.extrudedSolidId === "ball")).toBe(true); + }, 30_000); +}); diff --git a/src/plan/sectionExtras.ts b/src/plan/sectionExtras.ts new file mode 100644 index 00000000..c607890e --- /dev/null +++ b/src/plan/sectionExtras.ts @@ -0,0 +1,134 @@ +// Zusätze zum Schnitt, die nicht Wand/Decke/Dach sind (Baustein [G1], meshSection.ts): +// • Geländelinie: das Gelände-Netz (`TerrainMesh`) geschnitten → Linienzug im Schnitt, +// auf den Bereich des Gebäudes (± Rand) begrenzt. +// • Körper: freie Körper (`ExtrudedSolid`, B-Rep) geschnitten → gefüllte Schnittflächen. + +import type { ExtrudedSolid, Project } from "../model/types"; +import { getFloor } from "../model/types"; +import { buildSolidMesh } from "../engine/truckSolid"; +import { extrudedSolidSpec } from "./extrudedSolidSpec"; +import { isVoidSolid } from "./voids"; +import { cutMeshWithPlane, ringsToPolygons } from "./meshSection"; +import type { Pt2 } from "./meshSection"; +import { keyhole } from "./voids"; +import type { SectionCutPolygon, SectionOutput, SectionPlaneSpec } from "./toSection"; + +type UDir = { x: number; y: number }; + +/** Seitenrand (m) der Geländelinie um den Inhalt des Schnitts. */ +const TERRAIN_MARGIN = 6; +/** Neutrale Füllung geschnittener freier Körper (kein Bauteil/Material zugeordnet). */ +const BODY_FILL = "#d9dde2"; +const BODY_RGB: [number, number, number] = [0.85, 0.87, 0.89]; + +function planeOf(plane: SectionPlaneSpec, dir: UDir) { + return { kind: "vertical" as const, point: { x: plane.point[0], y: plane.point[2] }, dir }; +} + +/** Wertebereich in u über alle Cut-Polygone und Kanten (für die Geländebegrenzung). */ +function uRange(output: SectionOutput): [number, number] | null { + let lo = Infinity; + let hi = -Infinity; + for (const cp of output.cutPolygons) for (const p of cp.pts) (lo = Math.min(lo, p[0])), (hi = Math.max(hi, p[0])); + for (const e of [...output.visibleEdges, ...output.hiddenEdges]) { + lo = Math.min(lo, e.a[0], e.b[0]); + hi = Math.max(hi, e.a[0], e.b[0]); + } + return Number.isFinite(lo) ? [lo, hi] : null; +} + +/** Schneidet einen Linienzug auf [lo, hi] in u (Segmentweise, interpoliert die Ränder). */ +function clipLineToU(pts: Pt2[], lo: number, hi: number): Pt2[][] { + const out: Pt2[][] = []; + let cur: Pt2[] = []; + const flush = () => { + if (cur.length >= 2) out.push(cur); + cur = []; + }; + for (let i = 0; i + 1 < pts.length; i++) { + const a = pts[i]; + const b = pts[i + 1]; + const [u0, u1] = [a[0], b[0]]; + if ((u0 < lo && u1 < lo) || (u0 > hi && u1 > hi)) { + flush(); + continue; + } + const at = (u: number): Pt2 => { + const t = u1 === u0 ? 0 : (u - u0) / (u1 - u0); + return [u, a[1] + (b[1] - a[1]) * t]; + }; + const pa: Pt2 = u0 < lo ? at(lo) : u0 > hi ? at(hi) : a; + const pb: Pt2 = u1 < lo ? at(lo) : u1 > hi ? at(hi) : b; + if (cur.length === 0) cur.push(pa); + cur.push(pb); + if (u1 < lo || u1 > hi) flush(); + } + flush(); + return out; +} + +/** Hängt die Geländelinien aller Gelände-Netze an `output.profiles`. */ +export function appendTerrainProfiles(output: SectionOutput, project: Project, plane: SectionPlaneSpec, uDir: UDir): void { + const terrains = (project.context ?? []).filter((c) => c.type === "terrainMesh"); + if (terrains.length === 0) return; + const range = uRange(output); + const cut = planeOf(plane, uDir); + for (const t of terrains) { + if (t.type !== "terrainMesh" || t.positions.length < 9) continue; + const { lines } = cutMeshWithPlane(t.positions, t.indices, cut); + for (const line of lines) { + const parts = range ? clipLineToU(line, range[0] - TERRAIN_MARGIN, range[1] + TERRAIN_MARGIN) : [line]; + for (const pts of parts) { + (output.profiles ??= []).push({ kind: "terrain", id: t.id, categoryCode: t.categoryCode, pts }); + } + } + } +} + +/** Welche Körper im Schnitt erscheinen (freie Körper, keine Abzugskörper). */ +export function sectionSolids(project: Project): Array<{ index: number; solid: ExtrudedSolid }> { + const out: Array<{ index: number; solid: ExtrudedSolid }> = []; + (project.extrudedSolids ?? []).forEach((solid, index) => { + if (solid.operation === "subtract" || isVoidSolid(solid)) return; + out.push({ index, solid }); + }); + return out; +} + +/** Hängt die geschnittenen freien Körper als Cut-Polygone an (truck-Mesh, asynchron). */ +export async function appendBodySections(output: SectionOutput, project: Project, plane: SectionPlaneSpec, uDir: UDir): Promise { + const cut = planeOf(plane, uDir); + for (const { index, solid } of sectionSolids(project)) { + let baseZ = 0; + try { + baseZ = solid.levelId ? (getFloor(project, solid.levelId).baseElevation ?? 0) : 0; + } catch { + baseZ = 0; + } + const spec = extrudedSolidSpec(solid, baseZ); + if (!spec) continue; + let mesh: { positions: number[]; indices: number[] }; + try { + mesh = await buildSolidMesh(spec, 0.01); + } catch { + continue; // Körper nicht baubar → im Schnitt weglassen, wie im 3D + } + const { rings } = cutMeshWithPlane(mesh.positions, mesh.indices, cut); + for (const poly of ringsToPolygons(rings)) { + const { pts } = poly.length > 1 ? keyhole(poly as [number, number][][]) : { pts: poly[0] }; + const cp: SectionCutPolygon = { + component: { kind: "body", index }, + color: BODY_RGB, + pts: pts as Array<[number, number]>, + fill: BODY_FILL, + noStrokeEdges: poly.length > 1 ? holeBridgeEdges(pts.length, poly) : undefined, + }; + output.cutPolygons.push(cp); + } + } +} + +/** Kanten-Indizes der Schlüsselloch-Stege eines Rings mit Löchern. */ +function holeBridgeEdges(n: number, poly: Pt2[][]): number[] { + return keyhole(poly as [number, number][][]).bridges.filter((i) => i < n); +} diff --git a/src/plan/toSection.ts b/src/plan/toSection.ts index b9e6ad29..72763721 100644 --- a/src/plan/toSection.ts +++ b/src/plan/toSection.ts @@ -25,6 +25,7 @@ import { openingInterval, openingVerticalExtent } from "../geometry/opening"; import { roofBaseElevation, roofGeometry } from "../geometry/roof"; import { projectToModel3d } from "./toWalls3d"; import { subtractVoidsInSection } from "./voids"; +import { appendBodySections, appendTerrainProfiles } from "./sectionExtras"; import { loadEngine3d } from "../engine/engine3d"; import { HATCH_INK, @@ -40,7 +41,7 @@ import type { HatchRender } from "./generatePlan"; /** Bauteil-Referenz eines Cut-Polygons/einer Kante (Art + Eingabe-Index). */ export interface SectionComponentRef { - kind: "wall" | "slab" | "roof"; + kind: "wall" | "slab" | "roof" | "body"; index: number; } @@ -118,6 +119,17 @@ export interface SectionOutput { cutPolygons: SectionCutPolygon[]; visibleEdges: SectionEdge[]; hiddenEdges: SectionEdge[]; + /** Linien im Schnitt, die kein Bauteil-Umriss sind (Geländelinie). */ + profiles?: SectionProfile[]; +} + +/** Geländelinie im Schnitt (Linienzug in (u, v)). */ +export interface SectionProfile { + kind: "terrain"; + /** Id des Gelände-Netzes (`TerrainMesh.id`). */ + id: string; + categoryCode?: string; + pts: Array<[number, number]>; } /** Minimal-Typ des WASM-Exports (das echte `.d.ts` entsteht erst beim wasm-Build). */ @@ -1211,6 +1223,15 @@ export function filterByDepth( output.hiddenEdges = output.hiddenEdges.filter(keep); } +/** Körper-Schnittflächen: Herkunft + Kategorie nachtragen (Auswahl/Filter im Schnitt). */ +function attachBodyOwners(output: SectionOutput, project: Project): void { + const solids = project.extrudedSolids ?? []; + for (const cp of output.cutPolygons) { + if (cp.component.kind !== "body") continue; + cp.sourceId = solids[cp.component.index]?.id; + } +} + export async function computeSection( project: Project, level: DrawingLevel, @@ -1276,6 +1297,10 @@ export async function computeSection( { x: plane.point[0], y: plane.point[2] }, sectionUAxisModel(plane), ); + // Freie Körper (B-Rep) und Gelände: über den allgemeinen Ebenenschnitt (meshSection.ts). + await appendBodySections(output, project, plane, sectionUAxisModel(plane)); + attachBodyOwners(output, project); + appendTerrainProfiles(output, project, plane, sectionUAxisModel(plane)); // Schnitt-Tiefe: Kanten weiter als `level.depth` hinter der Ebene ausblenden. if (level.depth !== undefined) filterByDepth(output, project, plane, level.depth); return output;