diff --git a/src/App.tsx b/src/App.tsx
index e6e16ee..ff06b71 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -609,6 +609,8 @@ export default function App() {
const setSelectedColumnIds = useStore((s) => s.setSelectedColumnIds);
const selectedColumnId =
selectedColumnIds.length === 1 ? selectedColumnIds[0] : null;
+ const selectedContextObjectIds = useStore((s) => s.selectedContextObjectIds);
+ const setSelectedContextObjectIds = useStore((s) => s.setSelectedContextObjectIds);
// ── Kontextmenüs (transienter UI-State, bleibt lokal) ─────────────────────
// Eigenschaften-Zwischenablage des Ebenen-Kontextmenüs (Farbe + Linienstärke).
@@ -1905,7 +1907,8 @@ export default function App() {
selectedRoomIds.length ||
selectedExtrudedSolidIds.length ||
selectedColumnIds.length ||
- selectedRoofIds.length
+ selectedRoofIds.length ||
+ selectedContextObjectIds.length
) {
const dids = new Set(selectedDrawingIds);
const wids = new Set(selectedWallIds);
@@ -1916,6 +1919,7 @@ export default function App() {
const exids = new Set(selectedExtrudedSolidIds);
const colids = new Set(selectedColumnIds);
const rfids = new Set(selectedRoofIds);
+ const ctxids = new Set(selectedContextObjectIds);
setProject((p) => ({
...p,
drawings2d: p.drawings2d.filter((d) => !dids.has(d.id)),
@@ -1931,6 +1935,7 @@ export default function App() {
openings: (p.openings ?? []).filter(
(o) => !oids.has(o.id) && !wids.has(o.hostWallId),
),
+ context: (p.context ?? []).filter((o) => !ctxids.has(o.id)),
}));
setSelectedDrawingIds([]);
setSelectedWallIds([]);
@@ -1941,6 +1946,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
e.preventDefault();
}
@@ -1958,6 +1964,7 @@ export default function App() {
selectedExtrudedSolidIds,
selectedColumnIds,
selectedRoofIds,
+ selectedContextObjectIds,
selectedSectionLineId,
project.walls,
project.drawings2d,
@@ -3531,6 +3538,7 @@ export default function App() {
},
// ── Site-/Kontext-Schicht ──────────────────────────────────────────────
contextObjects: project.context ?? [],
+ selectedContextObjectIds,
onAddContextObjects: addContextObjectsAndFit,
onRemoveContextObject: removeContextObject,
onGenerateTerrain: generateTerrain,
@@ -3555,6 +3563,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
switch (kind) {
case "Wand":
@@ -3932,6 +3941,7 @@ export default function App() {
sectionLine,
selectedSectionLineId,
selectedViewSnapshotId,
+ selectedContextObjectIds,
]);
// ── Ausschnitt-Anwahl: „bleibt bis zur Abweichung" (DOSSIER A2) ───────────
@@ -4417,6 +4427,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
setStampEditorRoomId(roomId);
};
@@ -4433,6 +4444,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
setEditTextId(drawingId);
};
@@ -4546,6 +4558,7 @@ export default function App() {
if (keep !== "extrudedSolid") setSelectedExtrudedSolidIds([]);
if (keep !== "column") setSelectedColumnIds([]);
if (keep !== "roof") setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
if (keep !== "sectionLine") setSelectedSectionLineId(null);
};
if (sel.sectionLineId) {
@@ -4596,6 +4609,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
};
// Rechts-Klick im Grundriss: Wand-Kontextmenü. Trifft der Klick eine Wand, die
@@ -4679,6 +4693,21 @@ export default function App() {
// Leertreffer (kein Bauteil) → alle Selektionen leeren.
const onViewport3dPick = (hit: Pick3dHit, additive: boolean) => {
if (!hit) {
+ setSelectedWallIds([]);
+ setSelectedCeilingIds([]);
+ setSelectedDrawingId(null);
+ setSelectedOpeningIds([]);
+ setSelectedStairIds([]);
+ setSelectedRoomIds([]);
+ setSelectedExtrudedSolidIds([]);
+ setSelectedColumnIds([]);
+ setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
+ setSelectedSectionLineId(null);
+ return;
+ }
+ if (hit.kind === "contextObject") {
+ setSelectedContextObjectIds([hit.id]);
setSelectedWallIds([]);
setSelectedCeilingIds([]);
setSelectedDrawingId(null);
@@ -4704,6 +4733,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
return;
}
@@ -4717,6 +4747,7 @@ export default function App() {
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
setSelectedRoofIds([]);
+ setSelectedContextObjectIds([]);
setSelectedSectionLineId(null);
return;
}
@@ -4730,6 +4761,7 @@ export default function App() {
setSelectedRoomIds([]);
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
+ setSelectedContextObjectIds([]);
return;
}
if (hit.kind === "wall") {
@@ -4746,6 +4778,7 @@ export default function App() {
setSelectedDrawingId(null);
setSelectedOpeningIds([]);
setSelectedStairIds([]);
+ setSelectedContextObjectIds([]);
setSelectedRoomIds([]);
setSelectedExtrudedSolidIds([]);
setSelectedColumnIds([]);
@@ -5046,6 +5079,7 @@ export default function App() {
selectedColumnIds={selectedColumnIds}
selectedRoomIds={selectedRoomIds}
selectedRoofIds={selectedRoofIds}
+ selectedContextObjectIds={selectedContextObjectIds}
selectedSectionLineId={selectedSectionLineId}
roomStamp={roomStamp}
onStampMove={moveRoomStamp}
@@ -6185,6 +6219,7 @@ function Content({
selectedColumnIds,
selectedRoomIds,
selectedRoofIds,
+ selectedContextObjectIds,
selectedSectionLineId,
roomStamp,
onStampMove,
@@ -6265,6 +6300,7 @@ function Content({
selectedColumnIds: string[];
selectedRoomIds: string[];
selectedRoofIds: string[];
+ selectedContextObjectIds: string[];
selectedSectionLineId: string | null;
roomStamp: { roomId: string; anchor: Vec2 } | null;
onStampMove: (roomId: string, delta: Vec2) => void;
@@ -6337,7 +6373,8 @@ function Content({
selectedCeilingIds.length === 0 &&
selectedOpeningIds.length === 0 &&
selectedStairIds.length === 0 &&
- selectedRoofIds.length === 0
+ selectedRoofIds.length === 0 &&
+ selectedContextObjectIds.length === 0
? null
: selectionHighlightLines(
project,
@@ -6347,6 +6384,7 @@ function Content({
selectedOpeningIds,
selectedStairIds,
selectedRoofIds,
+ selectedContextObjectIds,
),
[
project,
@@ -6355,6 +6393,7 @@ function Content({
selectedOpeningIds,
selectedStairIds,
selectedRoofIds,
+ selectedContextObjectIds,
],
);
diff --git a/src/panels/SitePanel.tsx b/src/panels/SitePanel.tsx
index 172e9da..27dc8ea 100644
--- a/src/panels/SitePanel.tsx
+++ b/src/panels/SitePanel.tsx
@@ -115,7 +115,13 @@ export function SitePanel() {
) : (
{objects.map((obj) => (
- -
+
-
{obj.name}
diff --git a/src/panels/host.ts b/src/panels/host.ts
index 806e682..9595155 100644
--- a/src/panels/host.ts
+++ b/src/panels/host.ts
@@ -402,6 +402,8 @@ export interface PanelHostValue {
// ── Site-/Kontext-Schicht (SitePanel) ───────────────────────────────────
/** Aktuelle Kontext-Objekte (Meshes/Konturen/Gelände) aus `project.context`. */
contextObjects: ContextObject[];
+ /** Im 3D-Viewport angewählte Kontext-Objekte (Umriss-Hervorhebung), s. selectionSlice. */
+ selectedContextObjectIds: string[];
/** Fügt mehrere Kontext-Objekte hinzu (z. B. aus einem DXF-Import). */
onAddContextObjects: (objs: ContextObject[]) => void;
/** Entfernt ein Kontext-Objekt per ID. */
diff --git a/src/plan/toWalls3d.test.ts b/src/plan/toWalls3d.test.ts
index 314a125..d5051c3 100644
--- a/src/plan/toWalls3d.test.ts
+++ b/src/plan/toWalls3d.test.ts
@@ -101,6 +101,48 @@ describe("selectionHighlightLines", () => {
});
});
+describe("selectionHighlightLines — Kontext-Objekte", () => {
+ // Ein einzelnes Dreieck als Kontext-Mesh (importiertes Gebäude): das
+ // Highlight zeichnet dessen achsenparallele Bounding-Box (12 Kanten), NICHT
+ // das volle Dreiecks-Wireframe (s. Kommentar in toWalls3d.ts).
+ const withContext: Project = {
+ ...sampleProject,
+ context: [
+ {
+ id: "CTX1",
+ type: "importedMesh",
+ name: "Testgebäude",
+ positions: [0, 0, 0, 2, 0, 0, 2, 3, 4],
+ indices: [0, 1, 2],
+ },
+ ],
+ };
+
+ it("leere Kontext-Auswahl -> leeres Array", () => {
+ const lines = selectionHighlightLines(withContext, [], [], RGB, [], [], [], []);
+ expect(lines.length).toBe(0);
+ });
+
+ it("unbekannte Kontext-Id -> leeres Array", () => {
+ const lines = selectionHighlightLines(withContext, [], [], RGB, [], [], [], ["does-not-exist"]);
+ expect(lines.length).toBe(0);
+ });
+
+ it("ein Kontext-Objekt -> Bounding-Box-Umriss (12 Kanten)", () => {
+ const lines = selectionHighlightLines(withContext, [], [], RGB, [], [], [], ["CTX1"]);
+ expect(lines.length).toBe(12 * VERTICES_PER_EDGE * FLOATS_PER_VERTEX);
+ expect(lines[3]).toBeCloseTo(RGB[0], 6);
+ expect(lines[5]).toBeCloseTo(RGB[2], 6);
+ });
+
+ it("Kontext-Objekt zusammen mit einer Wand -> Summe der Einzelgeometrien", () => {
+ const wallOnly = selectionHighlightLines(withContext, ["W2"], [], RGB);
+ const ctxOnly = selectionHighlightLines(withContext, [], [], RGB, [], [], [], ["CTX1"]);
+ const both = selectionHighlightLines(withContext, ["W2"], [], RGB, [], [], [], ["CTX1"]);
+ expect(both.length).toBe(wallOnly.length + ctxOnly.length);
+ });
+});
+
describe("Öffnungen als echte Löcher in EINEM Körper (3D-Pfad, layeredWalls:true)", () => {
// Wand W1 (EG, Wandtyp "aw" = 4 Schichten) mit ZWEI Fenstern, deren Achsen-
// Intervalle sich ÜBERLAPPEN, aber auf UNTERSCHIEDLICHER Höhe liegen:
@@ -1890,3 +1932,52 @@ describe("pickGeometry — Dächer (Ray-Dreieck-Pick)", () => {
for (const tri of roofs[0].tris) for (const p of tri) expect(p[1]).toBeGreaterThan(0);
});
});
+
+describe("pickGeometry — Kontext-Meshes (importierte Gebäude/Terrain)", () => {
+ const withContext: Project = {
+ ...sampleProject,
+ context: [
+ {
+ id: "CTX1",
+ type: "importedMesh",
+ name: "Testgebäude",
+ // model (x,y,z=Höhe) -> world (x, z, y): ein Dreieck.
+ positions: [0, 0, 0, 2, 0, 0, 2, 3, 4],
+ indices: [0, 1, 2],
+ },
+ {
+ id: "CONTOURS1",
+ type: "contourSet",
+ name: "Höhenlinien",
+ contours: [],
+ },
+ ],
+ };
+
+ it("liefert je Kontext-Objekt World-Dreiecke, contourSet wird übersprungen", () => {
+ const { contextMeshes } = pickGeometry(withContext);
+ expect(contextMeshes.length).toBe(1);
+ expect(contextMeshes[0].contextObjectId).toBe("CTX1");
+ expect(contextMeshes[0].tris.length).toBe(1);
+ // world = (x, z, y) — dasselbe Dreieck (0,0,0),(2,0,0),(2,3,4)
+ // -> world (0,0,0),(2,0,0),(2,4,3).
+ expect(contextMeshes[0].tris[0][2]).toEqual([2, 4, 3]);
+ });
+
+ it("kaputte Indizes werden robust übersprungen statt abzustürzen", () => {
+ const broken: Project = {
+ ...sampleProject,
+ context: [
+ {
+ id: "CTX2",
+ type: "importedMesh",
+ name: "Kaputt",
+ positions: [0, 0, 0, 1, 0, 0, 1, 1, 0],
+ indices: [0, 1, 99], // Index 99 existiert nicht
+ },
+ ],
+ };
+ const { contextMeshes } = pickGeometry(broken);
+ expect(contextMeshes.length).toBe(0);
+ });
+});
diff --git a/src/plan/toWalls3d.ts b/src/plan/toWalls3d.ts
index df27fdb..b335dfb 100644
--- a/src/plan/toWalls3d.ts
+++ b/src/plan/toWalls3d.ts
@@ -62,7 +62,7 @@ import type { WallCuts } from "../model/joins";
import { add, dot, leftNormal, lineIntersect, normalize, scale, sub } from "../model/geometry";
import type { Line } from "../model/geometry";
import { resolveHatch } from "./generatePlan";
-import type { PickOpening, PickRoof, PickStair } from "../viewport/raycast3d";
+import type { PickOpening, PickRoof, PickStair, PickContextMesh } from "../viewport/raycast3d";
/** World-Punkt [x, Höhe, y] für den Dach-Pick (Vec3-Konvention von raycast3d). */
type RoofPickVec3 = [number, number, number];
@@ -2825,6 +2825,7 @@ export function pickGeometry(
openings: PickOpening[];
stairs: PickStair[];
roofs: PickRoof[];
+ contextMeshes: PickContextMesh[];
} {
// Dächer: Dachflächen + Giebel als WORLD-Dreiecke (fan-trianguliert wie
// emitRoofs; model [x,y,z=Höhe] → world [x, z, y]) — präzise geneigte
@@ -2878,7 +2879,27 @@ export function pickGeometry(
stairId: st.id,
});
}
- return { walls: projectToWalls3d(project), slabs: emitSlabs(project), openings, stairs, roofs };
+ // Kontext-Meshes (importierte Gebäude/Terrain, s. ContextObject): rohe
+ // WORLD-Dreiecke wie beim Dach, direkt aus dem Indexpuffer (kein Fan nötig —
+ // ImportedMesh/TerrainMesh liefern bereits ein echtes Dreiecksnetz). Kaputte
+ // Indizes robust überspringen statt abzustürzen (importierte Daten).
+ const contextMeshes: PickContextMesh[] = [];
+ for (const obj of project.context ?? []) {
+ if (obj.type === "contourSet") continue;
+ const pos = obj.positions;
+ const vcount = pos.length / 3;
+ const tris: [RoofPickVec3, RoofPickVec3, RoofPickVec3][] = [];
+ for (let i = 0; i + 2 < obj.indices.length; i += 3) {
+ const ia = obj.indices[i];
+ const ib = obj.indices[i + 1];
+ const ic = obj.indices[i + 2];
+ if (ia >= vcount || ib >= vcount || ic >= vcount) continue;
+ const at = (idx: number): RoofPickVec3 => [pos[idx * 3], pos[idx * 3 + 2], pos[idx * 3 + 1]];
+ tris.push([at(ia), at(ib), at(ic)]);
+ }
+ if (tris.length > 0) contextMeshes.push({ tris, contextObjectId: obj.id });
+ }
+ return { walls: projectToWalls3d(project), slabs: emitSlabs(project), openings, stairs, roofs, contextMeshes };
}
/**
@@ -2915,13 +2936,15 @@ export function selectionHighlightLines(
openingIds: string[] = [],
stairIds: string[] = [],
roofIds: string[] = [],
+ contextObjectIds: string[] = [],
): Float32Array {
if (
wallIds.length === 0 &&
ceilingIds.length === 0 &&
openingIds.length === 0 &&
stairIds.length === 0 &&
- roofIds.length === 0
+ roofIds.length === 0 &&
+ contextObjectIds.length === 0
) {
return new Float32Array(0);
}
@@ -2931,6 +2954,7 @@ export function selectionHighlightLines(
const openingSet = new Set(openingIds);
const stairSet = new Set(stairIds);
const roofSet = new Set(roofIds);
+ const contextObjSet = new Set(contextObjectIds);
const out: number[] = [];
const pushEdge = (a: RVec3, b: RVec3) => {
out.push(a[0], a[1], a[2], rgb[0], rgb[1], rgb[2]);
@@ -3013,5 +3037,35 @@ export function selectionHighlightLines(
for (const gab of g.gables) polyEdges(gab);
}
}
+ // Kontext-Objekt (importiertes Gebäude/Terrain): Umriss als achsenparallele
+ // Bounding-Box statt vollem Dreiecks-Wireframe — importierte Meshes haben oft
+ // tausende Dreiecke, deren Kanten einzeln zu zeichnen wäre unbrauchbar dicht.
+ if (contextObjSet.size > 0) {
+ for (const obj of project.context ?? []) {
+ if (obj.type === "contourSet" || !contextObjSet.has(obj.id)) continue;
+ const pos = obj.positions;
+ let minX = Infinity, maxX = -Infinity;
+ let minY = Infinity, maxY = -Infinity;
+ let minH = Infinity, maxH = -Infinity;
+ for (let i = 0; i + 2 < pos.length; i += 3) {
+ const x = pos[i], y = pos[i + 1], h = pos[i + 2];
+ if (x < minX) minX = x;
+ if (x > maxX) maxX = x;
+ if (y < minY) minY = y;
+ if (y > maxY) maxY = y;
+ if (h < minH) minH = h;
+ if (h > maxH) maxH = h;
+ }
+ if (!isFinite(minX)) continue;
+ const corners: RVec3[] = [];
+ for (let i = 0; i < 8; i++) {
+ const x = i & 1 ? maxX : minX;
+ const h = i & 2 ? maxH : minH;
+ const y = i & 4 ? maxY : minY;
+ corners.push([x, h, y]);
+ }
+ for (const [i, j] of BOX_EDGES) pushEdge(corners[i], corners[j]);
+ }
+ }
return new Float32Array(out);
}
diff --git a/src/state/selectionSlice.ts b/src/state/selectionSlice.ts
index 3da70a9..194fb36 100644
--- a/src/state/selectionSlice.ts
+++ b/src/state/selectionSlice.ts
@@ -25,6 +25,8 @@ export interface SelectionSlice {
selectedColumnIds: string[];
// Gewählte Dächer als MENGE von IDs — getrennt von den übrigen Kanälen.
selectedRoofIds: string[];
+ // Gewählte Kontext-Objekte (importierte Gebäude/Terrain) als MENGE von IDs.
+ selectedContextObjectIds: string[];
// Gewählte Schnitt-/Ansichtslinie (DrawingLevel-Id) — EINZELauswahl (eine Linie
// zur Zeit), getrennt von den übrigen Kanälen. `null` = keine Linie gewählt.
selectedSectionLineId: string | null;
@@ -38,6 +40,7 @@ export interface SelectionSlice {
setSelectedExtrudedSolidIds: (ids: string[]) => void;
setSelectedColumnIds: (ids: string[]) => void;
setSelectedRoofIds: (ids: string[]) => void;
+ setSelectedContextObjectIds: (ids: string[]) => void;
setSelectedSectionLineId: (id: string | null) => void;
/** Auswahl (Wände + Decken + Öffnungen + Treppen + Dächer + 2D-Elemente) leeren. */
clearSelection: () => void;
@@ -57,6 +60,7 @@ export function createSelectionSlice(
selectedExtrudedSolidIds: [],
selectedColumnIds: [],
selectedRoofIds: [],
+ selectedContextObjectIds: [],
selectedSectionLineId: null,
setSelectedWallIds: (ids) => set({ selectedWallIds: ids }),
setSelectedDrawingIds: (ids) => set({ selectedDrawingIds: ids }),
@@ -67,6 +71,7 @@ export function createSelectionSlice(
setSelectedExtrudedSolidIds: (ids) => set({ selectedExtrudedSolidIds: ids }),
setSelectedColumnIds: (ids) => set({ selectedColumnIds: ids }),
setSelectedRoofIds: (ids) => set({ selectedRoofIds: ids }),
+ setSelectedContextObjectIds: (ids) => set({ selectedContextObjectIds: ids }),
setSelectedSectionLineId: (id) => set({ selectedSectionLineId: id }),
clearSelection: () =>
set({
@@ -79,6 +84,7 @@ export function createSelectionSlice(
selectedExtrudedSolidIds: [],
selectedColumnIds: [],
selectedRoofIds: [],
+ selectedContextObjectIds: [],
selectedSectionLineId: null,
}),
};
diff --git a/src/styles.css b/src/styles.css
index bb88601..71d01dd 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -4043,6 +4043,10 @@ body {
.site-panel .site-row:hover {
background: var(--accent-dim);
}
+.site-panel .site-row-selected {
+ background: var(--accent-dim);
+ outline: 1px solid var(--accent);
+}
.site-panel .site-name {
flex: 1 1 auto;
min-width: 0;
diff --git a/src/viewport/Wasm3DViewport.tsx b/src/viewport/Wasm3DViewport.tsx
index be0bbb5..e6f9432 100644
--- a/src/viewport/Wasm3DViewport.tsx
+++ b/src/viewport/Wasm3DViewport.tsx
@@ -57,7 +57,7 @@ import { drawingGripVertices, drawingMoveAnchor } from "./drawingGrips";
/** Ein per 3D-Klick getroffenes Bauteil, oder null (Leerraum). */
export type Pick3dHit =
- | { kind: "wall" | "ceiling" | "opening" | "stair" | "roof"; id: string }
+ | { kind: "wall" | "ceiling" | "opening" | "stair" | "roof" | "contextObject"; id: string }
| null;
/** Klick-vs-Drag-Schwelle (Pixel): bleibt die Maus zwischen pointerdown und
@@ -798,10 +798,10 @@ export function Wasm3DViewport({
const ndc = toNdc(clientX, clientY);
if (!cb || !o || !ndc) return;
const ray = cameraRay(orbitCamera(o), ndc.ndcX, ndc.ndcY, ndc.aspect);
- const { walls, slabs, openings, stairs, roofs } = pickGeometry(projectRef.current);
- // Erweiterter Pick inkl. Öffnungen (Fenster/Türen) + Treppen — sonst wären
- // im WASM-Viewport nur Wände/Decken anwählbar.
- const hit = pickNearest(ray, walls, slabs, openings, stairs, roofs);
+ const { walls, slabs, openings, stairs, roofs, contextMeshes } = pickGeometry(projectRef.current);
+ // Erweiterter Pick inkl. Öffnungen (Fenster/Türen) + Treppen + Kontext-
+ // Meshes — sonst wären im WASM-Viewport nur Wände/Decken anwählbar.
+ const hit = pickNearest(ray, walls, slabs, openings, stairs, roofs, contextMeshes);
cb(hit ? { kind: hit.kind, id: hit.id } : null, additive);
};
diff --git a/src/viewport/raycast3d.test.ts b/src/viewport/raycast3d.test.ts
index c173393..9091d69 100644
--- a/src/viewport/raycast3d.test.ts
+++ b/src/viewport/raycast3d.test.ts
@@ -9,6 +9,7 @@ import {
type PickSlab,
type PickOpening,
type PickStair,
+ type PickContextMesh,
} from "./raycast3d";
// Kamera bei (0,1,10), Blick entlang -Z auf (0,1,0). Zentraler Strahl (ndc 0,0)
@@ -207,3 +208,69 @@ describe("pickNearest — Öffnungen und Treppen", () => {
expect(hit!.id).toBe("w1");
});
});
+
+// Kontext-Mesh (importiertes Gebäude/Terrain): eine flache, senkrechte
+// Rechteckfläche bei model y=5 (world z=5), model x∈[-1,1], model z (Höhe)
+// ∈[0,2] — dieselbe Front wie WALL, aber als rohes Dreiecksnetz statt Quader.
+const CONTEXT_MESH: PickContextMesh = {
+ tris: [
+ [
+ [-1, 0, 5],
+ [1, 0, 5],
+ [1, 2, 5],
+ ],
+ [
+ [-1, 0, 5],
+ [1, 2, 5],
+ [-1, 2, 5],
+ ],
+ ],
+ contextObjectId: "ctx1",
+};
+
+describe("pickNearest — Kontext-Meshes", () => {
+ it("Strahl auf ein Kontext-Mesh liefert contextObjectId", () => {
+ const hit = pickNearest(
+ { origin: [0, 1, 10], dir: [0, 0, -1] },
+ [],
+ [],
+ [],
+ [],
+ [],
+ [CONTEXT_MESH],
+ );
+ expect(hit).not.toBeNull();
+ expect(hit!.kind).toBe("contextObject");
+ expect(hit!.contextObjectId).toBe("ctx1");
+ expect(hit!.t).toBeCloseTo(5, 6);
+ });
+
+ it("Strahl neben dem Mesh verfehlt", () => {
+ const hit = pickNearest(
+ { origin: [5, 1, 10], dir: [0, 0, -1] },
+ [],
+ [],
+ [],
+ [],
+ [],
+ [CONTEXT_MESH],
+ );
+ expect(hit).toBeNull();
+ });
+
+ it("bei näherer Wand gewinnt die Wand vor dem weiter entfernten Kontext-Mesh", () => {
+ // WALL-Front bei z=5.1 → t=4.9, näher als das Mesh bei z=5 (t=5).
+ const hit = pickNearest(
+ { origin: [0, 1, 10], dir: [0, 0, -1] },
+ [WALL],
+ [],
+ [],
+ [],
+ [],
+ [CONTEXT_MESH],
+ );
+ expect(hit).not.toBeNull();
+ expect(hit!.kind).toBe("wall");
+ expect(hit!.id).toBe("w1");
+ });
+});
diff --git a/src/viewport/raycast3d.ts b/src/viewport/raycast3d.ts
index eb39a9d..035c86b 100644
--- a/src/viewport/raycast3d.ts
+++ b/src/viewport/raycast3d.ts
@@ -96,15 +96,29 @@ export interface PickRoof {
roofId: string;
}
+/**
+ * Minimal-Form eines Kontext-Meshes (importiertes Gebäude/Terrain, s.
+ * `ContextObject`) für den Raycast: rohe WORLD-Dreiecke (dieselbe Konvention
+ * wie {@link PickRoof} — model (x,y,z=Höhe) → world (x, z, y)), keine
+ * Vereinfachung auf ein Bounding-Prisma (Gebäude-Meshes sind zu unregelmässig
+ * dafür, ein grosszügiges Prisma stähle Klicks auf dahinterliegende Bauteile).
+ */
+export interface PickContextMesh {
+ tris: ReadonlyArray;
+ contextObjectId: string;
+}
+
/** Ergebnis eines Picks: getroffenes Bauteil + Distanz entlang des Strahls. */
export interface PickHit {
- kind: "wall" | "ceiling" | "opening" | "stair" | "roof";
+ kind: "wall" | "ceiling" | "opening" | "stair" | "roof" | "contextObject";
id: string;
t: number;
/** Bei `kind:"opening"` dieselbe Id wie `id` (ausdrucksstärkerer Zugriff). */
openingId?: string;
/** Bei `kind:"stair"` dieselbe Id wie `id` (ausdrucksstärkerer Zugriff). */
stairId?: string;
+ /** Bei `kind:"contextObject"` dieselbe Id wie `id` (ausdrucksstärkerer Zugriff). */
+ contextObjectId?: string;
}
const EPS = 1e-9;
@@ -362,6 +376,17 @@ function rayRoofTris(ray: Ray, roof: PickRoof): number | null {
return best;
}
+/** Kleinstes t über alle Dreiecke eines Kontext-Meshes, oder null — wie
+ * {@link rayRoofTris}, dieselbe doppelseitige {@link rayTriangle}. */
+function rayContextMeshTris(ray: Ray, mesh: PickContextMesh): number | null {
+ let best: number | null = null;
+ for (const [a, b, c] of mesh.tris) {
+ const t = rayTriangle(ray, a, b, c);
+ if (t !== null && (best === null || t < best)) best = t;
+ }
+ return best;
+}
+
// Overload OHNE Öffnungen/Treppen: bleibt exakt die bisherige Signatur, sodass
// der bestehende Aufrufer (Wasm3DViewport.tsx: `pickNearest(ray, walls, slabs)`)
// unverändert kompiliert — inklusive des dort erwarteten engen `kind`-Typs
@@ -372,7 +397,8 @@ export function pickNearest(
walls: readonly PickWall[],
slabs: readonly PickSlab[],
): (PickHit & { kind: "wall" | "ceiling" }) | null;
-// Erweiterter Overload MIT Öffnungen/Treppen/Dächern: `kind` deckt alle Arten ab.
+// Erweiterter Overload MIT Öffnungen/Treppen/Dächern/Kontext-Meshes: `kind`
+// deckt alle Arten ab.
export function pickNearest(
ray: Ray,
walls: readonly PickWall[],
@@ -380,6 +406,7 @@ export function pickNearest(
openings: readonly PickOpening[],
stairs?: readonly PickStair[],
roofs?: readonly PickRoof[],
+ contextMeshes?: readonly PickContextMesh[],
): PickHit | null;
/**
* Nächstes getroffenes Bauteil (kleinstes t) über Wände, Decken, Öffnungen und
@@ -397,6 +424,7 @@ export function pickNearest(
openings: readonly PickOpening[] = [],
stairs: readonly PickStair[] = [],
roofs: readonly PickRoof[] = [],
+ contextMeshes: readonly PickContextMesh[] = [],
): PickHit | null {
const TIE_EPS = 1e-6;
let hit: PickHit | null = null;
@@ -433,6 +461,13 @@ export function pickNearest(
const t = rayRoofTris(ray, r);
if (t !== null) consider({ kind: "roof", id: r.roofId, t }, 1);
}
+ // Kontext-Meshes (importierte Gebäude/Terrain): Rang wie Wand/Decke/Dach.
+ for (const m of contextMeshes) {
+ const t = rayContextMeshTris(ray, m);
+ if (t !== null) {
+ consider({ kind: "contextObject", id: m.contextObjectId, t, contextObjectId: m.contextObjectId }, 1);
+ }
+ }
return hit;
}