From a3f28c0b89dccf9da07bd2cc21d40ac4a485c057 Mon Sep 17 00:00:00 2001 From: fugazister Date: Tue, 20 Oct 2026 10:47:26 +0000 Subject: [PATCH] Look At frames the sketch's geometry, not its plane's origin (#3198) (#3199) * claim: #3198 Co-Authored-By: Claude Opus 5.5 * Look At frames the sketch's geometry, not its plane's origin (#3198) Look At on a sketch (its menu, Ctrl+8, the navigation bar, entering a sketch) centred the view on the sketch plane's origin and kept the zoom, so a sketch drawn away from it - an imported part placed far from the origin - ended up out of view. Both paths now share onto_sketch: centred on the sketch's bounds, zoomed out when they would not fit (never in); an empty sketch keeps the plane's origin. Co-Authored-By: Claude Opus 5.5 * Register the Look At tests Co-Authored-By: Claude Opus 5.5 * Look At tests: Vec3::len Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/context_menu.rs | 3 +- crates/ui-egui/src/dialogs.rs | 24 ++++++++++- crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/look_at_tests.rs | 62 +++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/look_at_tests.rs diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index b71b08e23..871e4e600 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -1233,8 +1233,7 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) { "ui.lookAt" => { let st = app.session.world_state(); if let Some(ss) = id_of(&p, "sketch").and_then(|id| st.sketch(id)) { - let mut to = app.cam.looking_from(ss.plane.normal()); - to.target = ss.plane.origin; + let to = crate::dialogs::onto_sketch(app.cam.looking_from(ss.plane.normal()), ss); app.animate_to(to); } } diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 532279f4e..a815204a5 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -3689,11 +3689,31 @@ fn look_at_sketch_from(app: &mut SolveApp, again: bool) { let x = ss.plane.x; to.yaw = if n.z > 0.0 { (-x.y).atan2(x.x) } else { x.y.atan2(x.x) }; } - to.target = ss.plane.origin; - app.animate_to(to); + app.animate_to(onto_sketch(to, ss)); } } +/// `to` (already facing the sketch) moved onto sketch `ss` (#3198): centred on its geometry, and +/// zoomed out when that would not fit in the view; it never zooms in, so a zoom the user chose +/// stays. A sketch without geometry keeps its plane's origin. Centring on the origin alone left a +/// sketch drawn away from it (a part placed far from the origin, as imports often are) out of view. +pub(crate) fn onto_sketch(mut to: solidcraft_engine::render::Camera, ss: &solidcraft_engine::doc::SolvedSketch) -> solidcraft_engine::render::Camera { + let Some((lo, hi)) = ss.sketch.bounds() else { + to.target = ss.plane.origin; + return to; + }; + let mut b = Aabb3::default(); + for p in [lo, hi, solidcraft_engine::geom::Vec2::new(lo.x, hi.y), solidcraft_engine::geom::Vec2::new(hi.x, lo.y)] { + b.add(ss.plane.to_world(p)); + } + let keep = to.distance; + to.fit(&b); + if keep.is_finite() && keep > to.distance { + to.distance = keep; + } + to +} + /// The side to look from at a plane with normal `n`: its own side, or with `again` the other /// side when the view already looks from `n`, so a second Look At turns the view round (#1886). pub(crate) fn facing(app: &SolveApp, n: Vec3, again: bool) -> Vec3 { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 437f9fed0..60870c71f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -74,6 +74,8 @@ pub mod keymap; mod linked_source; #[cfg(test)] mod loft_rails_dialog_tests; +#[cfg(test)] +mod look_at_tests; mod material_card_review; #[cfg(test)] mod measurement_ui_tests; diff --git a/crates/ui-egui/src/look_at_tests.rs b/crates/ui-egui/src/look_at_tests.rs new file mode 100644 index 000000000..753f8422c --- /dev/null +++ b/crates/ui-egui/src/look_at_tests.rs @@ -0,0 +1,62 @@ +//! Look At on a sketch (#3198): it faces the sketch and frames its geometry, not its plane's +//! origin, so a sketch drawn far from the origin (an imported part placed there) stays in view. + +use serde_json::json; +use solidcraft_engine::geom::Vec3; + +use crate::scenario::Harness; + +/// A 20 × 30 rectangle at x 330–350, y 475–505 on the XY plane raised 8 mm, as an imported part +/// rebuilt where it was placed; the camera jumps straight to where Look At sends it. +fn far_sketch() -> (Harness, u64) { + let mut h = Harness::new(); + h.app.home.open = false; + h.app.preferences.look_at_anim = false; + h.app.run("sketch.create", json!({"plane": "XY", "offset": 8, "name": "Far"})).unwrap(); + h.app.run("sketch.rectangle.two_point", json!({"p0": [330, 475], "p1": [350, 505]})).unwrap(); + h.app.run("sketch.finish", json!({})).unwrap(); + let id = h.app.session.doc.find_feature("Far").map(|f| f.id).unwrap(); + (h, id) +} + +fn near(a: Vec3, b: Vec3) -> bool { + (a - b).len() < 1e-6 +} + +/// The camera's view half-width (mm) the rectangle needs: its half diagonal. +const HALF_DIAGONAL: f64 = 18.027_756_377_319_946; + +#[test] +fn the_menus_look_at_frames_a_sketch_far_from_the_origin() { + let (mut h, id) = far_sketch(); + h.app.cam.distance = 20.0; + let items = crate::context_menu::items(&h.app, &crate::context_menu::Target::Sketch { id }); + let look = items.iter().find(|i| i.id == "ui.lookAt").cloned().expect("the sketch menu has Look At"); + crate::context_menu::run_item(&mut h.app, &look, egui::Pos2::ZERO); + assert!(near(h.app.cam.target, Vec3::new(340.0, 490.0, 8.0)), "centred on the rectangle, not the plane's origin: {:?}", h.app.cam.target); + assert!((h.app.cam.back() - Vec3::new(0.0, 0.0, 1.0)).len() < 1e-6, "facing the sketch from above"); + let need = HALF_DIAGONAL / 0.4 * 1.15; + assert!(h.app.cam.distance >= need - 1e-6, "zoomed out to fit it: {} < {need}", h.app.cam.distance); +} + +#[test] +fn look_at_on_the_active_sketch_keeps_a_wider_zoom() { + let (mut h, id) = far_sketch(); + h.app.session.active_sketch = Some(id); + h.app.cam.distance = 5000.0; + crate::dialogs::look_at_sketch(&mut h.app); + assert!(near(h.app.cam.target, Vec3::new(340.0, 490.0, 8.0)), "{:?}", h.app.cam.target); + assert_eq!(h.app.cam.distance, 5000.0, "it never zooms in on a zoom the user chose"); +} + +#[test] +fn an_empty_sketch_keeps_its_planes_origin() { + let mut h = Harness::new(); + h.app.home.open = false; + h.app.preferences.look_at_anim = false; + h.app.run("sketch.create", json!({"plane": "XY", "offset": 8})).unwrap(); + h.app.cam.distance = 123.0; + crate::dialogs::look_at_sketch(&mut h.app); + assert!(near(h.app.cam.target, Vec3::new(0.0, 0.0, 8.0)), "{:?}", h.app.cam.target); + assert_eq!(h.app.cam.distance, 123.0); +}