[125] Schnitt-Dominanz: eine Fassung in dossier_hatch::section_dominance
2D-Schnitt (core/plan/section.rs) und 3D-Live-Schnitt (render3d/section_boolean.rs) rufen dieselbe f64-Rechnung (Subtraktion + Verschmelzen, Schneider-Vorauswahl über ugrid, das mit nach hatchgen wandert). Flachpuffer plan_section::dominant_bands und die f32-Kopie in render3d entfallen. Abweichungen behoben: f32-Rundung im 3D, NaN-Rechteck (2D löschte damit jedes schwächere Band) nimmt nicht mehr teil.
This commit is contained in:
8 files changed
+563
-679
No files matched your search
@@ -1069,7 +1069,13 @@ Prüfung des ganzen Programms (Rust-Kern, TS-Hülle, Build/Sicherheit), nur Code
|
||||
alles über `CommandEngine`. `commitTransform` (nur Tests) löschen. · M
|
||||
- ○ **[124] Kern typisiert.** 152 Funktionen lesen rohes JSON (`grips::field/num_of`); Ops gegen Modell prüfen,
|
||||
`cmd_*` auf `&Project`. · L
|
||||
- ○ **[125] Doppelte Schnitt-Dominanz** (`render3d/section_boolean.rs` ↔ `core/plan_section.rs`) auf eine Fassung. · S
|
||||
- ✅ **[125] Doppelte Schnitt-Dominanz** (`render3d/section_boolean.rs` ↔ `core/plan_section.rs`) auf eine Fassung. · S
|
||||
3.11.: eine Fassung `dossier_hatch::section_dominance` (f64, JS-`min/max`, Schneider-Vorauswahl `ugrid` — mit
|
||||
`ugrid` aus dem Kern nach `hatchgen` verschoben); `core/plan/section.rs` und `render3d/section_boolean.rs` sind
|
||||
dünne Adapter, der Flachpuffer `plan_section::dominant_bands` entfällt. Abweichungen der alten Fassungen: 3D
|
||||
rechnete in f32 (Überlapp knapp über EPS bei ~100 m ging verloren); NaN-Ecke liess im 2D jedes schwächere Band
|
||||
verschwinden, im 3D zählte die endliche Hülle — jetzt nimmt ein nicht endliches Rechteck nicht teil. Sonst
|
||||
gleich (Verschmelzen geprüft). Golden-Tests unverändert; nicht in der Tauri-App angesehen.
|
||||
- ○ **[126] `jsnum`/`jsmath` ersetzen** (libm, deterministisch über Plattformen); `plan_*.rs`-Paare nach `plan/`
|
||||
zusammenführen. · M
|
||||
- ○ **[127] 3D-Booleans klären:** BSP in `fileio/csg.rs` stilllegen, Manifold vs. truck dokumentieren. · S
|
||||
|
||||
@@ -100,7 +100,7 @@ pub mod parametric_walls;
|
||||
pub mod geo_translate;
|
||||
pub mod selection_filter;
|
||||
pub mod reorder;
|
||||
pub mod ugrid;
|
||||
pub use dossier_hatch::ugrid;
|
||||
pub mod wasm_alloc;
|
||||
pub mod text_layout;
|
||||
pub mod plan_text;
|
||||
|
||||
@@ -833,32 +833,27 @@ fn apply_wall_termination(bands: Vec<Cut>, walls: &[Option<&Wall>]) -> Vec<Cut>
|
||||
out
|
||||
}
|
||||
|
||||
/// `subtractDominantBands` (Rechnung: `plan_section::dominant_bands`).
|
||||
/// `subtractDominantBands` (Rechnung: `dossier_hatch::section_dominance`, gemeinsam mit dem 3D-Live-Schnitt).
|
||||
fn subtract_dominant_bands(bands: Vec<Cut>) -> Vec<Cut> {
|
||||
let mut ids: HashMap<&str, usize> = HashMap::new();
|
||||
let mut b: Vec<f64> = Vec::with_capacity(1 + 7 * bands.len());
|
||||
b.push(bands.len() as f64);
|
||||
for band in &bands {
|
||||
let r = rect_of_band(band);
|
||||
let mut comp = -1.0;
|
||||
if let Some(id) = band.component_id.as_deref() {
|
||||
let n = ids.len();
|
||||
let k = *ids.entry(id).or_insert(n);
|
||||
comp = k as f64;
|
||||
}
|
||||
let r4 = r.unwrap_or([0.0; 4]);
|
||||
b.extend([if r.is_some() { 1.0 } else { 0.0 }, r4[0], r4[1], r4[2], r4[3], band.join_priority.unwrap_or(f64::NAN), comp]);
|
||||
}
|
||||
let f = crate::plan_section::dominant_bands(&b);
|
||||
let mut out = Vec::new();
|
||||
let Some(&n) = f.first() else { return out };
|
||||
let mut i = 1;
|
||||
for _ in 0..n as usize {
|
||||
let src = &bands[f[i] as usize];
|
||||
if f[i + 1] == 0.0 {
|
||||
out.push(src.clone());
|
||||
} else {
|
||||
let mut c = Cut::new(src.kind, src.index, vec![[f[i + 2], f[i + 5]], [f[i + 3], f[i + 5]], [f[i + 3], f[i + 4]], [f[i + 2], f[i + 4]]]);
|
||||
use dossier_hatch::section_dominance::{dominant_bands, Band};
|
||||
let mut ids: HashMap<&str, u32> = HashMap::new();
|
||||
let inp: Vec<Band> = bands
|
||||
.iter()
|
||||
.map(|band| {
|
||||
let comp = band.component_id.as_deref().map(|id| {
|
||||
let n = ids.len() as u32;
|
||||
*ids.entry(id).or_insert(n)
|
||||
});
|
||||
let rect = rect_of_band(band).map(|r| dossier_hatch::section_dominance::Rect { u_min: r[0], u_max: r[1], v_min: r[2], v_max: r[3] });
|
||||
Band { rect, prio: band.join_priority, comp }
|
||||
})
|
||||
.collect();
|
||||
dominant_bands(&inp)
|
||||
.into_iter()
|
||||
.map(|piece| {
|
||||
let src = &bands[piece.src];
|
||||
let Some(r) = piece.rect else { return src.clone() };
|
||||
let mut c = Cut::new(src.kind, src.index, vec![[r.u_min, r.v_max], [r.u_max, r.v_max], [r.u_max, r.v_min], [r.u_min, r.v_min]]);
|
||||
c.fill = src.fill.clone();
|
||||
c.hatch = src.hatch.clone();
|
||||
c.join_priority = src.join_priority;
|
||||
@@ -867,11 +862,9 @@ fn subtract_dominant_bands(bands: Vec<Cut>) -> Vec<Cut> {
|
||||
// Herkunft bleibt am Stück (Ebenen-Sichtbarkeit, Treppen-Pick).
|
||||
c.category = src.category.clone();
|
||||
c.stair_id = src.stair_id.clone();
|
||||
out.push(c);
|
||||
}
|
||||
i += 6;
|
||||
}
|
||||
out
|
||||
c
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ── Dächer ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
//! Schnitt-Bänder ([K3c]): Dominanz-Subtraktion und Verschmelzung achsparalleler Schnitt-Rechtecke
|
||||
//! (`toSection.ts`: `subtractDominantBands`, `mergeSameComponentBands`) sowie die Vereinigung zu
|
||||
//! Aussenringen (`unionRectOutlines`). Stil, Füllung, Schraffur und Herkunft hängen in TS an den
|
||||
//! Bändern; Rust rechnet nur auf Rechtecken. Rechenreihenfolge wie in TS.
|
||||
//!
|
||||
//! ## Dominanz
|
||||
//! `dominant_bands(in)` mit `in = [n, Band × n]`, Band = `[gueltig, uMin, uMax, vMin, vMax, prio, comp]`
|
||||
//! (`gueltig` 0 = weniger als 3 Ecken; `prio` NaN = keine; `comp` < 0 = keine Komponente) →
|
||||
//! `[m, Eintrag × m]`, Eintrag = `[quelle, istRechteck, uMin, uMax, vMin, vMax]`. `istRechteck` 0 =
|
||||
//! Quellband unverändert übernehmen, sonst neues Rechteck mit Stil von `quelle`.
|
||||
//! Schnitt-Bänder ([K3c]): Vereinigung achsparalleler Schnitt-Rechtecke zu Aussenringen
|
||||
//! (`unionRectOutlines`) und Dach-Schnittspur. Die Dominanz-Subtraktion (`subtractDominantBands`) liegt
|
||||
//! seit [125] gemeinsam mit dem 3D-Live-Schnitt in `dossier_hatch::section_dominance`. Rechenreihenfolge
|
||||
//! wie in TS.
|
||||
//!
|
||||
//! ## Dach-Schnittspur
|
||||
//! `roof_segments(base_x, base_y, u_x, u_y, planes)` mit `planes = [n, (k, x, y, z …) × n]` (Modell
|
||||
@@ -19,249 +13,9 @@
|
||||
//! `union_outlines(in)` mit `in = [n, (uMin, uMax, vMin, vMax) × n]` → `[r, (k, x, y × k) × r]`.
|
||||
|
||||
use crate::jsnum::{max, min};
|
||||
use crate::ugrid::Grid1;
|
||||
use dossier_hatch::section_dominance::Rect;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const RECT_EPS: f64 = 1e-6;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Rect {
|
||||
u_min: f64,
|
||||
u_max: f64,
|
||||
v_min: f64,
|
||||
v_max: f64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Band {
|
||||
rect: Option<Rect>,
|
||||
prio: Option<f64>,
|
||||
comp: Option<i64>,
|
||||
}
|
||||
|
||||
struct Out {
|
||||
src: usize,
|
||||
rect: Option<Rect>,
|
||||
}
|
||||
|
||||
fn subtract_rect(base: Rect, cutter: Rect) -> Vec<Rect> {
|
||||
let o_min_u = max(base.u_min, cutter.u_min);
|
||||
let o_max_u = min(base.u_max, cutter.u_max);
|
||||
let o_min_v = max(base.v_min, cutter.v_min);
|
||||
let o_max_v = min(base.v_max, cutter.v_max);
|
||||
if o_max_u - o_min_u <= RECT_EPS || o_max_v - o_min_v <= RECT_EPS {
|
||||
return vec![base];
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut push = |r: Rect| {
|
||||
if r.u_max - r.u_min > RECT_EPS && r.v_max - r.v_min > RECT_EPS {
|
||||
out.push(r);
|
||||
}
|
||||
};
|
||||
push(Rect { u_min: base.u_min, u_max: o_min_u, v_min: base.v_min, v_max: base.v_max });
|
||||
push(Rect { u_min: o_max_u, u_max: base.u_max, v_min: base.v_min, v_max: base.v_max });
|
||||
push(Rect { u_min: o_min_u, u_max: o_max_u, v_min: base.v_min, v_max: o_min_v });
|
||||
push(Rect { u_min: o_min_u, u_max: o_max_u, v_min: o_max_v, v_max: base.v_max });
|
||||
out
|
||||
}
|
||||
|
||||
fn rect_union_if_rect(a: Rect, b: Rect) -> Option<Rect> {
|
||||
let e = RECT_EPS;
|
||||
let a_in_b = b.u_min - a.u_min <= e && a.u_max - b.u_max <= e && b.v_min - a.v_min <= e && a.v_max - b.v_max <= e;
|
||||
if a_in_b {
|
||||
return Some(b);
|
||||
}
|
||||
let b_in_a = a.u_min - b.u_min <= e && b.u_max - a.u_max <= e && a.v_min - b.v_min <= e && b.v_max - a.v_max <= e;
|
||||
if b_in_a {
|
||||
return Some(a);
|
||||
}
|
||||
let u_aligned = (a.u_min - b.u_min).abs() <= e && (a.u_max - b.u_max).abs() <= e;
|
||||
let v_aligned = (a.v_min - b.v_min).abs() <= e && (a.v_max - b.v_max).abs() <= e;
|
||||
let v_touch = a.v_max >= b.v_min - e && b.v_max >= a.v_min - e;
|
||||
let u_touch = a.u_max >= b.u_min - e && b.u_max >= a.u_min - e;
|
||||
if (u_aligned && v_touch) || (v_aligned && u_touch) {
|
||||
return Some(Rect {
|
||||
u_min: min(a.u_min, b.u_min),
|
||||
u_max: max(a.u_max, b.u_max),
|
||||
v_min: min(a.v_min, b.v_min),
|
||||
v_max: max(a.v_max, b.v_max),
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Verschmilzt Rechtecke gleicher Komponente und Priorität. Ergebnis wie die schlichte Fassung
|
||||
/// ([`merge_same_component_naive`]: immer das lexikographisch kleinste verschmelzbare Paar (i, j),
|
||||
/// dann von vorn), aber ohne den Neustart über alle Paare: Nach dem Verschmelzen von (p, b) können
|
||||
/// nur Paare mit dem geänderten Eintrag p neu passen; davor liegende Einträge hatten sonst keinen
|
||||
/// Partner. Kandidaten nur aus derselben Gruppe (Komponente, Priorität).
|
||||
fn merge_same_component(bands: &[Band], list: Vec<Out>) -> Vec<Out> {
|
||||
let n = list.len();
|
||||
let mut rects: Vec<Option<Rect>> = list.iter().map(|o| o.rect.or(bands[o.src].rect)).collect();
|
||||
let mut changed = vec![false; n];
|
||||
let mut alive = vec![true; n];
|
||||
// Gruppe je Eintrag (nur verschmelzbare: Komponente + Priorität + Rechteck).
|
||||
let mut groups: HashMap<(i64, u64), Vec<usize>> = HashMap::new();
|
||||
let mut group_of: Vec<Option<(i64, u64)>> = vec![None; n];
|
||||
for (k, o) in list.iter().enumerate() {
|
||||
let b = bands[o.src];
|
||||
if let (Some(c), Some(pr), Some(_)) = (b.comp, b.prio, rects[k]) {
|
||||
let key = (c, if pr == 0.0 { 0 } else { pr.to_bits() });
|
||||
groups.entry(key).or_default().push(k);
|
||||
group_of[k] = Some(key);
|
||||
}
|
||||
}
|
||||
let ok = |rects: &[Option<Rect>], a: usize, b: usize| -> Option<Rect> { rect_union_if_rect(rects[a]?, rects[b]?) };
|
||||
let mut p = 0;
|
||||
while p < n {
|
||||
let Some(key) = group_of[p].filter(|_| alive[p]) else {
|
||||
p += 1;
|
||||
continue;
|
||||
};
|
||||
let members = &groups[&key];
|
||||
// kleinster Partner b > p
|
||||
let hit = members.iter().copied().filter(|&b| b > p && alive[b]).find_map(|b| ok(&rects, p, b).map(|u| (b, u)));
|
||||
let Some((b, u)) = hit else {
|
||||
p += 1;
|
||||
continue;
|
||||
};
|
||||
rects[p] = Some(u);
|
||||
changed[p] = true;
|
||||
alive[b] = false;
|
||||
// Rückwärts: kleinster a < p, der jetzt mit dem geänderten Eintrag verschmilzt (Kette).
|
||||
let mut cur = p;
|
||||
loop {
|
||||
let members = &groups[&key];
|
||||
let back = members.iter().copied().filter(|&a| a < cur && alive[a]).find_map(|a| ok(&rects, a, cur).map(|u| (a, u)));
|
||||
let Some((a, u)) = back else { break };
|
||||
rects[a] = Some(u);
|
||||
changed[a] = true;
|
||||
alive[cur] = false;
|
||||
cur = a;
|
||||
}
|
||||
p = cur;
|
||||
}
|
||||
list.into_iter()
|
||||
.enumerate()
|
||||
.filter(|(k, _)| alive[*k])
|
||||
.map(|(k, o)| Out { src: o.src, rect: if changed[k] { rects[k] } else { o.rect } })
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn merge_same_component_naive(bands: &[Band], list: Vec<Out>) -> Vec<Out> {
|
||||
let mut out = list;
|
||||
let mut merged = true;
|
||||
while merged {
|
||||
merged = false;
|
||||
let mut i = 0;
|
||||
while i < out.len() && !merged {
|
||||
let bi = bands[out[i].src];
|
||||
// Verschmolzene Rechtecke erben Komponente/Priorität des Quellbands.
|
||||
if bi.comp.is_none() || bi.prio.is_none() {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let ri = match out[i].rect.or(bi.rect) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
for j in i + 1..out.len() {
|
||||
let bj = bands[out[j].src];
|
||||
if bj.comp != bi.comp || bj.prio != bi.prio {
|
||||
continue;
|
||||
}
|
||||
let rj = match out[j].rect.or(bj.rect) {
|
||||
Some(r) => r,
|
||||
None => continue,
|
||||
};
|
||||
if let Some(u) = rect_union_if_rect(ri, rj) {
|
||||
out[i].rect = Some(u);
|
||||
out.remove(j);
|
||||
merged = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn dominant_bands(inp: &[f64]) -> Vec<f64> {
|
||||
if inp.is_empty() || !inp[0].is_finite() || inp[0] < 0.0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let n = inp[0] as usize;
|
||||
if inp.len() != 1 + 7 * n {
|
||||
return Vec::new();
|
||||
}
|
||||
let bands: Vec<Band> = (0..n)
|
||||
.map(|k| {
|
||||
let b = &inp[1 + 7 * k..8 + 7 * k];
|
||||
Band {
|
||||
rect: (b[0] != 0.0).then_some(Rect { u_min: b[1], u_max: b[2], v_min: b[3], v_max: b[4] }),
|
||||
prio: (!b[5].is_nan()).then_some(b[5]),
|
||||
comp: (b[6] >= 0.0).then_some(b[6] as i64),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut out: Vec<Out> = Vec::new();
|
||||
// Vorauswahl der Schneider über u (aufsteigend wie die volle Schleife): ein Schneider, der das
|
||||
// Grundrechteck nicht trifft, lässt alle Teilstücke unverändert (`subtract_rect`).
|
||||
let cutters: Vec<usize> = (0..n).filter(|&j| bands[j].rect.is_some() && bands[j].prio.is_some()).collect();
|
||||
let grid = Grid1::new(&cutters.iter().map(|&j| bands[j].rect.map_or((0.0, 0.0), |r| (r.u_min, r.u_max))).collect::<Vec<_>>());
|
||||
let mut cand = Vec::new();
|
||||
for i in 0..n {
|
||||
let (base, prio) = match (bands[i].rect, bands[i].prio) {
|
||||
(Some(r), Some(p)) => (r, p),
|
||||
_ => {
|
||||
out.push(Out { src: i, rect: None });
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let mut pieces = vec![base];
|
||||
grid.query(base.u_min, base.u_max, &mut cand);
|
||||
for &c in &cand {
|
||||
let j = cutters[c as usize];
|
||||
if pieces.is_empty() {
|
||||
break;
|
||||
}
|
||||
if j == i {
|
||||
continue;
|
||||
}
|
||||
let (cutter, other) = match (bands[j].rect, bands[j].prio) {
|
||||
(Some(c), Some(p)) => (c, p),
|
||||
_ => continue,
|
||||
};
|
||||
if other <= prio {
|
||||
continue;
|
||||
}
|
||||
let mut next = Vec::new();
|
||||
for p in pieces {
|
||||
next.extend(subtract_rect(p, cutter));
|
||||
}
|
||||
pieces = next;
|
||||
}
|
||||
for p in pieces {
|
||||
out.push(Out { src: i, rect: Some(p) });
|
||||
}
|
||||
}
|
||||
let out = merge_same_component(&bands, out);
|
||||
let mut f = Vec::with_capacity(1 + 6 * out.len());
|
||||
f.push(out.len() as f64);
|
||||
for o in &out {
|
||||
match o.rect {
|
||||
Some(r) => f.extend([o.src as f64, 1.0, r.u_min, r.u_max, r.v_min, r.v_max]),
|
||||
None => f.extend([o.src as f64, 0.0, 0.0, 0.0, 0.0, 0.0]),
|
||||
}
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
fn signed_area(ring: &[(f64, f64)]) -> f64 {
|
||||
let mut a = 0.0;
|
||||
for i in 0..ring.len() {
|
||||
@@ -523,49 +277,6 @@ pub fn roof_segments(bx: f64, by: f64, ux: f64, uy: f64, planes: &[f64]) -> Vec<
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn band(u0: f64, u1: f64, v0: f64, v1: f64, prio: f64, comp: f64) -> [f64; 7] {
|
||||
[1.0, u0, u1, v0, v1, prio, comp]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hoehere_prioritaet_schneidet_weg() {
|
||||
let mut inp = vec![2.0];
|
||||
inp.extend(band(0.0, 1.0, 0.0, 1.0, 10.0, 0.0));
|
||||
inp.extend(band(0.0, 1.0, 0.5, 1.0, 100.0, 1.0));
|
||||
let f = dominant_bands(&inp);
|
||||
// Basis behält die untere Hälfte, Cutter bleibt ganz.
|
||||
assert_eq!(f[0], 2.0);
|
||||
assert_eq!(&f[1..7], &[0.0, 1.0, 0.0, 1.0, 0.0, 0.5]);
|
||||
}
|
||||
|
||||
fn r(u0: f64, u1: f64, v0: f64, v1: f64) -> Rect {
|
||||
Rect { u_min: u0, u_max: u1, v_min: v0, v_max: v1 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rechteck_subtraktion() {
|
||||
// Kein Überlapp, nur Berührung, volle Überdeckung, Ecke, Randstreifen.
|
||||
assert_eq!(subtract_rect(r(0.0, 1.0, 0.0, 1.0), r(2.0, 3.0, 2.0, 3.0)).len(), 1);
|
||||
assert_eq!(subtract_rect(r(0.0, 1.0, 0.0, 1.0), r(1.0, 2.0, 0.0, 1.0)).len(), 1);
|
||||
assert!(subtract_rect(r(1.0, 2.0, 1.0, 2.0), r(0.0, 3.0, 0.0, 3.0)).is_empty());
|
||||
let ecke = subtract_rect(r(0.0, 2.0, 0.0, 2.0), r(1.0, 3.0, 1.0, 3.0));
|
||||
let flaeche: f64 = ecke.iter().map(|x| (x.u_max - x.u_min) * (x.v_max - x.v_min)).sum();
|
||||
assert_eq!((ecke.len(), flaeche), (2, 3.0));
|
||||
let rand = subtract_rect(r(0.0, 2.0, 0.0, 2.0), r(-1.0, 3.0, 1.0, 3.0));
|
||||
assert_eq!(rand.len(), 1);
|
||||
assert_eq!((rand[0].v_min, rand[0].v_max), (0.0, 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gleiche_komponente_verschmilzt() {
|
||||
let mut inp = vec![2.0];
|
||||
inp.extend(band(0.0, 1.0, 0.0, 1.0, 10.0, 3.0));
|
||||
inp.extend(band(1.0, 2.0, 0.0, 1.0, 10.0, 3.0));
|
||||
let f = dominant_bands(&inp);
|
||||
assert_eq!(f[0], 1.0);
|
||||
assert_eq!(&f[1..7], &[0.0, 1.0, 0.0, 2.0, 0.0, 1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn l_form_ergibt_einen_ring() {
|
||||
let f = union_outlines(&[2.0, 0.0, 2.0, 0.0, 1.0, 0.0, 1.0, 0.0, 2.0]);
|
||||
@@ -586,8 +297,6 @@ mod tests {
|
||||
#[test]
|
||||
fn unsinn_ist_leer() {
|
||||
assert!(roof_segments(f64::NAN, 0.0, 1.0, 0.0, &[0.0]).is_empty());
|
||||
assert!(dominant_bands(&[f64::NAN]).is_empty());
|
||||
assert!(dominant_bands(&[2.0, 1.0]).is_empty());
|
||||
assert!(union_outlines(&[1.0, f64::NAN, 1.0, 0.0, 1.0]).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -596,62 +305,6 @@ mod tests {
|
||||
mod tests_schnell {
|
||||
use super::*;
|
||||
|
||||
/// Die schnelle Verschmelzung und die Vorauswahl der Schneider liefern dasselbe wie die schlichte
|
||||
/// Fassung (Zufalls-Rechtecke auf einem groben Raster, viele Berührungen und gleiche Komponenten).
|
||||
#[test]
|
||||
fn schnell_gleich_schlicht() {
|
||||
let mut x: u64 = 12345;
|
||||
let mut rnd = |m: u64| {
|
||||
x = x.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
||||
(x >> 33) % m
|
||||
};
|
||||
for _ in 0..300 {
|
||||
let n = 1 + rnd(40) as usize;
|
||||
let mut inp = vec![n as f64];
|
||||
for _ in 0..n {
|
||||
let u0 = rnd(10) as f64 * 0.5;
|
||||
let v0 = rnd(10) as f64 * 0.5;
|
||||
let (w, h) = (0.5 * (1 + rnd(4)) as f64, 0.5 * (1 + rnd(4)) as f64);
|
||||
let prio = [10.0, 50.0, 100.0, f64::NAN][rnd(4) as usize];
|
||||
let comp = rnd(4) as f64 - 1.0;
|
||||
inp.extend([if rnd(10) == 0 { 0.0 } else { 1.0 }, u0, u0 + w, v0, v0 + h, prio, comp]);
|
||||
}
|
||||
let fast = dominant_bands(&inp);
|
||||
// schlicht: alle Schneider, schlichte Verschmelzung
|
||||
let bands: Vec<Band> = (0..n)
|
||||
.map(|k| {
|
||||
let b = &inp[1 + 7 * k..8 + 7 * k];
|
||||
Band { rect: (b[0] != 0.0).then_some(Rect { u_min: b[1], u_max: b[2], v_min: b[3], v_max: b[4] }), prio: (!b[5].is_nan()).then_some(b[5]), comp: (b[6] >= 0.0).then_some(b[6] as i64) }
|
||||
})
|
||||
.collect();
|
||||
let mut out = Vec::new();
|
||||
for i in 0..n {
|
||||
let (Some(base), Some(prio)) = (bands[i].rect, bands[i].prio) else {
|
||||
out.push(Out { src: i, rect: None });
|
||||
continue;
|
||||
};
|
||||
let mut pieces = vec![base];
|
||||
for j in 0..n {
|
||||
let (Some(c), Some(o)) = (bands[j].rect, bands[j].prio) else { continue };
|
||||
if j == i || o <= prio {
|
||||
continue;
|
||||
}
|
||||
pieces = pieces.into_iter().flat_map(|p| subtract_rect(p, c)).collect();
|
||||
}
|
||||
out.extend(pieces.into_iter().map(|p| Out { src: i, rect: Some(p) }));
|
||||
}
|
||||
let slow = merge_same_component_naive(&bands, out);
|
||||
let mut f = vec![slow.len() as f64];
|
||||
for o in &slow {
|
||||
match o.rect {
|
||||
Some(r) => f.extend([o.src as f64, 1.0, r.u_min, r.u_max, r.v_min, r.v_max]),
|
||||
None => f.extend([o.src as f64, 0.0, 0.0, 0.0, 0.0, 0.0]),
|
||||
}
|
||||
}
|
||||
assert_eq!(fast, f);
|
||||
}
|
||||
}
|
||||
|
||||
/// Belegung der Umriss-Zellen: Bereichs-Malen = Prüfung je Zelle gegen alle Rechtecke.
|
||||
#[test]
|
||||
fn umriss_belegung_wie_je_zelle() {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
//! Schraffur-Generator ([109]): `hatch` (Linien-, Schicht-, Streumuster), `jsnum`/`jsmath` (JS-Zahlsemantik).
|
||||
//! Schraffur-Generator ([109]): `hatch` (Linien-, Schicht-, Streumuster), `jsnum`/`jsmath` (JS-Zahlsemantik);
|
||||
//! `section_dominance` (Schnitt-Dominanz der Schnittbänder, [125]) und `ugrid` (Bereichs-Vorauswahl).
|
||||
//! Gemeinsam genutzt von `dossier-core` (Re-Export unter `crate::hatch` usw.) und `render3d` (Live-Schnitt).
|
||||
|
||||
pub mod hatch;
|
||||
pub mod jsmath;
|
||||
pub mod jsnum;
|
||||
pub mod section_dominance;
|
||||
pub mod ugrid;
|
||||
@@ -0,0 +1,455 @@
|
||||
//! Schnitt-Dominanz ([125]): die EINE Fassung von `subtractDominantBands` (früher `toSection.ts`) für den
|
||||
//! 2D-Schnitt (`dossier-core`, `plan/section.rs`) und den 3D-Live-Schnitt (`render3d`,
|
||||
//! `section_boolean.rs`). Beide Aufrufer reichen ihre Schnittbänder als achsparallele (u,v)-Rechtecke mit
|
||||
//! Priorität (`Component.joinPriority`) und Komponenten-Nummer herein und bekommen Teilstücke zurück;
|
||||
//! Stil, Schraffur und Herkunft bleiben beim Aufrufer (Feld `src` = Index des Quellbands).
|
||||
//!
|
||||
//! 1. **Subtraktion:** Jedes Band verliert die Überlappung mit ALLEN Bändern STRIKT höherer Priorität
|
||||
//! (elementübergreifend, z. B. schneidet Decken-Beton 100 den Wand-Putz 10 weg). Gleiche Priorität
|
||||
//! koexistiert. Bänder ohne Priorität oder ohne (endliches) Rechteck nehmen weder als Basis noch als
|
||||
//! Schneider teil und bleiben unverändert.
|
||||
//! 2. **Verschmelzen:** Stücke gleicher Komponente und Priorität, deren Union wieder ein Rechteck ist,
|
||||
//! werden zu einem Stück (keine innere Trennlinie durch gleiches Material).
|
||||
//!
|
||||
//! Gerechnet wird in f64 mit JS-Zahlsemantik (`jsnum::min/max`), Reihenfolge wie die TS-Vorlage. Keine
|
||||
//! wasm-Bindungen hier.
|
||||
|
||||
use crate::jsnum::{max, min};
|
||||
use crate::ugrid::Grid1;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Toleranz für Überlappungs-/Rest-Rechtecke (Meter): Null-/Negativflächen und Rest-Streifen dünner als
|
||||
/// `RECT_EPS` werden verworfen.
|
||||
pub const RECT_EPS: f64 = 1e-6;
|
||||
|
||||
/// Achsparalleles Rechteck in (u, v)-Metern.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Rect {
|
||||
pub u_min: f64,
|
||||
pub u_max: f64,
|
||||
pub v_min: f64,
|
||||
pub v_max: f64,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
fn is_finite(&self) -> bool {
|
||||
self.u_min.is_finite() && self.u_max.is_finite() && self.v_min.is_finite() && self.v_max.is_finite()
|
||||
}
|
||||
}
|
||||
|
||||
/// Umgebendes Rechteck von Punkten (JS-`Math.min/max`: NaN bleibt NaN), `None` bei weniger als 3 Punkten.
|
||||
pub fn rect_of_points(pts: impl ExactSizeIterator<Item = (f64, f64)>) -> Option<Rect> {
|
||||
if pts.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
let mut r = Rect { u_min: f64::INFINITY, u_max: f64::NEG_INFINITY, v_min: f64::INFINITY, v_max: f64::NEG_INFINITY };
|
||||
for (u, v) in pts {
|
||||
r.u_min = min(r.u_min, u);
|
||||
r.u_max = max(r.u_max, u);
|
||||
r.v_min = min(r.v_min, v);
|
||||
r.v_max = max(r.v_max, v);
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
|
||||
/// Eingabe-Band: Rechteck (`None` = keine Rechteck-Form), Priorität, Komponente (gleiche Nummer = gleiche
|
||||
/// `componentId`).
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Band {
|
||||
pub rect: Option<Rect>,
|
||||
pub prio: Option<f64>,
|
||||
pub comp: Option<u32>,
|
||||
}
|
||||
|
||||
/// Ergebnis-Stück: `rect == None` heisst Quellband `src` unverändert übernehmen, sonst neues Rechteck mit
|
||||
/// dem Stil von `src`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Piece {
|
||||
pub src: usize,
|
||||
pub rect: Option<Rect>,
|
||||
}
|
||||
|
||||
/// Rechteck minus Rechteck: `base` ohne die Überlappung mit `cutter`, als 0–4 disjunkte Reste (links/rechts
|
||||
/// über die volle Höhe, dann unten/oben im mittleren u-Streifen). Kein echter Überlapp (≤ EPS) ⇒ `base`.
|
||||
pub fn subtract_rect(base: Rect, cutter: Rect) -> Vec<Rect> {
|
||||
let o_min_u = max(base.u_min, cutter.u_min);
|
||||
let o_max_u = min(base.u_max, cutter.u_max);
|
||||
let o_min_v = max(base.v_min, cutter.v_min);
|
||||
let o_max_v = min(base.v_max, cutter.v_max);
|
||||
if o_max_u - o_min_u <= RECT_EPS || o_max_v - o_min_v <= RECT_EPS {
|
||||
return vec![base];
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut push = |r: Rect| {
|
||||
if r.u_max - r.u_min > RECT_EPS && r.v_max - r.v_min > RECT_EPS {
|
||||
out.push(r);
|
||||
}
|
||||
};
|
||||
push(Rect { u_min: base.u_min, u_max: o_min_u, v_min: base.v_min, v_max: base.v_max });
|
||||
push(Rect { u_min: o_max_u, u_max: base.u_max, v_min: base.v_min, v_max: base.v_max });
|
||||
push(Rect { u_min: o_min_u, u_max: o_max_u, v_min: base.v_min, v_max: o_min_v });
|
||||
push(Rect { u_min: o_min_u, u_max: o_max_u, v_min: o_max_v, v_max: base.v_max });
|
||||
out
|
||||
}
|
||||
|
||||
/// Union zweier Rechtecke, nur wenn sie wieder EXAKT ein Rechteck ist (Enthaltensein oder Achs-Deckung +
|
||||
/// Berührung), sonst `None` (L-Form, Spalt).
|
||||
pub fn rect_union_if_rect(a: Rect, b: Rect) -> Option<Rect> {
|
||||
let e = RECT_EPS;
|
||||
let a_in_b = b.u_min - a.u_min <= e && a.u_max - b.u_max <= e && b.v_min - a.v_min <= e && a.v_max - b.v_max <= e;
|
||||
if a_in_b {
|
||||
return Some(b);
|
||||
}
|
||||
let b_in_a = a.u_min - b.u_min <= e && b.u_max - a.u_max <= e && a.v_min - b.v_min <= e && b.v_max - a.v_max <= e;
|
||||
if b_in_a {
|
||||
return Some(a);
|
||||
}
|
||||
let u_aligned = (a.u_min - b.u_min).abs() <= e && (a.u_max - b.u_max).abs() <= e;
|
||||
let v_aligned = (a.v_min - b.v_min).abs() <= e && (a.v_max - b.v_max).abs() <= e;
|
||||
let v_touch = a.v_max >= b.v_min - e && b.v_max >= a.v_min - e;
|
||||
let u_touch = a.u_max >= b.u_min - e && b.u_max >= a.u_min - e;
|
||||
if (u_aligned && v_touch) || (v_aligned && u_touch) {
|
||||
return Some(Rect {
|
||||
u_min: min(a.u_min, b.u_min),
|
||||
u_max: max(a.u_max, b.u_max),
|
||||
v_min: min(a.v_min, b.v_min),
|
||||
v_max: max(a.v_max, b.v_max),
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Teilnehmendes Band (endliches Rechteck + Priorität)?
|
||||
fn active(b: &Band) -> Option<(Rect, f64)> {
|
||||
match (b.rect, b.prio) {
|
||||
(Some(r), Some(p)) if r.is_finite() && !p.is_nan() => Some((r, p)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Schnitt-Dominanz: Subtraktion, dann Verschmelzen (siehe Modulkopf).
|
||||
pub fn dominant_bands(bands: &[Band]) -> Vec<Piece> {
|
||||
let n = bands.len();
|
||||
let mut out: Vec<Piece> = Vec::new();
|
||||
// Vorauswahl der Schneider über u (aufsteigend wie die volle Schleife): ein Schneider, der das
|
||||
// Grundrechteck nicht trifft, lässt alle Teilstücke unverändert (`subtract_rect`).
|
||||
let cutters: Vec<usize> = (0..n).filter(|&j| active(&bands[j]).is_some()).collect();
|
||||
let grid = Grid1::new(&cutters.iter().map(|&j| active(&bands[j]).map_or((0.0, 0.0), |(r, _)| (r.u_min, r.u_max))).collect::<Vec<_>>());
|
||||
let mut cand = Vec::new();
|
||||
for i in 0..n {
|
||||
let Some((base, prio)) = active(&bands[i]) else {
|
||||
out.push(Piece { src: i, rect: None });
|
||||
continue;
|
||||
};
|
||||
let mut pieces = vec![base];
|
||||
grid.query(base.u_min, base.u_max, &mut cand);
|
||||
for &c in &cand {
|
||||
let j = cutters[c as usize];
|
||||
if pieces.is_empty() {
|
||||
break;
|
||||
}
|
||||
if j == i {
|
||||
continue;
|
||||
}
|
||||
let Some((cutter, other)) = active(&bands[j]) else { continue };
|
||||
if other <= prio {
|
||||
continue;
|
||||
}
|
||||
let mut next = Vec::new();
|
||||
for p in pieces {
|
||||
next.extend(subtract_rect(p, cutter));
|
||||
}
|
||||
pieces = next;
|
||||
}
|
||||
for p in pieces {
|
||||
out.push(Piece { src: i, rect: Some(p) });
|
||||
}
|
||||
}
|
||||
merge_same_component(bands, out)
|
||||
}
|
||||
|
||||
/// Verschmilzt Stücke gleicher Komponente und Priorität. Ergebnis wie die schlichte Fassung
|
||||
/// ([`merge_same_component_naive`]: immer das lexikographisch kleinste verschmelzbare Paar (i, j), dann
|
||||
/// von vorn), aber ohne den Neustart über alle Paare: Nach dem Verschmelzen von (p, b) können nur Paare
|
||||
/// mit dem geänderten Eintrag p neu passen; davor liegende Einträge hatten sonst keinen Partner.
|
||||
/// Kandidaten nur aus derselben Gruppe (Komponente, Priorität).
|
||||
fn merge_same_component(bands: &[Band], list: Vec<Piece>) -> Vec<Piece> {
|
||||
let n = list.len();
|
||||
// Unverändert übernommene Bänder (`rect == None`) nehmen nicht teil: sie haben keine Priorität
|
||||
// oder kein endliches Rechteck.
|
||||
let mut rects: Vec<Option<Rect>> = list.iter().map(|o| o.rect).collect();
|
||||
let mut changed = vec![false; n];
|
||||
let mut alive = vec![true; n];
|
||||
let mut groups: HashMap<(u32, u64), Vec<usize>> = HashMap::new();
|
||||
let mut group_of: Vec<Option<(u32, u64)>> = vec![None; n];
|
||||
for (k, o) in list.iter().enumerate() {
|
||||
let b = bands[o.src];
|
||||
if let (Some(c), Some(pr), Some(_)) = (b.comp, b.prio, rects[k]) {
|
||||
let key = (c, if pr == 0.0 { 0 } else { pr.to_bits() });
|
||||
groups.entry(key).or_default().push(k);
|
||||
group_of[k] = Some(key);
|
||||
}
|
||||
}
|
||||
let ok = |rects: &[Option<Rect>], a: usize, b: usize| -> Option<Rect> { rect_union_if_rect(rects[a]?, rects[b]?) };
|
||||
let mut p = 0;
|
||||
while p < n {
|
||||
let Some(key) = group_of[p].filter(|_| alive[p]) else {
|
||||
p += 1;
|
||||
continue;
|
||||
};
|
||||
let members = &groups[&key];
|
||||
// kleinster Partner b > p
|
||||
let hit = members.iter().copied().filter(|&b| b > p && alive[b]).find_map(|b| ok(&rects, p, b).map(|u| (b, u)));
|
||||
let Some((b, u)) = hit else {
|
||||
p += 1;
|
||||
continue;
|
||||
};
|
||||
rects[p] = Some(u);
|
||||
changed[p] = true;
|
||||
alive[b] = false;
|
||||
// Rückwärts: kleinster a < p, der jetzt mit dem geänderten Eintrag verschmilzt (Kette).
|
||||
let mut cur = p;
|
||||
loop {
|
||||
let members = &groups[&key];
|
||||
let back = members.iter().copied().filter(|&a| a < cur && alive[a]).find_map(|a| ok(&rects, a, cur).map(|u| (a, u)));
|
||||
let Some((a, u)) = back else { break };
|
||||
rects[a] = Some(u);
|
||||
changed[a] = true;
|
||||
alive[cur] = false;
|
||||
cur = a;
|
||||
}
|
||||
p = cur;
|
||||
}
|
||||
list.into_iter()
|
||||
.enumerate()
|
||||
.filter(|(k, _)| alive[*k])
|
||||
.map(|(k, o)| Piece { src: o.src, rect: if changed[k] { rects[k] } else { o.rect } })
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Schlichte Fassung (TS-Vorlage `mergeSameComponentBands`): Referenz für die Tests.
|
||||
#[cfg(test)]
|
||||
fn merge_same_component_naive(bands: &[Band], list: Vec<Piece>) -> Vec<Piece> {
|
||||
let mut out = list;
|
||||
let mut merged = true;
|
||||
while merged {
|
||||
merged = false;
|
||||
let mut i = 0;
|
||||
while i < out.len() && !merged {
|
||||
let bi = bands[out[i].src];
|
||||
let (Some(_), Some(_), Some(ri)) = (bi.comp, bi.prio, out[i].rect) else {
|
||||
i += 1;
|
||||
continue;
|
||||
};
|
||||
for j in i + 1..out.len() {
|
||||
let bj = bands[out[j].src];
|
||||
if bj.comp != bi.comp || bj.prio != bi.prio {
|
||||
continue;
|
||||
}
|
||||
let Some(rj) = out[j].rect else { continue };
|
||||
if let Some(u) = rect_union_if_rect(ri, rj) {
|
||||
out[i].rect = Some(u);
|
||||
out.remove(j);
|
||||
merged = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn r(u0: f64, u1: f64, v0: f64, v1: f64) -> Rect {
|
||||
Rect { u_min: u0, u_max: u1, v_min: v0, v_max: v1 }
|
||||
}
|
||||
|
||||
fn band(u0: f64, u1: f64, v0: f64, v1: f64, prio: f64, comp: u32) -> Band {
|
||||
Band { rect: Some(r(u0, u1, v0, v1)), prio: Some(prio), comp: Some(comp) }
|
||||
}
|
||||
|
||||
fn area_of(out: &[Piece], bands: &[Band], comp: u32) -> f64 {
|
||||
out.iter()
|
||||
.filter(|p| bands[p.src].comp == Some(comp))
|
||||
.map(|p| p.rect.or(bands[p.src].rect).map_or(0.0, |x| (x.u_max - x.u_min) * (x.v_max - x.v_min)))
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn approx(a: f64, b: f64) -> bool {
|
||||
(a - b).abs() < 1e-9
|
||||
}
|
||||
|
||||
// --- subtract_rect ----------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn rechteck_subtraktion() {
|
||||
// Kein Überlapp, nur Berührung, volle Überdeckung, mittig, Ecke, Randstreifen.
|
||||
assert_eq!(subtract_rect(r(0.0, 1.0, 0.0, 1.0), r(2.0, 3.0, 2.0, 3.0)), vec![r(0.0, 1.0, 0.0, 1.0)]);
|
||||
assert_eq!(subtract_rect(r(0.0, 1.0, 0.0, 1.0), r(1.0, 2.0, 0.0, 1.0)), vec![r(0.0, 1.0, 0.0, 1.0)]);
|
||||
assert!(subtract_rect(r(1.0, 2.0, 1.0, 2.0), r(0.0, 3.0, 0.0, 3.0)).is_empty());
|
||||
let mitte = subtract_rect(r(0.0, 3.0, 0.0, 3.0), r(1.0, 2.0, 1.0, 2.0));
|
||||
let flaeche: f64 = mitte.iter().map(|x| (x.u_max - x.u_min) * (x.v_max - x.v_min)).sum();
|
||||
assert_eq!((mitte.len(), flaeche), (4, 8.0));
|
||||
let ecke = subtract_rect(r(0.0, 2.0, 0.0, 2.0), r(1.0, 3.0, 1.0, 3.0));
|
||||
let flaeche: f64 = ecke.iter().map(|x| (x.u_max - x.u_min) * (x.v_max - x.v_min)).sum();
|
||||
assert_eq!((ecke.len(), flaeche), (2, 3.0));
|
||||
let rand = subtract_rect(r(0.0, 2.0, 0.0, 2.0), r(-1.0, 3.0, 1.0, 3.0));
|
||||
assert_eq!(rand, vec![r(0.0, 2.0, 0.0, 1.0)]);
|
||||
}
|
||||
|
||||
// --- dominant_bands -----------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn hoehere_prioritaet_schneidet_weg() {
|
||||
let bands = [band(0.0, 1.0, 0.0, 1.0, 10.0, 0), band(0.0, 1.0, 0.5, 1.0, 100.0, 1)];
|
||||
let out = dominant_bands(&bands);
|
||||
// Basis behält die untere Hälfte, Schneider bleibt ganz.
|
||||
assert_eq!(out, vec![Piece { src: 0, rect: Some(r(0.0, 1.0, 0.0, 0.5)) }, Piece { src: 1, rect: Some(r(0.0, 1.0, 0.5, 1.0)) }]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decke_100_schneidet_wand_50_im_ueberlappungs_intervall_weg() {
|
||||
// Backstein (50, volle Höhe) und Decken-Beton (100, v in [2.6, 2.8]) am selben u.
|
||||
let bands = [band(0.0, 0.2, 0.0, 3.0, 50.0, 0), band(0.0, 0.2, 2.6, 2.8, 100.0, 1)];
|
||||
let out = dominant_bands(&bands);
|
||||
assert!(approx(area_of(&out, &bands, 1), 0.2 * 0.2));
|
||||
for p in out.iter().filter(|p| p.src == 0) {
|
||||
let x = p.rect.unwrap();
|
||||
assert!(!(x.v_min < 2.8 - 1e-4 && x.v_max > 2.6 + 1e-4), "Backstein-Rest {x:?} ragt ins Beton-Intervall");
|
||||
}
|
||||
assert!(approx(area_of(&out, &bands, 0), 0.56));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wand_50_schneidet_estrich_30() {
|
||||
let bands = [band(0.0, 2.0, 0.0, 3.0, 30.0, 0), band(0.0, 2.0, 2.0, 4.0, 50.0, 1)];
|
||||
let out = dominant_bands(&bands);
|
||||
assert_eq!(out[0], Piece { src: 0, rect: Some(r(0.0, 2.0, 0.0, 2.0)) });
|
||||
assert!(approx(area_of(&out, &bands, 1), 4.0));
|
||||
}
|
||||
|
||||
/// Schnitt durch Aussenwand + Geschossdecke: Estrich (30) und Dämmung (20) verlieren den u-Bereich
|
||||
/// des Backsteins (50, zwei Boxen um den Beton herum); Beton (100) bleibt voll.
|
||||
#[test]
|
||||
fn backstein_schneidet_estrich_und_daemmung() {
|
||||
let bu = (0.0075, 0.1575);
|
||||
let bands = [
|
||||
band(0.0, 5.0, 2.54, 2.6, 30.0, 0),
|
||||
band(0.0, 5.0, 2.50, 2.54, 20.0, 1),
|
||||
band(0.0, 5.0, 2.30, 2.50, 100.0, 2),
|
||||
band(bu.0, bu.1, 0.0, 2.3, 50.0, 3),
|
||||
band(bu.0, bu.1, 2.5, 2.6, 50.0, 3),
|
||||
];
|
||||
let out = dominant_bands(&bands);
|
||||
for p in out.iter().filter(|p| p.src < 2) {
|
||||
let x = p.rect.unwrap();
|
||||
assert!(!(x.u_min < bu.1 - 1e-4 && x.u_max > bu.0 + 1e-4), "Rest {x:?} ragt in den Backstein");
|
||||
}
|
||||
assert!(approx(area_of(&out, &bands, 2), 1.0));
|
||||
assert!(approx(area_of(&out, &bands, 3), 0.15 * 2.3 + 0.15 * 0.1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gleiche_prioritaet_koexistiert() {
|
||||
// Gleiche Priorität, verschiedene Komponenten: kein Schnitt, kein Verschmelzen.
|
||||
let bands = [band(0.0, 1.0, 0.0, 3.0, 100.0, 0), band(0.0, 1.0, 2.0, 4.0, 100.0, 1)];
|
||||
let out = dominant_bands(&bands);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert!(approx(area_of(&out, &bands, 0), 3.0) && approx(area_of(&out, &bands, 1), 2.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ohne_prioritaet_keine_teilnahme() {
|
||||
let bands = [band(0.0, 2.0, 0.0, 2.0, 100.0, 0), Band { rect: Some(r(0.0, 2.0, 0.0, 2.0)), prio: None, comp: None }];
|
||||
let out = dominant_bands(&bands);
|
||||
assert_eq!(out, vec![Piece { src: 0, rect: Some(r(0.0, 2.0, 0.0, 2.0)) }, Piece { src: 1, rect: None }]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gleiche_komponente_verschmilzt() {
|
||||
// Nebeneinander (v-deckungsgleich) und übereinander mit Überlapp (u-deckungsgleich).
|
||||
let bands = [band(0.0, 1.0, 0.0, 1.0, 10.0, 3), band(1.0, 2.0, 0.0, 1.0, 10.0, 3)];
|
||||
assert_eq!(dominant_bands(&bands), vec![Piece { src: 0, rect: Some(r(0.0, 2.0, 0.0, 1.0)) }]);
|
||||
let bands = [band(0.0, 1.0, 0.0, 3.0, 100.0, 0), band(0.0, 1.0, 2.0, 4.0, 100.0, 0)];
|
||||
assert_eq!(dominant_bands(&bands), vec![Piece { src: 0, rect: Some(r(0.0, 1.0, 0.0, 4.0)) }]);
|
||||
}
|
||||
|
||||
/// [125] Abweichung der früheren Fassungen: Ein Band mit nicht endlichem Rechteck (NaN-Ecke) liess im
|
||||
/// 2D-Schnitt (JS-`Math.min/max`) als Schneider JEDES schwächere Band verschwinden (alle Vergleiche
|
||||
/// mit NaN falsch ⇒ kein Rest), im 3D-Schnitt (f32 `min/max` ignorieren NaN) zählte nur die endliche
|
||||
/// Hülle. Jetzt nimmt es wie ein Band ohne Rechteck nicht teil.
|
||||
#[test]
|
||||
fn nicht_endliches_rechteck_nimmt_nicht_teil() {
|
||||
let kaputt = Band { rect: Some(r(f64::NAN, 1.0, 0.0, 1.0)), prio: Some(100.0), comp: Some(1) };
|
||||
let bands = [band(0.0, 1.0, 0.0, 1.0, 10.0, 0), kaputt];
|
||||
let out = dominant_bands(&bands);
|
||||
assert_eq!(out, vec![Piece { src: 0, rect: Some(r(0.0, 1.0, 0.0, 1.0)) }, Piece { src: 1, rect: None }]);
|
||||
let pts = [(0.0, 0.0), (f64::NAN, 1.0), (1.0, 1.0)];
|
||||
assert!(rect_of_points(pts.iter().copied()).is_some_and(|x| !x.is_finite()));
|
||||
assert!(rect_of_points([(0.0, 0.0), (1.0, 1.0)].into_iter()).is_none());
|
||||
}
|
||||
|
||||
/// [125] Abweichung der früheren 3D-Fassung: sie rechnete in f32 — bei Koordinaten um 100 m (ulp ≈ 8e-6)
|
||||
/// verschwand ein Überlapp von 2e-6 (> EPS) beim Runden; das Stück blieb im 3D-Schnitt stehen, im
|
||||
/// 2D-Schnitt nicht. Beide rechnen jetzt in f64.
|
||||
#[test]
|
||||
fn kleiner_ueberlapp_bei_grossen_koordinaten() {
|
||||
let bands = [band(0.0, 100.0, 0.0, 1.0, 10.0, 0), band(99.999998, 200.0, 0.0, 1.0, 100.0, 1)];
|
||||
let out = dominant_bands(&bands);
|
||||
assert_eq!(out[0], Piece { src: 0, rect: Some(r(0.0, 99.999998, 0.0, 1.0)) });
|
||||
// f32 sah hier keinen Überlapp:
|
||||
assert_eq!(99.999998_f32, 100.0_f32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leere_eingabe() {
|
||||
assert!(dominant_bands(&[]).is_empty());
|
||||
}
|
||||
|
||||
/// Die schnelle Verschmelzung und die Vorauswahl der Schneider liefern dasselbe wie die schlichte
|
||||
/// Fassung (Zufalls-Rechtecke auf einem groben Raster, viele Berührungen und gleiche Komponenten).
|
||||
#[test]
|
||||
fn schnell_gleich_schlicht() {
|
||||
let mut x: u64 = 12345;
|
||||
let mut rnd = |m: u64| {
|
||||
x = x.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
||||
(x >> 33) % m
|
||||
};
|
||||
for _ in 0..300 {
|
||||
let n = 1 + rnd(40) as usize;
|
||||
let bands: Vec<Band> = (0..n)
|
||||
.map(|_| {
|
||||
let (u0, v0) = (rnd(10) as f64 * 0.5, rnd(10) as f64 * 0.5);
|
||||
let (w, h) = (0.5 * (1 + rnd(4)) as f64, 0.5 * (1 + rnd(4)) as f64);
|
||||
let prio = [Some(10.0), Some(50.0), Some(100.0), None][rnd(4) as usize];
|
||||
let comp = rnd(4).checked_sub(1).map(|c| c as u32);
|
||||
let rect = (rnd(10) != 0).then_some(r(u0, u0 + w, v0, v0 + h));
|
||||
Band { rect, prio, comp }
|
||||
})
|
||||
.collect();
|
||||
let fast = dominant_bands(&bands);
|
||||
// schlicht: alle Schneider der Reihe nach, schlichte Verschmelzung
|
||||
let mut out = Vec::new();
|
||||
for i in 0..n {
|
||||
let (Some(base), Some(prio)) = (bands[i].rect, bands[i].prio) else {
|
||||
out.push(Piece { src: i, rect: None });
|
||||
continue;
|
||||
};
|
||||
let mut pieces = vec![base];
|
||||
for j in 0..n {
|
||||
let (Some(c), Some(o)) = (bands[j].rect, bands[j].prio) else { continue };
|
||||
if j == i || o <= prio {
|
||||
continue;
|
||||
}
|
||||
pieces = pieces.into_iter().flat_map(|p| subtract_rect(p, c)).collect();
|
||||
}
|
||||
out.extend(pieces.into_iter().map(|p| Piece { src: i, rect: Some(p) }));
|
||||
}
|
||||
assert_eq!(fast, merge_same_component_naive(&bands, out));
|
||||
}
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -1,260 +1,55 @@
|
||||
//! Schnitt-Boolean-Dominanz fuer den 3D-Live-Schnitt — die 1:1-Portierung von
|
||||
//! `src/plan/toSection.ts::subtractDominantBands`/`subtractRect` nach Rust.
|
||||
//! Schnitt-Boolean-Dominanz fuer den 3D-Live-Schnitt: duenner Adapter um
|
||||
//! `dossier_hatch::section_dominance` ([125]) — dieselbe Rechnung wie der 2D-Schnitt
|
||||
//! (`dossier-core`, `plan/section.rs`), damit beide nicht auseinanderlaufen.
|
||||
//!
|
||||
//! GRUND: der 2D-Schnitt (`toSection.ts`) ist die Referenz-Implementierung. Er
|
||||
//! sammelt alle Cut-Baender (Wand- und Deckenschichten) als achsparallele
|
||||
//! (u,v)-Rechtecke, taggt jedes mit seiner `Component.joinPriority` und laesst
|
||||
//! dann die STAERKERE Schicht die schwaechere per Rechteck-Subtraktion wegschneiden
|
||||
//! (elementuebergreifend). Der 3D-Rust-Cut (`section.rs`) produziert bereits genau
|
||||
//! dasselbe Format — achsparallele (u,v)-Rechteck-`CutPolygon`s — also laesst sich
|
||||
//! die Dominanz-Zerlegung hier identisch nachbauen, damit der 3D-Live-Schnitt fuer
|
||||
//! Wand+Decken-Schichten EXAKT wie der 2D-Schnitt aussieht.
|
||||
//!
|
||||
//! Die Prioritaet/Identitaet je Band kommt aus `CutPolygon::cut` (`CutBandMeta`,
|
||||
//! aus der TS-Emission `toWalls3d`). Baender ohne `join_priority` (unaufloesbares
|
||||
//! Bauteil) nehmen weder als Basis noch als Cutter teil und bleiben unveraendert.
|
||||
//! Der 3D-Rust-Cut (`section.rs`) liefert achsparallele (u,v)-Rechteck-`CutPolygon`s; Prioritaet und
|
||||
//! Komponente je Band kommen aus `CutPolygon::cut` (`CutBandMeta`, aus der TS-Emission `toWalls3d`).
|
||||
//! Baender ohne `join_priority` (unaufloesbares Bauteil, Dach-Schichten) nehmen weder als Basis noch als
|
||||
//! Cutter teil und bleiben unveraendert. Gerechnet wird in f64, das Ergebnis zurueck in f32.
|
||||
|
||||
use crate::section::CutPolygon;
|
||||
use dossier_hatch::section_dominance::{dominant_bands, rect_of_points, Band, Rect};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Toleranz fuer Ueberlappungs-/Rest-Rechtecke (Meter): Null-/Negativflaechen und
|
||||
/// Rest-Streifen duenner als EPS werden verworfen (Float-Rauschen der (u,v)-Meter).
|
||||
/// Identisch zu `RECT_EPS` in `toSection.ts`.
|
||||
const RECT_EPS: f32 = 1e-6;
|
||||
|
||||
/// Achsparalleles Rechteck in (u, v)-Metern — das Rust-Gegenstueck zu `Rect` in
|
||||
/// `toSection.ts`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Rect {
|
||||
pub u_min: f32,
|
||||
pub u_max: f32,
|
||||
pub v_min: f32,
|
||||
pub v_max: f32,
|
||||
}
|
||||
|
||||
/// Bounding-Box eines (achsparallelen) Cut-Bands als `Rect`, oder `None` bei <3
|
||||
/// Ecken (analog `rectOfBand`).
|
||||
pub fn rect_of_poly(poly: &CutPolygon) -> Option<Rect> {
|
||||
if poly.pts.len() < 3 {
|
||||
return None;
|
||||
}
|
||||
let mut u_min = f32::INFINITY;
|
||||
let mut u_max = f32::NEG_INFINITY;
|
||||
let mut v_min = f32::INFINITY;
|
||||
let mut v_max = f32::NEG_INFINITY;
|
||||
for p in &poly.pts {
|
||||
u_min = u_min.min(p[0]);
|
||||
u_max = u_max.max(p[0]);
|
||||
v_min = v_min.min(p[1]);
|
||||
v_max = v_max.max(p[1]);
|
||||
}
|
||||
Some(Rect {
|
||||
u_min,
|
||||
u_max,
|
||||
v_min,
|
||||
v_max,
|
||||
})
|
||||
}
|
||||
|
||||
/// Rechteck-minus-Rechteck (beide achsparallel): `base` ohne die Ueberlappung mit
|
||||
/// `cutter`, zerlegt in 0–4 disjunkte Rest-Rechtecke (links/rechts ueber die volle
|
||||
/// Hoehe, dann oben/unten im mittleren u-Streifen — ueberlappungsfrei). Kein echter
|
||||
/// Ueberlapp (Flaeche ≤ EPS in u oder v) ⇒ `base` bleibt ganz. Vollstaendige
|
||||
/// Ueberdeckung ⇒ leere Liste. Rest-Streifen duenner als EPS werden verworfen.
|
||||
/// 1:1 aus `toSection.ts::subtractRect`.
|
||||
pub fn subtract_rect(base: Rect, cutter: Rect) -> Vec<Rect> {
|
||||
let o_min_u = base.u_min.max(cutter.u_min);
|
||||
let o_max_u = base.u_max.min(cutter.u_max);
|
||||
let o_min_v = base.v_min.max(cutter.v_min);
|
||||
let o_max_v = base.v_max.min(cutter.v_max);
|
||||
// Kein (nennenswerter) Ueberlapp — `base` unveraendert.
|
||||
if o_max_u - o_min_u <= RECT_EPS || o_max_v - o_min_v <= RECT_EPS {
|
||||
return vec![base];
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut push = |r: Rect| {
|
||||
if r.u_max - r.u_min > RECT_EPS && r.v_max - r.v_min > RECT_EPS {
|
||||
out.push(r);
|
||||
}
|
||||
};
|
||||
// Links / rechts der Ueberlappung, jeweils ueber die volle Basis-Hoehe.
|
||||
push(Rect {
|
||||
u_min: base.u_min,
|
||||
u_max: o_min_u,
|
||||
v_min: base.v_min,
|
||||
v_max: base.v_max,
|
||||
});
|
||||
push(Rect {
|
||||
u_min: o_max_u,
|
||||
u_max: base.u_max,
|
||||
v_min: base.v_min,
|
||||
v_max: base.v_max,
|
||||
});
|
||||
// Unten / oben im mittleren u-Streifen (nur der von der Ueberlappung befreite Rest).
|
||||
push(Rect {
|
||||
u_min: o_min_u,
|
||||
u_max: o_max_u,
|
||||
v_min: base.v_min,
|
||||
v_max: o_min_v,
|
||||
});
|
||||
push(Rect {
|
||||
u_min: o_min_u,
|
||||
u_max: o_max_u,
|
||||
v_min: o_max_v,
|
||||
v_max: base.v_max,
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// `join_priority` eines Bands (aus `CutBandMeta`), oder `None`.
|
||||
fn priority_of(poly: &CutPolygon) -> Option<i32> {
|
||||
poly.cut.as_ref().and_then(|c| c.join_priority)
|
||||
}
|
||||
|
||||
/// `component_id` eines Bands (aus `CutBandMeta`), oder `None`.
|
||||
fn component_of(poly: &CutPolygon) -> Option<&str> {
|
||||
poly.cut.as_ref().and_then(|c| c.component_id.as_deref())
|
||||
}
|
||||
|
||||
/// Erzeugt aus einem Rest-`Rect` ein Cut-Band, das Stil/Referenz/Metadaten von
|
||||
/// `src` erbt (analog `bandFromRect`). Ecken-Reihenfolge wie in `section.rs`
|
||||
/// (CCW-analog, hier v_max oben zuerst — fuer die Fan-Triangulierung irrelevant).
|
||||
/// Erzeugt aus einem Rest-`Rect` ein Cut-Band, das Stil/Referenz/Metadaten von `src` erbt. Ecken wie in
|
||||
/// `section.rs` (v_max oben zuerst — fuer die Fan-Triangulierung irrelevant).
|
||||
fn poly_from_rect(src: &CutPolygon, r: Rect) -> CutPolygon {
|
||||
let (u0, u1, v0, v1) = (r.u_min as f32, r.u_max as f32, r.v_min as f32, r.v_max as f32);
|
||||
CutPolygon {
|
||||
component: src.component,
|
||||
color: src.color,
|
||||
hatch: src.hatch,
|
||||
cut: src.cut.clone(),
|
||||
pts: vec![
|
||||
[r.u_min, r.v_max],
|
||||
[r.u_max, r.v_max],
|
||||
[r.u_max, r.v_min],
|
||||
[r.u_min, r.v_min],
|
||||
],
|
||||
pts: vec![[u0, v1], [u1, v1], [u1, v0], [u0, v0]],
|
||||
}
|
||||
}
|
||||
|
||||
/// Globale Schnitt-Dominanz: fuer jedes Band wird die Rechteck-Ueberlappung ALLER
|
||||
/// Baender mit STRIKT hoeherer `join_priority` subtrahiert (elementuebergreifend —
|
||||
/// z. B. schneidet ein Decken-Beton-Band 100 ein ueberlappendes Wand-Putz-Band 10
|
||||
/// weg). Gleiche Prioritaet schneidet NICHT (koexistiert → durchgehende Flaeche).
|
||||
/// Baender ohne `join_priority` nehmen weder als Basis noch als Cutter teil und
|
||||
/// bleiben unveraendert erhalten. 1:1 aus `toSection.ts::subtractDominantBands`
|
||||
/// (inklusive der abschliessenden MERGE-Phase).
|
||||
/// Globale Schnitt-Dominanz (staerkere `join_priority` schneidet schwaechere weg, gleiche Komponente
|
||||
/// und Prioritaet verschmilzt) — siehe `dossier_hatch::section_dominance`.
|
||||
pub fn subtract_dominant_bands(bands: Vec<CutPolygon>) -> Vec<CutPolygon> {
|
||||
let rects: Vec<Option<Rect>> = bands.iter().map(rect_of_poly).collect();
|
||||
let prios: Vec<Option<i32>> = bands.iter().map(priority_of).collect();
|
||||
let mut out: Vec<CutPolygon> = Vec::new();
|
||||
|
||||
for i in 0..bands.len() {
|
||||
// Nicht-rechteckig oder ohne Prioritaet: unveraendert durchreichen.
|
||||
let (base, prio) = match (rects[i], prios[i]) {
|
||||
(Some(b), Some(p)) => (b, p),
|
||||
_ => {
|
||||
out.push(bands[i].clone());
|
||||
continue;
|
||||
let mut ids: HashMap<&str, u32> = HashMap::new();
|
||||
let inp: Vec<Band> = bands
|
||||
.iter()
|
||||
.map(|b| {
|
||||
let meta = b.cut.as_ref();
|
||||
let comp = meta.and_then(|c| c.component_id.as_deref()).map(|id| {
|
||||
let n = ids.len() as u32;
|
||||
*ids.entry(id).or_insert(n)
|
||||
});
|
||||
Band {
|
||||
rect: rect_of_points(b.pts.iter().map(|p| (p[0] as f64, p[1] as f64))),
|
||||
prio: meta.and_then(|c| c.join_priority).map(f64::from),
|
||||
comp,
|
||||
}
|
||||
};
|
||||
let mut pieces = vec![base];
|
||||
for j in 0..bands.len() {
|
||||
if pieces.is_empty() {
|
||||
break;
|
||||
}
|
||||
if j == i {
|
||||
continue;
|
||||
}
|
||||
let cutter = match (rects[j], prios[j]) {
|
||||
(Some(c), Some(op)) if op > prio => c,
|
||||
_ => continue,
|
||||
};
|
||||
let mut next = Vec::new();
|
||||
for p in &pieces {
|
||||
next.extend(subtract_rect(*p, cutter));
|
||||
}
|
||||
pieces = next;
|
||||
}
|
||||
for p in pieces {
|
||||
out.push(poly_from_rect(&bands[i], p));
|
||||
}
|
||||
}
|
||||
merge_same_component_bands(out)
|
||||
}
|
||||
|
||||
/// Union zweier achsparalleler Rechtecke, ABER NUR wenn das Ergebnis EXAKT wieder
|
||||
/// ein Rechteck ist — sonst `None`. Enthaltensein oder Achs-Deckung + Beruehrung
|
||||
/// liefern eine rechteckige Union; Teil-/Eck-Ueberlappungen (L-Form) und Baender
|
||||
/// mit Spalt liefern `None`. 1:1 aus `toSection.ts::rectUnionIfRect`.
|
||||
fn rect_union_if_rect(a: Rect, b: Rect) -> Option<Rect> {
|
||||
const E: f32 = RECT_EPS;
|
||||
// Enthaltensein (inkl. Deckungsgleichheit): Union = umschliessendes Rechteck.
|
||||
let a_in_b = b.u_min - a.u_min <= E
|
||||
&& a.u_max - b.u_max <= E
|
||||
&& b.v_min - a.v_min <= E
|
||||
&& a.v_max - b.v_max <= E;
|
||||
if a_in_b {
|
||||
return Some(b);
|
||||
}
|
||||
let b_in_a = a.u_min - b.u_min <= E
|
||||
&& b.u_max - a.u_max <= E
|
||||
&& a.v_min - b.v_min <= E
|
||||
&& b.v_max - a.v_max <= E;
|
||||
if b_in_a {
|
||||
return Some(a);
|
||||
}
|
||||
let u_aligned = (a.u_min - b.u_min).abs() <= E && (a.u_max - b.u_max).abs() <= E;
|
||||
let v_aligned = (a.v_min - b.v_min).abs() <= E && (a.v_max - b.v_max).abs() <= E;
|
||||
// Beruehrung/Ueberlappung in der jeweils ANDEREN Achse (kein Spalt groesser EPS).
|
||||
let v_touch = a.v_max >= b.v_min - E && b.v_max >= a.v_min - E;
|
||||
let u_touch = a.u_max >= b.u_min - E && b.u_max >= a.u_min - E;
|
||||
if (u_aligned && v_touch) || (v_aligned && u_touch) {
|
||||
return Some(Rect {
|
||||
u_min: a.u_min.min(b.u_min),
|
||||
u_max: a.u_max.max(b.u_max),
|
||||
v_min: a.v_min.min(b.v_min),
|
||||
v_max: a.v_max.max(b.v_max),
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// MERGE-Phase der Schnitt-Dominanz: Baender mit GLEICHER `component_id` UND
|
||||
/// gleicher `join_priority`, deren achsparallele Rechtecke sich beruehren/ueberlappen
|
||||
/// und eine rechteckige Union bilden (`rect_union_if_rect`), werden zu EINEM Band
|
||||
/// zusammengefasst — so entsteht an der Beruehrkante keine innere Trennlinie durch
|
||||
/// gleichartiges Material. Baender ohne `component_id`/`join_priority` oder ohne
|
||||
/// Rechteck-Form nehmen nicht teil. Iterativ bis zum Fixpunkt. 1:1 aus
|
||||
/// `toSection.ts::mergeSameComponentBands`.
|
||||
fn merge_same_component_bands(bands: Vec<CutPolygon>) -> Vec<CutPolygon> {
|
||||
let mut out = bands;
|
||||
let mut merged = true;
|
||||
while merged {
|
||||
merged = false;
|
||||
'outer: for i in 0..out.len() {
|
||||
let (ci, pi) = match (component_of(&out[i]), priority_of(&out[i])) {
|
||||
(Some(c), Some(p)) => (c.to_string(), p),
|
||||
_ => continue,
|
||||
};
|
||||
let ri = match rect_of_poly(&out[i]) {
|
||||
Some(r) => r,
|
||||
None => continue,
|
||||
};
|
||||
for j in (i + 1)..out.len() {
|
||||
if component_of(&out[j]) != Some(ci.as_str()) || priority_of(&out[j]) != Some(pi) {
|
||||
continue;
|
||||
}
|
||||
let rj = match rect_of_poly(&out[j]) {
|
||||
Some(r) => r,
|
||||
None => continue,
|
||||
};
|
||||
if let Some(u) = rect_union_if_rect(ri, rj) {
|
||||
out[i] = poly_from_rect(&out[i], u);
|
||||
out.remove(j);
|
||||
merged = true; // Fixpunkt-Neustart
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
})
|
||||
.collect();
|
||||
dominant_bands(&inp)
|
||||
.into_iter()
|
||||
.map(|piece| match piece.rect {
|
||||
Some(r) => poly_from_rect(&bands[piece.src], r),
|
||||
None => bands[piece.src].clone(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -295,8 +90,22 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn rect_of(p: &CutPolygon) -> Rect {
|
||||
rect_of_poly(p).unwrap()
|
||||
/// Umgebendes Rechteck eines Bands (f32, wie die Ecken).
|
||||
#[derive(Debug)]
|
||||
struct R {
|
||||
u_min: f32,
|
||||
u_max: f32,
|
||||
v_min: f32,
|
||||
v_max: f32,
|
||||
}
|
||||
|
||||
fn rect_of(p: &CutPolygon) -> R {
|
||||
let r = rect_of_points(p.pts.iter().map(|q| (q[0] as f64, q[1] as f64))).unwrap();
|
||||
R { u_min: r.u_min as f32, u_max: r.u_max as f32, v_min: r.v_min as f32, v_max: r.v_max as f32 }
|
||||
}
|
||||
|
||||
fn component_of(poly: &CutPolygon) -> Option<&str> {
|
||||
poly.cut.as_ref().and_then(|c| c.component_id.as_deref())
|
||||
}
|
||||
|
||||
fn area(bands: &[CutPolygon]) -> f32 {
|
||||
@@ -313,56 +122,7 @@ mod tests {
|
||||
(a - b).abs() < 1e-5
|
||||
}
|
||||
|
||||
// --- subtractRect (Referenz: toSection.boolean.test.ts) ------------------
|
||||
|
||||
#[test]
|
||||
fn subtract_rect_kein_overlap_base_unveraendert() {
|
||||
let base = Rect { u_min: 0.0, u_max: 1.0, v_min: 0.0, v_max: 1.0 };
|
||||
let cut = Rect { u_min: 2.0, u_max: 3.0, v_min: 2.0, v_max: 3.0 };
|
||||
assert_eq!(subtract_rect(base, cut), vec![base]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtract_rect_kantenberuehrung_base_unveraendert() {
|
||||
let base = Rect { u_min: 0.0, u_max: 1.0, v_min: 0.0, v_max: 1.0 };
|
||||
let cut = Rect { u_min: 1.0, u_max: 2.0, v_min: 0.0, v_max: 1.0 };
|
||||
assert_eq!(subtract_rect(base, cut), vec![base]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtract_rect_cutter_enthaelt_base_leer() {
|
||||
let base = Rect { u_min: 1.0, u_max: 2.0, v_min: 1.0, v_max: 2.0 };
|
||||
let cut = Rect { u_min: 0.0, u_max: 3.0, v_min: 0.0, v_max: 3.0 };
|
||||
assert!(subtract_rect(base, cut).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtract_rect_cutter_mittig_vier_reste() {
|
||||
let base = Rect { u_min: 0.0, u_max: 3.0, v_min: 0.0, v_max: 3.0 };
|
||||
let cut = Rect { u_min: 1.0, u_max: 2.0, v_min: 1.0, v_max: 2.0 };
|
||||
let rest = subtract_rect(base, cut);
|
||||
assert_eq!(rest.len(), 4);
|
||||
let a: f32 = rest
|
||||
.iter()
|
||||
.map(|r| (r.u_max - r.u_min) * (r.v_max - r.v_min))
|
||||
.sum();
|
||||
assert!(approx(a, 8.0), "9 (base) - 1 (overlap) = 8, ist {a}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subtract_rect_eck_ueberlappung_zwei_reste() {
|
||||
// cutter deckt die obere-rechte Ecke von base: overlap [1,2]x[1,2].
|
||||
let base = Rect { u_min: 0.0, u_max: 2.0, v_min: 0.0, v_max: 2.0 };
|
||||
let cut = Rect { u_min: 1.0, u_max: 3.0, v_min: 1.0, v_max: 3.0 };
|
||||
let rest = subtract_rect(base, cut);
|
||||
let a: f32 = rest
|
||||
.iter()
|
||||
.map(|r| (r.u_max - r.u_min) * (r.v_max - r.v_min))
|
||||
.sum();
|
||||
assert!(approx(a, 3.0), "4 - 1 = 3, ist {a}");
|
||||
}
|
||||
|
||||
// --- subtractDominantBands: die geometrische Kernaussage ------------------
|
||||
// --- Adapter Ende-zu-Ende (Rechnung: dossier_hatch::section_dominance) ----
|
||||
|
||||
#[test]
|
||||
fn decke_100_schneidet_wand_50_im_ueberlappungs_z_intervall_weg() {
|
||||
@@ -513,4 +273,18 @@ mod tests {
|
||||
let r = rect_of(&out[0]);
|
||||
assert!(approx(r.v_min, 0.0) && approx(r.v_max, 4.0), "verschmolzen {r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dach_ohne_prioritaet_bleibt_polygon() {
|
||||
// Dach-Schicht (schiefes Trapez, ohne join_priority) wird unveraendert durchgereicht, auch wenn
|
||||
// ein staerkeres Band ueberlappt — keine Ersetzung durch die Bounding-Box.
|
||||
let mut dach = band(0.0, 2.0, 0.0, 1.0, 0, Some("dach"));
|
||||
dach.cut = None;
|
||||
dach.pts = vec![[0.0, 0.0], [2.0, 0.0], [1.5, 1.0], [0.5, 1.0]];
|
||||
let pts = dach.pts.clone();
|
||||
let beton = band(0.0, 2.0, 0.0, 1.0, 100, Some("beton"));
|
||||
let out = subtract_dominant_bands(vec![dach, beton]);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!(out[0].pts, pts);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user