Skip to content

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

Select Other: list what is under the pointer front to back, one body at a time (#5345) (#5509)

HiroKenEzf49cd33db134
diff --git a/crates/ui-egui/src/select_other.rs b/crates/ui-egui/src/select_other.rs
index 60271ad7c8..7176139803 100644
--- a/crates/ui-egui/src/select_other.rs
+++ b/crates/ui-egui/src/select_other.rs
@@ -4,11 +4,12 @@
//!
//! - Holding the left button still (or a finger, on touch screens) for [`HOLD_SECS`], or the
//! viewport menu's "Select Other…", opens a list at the cursor of every pickable thing there,
-//! in the selection filter's and the open dialog's terms: sketch points and curves first, then
-//! vertices, edges, faces, bodies, profiles, planes and axes, each kind nearest first. On a
-//! dense mesh (a modelled thread, an imported mesh) thousands of facets can share one cursor,
-//! so at most [`MAX_PER_KIND`] rows are kept per kind, nearest first, and the list reports how
-//! many things there were in all.
+//! in the selection filter's and the open dialog's terms, front to back like layers: sketch
+//! points and curves first (drawn over the model), then what belongs to a body, one body at a
+//! time and the nearest body first (its vertices, edges, face, then the body itself), then
+//! profiles, planes and axes. On a dense mesh (a modelled thread, an imported mesh) thousands
+//! of facets can share one cursor, so at most [`MAX_PER_KIND`] rows are kept per kind, nearest
+//! first, and the list reports how many things there were in all.
//! - Hovering a row lights its entity up in the viewport; clicking it selects it (Shift/Ctrl add,
//! as clicks do, held then or at the click or press that opened the list) or, with a dialog
//! open, gives it to the dialog's active selection input.
@@ -90,7 +91,8 @@ pub struct State {
// What is under the cursor
// ---------------------------------------------------------------------------------------------
-/// Every pickable thing under `pos`, ordered by kind (the sketch being edited first), then depth.
+/// Every pickable thing under `pos`, front to back (the sketch being edited first): see
+/// [`order`].
pub fn rows(app: &SolveApp, proj: &Proj, pos: Pos2) -> Vec<Row> {
rows_capped(app, proj, pos).0
}
@@ -118,7 +120,7 @@ fn rows_capped(app: &SolveApp, proj: &Proj, pos: Pos2) -> (Vec<Row>, usize) {
}
}
let sketching = app.session.active_sketch.is_some();
- out.sort_by_key(|(h, s)| rank(&shown_as(h, s), sketching));
+ let out = order(app, proj, pos, out, sketching);
let total = out.len();
// Nearest first within a kind, so capping drops the farther ones.
let mut per_kind: std::collections::HashMap<u8, usize> = std::collections::HashMap::new();
@@ -141,6 +143,81 @@ fn rows_capped(app: &SolveApp, proj: &Proj, pos: Pos2) -> (Vec<Row>, usize) {
(rows, total)
}
+/// The list's order, front to back like layers (#5345). Sketch points and curves come first
+/// (drawn over the model, the sketch being edited before the rest), then each body's things
+/// together, bodies by their nearest hit along the pick ray; within a body by kind (vertices,
+/// edges, its face, the body), each kind keeping the pick's nearest-first order. Profiles,
+/// planes and axes follow. The sort is stable.
+fn order(app: &SolveApp, proj: &Proj, pos: Pos2, found: Vec<(Hit, Sel)>, sketching: bool) -> Vec<(Hit, Sel)> {
+ let (o, d) = proj.ray(pos);
+ let st = crate::open_meshing::shown_world(app);
+ let index: std::collections::HashMap<&str, usize> = st.bodies.iter().enumerate().map(|(i, b)| (b.name.as_str(), i)).collect();
+ let mut nearest: std::collections::HashMap<String, f64> = std::collections::HashMap::new();
+ for (h, _) in &found {
+ if let (Some(body), Some(t)) = (body_of(h), hit_depth(&st, &index, o, d, h)) {
+ let e = nearest.entry(body.to_string()).or_insert(t);
+ *e = e.min(t);
+ }
+ }
+ let mut keyed: Vec<((u8, u8, f64, u8), (Hit, Sel))> = found
+ .into_iter()
+ .map(|(h, s)| {
+ let (own, kind) = rank(&shown_as(&h, &s), sketching);
+ let key = match body_of(&h) {
+ Some(body) => (own, 1, nearest.get(body).copied().unwrap_or(f64::INFINITY), kind),
+ None => (own, if kind <= 3 { 0 } else { 2 }, 0.0, kind),
+ };
+ (key, (h, s))
+ })
+ .collect();
+ keyed.sort_by(|(a, _), (b, _)| (a.0, a.1).cmp(&(b.0, b.1)).then(a.2.total_cmp(&b.2)).then(a.3.cmp(&b.3)));
+ keyed.into_iter().map(|(_, x)| x).collect()
+}
+
+/// The body a hit lies on (the origin point, a vertex of no body, has none).
+fn body_of(h: &Hit) -> Option<&str> {
+ match h {
+ Hit::Vertex { body, .. } | Hit::Edge { body, .. } | Hit::Face { body, .. } if !body.is_empty() => Some(body),
+ _ => None,
+ }
+}
+
+/// How far along the pick ray (`o`, `d`) a body's hit lies: a face's or vertex's point, or an
+/// edge's point nearest the ray (its middle can lie much deeper or nearer).
+fn hit_depth(
+ st: &solidcraft_engine::doc::ModelState,
+ index: &std::collections::HashMap<&str, usize>,
+ o: solidcraft_engine::geom::Vec3,
+ d: solidcraft_engine::geom::Vec3,
+ h: &Hit,
+) -> Option<f64> {
+ let along = |p: solidcraft_engine::geom::Vec3| Some((p - o).dot(d)).filter(|t| t.is_finite());
+ match h {
+ Hit::Vertex { point, .. } | Hit::Face { point, .. } => along(*point),
+ Hit::Edge { body, index: ei, mid } => {
+ let under = index.get(body.as_str()).and_then(|i| st.bodies.get(*i)).and_then(|b| {
+ let (m, place) = b.mesh_in_place();
+ let at = |p: solidcraft_engine::geom::Vec3| solidcraft_engine::doc::apply_point(&place, p);
+ let dd = d.dot(d);
+ m.edges
+ .get(*ei)?
+ .windows(2)
+ .map(|w| crate::viewport::ray_segment_closest(o, d, at(w[0]), at(w[1])))
+ .map(|q| {
+ let t = (q - o).dot(d);
+ // Squared distance from the ray, to take the segment under the cursor.
+ ((q - o).dot(q - o) - t * t / dd, t)
+ })
+ .filter(|(off, t)| off.is_finite() && t.is_finite())
+ .min_by(|a, b| a.0.total_cmp(&b.0))
+ .map(|(_, t)| t)
+ });
+ under.or_else(|| along(*mid))
+ }
+ _ => None,
+ }
+}
+
/// What a row shows itself as: a face place a sketch point snapped is that point (ranked, capped
/// and named with the points).
fn shown_as(hit: &Hit, sel: &Sel) -> Sel {
diff --git a/crates/ui-egui/src/select_other_tests.rs b/crates/ui-egui/src/select_other_tests.rs
index 4831003115..50fda5df46 100644
--- a/crates/ui-egui/src/select_other_tests.rs
+++ b/crates/ui-egui/src/select_other_tests.rs
@@ -413,3 +413,57 @@ fn a_click_picks_the_sketchs_own_curve_over_a_projected_one_under_it() {
h.step(&json!({"click": {"handle": format!("select_other:{}", row_of(&h, &projected))}})).unwrap();
assert_eq!(curves(&h), [projected]);
}
+
+/// Three plates stacked along Z (made bottom, top, middle, so model order isn't depth order),
+/// the top one smallest, held over the top plate's edge from above.
+fn stacked_plates() -> Harness {
+ let mut h = Harness::new();
+ h.app.set_origin_shown(false);
+ h.app.run("solid.box", json!({"length":30,"width":30,"height":2,"corner":[-15,-15,0],"body_name":"Bottom"})).unwrap();
+ h.app.run("solid.box", json!({"length":10,"width":10,"height":2,"corner":[-5,-5,20],"body_name":"Top"})).unwrap();
+ h.app.run("solid.box", json!({"length":20,"width":20,"height":2,"corner":[-10,-10,10],"body_name":"Middle"})).unwrap();
+ h.call("ui.view", json!({"view":"top"}));
+ h.call("ui.view", json!({"view":"fit"}));
+ h
+}
+
+#[test]
+fn stacked_bodies_list_front_to_back_one_body_at_a_time() {
+ let mut h = stacked_plates();
+ h.step(&json!({"hold": {"world": [4.9, 0, 22]}})).unwrap();
+ let rows = labels(&h);
+ // Each row's body, in list order: one body at a time, the nearest first (#5345).
+ let bodies: Vec<&str> = rows.iter().filter_map(|r| r.rsplit(" · ").next()).collect();
+ let mut runs = bodies.clone();
+ runs.dedup();
+ assert_eq!(runs, ["Top", "Middle", "Bottom"], "front to back, each body's rows together: {rows:?}");
+ // Within a body: the edge under the pointer, then its face, then the body itself.
+ assert!(rows[0].starts_with("Edge ") && rows[0].ends_with("· Top"), "{rows:?}");
+ for body in ["Top", "Middle", "Bottom"] {
+ let mine: Vec<&String> = rows.iter().filter(|r| r.ends_with(&format!("· {body}"))).collect();
+ assert!(mine.iter().any(|r| r.starts_with("Face ")), "{body}: {rows:?}");
+ assert_eq!(mine.last().map(|r| r.as_str()), Some(format!("Body · {body}").as_str()), "{rows:?}");
+ }
+}
+
+#[test]
+fn an_edge_is_as_deep_as_its_point_under_the_pointer_not_its_middle() {
+ let mut h = stacked_plates();
+ h.call("ui.view", json!({"view":"iso"}));
+ h.call("ui.view", json!({"view":"fit"}));
+ h.settle();
+ // On the bottom plate's near top edge along Y (x = 15, y from -15 to 15), well off its middle.
+ let under = solidcraft_engine::geom::Vec3::new(15.0, 9.0, 2.0);
+ let at = h.call("ui.at", json!({"world": [under.x, under.y, under.z]}))["result"].clone();
+ let pos = egui::pos2(at[0].as_f64().unwrap() as f32, at[1].as_f64().unwrap() as f32);
+ let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
+ let hits = crate::viewport::pick_all(&h.app, &proj, pos);
+ let edge = hits.iter().find(|x| matches!(x, crate::viewport::Hit::Edge { body, .. } if body == "Bottom")).expect("the edge");
+ let (o, d) = proj.ray(pos);
+ let st = crate::open_meshing::shown_world(&h.app);
+ let index = st.bodies.iter().enumerate().map(|(i, b)| (b.name.as_str(), i)).collect();
+ let t = super::hit_depth(&st, &index, o, d, edge).expect("a depth");
+ let want = (under - o).dot(d);
+ let mid = solidcraft_engine::geom::Vec3::new(15.0, 0.0, 2.0);
+ assert!((t - want).abs() < 0.5, "depth {t}, the point under the pointer {want}, its middle {}", (mid - o).dot(d));
+}