diff --git a/src/native/resourceWindow.ts b/src/native/resourceWindow.ts index 40d6c0b3..773adb42 100644 --- a/src/native/resourceWindow.ts +++ b/src/native/resourceWindow.ts @@ -106,13 +106,27 @@ export function useResourceWindowHost( }; }, []); - // Bei jeder Projektänderung ans (evtl. offene) Ressourcen-Fenster pushen. - // `emitTo` auf ein nicht existierendes Fenster ist harmlos (kein Empfänger). + // Bei jeder Projektänderung ans Ressourcen-Fenster pushen — ABER NUR, wenn es + // offen ist. `emitTo` auf ein nicht existierendes Fenster scheitert zwar + // harmlos, serialisiert den Payload aber vorher trotzdem komplett: bei einem + // Kataster-Import (128k Zeichnungen) mehrere Sekunden Hauptthread-Last nach + // JEDER Änderung (z. B. beim Zuweisen einer Schraffur), für ein Fenster, das + // gar nicht da ist. Kurz gebündelt, damit schnelle Folgeänderungen (Ziehen) + // nicht jedes Mal das ganze Projekt serialisieren. useEffect(() => { if (!isTauriRuntime()) return; - void (async () => { - const { emitTo } = await import("@tauri-apps/api/event"); - await emitTo(RESOURCE_WINDOW_LABEL, "resource-sync-project", project).catch(() => {}); - })(); + let cancelled = false; + const timer = setTimeout(() => { + void (async () => { + const { WebviewWindow } = await import("@tauri-apps/api/webviewWindow"); + if (cancelled || !(await WebviewWindow.getByLabel(RESOURCE_WINDOW_LABEL))) return; + const { emitTo } = await import("@tauri-apps/api/event"); + await emitTo(RESOURCE_WINDOW_LABEL, "resource-sync-project", project).catch(() => {}); + })(); + }, 150); + return () => { + cancelled = true; + clearTimeout(timer); + }; }, [project]); } diff --git a/src/plan/PlanView.tsx b/src/plan/PlanView.tsx index 253a34e7..74194e2f 100644 --- a/src/plan/PlanView.tsx +++ b/src/plan/PlanView.tsx @@ -59,6 +59,7 @@ import { PlanRulers } from "./planView/PlanRulers"; import { PlanGrid } from "./planView/PlanGrid"; import { expandRegion, indexedInRegion, regionContains, type Bounds } from "./planView/cullPrimitives"; import { buildSegmentIndex, nearestSegment, pointSegDist } from "./planView/segmentIndex"; +import { buildOwnerIndex, ownedBy, type OwnerField, type OwnerIndex } from "./planView/ownerIndex"; import { endBusyAfterPaint } from "../state/busyStore"; import { createCursorStore, useCursorValue, type CursorStore } from "../state/cursorStore"; @@ -83,12 +84,16 @@ const cullMargin = (vb: ViewBox) => Math.max(vb.w, vb.h) * CULL_MARGIN_FACTOR; // 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; +// Reingezoomt seit der letzten Neuberechnung (View < 75 % der Grösse, für die +// die Lesbarkeits-Schwelle berechnet wurde) → neu rechnen, auch wenn `view` +// noch in der gepufferten Region liegt — sonst bliebe die 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. Früher 30 %: da lief das Culling +// noch über ALLE Primitive (Mio. bei Kataster-Importen) und durfte selten +// laufen; seit `svgCandidates` filtert es nur noch die SVG-Elemente (Texte, +// ~10^4–10^5) und ist billig — bei 30 % blieben Beschriftungen beim Reinzoomen +// bis zu 3,3× zu lange ausgeblendet. +const CULL_RESCALE_FACTOR = 0.75; /** Lineale, die den Cursor selbst abonnieren — nur sie rendern bei Mausbewegung. */ function LiveRulers({ store, ...rest }: Omit, "cursor"> & { store: CursorStore }) { @@ -2304,13 +2309,26 @@ export const PlanView = forwardRef( // gewählten Wand umrandet. Ohne Wand-Auswahl greift die lokale Klick- // Hervorhebung (ein einzelnes Polygon per hitIndex), z. B. für Polygone ohne // Wandbezug. + // Besitzer-Index (s. planView/ownerIndex.ts): EIN Durchgang je Plan, lazy beim + // ersten Bedarf — statt je Elementart ein `plan.primitives.filter`. + const ownerIndexRef = useRef<{ prims: readonly Primitive[]; index: OwnerIndex } | null>(null); + const primsOwnedBy = ( + field: OwnerField, + ids: ReadonlyArray | undefined, + keep: (p: Primitive) => p is K, + ): K[] => { + if (!ids || ids.length === 0) return []; + let cached = ownerIndexRef.current; + if (!cached || cached.prims !== plan.primitives) { + cached = { prims: plan.primitives, index: buildOwnerIndex(plan.primitives) }; + ownerIndexRef.current = cached; + } + return ownedBy(cached.index, plan.primitives, field, ids).filter(keep); + }; + const highlightPolys = useMemo(() => { - const ids = selectedWallIds && selectedWallIds.length ? new Set(selectedWallIds) : null; - if (ids) { - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.wallId != null && ids.has(p.wallId), - ); + if (selectedWallIds && selectedWallIds.length) { + return primsOwnedBy("wallId", selectedWallIds, (p): p is Extract => p.kind === "polygon"); } if (selected !== null) { const p = plan.primitives[selected]; @@ -2328,12 +2346,7 @@ export const PlanView = forwardRef( ? new Set(selectedDrawingIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - (p.kind === "line" || p.kind === "polygon") && - p.drawingId != null && - ids.has(p.drawingId), - ); + return primsOwnedBy("drawingId", selectedDrawingIds, (p): p is Extract => p.kind === "line" || p.kind === "polygon"); }, [plan, selectedDrawingIds]); // Hervorhebung ausgewählter TEXT-Objekte (Auswahl-Rahmen, Nutzer-Wunsch): @@ -2345,13 +2358,7 @@ export const PlanView = forwardRef( const ids = selectedDrawingIds && selectedDrawingIds.length ? new Set(selectedDrawingIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "drawingText" && - p.drawingId != null && - ids.has(p.drawingId) && - p.drawingId !== editTextId, - ); + return primsOwnedBy("drawingId", selectedDrawingIds, (p): p is Extract => p.kind === "drawingText" && p.drawingId !== editTextId); }, [plan, selectedDrawingIds, editTextId]); // Hervorhebung der selektierten Decken: alle Polygone JEDER gewählten Decke @@ -2362,10 +2369,7 @@ export const PlanView = forwardRef( ? new Set(selectedCeilingIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.ceilingId != null && ids.has(p.ceilingId), - ); + return primsOwnedBy("ceilingId", selectedCeilingIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedCeilingIds]); // Hervorhebung der selektierten Öffnungen: alle Symbol-Primitive (Linien/Bögen/ @@ -2376,12 +2380,7 @@ export const PlanView = forwardRef( ? new Set(selectedOpeningIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - (p.kind === "line" || p.kind === "arc" || p.kind === "polygon") && - p.openingId != null && - ids.has(p.openingId), - ); + return primsOwnedBy("openingId", selectedOpeningIds, (p): p is Extract => p.kind === "line" || p.kind === "arc" || p.kind === "polygon"); }, [plan, selectedOpeningIds]); // Hervorhebung der selektierten Treppen: alle Tritt-/Symbol-Primitive (Polygone @@ -2390,12 +2389,7 @@ export const PlanView = forwardRef( const ids = selectedStairIds && selectedStairIds.length ? new Set(selectedStairIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - (p.kind === "line" || p.kind === "polygon") && - p.stairId != null && - ids.has(p.stairId), - ); + return primsOwnedBy("stairId", selectedStairIds, (p): p is Extract => p.kind === "line" || p.kind === "polygon"); }, [plan, selectedStairIds]); // Hervorhebung der selektierten Räume: die Fläche JEDES gewählten Raums (Polygon @@ -2404,10 +2398,7 @@ export const PlanView = forwardRef( const ids = selectedRoomIds && selectedRoomIds.length ? new Set(selectedRoomIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.roomId != null && ids.has(p.roomId), - ); + return primsOwnedBy("roomId", selectedRoomIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedRoomIds]); // Hervorhebung der selektierten Dächer: das (unsichtbare) Traufe-Pick-Polygon @@ -2416,10 +2407,7 @@ export const PlanView = forwardRef( const ids = selectedRoofIds && selectedRoofIds.length ? new Set(selectedRoofIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.roofId != null && ids.has(p.roofId), - ); + return primsOwnedBy("roofId", selectedRoofIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedRoofIds]); // Hervorhebung der selektierten Kontext-Objekte (Gebäude/Terrain): das @@ -2431,10 +2419,7 @@ export const PlanView = forwardRef( ? new Set(selectedContextObjectIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.contextObjectId != null && ids.has(p.contextObjectId), - ); + return primsOwnedBy("contextObjectId", selectedContextObjectIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedContextObjectIds]); // Hervorhebung der selektierten Schnitt-/Ansichtslinie: das (unsichtbare) @@ -2445,10 +2430,7 @@ export const PlanView = forwardRef( ? new Set(selectedSectionLineIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.sectionLineId != null && ids.has(p.sectionLineId), - ); + return primsOwnedBy("sectionLineId", selectedSectionLineIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedSectionLineIds]); // Hervorhebung der selektierten extrudierten Körper (truck-Integration): der @@ -2460,10 +2442,7 @@ export const PlanView = forwardRef( ? new Set(selectedExtrudedSolidIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.extrudedSolidId != null && ids.has(p.extrudedSolidId), - ); + return primsOwnedBy("extrudedSolidId", selectedExtrudedSolidIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedExtrudedSolidIds]); // Hervorhebung der selektierten Stützen (Tragwerk): der Querschnitt-Umriss @@ -2472,10 +2451,7 @@ export const PlanView = forwardRef( const ids = selectedColumnIds && selectedColumnIds.length ? new Set(selectedColumnIds) : null; if (!ids) return []; - return plan.primitives.filter( - (p): p is Extract => - p.kind === "polygon" && p.columnId != null && ids.has(p.columnId), - ); + return primsOwnedBy("columnId", selectedColumnIds, (p): p is Extract => p.kind === "polygon"); }, [plan, selectedColumnIds]); // Ist eine GRUPPE gewählt (statt einzelner Elemente), zeigt die Auswahl NUR @@ -2525,7 +2501,13 @@ export const PlanView = forwardRef( // so gehren dicke Ecken sauber (kein Butt-Cap-Sprung). generatePlan schiebt die // Kanten eines Elements aufeinanderfolgend + End-an-Start verkettet ein, daher // genügt greedy-Anhängen benachbarter Segmente. - const drawingRuns = useMemo(() => buildDrawingRuns(plan.primitives), [plan]); + // Nur der reine SVG-Pfad zeichnet die gebündelten Linienzüge; bei GPU (WebGL/ + // WASM) übernimmt die GPU-Ebene — dort wäre der Durchgang über alle Primitive + // (bei Kataster 3,7 Mio.) reine Verschwendung bei jeder Plan-Änderung. + const drawingRuns = useMemo( + () => (useGpuRenderer ? [] : buildDrawingRuns(plan.primitives)), + [plan, useGpuRenderer], + ); // Pro Polygon mit Linien-/Kreuz-Schraffur ein eigenes sammeln. // Vollfüllung ("solid") und "none" brauchen kein Muster. Gedimmte Polygone @@ -2535,6 +2517,8 @@ export const PlanView = forwardRef( // sonst weiterhin in der ORIGINAL-Bauteilfarbe erscheinen. const hatched = useMemo(() => { const list: HatchedPoly[] = []; + // Im WebGL-Modus zeichnet das SVG nur Texte (keine Polygone → keine Muster). + if (useGpuRenderer && !wantWasm) return list; plan.primitives.forEach((p, i) => { if (p.kind === "polygon" && polyUsesPattern(p.hatch)) { // Verlauf-Schraffuren tragen ihre eigentliche Farbe in `gradient.color1/ @@ -2556,7 +2540,7 @@ export const PlanView = forwardRef( } }); return list; - }, [plan]); + }, [plan, useGpuRenderer, wantWasm]); // `cullState` (s. o.) reduziert bei sehr vielen Primitiven die Menge VOR // dem eigentlichen JSX-Mapping — gleiche Eigenschaft wie zuvor (hängt NICHT diff --git a/src/plan/planView/ownerIndex.test.ts b/src/plan/planView/ownerIndex.test.ts new file mode 100644 index 00000000..f0a86a9b --- /dev/null +++ b/src/plan/planView/ownerIndex.test.ts @@ -0,0 +1,44 @@ +import { describe, it, expect } from "vitest"; +import type { Primitive } from "../generatePlan"; +import { buildOwnerIndex, ownedBy } from "./ownerIndex"; + +const line = (drawingId: string): Primitive => + ({ kind: "line", a: { x: 0, y: 0 }, b: { x: 1, y: 0 }, cls: "draw2d", weightMm: 0.1, drawingId }) as Primitive; +const poly = (extra: Record): Primitive => + ({ kind: "polygon", pts: [], fill: "#fff", stroke: "#000", strokeWidthMm: 0.1, ...extra }) as unknown as Primitive; + +describe("ownerIndex", () => { + const prims: Primitive[] = [ + poly({ wallId: "w1" }), + line("d1"), + poly({ roomId: "r1" }), + line("d2"), + poly({ wallId: "w2", openingId: "o1" }), + line("d1"), + ]; + const idx = buildOwnerIndex(prims); + + it("findet Primitive je Besitzer-ID in Plan-Reihenfolge", () => { + expect(ownedBy(idx, prims, "drawingId", ["d1"])).toEqual([prims[1], prims[5]]); + expect(ownedBy(idx, prims, "wallId", ["w2", "w1"])).toEqual([prims[0], prims[4]]); + expect(ownedBy(idx, prims, "roomId", ["r1"])).toEqual([prims[2]]); + }); + + it("ein Primitiv kann mehrere Besitzer-Felder tragen (ausser Zeichnungs-Primitive)", () => { + expect(ownedBy(idx, prims, "openingId", ["o1"])).toEqual([prims[4]]); + expect(ownedBy(idx, prims, "wallId", ["w2"])).toEqual([prims[4]]); + }); + + it("liefert nichts für unbekannte IDs/leere Mengen und gleicht dem früheren filter()", () => { + expect(ownedBy(idx, prims, "drawingId", ["x"])).toEqual([]); + expect(ownedBy(idx, prims, "wallId", [])).toEqual([]); + const ids = new Set(["d2", "w1"]); + const viaFilter = prims.filter( + (p) => ids.has((p as any).drawingId) || ids.has((p as any).wallId), + ); + const viaIndex = [...ownedBy(idx, prims, "drawingId", ids), ...ownedBy(idx, prims, "wallId", ids)].sort( + (a, b) => prims.indexOf(a) - prims.indexOf(b), + ); + expect(viaIndex).toEqual(viaFilter); + }); +}); diff --git a/src/plan/planView/ownerIndex.ts b/src/plan/planView/ownerIndex.ts new file mode 100644 index 00000000..367da3e1 --- /dev/null +++ b/src/plan/planView/ownerIndex.ts @@ -0,0 +1,66 @@ +// Besitzer-Index über die Primitive eines Plans: welche Primitive gehören zu +// Wand/Decke/Raum/Zeichnung/…-ID X? Die Auswahl-Hervorhebung in PlanView hat +// das bisher mit je einem `plan.primitives.filter(...)` PRO Elementart gelöst — +// zehn Durchgänge über alle Primitive bei jeder Plan-Änderung (bei einem +// Kataster-Import 3,7 Mio. → ~300 ms pro Änderung). Der Index braucht EINEN +// Durchgang je Plan; danach kostet jede Abfrage nur die Treffer. + +import type { Primitive } from "../generatePlan"; + +export const OWNER_FIELDS = [ + "wallId", + "drawingId", + "ceilingId", + "openingId", + "stairId", + "roomId", + "roofId", + "contextObjectId", + "sectionLineId", + "extrudedSolidId", + "columnId", +] as const; +export type OwnerField = (typeof OWNER_FIELDS)[number]; + +export type OwnerIndex = Record>; + +export function buildOwnerIndex(prims: readonly Primitive[]): OwnerIndex { + const index = Object.fromEntries(OWNER_FIELDS.map((f) => [f, new Map()])) as OwnerIndex; + const add = (field: OwnerField, id: string, i: number) => { + const m = index[field]; + const list = m.get(id); + if (list) list.push(i); + else m.set(id, [i]); + }; + for (let i = 0; i < prims.length; i++) { + const rec = prims[i] as unknown as Record; + // Schnellpfad: Primitive aus 2D-Zeichnungen (der Massenfall) tragen NUR eine + // drawingId, nie eine andere Besitzer-ID (s. generatePlan/drawing2d.ts). + const did = rec.drawingId; + if (typeof did === "string") { + add("drawingId", did, i); + continue; + } + for (const f of OWNER_FIELDS) { + const v = rec[f]; + if (typeof v === "string") add(f, v, i); + } + } + return index; +} + +/** Primitive, deren `field` in `ids` liegt — in Plan-Reihenfolge (wie `filter`). */ +export function ownedBy( + index: OwnerIndex, + prims: readonly Primitive[], + field: OwnerField, + ids: Iterable, +): Primitive[] { + const hits: number[] = []; + for (const id of ids) { + const list = index[field].get(id); + if (list) for (const i of list) hits.push(i); + } + hits.sort((a, b) => a - b); + return hits.map((i) => prims[i]); +}