diff --git a/crates/ui-egui/src/view_pick.rs b/crates/ui-egui/src/view_pick.rs index 3da1ee6e6..cb8d096f2 100644 --- a/crates/ui-egui/src/view_pick.rs +++ b/crates/ui-egui/src/view_pick.rs @@ -32,7 +32,7 @@ const DEFAULT_MODEL_H: f64 = 600.0; /// How far past the section plane a face point may lie and still count as kept (the renderer /// clips at `n·p <= n·o`; this is the same hairline `clip_segment` allows). -const SECTION_EPS: f64 = 1e-6; +pub(crate) const SECTION_EPS: f64 = 1e-6; /// Crossings one body may contribute before the section-cap walk gives up on it. const MAX_CROSSINGS: usize = 64; @@ -307,6 +307,26 @@ fn section_cap( if !(t_cap > 0.0) || t_cap >= first_kept.unwrap_or(f64::INFINITY) { return None; } + let body = body_cut_at(app, st, bounds, input.o, input.d, t_cap, include_ghosted, replaced, active_bodies)?; + Some((t_cap, body, sn)) +} + +/// The body the ray `o + d·t` is inside of at `t_cap` (where it crosses the section plane): an +/// odd number of mesh crossings before it. The walk re-raycasts each body past every crossing +/// it finds, bounded per body. The viewport's picker asks the same, so a cut cap hides what lies +/// behind it there too. +#[allow(clippy::too_many_arguments)] +pub(crate) fn body_cut_at( + app: &SolveApp, + st: &solidcraft_engine::doc::ModelState, + bounds: &[(String, solidcraft_engine::geom::Aabb3)], + o: Vec3, + d: Vec3, + t_cap: f64, + include_ghosted: bool, + replaced: &[String], + active_bodies: &Option>, +) -> Option { let walk_eps = 1e-6 * (1.0 + crate::viewport::scene_radius(app)); for (i, b) in st.bodies.iter().enumerate() { if !app.session.body_visible(&b.name) || replaced.contains(&b.name) { @@ -317,7 +337,7 @@ fn section_cap( } if let Some((_, bb)) = bounds.get(i) { let pad = bb.diagonal() * 1e-6 + 1e-9; - match crate::viewport::ray_box(input.o, input.d, bb, pad) { + match crate::viewport::ray_box(o, d, bb, pad) { None => continue, Some(t0) if t0 > t_cap => continue, _ => {} @@ -326,7 +346,7 @@ fn section_cap( let (m, place) = b.mesh_in_place(); let index = app.viewport.pick_index.of(&m); let (mut t, mut crossings) = (0.0f64, 0usize); - while let Some((lo, ld)) = crate::viewport::local_ray(&place, input.o + input.d * t, input.d) { + while let Some((lo, ld)) = crate::viewport::local_ray(&place, o + d * t, d) { let Some((dt, _)) = crate::pick_index::raycast(index.as_deref(), &m, lo, ld) else { break }; let tt = t + dt; if tt >= t_cap - SECTION_EPS { @@ -339,7 +359,7 @@ fn section_cap( } } if crossings % 2 == 1 { - return Some((t_cap, b.name.clone(), sn)); + return Some(b.name.clone()); } } None diff --git a/crates/ui-egui/src/view_pick_tests.rs b/crates/ui-egui/src/view_pick_tests.rs index 1f5967ed6..16148bead 100644 --- a/crates/ui-egui/src/view_pick_tests.rs +++ b/crates/ui-egui/src/view_pick_tests.rs @@ -201,3 +201,39 @@ fn all_lists_the_faces_along_the_ray_and_the_cap_is_enforced() { assert_eq!(v["ok"], false); assert!(v["error"].as_str().is_some_and(|e| e.contains("1024")), "{v}"); } + +/// The pointer over a section's cap picks nothing behind it (#6328): hovering the cut used to +/// pick the removed top face, whose highlighted outline then showed dimmed through the cap +/// wherever it ran behind it, so the cut looked see-through. Through the pocket the cut opens, +/// the pointer still finds the pocket's floor. +#[test] +fn the_mouse_picker_stops_at_a_section_cap() { + use crate::viewport::{Hit, pick, pick_all, projection}; + let mut h = top_view(); + h.call( + "engine.execute", + json!({"command": "solid.box", "params": {"length": 4, "width": 4, "height": 4, "corner": [3, 3, 6], "operation": "cut"}}), + ); + h.call("engine.execute", json!({"command": "inspect.section", "params": {"plane": {"origin": [0, 0, 8], "normal": [0, 0, 1]}}})); + let rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(600.0, 600.0)); + let proj = projection(&h.app, rect); + let faces = |hits: &[Hit]| hits.iter().filter(|x| matches!(x, Hit::Face { .. })).cloned().collect::>(); + // Over the cut material, between the pocket and the box's side: no face, and no edge or + // corner behind the cap. + let solid = proj.to_screen(Vec3::new(1.5, 5.0, 8.0)).unwrap(); + let hits = pick(&h.app, &proj, solid); + assert!(hits.iter().all(|x| !matches!(x, Hit::Face { .. } | Hit::Edge { .. } | Hit::Vertex { .. })), "{hits:?}"); + assert!(faces(&pick_all(&h.app, &proj, solid)).is_empty()); + // Over the pocket: its floor at z = 6, below the cut. + let pocket = proj.to_screen(Vec3::new(5.0, 5.0, 8.0)).unwrap(); + match faces(&pick(&h.app, &proj, pocket)).first() { + Some(Hit::Face { point, .. }) => assert!((point.z - 6.0).abs() < 1e-6, "{point:?}"), + other => panic!("the pocket's floor: {other:?}"), + } + // Without the section, the box's top face answers there. + h.call("engine.execute", json!({"command": "inspect.section", "params": {"clear": true}})); + match faces(&pick(&h.app, &proj, solid)).first() { + Some(Hit::Face { point, .. }) => assert!((point.z - 10.0).abs() < 1e-6, "{point:?}"), + other => panic!("the top face: {other:?}"), + } +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 4d5d19a22..080bc60e4 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -1706,6 +1706,11 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, } } hits.extend(bestc.hits()); + // A section hides the material its normal points at: nothing there is picked, and where the + // plane crosses a body its cap hides what lies behind it (#6328), as `view.pick` answers. + let section = section_plane(app); + let kept = |p: Vec3| section.is_none_or(|(so, sn)| sn.dot(p) <= sn.dot(so) + crate::view_pick::SECTION_EPS); + let walk_eps = 1e-6 * (1.0 + scene_radius(app)); // Nearest face along the ray (hides what is behind it); with `all`, every body's nearest. let mut bestf: Option<(f64, Hit)> = None; let mut faces = Near::new(all); @@ -1727,8 +1732,21 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, // A placed part is picked in its own frame, from its shared mesh and index: the ray is // moved there (a rigid move keeps distances along it), so no moved copy is made. let (m, place) = b.mesh_in_place(); - let Some((lo, ld)) = local_ray(&place, o, d) else { continue }; - if let Some((t, ti)) = crate::pick_index::raycast(app.viewport.pick_index.of(&m).as_deref(), &m, lo, ld) { + let index = app.viewport.pick_index.of(&m); + // The nearest kept face: past any the section removes (a bounded walk along the ray). + let mut nearest = None; + let mut from = 0.0f64; + for _ in 0..64 { + let Some((lo, ld)) = local_ray(&place, o + d * from, d) else { break }; + let Some((dt, ti)) = crate::pick_index::raycast(index.as_deref(), &m, lo, ld) else { break }; + let t = from + dt; + if kept(o + d * t) { + nearest = Some((t, ti)); + break; + } + from = t + walk_eps; + } + if let Some((t, ti)) = nearest { let fi = m.tri_face.get(ti).copied().unwrap_or(0) as usize; let hit = Hit::Face { body: b.name.clone(), index: fi, point: o + d * t }; if bestf.as_ref().is_none_or(|(bt, _)| t < *bt) { @@ -1739,10 +1757,25 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, } } } + // The cut cap, where the plane crosses a body nearer than any kept face: it hides that face + // and everything behind it, and is no face itself. + let cap = section.and_then(|(so, sn)| { + let den = sn.dot(d); + let t_cap = (sn.dot(so) - sn.dot(o)) / den; + let nearer = den.abs() > 1e-12 && t_cap > 0.0 && bestf.as_ref().is_none_or(|(t, _)| t_cap < *t); + let replaced = app.preview.effective_replaced(); + let shown_bodies = &None; + (nearer && crate::view_pick::body_cut_at(app, &st, &bounds, o, d, t_cap, true, replaced, shown_bodies).is_some()).then_some(t_cap) + }); + if let Some(t_cap) = cap { + bestf = None; + 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 wireframe = app.ui.visual_style == 2; - let visible = |p: Vec3| wireframe || bestf.as_ref().is_none_or(|(t, _)| (p - o).dot(d) <= *t + slack); + let visible = |p: Vec3| 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);