Skip to content

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

Measure: two flat faces always show the distance between them (#11833) (#12708)

PaddleStroke7b3ca00054d0
diff --git a/crates/engine/src/cmd/measure_sel.rs b/crates/engine/src/cmd/measure_sel.rs
index 4d073b7b0a..0f998ad7df 100644
--- a/crates/engine/src/cmd/measure_sel.rs
+++ b/crates/engine/src/cmd/measure_sel.rs
@@ -457,8 +457,10 @@ fn axis_named(s: &Session, name: &str) -> Option<(Vec3, Vec3)> {
}
/// The closest points of two exact shapes, where the sampled points would be wrong: a plane or
-/// an axis runs on, so a point set of it is not the object.
-fn exact_min(a: &Shape, b: &Shape) -> Option<(f64, Vec3, Vec3)> {
+/// an axis runs on, so a point set of it is not the object. `anchor` is a point of `a` to
+/// measure from where any point would do (two parallel planes); `unbounded` says whether `a`
+/// and `b` run on for ever (datum planes and axes) rather than ending (faces and edges).
+fn exact_min(a: &Shape, b: &Shape, anchor: Option<Vec3>, unbounded: (bool, bool)) -> Option<(f64, Vec3, Vec3)> {
use Shape::*;
let to_plane = |p: Vec3, point: Vec3, normal: UnitVec3| {
let n = normal.vec();
@@ -484,17 +486,7 @@ fn exact_min(a: &Shape, b: &Shape) -> Option<(f64, Vec3, Vec3)> {
}
(Point(p), Point(q)) => Some((p.dist(*q), *p, *q)),
(Plane { point: p1, normal: n1 }, Plane { point: p2, normal: n2 }) => {
- let (v1, v2) = (n1.vec(), n2.vec());
- if v1.cross(v2).len() < 1e-9 {
- let h = (*p2 - *p1).dot(v1);
- Some((h.abs(), *p1, *p2 - v1 * h))
- } else {
- // They meet: a point on the line of intersection has distance zero.
- let dir = v1.cross(v2);
- let t = -(*p1 - *p2).dot(v2) / dir.dot(v2);
- let p = *p1 + dir * t;
- Some((0.0, p, p))
- }
+ super::measure_shape::plane_plane(*p1, *n1, *p2, *n2, anchor.unwrap_or(*p1), unbounded.0 && unbounded.1)
}
(Plane { point, normal }, Line { a: la, b: lb }) => {
let d = *lb - *la;
@@ -712,7 +704,10 @@ pub(crate) fn measure_items(s: &Session, items: &Value) -> Result<Value> {
out["delta"] = json!(q - p);
out["min"] = json!(d);
}
- if let Some((d, p, q)) = exact_min(&a.shape, &b.shape) {
+ let unbounded = |i: usize| matches!(sels.get(i), Some(Sel::Plane { .. } | Sel::Axis { .. }));
+ if let Some((d, p, q)) = exact_min(&a.shape, &b.shape, near.map(|(p, _)| p), (unbounded(0), unbounded(1)))
+ .filter(|(d, p, q)| d.is_finite() && p.is_finite() && q.is_finite())
+ {
out["distance_mm"] = json!(d);
out["from"] = json!(p);
out["to"] = json!(q);
diff --git a/crates/engine/src/cmd/measure_shape.rs b/crates/engine/src/cmd/measure_shape.rs
index eb61dddfd5..80bacac3f2 100644
--- a/crates/engine/src/cmd/measure_shape.rs
+++ b/crates/engine/src/cmd/measure_shape.rs
@@ -271,6 +271,32 @@ fn from_round(a: &Shape, b: &Shape) -> Option<Centre> {
(c.distance.is_finite() && c.from.is_finite() && c.to.is_finite()).then_some(c)
}
+/// The distance between two planes, `from` on the first and `to` on the second.
+///
+/// Parallel planes (normals within 1e-6 rad, as [`parallel`] counts axes) are the
+/// perpendicular gap between them (#11833), measured from `anchor` (a point of the first item,
+/// so the dimension is drawn on it) moved onto the first plane. Planes at an angle meet: only
+/// when both run on for ever (datum planes, `meet`) is that a distance of zero on their line of
+/// intersection; a face ends, so its distance is left to its own points (`None`).
+pub(crate) fn plane_plane(p1: Vec3, n1: UnitVec3, p2: Vec3, n2: UnitVec3, anchor: Vec3, meet: bool) -> Option<(f64, Vec3, Vec3)> {
+ let (v1, v2) = (n1.vec(), n2.vec());
+ let across = v1.cross(v2);
+ if across.len() < 1e-6 {
+ let from = anchor - v1 * (anchor - p1).dot(v1);
+ let h = (from - p2).dot(v1);
+ return Some((h.abs(), from, from - v1 * h));
+ }
+ if !meet {
+ return None;
+ }
+ // The point of both planes nearest the first plane's point: from `p1` across the first
+ // plane, square to the line `n1 × n2`, until it reaches the second plane. Worked from the
+ // cross product, not `1 - (n1·n2)²`, which cancels when the planes are nearly parallel.
+ let w = across.cross(v1);
+ let p = p1 + w * ((p2 - p1).dot(v2) / across.dot(across));
+ p.is_finite().then_some((0.0, p, p))
+}
+
/// A slot's sizes: width (the ends' diameter), the distance between the ends' centres and the
/// overall length, with the two centres.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -504,4 +530,42 @@ mod tests {
assert!(slot_outline(&arcs, &skew).is_none());
assert!(slot_outline(&[(Vec2::new(0.0, 0.0), 3.0, PI), (Vec2::new(20.0, 0.0), 4.0, PI)], &lines).is_none());
}
+
+ /// Two walls of a box whose normals are parallel only to rounding (#11833): the gap between
+ /// the planes, from the picked point on the first to its foot on the second, never NaN.
+ #[test]
+ fn nearly_parallel_planes_measure_the_gap_between_them() {
+ let tilt = UnitVec3::new(Vec3::new(1e-8, 0.0, 1.0)).unwrap();
+ for (n2, gap) in [(UnitVec3::Z, 20.0), (-UnitVec3::Z, 20.0), (tilt, 20.0)] {
+ let anchor = Vec3::new(7.0, 3.0, 0.5);
+ let (d, from, to) = plane_plane(Vec3::new(100.0, -50.0, 0.0), UnitVec3::Z, Vec3::new(-4.0, 9.0, gap), n2, anchor, false).unwrap();
+ assert!((d - gap).abs() < 1e-6, "{n2:?}: {d}");
+ assert!(from.is_finite() && to.is_finite());
+ assert!(from.dist(Vec3::new(7.0, 3.0, 0.0)) < 1e-12, "from lies on the first plane under the pick: {from:?}");
+ assert!((to.z - gap).abs() < 1e-6 && (to - from).cross(Vec3::Z).len() < 1e-6, "to lies on the second plane: {to:?}");
+ }
+ }
+
+ /// Planes at an angle: datum planes meet (zero, on both planes); faces end, so their
+ /// distance is left to their points.
+ #[test]
+ fn planes_at_an_angle_meet_only_when_they_run_on() {
+ let (p1, p2) = (Vec3::new(0.0, 0.0, 5.0), Vec3::new(3.0, 0.0, 0.0));
+ let n2 = UnitVec3::new(Vec3::new(1.0, 0.0, 1.0)).unwrap();
+ let (d, from, to) = plane_plane(p1, UnitVec3::Z, p2, n2, p1, true).unwrap();
+ assert_eq!((d, from), (0.0, to));
+ assert!(from.is_finite() && (from.z - 5.0).abs() < 1e-12 && (from - p2).dot(n2.vec()).abs() < 1e-12, "{from:?}");
+ assert!(plane_plane(p1, UnitVec3::Z, p2, n2, p1, false).is_none());
+ assert!(plane_plane(p1, UnitVec3::Z, p2, UnitVec3::X, p1, true).is_some_and(|(_, p, _)| p.is_finite()));
+ // Just past parallel and far from the origin: the point still lies on both planes.
+ for (tilt, z1, z2) in [(2e-6, 0.0, 20.0), (2e-6, 100.0, 120.0), (1.01e-6, 1e5, 100_020.0)] {
+ let (p1, p2) = (Vec3::new(3.0, -4.0, z1), Vec3::new(-7.0, 2.0, z2));
+ let n2 = UnitVec3::new(Vec3::new(tilt, 0.0, 1.0)).unwrap();
+ let (d, p, _) = plane_plane(p1, UnitVec3::Z, p2, n2, p1, true).unwrap();
+ let scale = 1.0 + p.len();
+ assert_eq!(d, 0.0);
+ assert!((p.z - z1).abs() < 1e-9 * scale, "tilt {tilt}: {p:?} off the first plane");
+ assert!((p - p2).dot(n2.vec()).abs() < 1e-9 * scale, "tilt {tilt}: {p:?} off the second plane");
+ }
+ }
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 005cdea9a1..fe656208e6 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -1918,9 +1918,17 @@ fn measure_items() {
let r = run(&mut s, "inspect.measure", json!({"items": [face(ft, [20.0, 15.0, 20.0]), face(fb, [20.0, 15.0, 0.0])]}));
assert!((r["distance_mm"].as_f64().unwrap() - 20.0).abs() < 1e-9, "{r}");
assert!((r["angle_deg"].as_f64().unwrap() - 180.0).abs() < 1e-6, "{r}");
+ // The ends of a distance are real points (#11833: NaN ends hid the Distance row): one on
+ // each face, straight across.
+ let end = |k: &str| -> Vec3 { serde_json::from_value(r[k].clone()).unwrap_or_else(|e| panic!("{k}: {e} in {r}")) };
+ let (from, to) = (end("from"), end("to"));
+ assert!((from.z - 20.0).abs() < 1e-9 && to.z.abs() < 1e-9 && (to - from).cross(Vec3::Z).len() < 1e-9, "{r}");
let r = run(&mut s, "inspect.measure", json!({"items": [face(ft, [20.0, 15.0, 20.0]), face(ff, [20.0, 0.0, 10.0])]}));
assert!(r["distance_mm"].as_f64().unwrap() < 1e-9);
assert!((r["angle_deg"].as_f64().unwrap() - 90.0).abs() < 1e-6);
+ for k in ["from", "to"] {
+ assert!(serde_json::from_value::<Vec3>(r[k].clone()).is_ok_and(|p| p.is_finite()), "{k} in {r}");
+ }
let r = run(&mut s, "inspect.measure", json!({"items": [face(ft, [20.0, 15.0, 20.0])]}));
assert!((r["items"][0]["area_mm2"].as_f64().unwrap() - 1200.0).abs() < 1e-6, "{r}");
let v = json!({"type": "vertex", "body": "Body1", "point": [0, 0, 0]});
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 518cc18ef5..dd512bd541 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1501,11 +1501,11 @@ fn measure_line(
for s in [sa, sb] {
painter.circle(s, 3.5, t.manipulator, Stroke::new(1.0, Color32::WHITE));
}
- pick_numbers(painter, sa, sb);
let mid = sa + (sb - sa) * 0.5;
let galley = painter.layout_no_wrap(crate::prefs::show_mm(app, d, 1), egui::FontId::proportional(crate::theme::Tokens::get().font_label), t.text);
let own = egui::Rect::from_min_size(mid + vec2(10.0, -14.0), galley.size() + vec2(10.0, 6.0));
let rect = moved_label(proj, own, labels.offset(0));
+ pick_numbers(painter, sa, sb, rect);
crate::scenario::publish_target("measure:label", rect);
if app.preferences.measure_axis_distances {
measure_components(app, ui, painter, proj, a, b, rect, labels);
@@ -1519,23 +1519,46 @@ fn measure_line(
}
/// "1" and "2" beside the measured line's ends (#6020), as the panel lists the picks: each just
-/// past its end, away from the line and its length in the middle.
-fn pick_numbers(painter: &egui::Painter, sa: Pos2, sb: Pos2) {
+/// past its end, away from the line and from the distance `label`.
+fn pick_numbers(painter: &egui::Painter, sa: Pos2, sb: Pos2, label: egui::Rect) {
let t = Tokens::get();
- let along = (sb - sa).normalized();
- // Ends on one spot (or one behind the other): side by side above it.
- let (out_a, out_b) = if along.is_finite() && along.length_sq() > 0.5 { (-along, along) } else { (vec2(-0.7, -0.7), vec2(0.7, -0.7)) };
- for (n, at, out) in [("1", sa, out_a), ("2", sb, out_b)] {
- let galley = painter.layout_no_wrap(n.to_string(), egui::FontId::proportional(t.font_label), t.text);
- let size = galley.size() + vec2(8.0, 4.0);
- let side = size.x.max(size.y);
- let rect = egui::Rect::from_center_size(at + out * (side * 0.5 + 8.0), vec2(side, size.y));
+ let galleys = ["1", "2"].map(|n| painter.layout_no_wrap(n.to_string(), egui::FontId::proportional(t.font_label), t.text));
+ let size = galleys.iter().map(|g| g.size()).fold(egui::Vec2::ZERO, |a, b| a.max(b)) + vec2(8.0, 4.0);
+ let rects = pick_number_rects(sa, sb, size, label);
+ for ((n, galley), rect) in ["1", "2"].into_iter().zip(galleys).zip(rects) {
painter.rect(rect, t.radius_sm.saturating_sub(1), t.overlay, Stroke::new(1.0, t.manipulator), egui::StrokeKind::Inside);
painter.galley(rect.center() - galley.size() * 0.5, galley, t.text);
crate::scenario::publish_target(&format!("measure:item:{n}"), rect);
}
}
+/// Where "1" and "2" go, each `size` (text and padding): just past its end of the line, away
+/// from it. Where that would cover the distance `label` or the other number (ends on one spot or
+/// a few pixels apart, as two faces measured at their shared corner, #11833), the first free spot
+/// around its end instead, so the two never hide each other.
+fn pick_number_rects(sa: Pos2, sb: Pos2, size: egui::Vec2, label: egui::Rect) -> [egui::Rect; 2] {
+ let side = size.x.max(size.y);
+ let reach = side * 0.5 + 8.0;
+ let along = (sb - sa).normalized();
+ // Ends on one spot (or one behind the other): side by side above it.
+ let (out_a, out_b) = if along.is_finite() && along.length_sq() > 0.5 { (-along, along) } else { (vec2(-0.7, -0.7), vec2(0.7, -0.7)) };
+ // Below, left, then the other ways round; the distance label sits up and to the right.
+ let around =
+ [vec2(0.0, 1.0), vec2(-1.0, 0.0), vec2(-0.7, 0.7), vec2(0.0, -1.0), vec2(-0.7, -0.7), vec2(1.0, 0.0), vec2(0.7, 0.7), vec2(0.7, -0.7)];
+ let place = |at: Pos2, out: egui::Vec2, taken: Option<egui::Rect>| {
+ let towards = |d: egui::Vec2| egui::Rect::from_center_size(at + d * reach, vec2(side, size.y));
+ let first = towards(out);
+ std::iter::once(first)
+ .chain(around.iter().map(|d| towards(*d)))
+ .chain(around.iter().map(|d| towards(*d * 2.0)))
+ .find(|r| !r.intersects(label) && taken.is_none_or(|t| !r.intersects(t)))
+ .unwrap_or(first)
+ };
+ let a = place(sa, out_a, None);
+ let b = place(sb, out_b, Some(a));
+ [a, b]
+}
+
/// A world-axis-aligned chain between the same two measured points. Zero-length and
/// view-aligned legs still get a label; labels move apart when their projections coincide, and
/// the user can drag each away (`labels` 1 to 3).
@@ -2065,6 +2088,34 @@ fn arrow(painter: &egui::Painter, a: Pos2, b: Pos2, col: Color32) {
mod tests {
use super::*;
+ /// "1" and "2" never cover each other or the distance (#11833): ends on one spot (two faces
+ /// measured at their shared corner, a distance of 0), a few pixels apart, and a line long
+ /// enough that both keep their own place past their ends.
+ #[test]
+ fn pick_numbers_stay_apart_and_off_the_distance() {
+ let size = vec2(16.0, 18.0);
+ let label_at = |sa: Pos2, sb: Pos2| egui::Rect::from_min_size(sa + (sb - sa) * 0.5 + vec2(10.0, -14.0), vec2(70.0, 22.0));
+ let corner = egui::pos2(400.0, 300.0);
+ let mut cases = vec![(corner, corner)];
+ for d in [vec2(1.0, 0.0), vec2(0.5, -0.5), vec2(3.0, 2.0), vec2(-2.0, -4.0), vec2(6.0, 0.0), vec2(0.0, 7.0)] {
+ cases.push((corner, corner + d));
+ }
+ for (sa, sb) in cases {
+ let label = label_at(sa, sb);
+ let [a, b] = pick_number_rects(sa, sb, size, label);
+ assert!(!a.intersects(b), "{sa:?} {sb:?}: 1 {a:?} and 2 {b:?} overlap");
+ for (n, r, own) in [("1", a, sa), ("2", b, sb)] {
+ assert!(!r.intersects(label), "{sa:?} {sb:?}: {n} {r:?} covers the distance {label:?}");
+ assert!(r.center().distance(own) < 80.0, "{sa:?} {sb:?}: {n} strays from its end");
+ }
+ }
+ // A long line: each number just past its own end, as before.
+ let (sa, sb) = (egui::pos2(100.0, 400.0), egui::pos2(300.0, 200.0));
+ let [a, b] = pick_number_rects(sa, sb, size, label_at(sa, sb));
+ let along = (sb - sa).normalized();
+ assert!((a.center() - (sa - along * 17.0)).length() < 1e-3 && (b.center() - (sb + along * 17.0)).length() < 1e-3, "{a:?} {b:?}");
+ }
+
/// The arrow's point on a picked edge: one point, a zero-length piece, a click past the end.
#[test]
fn nearest_on_polyline_handles_short_edges_and_clicks_past_the_end() {