[K3d] Objektfang auf Strecken im Kern (Lot, Endpunkt, Mitte, Schnitt, Kante, Verweilen)

core/src/snap.rs (+ web/snap.rs, Fassade src/geometry/snapSegments.ts): die Strecken eines
Projekt-Geschosses liegen einmal im Kern (Raster über den Boxen); je Mausbewegung nur Cursor,
Toleranz und Entwurfs-/Blattstrecken. Schnittpunkte nur noch zwischen Geraden, die näher als
die Fangtoleranz am Cursor vorbeigehen (andere Paare können nichts beitragen; Reihenfolge
bleibt) — vorher alle Paare. computeSnap führt die Kern-Blöcke in der alten Reihenfolge mit
den eigenen Kandidaten zusammen; Raum-Anker, Kreise, Ellipsen je Projekt/Geschoss gecacht.
Gleich zur alten Fassung (2600 Abfragen: Beispielprojekt, mit 2D-Zeichnung, Nahmodus > 2000
Zeichnungen, bigSample(3), Verweilen an/aus, alle Fang-Arten zufällig). Grosses Projekt
(20×20 Kacheln): 27,6 s → 0,8 ms je Mausbewegung; bigSample(5): 135 ms → 57 µs.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-28 23:28:22 +00:00
1 parent 1a90f954da
commit 1fe83d6349
9 files changed
+692 -89

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
// 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].)
import { describe, expect, it } from "vitest";
import { holdSnapSegments, releaseSnapSegments, snapOnSegments, type SnapSeg } from "./snapSegments";
import type { Vec2 } from "../model/types";
const PRIORITY: Record<string, number> = { perpendicular: 1, endpoint: 7, midpoint: 3, intersection: 5, onEdge: 0 };
/** Einfache Fassung (alle Strecken, alle Paare) — Block A (Lot, Endpunkte) und Block B (Mitte, Schnitt, Kante). */
function brute(segs: SnapSeg[], raw: Vec2, tolM: number, pxPerM: number, last: Vec2 | null) {
const perp = (p: Vec2, a: Vec2, b: Vec2): Vec2 => {
const dx = b.x - a.x, dy = b.y - a.y;
const len2 = dx * dx + dy * dy;
if (len2 < 1e-12) return a;
const t = Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2));
return { x: a.x + dx * t, y: a.y + dy * t };
};
const block = () => {
let best: { point: Vec2; kind: string; distPx: number; score: number } | null = null;
let bestScore = Infinity;
const consider = (point: Vec2, kind: string) => {
const d = Math.hypot(point.x - raw.x, point.y - raw.y);
if (d > tolM) return;
const score = d * pxPerM - PRIORITY[kind];
if (score < bestScore) {
bestScore = score;
best = { point, kind, distPx: d * pxPerM, score };
}
};
return { consider, get: () => best };
};
const a = block();
if (last) for (const [p, q] of segs) a.consider(perp(last, p, q), "perpendicular");
for (const [p, q] of segs) {
a.consider(p, "endpoint");
a.consider(q, "endpoint");
}
const b = block();
for (const [p, q] of segs) b.consider({ x: (p.x + q.x) / 2, y: (p.y + q.y) / 2 }, "midpoint");
for (let i = 0; i < segs.length; i++) {
for (let j = i + 1; j < segs.length; j++) {
const [p, q] = segs[i];
const [r, s] = segs[j];
const da = { x: q.x - p.x, y: q.y - p.y };
const db = { x: s.x - r.x, y: s.y - r.y };
const den = da.x * db.y - da.y * db.x;
if (Math.abs(den) < 1e-9) continue;
const t = ((r.x - p.x) * db.y - (r.y - p.y) * db.x) / den;
b.consider({ x: p.x + da.x * t, y: p.y + da.y * t }, "intersection");
}
}
for (const [p, q] of segs) b.consider(perp(raw, p, q), "onEdge");
return { a: a.get(), b: b.get() };
}
describe("snapOnSegments (Kern)", () => {
it("gleich wie die Suche über alle Strecken und Paare (Zufallsdaten)", () => {
let seed = 3;
const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
for (let run = 0; run < 8; run++) {
const segs: SnapSeg[] = [];
for (let i = 0; i < 300; i++) {
const x = rnd() * 20, y = rnd() * 20;
// Viele achsparallele Strecken (Wände), einige lange schräge (Verlängerungen schneiden weit weg).
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);
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 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);
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,
})!;
expect(r.nearest).toEqual([{ x: 0, y: 0 }, { x: 10, y: 0 }]);
expect(r.b?.kind).toBe("intersection");
});
});
+95
View File
@@ -0,0 +1,95 @@
// 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),
};
}