[K4][K3d] S3 Fang-Kandidaten aus dem Modell im Kern (snap_model.rs), je Dokument-Stand gesammelt

Wände/Schichtgrenzen, Umrisse, 2D-Zeichnungen, Kreise, Raum-Anker, Ellipsen sammelt der Kern in zwei Teilen (Modell/Zeichnungen) je Revs-Stand; je Mausbewegung ein Aufruf snap_query, Nahmodus im Kern. TS behält Toleranz, Verweilen, Parallele, Raster/Winkel. Gelöscht: TS-Sammlung, snapSegments.ts, model/wall.ts, alte Bindungen. Gleichheit (einmalig): 20 100 Abfragen alt/neu gleich. Messung 3000 Wände + 20 000 Zeichnungen 9,7 ms → 23 µs je Mausbewegung.
This commit is contained in:
Claude committed 2026-10-29 15:04:17 +00:00
1 parent ddff1ede18
commit 0ff0f472f9
16 files changed
+1158 -714

No files matched your search

+3 -7
View File
@@ -1,8 +1,8 @@
// Raster-Index über die Bounding-Boxen von Linien-/Polylinien-Zeichnungen
// (Drawing2D). Abfragen prüfen nur die Zeichnungen in Cursornähe statt alle —
// bei einem Kataster-Import ~10^5 Zeichnungen / 3,7 Mio. Strecken. Genutzt für
// Hover/Pick/Marquee (plan/planView/underlayAccess.ts) und den Objektfang
// (tools/snapping.ts).
// Hover/Pick/Marquee (plan/planView/underlayAccess.ts); der Objektfang hat seinen
// eigenen Index im Kern (`snap_model.rs`). `pointSegDist` nutzt der Fang für die Blatt-Geometrie.
import type { Drawing2D, Vec2 } from "../model/types";
import { drawingBox } from "./drawingBounds";
@@ -47,10 +47,6 @@ const TARGET_PER_CELL = 4;
const MAX_CELLS = 2_000_000;
const MAX_CELLS_PER_DRAWING = 256;
/** Ob eine Zeichnung vom Index erfasst wird (Linie/Polylinie mit Punkten). */
export const isIndexable = (d: Drawing2D): boolean =>
d.geom.shape === "line" || d.geom.shape === "polyline";
export function buildDrawingIndex(drawings: readonly Drawing2D[]): DrawingIndex | null {
if (drawings.length === 0) return null;
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
@@ -104,7 +100,7 @@ export function buildDrawingIndex(drawings: readonly Drawing2D[]): DrawingIndex
* Indizes der Zeichnungen, deren Bounding-Box (um `r` erweitert) den Punkt `m`
* enthält — Kandidaten, jede höchstens einmal.
*/
export function drawingsNear(index: DrawingIndex, m: Vec2, r: number): number[] {
function drawingsNear(index: DrawingIndex, m: Vec2, r: number): number[] {
const { minX, minY, cell, cols, rows, cellStart, items, long, drawings } = index;
const out: number[] = [];
const seen = new Set<number>();
@@ -1,10 +1,11 @@
// Objektfang auf Strecken im Kern ([K3d], `core/src/snap.rs`): gleiche Wahl wie die einfache
// Suche über alle Strecken/Paare (Raster-Vorauswahl und Geraden-Filter für Schnittpunkte dürfen
// nichts ändern). (Gleichheit zur früheren TS-Fassung von computeSnap: 2600 Abfragen, s. TODO [K3d].)
// Objektfang aus dem Modell im Kern ([K4] S3, `core/src/snap_model.rs` + `snap.rs`): gleiche Wahl wie
// die einfache Suche über alle Strecken/Paare (Raster-Vorauswahl und Geraden-Filter für Schnittpunkte
// dürfen nichts ändern), gleiche Nummer solange das Dokument gleich bleibt.
import { describe, expect, it } from "vitest";
import { holdSnapSegments, releaseSnapSegments, snapOnSegments, type SnapSeg } from "./snapSegments";
import type { Vec2 } from "../model/types";
import { holdModelSnap, queryModelSnap, type SnapSeg } from "./modelSnap";
import { sampleProject } from "../model/sampleProject";
import type { Drawing2D, Project, Vec2 } from "../model/types";
const PRIORITY: Record<string, number> = { perpendicular: 1, endpoint: 7, midpoint: 3, intersection: 5, onEdge: 0 };
@@ -55,10 +56,21 @@ function brute(segs: SnapSeg[], raw: Vec2, tolM: number, pxPerM: number, last: V
return { a: a.get(), b: b.get() };
}
describe("snapOnSegments (Kern)", () => {
const LEVEL = sampleProject.drawingLevels[0].id;
const CODE = "01";
function lineProject(segs: SnapSeg[]): Project {
const drawings: Drawing2D[] = segs.map(([a, b], i) => ({ id: `l${i}`, type: "drawing2d", levelId: LEVEL, categoryCode: CODE, geom: { shape: "line", a, b } }));
return { ...sampleProject, walls: [], rooms: [], ceilings: [], roofs: [], stairs: [], columns: [], drawings2d: drawings };
}
const flags = { segs: true, perpendicular: true, endpoint: true, midpoint: true, intersection: true, onEdge: true, center: false, tangent: false };
describe("Objektfang aus dem Modell (Kern)", () => {
it("gleich wie die Suche über alle Strecken und Paare (Zufallsdaten)", () => {
let seed = 3;
const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
const codes = new Set([CODE]);
for (let run = 0; run < 8; run++) {
const segs: SnapSeg[] = [];
for (let i = 0; i < 300; i++) {
@@ -67,36 +79,31 @@ describe("snapOnSegments (Kern)", () => {
const k = rnd();
segs.push(k < 0.4 ? [{ x, y }, { x: x + rnd() * 5, y }] : k < 0.8 ? [{ x, y }, { x, y: y + rnd() * 5 }] : [{ x, y }, { x: x + rnd() * 30 - 15, y: y + rnd() * 30 - 15 }]);
}
const base = segs.slice(0, 250);
const extra = segs.slice(250);
const h = holdSnapSegments(base);
const p = lineProject(segs.slice(0, 250));
const draft = segs.slice(250);
const h = holdModelSnap(p, LEVEL, codes);
expect(holdModelSnap(p, LEVEL, codes)).toBe(h);
for (let q = 0; q < 25; q++) {
const raw = { x: rnd() * 20, y: rnd() * 20 };
const tolM = 0.05 + rnd() * 0.5;
const last = rnd() < 0.5 ? { x: rnd() * 20, y: rnd() * 20 } : null;
const r = snapOnSegments(h, extra, null, { raw, tolM, pxPerMeter: 100, perpendicular: true, endpoint: true, midpoint: true, intersection: true, onEdge: true, lastPoint: last })!;
const r = queryModelSnap(h, draft, [], { raw, tolM, pxPerMeter: 100, lastPoint: last, ...flags })!;
const e = brute(segs, raw, tolM, 100, last);
expect(r.a).toEqual(e.a);
expect(r.b).toEqual(e.b);
}
releaseSnapSegments(h);
expect(snapOnSegments(h, [], null, { raw: { x: 0, y: 0 }, tolM: 1, pxPerMeter: 1, perpendicular: false, endpoint: true, midpoint: false, intersection: false, onEdge: false, lastPoint: null })).toBeNull();
}
}, 60_000);
expect(queryModelSnap(987654, [], [], { raw: { x: 0, y: 0 }, tolM: 1, pxPerMeter: 1, lastPoint: null, ...flags })).toBeNull();
}, 60_000);
it("nächste Strecke für das Verweilen", () => {
const r = snapOnSegments(0, [[{ x: 0, y: 0 }, { x: 10, y: 0 }], [{ x: 5, y: -5 }, { x: 5, y: 5 }]], null, {
raw: { x: 5.05, y: 0.02 },
tolM: 0.2,
pxPerMeter: 100,
perpendicular: false,
endpoint: true,
midpoint: true,
intersection: true,
onEdge: true,
lastPoint: null,
})!;
const p = lineProject([[{ x: 0, y: 0 }, { x: 10, y: 0 }]]);
const h = holdModelSnap(p, LEVEL, new Set([CODE]));
const r = queryModelSnap(h, [[{ x: 5, y: -5 }, { x: 5, y: 5 }]], [], { raw: { x: 5.05, y: 0.02 }, tolM: 0.2, pxPerMeter: 100, lastPoint: null, ...flags })!;
expect(r.nearest).toEqual([{ x: 0, y: 0 }, { x: 10, y: 0 }]);
expect(r.b?.kind).toBe("intersection");
// Ohne sichtbare Ebene: keine Strecken.
const h2 = holdModelSnap(p, LEVEL, new Set(["xx"]));
expect(queryModelSnap(h2, [], [], { raw: { x: 5.05, y: 0.02 }, tolM: 0.2, pxPerMeter: 100, lastPoint: null, ...flags })!.nearest).toBeNull();
});
});
+115
View File
@@ -0,0 +1,115 @@
// Objektfang aus dem Modell über den Kern ([K4] S3, `src-tauri/core/src/snap_model.rs` + `snap.rs`):
// Der Kern sammelt die Fang-Kandidaten eines Geschosses (Wandachsen, Schichtgrenzen, Umrisse von
// Raum/Decke/Dach/Treppe/Stütze, 2D-Zeichnungen, Kreise/Bögen, Raum-Anker, Ellipsen-Mittelpunkte)
// einmal je Dokument-Stand ({@link holdModelSnap}); je Mausbewegung gehen nur Cursor, Toleranz und
// die Zusatzstrecken (Entwurf, Blatt-Geometrie) über die Grenze ({@link queryModelSnap}). Die
// Bildschirm-Toleranz, Verweilen/Teilpunkte, Parallele, Raster und Winkel bleiben in
// `tools/snapping.ts`.
import type { Project, Vec2 } from "../model/types";
import { core } from "../engine/coreWasm";
import { documentFor } from "../core/document";
export type SnapSeg = [Vec2, Vec2];
/** Fang-Arten des Kerns (Code → Name wie `SnapKind`). */
const KINDS = ["perpendicular", "endpoint", "midpoint", "intersection", "onEdge", "center", "quadrant", "tangent"] as const;
export type ModelSnapKind = (typeof KINDS)[number];
export interface ModelSnapCand {
point: Vec2;
kind: ModelSnapKind;
distPx: number;
/** `distPx − Priorität` (kleiner gewinnt). */
score: number;
}
/** Ergebnis in der Reihenfolge, in der `computeSnap` die Kandidaten zusammenführt. */
export interface ModelSnapResult {
/** Nächste Strecke innerhalb der Toleranz (Lotfusspunkt-Abstand) oder null (Verweilen). */
nearest: SnapSeg | null;
/** Lot vom letzten Punkt, Strecken-Endpunkte. */
a: ModelSnapCand | null;
/** Raum-Stempel-Anker (Endpunkt). */
anchors: ModelSnapCand | null;
/** Mittelpunkte, Schnittpunkte, Punkt auf der Kante. */
b: ModelSnapCand | null;
/** Kreise/Bögen (Mittelpunkt, Quadranten, Bogen-Endpunkte, Tangente), Ellipsen-Mittelpunkte. */
c: ModelSnapCand | null;
}
export interface ModelSnapQuery {
raw: Vec2;
tolM: number;
pxPerMeter: number;
/** Strecken-Fang überhaupt (auch nur für Verweilen/Parallele). */
segs: boolean;
perpendicular: boolean;
endpoint: boolean;
midpoint: boolean;
intersection: boolean;
onEdge: boolean;
center: boolean;
tangent: boolean;
lastPoint: Vec2 | null;
}
function flat(segs: readonly SnapSeg[]): Float64Array {
const out = new Float64Array(segs.length * 4);
for (let i = 0; i < segs.length; i++) {
const [a, b] = segs[i];
out[4 * i] = a.x;
out[4 * i + 1] = a.y;
out[4 * i + 2] = b.x;
out[4 * i + 3] = b.y;
}
return out;
}
/**
* Kandidaten des Geschosses `levelId` (sichtbare Ebenen `codes`) im Kern sammeln bzw. wiederfinden:
* dieselbe Nummer, solange sich die betroffenen Felder des Dokuments nicht ändern. 0 = kein Dokument.
*/
export function holdModelSnap(project: Project, levelId: string, codes: ReadonlySet<string>): number {
const doc = documentFor(project);
return doc.id ? core().doc_snap_hold(doc.id, levelId, [...codes].join("\n")) : 0;
}
const cand = (f: Float64Array, k: number): ModelSnapCand | null =>
Number.isNaN(f[k + 2]) ? null : { point: { x: f[k], y: f[k + 1] }, kind: KINDS[f[k + 2]], distPx: f[k + 3], score: f[k + 4] };
/**
* Fang je Mausbewegung über die abgelegten Kandidaten `held` plus Entwurfs- (`draft`) und
* Blatt-Strecken (`sheet`, nahe am Cursor, nächste zuerst). null, wenn `held` nicht (mehr) im Kern liegt.
*/
export function queryModelSnap(held: number, draft: readonly SnapSeg[], sheet: readonly SnapSeg[], q: ModelSnapQuery): ModelSnapResult | null {
const flags =
(q.perpendicular ? 1 : 0) |
(q.endpoint ? 2 : 0) |
(q.midpoint ? 4 : 0) |
(q.intersection ? 8 : 0) |
(q.onEdge ? 16 : 0) |
(q.center ? 32 : 0) |
(q.tangent ? 64 : 0) |
(q.segs ? 128 : 0);
const f = core().snap_query(
held,
flat(draft),
flat(sheet),
q.raw.x,
q.raw.y,
q.tolM,
q.pxPerMeter,
flags,
q.lastPoint ? q.lastPoint.x : NaN,
q.lastPoint ? q.lastPoint.y : NaN,
);
if (f.length < 25) return null;
return {
nearest: f[0] >= 0 ? [{ x: f[1], y: f[2] }, { x: f[3], y: f[4] }] : null,
a: cand(f, 5),
anchors: cand(f, 10),
b: cand(f, 15),
c: cand(f, 20),
};
}
-95
View File
@@ -1,95 +0,0 @@
// Objektfang auf Strecken über den Kern ([K3d], `src-tauri/core/src/snap.rs`): Lotfusspunkt,
// Endpunkte, Mittelpunkte, Schnittpunkte, Punkt auf der Kante und die nächste Strecke (Verweilen).
// Die Strecken eines Projekt-Geschosses liegen einmal im Kern ({@link holdSnapSegments}); je
// Mausbewegung gehen nur Cursor und Zusatzstrecken über die Grenze. Die Sammlung der Strecken
// und die übrigen Fang-Arten (Raster, Winkel, Kreise, Teilpunkte) bleiben in `tools/snapping.ts`.
import type { Vec2 } from "../model/types";
import { core } from "../engine/coreWasm";
export type SnapSeg = [Vec2, Vec2];
/** Fang-Arten des Kerns (Code → Name wie `SnapKind`). */
const KINDS = ["perpendicular", "endpoint", "midpoint", "intersection", "onEdge"] as const;
export type SegSnapKind = (typeof KINDS)[number];
export interface SegSnapCand {
point: Vec2;
kind: SegSnapKind;
distPx: number;
/** `distPx − Priorität` (kleiner gewinnt). */
score: number;
}
export interface SegSnapResult {
/** Nächste Strecke innerhalb der Toleranz (Lotfusspunkt-Abstand) oder null. */
nearest: SnapSeg | null;
/** Block A: Lot vom letzten Punkt, Endpunkte der Strecken. */
a: SegSnapCand | null;
/** Block B: Mittelpunkte, Schnittpunkte, Punkt auf der Kante. */
b: SegSnapCand | null;
}
export interface SegSnapQuery {
raw: Vec2;
tolM: number;
pxPerMeter: number;
perpendicular: boolean;
endpoint: boolean;
midpoint: boolean;
intersection: boolean;
onEdge: boolean;
lastPoint: Vec2 | null;
}
function flat(segs: readonly SnapSeg[]): Float64Array {
const out = new Float64Array(segs.length * 4);
for (let i = 0; i < segs.length; i++) {
const [a, b] = segs[i];
out[4 * i] = a.x;
out[4 * i + 1] = a.y;
out[4 * i + 2] = b.x;
out[4 * i + 3] = b.y;
}
return out;
}
/** Strecken im Kern ablegen; Nummer für {@link snapOnSegments} (freigeben: {@link releaseSnapSegments}). */
export function holdSnapSegments(segs: readonly SnapSeg[]): number {
return core().snap_segs_hold(flat(segs));
}
export function releaseSnapSegments(id: number): void {
if (id) core().snap_segs_release(id);
}
const cand = (f: Float64Array, k: number): SegSnapCand | null =>
Number.isNaN(f[k + 2]) ? null : { point: { x: f[k], y: f[k + 1] }, kind: KINDS[f[k + 2]], distPx: f[k + 3], score: f[k + 4] };
/**
* Fang über die abgelegten Strecken `held` (0 = keine) und `extra` (in dieser Reihenfolge);
* Schnittpunkte aus `intSegs` (null = aus denselben Strecken). null, wenn `held` nicht (mehr) im
* Kern liegt.
*/
export function snapOnSegments(held: number, extra: readonly SnapSeg[], intSegs: readonly SnapSeg[] | null, q: SegSnapQuery): SegSnapResult | null {
const flags = (q.perpendicular ? 1 : 0) | (q.endpoint ? 2 : 0) | (q.midpoint ? 4 : 0) | (q.intersection ? 8 : 0) | (q.onEdge ? 16 : 0);
const f = core().snap_eval(
held,
flat(extra),
intSegs ? 1 : 0,
intSegs ? flat(intSegs) : new Float64Array(0),
q.raw.x,
q.raw.y,
q.tolM,
q.pxPerMeter,
flags,
q.lastPoint ? q.lastPoint.x : NaN,
q.lastPoint ? q.lastPoint.y : NaN,
);
if (f.length < 15) return null;
return {
nearest: f[0] >= 0 ? [{ x: f[1], y: f[2] }, { x: f[3], y: f[4] }] : null,
a: cand(f, 5),
b: cand(f, 10),
};
}