Grundriss: Viewport-Culling für die SVG-Primitive ab 5000 Elementen
plan.primitives.map(...) erzeugte für jedes Primitiv ein SVG-Element, auch ausserhalb des sichtbaren Ausschnitts — bei einem Kataster-Import (59k Textbeschriftungen) zehntausende DOM-Knoten auf einmal. Ab 5000 Primitiven werden nur noch die gerendert, deren grobe Bounding-Box den (mit Rand erweiterten) Ausschnitt schneidet; Texte, die bei der aktuellen Zoomstufe unlesbar klein wären, entfallen ebenfalls. Hysterese: neu gerechnet wird erst, wenn der Ausschnitt die Region verlässt oder stark hineingezoomt wird. Indizes bleiben die ursprünglichen (Auswahl und Hatch-IDs indizieren direkt in plan.primitives).
This commit is contained in:
1 parent
1da6b6743b
commit
0e71a2381f
3 files changed
+463
-7
No files matched your search
+98
-7
@@ -56,6 +56,7 @@ import {
|
||||
import { DraftOverlay } from "./planView/overlays";
|
||||
import { PlanRulers } from "./planView/PlanRulers";
|
||||
import { PlanGrid } from "./planView/PlanGrid";
|
||||
import { expandRegion, primitivesInRegion, regionContains, type Bounds } from "./planView/cullPrimitives";
|
||||
|
||||
/**
|
||||
* Feld-Status der Befehlszeile fürs Cursor-HUD (Tab-Feld-Zyklus §2.7) — Typ
|
||||
@@ -64,6 +65,46 @@ import { PlanGrid } from "./planView/PlanGrid";
|
||||
*/
|
||||
export type { HudFieldsState } from "../tools/hudFormat";
|
||||
|
||||
// Viewport-Culling (s. `cullPrimitives.ts`): erst ab dieser Primitiv-Anzahl
|
||||
// aktiv — normale Projekte liegen weit darunter und bleiben unverändert.
|
||||
const CULL_THRESHOLD = 5000;
|
||||
// Puffer um `view` als Vielfaches der View-Grösse je Seite (50 % → ein
|
||||
// Schwenk muss schon über die halbe Bildschirmbreite hinausgehen, bevor neu
|
||||
// gerechnet wird).
|
||||
const CULL_MARGIN_FACTOR = 0.5;
|
||||
const cullMargin = (vb: ViewBox) => Math.max(vb.w, vb.h) * CULL_MARGIN_FACTOR;
|
||||
// Text-Primitive unter dieser Höhe (als Anteil der aktuellen View-Höhe)
|
||||
// gelten als unleserlich und werden gar nicht erst gerendert (s.
|
||||
// `isTooSmallToRead` in cullPrimitives.ts) — unabhängig von der Position.
|
||||
// 0.5 % einer typischen Bildschirmhöhe (~800 px) sind ~4 px, die Schwelle,
|
||||
// ab der Text praktisch ohnehin nicht mehr lesbar ist.
|
||||
const MIN_RELATIVE_TEXT_HEIGHT = 0.005;
|
||||
// Deutlich reingezoomt seit der letzten Neuberechnung (View < 30 % der
|
||||
// gepufferten Region) → neu rechnen, auch wenn `view` noch "drin liegt" —
|
||||
// sonst bliebe die LOD-Schwelle (an der ALTEN, grösseren View-Höhe bemessen)
|
||||
// zu streng und Text, der beim aktuellen Zoom längst lesbar wäre, bliebe
|
||||
// unsichtbar.
|
||||
const CULL_RESCALE_FACTOR = 0.3;
|
||||
|
||||
interface CullState {
|
||||
region: Bounds;
|
||||
minTextHeightVb: number;
|
||||
forViewW: number;
|
||||
forViewH: number;
|
||||
}
|
||||
function computeCullState(view: ViewBox): CullState {
|
||||
return {
|
||||
region: expandRegion(view, cullMargin(view)),
|
||||
minTextHeightVb: view.h * MIN_RELATIVE_TEXT_HEIGHT,
|
||||
forViewW: view.w,
|
||||
forViewH: view.h,
|
||||
};
|
||||
}
|
||||
function cullStateStale(s: CullState, view: ViewBox): boolean {
|
||||
if (!regionContains(s.region, view)) return true;
|
||||
return view.w < s.forViewW * CULL_RESCALE_FACTOR || view.h < s.forViewH * CULL_RESCALE_FACTOR;
|
||||
}
|
||||
|
||||
// Zoom-Grenzen relativ zur eingepassten Default-Ansicht. Kleinere viewBox-
|
||||
// Breite = stärkerer Zoom. ZOOM_MAX → max. Hineinzoomen, ZOOM_MIN → Herauszoomen.
|
||||
// Grosszügig bemessen (CAD-Anspruch: vom Milimeter-Detail bis zum Siedlungs-
|
||||
@@ -629,6 +670,44 @@ export const PlanView = forwardRef<PlanViewHandle, PlanViewProps>(
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [resetKey]);
|
||||
|
||||
// Viewport-/LOD-Culling für `primitiveEls` (s. cullPrimitives.ts) — NUR
|
||||
// aktiv bei sehr vielen Primitiven (Kataster-DXF-Import, Nutzer-Report
|
||||
// 2026-10-09: 59'391 Einzel-Textbeschriftungen liessen die App nach dem
|
||||
// Import hängen, weil React/SVG ALLE auf einmal als DOM-Knoten erzeugte,
|
||||
// egal ob sichtbar oder lesbar) — bei normalen Projekten bleibt das
|
||||
// Verhalten exakt wie zuvor (kein Overhead, `cullState` bleibt `null`).
|
||||
//
|
||||
// ZWEI Kriterien, nicht nur Position: direkt nach einem Import zeigt die
|
||||
// "eingepasste" Übersicht den GESAMTEN Umriss (bei Luzern: 2158×2583 m) —
|
||||
// technisch liegen dann ALLE 59'391 Labels "im Bild", wären aber jedes nur
|
||||
// ein paar Pixel gross und unleserlich. `minTextHeightVb` filtert zusätzlich
|
||||
// nach Lesbarkeit bei der aktuellen Zoomstufe (unabhängig von der Position) —
|
||||
// erst damit bleibt auch der ALLERERSTE Blick nach dem Import schnell.
|
||||
//
|
||||
// Puffer+Hysterese (CULL_MARGIN_FACTOR, `cullStateStale`), damit kleine
|
||||
// Schwenks/Zoom-Schritte KEINE Neuberechnung auslösen — dieselbe Überlegung
|
||||
// wie beim bestehenden `primitiveEls`-Memo weiter unten, das bewusst NICHT
|
||||
// an `view` hängt (sonst Ruckler auf WebKitGTK bei jedem Pan/Zoom, s.
|
||||
// Kommentar dort): nur bei einem GRÖSSEREN Schwenk ODER spürbarem Hinein-
|
||||
// zoomen (view schrumpft stark — reines "liegt noch drin" reicht hier
|
||||
// NICHT, ein View, der nur kleiner wird, bleibt sonst fälschlich innerhalb
|
||||
// der alten, zu grosszügigen Region) wird neu gerechnet.
|
||||
const needsCull = plan.primitives.length > CULL_THRESHOLD;
|
||||
const [cullState, setCullState] = useState<CullState | null>(() => {
|
||||
if (!needsCull) return null;
|
||||
return computeCullState(defaultView);
|
||||
});
|
||||
useEffect(() => {
|
||||
if (!needsCull) {
|
||||
if (cullState !== null) setCullState(null);
|
||||
return;
|
||||
}
|
||||
if (!cullState || cullStateStale(cullState, view)) {
|
||||
setCullState(computeCullState(view));
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [view, needsCull, plan]);
|
||||
|
||||
const svgRef = useRef<SVGSVGElement | null>(null);
|
||||
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||
|
||||
@@ -2415,14 +2494,26 @@ export const PlanView = forwardRef<PlanViewHandle, PlanViewProps>(
|
||||
return list;
|
||||
}, [plan]);
|
||||
|
||||
// Perf: die Primitiv-Elemente hängen NUR am Plan (+ Haarlinie/Massstab), NICHT
|
||||
// an `view`. Ohne Memo würde jedes Schwenken/Zoomen (das nur die viewBox ändert)
|
||||
// den gesamten Element-Baum neu erzeugen und React ihn komplett abgleichen —
|
||||
// auf WebKitGTK (Tauri-Desktop) der spürbare Ruckler. Memoisiert bleibt der Baum
|
||||
// referenzstabil, Pan/Zoom aktualisiert nur noch das viewBox-Attribut.
|
||||
// `cullState` (s. o.) reduziert bei sehr vielen Primitiven die Menge VOR
|
||||
// dem eigentlichen JSX-Mapping — gleiche Eigenschaft wie zuvor (hängt NICHT
|
||||
// direkt an `view`, nur am per Hysterese gepufferten `cullState`), behält
|
||||
// aber den URSPRÜNGLICHEN Index `i` (Auswahl/Hit-Testing + Hatch-Pattern-IDs
|
||||
// indizieren in `plan.primitives[i]`, s. `cullPrimitives.ts`). Ohne aktives
|
||||
// Culling (`cullState === null`, der Normalfall) identisch zu vorher.
|
||||
const culledIndexed = useMemo(() => {
|
||||
if (!cullState) return plan.primitives.map((p, i) => ({ p, i }));
|
||||
return primitivesInRegion(plan.primitives, cullState.region, cullState.minTextHeightVb);
|
||||
}, [plan, cullState]);
|
||||
|
||||
// Perf: die Primitiv-Elemente hängen NUR an `culledIndexed` (+ Haarlinie/
|
||||
// Massstab), NICHT an `view` direkt. Ohne Memo würde jedes Schwenken/Zoomen
|
||||
// (das nur die viewBox ändert) den gesamten Element-Baum neu erzeugen und
|
||||
// React ihn komplett abgleichen — auf WebKitGTK (Tauri-Desktop) der spürbare
|
||||
// Ruckler. Memoisiert bleibt der Baum referenzstabil, Pan/Zoom aktualisiert
|
||||
// nur noch das viewBox-Attribut (ausser `cullState` wechselt, s. o.).
|
||||
const primitiveEls = useMemo(
|
||||
() =>
|
||||
plan.primitives.map((p, i) =>
|
||||
culledIndexed.map(({ p, i }) =>
|
||||
// Bei aktivem GL zeichnet die WebGL-Ebene Poché/Linien/Bögen; das SVG wird
|
||||
// zur reinen Text-Overlay-Ebene (scharfe Schrift, DOM-Hit-Test) → hier nur
|
||||
// noch Text-Primitive. Bei aktiver WASM-Engine rastert render2d den
|
||||
@@ -2458,7 +2549,7 @@ export const PlanView = forwardRef<PlanViewHandle, PlanViewProps>(
|
||||
),
|
||||
),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- toScreen ist modulweit konstant
|
||||
[plan, hairline, paperScale, useGpuRenderer, wantWasm, editTextId],
|
||||
[culledIndexed, hairline, paperScale, useGpuRenderer, wantWasm, editTextId],
|
||||
);
|
||||
const drawingRunEls = useMemo(
|
||||
() =>
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
// Tests für `cullPrimitives.ts` — Viewport-Culling der PlanView-Primitive.
|
||||
// Zwei Eigenschaften sind regressionskritisch (s. Kopfkommentar der Datei):
|
||||
// (1) ein sichtbares Element darf NIE fälschlich wegfallen (konservative
|
||||
// Bounding-Box, lieber zu grosszügig); (2) der URSPRÜNGLICHE Index bleibt
|
||||
// erhalten (Auswahl/Hit-Testing und Hatch-Pattern-IDs indizieren direkt in
|
||||
// `plan.primitives`).
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Primitive } from "../generatePlan";
|
||||
import {
|
||||
expandRegion,
|
||||
primitiveBoundsVb,
|
||||
primitivesInRegion,
|
||||
regionContains,
|
||||
} from "./cullPrimitives";
|
||||
import { PX_PER_M, toScreen, type ViewBox } from "./geometry";
|
||||
|
||||
function line(ax: number, ay: number, bx: number, by: number): Primitive {
|
||||
return {
|
||||
kind: "line",
|
||||
a: { x: ax, y: ay },
|
||||
b: { x: bx, y: by },
|
||||
cls: "c",
|
||||
weightMm: 0.1,
|
||||
} as Primitive;
|
||||
}
|
||||
|
||||
function drawingText(x: number, y: number, text = "ABC"): Primitive {
|
||||
return {
|
||||
kind: "drawingText",
|
||||
at: { x, y },
|
||||
text,
|
||||
heightM: 0.25,
|
||||
angle: 0,
|
||||
color: "#000",
|
||||
} as Primitive;
|
||||
}
|
||||
|
||||
describe("primitiveBoundsVb", () => {
|
||||
it("line: Bbox umschliesst beide Endpunkte (viewBox-Einheiten, Y gespiegelt)", () => {
|
||||
const b = primitiveBoundsVb(line(0, 0, 2, 3))!;
|
||||
const sa = toScreen({ x: 0, y: 0 });
|
||||
const sb = toScreen({ x: 2, y: 3 });
|
||||
expect(b.minX).toBeCloseTo(Math.min(sa.x, sb.x), 6);
|
||||
expect(b.maxX).toBeCloseTo(Math.max(sa.x, sb.x), 6);
|
||||
expect(b.minY).toBeCloseTo(Math.min(sa.y, sb.y), 6);
|
||||
expect(b.maxY).toBeCloseTo(Math.max(sa.y, sb.y), 6);
|
||||
});
|
||||
|
||||
it("polygon: Bbox umschliesst alle Punkte", () => {
|
||||
const p: Primitive = {
|
||||
kind: "polygon",
|
||||
pts: [{ x: 0, y: 0 }, { x: 5, y: 0 }, { x: 5, y: 2 }, { x: 0, y: 2 }],
|
||||
fill: "#fff",
|
||||
stroke: "#000",
|
||||
strokeWidthMm: 0.1,
|
||||
hatch: { pattern: "none", scale: 1, angle: 0, color: "#000", lineWeight: 0.1, dash: null } as any,
|
||||
} as Primitive;
|
||||
const b = primitiveBoundsVb(p)!;
|
||||
expect(b.maxX - b.minX).toBeCloseTo(5 * PX_PER_M, 3);
|
||||
expect(b.maxY - b.minY).toBeCloseTo(2 * PX_PER_M, 3);
|
||||
});
|
||||
|
||||
it("leeres Polygon liefert null (degeneriert, wird nie gerendert)", () => {
|
||||
const p: Primitive = {
|
||||
kind: "polygon",
|
||||
pts: [],
|
||||
fill: "#fff",
|
||||
stroke: "#000",
|
||||
strokeWidthMm: 0.1,
|
||||
hatch: { pattern: "none", scale: 1, angle: 0, color: "#000", lineWeight: 0.1, dash: null } as any,
|
||||
} as Primitive;
|
||||
expect(primitiveBoundsVb(p)).toBeNull();
|
||||
});
|
||||
|
||||
it("arc/drawingCircle: Bbox ist der volle Kreis um das Zentrum", () => {
|
||||
const p: Primitive = { kind: "drawingCircle", center: { x: 1, y: 1 }, r: 3, fill: "none", stroke: "#000", weightMm: 0.1 } as Primitive;
|
||||
const b = primitiveBoundsVb(p)!;
|
||||
const c = toScreen({ x: 1, y: 1 });
|
||||
expect(b.minX).toBeCloseTo(c.x - 3 * PX_PER_M, 3);
|
||||
expect(b.maxX).toBeCloseTo(c.x + 3 * PX_PER_M, 3);
|
||||
});
|
||||
|
||||
it("drawingText: längerer Text ergibt eine grössere Bbox als kürzerer", () => {
|
||||
const short = primitiveBoundsVb(drawingText(0, 0, "A"))!;
|
||||
const long = primitiveBoundsVb(drawingText(0, 0, "A".repeat(50)))!;
|
||||
expect(long.maxX - long.minX).toBeGreaterThan(short.maxX - short.minX);
|
||||
});
|
||||
|
||||
it("drawingText: Bbox ist um den Anker ZENTRIERT (rotationsunabhängig)", () => {
|
||||
const b = primitiveBoundsVb(drawingText(0, 0, "Hallo"))!;
|
||||
const c = toScreen({ x: 0, y: 0 });
|
||||
expect((b.minX + b.maxX) / 2).toBeCloseTo(c.x, 3);
|
||||
expect((b.minY + b.maxY) / 2).toBeCloseTo(c.y, 3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("expandRegion / regionContains", () => {
|
||||
const view: ViewBox = { x: 0, y: 0, w: 100, h: 100 };
|
||||
|
||||
it("expandRegion vergrössert nach allen vier Seiten um genau `margin`", () => {
|
||||
const r = expandRegion(view, 20);
|
||||
expect(r).toEqual({ minX: -20, minY: -20, maxX: 120, maxY: 120 });
|
||||
});
|
||||
|
||||
it("regionContains: eine Region enthält die View, aus der sie gebaut wurde", () => {
|
||||
const r = expandRegion(view, 20);
|
||||
expect(regionContains(r, view)).toBe(true);
|
||||
});
|
||||
|
||||
it("regionContains: false, sobald die View über den Rand hinausragt", () => {
|
||||
const r = expandRegion(view, 20);
|
||||
expect(regionContains(r, { x: -25, y: 0, w: 100, h: 100 })).toBe(false);
|
||||
expect(regionContains(r, { x: 0, y: 0, w: 141, h: 100 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("primitivesInRegion", () => {
|
||||
it("behält NUR Primitive, deren Bbox die Region schneidet", () => {
|
||||
// Region deckt (viewBox-Einheiten) x∈[0,200] y∈[-200,0] ab → Modell
|
||||
// x∈[0, 200/PX_PER_M] y∈[0, 200/PX_PER_M] (Y gespiegelt, s. toScreen).
|
||||
const prims = [line(0, 0, 1, 1), line(1000, 1000, 1001, 1001), line(1.5, 1.5, 2, 1.8)];
|
||||
const region = expandRegion({ x: 0, y: -200, w: 200, h: 200 }, 0);
|
||||
const kept = primitivesInRegion(prims, region);
|
||||
expect(kept.map((k) => k.i)).toEqual([0, 2]);
|
||||
});
|
||||
|
||||
it("behält den URSPRÜNGLICHEN Index auch nach dem Herausfiltern (kein Renummerieren)", () => {
|
||||
const prims = [line(1000, 1000, 1001, 1001), line(0, 0, 1, 1), line(1000, 1000, 1001, 1001)];
|
||||
const region = expandRegion({ x: 0, y: -200, w: 200, h: 200 }, 0);
|
||||
const kept = primitivesInRegion(prims, region);
|
||||
expect(kept).toHaveLength(1);
|
||||
expect(kept[0].i).toBe(1); // NICHT 0 — Index muss der im Original-Array bleiben.
|
||||
expect(kept[0].p).toBe(prims[1]);
|
||||
});
|
||||
|
||||
it("leere Eingabe liefert leeres Ergebnis", () => {
|
||||
expect(primitivesInRegion([], expandRegion({ x: 0, y: 0, w: 10, h: 10 }, 0))).toEqual([]);
|
||||
});
|
||||
|
||||
describe("LOD-Filter (minTextHeightVb) — Nutzer-Report 2026-10-09", () => {
|
||||
// Regression: der eingepasste Übersichts-Ausschnitt direkt nach einem
|
||||
// Kataster-Import deckt den GESAMTEN Umriss ab — rein positionell liegen
|
||||
// dann ALLE Text-Labels "im Bild". Ohne Grössen-Filter würde also genau
|
||||
// der Fall, der den Hänger ausgelöst hat, NICHT entschärft.
|
||||
const bigRegion = expandRegion({ x: -1_000_000, y: -1_000_000, w: 2_000_000, h: 2_000_000 }, 0);
|
||||
|
||||
it("ohne minTextHeightVb (0, Default): auch winzige Texte bleiben drin", () => {
|
||||
const tiny: Primitive = {
|
||||
kind: "drawingText", at: { x: 0, y: 0 }, text: "x", heightM: 0.001, angle: 0, color: "#000",
|
||||
} as Primitive;
|
||||
expect(primitivesInRegion([tiny], bigRegion)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("mit minTextHeightVb: ein winziges Label (weit unter der Schwelle) fällt weg", () => {
|
||||
const tiny: Primitive = {
|
||||
kind: "drawingText", at: { x: 0, y: 0 }, text: "x", heightM: 0.001, angle: 0, color: "#000",
|
||||
} as Primitive;
|
||||
expect(primitivesInRegion([tiny], bigRegion, 1000)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("mit minTextHeightVb: ein grosses Label (über der Schwelle) bleibt drin", () => {
|
||||
const big: Primitive = {
|
||||
kind: "drawingText", at: { x: 0, y: 0 }, text: "x", heightM: 1000, angle: 0, color: "#000",
|
||||
} as Primitive;
|
||||
expect(primitivesInRegion([big], bigRegion, 1000)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("Geometrie-Primitive (line/polygon) bleiben vom LOD-Filter UNBERÜHRT, egal wie klein", () => {
|
||||
const tinyLine = line(0, 0, 0.0001, 0.0001);
|
||||
expect(primitivesInRegion([tinyLine], bigRegion, 1_000_000)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("Raum-Stempel (kind:\"text\") bleiben vom LOD-Filter UNBERÜHRT (selten, nicht die Masse)", () => {
|
||||
const stamp: Primitive = {
|
||||
kind: "text", at: { x: 0, y: 0 }, doc: { blocks: [] } as any, extraLines: [], basePt: 1, color: "#000",
|
||||
} as Primitive;
|
||||
expect(primitivesInRegion([stamp], bigRegion, 1_000_000)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("realistisches Szenario: Kataster-Übersicht (grosser Umriss) filtert winzige Labels, Nahaufnahme nicht", () => {
|
||||
const label: Primitive = {
|
||||
kind: "drawingText", at: { x: 0, y: 0 }, text: "Parzelle 123", heightM: 0.25, angle: 0, color: "#000",
|
||||
} as Primitive;
|
||||
// Eingepasst auf einen 2000 m hohen Umriss (wie beim echten Luzern-Import).
|
||||
const overviewMinHeight = 2000 * PX_PER_M * 0.005;
|
||||
expect(primitivesInRegion([label], bigRegion, overviewMinHeight)).toHaveLength(0);
|
||||
// Herangezoomt auf 50 m.
|
||||
const zoomedMinHeight = 50 * PX_PER_M * 0.005;
|
||||
expect(primitivesInRegion([label], bigRegion, zoomedMinHeight)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,172 @@
|
||||
// Viewport-Culling für `PlanView`'s Primitiv-Liste — Nutzer-Report
|
||||
// 2026-10-09: eine DXF-Kataster-Datei mit 59'391 Einzel-Textbeschriftungen
|
||||
// liess die App nach dem Import "hängen". Ursache: `plan.primitives.map(...)`
|
||||
// erzeugt für JEDES Primitiv ein SVG-Element, UNABHÄNGIG davon, ob es im
|
||||
// aktuell sichtbaren Ausschnitt liegt — bei 59'391 Text-Primitiven (jedes mit
|
||||
// eigenem Rich-Text-Layout, s. `primitives.tsx` Fall `"text"`/`"drawingText"`)
|
||||
// bedeutet das 59'391 React-Elemente + DOM-Knoten AUF EINMAL, obwohl zu jedem
|
||||
// Zeitpunkt nur ein Bruchteil davon im Bild ist.
|
||||
//
|
||||
// Diese Datei ist bewusst von PlanView.tsx GETRENNT (reine Funktionen, keine
|
||||
// Hooks/JSX) — leicht testbar, und die React-Integration (Hysterese-Puffer,
|
||||
// damit kleine Schwenks keine Neuberechnung auslösen, s. Kommentar bei
|
||||
// `primitiveEls` in PlanView.tsx) bleibt dort.
|
||||
|
||||
import type { Vec2 } from "../../model/types";
|
||||
import type { Primitive } from "../generatePlan";
|
||||
import { PX_PER_M, toScreen, type ViewBox } from "./geometry";
|
||||
|
||||
export interface Bounds {
|
||||
minX: number;
|
||||
minY: number;
|
||||
maxX: number;
|
||||
maxY: number;
|
||||
}
|
||||
|
||||
function expandByPoint(b: Bounds | null, p: Vec2): Bounds {
|
||||
const s = toScreen(p);
|
||||
if (!b) return { minX: s.x, maxX: s.x, minY: s.y, maxY: s.y };
|
||||
return {
|
||||
minX: Math.min(b.minX, s.x),
|
||||
maxX: Math.max(b.maxX, s.x),
|
||||
minY: Math.min(b.minY, s.y),
|
||||
maxY: Math.max(b.maxY, s.y),
|
||||
};
|
||||
}
|
||||
|
||||
function circleBounds(center: Vec2, rModel: number): Bounds {
|
||||
const c = toScreen(center);
|
||||
const rVb = Math.abs(rModel) * PX_PER_M;
|
||||
return { minX: c.x - rVb, maxX: c.x + rVb, minY: c.y - rVb, maxY: c.y + rVb };
|
||||
}
|
||||
|
||||
// Pauschale Text-Schätzungen für die konservative Bounding-Box — lieber zu
|
||||
// grosszügig (ein unsichtbares Element bleibt gerendert) als zu knapp (ein
|
||||
// sichtbares Element würde fälschlich wegfallen). Kein echtes Font-Layout
|
||||
// hier — das wäre selbst wieder der teure Schritt, den wir vermeiden wollen.
|
||||
const AVG_CHAR_WIDTH_FACTOR = 0.65; // Breite ≈ Zeichenzahl × Höhe × Faktor
|
||||
const TEXT_LINE_HEIGHT_FACTOR = 1.3;
|
||||
/** Pauschalradius (Meter) für `kind:"text"` (Rich-Text-Raumstempel) — ohne
|
||||
* Doc-Layout ist die reale Breite hier nicht günstig zu schätzen; Raum-
|
||||
* stempel sind aber selten (nicht der Massenfall aus Kataster-Importen). */
|
||||
const ROOM_STAMP_PAD_M = 4;
|
||||
|
||||
/**
|
||||
* Grobe (konservative) Bounding-Box eines Primitivs in viewBox-Einheiten —
|
||||
* NUR fürs Viewport-Culling gedacht, nicht fürs exakte Rendering. `null` bei
|
||||
* leerer/entarteter Geometrie (z. B. 0-Punkt-Polygon) — solche Primitive
|
||||
* werden nie gerendert, also auch beim Culling übersprungen (sicher: sie
|
||||
* landeten so oder so als `null` im JSX, s. `renderPrimitive`).
|
||||
*/
|
||||
export function primitiveBoundsVb(p: Primitive): Bounds | null {
|
||||
switch (p.kind) {
|
||||
case "polygon": {
|
||||
if (p.pts.length === 0) return null;
|
||||
let b: Bounds | null = null;
|
||||
for (const pt of p.pts) b = expandByPoint(b, pt);
|
||||
return b;
|
||||
}
|
||||
case "line":
|
||||
return expandByPoint(expandByPoint(null, p.a), p.b);
|
||||
case "arc":
|
||||
// Volle Kreis-Bbox statt Bogen-Spanne — überschätzt, aber einfach und
|
||||
// immer sicher (der Bogen liegt stets innerhalb dieses Kreises).
|
||||
return circleBounds(p.center, p.r);
|
||||
case "drawingCircle":
|
||||
return circleBounds(p.center, p.r);
|
||||
case "drawingArc":
|
||||
return circleBounds(p.center, p.r);
|
||||
case "drawingImage":
|
||||
return expandByPoint(expandByPoint(null, p.min), p.max);
|
||||
case "drawingTextPath": {
|
||||
if (p.pathPts.length === 0) return null;
|
||||
let b: Bounds | null = null;
|
||||
for (const pt of p.pathPts) b = expandByPoint(b, pt);
|
||||
const padVb = p.heightM * 2 * PX_PER_M;
|
||||
return b && {
|
||||
minX: b.minX - padVb,
|
||||
maxX: b.maxX + padVb,
|
||||
minY: b.minY - padVb,
|
||||
maxY: b.maxY + padVb,
|
||||
};
|
||||
}
|
||||
case "drawingText": {
|
||||
const estW = p.text.length * p.heightM * AVG_CHAR_WIDTH_FACTOR;
|
||||
const estH = p.heightM * TEXT_LINE_HEIGHT_FACTOR;
|
||||
// Radius statt Rechteck (rotationsunabhängig korrekt, s. Kopfkommentar):
|
||||
// `angle` dreht den Text im Modell, ein Rechteck müsste mitgedreht
|
||||
// werden — der Halbdiagonal-Radius deckt jede Rotation gleichermassen ab.
|
||||
const halfDiag = 0.5 * Math.hypot(estW, estH);
|
||||
return circleBounds(p.at, halfDiag);
|
||||
}
|
||||
case "text":
|
||||
return circleBounds(p.at, ROOM_STAMP_PAD_M);
|
||||
}
|
||||
}
|
||||
|
||||
/** `region` um `margin` (viewBox-Einheiten) nach allen Seiten vergrössert. */
|
||||
export function expandRegion(view: ViewBox, margin: number): Bounds {
|
||||
return {
|
||||
minX: view.x - margin,
|
||||
minY: view.y - margin,
|
||||
maxX: view.x + view.w + margin,
|
||||
maxY: view.y + view.h + margin,
|
||||
};
|
||||
}
|
||||
|
||||
/** Ob `view` vollständig innerhalb `region` liegt (Hysterese-Test). */
|
||||
export function regionContains(region: Bounds, view: ViewBox): boolean {
|
||||
return (
|
||||
view.x >= region.minX &&
|
||||
view.x + view.w <= region.maxX &&
|
||||
view.y >= region.minY &&
|
||||
view.y + view.h <= region.maxY
|
||||
);
|
||||
}
|
||||
|
||||
function intersects(a: Bounds, b: Bounds): boolean {
|
||||
return a.minX <= b.maxX && a.maxX >= b.minX && a.minY <= b.maxY && a.maxY >= b.minY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ob ein Text-Primitiv bei der aktuellen Zoomstufe zu klein wäre, um lesbar
|
||||
* zu sein — UNABHÄNGIG von der Viewport-Position (betrifft auch den
|
||||
* eingepassten Übersichts-Ausschnitt direkt nach einem Import, wo technisch
|
||||
* ALLE Elemente "im Bild" liegen, aber bei einem kilometerweiten Umriss jedes
|
||||
* einzelne Label nur wenige Bildschirm-Pixel gross und damit unleserlich
|
||||
* wäre). `minHeightVb` ist die Mindesthöhe in viewBox-Einheiten (s.
|
||||
* `PlanView.tsx`: `view.h × MIN_RELATIVE_TEXT_HEIGHT`) — reine Text-
|
||||
* Primitive (Raumstempel, `kind:"text"`) bleiben unangetastet: sie sind
|
||||
* selten (nicht die Masse aus einem Kataster-Import) und meist bewusst
|
||||
* platziert.
|
||||
*/
|
||||
function isTooSmallToRead(p: Primitive, minHeightVb: number): boolean {
|
||||
if (minHeightVb <= 0) return false;
|
||||
if (p.kind === "drawingText" || p.kind === "drawingTextPath") {
|
||||
return p.heightM * PX_PER_M < minHeightVb;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filtert `primitives` auf die, deren (grobe) Bounding-Box `region`
|
||||
* schneidet UND (falls `minTextHeightVb > 0`) bei der aktuellen Zoomstufe
|
||||
* gross genug wäre, um lesbar zu sein — behält den URSPRÜNGLICHEN Index
|
||||
* (wichtig: Auswahl/Hit-Testing in PlanView.tsx indiziert direkt in
|
||||
* `plan.primitives[i]`, Hatch-Pattern-IDs sind `hatch-${i}` — ein
|
||||
* renummeriertes Array würde beides zerstören).
|
||||
*/
|
||||
export function primitivesInRegion(
|
||||
primitives: readonly Primitive[],
|
||||
region: Bounds,
|
||||
minTextHeightVb = 0,
|
||||
): Array<{ p: Primitive; i: number }> {
|
||||
const out: Array<{ p: Primitive; i: number }> = [];
|
||||
for (let i = 0; i < primitives.length; i++) {
|
||||
const p = primitives[i];
|
||||
if (isTooSmallToRead(p, minTextHeightVb)) continue;
|
||||
const b = primitiveBoundsVb(p);
|
||||
if (b === null || intersects(b, region)) out.push({ p, i });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user