Pick by the body's own size, not the camera's distance (#7919) (#7920)

* claim: #7919

* Pick by the body's own size, not the camera's distance (#7919)

The occlusion test that decides whether an edge or vertex is hidden by the
face in front allowed a slack of `scene_radius(app) * 0.02`, and
`scene_radius` ends with `.max(app.cam.half_height() * 2.0)`: in an
orthographic view that is `0.016 x camera distance`. Zooming out grew the
slack without bound until a whole body fitted inside it and every edge behind
the front face counted as visible, and edges and vertices are offered before
faces, so they won the pick. Cutting the slack from the scene also gave a
small part in a large assembly a tolerance many times its own size.

`pick_slack` now takes a size and returns the mesh's own chord tolerance with
room to spare, `DisplayQuality::Coarse.chord(size) * 4.0`: a facet sits at
most a chord tolerance inside its surface, and that tolerance is already a
fraction of the body's size, so the slack no longer moves when the camera
does. Each body is tested with its own bounding box, which the loop already
holds. `view.pick` does the same, so an agent's pick still matches a click on
the same pixel. Sketch profiles and origin planes belong to no body and keep
a scene-level value, `scene_pick_slack`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
N3MI-DG
2026-10-28 23:15:09 +00:00
committed by GitHub
parent 17807ab6dc
commit 33c27106ff
3 changed files with 99 additions and 18 deletions
+53
View File
@@ -562,3 +562,56 @@ fn a_press_that_opened_the_list_does_not_move_the_view_when_it_moves_on() {
assert_eq!(h.app.cam, cam, "{slot}: the view did not move under the open list");
}
}
/// Zooming out does not make a click reach through the body. The pixel of the box's hidden far
/// corner is on the near +X face, so every distance picks something standing on that face —
/// the face, or one of its edges or corners once the box is small on screen — never the far
/// side behind it. The occlusion slack used to be cut from the camera's distance, so far
/// enough out the whole body fitted inside it.
#[test]
fn a_zoomed_out_click_picks_the_near_face_not_an_edge_through_the_body() {
let mut h = Harness::new();
h.app.set_origin_shown(false);
h.app.run("solid.box", json!({"length":10,"width":10,"height":10,"corner":[-5,-5,-5]})).unwrap();
for distance in [40.0, 300.0, 1000.0, 3000.0] {
h.app.cam.distance = distance;
h.settle();
let at = h.at(&json!({"world": [0, 5, -5]})).unwrap();
let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
let hits = crate::viewport::pick(&h.app, &proj, egui::pos2(at[0] as f32, at[1] as f32));
let first = hits.first().expect("the box is under the cursor");
let point = match first {
crate::viewport::Hit::Face { point, .. } | crate::viewport::Hit::Vertex { point, .. } => *point,
crate::viewport::Hit::Edge { mid, .. } => *mid,
other => panic!("at {distance} mm away the box is not picked: {other:?}"),
};
assert!(point.x > 4.9, "at {distance} mm away the pick is behind the near face: {first:?}");
}
}
/// A small part on a large one is picked by its own size, not the assembly's: the 2 mm box's
/// far corner is 2 mm behind its near face, and a slack cut from the 200 mm plate (or from the
/// camera) reached straight through it — the kraken board's 2.54 mm connectors picked their far
/// side from 5 mm behind.
#[test]
fn a_small_body_on_a_large_one_is_not_picked_through() {
let mut h = Harness::new();
h.app.set_origin_shown(false);
h.app.run("solid.box", json!({"length":200,"width":200,"height":10,"corner":[-100,-100,-10]})).unwrap();
h.app.run("solid.box", json!({"length":2,"width":2,"height":2,"corner":[-1,-1,0]})).unwrap();
for distance in [20.0, 100.0, 600.0] {
h.app.cam.distance = distance;
h.settle();
// The small box's far bottom corner, whose pixel is on its near +X face.
let at = h.at(&json!({"world": [0, 1, 0]})).unwrap();
let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
let hits = crate::viewport::pick(&h.app, &proj, egui::pos2(at[0] as f32, at[1] as f32));
let first = hits.first().expect("the small box is under the cursor");
let point = match first {
crate::viewport::Hit::Face { point, .. } | crate::viewport::Hit::Vertex { point, .. } => *point,
crate::viewport::Hit::Edge { mid, .. } => *mid,
other => panic!("at {distance} mm away the small box is not picked: {other:?}"),
};
assert!(point.x > 0.9, "at {distance} mm away the pick is behind the near face: {first:?}");
}
}
+12 -8
View File
@@ -132,7 +132,7 @@ pub fn pick_command(app: &mut SolveApp, p: &Value) -> Result<Value, String> {
let replaced: &[String] = app.preview.effective_replaced();
let section = crate::viewport::section_plane(app);
let wireframe = app.ui.visual_style == 2;
let slack = crate::viewport::scene_radius(app) * 0.02;
let slack = crate::viewport::scene_pick_slack(app);
let occ = occurrence_map(app);
let comps = component_names(app);
@@ -384,7 +384,9 @@ fn near_edges(
slack: f64,
) -> (Option<EdgeHit>, Option<VertexHit>) {
let reach = crate::pick_index::reach(proj, pos, edge_px.max(6.0));
let visible = |p: Vec3| wireframe || face_t.is_none_or(|t| (p - input.o).dot(input.d) <= t + slack);
// `slack` is the body's own: what counts as lying on the face in front depends on how
// coarsely that body is meshed, never on where the camera stands.
let visible = |p: Vec3, slack: f64| wireframe || face_t.is_none_or(|t| (p - input.o).dot(input.d) <= t + slack);
let mut best_e: Option<EdgeHit> = None;
let mut best_v: Option<VertexHit> = None;
for (i, b) in st.bodies.iter().enumerate() {
@@ -394,10 +396,12 @@ fn near_edges(
if !include_ghosted && crate::viewport::body_faded(app, b, active_bodies) {
continue;
}
if let Some((_, bb)) = bounds.get(i)
&& crate::viewport::screen_box(proj, bb, edge_px).is_some_and(|r| !r.contains(pos))
{
continue;
let mut body_slack = slack;
if let Some((_, bb)) = bounds.get(i) {
if crate::viewport::screen_box(proj, bb, edge_px).is_some_and(|r| !r.contains(pos)) {
continue;
}
body_slack = crate::viewport::pick_slack(bb.diagonal());
}
let (m, place) = b.mesh_in_place();
let Some((lo, ld)) = crate::viewport::local_ray(&place, input.o, input.d) else { continue };
@@ -415,7 +419,7 @@ fn near_edges(
for v in [e.first(), e.last()].into_iter().flatten().map(|v| at(*v)) {
if let Some(sp) = proj.to_screen(v) {
let dd = sp.distance(pos);
if dd <= edge_px + 1.0 && visible(v) && best_v.as_ref().is_none_or(|x| dd < x.dist_px) {
if dd <= edge_px + 1.0 && visible(v, body_slack) && best_v.as_ref().is_none_or(|x| dd < x.dist_px) {
best_v = Some(VertexHit { dist_px: dd, point: v });
}
}
@@ -425,7 +429,7 @@ fn near_edges(
let (Some(a), Some(c)) = (proj.to_screen(p0), proj.to_screen(p1)) else { continue };
let dd = crate::viewport::seg_dist(pos, a, c);
if dd <= edge_px
&& visible(crate::viewport::ray_segment_closest(input.o, input.d, p0, p1))
&& visible(crate::viewport::ray_segment_closest(input.o, input.d, p0, p1), body_slack)
&& best_e.as_ref().is_none_or(|x| dd < x.dist_px)
{
let k = seg_param(pos, a, c);
+34 -10
View File
@@ -365,6 +365,27 @@ pub fn scene_radius(app: &SolveApp) -> f64 {
r.max(app.cam.half_height() * 2.0).max(10.0)
}
/// How far behind the nearest face a point of a body this size may lie and still count as on it,
/// rather than through it: the body's own mesh error, with room to spare. A facet sits at most
/// a chord tolerance inside its surface, and that tolerance is a fraction of the body's size
/// (see [`DisplayQuality::chord`]), so a tolerance taken from the body is the same whatever the
/// camera does. One cut from the scene or the camera instead reached through a small part in a
/// large assembly, and reached further with every step of zoom out.
///
/// [`DisplayQuality::chord`]: solidcraft_engine::doc::display_quality::DisplayQuality::chord
pub(crate) fn pick_slack(size: f64) -> f64 {
use solidcraft_engine::doc::display_quality::DisplayQuality;
// The coarsest level, so the slack covers the mesh at any display quality.
DisplayQuality::Coarse.chord(size) * 4.0
}
/// The slack for the whole scene, for picks that belong to no one body (a sketch profile or a
/// plane against the face it lies on).
pub(crate) fn scene_pick_slack(app: &SolveApp) -> f64 {
let b = scene_bounds(app);
pick_slack(if b.is_empty() { 100.0 } else { b.diagonal() })
}
pub fn projection(app: &SolveApp, rect: Rect) -> Proj {
let mut cam = app.cam;
// Render owns an explicit product camera, including its saved/custom FOV.
@@ -2351,10 +2372,11 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
faces.v.retain(|(t, _)| *t < t_cap);
}
let hidden_from = cap.or(bestf.as_ref().map(|(t, _)| *t));
let slack = scene_radius(app) * 0.02;
// Wireframe draws no faces, so nothing hides an edge or a vertex.
let slack = scene_pick_slack(app);
// Wireframe draws no faces, so nothing hides an edge or a vertex. `slack` is the body's own:
// what counts as lying on the face in front depends on how coarsely that body is meshed.
let wireframe = app.ui.visual_style == 2;
let visible = |p: Vec3| kept(p) && (wireframe || hidden_from.is_none_or(|t| (p - o).dot(d) <= t + slack));
let visible = |p: Vec3, slack: f64| kept(p) && (wireframe || hidden_from.is_none_or(|t| (p - o).dot(d) <= t + slack));
// Vertices (edge end points) and edges, within 7 and 6 points of the cursor.
let reach = crate::pick_index::reach(proj, pos, 8.0);
let mut bestv = Near::new(all);
@@ -2363,11 +2385,13 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
if !app.session.body_visible(&b.name) {
continue;
}
// Only bodies whose outline on screen is near the cursor can have an edge under it.
if let Some((_, bb)) = bounds.get(i)
&& screen_box(proj, bb, 8.0).is_some_and(|r| !r.contains(pos))
{
continue;
let mut body_slack = slack;
if let Some((_, bb)) = bounds.get(i) {
// Only bodies whose outline on screen is near the cursor can have an edge under it.
if screen_box(proj, bb, 8.0).is_some_and(|r| !r.contains(pos)) {
continue;
}
body_slack = pick_slack(bb.diagonal());
}
// In the part's own frame, as for faces; its points are moved back to where it stands.
let (m, place) = b.mesh_in_place();
@@ -2388,7 +2412,7 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
for v in [e.first(), e.last()].into_iter().flatten().map(|v| at(*v)) {
if let Some(sp) = proj.to_screen(v) {
let dd = sp.distance(pos);
if dd < 7.0 && visible(v) && bestv.wants(dd) {
if dd < 7.0 && visible(v, body_slack) && bestv.wants(dd) {
bestv.offer(dd, Hit::Vertex { body: b.name.clone(), point: v });
}
}
@@ -2399,7 +2423,7 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
let dd = seg_dist(pos, a, c);
// Visible where the cursor is: the edge's point under the cursor, not an end or
// the middle, which can lie deeper than the face the cursor is on.
if dd < 6.0 && beste.wants(dd) && visible(ray_segment_closest(o, d, p0, p1)) {
if dd < 6.0 && beste.wants(dd) && visible(ray_segment_closest(o, d, p0, p1), body_slack) {
beste.offer(dd, Hit::Edge { body: b.name.clone(), index: ei, mid: at(polyline_mid(e)) });
}
}