diff --git a/TODO.md b/TODO.md index 3fe4dec6..f58b068a 100644 --- a/TODO.md +++ b/TODO.md @@ -28,7 +28,7 @@ truck (`trucksolid`) bleibt eigenes WASM-Modul (Panic-Isolation), wird vom Kern langsamer). Schatten, Baurecht (exakter Puffer statt Kapsel), Aussenbemassung, Kennzahlen in Rust; die `checkRoomProgram` bleibt vorerst TS (SIA-416-Zuordnung: seit [K2] `room_area.rs`). `bulge.ts` ist Fassade über den Rust-Kern (`bulge_polyline_points` für ganze Polylinien; `hasRealBulge`/`pointOnCircle` bleiben TS, - Einzeiler). Offen: Triangulierung, Straight Skeleton [B2]. · M + Einzeiler). Delaunay-Triangulierung (`delaunay.rs`, Port von delaunator inkl. exaktem `orient2d`; bitgleich zu JS über Zufalls-/Raster-/kozirkulare/fast-kollineare Punkte; `delaunator` aus den Abhängigkeiten entfernt, 28.10.): Gelände-TIN und Kontext-Gebäude. Offen: Straight Skeleton [B2]. · M - ✅ **Mac-Test 27.10.** (Maintainer): App startet mit dem Rust-Kern, Darstellung sieht gut aus — Stand `5b72069` (K0–K2, render2d binär); zweiter Test mit GPU-Kompilierung [K3b] (`7a8af4f`) ebenfalls bestanden. - ✅ **[K2] Geometrie-Ableitungen.** (27.10.) Dachgeometrie in Rust (`roof.rs`, `roof_skeleton.rs`; diff --git a/package-lock.json b/package-lock.json index dfe79507..94aa3826 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "@tauri-apps/plugin-process": "^2.3.1", "@tauri-apps/plugin-shell": "^2.3.5", "@tauri-apps/plugin-updater": "^2.10.1", - "delaunator": "^5.1.0", "dxf-parser": "^1.1.2", "jspdf": "^4.2.1", "jszip": "^3.10.1", @@ -2978,15 +2977,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/delaunator": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/delaunator/-/delaunator-5.1.0.tgz", - "integrity": "sha512-AGrQ4QSgssa1NGmWmLPqN5NY2KajF5MqxetNEO+o0n3ZwZZeTmt7bBnvzHWrmkZFxGgr4HdyFgelzgi06otLuQ==", - "license": "ISC", - "dependencies": { - "robust-predicates": "^3.0.2" - } - }, "node_modules/detect-libc": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", @@ -5194,12 +5184,6 @@ "node": ">= 0.8.15" } }, - "node_modules/robust-predicates": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/robust-predicates/-/robust-predicates-3.0.3.tgz", - "integrity": "sha512-NS3levdsRIUOmiJ8FZWCP7LG3QpJyrs/TE0Zpf1yvZu8cAJJ6QMW92H1c7kWpdIHo8RvmLxN/o2JXTKHp74lUA==", - "license": "Unlicense" - }, "node_modules/rolldown": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.1.4.tgz", diff --git a/package.json b/package.json index cba3e081..232b60fd 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "@tauri-apps/plugin-process": "^2.3.1", "@tauri-apps/plugin-shell": "^2.3.5", "@tauri-apps/plugin-updater": "^2.10.1", - "delaunator": "^5.1.0", "dxf-parser": "^1.1.2", "jspdf": "^4.2.1", "jszip": "^3.10.1", diff --git a/src-tauri/core/src/delaunay.rs b/src-tauri/core/src/delaunay.rs new file mode 100644 index 00000000..0345ffc5 --- /dev/null +++ b/src-tauri/core/src/delaunay.rs @@ -0,0 +1,538 @@ +//! Delaunay-Triangulierung ([K1]): Port von `delaunator` 5.1.0 (Volodymyr Agafonkin / Mapbox, ISC-Lizenz, +//! https://github.com/mapbox/delaunator) samt exaktem +//! `orient2d` (robust-predicates). Dieselbe Reihenfolge der Schritte wie in JS, damit Dreiecke und +//! deren Indizes bitgleich herauskommen — auch bei kozirkulären Punkten (Raster). Verwendet vom +//! Gelände-TIN (`model/terrain.ts`) und den Kontext-Gebäuden (`io/geoContext.ts`). +//! +//! `triangulate(coords)` mit `coords = [x0, y0, x1, y1 …]` → Dreiecks-Indizes (je drei, gegen den +//! Uhrzeigersinn); leer bei < 3 Punkten, nur kollinearen Punkten oder nicht endlichen Werten. + +const EPSILON: f64 = 1.0 / 4503599627370496.0; // 2^-52 +const EDGE_STACK_LEN: usize = 512; + +// ── exaktes orient2d ─────────────────────────────────────────────────────── + +fn two_sum(a: f64, b: f64) -> (f64, f64) { + let x = a + b; + let bv = x - a; + let av = x - bv; + (x, (a - av) + (b - bv)) +} + +fn two_product(a: f64, b: f64) -> (f64, f64) { + let x = a * b; + (x, a.mul_add(b, -x)) +} + +/// Summe einer Expansion (aufsteigend, nicht überlappend) und eines Doubles. +fn grow(e: &[f64], b: f64) -> Vec { + let mut out = Vec::with_capacity(e.len() + 1); + let mut q = b; + for &x in e { + let (s, t) = two_sum(q, x); + q = s; + if t != 0.0 { + out.push(t); + } + } + if q != 0.0 || out.is_empty() { + out.push(q); + } + out +} + +fn scale(e: &[f64], b: f64) -> Vec { + let mut out = Vec::with_capacity(2 * e.len()); + let (mut q, h) = two_product(e[0], b); + if h != 0.0 { + out.push(h); + } + for &x in &e[1..] { + let (p1, p0) = two_product(x, b); + let (sum, h) = two_sum(q, p0); + if h != 0.0 { + out.push(h); + } + let (q2, h2) = two_sum(p1, sum); + q = q2; + if h2 != 0.0 { + out.push(h2); + } + } + if q != 0.0 || out.is_empty() { + out.push(q); + } + out +} + +fn sum(e: &[f64], f: &[f64]) -> Vec { + let mut out = e.to_vec(); + for &x in f { + out = grow(&out, x); + } + out +} + +fn estimate_sign(e: &[f64]) -> f64 { + for &x in e.iter().rev() { + if x != 0.0 { + return x; + } + } + 0.0 +} + +/// Wie `orient2d` aus `robust-predicates`: Vorzeichen exakt (negativ, wenn a, b, c gegen den +/// Uhrzeigersinn liegen — Konvention dieses Pakets), Betrag nur die schnelle Näherung. +pub fn orient2d(ax: f64, ay: f64, bx: f64, by: f64, cx: f64, cy: f64) -> f64 { + const EPS: f64 = 1.1102230246251565e-16; // 2^-53 + const CCW_ERR_BOUND_A: f64 = (3.0 + 16.0 * EPS) * EPS; + let detleft = (ay - cy) * (bx - cx); + let detright = (ax - cx) * (by - cy); + let det = detleft - detright; + let detsum = (detleft + detright).abs(); + if det.abs() >= CCW_ERR_BOUND_A * detsum { + return det; + } + // exakt: (ay-cy)(bx-cx) - (ax-cx)(by-cy); jede Differenz als 2-Term-Expansion + let (acx, acx_t) = two_sum(ax, -cx); + let (bcx, bcx_t) = two_sum(bx, -cx); + let (acy, acy_t) = two_sum(ay, -cy); + let (bcy, bcy_t) = two_sum(by, -cy); + let prod = |a: &[f64; 2], b: &[f64; 2]| -> Vec { sum(&scale(a, b[0]), &scale(a, b[1])) }; + let left = prod(&[acy_t, acy], &[bcx_t, bcx]); + let right = prod(&[acx_t, acx], &[bcy_t, bcy]); + let neg: Vec = right.iter().map(|v| -v).collect(); + estimate_sign(&sum(&left, &neg)) +} + +// ── Delaunator ───────────────────────────────────────────────────────────── + +fn dist(ax: f64, ay: f64, bx: f64, by: f64) -> f64 { + let dx = ax - bx; + let dy = ay - by; + dx * dx + dy * dy +} + +fn in_circle(ax: f64, ay: f64, bx: f64, by: f64, cx: f64, cy: f64, px: f64, py: f64) -> bool { + let dx = ax - px; + let dy = ay - py; + let ex = bx - px; + let ey = by - py; + let fx = cx - px; + let fy = cy - py; + let ap = dx * dx + dy * dy; + let bp = ex * ex + ey * ey; + let cp = fx * fx + fy * fy; + dx * (ey * cp - bp * fy) - dy * (ex * cp - bp * fx) + ap * (ex * fy - ey * fx) < 0.0 +} + +fn circumradius(ax: f64, ay: f64, bx: f64, by: f64, cx: f64, cy: f64) -> f64 { + let dx = bx - ax; + let dy = by - ay; + let ex = cx - ax; + let ey = cy - ay; + let bl = dx * dx + dy * dy; + let cl = ex * ex + ey * ey; + let d = 0.5 / (dx * ey - dy * ex); + let x = (ey * bl - dy * cl) * d; + let y = (dx * cl - ex * bl) * d; + x * x + y * y +} + +fn circumcenter(ax: f64, ay: f64, bx: f64, by: f64, cx: f64, cy: f64) -> (f64, f64) { + let dx = bx - ax; + let dy = by - ay; + let ex = cx - ax; + let ey = cy - ay; + let bl = dx * dx + dy * dy; + let cl = ex * ex + ey * ey; + let d = 0.5 / (dx * ey - dy * ex); + (ax + (ey * bl - dy * cl) * d, ay + (dx * cl - ex * bl) * d) +} + +fn pseudo_angle(dx: f64, dy: f64) -> f64 { + let p = dx / (dx.abs() + dy.abs()); + (if dy > 0.0 { 3.0 - p } else { 1.0 + p }) / 4.0 +} + +fn quicksort(ids: &mut [u32], dists: &[f64], left: isize, right: isize) { + if right - left <= 20 { + for i in left + 1..=right { + let temp = ids[i as usize]; + let temp_dist = dists[temp as usize]; + let mut j = i - 1; + while j >= left && dists[ids[j as usize] as usize] > temp_dist { + ids[(j + 1) as usize] = ids[j as usize]; + j -= 1; + } + ids[(j + 1) as usize] = temp; + } + } else { + let median = (left + right) >> 1; + let mut i = left + 1; + let mut j = right; + ids.swap(median as usize, i as usize); + if dists[ids[left as usize] as usize] > dists[ids[right as usize] as usize] { + ids.swap(left as usize, right as usize); + } + if dists[ids[i as usize] as usize] > dists[ids[right as usize] as usize] { + ids.swap(i as usize, right as usize); + } + if dists[ids[left as usize] as usize] > dists[ids[i as usize] as usize] { + ids.swap(left as usize, i as usize); + } + let temp = ids[i as usize]; + let temp_dist = dists[temp as usize]; + loop { + loop { + i += 1; + if !(dists[ids[i as usize] as usize] < temp_dist) { + break; + } + } + loop { + j -= 1; + if !(dists[ids[j as usize] as usize] > temp_dist) { + break; + } + } + if j < i { + break; + } + ids.swap(i as usize, j as usize); + } + ids[(left + 1) as usize] = ids[j as usize]; + ids[j as usize] = temp; + if right - i + 1 >= j - left { + quicksort(ids, dists, i, right); + quicksort(ids, dists, left, j - 1); + } else { + quicksort(ids, dists, left, j - 1); + quicksort(ids, dists, i, right); + } + } +} + +struct State<'a> { + coords: &'a [f64], + triangles: Vec, + halfedges: Vec, + hull_prev: Vec, + hull_tri: Vec, + hull_start: usize, + edge_stack: [u32; EDGE_STACK_LEN], +} + +impl State<'_> { + fn link(&mut self, a: usize, b: i32) { + self.halfedges[a] = b; + if b != -1 { + self.halfedges[b as usize] = a as i32; + } + } + + fn add_triangle(&mut self, i0: u32, i1: u32, i2: u32, a: i32, b: i32, c: i32) -> usize { + let t = self.triangles.len(); + self.triangles.extend([i0, i1, i2]); + self.halfedges.extend([-1, -1, -1]); + self.link(t, a); + self.link(t + 1, b); + self.link(t + 2, c); + t + } + + fn legalize(&mut self, a_in: usize) -> usize { + let mut a = a_in; + let mut i = 0usize; + let mut ar; + loop { + let b = self.halfedges[a]; + let a0 = a - a % 3; + ar = a0 + (a + 2) % 3; + if b == -1 { + if i == 0 { + break; + } + i -= 1; + a = self.edge_stack[i] as usize; + continue; + } + let b = b as usize; + let b0 = b - b % 3; + let al = a0 + (a + 1) % 3; + let bl = b0 + (b + 2) % 3; + let p0 = self.triangles[ar] as usize; + let pr = self.triangles[a] as usize; + let pl = self.triangles[al] as usize; + let p1 = self.triangles[bl] as usize; + let c = self.coords; + let illegal = in_circle( + c[2 * p0], c[2 * p0 + 1], c[2 * pr], c[2 * pr + 1], c[2 * pl], c[2 * pl + 1], c[2 * p1], c[2 * p1 + 1], + ); + if illegal { + self.triangles[a] = p1 as u32; + self.triangles[b] = p0 as u32; + let hbl = self.halfedges[bl]; + if hbl == -1 { + let mut e = self.hull_start; + loop { + if self.hull_tri[e] as usize == bl { + self.hull_tri[e] = a as u32; + break; + } + e = self.hull_prev[e] as usize; + if e == self.hull_start { + break; + } + } + } + self.link(a, hbl); + let har = self.halfedges[ar]; + self.link(b, har); + self.link(ar, bl as i32); + let br = b0 + (b + 1) % 3; + if i < EDGE_STACK_LEN { + self.edge_stack[i] = br as u32; + i += 1; + } + } else { + if i == 0 { + break; + } + i -= 1; + a = self.edge_stack[i] as usize; + } + } + ar + } +} + +pub fn triangulate(coords: &[f64]) -> Vec { + let n = coords.len() >> 1; + if n < 3 || coords.len() != 2 * n || coords.iter().any(|v| !v.is_finite()) { + return Vec::new(); + } + let max_triangles = if n * 2 >= 5 { 2 * n - 5 } else { 0 }; + let hash_size = (n as f64).sqrt().ceil() as usize; + let mut hull_next = vec![0u32; n]; + let mut hull_hash = vec![-1i32; hash_size]; + let mut st = State { + coords, + triangles: Vec::with_capacity(max_triangles * 3), + halfedges: Vec::with_capacity(max_triangles * 3), + hull_prev: vec![0u32; n], + hull_tri: vec![0u32; n], + hull_start: 0, + edge_stack: [0; EDGE_STACK_LEN], + }; + let mut ids: Vec = (0..n as u32).collect(); + let mut dists = vec![0.0f64; n]; + + let (mut min_x, mut min_y, mut max_x, mut max_y) = (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY); + for i in 0..n { + let x = coords[2 * i]; + let y = coords[2 * i + 1]; + if x < min_x { + min_x = x; + } + if y < min_y { + min_y = y; + } + if x > max_x { + max_x = x; + } + if y > max_y { + max_y = y; + } + } + let cx = (min_x + max_x) / 2.0; + let cy = (min_y + max_y) / 2.0; + + let (mut i0, mut i1, mut i2) = (0usize, 0usize, 0usize); + let mut min_dist = f64::INFINITY; + for i in 0..n { + let d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]); + if d < min_dist { + i0 = i; + min_dist = d; + } + } + let i0x = coords[2 * i0]; + let i0y = coords[2 * i0 + 1]; + let mut min_dist = f64::INFINITY; + for i in 0..n { + if i == i0 { + continue; + } + let d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]); + if d < min_dist && d > 0.0 { + i1 = i; + min_dist = d; + } + } + let mut i1x = coords[2 * i1]; + let mut i1y = coords[2 * i1 + 1]; + let mut min_radius = f64::INFINITY; + for i in 0..n { + if i == i0 || i == i1 { + continue; + } + let r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]); + if r < min_radius { + i2 = i; + min_radius = r; + } + } + let mut i2x = coords[2 * i2]; + let mut i2y = coords[2 * i2 + 1]; + if min_radius == f64::INFINITY { + return Vec::new(); // alle Punkte kollinear: keine Dreiecke + } + if orient2d(i0x, i0y, i1x, i1y, i2x, i2y) < 0.0 { + std::mem::swap(&mut i1, &mut i2); + std::mem::swap(&mut i1x, &mut i2x); + std::mem::swap(&mut i1y, &mut i2y); + } + let (ccx, ccy) = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y); + for i in 0..n { + dists[i] = dist(coords[2 * i], coords[2 * i + 1], ccx, ccy); + } + quicksort(&mut ids, &dists, 0, n as isize - 1); + + let hash_key = |x: f64, y: f64| -> usize { + let v = (pseudo_angle(x - ccx, y - ccy) * hash_size as f64).floor(); + if v.is_nan() { + 0 + } else { + (v as i64).rem_euclid(hash_size as i64) as usize + } + }; + + st.hull_start = i0; + hull_next[i0] = i1 as u32; + st.hull_prev[i2] = i1 as u32; + hull_next[i1] = i2 as u32; + st.hull_prev[i0] = i2 as u32; + hull_next[i2] = i0 as u32; + st.hull_prev[i1] = i0 as u32; + st.hull_tri[i0] = 0; + st.hull_tri[i1] = 1; + st.hull_tri[i2] = 2; + hull_hash[hash_key(i0x, i0y)] = i0 as i32; + hull_hash[hash_key(i1x, i1y)] = i1 as i32; + hull_hash[hash_key(i2x, i2y)] = i2 as i32; + st.add_triangle(i0 as u32, i1 as u32, i2 as u32, -1, -1, -1); + + let (mut xp, mut yp) = (0.0f64, 0.0f64); + for k in 0..n { + let i = ids[k] as usize; + let x = coords[2 * i]; + let y = coords[2 * i + 1]; + if k > 0 && (x - xp).abs() <= EPSILON && (y - yp).abs() <= EPSILON { + continue; + } + xp = x; + yp = y; + if i == i0 || i == i1 || i == i2 { + continue; + } + let mut start = 0usize; + let key = hash_key(x, y); + for j in 0..hash_size { + let s = hull_hash[(key + j) % hash_size]; + start = if s < 0 { usize::MAX } else { s as usize }; + if s != -1 && start != hull_next[start] as usize { + break; + } + } + if start == usize::MAX { + continue; + } + start = st.hull_prev[start] as usize; + let mut e = start; + let mut found = true; + loop { + let q = hull_next[e] as usize; + if orient2d(x, y, coords[2 * e], coords[2 * e + 1], coords[2 * q], coords[2 * q + 1]) >= 0.0 { + e = q; + if e == start { + found = false; + break; + } + } else { + break; + } + } + if !found { + continue; + } + let mut t = st.add_triangle(e as u32, i as u32, hull_next[e], -1, -1, st.hull_tri[e] as i32); + st.hull_tri[i] = st.legalize(t + 2) as u32; + st.hull_tri[e] = t as u32; + + let mut nn = hull_next[e] as usize; + loop { + let q = hull_next[nn] as usize; + if orient2d(x, y, coords[2 * nn], coords[2 * nn + 1], coords[2 * q], coords[2 * q + 1]) < 0.0 { + t = st.add_triangle(nn as u32, i as u32, q as u32, st.hull_tri[i] as i32, -1, st.hull_tri[nn] as i32); + st.hull_tri[i] = st.legalize(t + 2) as u32; + hull_next[nn] = nn as u32; + nn = q; + } else { + break; + } + } + if e == start { + loop { + let q = st.hull_prev[e] as usize; + if orient2d(x, y, coords[2 * q], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1]) < 0.0 { + t = st.add_triangle(q as u32, i as u32, e as u32, -1, st.hull_tri[e] as i32, st.hull_tri[q] as i32); + st.legalize(t + 2); + st.hull_tri[q] = t as u32; + hull_next[e] = e as u32; + e = q; + } else { + break; + } + } + } + st.hull_start = e; + st.hull_prev[i] = e as u32; + hull_next[e] = i as u32; + st.hull_prev[nn] = i as u32; + hull_next[i] = nn as u32; + hull_hash[hash_key(x, y)] = i as i32; + hull_hash[hash_key(coords[2 * e], coords[2 * e + 1])] = e as i32; + } + st.triangles +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn quadrat_ergibt_zwei_dreiecke() { + let t = triangulate(&[0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]); + assert_eq!(t.len(), 6); + } + + #[test] + fn kollinear_und_zu_wenig_sind_leer() { + assert!(triangulate(&[0.0, 0.0, 1.0, 1.0, 2.0, 2.0]).is_empty()); + assert!(triangulate(&[0.0, 0.0, 1.0, 1.0]).is_empty()); + assert!(triangulate(&[0.0, f64::NAN, 1.0, 1.0, 2.0, 5.0]).is_empty()); + } + + #[test] + fn orient_ist_exakt() { + // fast kollinear: nur mit exakter Arithmetik korrekt + let a = (0.5, 0.5); + let b = (12.0, 12.0); + let c = (24.0, 24.0); + assert_eq!(orient2d(a.0, a.1, b.0, b.1, c.0, c.1), 0.0); + assert!(orient2d(0.0, 0.0, 1.0, 0.0, 0.0, 1.0) < 0.0); + assert!(orient2d(0.0, 0.0, 0.0, 1.0, 1.0, 0.0) > 0.0); + } +} diff --git a/src-tauri/core/src/lib.rs b/src-tauri/core/src/lib.rs index 58f1fbb7..3a0b0adc 100644 --- a/src-tauri/core/src/lib.rs +++ b/src-tauri/core/src/lib.rs @@ -37,6 +37,7 @@ pub mod plan_stairs; pub mod plan_walls; pub mod opening_geom; pub mod plan_openings; +pub mod delaunay; pub mod mesh_section; pub mod plan_elevation; pub mod plan_misc; diff --git a/src-tauri/core/src/web/delaunay.rs b/src-tauri/core/src/web/delaunay.rs new file mode 100644 index 00000000..b936ed86 --- /dev/null +++ b/src-tauri/core/src/web/delaunay.rs @@ -0,0 +1,9 @@ +//! Bindung der Delaunay-Triangulierung (`crate::delaunay`). + +use wasm_bindgen::prelude::wasm_bindgen; + +/// Delaunay-Dreiecke zu `[x0, y0, x1, y1 …]`; Indizes je drei; leer bei < 3 oder kollinearen Punkten. +#[wasm_bindgen] +pub fn delaunay_flat(coords: &[f64]) -> Vec { + crate::delaunay::triangulate(coords).into_iter().map(f64::from).collect() +} diff --git a/src-tauri/core/src/web/mod.rs b/src-tauri/core/src/web/mod.rs index 4a75e16a..db426be7 100644 --- a/src-tauri/core/src/web/mod.rs +++ b/src-tauri/core/src/web/mod.rs @@ -28,6 +28,7 @@ mod gpu_compile; mod plan_stairs; mod plan_walls; mod plan_openings; +mod delaunay; mod mesh_section; mod plan_elevation; mod plan_misc; diff --git a/src/io/geoContext.ts b/src/io/geoContext.ts index a16e839f..2059d353 100644 --- a/src/io/geoContext.ts +++ b/src/io/geoContext.ts @@ -8,7 +8,7 @@ // Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md). import { allowHost } from "../net/net"; -import Delaunator from "delaunator"; +import { core } from "../engine/coreWasm"; import type { ContextObject, Contour, ImportedMesh } from "../model/types"; import { GEO_CATEGORY_DEFS } from "../model/contextCategories"; @@ -228,12 +228,7 @@ function triangulateRing(ring: { x: number; y: number }[]): number[] { coords[i * 2] = ring[i].x; coords[i * 2 + 1] = ring[i].y; } - let tri: Uint32Array; - try { - tri = new Delaunator(coords).triangles; - } catch { - return fanTriangulate(n); - } + const tri = core().delaunay_flat(coords); const out: number[] = []; for (let t = 0; t < tri.length; t += 3) { const a = tri[t], b = tri[t + 1], c = tri[t + 2]; diff --git a/src/model/terrain.ts b/src/model/terrain.ts index a1082226..60ddd585 100644 --- a/src/model/terrain.ts +++ b/src/model/terrain.ts @@ -8,7 +8,7 @@ // // Gelände ist NICHT semantisch — es ist Kontext-Geometrie (Project.context). -import Delaunator from "delaunator"; +import { core } from "../engine/coreWasm"; import type { Contour, TerrainMesh } from "./types"; import { TERRAIN_CATEGORY } from "./contextCategories"; @@ -25,7 +25,7 @@ export const TERRAIN_LAYER = "Terrain"; * z = Kontur-Höhe. Es wird KEIN zusätzliches Resampling betrieben — die * Kontur-Vertices selbst sind die Breaklines. * 2. Delaunay: 2D-Triangulation über die (x,y)-Projektion aller Vertices - * (Delaunator). Die Z-Höhe je Vertex bleibt erhalten → echtes Gelände-TIN. + * (Rust-Kern, `delaunay.rs`). Die Z-Höhe je Vertex bleibt erhalten → echtes Gelände-TIN. * 3. Degenerate-Filter: Dreiecke mit (nahezu) null Fläche in der (x,y)-Ebene * bzw. mit nicht-endlichen Koordinaten werden verworfen. * @@ -59,21 +59,16 @@ export function generateTerrainFromContours(contours: Contour[]): TerrainMesh { return { id, type: "terrainMesh", name, categoryCode: TERRAIN_CATEGORY.code, positions: [], indices: [] }; } - // 2. Delaunay über die (x,y)-Projektion. Delaunator erwartet ein flaches + // 2. Delaunay über die (x,y)-Projektion. der Kern erwartet ein flaches // [x0,y0, x1,y1, …]-Koordinatenarray. const coords2d = new Float64Array(xs.length * 2); for (let i = 0; i < xs.length; i++) { coords2d[i * 2] = xs[i]; coords2d[i * 2 + 1] = ys[i]; } - // Bei kollinearen Punkten liefert Delaunator eine leere Triangulation — das ist - // korrekt (kein Crash). Trotzdem schützen wir gegen einen möglichen Wurf. - let triangles: Uint32Array; - try { - triangles = new Delaunator(coords2d).triangles; - } catch { - return { id, type: "terrainMesh", name, categoryCode: TERRAIN_CATEGORY.code, positions: [], indices: [] }; - } + // Der Kern (`delaunay.rs`, Port von delaunator) liefert bei kollinearen Punkten eine leere + // Triangulation — das ist korrekt. + const triangles = core().delaunay_flat(coords2d); // 3. positions (x,y,z) aufbauen; Z aus zs je Vertex. const positions: number[] = new Array(xs.length * 3);