552 lines
24 KiB
TypeScript
552 lines
24 KiB
TypeScript
// [245] Begehen mit Kollision und Bodenfolge: reine Rechnung auf einer einfachen räumlichen Struktur (Raster in der
|
||
// Grundrissebene) aus den 3D-Teilen, die der Viewer ohnehin hat (Wand-Quader mit Löchern, Decken-/Tritt-Prismen,
|
||
// Netze). Ohne React und Engine, damit testbar. Kein Ray-Test gegen das ganze Modell: je Schritt nur Rasterzellen
|
||
// im Umkreis der Person.
|
||
//
|
||
// KONVENTION wie render3d: Modell (x, y, z=Höhe) → world (x, Höhe, y); Gelände und Import-Netze mit Nord-Negation
|
||
// (x, Höhe, −y) wie in `model3d/pick.rs`. Alle Zahlen hier sind world-Meter.
|
||
//
|
||
// Hindernisse sind senkrechte „Kapseln": eine Strecke in der XZ-Ebene mit Halbdicke `h` und Höhenband [y0, y1].
|
||
// • Wand = Strecke entlang der Achse (Halbdicke = halbe Wandstärke), in Stücke ausserhalb der Löcher geteilt;
|
||
// über und unter einem Loch bleiben Sturz bzw. Brüstung. Tür oder Fenster entscheidet die Öffnungsart im Modell
|
||
// (`Opening.kind`, über `walkInputFrom` am Loch vermerkt: `door`): Türen sind frei — auch Schiebe-/Hebeschiebetüren
|
||
// („Fenstertüren“ sind im Modell Türen) und Türen mit Rund-/Bogenform, deren Loch das umschreibende Rechteck ist —,
|
||
// Fenster sperren mit ihrer Glasebene, auch mit Brüstung 0. Ohne Vermerk (synthetische Eingabe, Loch
|
||
// keiner Öffnung zuzuordnen) gilt die Höhe: Loch bis zur Wand-UK = Tür, mit Brüstung = Fenster.
|
||
// • Decken (nicht Treppentritte): die Unterseite begrenzt den Kopf, ausser über Löchern (Treppenauge, Schacht: der
|
||
// Umriss trägt sie als Brücke, gerade/ungerade-Regel); so stösst man im Raum an, im Treppenhaus nicht.
|
||
// • Dachschrägen: die nach unten zeigenden Dreiecke der Dachnetze (`roof`, Unterseite der untersten Schicht; Gauben- und
|
||
// Dachfenster-Aussparungen fehlen im Netz und sind damit offen) begrenzen den Kopf wie die Deckenunterseite: wo das
|
||
// Dach unter Fusshöhe + Körperhöhe liegt (Kniestock), kommt man nicht hin; wer schon darunter steht, bleibt beweglich.
|
||
// • Decken-/Trittkante = Polygonkante über [zBottom, zTop]; senkrechte Dreiecke der Netze (Stützen, Körper) ebenso.
|
||
// Böden sind die Oberseiten der Decken/Tritte (Polygon, Höhe zTop) und alle Dreiecke mit nach oben zeigender Normale
|
||
// (Gelände, Dächer, Körper).
|
||
|
||
import { bulgeArc } from "../core";
|
||
import type { Opening, Project } from "../model/types";
|
||
|
||
/** Person: Radius und Körperhöhe (m); Stufen bis `STEP_UP` steigt man ohne Hindernis. */
|
||
export const PERSON_RADIUS = 0.25;
|
||
export const PERSON_HEIGHT = 1.8;
|
||
export const STEP_UP = 0.25;
|
||
|
||
const CELL = 2;
|
||
/** Mehr Zellen als das → Eintrag kommt in die Liste „gross" (immer geprüft), statt Zehntausende Zellen zu füllen. */
|
||
const MAX_CELLS_PER_ENTRY = 1024;
|
||
const EPS = 1e-6;
|
||
|
||
type V2 = readonly [number, number];
|
||
type Tri3 = readonly [number, number, number, number, number, number, number, number, number];
|
||
|
||
/** Minimalformen der Eingabe (RModel3d erfüllt sie strukturell). */
|
||
export interface WalkWall {
|
||
start: V2;
|
||
end: V2;
|
||
thickness: number;
|
||
height: number;
|
||
baseElevation: number;
|
||
holes?: ReadonlyArray<{ from: number; to: number; zBottom: number; zTop: number; door?: boolean }>;
|
||
}
|
||
export interface WalkSlab {
|
||
outline: ReadonlyArray<V2>;
|
||
zBottom: number;
|
||
zTop: number;
|
||
/** Decke des Modells (Unterseite begrenzt den Kopf); Treppentritte, Stützen u. a. nicht. */
|
||
ceiling?: boolean;
|
||
}
|
||
export interface WalkMesh {
|
||
/** Flach (x, y, z=Höhe) in Modell-Metern. */
|
||
positions: ReadonlyArray<number>;
|
||
indices: ReadonlyArray<number>;
|
||
/** `terrain`/`imported`: world-z = −y. */
|
||
kind?: string;
|
||
/** Dachnetz: nach unten zeigende Dreiecke (Dachschräge) begrenzen den Kopf. Die Dreiecke müssen nach aussen orientiert sein. */
|
||
roof?: boolean;
|
||
}
|
||
export interface WalkModelInput {
|
||
walls: ReadonlyArray<WalkWall>;
|
||
slabs: ReadonlyArray<WalkSlab>;
|
||
meshes: ReadonlyArray<WalkMesh>;
|
||
}
|
||
|
||
interface Obstacle {
|
||
x0: number;
|
||
z0: number;
|
||
x1: number;
|
||
z1: number;
|
||
/** Halbdicke. */
|
||
h: number;
|
||
y0: number;
|
||
y1: number;
|
||
}
|
||
|
||
interface Floor {
|
||
/** Dreieck (neun Zahlen, world) oder Polygon mit fester Höhe. */
|
||
tri?: Tri3;
|
||
poly?: ReadonlyArray<V2>;
|
||
y: number;
|
||
}
|
||
|
||
const key = (ix: number, iz: number): number => (ix + 5_000_000) * 10_000_000 + (iz + 5_000_000);
|
||
|
||
/** Obergrenze für Dreiecke aus Netzen (Speicher/Bauzeit); der Rest bleibt unberücksichtigt. */
|
||
export const MAX_MESH_TRIS = 600_000;
|
||
|
||
export class WalkWorld {
|
||
private obstacles: Obstacle[] = [];
|
||
private floors: Floor[] = [];
|
||
private obstacleCells = new Map<number, number[]>();
|
||
private floorCells = new Map<number, number[]>();
|
||
private bigObstacles: number[] = [];
|
||
private bigFloors: number[] = [];
|
||
private ceilings: Array<{ poly?: ReadonlyArray<V2>; tri?: Tri3; y: number }> = [];
|
||
private ceilingCells = new Map<number, number[]>();
|
||
private bigCeilings: number[] = [];
|
||
/** Zahl der Dreiecke aus Netzen, die nicht mehr aufgenommen wurden. */
|
||
skippedTris = 0;
|
||
|
||
constructor(input: WalkModelInput) {
|
||
for (const w of input.walls) this.addWall(w);
|
||
for (const s of input.slabs) this.addSlab(s);
|
||
let budget = MAX_MESH_TRIS;
|
||
for (const m of input.meshes) {
|
||
const n = Math.floor(m.indices.length / 3);
|
||
if (n > budget) {
|
||
this.skippedTris += n;
|
||
continue;
|
||
}
|
||
budget -= n;
|
||
this.addMesh(m);
|
||
}
|
||
}
|
||
|
||
get obstacleCount(): number {
|
||
return this.obstacles.length;
|
||
}
|
||
get floorCount(): number {
|
||
return this.floors.length;
|
||
}
|
||
|
||
// ── Aufbau ─────────────────────────────────────────────────────────────────────────────────────────────────
|
||
|
||
private addObstacle(o: Obstacle): void {
|
||
if (!(o.y1 > o.y0) || ![o.x0, o.z0, o.x1, o.z1, o.h, o.y0, o.y1].every(Number.isFinite)) return;
|
||
const id = this.obstacles.length;
|
||
this.obstacles.push(o);
|
||
const r = o.h;
|
||
this.index(this.obstacleCells, this.bigObstacles, id, Math.min(o.x0, o.x1) - r, Math.min(o.z0, o.z1) - r, Math.max(o.x0, o.x1) + r, Math.max(o.z0, o.z1) + r);
|
||
}
|
||
|
||
private addFloor(f: Floor, x0: number, z0: number, x1: number, z1: number): void {
|
||
const id = this.floors.length;
|
||
this.floors.push(f);
|
||
this.index(this.floorCells, this.bigFloors, id, x0, z0, x1, z1);
|
||
}
|
||
|
||
private index(cells: Map<number, number[]>, big: number[], id: number, x0: number, z0: number, x1: number, z1: number): void {
|
||
const ix0 = Math.floor(x0 / CELL);
|
||
const ix1 = Math.floor(x1 / CELL);
|
||
const iz0 = Math.floor(z0 / CELL);
|
||
const iz1 = Math.floor(z1 / CELL);
|
||
if (!(Number.isFinite(ix0 + ix1 + iz0 + iz1)) || (ix1 - ix0 + 1) * (iz1 - iz0 + 1) > MAX_CELLS_PER_ENTRY) {
|
||
big.push(id);
|
||
return;
|
||
}
|
||
for (let ix = ix0; ix <= ix1; ix++) {
|
||
for (let iz = iz0; iz <= iz1; iz++) {
|
||
const k = key(ix, iz);
|
||
const l = cells.get(k);
|
||
if (l) l.push(id);
|
||
else cells.set(k, [id]);
|
||
}
|
||
}
|
||
}
|
||
|
||
private addWall(w: WalkWall): void {
|
||
const [sx, sz] = w.start;
|
||
const [ex, ez] = w.end;
|
||
const len = Math.hypot(ex - sx, ez - sz);
|
||
if (!(len > EPS) || !(w.height > 0)) return;
|
||
const ux = (ex - sx) / len;
|
||
const uz = (ez - sz) / len;
|
||
const h = Math.max(0, w.thickness) / 2;
|
||
const base = w.baseElevation;
|
||
const top = base + w.height;
|
||
const at = (s: number): V2 => [sx + ux * s, sz + uz * s];
|
||
const piece = (a: number, b: number, y0: number, y1: number) => {
|
||
if (b - a < EPS || y1 - y0 < EPS) return;
|
||
const p = at(a);
|
||
const q = at(b);
|
||
this.addObstacle({ x0: p[0], z0: p[1], x1: q[0], z1: q[1], h, y0, y1 });
|
||
};
|
||
const holes = [...(w.holes ?? [])]
|
||
.map((o) => ({ from: Math.max(0, Math.min(o.from, o.to)), to: Math.min(len, Math.max(o.from, o.to)), zBottom: o.zBottom, zTop: o.zTop, door: o.door }))
|
||
.filter((o) => o.to - o.from > EPS)
|
||
.sort((a, b) => a.from - b.from);
|
||
let cursor = 0;
|
||
for (const o of holes) {
|
||
piece(cursor, o.from, base, top);
|
||
// Brüstung unter und Sturz über dem Loch.
|
||
piece(o.from, o.to, base, Math.min(top, o.zBottom));
|
||
piece(o.from, o.to, Math.max(base, o.zTop), top);
|
||
// Fenster: die Glasebene sperrt, auch wo die Brüstung niedriger als die Stufenhöhe oder 0 ist.
|
||
if (!(o.door ?? o.zBottom <= base + EPS)) {
|
||
const m = (o.from + o.to) / 2;
|
||
const half = (o.to - o.from) / 2;
|
||
const p = at(m - half);
|
||
const q = at(m + half);
|
||
this.addObstacle({ x0: p[0], z0: p[1], x1: q[0], z1: q[1], h: 0.02, y0: Math.max(base, o.zBottom), y1: Math.min(top, o.zTop) });
|
||
}
|
||
cursor = Math.max(cursor, o.to);
|
||
}
|
||
piece(cursor, len, base, top);
|
||
}
|
||
|
||
private addSlab(s: WalkSlab): void {
|
||
const pts = s.outline.filter((p) => Number.isFinite(p[0]) && Number.isFinite(p[1]));
|
||
if (pts.length < 3 || !(s.zTop >= s.zBottom)) return;
|
||
let minX = Infinity;
|
||
let minZ = Infinity;
|
||
let maxX = -Infinity;
|
||
let maxZ = -Infinity;
|
||
for (const p of pts) {
|
||
minX = Math.min(minX, p[0]);
|
||
maxX = Math.max(maxX, p[0]);
|
||
minZ = Math.min(minZ, p[1]);
|
||
maxZ = Math.max(maxZ, p[1]);
|
||
}
|
||
this.addFloor({ poly: pts, y: s.zTop }, minX, minZ, maxX, maxZ);
|
||
if (s.ceiling) {
|
||
const id = this.ceilings.length;
|
||
this.ceilings.push({ poly: pts, y: s.zBottom });
|
||
this.index(this.ceilingCells, this.bigCeilings, id, minX, minZ, maxX, maxZ);
|
||
}
|
||
for (let i = 0; i < pts.length; i++) {
|
||
const a = pts[i];
|
||
const b = pts[(i + 1) % pts.length];
|
||
this.addObstacle({ x0: a[0], z0: a[1], x1: b[0], z1: b[1], h: 0, y0: s.zBottom, y1: s.zTop });
|
||
}
|
||
}
|
||
|
||
private addMesh(m: WalkMesh): void {
|
||
const flip = m.kind === "terrain" || m.kind === "imported" ? -1 : 1;
|
||
const p = m.positions;
|
||
const idx = m.indices;
|
||
const vx = (i: number) => p[i * 3];
|
||
const vy = (i: number) => p[i * 3 + 2];
|
||
const vz = (i: number) => p[i * 3 + 1] * flip;
|
||
for (let t = 0; t + 2 < idx.length; t += 3) {
|
||
const a = idx[t];
|
||
const b = idx[t + 1];
|
||
const c = idx[t + 2];
|
||
if (a * 3 + 2 >= p.length || b * 3 + 2 >= p.length || c * 3 + 2 >= p.length) continue;
|
||
const x = [vx(a), vx(b), vx(c)];
|
||
const y = [vy(a), vy(b), vy(c)];
|
||
const z = [vz(a), vz(b), vz(c)];
|
||
if (![...x, ...y, ...z].every(Number.isFinite)) continue;
|
||
// Normale (world), Y-Anteil entscheidet: Boden, Wand oder Decke/Unterseite.
|
||
const e1 = [x[1] - x[0], y[1] - y[0], z[1] - z[0]];
|
||
const e2 = [x[2] - x[0], y[2] - y[0], z[2] - z[0]];
|
||
const nx = e1[1] * e2[2] - e1[2] * e2[1];
|
||
const ny = e1[2] * e2[0] - e1[0] * e2[2];
|
||
const nz = e1[0] * e2[1] - e1[1] * e2[0];
|
||
const nl = Math.hypot(nx, ny, nz);
|
||
if (!(nl > 1e-12)) continue;
|
||
// Das Modell ist rechtshändig (x, y, z=Höhe); in world (x, z, y) kippt die Orientierung: Betrag zählt, nicht das Vorzeichen.
|
||
const upness = Math.abs(ny) / nl;
|
||
// Dachschräge: Unterseite (Normale nach unten, Modell-Orientierung gegen den Uhrzeigersinn von aussen gesehen) als Kopfgrenze.
|
||
if (m.roof && flip === 1) {
|
||
// world (x, Höhe, y) spiegelt das Modell: die Höhenkomponente der Modell-Normalen ist −ny, „nach unten“ also ny > 0.
|
||
if (ny / nl > 0.3) {
|
||
const id = this.ceilings.length;
|
||
this.ceilings.push({ tri: [x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2]], y: 0 });
|
||
this.index(this.ceilingCells, this.bigCeilings, id, Math.min(...x), Math.min(...z), Math.max(...x), Math.max(...z));
|
||
}
|
||
}
|
||
if (upness > 0.5) {
|
||
// Dreieck als Boden. Ohne Orientierung gilt jede flache Fläche als begehbar (auch Unterseiten): die Bodenabfrage
|
||
// nimmt ohnehin nur Flächen bis Fusshöhe + Stufe, Dächer und Decken darüber bleiben aussen vor.
|
||
this.addFloor(
|
||
{ tri: [x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2]], y: 0 },
|
||
Math.min(...x),
|
||
Math.min(...z),
|
||
Math.max(...x),
|
||
Math.max(...z),
|
||
);
|
||
} else {
|
||
// Senkrechtes Dreieck: die zwei am weitesten entfernten Punkte in der Grundrissebene bilden die Strecke.
|
||
let best = 0;
|
||
let bi = 0;
|
||
let bj = 1;
|
||
for (const [i, j] of [[0, 1], [1, 2], [0, 2]] as const) {
|
||
const d = Math.hypot(x[i] - x[j], z[i] - z[j]);
|
||
if (d > best) {
|
||
best = d;
|
||
bi = i;
|
||
bj = j;
|
||
}
|
||
}
|
||
if (best < 1e-3) continue;
|
||
this.addObstacle({ x0: x[bi], z0: z[bi], x1: x[bj], z1: z[bj], h: 0, y0: Math.min(...y), y1: Math.max(...y) });
|
||
}
|
||
}
|
||
}
|
||
|
||
// ── Abfragen ───────────────────────────────────────────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* Höhe des Bodens unter (x, z): die höchste begehbare Fläche mit Höhe ≤ `maxY`. `null`, wenn dort gar nichts liegt
|
||
* (ausserhalb des Modells). Genau ein Ray-Test nach unten, nur gegen die Rasterzelle.
|
||
*/
|
||
groundAt(x: number, z: number, maxY: number): number | null {
|
||
let best: number | null = null;
|
||
const test = (id: number) => {
|
||
const f = this.floors[id];
|
||
let y: number | null = null;
|
||
if (f.poly) {
|
||
if (pointInPoly(x, z, f.poly)) y = f.y;
|
||
} else if (f.tri) {
|
||
y = triHeight(f.tri, x, z);
|
||
}
|
||
if (y !== null && y <= maxY + EPS && (best === null || y > best)) best = y;
|
||
};
|
||
const cell = this.floorCells.get(key(Math.floor(x / CELL), Math.floor(z / CELL)));
|
||
if (cell) for (const id of cell) test(id);
|
||
for (const id of this.bigFloors) test(id);
|
||
return best;
|
||
}
|
||
|
||
/**
|
||
* Bewegung (x, z) → (x + dx, z + dz) einer Person mit den Füssen auf `feetY`. Teilschritte gegen Durchtunneln;
|
||
* Hindernisse werden herausgeschoben, die Person gleitet also an Wänden entlang. Gibt die neue Lage zurück.
|
||
*/
|
||
move(x: number, z: number, dx: number, dz: number, feetY: number, radius: number = PERSON_RADIUS): V2 {
|
||
const dist = Math.hypot(dx, dz);
|
||
if (!(dist > 0) || !Number.isFinite(dist)) return [x, z];
|
||
const steps = Math.max(1, Math.ceil(dist / (radius * 0.6)));
|
||
const sx = dx / steps;
|
||
const sz = dz / steps;
|
||
let px = x;
|
||
let pz = z;
|
||
for (let i = 0; i < steps; i++) {
|
||
const prevX = px;
|
||
const prevZ = pz;
|
||
px += sx;
|
||
pz += sz;
|
||
[px, pz] = this.pushOut(px, pz, prevX, prevZ, feetY, radius);
|
||
// Kopf unter einer Decke: nicht hineingehen (wer schon darunter steht, bleibt beweglich).
|
||
if (this.headBlocked(px, pz, feetY) && !this.headBlocked(prevX, prevZ, feetY)) {
|
||
px = prevX;
|
||
pz = prevZ;
|
||
}
|
||
}
|
||
return [px, pz];
|
||
}
|
||
|
||
/** Stösst der Kopf bei (x, z) an eine Deckenunterseite (Decke über den Füssen, tiefer als die Körperhöhe, kein Loch)? */
|
||
headBlocked(x: number, z: number, feetY: number): boolean {
|
||
const hi = feetY + PERSON_HEIGHT;
|
||
let blocked = false;
|
||
const test = (id: number) => {
|
||
const c = this.ceilings[id];
|
||
if (blocked) return;
|
||
const y = c.poly ? (pointInPoly(x, z, c.poly) ? c.y : null) : c.tri ? triHeight(c.tri, x, z) : null;
|
||
if (y !== null && y > feetY + EPS && y < hi) blocked = true;
|
||
};
|
||
const cell = this.ceilingCells.get(key(Math.floor(x / CELL), Math.floor(z / CELL)));
|
||
if (cell) for (const id of cell) test(id);
|
||
for (const id of this.bigCeilings) test(id);
|
||
return blocked;
|
||
}
|
||
|
||
/** Ist die Person an dieser Stelle frei (kein Hindernis im Körperband)? */
|
||
isFree(x: number, z: number, feetY: number, radius: number = PERSON_RADIUS): boolean {
|
||
let free = true;
|
||
this.forNearby(x, z, radius, feetY, (o) => {
|
||
if (segDist(x, z, o) < o.h + radius - 1e-4) free = false;
|
||
});
|
||
return free;
|
||
}
|
||
|
||
private pushOut(px0: number, pz0: number, prevX: number, prevZ: number, feetY: number, radius: number): V2 {
|
||
let px = px0;
|
||
let pz = pz0;
|
||
// Mehrere Durchgänge: ein Schub kann in die nächste Wand führen (Ecke).
|
||
for (let pass = 0; pass < 4; pass++) {
|
||
let moved = false;
|
||
this.forNearby(px, pz, radius, feetY, (o) => {
|
||
const [cx, cz] = closestOnSeg(px, pz, o);
|
||
let nx = px - cx;
|
||
let nz = pz - cz;
|
||
const d = Math.hypot(nx, nz);
|
||
const need = o.h + radius;
|
||
if (d >= need - 1e-9) return;
|
||
if (d > 1e-9) {
|
||
nx /= d;
|
||
nz /= d;
|
||
} else {
|
||
// Genau auf der Achse: zur Seite schieben, von der die Person kam.
|
||
const ax = o.x1 - o.x0;
|
||
const az = o.z1 - o.z0;
|
||
const al = Math.hypot(ax, az) || 1;
|
||
nx = -az / al;
|
||
nz = ax / al;
|
||
if (nx * (prevX - cx) + nz * (prevZ - cz) < 0) {
|
||
nx = -nx;
|
||
nz = -nz;
|
||
}
|
||
}
|
||
px = cx + nx * need;
|
||
pz = cz + nz * need;
|
||
moved = true;
|
||
});
|
||
if (!moved) break;
|
||
}
|
||
return [px, pz];
|
||
}
|
||
|
||
/** Alle Hindernisse im Umkreis, deren Höhenband den Körper (Füsse + Stufe … Füsse + Körperhöhe) trifft. */
|
||
private forNearby(x: number, z: number, radius: number, feetY: number, fn: (o: Obstacle) => void): void {
|
||
const lo = feetY + STEP_UP + EPS;
|
||
const hi = feetY + PERSON_HEIGHT;
|
||
const seen = new Set<number>();
|
||
const visit = (id: number) => {
|
||
if (seen.has(id)) return;
|
||
seen.add(id);
|
||
const o = this.obstacles[id];
|
||
if (o.y1 <= lo || o.y0 >= hi) return;
|
||
fn(o);
|
||
};
|
||
const ix0 = Math.floor((x - radius - 1e-9) / CELL);
|
||
const ix1 = Math.floor((x + radius + 1e-9) / CELL);
|
||
const iz0 = Math.floor((z - radius - 1e-9) / CELL);
|
||
const iz1 = Math.floor((z + radius + 1e-9) / CELL);
|
||
for (let ix = ix0; ix <= ix1; ix++) {
|
||
for (let iz = iz0; iz <= iz1; iz++) {
|
||
const l = this.obstacleCells.get(key(ix, iz));
|
||
if (l) for (const id of l) visit(id);
|
||
}
|
||
}
|
||
for (const id of this.bigObstacles) visit(id);
|
||
}
|
||
}
|
||
|
||
// ── Geometrie ────────────────────────────────────────────────────────────────────────────────────────────────
|
||
|
||
function closestOnSeg(px: number, pz: number, o: Obstacle): V2 {
|
||
const ax = o.x1 - o.x0;
|
||
const az = o.z1 - o.z0;
|
||
const l2 = ax * ax + az * az;
|
||
const t = l2 > 0 ? Math.max(0, Math.min(1, ((px - o.x0) * ax + (pz - o.z0) * az) / l2)) : 0;
|
||
return [o.x0 + ax * t, o.z0 + az * t];
|
||
}
|
||
|
||
function segDist(px: number, pz: number, o: Obstacle): number {
|
||
const [cx, cz] = closestOnSeg(px, pz, o);
|
||
return Math.hypot(px - cx, pz - cz);
|
||
}
|
||
|
||
/** Punkt im Polygon (gerade/ungerade Regel). */
|
||
export function pointInPoly(x: number, z: number, poly: ReadonlyArray<V2>): boolean {
|
||
let inside = false;
|
||
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
||
const [xi, zi] = poly[i];
|
||
const [xj, zj] = poly[j];
|
||
if (zi > z !== zj > z && x < ((xj - xi) * (z - zi)) / (zj - zi) + xi) inside = !inside;
|
||
}
|
||
return inside;
|
||
}
|
||
|
||
/** Höhe des Dreiecks (world) über (x, z); `null`, wenn der Punkt ausserhalb liegt (kleine Toleranz gegen Fugen). */
|
||
function triHeight(t: Tri3, x: number, z: number): number | null {
|
||
const [ax, ay, az, bx, by, bz, cx, cy, cz] = t;
|
||
const d = (bz - cz) * (ax - cx) + (cx - bx) * (az - cz);
|
||
if (Math.abs(d) < 1e-12) return null;
|
||
const l1 = ((bz - cz) * (x - cx) + (cx - bx) * (z - cz)) / d;
|
||
const l2 = ((cz - az) * (x - cx) + (ax - cx) * (z - cz)) / d;
|
||
const l3 = 1 - l1 - l2;
|
||
const tol = -1e-6;
|
||
if (l1 < tol || l2 < tol || l3 < tol) return null;
|
||
return l1 * ay + l2 * by + l3 * cy;
|
||
}
|
||
|
||
// ── Eingabe aus dem 3D-Modell ───────────────────────────────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* Mindestanteil des Lochs (entlang der Achse), den die Öffnung überdecken muss. Der Anteil am Loch, nicht Schnitt/Vereinigung:
|
||
* ein Loch in einem Sehnenstück einer Bogenwand ist oft nur ein Teil der Öffnung.
|
||
*/
|
||
const HOLE_MATCH = 0.5;
|
||
|
||
/** Lage eines Punktes entlang der Wandachse (m ab Wandanfang): bei Geraden die Projektion, bei Bogenwänden die Bogenlänge. */
|
||
function axisPosition(pw: Pick<Project["walls"][number], "start" | "end" | "bulge">): ((x: number, y: number) => number) | null {
|
||
const len = Math.hypot(pw.end.x - pw.start.x, pw.end.y - pw.start.y);
|
||
if (!(len > EPS)) return null;
|
||
if (pw.bulge) {
|
||
const arc = bulgeArc(pw.start, pw.end, pw.bulge);
|
||
if (arc) {
|
||
const dir = arc.sweep >= 0 ? 1 : -1;
|
||
return (x, y) => {
|
||
// Winkel gegen den Anfang in Schwenkrichtung, auf (−π, π] gebracht (Schichtbänder liegen leicht vor dem Anfang).
|
||
let d = (Math.atan2(y - arc.center.y, x - arc.center.x) - arc.a0) * dir;
|
||
d -= 2 * Math.PI * Math.round(d / (2 * Math.PI));
|
||
return d * arc.r;
|
||
};
|
||
}
|
||
}
|
||
return (x, y) => ((x - pw.start.x) * (pw.end.x - pw.start.x) + (y - pw.start.y) * (pw.end.y - pw.start.y)) / len;
|
||
}
|
||
|
||
/**
|
||
* Das 3D-Modell (`projectToModel3d(…, { forWalk: true })`) als Kollisionseingabe, ergänzt um das, was nur das Projekt
|
||
* weiss: je Wandloch, ob die zugehörige Öffnung eine Tür ist (`Opening.kind`), und welche Platten Decken sind.
|
||
* Das Loch wird der Öffnung derselben Wand über die Achslage zugeordnet (Loch ab Stückanfang, projiziert auf die
|
||
* Wandachse; Schichtbänder weichen um den Einzug leicht ab). Bei Bogenwänden zählt die Bogenlänge. Löcher ohne Treffer bleiben ohne Vermerk.
|
||
*/
|
||
export function walkInputFrom(
|
||
model: { walls: ReadonlyArray<WalkWall & { wallId?: string }>; slabs: ReadonlyArray<WalkSlab & { ceilingId?: string }>; meshes: ReadonlyArray<WalkMesh> },
|
||
project: Pick<Project, "walls" | "openings" | "ceilings">,
|
||
): WalkModelInput {
|
||
const wallById = new Map((project.walls ?? []).map((w) => [w.id, w]));
|
||
const byWall = new Map<string, Opening[]>();
|
||
for (const o of project.openings ?? []) {
|
||
if (!o.hostWallId || !(o.width > 0) || !Number.isFinite(o.position)) continue;
|
||
const l = byWall.get(o.hostWallId);
|
||
if (l) l.push(o);
|
||
else byWall.set(o.hostWallId, [o]);
|
||
}
|
||
const ceilingIds = new Set((project.ceilings ?? []).map((c) => c.id));
|
||
const walls = model.walls.map((w): WalkWall => {
|
||
const pw = w.wallId ? wallById.get(w.wallId) : undefined;
|
||
const ops = w.wallId ? byWall.get(w.wallId) : undefined;
|
||
if (!w.holes?.length || !pw || !ops) return w;
|
||
// Lage auf der Wandachse: gerade = Projektion, Bogenwand = Bogenlänge (Öffnungen liegen auf dem Bogen).
|
||
const axisPos = axisPosition(pw);
|
||
if (!axisPos) return w;
|
||
const dx = w.end[0] - w.start[0];
|
||
const dz = w.end[1] - w.start[1];
|
||
const clen = Math.hypot(dx, dz);
|
||
if (!(clen > EPS)) return w;
|
||
const holes = w.holes.map((h) => {
|
||
// Das Loch liegt auf der Strecke des Stücks (bei Bogenwänden eine Sehne des Bogens): Endpunkte auf die Achse bringen.
|
||
const pa = axisPos(w.start[0] + (dx / clen) * Math.min(h.from, h.to), w.start[1] + (dz / clen) * Math.min(h.from, h.to));
|
||
const pb = axisPos(w.start[0] + (dx / clen) * Math.max(h.from, h.to), w.start[1] + (dz / clen) * Math.max(h.from, h.to));
|
||
const a = Math.min(pa, pb);
|
||
const b = Math.max(pa, pb);
|
||
let best: Opening | undefined;
|
||
let bestScore = HOLE_MATCH;
|
||
for (const o of ops) {
|
||
const inter = Math.min(b, o.position + o.width) - Math.max(a, o.position);
|
||
if (inter > 0 && b - a > EPS && inter / (b - a) >= bestScore) {
|
||
best = o;
|
||
bestScore = inter / (b - a);
|
||
}
|
||
}
|
||
return best ? { ...h, door: best.kind === "door" } : h;
|
||
});
|
||
return { ...w, holes };
|
||
});
|
||
const slabs = model.slabs.map((s): WalkSlab => (s.ceilingId && ceilingIds.has(s.ceilingId) ? { ...s, ceiling: true } : s));
|
||
return { walls, slabs, meshes: model.meshes };
|
||
}
|