From 090119a5c56ed7b5dc1e2a591b7b91a7140a2dc9 Mon Sep 17 00:00:00 2001 From: NOWN-DEV <257771410+NOWN-DEV@users.noreply.github.com> Date: Tue, 20 Oct 2026 21:56:33 +0000 Subject: [PATCH] Boolean: a small hole in a large body's face stays a hole when the face is divided (#2579) (#2580) * Boolean: a small hole in a large body's face stays a hole when the face is divided A boolean divides faces at a tolerance that follows the larger operand (a few millimetres on a part a few metres long). A small hole's rim, two half circles when read from STEP, then became two chords: its polygon in the face's parameters had no area, and the sign that tells a hole from a face piece came out at random. The hole became a face of its own, inside out, and the face it was cut in stayed whole. A box cut round such a hole gave a piece without it, and the shared part of that piece and a pin in the hole came out negative ("malformed") or the boolean failed. divide_face now divides a loop whose polygon is flat again, finer (down to a hundredth of the loop's own size), before reading its sign. Only the polygon that places the loop changes; loops that have an area are untouched. Co-Authored-By: Claude Opus 5.5 (1M context) * Kernel: the long-plate hole test in tests.rs; a flat loop's size from its box Review: the test moves into the kernel's test file (no file of its own), and finer_if_flat takes the loop's size from the diagonal of the box round its sample points instead of the largest distance between any two. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Aac7mP9rthK3WsPpPRvCsF --------- Co-authored-by: Claude Opus 5.5 (1M context) --- crates/kernel/src/tests.rs | 42 ++++++++++++++ vendor/README.md | 5 ++ .../src/transversal/divide_face/mod.rs | 56 +++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs index d7546cbc5..6a669145f 100644 --- a/crates/kernel/src/tests.rs +++ b/crates/kernel/src/tests.rs @@ -3012,3 +3012,45 @@ fn placing_a_body_does_not_turn_its_revolved_faces_again() { assert_eq!(a.faces, b.faces); assert!(a.bbox.min.dist(b.bbox.min) < 1e-9 && a.bbox.max.dist(b.bbox.max) < 1e-9); } + +/// A plate 3 m long and 2 mm thick with ten holes of radius `r` along a diagonal, turned by +/// `tilt` about the X axis and read back from STEP (each rim is then two half circles). +fn long_plate_with_small_holes(r: f64, tilt: f64) -> (Body, Vec) { + let mut p = box_solid(Vec3::new(-1500.0, -150.0, 0.0), Vec3::new(1500.0, 150.0, 2.0)).unwrap(); + let centres: Vec = (0..10).map(|i| Vec3::new(-1259.3 + 271.3 * i as f64, -120.0 + 27.7 * i as f64, 0.0)).collect(); + for &c in ¢res { + let tool = cylinder(c - Vec3::Z, Vec3::Z, r, 4.0).unwrap(); + p = boolean(&p, &tool, BoolOp::Cut).unwrap().unwrap(); + } + let shift = Vec3::new(0.0, 300.0, 300.0); + let p = transform(&p, shift, Vec3::ZERO, Vec3::X, tilt).unwrap(); + let text = step_export(&[&p], "plate").unwrap(); + let p = step_import(&text).unwrap().bodies.remove(0).body; + let (s, c) = tilt.sin_cos(); + let placed = centres.iter().map(|q| Vec3::new(q.x, q.y * c - q.z * s, q.y * s + q.z * c) + shift).collect(); + (p, placed) +} + +/// A small hole in a face of a large body, divided by a boolean. The boolean's tolerance follows +/// the larger body (a few millimetres on a part a few metres long), so each half circle of the +/// hole's rim was a single chord; the rim's polygon in the face's parameters had no area, and its +/// sign, which tells a hole from a face piece, came out at random. The hole then became a face of +/// its own, inside out, and the face it was cut in was left whole: a box cut round such a hole +/// gave a piece without it, and the shared part of that piece and a pin in the hole came out +/// negative or not at all. +#[test] +fn a_box_cut_round_a_small_hole_of_a_long_plate_keeps_the_hole() { + for (r, tilt) in [(1.8, 0.27), (1.8, 0.7)] { + let (p, centres) = long_plate_with_small_holes(r, tilt); + for q in centres { + let g = Vec3::new(8.0, 8.0, 8.0); + let cut = box_solid(q - g, q + g).unwrap(); + let piece = boolean(&p, &cut, BoolOp::Intersect).unwrap().unwrap(); + let walls = (0..piece.face_count()).filter(|&i| matches!(face_surface(&piece, i), Some(FaceSurface::Cylinder { .. }))).count(); + // Both faces of the plate in the box keep the hole: an outer loop and the rim. + let holed = piece.solid.face_iter().filter(|f| f.boundaries().len() == 2).count(); + assert!(walls >= 1 && holed == 2, "r {r}, tilt {tilt}, hole at {q:?}: {walls} walls, {holed} faces with a hole"); + assert!(measure(&piece).unwrap().volume > 0.0, "r {r}, tilt {tilt}, hole at {q:?}"); + } + } +} diff --git a/vendor/README.md b/vendor/README.md index 1d974fa7e..fc472e944 100644 --- a/vendor/README.md +++ b/vendor/README.md @@ -30,6 +30,11 @@ Changes: With no direction to go by, the edge goes where both of its ends are in one wire. - `transversal/divide_face/mod.rs`: a face piece whose wire is not simple makes the operation give up (`Face::try_new`) instead of panicking. +- `transversal/divide_face/mod.rs`: a loop whose polygon in the face's parameters has no area + (a small hole's rim divided at the tolerance of a much larger operand: one chord per half + circle) is divided again, finer, before its area's sign tells a hole from a face piece. The + sign of a zero area was random: the hole became a face of its own, inside out, and the face it + was cut in stayed whole. - `lib.rs`: `tr!` reports where an operation gives up when `SHAPEOPS_TRACE` is set; compiler warnings no longer fail the build. - Face and edge history (`transversal/history.rs`, new): `and_with_history` / `or_with_history` diff --git a/vendor/truck-shapeops/src/transversal/divide_face/mod.rs b/vendor/truck-shapeops/src/transversal/divide_face/mod.rs index 0226809a5..d4972464d 100644 --- a/vendor/truck-shapeops/src/transversal/divide_face/mod.rs +++ b/vendor/truck-shapeops/src/transversal/divide_face/mod.rs @@ -86,6 +86,61 @@ where Some(PolylineCurve(vec)) } +/// SolidCraft: whether a loop's polygon in the parameter plane has no area to speak of: two +/// points (a closed loop of two edges divided at their ends only), or an area that is rounding +/// beside the polygon's extent. Its sign then tells nothing. +fn flat_polygon(poly: &PolylineCurve) -> bool { + let (mut lo, mut hi) = (Point2::new(f64::INFINITY, f64::INFINITY), Point2::new(f64::NEG_INFINITY, f64::NEG_INFINITY)); + for p in poly.iter() { + lo = Point2::new(lo.x.min(p.x), lo.y.min(p.y)); + hi = Point2::new(hi.x.max(p.x), hi.y.max(p.y)); + } + let diag2 = (hi.x - lo.x).powi(2) + (hi.y - lo.y).powi(2); + poly.len() < 4 || !(poly.area().abs() > 1e-9 * diag2) +} + +/// SolidCraft: a loop whose polygon is flat ([`flat_polygon`]) divided again, finer, to the +/// loop's own size. A small hole's wire is two half circles; divided at a tolerance that +/// follows a much larger body (a few millimetres for a part over a metre long) each half is a +/// single chord, the polygon has no area, and its sign, which tells a hole from a face piece, +/// comes out at random: the hole became a face of its own, inside out, and the face it was cut +/// in was left whole. The polygon only places the loop; the edges are not changed. +fn finer_if_flat(face: &Face, wire: &Wire, poly: PolylineCurve, tol: f64) -> PolylineCurve +where + C: BoundedCurve + ParameterDivision1D, + S: Clone + ParametricSurface3D + SearchParameter + SearchNearestParameter, +{ + if !flat_polygon(&poly) { + return poly; + } + // The loop's extent: the diagonal of the box round its edges' ends and middles. + let mut pts = Vec::new(); + for edge in wire.edge_iter() { + let curve = edge.curve(); + let (t0, t1) = curve.range_tuple(); + pts.extend([curve.subs(t0), curve.subs((t0 + t1) / 2.0), curve.subs(t1)]); + } + let (mut lo, mut hi) = (Point3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY), Point3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY)); + for p in &pts { + lo = Point3::new(lo.x.min(p.x), lo.y.min(p.y), lo.z.min(p.z)); + hi = Point3::new(hi.x.max(p.x), hi.y.max(p.y), hi.z.max(p.z)); + } + let size = if pts.is_empty() { 0.0 } else { hi.distance(lo) }; + let mut fine = tol; + for _ in 0..3 { + fine = (fine / 8.0).min(size * 0.01); + if !(fine > 0.0 && fine.is_finite()) { + break; + } + let mut map = HashMap::default(); + let again = create_parameter_boundary(face, wire, &mut map, fine, false).or_else(|| create_parameter_boundary(face, wire, &mut map, fine, true)); + if let Some(again) = again.filter(|p| !flat_polygon(p)) { + return again; + } + } + poly +} + #[derive(Clone, Debug)] struct WireChunk<'a, C> { poly: PolylineCurve, @@ -109,6 +164,7 @@ where create_parameter_boundary(face, wire, &mut map, tol, false).or_else(|| create_parameter_boundary(face, wire, &mut map, tol, true)), "parameter boundary" ); + let poly = finer_if_flat(face, wire, poly, tol); match poly.area() > 0.0 { true => pre_faces.push(vec![WireChunk { poly, wire }]), false => negative_wires.push(WireChunk { poly, wire }),