diff --git a/src-tauri/trucksolid/src/boolean.rs b/src-tauri/trucksolid/src/boolean.rs index bf790d89..209cb631 100644 --- a/src-tauri/trucksolid/src/boolean.rs +++ b/src-tauri/trucksolid/src/boolean.rs @@ -42,8 +42,57 @@ pub struct BooleanMeshF64 { pub indices: Vec, } +/// Schweiss-Toleranz: relativ zur grössten Koordinate (Eingaben kommen oft als f32 — eine +/// f32-Einheit in der letzten Stelle ist bis 2⁻²³ ≈ 1,2e-7 relativ, je Achse; Spielraum ×8), +/// mindestens 1e-7 m. +fn weld_tolerance(positions: &[f64]) -> f64 { + let m = positions.iter().fold(0.0f64, |m, x| m.max(x.abs())); + (1e-6 * m).max(1e-7) +} + +/// Verschweisst Ecken, die näher als die Toleranz beieinander liegen (räumliches Raster mit +/// Nachbarzellen). Truck vernetzt jede Fläche für sich; bei nicht achsparallelen Körpern sind +/// gemeinsame Ecken dann nur fast gleich ([108], [G7]). Liefert die vereinten Ecken und je +/// Eingabe-Ecke den neuen Index. Reihenfolge-stabil: die erste Ecke einer Gruppe bleibt. +fn weld(positions: &[f64]) -> (Vec, Vec) { + let tol = weld_tolerance(positions); + let tol2 = tol * tol; + let cell = |x: f64| (x / tol).floor() as i64; + let mut grid: HashMap<[i64; 3], Vec> = HashMap::new(); + let mut verts: Vec = Vec::new(); + let mut map: Vec = Vec::with_capacity(positions.len() / 3); + for c in positions.chunks_exact(3) { + let p = [c[0] + 0.0, c[1] + 0.0, c[2] + 0.0]; + let k = [cell(p[0]), cell(p[1]), cell(p[2])]; + let near = |id: &u32| { + let j = *id as usize * 3; + let d = [verts[j] - p[0], verts[j + 1] - p[1], verts[j + 2] - p[2]]; + d[0] * d[0] + d[1] * d[1] + d[2] * d[2] <= tol2 + }; + let mut found: Option = None; + for n in 0..27i64 { + let key = [k[0] + n % 3 - 1, k[1] + (n / 3) % 3 - 1, k[2] + n / 9 - 1]; + if let Some(id) = grid.get(&key).and_then(|ids| ids.iter().find(|id| near(id))) { + found = Some(*id); + break; + } + } + let id = match found { + Some(id) => id, + None => { + let id = (verts.len() / 3) as u32; + verts.extend_from_slice(&p); + grid.entry(k).or_default().push(id); + id + } + }; + map.push(id); + } + (verts, map) +} + /// Prüft ein Eingabe-Mesh und liefert ein verschweisstes, nach aussen orientiertes Netz -/// (gleiche Koordinaten werden vereint; Null-Flächen-Dreiecke entfallen). +/// (Ecken innerhalb der Schweiss-Toleranz werden vereint; Null-Flächen-Dreiecke entfallen). fn checked_mesh(positions: &[f64], indices: &[u32], what: &str) -> Result { if positions.len() % 3 != 0 || indices.len() % 3 != 0 { return Err(format!("{what}: Positionen/Indizes müssen Dreier-Gruppen sein")); @@ -58,19 +107,7 @@ fn checked_mesh(positions: &[f64], indices: &[u32], what: &str) -> Result = HashMap::new(); - let mut verts: Vec = Vec::new(); - let mut map: Vec = Vec::with_capacity(n_in); - for c in positions.chunks_exact(3) { - let key = [(c[0] + 0.0).to_bits(), (c[1] + 0.0).to_bits(), (c[2] + 0.0).to_bits()]; - let next = ids.len() as u32; - let id = *ids.entry(key).or_insert_with(|| { - verts.extend_from_slice(&[c[0] + 0.0, c[1] + 0.0, c[2] + 0.0]); - next - }); - map.push(id); - } + let (verts, map) = weld(positions); let mut tris: Vec = Vec::with_capacity(indices.len()); for t in indices.chunks_exact(3) { let mut m = [0u32; 3]; @@ -282,6 +319,38 @@ mod tests { assert!((volume_of(&r.positions, &r.indices) - 2.0).abs() < 1e-6); } + /// Gedrehter Quader, jede Fläche mit eigenen Ecken (wie truck vernetzt) und Rundungs- + /// fehlern in der Grössenordnung von f32: wird mit Toleranz zu einem geschlossenen Netz ([108]). + fn rotated_cuboid_per_face(w: f64, l: f64, h: f64, deg: f64, jitter: f64) -> (Vec, Vec) { + let (p, i) = cuboid(0.0, 0.0, 0.0, w, l, h); + let (c, s) = (deg.to_radians().cos(), deg.to_radians().sin()); + let mut pos = Vec::new(); + let mut idx = Vec::new(); + for (n, &v) in i.iter().enumerate() { + let j = v as usize * 3; + let e = jitter * (((n * 7919) % 13) as f64 - 6.0) / 6.0; + pos.extend_from_slice(&[p[j] * c - p[j + 1] * s + e, p[j] * s + p[j + 1] * c - e, p[j + 2] + e]); + idx.push(n as u32); + } + (pos, idx) + } + + #[test] + fn rotated_unshared_corners_are_welded_with_tolerance() { + let a = rotated_cuboid_per_face(5.0, 0.3, 2.6, 30.0, 2e-7); + let b = rotated_cuboid_per_face(1.2, 0.3, 1.2, 30.0, 2e-7); + let b: Vec = b.0.chunks(3).flat_map(|q| { + let (c, s) = (30f64.to_radians().cos(), 30f64.to_radians().sin()); + [q[0] + c, q[1] + s, q[2] + 0.9] + }).collect(); + let bi: Vec = (0..36).collect(); + let r = boolean_mesh_core(&a.0, &a.1, &b, &bi, "difference").unwrap(); + let expect = 5.0 * 0.3 * 2.6 - 1.2 * 0.3 * 1.2; + assert!((volume_of(&r.positions, &r.indices) - expect).abs() < 1e-4); + // Wirklich offene Netze bleiben abgelehnt. + assert!(boolean_mesh_core(&a.0[..a.0.len() - 9], &a.1[..33], &b, &bi, "union").is_err()); + } + #[test] fn rejects_non_triangle_indices() { let (ap, _) = cuboid(0.0, 0.0, 0.0, 1.0, 1.0, 1.0); diff --git a/src-tauri/trucksolid/src/brep.rs b/src-tauri/trucksolid/src/brep.rs index 21d14910..5306794a 100644 --- a/src-tauri/trucksolid/src/brep.rs +++ b/src-tauri/trucksolid/src/brep.rs @@ -1079,6 +1079,52 @@ mod tests { let spec: SolidSpec = serde_json::from_str(rev).unwrap(); assert!(solid_mesh_core(&spec, 0.01).is_ok()); } + + /// Schräge Wand (30°) als Prisma in gedrehter Ebene, Aussparung bündig mit beiden Wandflächen + /// (B-Rep scheitert) → Mesh-Fallback. Truck vernetzt jede Fläche einzeln; die Ecken sind dann + /// nicht bitgleich und müssen mit Toleranz verschweisst werden ([108], [G7]). + fn schraeg(points: Vec, x0: f64, depth: f64) -> SolidSpec { + let (c, s) = (30f64.to_radians().cos(), 30f64.to_radians().sin()); + SolidSpec::ExtrudePlane { + points, + holes: vec![], + origin: [x0 * c, x0 * s, 0.0], + u_axis: [c, s, 0.0], + v_axis: [0.0, 0.0, 1.0], + depth, + } + } + + #[test] + fn schraege_wand_minus_gedrehte_aussparung_ueber_mesh_fallback() { + let wall = schraeg(ring(0.0, 0.0, 5.0, 2.6), 0.0, -0.3); + let void = schraeg(ring(1.0, 0.9, 2.2, 2.1), 0.0, -0.3); + let spec = SolidSpec::Boolean { op: BoolKind::Difference, a: Box::new(wall), b: Box::new(void) }; + let m = solid_mesh_core(&spec, 0.01).unwrap(); + let expect = (5.0 * 2.6 - 1.2 * 1.2) * 0.3; + assert!((volume(&m) - expect).abs() < 1e-4, "{}", volume(&m)); + } + + #[test] + fn gehrte_schraege_wand_minus_aussparung_ueber_mesh_fallback() { + // Wand-Grundriss als Trapez (Gehrungen 60°/120° an den Enden), um 30° gedreht. + let (c, s) = (30f64.to_radians().cos(), 30f64.to_radians().sin()); + let rot = |x: f64, y: f64| [x * c - y * s, x * s + y * c]; + let t = 0.3 / 60f64.to_radians().tan(); + let mut pts = vec![]; + for (x, y) in [(0.0, 0.0), (5.0, 0.0), (5.0 - t, 0.3), (t, 0.3)] { + pts.extend_from_slice(&rot(x, y)); + } + let wall = SolidSpec::Extrude { points: pts, holes: vec![], height: 2.6, z0: 0.0, corner_radius: 0.0, chamfer: 0.0 }; + let void = schraeg(ring(1.0, 0.9, 2.2, 2.1), 0.0, -0.3); + // truck-shapeops panikt hier selbst („wire is not simple"), deshalb direkt der Mesh-Pfad. + let (ma, mb) = (solid_mesh_core(&wall, 0.01).unwrap(), solid_mesh_core(&void, 0.01).unwrap()); + let f64s = |v: &[f32]| v.iter().map(|x| *x as f64).collect::>(); + let r = crate::boolean::boolean_mesh_core(&f64s(&ma.positions), &ma.indices, &f64s(&mb.positions), &mb.indices, "difference").unwrap(); + let m = MeshOutput { positions: r.positions, indices: r.indices }; + let expect = ((5.0 + 5.0 - 2.0 * t) / 2.0 * 0.3) * 2.6 - 1.2 * 1.2 * 0.3; + assert!((volume(&m) - expect).abs() < 1e-4, "{}", volume(&m)); + } }