Measure: datum planes, axes and points, at exact distances (#10157)
* Measure: datum planes, axes and points, at exact distances The Measure tool took faces, edges, vertices and sketch geometry; a datum plane or axis could not be picked into it, and a point set of an infinite plane or axis would measure the wrong thing anyway. Describe origin and construction planes, construction axes and construction points as items, accept PLANES, AXES and CONSTRUCTION_POINTS (and the origin) in the Measure dialog, and give the new shapes exact closest points: - a point and a plane: the perpendicular distance; - a point and a line: the perpendicular distance; - two planes: parallel distance, or zero where they meet; - a plane and a line: zero where they cross; - two lines: the skew distance. The panel and the quick readout show a plane's point and normal, and an axis's point and direction. Test: a point 10 above the XY plane, 5 from the Z axis, and a plane and an axis that meet. * Select: a straight sketch line is a curve in an input that takes curves Adding axes to Measure's input made a straight sketch line match the "axis" rule before the sketch-line rule, so a picked external sketch curve became a SketchCurve instead of the ExternalSketchCurve Measure records. The axis rule now applies only to inputs that take axes on their own (Move's direction and the like), not to inputs that also take curves. Test: measure_input_fits_external_sketch_geometry, and the Move axis picks. * rustfmt: format the new selection filter and measure code * Measure: drop an unused normalisation in the skew-line distance --------- Co-authored-by: clarkliu <277482660@qq.com>
This commit is contained in:
@@ -286,6 +286,31 @@ fn describe(s: &Session, sel: &Sel) -> Option<Item> {
|
||||
let st = s.world_state();
|
||||
let plain = |geo, dir, json| Some(Item { geo, dir, json, shape: Shape::Other, slot: None });
|
||||
match sel {
|
||||
Sel::Plane { name } => {
|
||||
let (point, normal) = plane_named(s, name)?;
|
||||
Some(Item {
|
||||
geo: Geo::Points(vec![point]),
|
||||
dir: Some(normal),
|
||||
json: json!({"type": "plane", "point": point, "normal": normal}),
|
||||
shape: Shape::Plane { point, normal: UnitVec3::new(normal)? },
|
||||
slot: None,
|
||||
})
|
||||
}
|
||||
Sel::Axis { name } => {
|
||||
let (origin, dir) = axis_named(s, name)?;
|
||||
let (a, b) = (origin - dir * 1e6, origin + dir * 1e6);
|
||||
Some(Item {
|
||||
geo: Geo::Points(vec![origin, a, b]),
|
||||
dir: Some(dir),
|
||||
json: json!({"type": "axis", "origin": origin, "direction": dir}),
|
||||
shape: Shape::Line { a, b },
|
||||
slot: None,
|
||||
})
|
||||
}
|
||||
Sel::ConstructionPoint { name } => {
|
||||
let p = crate::view::construction_points(s).into_iter().find(|(_, n, _)| n == name).map(|(_, _, p)| p)?;
|
||||
Some(Item { geo: Geo::Points(vec![p]), dir: None, json: json!({"type": "point", "position": p}), shape: Shape::Point(p), slot: None })
|
||||
}
|
||||
Sel::Vertex { point, .. } => Some(Item {
|
||||
geo: Geo::Points(vec![*point]),
|
||||
dir: None,
|
||||
@@ -397,6 +422,122 @@ fn sample<T: Copy>(v: &[T], max: usize) -> Vec<T> {
|
||||
}
|
||||
|
||||
/// Closest pair of points between two pieces of geometry.
|
||||
/// An origin plane (XY, XZ, YZ) or a construction plane's point and normal.
|
||||
fn plane_named(s: &Session, name: &str) -> Option<(Vec3, Vec3)> {
|
||||
match name {
|
||||
"XY" => Some((Vec3::ZERO, Vec3::Z)),
|
||||
"XZ" => Some((Vec3::ZERO, Vec3::Y)),
|
||||
"YZ" => Some((Vec3::ZERO, Vec3::X)),
|
||||
_ => crate::view::construction_planes(s).into_iter().find(|(_, n, _)| n == name).map(|(_, _, p)| (p.origin, p.x.cross(p.y))),
|
||||
}
|
||||
}
|
||||
|
||||
/// An origin axis (X, Y, Z) or a construction axis's point and unit direction.
|
||||
fn axis_named(s: &Session, name: &str) -> Option<(Vec3, Vec3)> {
|
||||
match name {
|
||||
"X" => Some((Vec3::ZERO, Vec3::X)),
|
||||
"Y" => Some((Vec3::ZERO, Vec3::Y)),
|
||||
"Z" => Some((Vec3::ZERO, Vec3::Z)),
|
||||
_ => crate::view::construction_axes(s).into_iter().find(|(_, n, _, _)| n == name).map(|(_, _, origin, dir)| (origin, dir)),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)> {
|
||||
use Shape::*;
|
||||
let to_plane = |p: Vec3, point: Vec3, normal: UnitVec3| {
|
||||
let n = normal.vec();
|
||||
let h = (p - point).dot(n);
|
||||
(h.abs(), p, p - n * h)
|
||||
};
|
||||
let to_line = |p: Vec3, a: Vec3, b: Vec3| {
|
||||
let d = b - a;
|
||||
let t = if d.len() > 1e-12 { ((p - a).dot(d) / d.dot(d)).clamp(0.0, 1.0) } else { 0.0 };
|
||||
let q = a + d * t;
|
||||
(p.dist(q), p, q)
|
||||
};
|
||||
match (a, b) {
|
||||
(Point(p), Plane { point, normal }) => Some(to_plane(*p, *point, *normal)),
|
||||
(Plane { point, normal }, Point(p)) => {
|
||||
let (d, f, t) = to_plane(*p, *point, *normal);
|
||||
Some((d, t, f))
|
||||
}
|
||||
(Point(p), Line { a: la, b: lb }) => Some(to_line(*p, *la, *lb)),
|
||||
(Line { a: la, b: lb }, Point(p)) => {
|
||||
let (d, f, t) = to_line(*p, *la, *lb);
|
||||
Some((d, t, f))
|
||||
}
|
||||
(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))
|
||||
}
|
||||
}
|
||||
(Plane { point, normal }, Line { a: la, b: lb }) => {
|
||||
let d = *lb - *la;
|
||||
let den = d.dot(normal.vec());
|
||||
if den.abs() < 1e-9 {
|
||||
let (dist, _, q) = {
|
||||
let h = (*la - *point).dot(normal.vec());
|
||||
(h.abs(), *la, *la - normal.vec() * h)
|
||||
};
|
||||
Some((dist, q, *la))
|
||||
} else {
|
||||
let t = (*point - *la).dot(normal.vec()) / den;
|
||||
let p = *la + d * t;
|
||||
Some((0.0, p, p))
|
||||
}
|
||||
}
|
||||
(Line { a: la, b: lb }, Plane { point, normal }) => {
|
||||
let d = *lb - *la;
|
||||
let den = d.dot(normal.vec());
|
||||
if den.abs() < 1e-9 {
|
||||
let h = (*la - *point).dot(normal.vec());
|
||||
Some((h.abs(), *la, *la - normal.vec() * h))
|
||||
} else {
|
||||
let t = (*point - *la).dot(normal.vec()) / den;
|
||||
let p = *la + d * t;
|
||||
Some((0.0, p, p))
|
||||
}
|
||||
}
|
||||
(Line { a: a1, b: b1 }, Line { a: a2, b: b2 }) => {
|
||||
let (d1, d2) = (*b1 - *a1, *b2 - *a2);
|
||||
let n = d1.cross(d2);
|
||||
if n.len() < 1e-9 {
|
||||
let (dist, _, q) = {
|
||||
let dd = d1;
|
||||
let t = if dd.len() > 1e-12 { ((*a2 - *a1).dot(dd) / dd.dot(dd)).clamp(0.0, 1.0) } else { 0.0 };
|
||||
let q = *a1 + dd * t;
|
||||
((*a2).dist(q), *a2, q)
|
||||
};
|
||||
Some((dist, q, *a2))
|
||||
} else {
|
||||
let w = *a1 - *a2;
|
||||
let a = d1.dot(d1);
|
||||
let b = d1.dot(d2);
|
||||
let c = d2.dot(d2);
|
||||
let d = d1.dot(w);
|
||||
let e = d2.dot(w);
|
||||
let den = a * c - b * b;
|
||||
let (t, s) = if den.abs() < 1e-12 { (0.0, e / c) } else { ((b * e - c * d) / den, (a * e - b * d) / den) };
|
||||
let p = *a1 + d1 * t;
|
||||
let q = *a2 + d2 * s;
|
||||
Some((p.dist(q), p, q))
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn closest(a: &Geo, b: &Geo) -> Option<(Vec3, Vec3)> {
|
||||
let pts_to_tris = |pts: &[Vec3], tris: &[[Vec3; 3]]| -> Option<(Vec3, Vec3)> {
|
||||
let pts = sample(pts, MAX_POINTS);
|
||||
@@ -555,6 +696,13 @@ 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) {
|
||||
out["distance_mm"] = json!(d);
|
||||
out["from"] = json!(p);
|
||||
out["to"] = json!(q);
|
||||
out["delta"] = json!(q - p);
|
||||
out["min"] = json!(d);
|
||||
}
|
||||
if let (Some((p, q)), Some(d)) = (far, max) {
|
||||
out["max"] = json!(d);
|
||||
out["max_from"] = json!(p);
|
||||
|
||||
@@ -1617,6 +1617,23 @@ fn auto_extrude_starts_new_body_in_empty_component() {
|
||||
}
|
||||
|
||||
/// Measuring picked geometry: distance between faces, angle, edge length.
|
||||
#[test]
|
||||
fn measure_origin_planes_axes_and_points() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 20}));
|
||||
let at = |p: [f64; 3]| json!({"type": "vertex", "body": "Body1", "point": p});
|
||||
// A point 10 above the XY plane.
|
||||
let r = run(&mut s, "inspect.measure", json!({"items": [{"type": "plane", "name": "XY"}, at([5.0, 5.0, 10.0])]}));
|
||||
assert!((r["distance_mm"].as_f64().unwrap() - 10.0).abs() < 1e-9, "{r}");
|
||||
assert_eq!(r["from"], json!([5.0, 5.0, 0.0]));
|
||||
// The point's distance from the Z axis.
|
||||
let r = run(&mut s, "inspect.measure", json!({"items": [{"type": "axis", "name": "Z"}, at([3.0, 4.0, 7.0])]}));
|
||||
assert!((r["distance_mm"].as_f64().unwrap() - 5.0).abs() < 1e-9, "{r}");
|
||||
// The XY plane and the Z axis meet: nothing between them.
|
||||
let r = run(&mut s, "inspect.measure", json!({"items": [{"type": "plane", "name": "XY"}, {"type": "axis", "name": "Z"}]}));
|
||||
assert!(r["distance_mm"].as_f64().unwrap().abs() < 1e-9, "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn measure_items() {
|
||||
let mut s = Session::default();
|
||||
|
||||
@@ -943,7 +943,11 @@ impl Dialog {
|
||||
| selection::POINTS
|
||||
| PROFILES
|
||||
| selection::SKETCH_LINES
|
||||
| selection::EXTERNAL_POINTS,
|
||||
| selection::EXTERNAL_POINTS
|
||||
| PLANES
|
||||
| AXES
|
||||
| selection::CONSTRUCTION_POINTS
|
||||
| selection::ORIGIN,
|
||||
true,
|
||||
)],
|
||||
),
|
||||
|
||||
@@ -139,6 +139,22 @@ fn measure_dialog_shows_the_obtuse_corner_of_selected_lines() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A datum plane and the origin's axis measure against points: the shapes run on, so the
|
||||
/// distances are exact rather than sampled.
|
||||
#[test]
|
||||
fn a_datum_plane_and_a_point_measure_their_distance() {
|
||||
let mut h = Harness::new();
|
||||
h.app.home.open = false;
|
||||
h.app.set_origin_shown(false);
|
||||
h.app.ui.show_browser = false;
|
||||
h.app.run("solid.box", json!({"length":40,"width":30,"height":20,"body_name":"Box"})).unwrap();
|
||||
h.app.session.selection = vec![Sel::Plane { name: "XY".into() }, Sel::Vertex { body: "Box".into(), point: [5.0, 5.0, 10.0].into() }];
|
||||
h.app.start("inspect.measure");
|
||||
h.settle();
|
||||
let d = result(&h)["distance_mm"].as_f64().unwrap();
|
||||
assert!((d - 10.0).abs() < 1e-9, "{d}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn measurement_axis_distances_follow_the_upstream_centre_min_and_max_choices() {
|
||||
let mut h = Harness::new();
|
||||
|
||||
@@ -151,10 +151,22 @@ pub(crate) fn item_lines(app: &SolveApp, it: &Value) -> Vec<String> {
|
||||
match it["type"].as_str() {
|
||||
Some("edge" | "sketch_curve") if !round => lines.push(format!("{} {}", tr("Length"), mm(&it["length_mm"]))),
|
||||
Some("face" | "profile") => lines.push(format!("{} {}", tr("Area"), crate::prefs::show_mm(app, it["area_mm2"].as_f64().unwrap_or(0.0), 2))),
|
||||
Some("vertex" | "sketch_point") => {
|
||||
Some("vertex" | "sketch_point" | "point") => {
|
||||
let p = &it["position"];
|
||||
lines.push(format!("{} {}, {}, {}", tr("measure.at"), mm(&p[0]), mm(&p[1]), mm(&p[2])));
|
||||
}
|
||||
Some("plane") => {
|
||||
let p = &it["point"];
|
||||
lines.push(format!("{} {}, {}, {}", tr("measure.at"), mm(&p[0]), mm(&p[1]), mm(&p[2])));
|
||||
let n = &it["normal"];
|
||||
lines.push(format!("Normal {}, {}, {}", mm(&n[0]), mm(&n[1]), mm(&n[2])));
|
||||
}
|
||||
Some("axis") => {
|
||||
let p = &it["origin"];
|
||||
lines.push(format!("{} {}, {}, {}", tr("measure.at"), mm(&p[0]), mm(&p[1]), mm(&p[2])));
|
||||
let d = &it["direction"];
|
||||
lines.push(format!("Direction {}, {}, {}", mm(&d[0]), mm(&d[1]), mm(&d[2])));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
lines
|
||||
|
||||
@@ -126,7 +126,9 @@ impl SelInput {
|
||||
Hit::Plane { name, .. } if a & PLANES != 0 => Some(Sel::Plane { name: name.clone() }),
|
||||
Hit::Axis { name } if a & AXES != 0 => Some(Sel::Axis { name: name.clone() }),
|
||||
Hit::ConstructionPoint { name } if a & CONSTRUCTION_POINTS != 0 => Some(Sel::ConstructionPoint { name: name.clone() }),
|
||||
Hit::SketchCurve { sketch, id, straight: true, .. } if a & AXES != 0 => Some(curve_pick(id, *sketch, active)),
|
||||
// A straight sketch line stands for an axis only in an input that takes axes for
|
||||
// what they are: an input that takes curves too (Measure) takes the line as its curve.
|
||||
Hit::SketchCurve { sketch, id, straight: true, .. } if a & AXES != 0 && a & CURVES == 0 => Some(curve_pick(id, *sketch, active)),
|
||||
Hit::SketchCurve { sketch, id, .. } if a & SKETCH_LINES != 0 => Some(Sel::ExternalSketchCurve { sketch: *sketch, id: id.clone() }),
|
||||
Hit::SketchCurve { sketch, id, .. } if a & CURVES != 0 => Some(curve_pick(id, *sketch, active)),
|
||||
Hit::SketchPoint { sketch, id, .. } if a & EXTERNAL_POINTS != 0 => Some(Sel::ExternalSketchPoint { sketch: *sketch, id: id.clone() }),
|
||||
|
||||
Reference in New Issue
Block a user