SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Intersection curves: a point that won't project no longer panics (#5562) (#5812)
DuBearte7804f9938a68
diff --git a/crates/engine/src/boolean_regression_tests.rs b/crates/engine/src/boolean_regression_tests.rsindex af7a91df39..f34b41709a 100644--- a/crates/engine/src/boolean_regression_tests.rs+++ b/crates/engine/src/boolean_regression_tests.rs@@ -73,6 +73,50 @@ fn volume(s: &Session, body: &str) -> f64 {s.model.state().body(body).map(|b| solidcraft_kernel::measure(&b.body).unwrap().volume).unwrap_or(0.0)}+/// #5562: a quarter disc in a box's side face, at its corner, revolved 360° about the box's+/// edge through the corner and cut: a hemisphere centred on the corner, its pole on that edge.+/// Dividing one of the cut's intersection edges met a point that projects onto neither surface,+/// and the fallback unwrapped that same failed projection: a kernel panic, recorded as a crash.+/// Nothing panics now; the box loses the eighth of the ball inside it, or the cut is refused+/// and the box is left whole.+#[test]+fn revolve_cut_round_a_box_corner_does_not_panic() {+ use std::sync::Arc;+ use std::sync::atomic::{AtomicUsize, Ordering};+ let mut s = Session::default();+ for (command, params) in [+ ("sketch.create", json!({"plane": "XY"})),+ ("sketch.rectangle.two_point", json!({"p0": [-5.4, 7.0], "p1": [15.0, -5.0]})),+ ("sketch.finish", json!({})),+ ("solid.extrude", json!({"direction": "positive", "distance": "10 mm", "operation": "new", "profiles": [0], "sketch": 1})),+ ("sketch.create", json!({"plane": {"body": "Body1", "face": [12.721266146083186, -5.0, 7.034827173055135], "index": 1, "path": []}})),+ ("sketch.circle.center", json!({"center": "p3", "radius": 4.664761515876241})),+ ("sketch.options", json!({"slice": true})),+ ("sketch.finish", json!({})),+ ] {+ s.execute(command, ¶ms).unwrap();+ }+ let before = volume(&s, "Body1");+ let me = std::thread::current().id();+ let count = Arc::new(AtomicUsize::new(0));+ let c2 = count.clone();+ let prev = std::panic::take_hook();+ std::panic::set_hook(Box::new(move |_| {+ if std::thread::current().id() == me {+ c2.fetch_add(1, Ordering::SeqCst);+ }+ }));+ let result = s.execute("solid.revolve", &json!({"angle": "360 deg", "axis": "l4", "operation": "cut", "profiles": [2], "sketch": 3}));+ std::panic::set_hook(prev);+ assert_eq!(count.load(Ordering::SeqCst), 0, "kernel panics during the cut");+ let removed = before - volume(&s, "Body1");+ let eighth = std::f64::consts::PI * 4.664761515876241_f64.powi(3) / 6.0;+ match result {+ Ok(_) => assert!((removed - eighth).abs() < 0.02 * eighth, "removed {removed}, an eighth of the ball is {eighth}"),+ Err(e) => assert!(removed.abs() < 1e-9, "{e}, yet the box lost {removed}"),+ }+}+/// #1985: the STEP export of a small enclosure never finished. The export reads its own file/// back, and meshing the screw hole's seam cylinder never ended: the boundary walk took/// projected angles millions of turns apart. The file is written, reads back cleanly, and holdsdiff --git a/vendor/README.md b/vendor/README.mdindex 9226d70ace..7eaeb587a7 100644--- a/vendor/README.md+++ b/vendor/README.md@@ -82,6 +82,15 @@ Changes:below truck's `TOLERANCE` (1e-6), or one that is not a number, divides at `TOLERANCE` insteadof panicking in truck's tolerance assertion; a join failed on that panic (#4623).`crates/kernel/src/issue_1038_tests.rs` holds it to the division at 1e-6.+- `decorators/intersection_curve.rs`: where the hinted projection behind `parameter_division`+ and the search from nothing both fail, the leader's point stands in, the division stops, and+ the failure is recorded for `take_projection_failure`. It fell back to `subs`, which unwraps+ that same failed search: a revolve cut whose sphere's pole sat on a box's edge panicked+ dividing an edge (#5562).+- truck-shapeops `transversal/divide_face/mod.rs`: an edge whose division recorded such a+ failure gives up the face division (`None`), so the Boolean attempt fails as quickly as the+ panic made it, and the kernel goes on to its other tries. Carrying the approximate points on+ made one test's Boolean run for minutes.## truck-stepio 0.3.0 (Apache-2.0, © RICOS Co. Ltd., https://github.com/ricosjp/truck)diff --git a/vendor/truck-geometry/src/decorators/intersection_curve.rs b/vendor/truck-geometry/src/decorators/intersection_curve.rsindex 9b14d0ef12..0cbc52fc2e 100644--- a/vendor/truck-geometry/src/decorators/intersection_curve.rs+++ b/vendor/truck-geometry/src/decorators/intersection_curve.rs@@ -182,13 +182,28 @@ where// SolidCraft: a point of the curve with its parameters on both surfaces.type Triple = (Point3, Point2, Point2);+thread_local! {+ // SolidCraft: a point of an intersection curve projected onto neither surface since the last+ // `take_projection_failure` (the leader's point stood in for it).+ static PROJECTION_FAILED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };+}++/// SolidCraft: whether `parameter_division` of an intersection curve met a point that projects+/// onto neither surface on this thread since the last call, which clears it. Such a division+/// holds the leader's points there and stops dividing: a Boolean gives up on the curve, as it+/// did when the failure panicked, instead of carrying approximate points on (#5562).+#[doc(hidden)]+pub fn take_projection_failure() -> bool { PROJECTION_FAILED.with(|f| f.replace(false)) }+impl<C, S> IntersectionCurve<C, S>whereC: ParametricCurve3D,S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,{// SolidCraft: `search_triple` started from `hint`'s parameters, falling back to the search- // from nothing (and, where that fails too, to `subs` as before). A hinted result is taken+ // from nothing. Where that fails too, the leader's point stands in and the failure is+ // recorded (`take_projection_failure`): `subs` unwrapped that same failed search and+ // panicked, failing a revolve cut through a box's corner (#5562). A hinted result is taken// only within the curve's tolerance of the leader's point, where the leader lies: started// from a neighbour (or from a periodic surface's seam), the projection can settle on another// part of a surface, such as the far side of a small hole, which the search from nothing@@ -210,7 +225,10 @@ where});match hinted.or_else(|| self.search_triple(t)) {Some(triple) => triple,- None => (self.subs(t), Point2::origin(), Point2::origin()),+ None => {+ PROJECTION_FAILED.with(|f| f.set(true));+ (lead, Point2::origin(), Point2::origin())+ }}}}@@ -233,7 +251,9 @@ wherelet t = range.0 * (1.0 - p) + range.1 * p;let mid = p0 + (p1 - p0) * p;let dist2 = curve.hinted_triple(t, Some(ends.0)).0.distance2(mid);- if dist2 < tol * tol || trials == 0 {+ // SolidCraft: a point that projects nowhere ends the division (see `take_projection_failure`).+ let failed = PROJECTION_FAILED.with(std::cell::Cell::get);+ if dist2 < tol * tol || trials == 0 || failed {(vec![range.0, range.1], vec![p0, p1])} else {let mid_param = (range.0 + range.1) / 2.0;diff --git a/vendor/truck-geometry/src/decorators/mod.rs b/vendor/truck-geometry/src/decorators/mod.rsindex 5961c1e56e..6271f3b25b 100644--- a/vendor/truck-geometry/src/decorators/mod.rs+++ b/vendor/truck-geometry/src/decorators/mod.rs@@ -146,4 +146,4 @@ mod intersection_curve;mod processor;mod revolved_curve;mod trimmied_curve;-pub use intersection_curve::double_projection;+pub use intersection_curve::{double_projection, take_projection_failure};diff --git a/vendor/truck-shapeops/src/transversal/divide_face/mod.rs b/vendor/truck-shapeops/src/transversal/divide_face/mod.rsindex d4972464d3..2736ac6a1d 100644--- a/vendor/truck-shapeops/src/transversal/divide_face/mod.rs+++ b/vendor/truck-shapeops/src/transversal/divide_face/mod.rs@@ -38,11 +38,18 @@ where.or_else(|| surface.search_nearest_parameter(pt, None, 100))?.into();let vec = wire.edge_iter().try_fold(vec![p], |mut vec, edge| {- let poly = polys.entry(edge.id()).or_insert_with(|| {+ // SolidCraft: an intersection curve with a point that projects onto neither surface+ // gives this division up (`take_projection_failure`), as its panic did (#5562).+ if !polys.contains_key(&edge.id()) {let curve = edge.curve();+ let _ = truck_geometry::prelude::take_projection_failure();let div = curve.parameter_division(curve.range_tuple(), tol).1;- PolylineCurve(div)- });+ if truck_geometry::prelude::take_projection_failure() {+ return None;+ }+ polys.insert(edge.id(), PolylineCurve(div));+ }+ let poly = polys.get(&edge.id())?;let mut p = *vec.last().unwrap();let closure = |q: &P| -> Option<Point2> {// SolidCraft: the points of an intersection curve on a lofted or swept surface lie on