Skip to content

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

Orbit about the cursor: over empty space, turn about the part in view (#2616) (#4947)

dudedagger42ee3d41210d5e
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index 24c602a3c1..ec5e76d709 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -1091,6 +1091,10 @@ pub(crate) fn pivot_under(app: &SolveApp, proj: &crate::viewport::Proj, at: Pos2
_ => {}
}
}
+ // Over empty space: the middle of the bodies in view, not of everything shown (#2616).
+ if let Some(c) = crate::viewport::in_view_centre(app, proj) {
+ return c;
+ }
let b = crate::viewport::view_bounds(app);
if b.is_empty() { app.cam.target } else { b.center() }
}
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index d24c4abbb6..ee86718747 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -323,6 +323,41 @@ pub fn view_bounds(app: &SolveApp) -> solidcraft_engine::geom::Aabb3 {
solidcraft_engine::view::bounds(&app.session)
}
+/// The middle of the shown bodies the view shows part of: what a cursor orbit begun over empty
+/// space turns about. The middle of everything shown can be far off screen when the part in
+/// view is far from other geometry (a part far from the origin and a sketch at it, #2616), and
+/// the part then swings out of view. `None` when no body is in view.
+pub(crate) fn in_view_centre(app: &SolveApp, proj: &Proj) -> Option<Vec3> {
+ let st = crate::open_meshing::shown_world(app);
+ let b = body_bounds(app, &st)
+ .iter()
+ .filter(|(name, b)| !b.is_empty() && app.session.body_visible(name) && in_view(proj, b))
+ .fold(solidcraft_engine::geom::Aabb3::EMPTY, |a, (_, b)| a.union(b));
+ (!b.is_empty()).then(|| b.center())
+}
+
+/// Does the view show part of box `b`? A box partly behind the eye counts.
+fn in_view(proj: &Proj, b: &solidcraft_engine::geom::Aabb3) -> bool {
+ let mut seen = Rect::NOTHING;
+ let mut behind = 0;
+ for i in 0..8 {
+ let corner = Vec3::new(
+ if i & 1 == 0 { b.min.x } else { b.max.x },
+ if i & 2 == 0 { b.min.y } else { b.max.y },
+ if i & 4 == 0 { b.min.z } else { b.max.z },
+ );
+ match proj.to_screen(corner) {
+ Some(p) => seen.extend_with(p),
+ None => behind += 1,
+ }
+ }
+ match behind {
+ 8 => false,
+ 0 => seen.intersects(proj.rect),
+ _ => true,
+ }
+}
+
fn scene_key(app: &SolveApp) -> u64 {
let mut h = std::collections::hash_map::DefaultHasher::new();
app.session.revision.hash(&mut h);
diff --git a/crates/ui-egui/tests/orbit_empty_space_pivot.rs b/crates/ui-egui/tests/orbit_empty_space_pivot.rs
new file mode 100644
index 0000000000..881446ffc0
--- /dev/null
+++ b/crates/ui-egui/tests/orbit_empty_space_pivot.rs
@@ -0,0 +1,61 @@
+use serde_json::json;
+use solidcraft_ui_egui::scenario::Harness;
+
+fn on_screen(h: &mut Harness, w: [f64; 3]) -> [f64; 2] {
+ let r = h.call("ui.at", json!({ "world": w }));
+ [r["result"][0].as_f64().unwrap(), r["result"][1].as_f64().unwrap()]
+}
+
+/// #2616: with Orbit around → Point under the cursor, an orbit begun over empty space turns
+/// about the middle of the part in view. It turned about the middle of everything shown, which
+/// for a part far from the origin and anything at the origin (a small body, a sketch) is far off
+/// screen: the part flew out of view.
+#[test]
+fn an_orbit_begun_over_empty_space_keeps_a_far_off_part_in_view() {
+ for at_origin in ["nothing", "box", "sketch"] {
+ let mut h = Harness::new();
+ h.app.preferences.orbit_around = solidcraft_ui_egui::prefs::OrbitAround::Cursor;
+ h.app.run("solid.box", json!({"length":40,"width":30,"height":20,"corner":[50000,50000,0]})).unwrap();
+ h.call("ui.view", json!({"view":"iso"}));
+ h.call("ui.view", json!({"view":"fit"}));
+ match at_origin {
+ "box" => {
+ h.app.run("solid.box", json!({"length":10,"width":10,"height":10})).unwrap();
+ }
+ "sketch" => {
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.run("sketch.circle.center", json!({"center":[0,0],"radius":5})).unwrap();
+ h.app.run("sketch.finish", json!({})).unwrap();
+ }
+ _ => {}
+ }
+ h.frames(3);
+ let centre = [50020.0, 50015.0, 10.0];
+ let before = on_screen(&mut h, centre);
+ let rect = h.app.viewport.rect.unwrap();
+ // Shift + middle drag from the window's empty corner.
+ let a = rect.left_top() + egui::vec2(40.0, 40.0);
+ let shift = egui::Modifiers { shift: true, ..Default::default() };
+ let send = |h: &mut Harness, e: egui::Event| {
+ h.app.synthetic.push(e);
+ h.frame();
+ };
+ send(&mut h, egui::Event::ModifiersChanged(egui::Modifiers::NONE));
+ send(&mut h, egui::Event::PointerMoved(a));
+ send(&mut h, egui::Event::PointerButton { pos: a, button: egui::PointerButton::Middle, pressed: true, modifiers: shift });
+ for k in 1..=18 {
+ send(&mut h, egui::Event::PointerMoved(a + egui::vec2(10.0 * k as f32, 0.0)));
+ }
+ let turned = h.app.cam;
+ send(
+ &mut h,
+ egui::Event::PointerButton { pos: a + egui::vec2(180.0, 0.0), button: egui::PointerButton::Middle, pressed: false, modifiers: shift },
+ );
+ let after = on_screen(&mut h, centre);
+ assert!(turned.yaw.is_finite(), "{at_origin}");
+ assert!(
+ (after[0] - before[0]).abs() < 2.0 && (after[1] - before[1]).abs() < 2.0,
+ "{at_origin} at the origin: the part's middle went from {before:?} to {after:?}"
+ );
+ }
+}