Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Boolean: a small hole in a large body's face stays a hole when the face is divided (#2579) (#2580)

NOWN-DEV090119a5c56e
diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs
index d7546cbc53..6a669145fa 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<Vec3>) {
+ 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<Vec3> = (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 &centres {
+ 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 1d974fa7ee..fc472e9441 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 0226809a5b..d4972464d3 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<Point2>) -> 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<C, S>(face: &Face<Point3, C, S>, wire: &Wire<Point3, C>, poly: PolylineCurve<Point2>, tol: f64) -> PolylineCurve<Point2>
+where
+ C: BoundedCurve<Point = Point3> + ParameterDivision1D<Point = Point3>,
+ S: Clone + ParametricSurface3D + SearchParameter<D2, Point = Point3> + SearchNearestParameter<D2, Point = Point3>,
+{
+ 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<Point2>,
@@ -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 }),