Files
DOSSIER/src-tauri/core/src/snap.rs
T
Claude 0ff0f472f9 [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.
2026-10-29 15:04:17 +00:00

276 lines
9.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Objektfang auf Strecken ([K3d], früher die Schleifen in `src/tools/snapping.ts`): Lotfusspunkt
//! vom letzten Punkt, Endpunkte, Mittelpunkte, Schnittpunkte (unendliche Geraden) und Punkt auf
//! der Kante, dazu die nächste Strecke für die Verweil-Erkennung ("divide"). Die Strecken eines
//! Geschosses sammelt `snap_model.rs` einmal je Dokument-Stand aus dem Modell; je Mausbewegung
//! kommen nur Cursor, Toleranz und die wenigen Zusatzstrecken (Entwurf, Blatt-Geometrie) dazu.
//!
//! Bewertung wie in TS: Kandidat nur bis `tol_m` Abstand, Punktzahl `distPx − Priorität`, der
//! erste mit der kleinsten Punktzahl gewinnt. Ergebnis in zwei Blöcken (die Reihenfolge in TS ist
//! Lot → Endpunkte (Strecken) → [Entwurfspunkte, Raum-Anker in TS] → Mitte → Schnitt → Kante),
//! TS führt sie mit seinen eigenen Kandidaten in derselben Reihenfolge zusammen.
//!
//! Schnittpunkte: nur Geraden, die näher als `tol_m` am Cursor vorbeigehen, können einen
//! Schnittpunkt in Reichweite haben — die übrigen Paare fallen vorab weg (früher alle Paare:
//! beim grossen Projekt ~25k Strecken → Sekunden je Mausbewegung). Die Reihenfolge der
//! verbleibenden Paare bleibt, das Ergebnis ist dasselbe.
use crate::jsmath::hypot;
use crate::jsnum;
use crate::plan_pick::{pad, BoxIndex};
/// Snap-Arten (Code in der Ausgabe).
pub const K_PERPENDICULAR: f64 = 0.0;
pub const K_ENDPOINT: f64 = 1.0;
pub const K_MIDPOINT: f64 = 2.0;
pub const K_INTERSECTION: f64 = 3.0;
pub const K_ON_EDGE: f64 = 4.0;
pub const K_CENTER: f64 = 5.0;
pub const K_QUADRANT: f64 = 6.0;
pub const K_TANGENT: f64 = 7.0;
/// Prioritäts-Bonus in Pixeln (wie `PRIORITY` in snapping.ts).
fn priority(kind: f64) -> f64 {
match kind as i32 {
0 => 1.0,
1 => 7.0,
2 => 3.0,
3 => 5.0,
5 | 6 => 3.0,
7 => 1.0,
_ => 0.0,
}
}
/// Welche Arten gefragt sind (Bits).
pub const F_PERPENDICULAR: u32 = 1;
pub const F_ENDPOINT: u32 = 2;
pub const F_MIDPOINT: u32 = 4;
pub const F_INTERSECTION: u32 = 8;
pub const F_ON_EDGE: u32 = 16;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Cand {
pub x: f64,
pub y: f64,
pub kind: f64,
pub dist_px: f64,
pub score: f64,
}
/// Bester Kandidat einer Folge (`consider` in snapping.ts): der erste mit der kleinsten Punktzahl.
pub struct Best {
raw: (f64, f64),
tol_m: f64,
px_per_m: f64,
pub best: Option<Cand>,
best_score: f64,
}
impl Best {
pub fn new(raw: (f64, f64), tol_m: f64, px_per_m: f64) -> Best {
Best { raw, tol_m, px_per_m, best: None, best_score: f64::INFINITY }
}
#[inline]
pub fn consider(&mut self, x: f64, y: f64, kind: f64) {
let dx = x - self.raw.0;
let dy = y - self.raw.1;
// Schnell verwerfen (hypot ≥ max(|dx|, |dy|)); NaN fällt durch wie in TS.
if dx.abs() > self.tol_m || dy.abs() > self.tol_m {
return;
}
let d = hypot(&[dx, dy]);
if d > self.tol_m {
return;
}
let dist_px = d * self.px_per_m;
let score = dist_px - priority(kind);
if score < self.best_score {
self.best_score = score;
self.best = Some(Cand { x, y, kind, dist_px, score });
}
}
}
/// Lotfusspunkt von `p` auf die Strecke `a–b`, aufs Segment geklemmt (`perpFoot`).
#[inline]
fn perp_foot(px: f64, py: f64, s: &[f64; 4]) -> (f64, f64) {
let dx = s[2] - s[0];
let dy = s[3] - s[1];
let len2 = dx * dx + dy * dy;
if len2 < 1e-12 {
return (s[0], s[1]);
}
let mut t = ((px - s[0]) * dx + (py - s[1]) * dy) / len2;
t = jsnum::max(0.0, jsnum::min(1.0, t));
(s[0] + dx * t, s[1] + dy * t)
}
/// Schnittpunkt der Geraden durch `s` und `u` (`lineIntersect`), `None` bei (fast) parallel.
#[inline]
fn line_intersect(s: &[f64; 4], u: &[f64; 4]) -> Option<(f64, f64)> {
let (dax, day) = (s[2] - s[0], s[3] - s[1]);
let (dbx, dby) = (u[2] - u[0], u[3] - u[1]);
let denom = dax * dby - day * dbx;
if denom.abs() < 1e-9 {
return None;
}
let t = ((u[0] - s[0]) * dby - (u[1] - s[1]) * dbx) / denom;
Some((s[0] + dax * t, s[1] + day * t))
}
/// Kann die Gerade durch `s` einen Schnittpunkt in `tol_m` um `raw` tragen? (grosszügig;
/// entartete/unendliche Strecken: ja)
fn line_near(s: &[f64; 4], raw: (f64, f64), tol_m: f64) -> bool {
let dx = s[2] - s[0];
let dy = s[3] - s[1];
let l = (dx * dx + dy * dy).sqrt();
if !(l > 0.0) || !l.is_finite() {
return true;
}
let d = ((raw.0 - s[0]) * dy - (raw.1 - s[1]) * dx).abs() / l;
!(d > tol_m * (1.0 + 1e-6) + 1e-9 * (1.0 + raw.0.abs().max(raw.1.abs())))
}
/// Abgelegte Strecken mit Raster über ihren Bounding-Boxen.
#[derive(Default)]
pub struct SnapSet {
pub segs: Vec<[f64; 4]>,
ix: BoxIndex,
}
impl SnapSet {
pub fn new(segs: Vec<[f64; 4]>) -> SnapSet {
let ix = BoxIndex::build(
segs.iter()
.enumerate()
.map(|(k, s)| {
let e = pad(s);
(k as u32, s[0].min(s[2]) - e, s[1].min(s[3]) - e, s[0].max(s[2]) + e, s[1].max(s[3]) + e)
})
.collect(),
);
SnapSet { segs, ix }
}
/// Strecken, die näher als `r` an `(x, y)` liegen können (aufsteigend). Alle Strecken-Fänge
/// (Endpunkt, Mitte, Lot, Kante, Verweilen) liegen auf der Strecke und höchstens `tol_m` vom
/// Cursor — die übrigen tragen nichts bei.
pub fn near(&self, x: f64, y: f64, r: f64) -> Vec<u32> {
if x.is_finite() && y.is_finite() && r.is_finite() && r >= 0.0 {
self.ix.candidates(x, y, r * (1.0 + 1e-6) + pad(&[x, y]))
} else {
(0..self.segs.len() as u32).collect()
}
}
}
/// Eingabe einer Fang-Abfrage.
pub struct SnapQuery<'a> {
/// Strecken in Fang-Reihenfolge: abgelegte Teile (der Reihe nach), dann Zusatz.
pub bases: &'a [&'a SnapSet],
pub extra: &'a [[f64; 4]],
/// Strecken für Schnittpunkte; `None` = dieselben wie `bases` + `extra`.
pub int_segs: Option<&'a [[f64; 4]]>,
pub raw: (f64, f64),
pub tol_m: f64,
pub px_per_m: f64,
pub flags: u32,
pub last: Option<(f64, f64)>,
}
/// Ergebnis: nächste Strecke (Verweilen), Block A (Lot, Endpunkte), Block B (Mitte, Schnitt, Kante).
pub struct SnapOut {
pub dwell: Option<[f64; 4]>,
pub a: Option<Cand>,
pub b: Option<Cand>,
}
pub fn snap_segments(q: &SnapQuery) -> SnapOut {
let (rx, ry) = q.raw;
let near_base: Vec<&[f64; 4]> =
q.bases.iter().flat_map(|b| b.near(rx, ry, q.tol_m).into_iter().map(move |k| &b.segs[k as usize])).collect();
// Strecken in Fang-Reihenfolge: nahe abgelegte, dann Zusatz.
let segs = || near_base.iter().copied().chain(q.extra.iter()).map(|s| ((), s));
// Nächste Strecke für die Verweil-Erkennung (`nearestSegment` in snapping.ts).
let mut dwell = None;
let mut best_d = q.tol_m;
for (_, s) in segs() {
let (fx, fy) = perp_foot(rx, ry, s);
let (dx, dy) = (fx - rx, fy - ry);
if dx.abs() >= best_d || dy.abs() >= best_d {
continue;
}
let d = hypot(&[dx, dy]);
if d < best_d {
best_d = d;
dwell = Some(*s);
}
}
let mut a = Best::new(q.raw, q.tol_m, q.px_per_m);
if q.flags & F_PERPENDICULAR != 0 {
if let Some((lx, ly)) = q.last {
for (_, s) in segs() {
let (x, y) = perp_foot(lx, ly, s);
a.consider(x, y, K_PERPENDICULAR);
}
}
}
if q.flags & F_ENDPOINT != 0 {
for (_, s) in segs() {
a.consider(s[0], s[1], K_ENDPOINT);
a.consider(s[2], s[3], K_ENDPOINT);
}
}
let mut b = Best::new(q.raw, q.tol_m, q.px_per_m);
if q.flags & F_MIDPOINT != 0 {
for (_, s) in segs() {
b.consider((s[0] + s[2]) / 2.0, (s[1] + s[3]) / 2.0, K_MIDPOINT);
}
}
if q.flags & F_INTERSECTION != 0 {
let near: Vec<&[f64; 4]> = match q.int_segs {
Some(l) => l.iter().filter(|s| line_near(s, q.raw, q.tol_m)).collect(),
None => q.bases.iter().flat_map(|b| b.segs.iter()).chain(q.extra.iter()).filter(|s| line_near(s, q.raw, q.tol_m)).collect(),
};
for i in 0..near.len() {
for j in i + 1..near.len() {
if let Some((x, y)) = line_intersect(near[i], near[j]) {
b.consider(x, y, K_INTERSECTION);
}
}
}
}
if q.flags & F_ON_EDGE != 0 {
for (_, s) in segs() {
let (x, y) = perp_foot(rx, ry, s);
b.consider(x, y, K_ON_EDGE);
}
}
SnapOut { dwell, a: a.best, b: b.best }
}
/// Strecken aus `[ax, ay, bx, by, …]`.
pub fn segs_from(flat: &[f64]) -> Vec<[f64; 4]> {
flat.chunks_exact(4).map(|c| [c[0], c[1], c[2], c[3]]).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn endpunkt_vor_kante_schnitt_nur_nahe_geraden() {
let segs = segs_from(&[0.0, 0.0, 10.0, 0.0, 5.0, -5.0, 5.0, 5.0, 100.0, 50.0, 200.0, 50.0]);
let base = SnapSet::new(segs[..1].to_vec());
let q = SnapQuery { bases: &[&base], extra: &segs[1..], int_segs: None, raw: (5.05, 0.02), tol_m: 0.2, px_per_m: 100.0, flags: 31, last: None };
let o = snap_segments(&q);
assert_eq!(o.dwell, Some(segs[0]));
assert_eq!(o.a, None);
let b = o.b.unwrap();
assert_eq!((b.x, b.y, b.kind), (5.0, 0.0, K_INTERSECTION));
let q = SnapQuery { raw: (9.95, 0.0), ..q };
assert_eq!(snap_segments(&q).a.unwrap().kind, K_ENDPOINT);
}
}