Skip to content

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

fix: axis or tangent plane on a drill tip no longer fails with "no face at NaN" (#567) (#655)

Lasse Liancb071bb93b96
diff --git a/apps/solidcraft-cli/tests/action_matrix.rs b/apps/solidcraft-cli/tests/action_matrix.rs
index b41a41b968..7800f26c7e 100644
--- a/apps/solidcraft-cli/tests/action_matrix.rs
+++ b/apps/solidcraft-cli/tests/action_matrix.rs
@@ -1169,8 +1169,36 @@ fn a_shell_of_a_swept_bar() {
);
}
+/// The construction a drill-tip script ends with.
+struct TipConstruction(solidcraft_engine::doc::construct::ConstructGeom);
+
+impl TipConstruction {
+ fn axis(&self) -> (Vec3, Vec3) {
+ match &self.0 {
+ solidcraft_engine::doc::construct::ConstructGeom::Axis { origin, dir } => (*origin, *dir),
+ other => panic!("expected an axis, got {other:?}"),
+ }
+ }
+ fn plane(&self) -> solidcraft_engine::geom::Plane {
+ match &self.0 {
+ solidcraft_engine::doc::construct::ConstructGeom::Plane(p) => *p,
+ other => panic!("expected a plane, got {other:?}"),
+ }
+ }
+}
+
+/// Replays the script and returns its last construction.
+fn drill_tip_construction(commands: Vec<Value>) -> TipConstruction {
+ let mut s = Session::default();
+ for c in &commands {
+ let id = c["command"].as_str().unwrap();
+ s.execute(id, &c["params"]).unwrap_or_else(|e| panic!("{id}: {e}"));
+ }
+ let state = s.model.state();
+ TipConstruction(state.construct.last().map(|c| c.geom.clone()).expect("a construction"))
+}
+
#[test]
-#[ignore = "fails until #567 is fixed"]
fn an_axis_through_a_drill_tip() {
let mut c: Vec<Value> = rectangle(json!("XY"), [0., 0.], [60., 40.]).into();
c.extend([
@@ -1178,7 +1206,30 @@ fn an_axis_through_a_drill_tip() {
json!({"command": "solid.hole", "params": {"position": [50, 20, 15], "direction": [0, 0, -1], "diameter": 6, "depth": 10, "tip_angle": 118}}),
json!({"command": "construct.axis.through_cylinder", "params": {"face": {"face": [48.44468907075447, 21.22448979591836, 4.399139380972439]}}}),
]);
- reproduces(None, Value::Array(c));
+ reproduces(None, Value::Array(c.clone()));
+ // Not just "no error": the axis runs along the drill, through the hole's centre line.
+ let (origin, dir) = drill_tip_construction(c).axis();
+ assert!((origin.x - 50.).abs() < 1e-6 && (origin.y - 20.).abs() < 1e-6, "{origin:?}");
+ assert!(dir.z.abs() > 1. - 1e-9, "{dir:?}");
+}
+
+#[test]
+fn a_tangent_plane_on_a_drill_tip() {
+ let mut c: Vec<Value> = rectangle(json!("XY"), [0., 0.], [60., 40.]).into();
+ c.extend([
+ json!({"command": "solid.extrude", "params": {"distance": 15}}),
+ json!({"command": "solid.hole", "params": {"position": [50, 20, 15], "direction": [0, 0, -1], "diameter": 6, "depth": 10, "tip_angle": 118}}),
+ json!({"command": "construct.plane.tangent", "params": {"face": {"face": [48.44468907075447, 21.22448979591836, 4.399139380972439]}}}),
+ ]);
+ reproduces(None, Value::Array(c.clone()));
+ // The plane touches the cone at the picked point and leans like the cone does: a 118 degree tip
+ // puts the wall 59 degrees off the drill axis, so its normal is 31 degrees off it.
+ let plane = drill_tip_construction(c).plane();
+ let picked = Vec3::new(48.44468907075447, 21.22448979591836, 4.399139380972439);
+ let distance = (picked - plane.origin).dot(plane.normal());
+ // The pick lies on the tessellated cone and the plane is the exact one, so allow for chord error.
+ assert!(distance.abs() < 0.05, "the pick is {distance} mm off the plane");
+ assert!((plane.normal().z.abs() - 31f64.to_radians().cos()).abs() < 1e-2, "{:?}", plane.normal());
}
#[test]
diff --git a/crates/doc/src/name_lookup.rs b/crates/doc/src/name_lookup.rs
index 6dba47d465..1499c1fa6b 100644
--- a/crates/doc/src/name_lookup.rs
+++ b/crates/doc/src/name_lookup.rs
@@ -104,7 +104,8 @@ fn nearest_on_face(m: &Mesh, face: usize, p: Vec3) -> Option<(f64, Vec3)> {
let Some([a, b, c]) = m.tri(t) else { continue };
let q = closest(p, a, b, c);
let d = q.dist(p);
- if best.is_none_or(|x| d < x.0) {
+ // A non-finite candidate must never become the best: nothing compares less than NaN.
+ if d.is_finite() && best.is_none_or(|x| d < x.0) {
best = Some((d, q));
}
}
@@ -317,7 +318,7 @@ fn legacy_locate<'a>(bodies: impl Iterator<Item = (usize, &'a ModelBody)>, name:
let Some([a, bb, c]) = m.tri(t) else { continue };
let q = closest(p, a, bb, c);
let d = q.dist(p) - if exact { 1e-9 } else { 0.0 };
- if best.is_none_or(|x| d < x.0) {
+ if d.is_finite() && best.is_none_or(|x| d < x.0) {
best = Some((d, bi, *f as usize, q, !exact, tol));
}
}
diff --git a/crates/doc/src/naming.rs b/crates/doc/src/naming.rs
index baa6a516b0..4f32705fc7 100644
--- a/crates/doc/src/naming.rs
+++ b/crates/doc/src/naming.rs
@@ -114,6 +114,8 @@ fn samples(m: &Mesh, nf: usize) -> Vec<Vec<Vec3>> {
.collect()
}
+/// The point of triangle `abc` nearest `p`. A zero-length edge (a cone's tip repeats its apex
+/// vertex) has no interior to project onto, so its branch is skipped rather than dividing 0 by 0.
pub(crate) fn closest(p: Vec3, a: Vec3, b: Vec3, c: Vec3) -> Vec3 {
let (ab, ac, ap) = (b - a, c - a, p - a);
let (d1, d2) = (ab.dot(ap), ac.dot(ap));
@@ -126,7 +128,7 @@ pub(crate) fn closest(p: Vec3, a: Vec3, b: Vec3, c: Vec3) -> Vec3 {
return b;
}
let vc = d1 * d4 - d3 * d2;
- if vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0 {
+ if vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0 && d1 - d3 > 0.0 {
return a + ab * (d1 / (d1 - d3));
}
let cp = p - c;
@@ -135,11 +137,11 @@ pub(crate) fn closest(p: Vec3, a: Vec3, b: Vec3, c: Vec3) -> Vec3 {
return c;
}
let vb = d5 * d2 - d1 * d6;
- if vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0 {
+ if vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0 && d2 - d6 > 0.0 {
return a + ac * (d2 / (d2 - d6));
}
let va = d3 * d6 - d5 * d4;
- if va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0 {
+ if va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0 && (d4 - d3) + (d5 - d6) > 0.0 {
return b + (c - b) * ((d4 - d3) / ((d4 - d3) + (d5 - d6)));
}
let den = va + vb + vc;
@@ -753,6 +755,20 @@ pub fn face_on_body(body: &ModelBody, name: &str, p: Vec3) -> Option<(usize, Vec
mod tests {
use super::*;
+ /// A cone's tip repeats its apex vertex, so a triangle can have a zero-length edge. The nearest
+ /// point on it must still be a real point (it used to be NaN, which broke picking the tip).
+ #[test]
+ fn closest_point_on_a_degenerate_triangle_is_finite() {
+ let (a, c) = (Vec3::new(0., 0., 0.), Vec3::new(2., 0., 0.));
+ for (b, c) in [(a, c), (c, c), (a, a)] {
+ for p in [Vec3::new(1., 1., 0.), Vec3::new(-1., 0.5, 0.), Vec3::new(3., -2., 1.), a] {
+ let q = closest(p, a, b, c);
+ assert!(q.x.is_finite() && q.y.is_finite() && q.z.is_finite(), "{a:?} {b:?} {c:?} {p:?} -> {q:?}");
+ }
+ }
+ assert_eq!(closest(Vec3::new(1., 1., 0.), a, a, c), Vec3::new(1., 0., 0.));
+ }
+
#[test]
fn merged_face_votes_collectively_for_pieces_of_one_source() {
let body = ModelBody::new("Input".into(), solidcraft_kernel::box_solid(Vec3::ZERO, Vec3::new(4., 1., 1.)).unwrap(), 1);