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: on an edge or sketch line, turn about the point under the cursor (#7356) (#7358)

froglemonade2e55eff7c5de1
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 16e871ec92..5eef89329e 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -1449,7 +1449,7 @@ impl Dialog {
/// point input takes the centre as a point. Inputs that take edges themselves keep the edge
/// (Move's points find its centre, `dialogs_move::sel_point`).
fn circle_centre_pick(&self, s: &Session, inp: &SelInput, h: &Hit, snap: Option<&FaceSnap>) -> Option<Sel> {
- let Hit::Edge { body, index, mid } = h else { return None };
+ let Hit::Edge { body, index, mid, .. } = h else { return None };
let a = inp.accept;
let places = snaps_to_points(a);
if a & EDGES != 0 || (a & selection::VERTICES == 0 && !places) {
diff --git a/crates/ui-egui/src/dialogs_all_tests.rs b/crates/ui-egui/src/dialogs_all_tests.rs
index 8e062ec3f0..9edb12c54d 100644
--- a/crates/ui-egui/src/dialogs_all_tests.rs
+++ b/crates/ui-egui/src/dialogs_all_tests.rs
@@ -13,7 +13,10 @@ fn measure_input_fits_external_sketch_geometry() {
assert!(fits(accept, &curve));
assert!(fits(accept, &point));
assert_eq!(d.items(), vec![curve.clone(), point.clone()]);
- assert_eq!(d.candidate(&h.app.session, &Hit::SketchCurve { sketch: 17, id: "line1".into(), straight: true }), Some(curve.clone()));
+ assert_eq!(
+ d.candidate(&h.app.session, &Hit::SketchCurve { sketch: 17, id: "line1".into(), straight: true, point: solidcraft_engine::geom::Vec3::ZERO }),
+ Some(curve.clone())
+ );
assert_eq!(
d.candidate(&h.app.session, &Hit::SketchPoint { sketch: 17, id: "p1".into(), at: solidcraft_engine::geom::Vec2::new(0.0, 0.0) }),
Some(point.clone())
diff --git a/crates/ui-egui/src/loft_rails_dialog_tests.rs b/crates/ui-egui/src/loft_rails_dialog_tests.rs
index f1f99f9c97..6a3a74aa6d 100644
--- a/crates/ui-egui/src/loft_rails_dialog_tests.rs
+++ b/crates/ui-egui/src/loft_rails_dialog_tests.rs
@@ -96,7 +96,7 @@ fn curves_that_share_an_id_are_picked_from_their_own_sketches() {
h.app.run("sketch.arc.three_point", json!({"start": [-10, 0], "end": [-10, -40], "through": [-15, -20], "id": "out"})).unwrap();
h.app.run("sketch.finish", json!({})).unwrap();
let left = h.app.session.doc.find_feature("L").unwrap().id;
- let hit = |sketch: u64, id: &str| Hit::SketchCurve { sketch, id: id.into(), straight: false };
+ let hit = |sketch: u64, id: &str| Hit::SketchCurve { sketch, id: id.into(), straight: false, point: solidcraft_engine::geom::Vec3::ZERO };
h.step(&json!({"call":"ui.start","params":{"command":"solid.loft"}})).unwrap();
h.frames(3);
let d = h.app.dialog.as_mut().unwrap();
diff --git a/crates/ui-egui/src/menu_tests.rs b/crates/ui-egui/src/menu_tests.rs
index 6413252dae..81e12a0f8d 100644
--- a/crates/ui-egui/src/menu_tests.rs
+++ b/crates/ui-egui/src/menu_tests.rs
@@ -223,7 +223,7 @@ fn right_click_on_a_face_of_a_selected_body_keeps_the_bodies() {
assert_eq!(app.session.selection, both, "the right-click keeps the bodies it is about");
assert!(!context_menu::items(&app, &Target::Viewport).iter().any(|i| i.label == "Create Sketch"), "the bodies' menu, not the face menu");
// An edge and a vertex of a selected body count the same way.
- app.viewport.hover = Some(Hit::Edge { body: first.clone(), index: 0, mid: Vec3::new(9.0, 9.0, 9.0) });
+ app.viewport.hover = Some(Hit::Edge { body: first.clone(), index: 0, mid: Vec3::new(9.0, 9.0, 9.0), point: Vec3::new(9.0, 9.0, 9.0) });
context_menu::open(&mut app, pos2(400.0, 300.0), Default::default());
assert_eq!(app.session.selection, both, "an edge of a selected body keeps the bodies");
// A face of a body outside the selection is still picked, so the menu is about that face.
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index e1b41b9057..eab7e35933 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -1774,14 +1774,17 @@ pub fn zoom_sign(app: &SolveApp) -> f64 {
if app.preferences.zoom_reverse { -1.0 } else { 1.0 }
}
-/// The model point under a screen position. Over empty space: the centre of the visible bodies
-/// and sketches (what Fit frames), or the view's centre when nothing is visible.
+/// The model point under a screen position: on a face, vertex, edge or sketch curve, the point
+/// the cursor is on (on an edge or a curve too, not its middle, which can be far off along a long
+/// one). Over empty space: the centre of the visible bodies and sketches (what Fit frames), or
+/// the view's centre when nothing is visible.
pub(crate) fn pivot_under(app: &SolveApp, proj: &crate::viewport::Proj, at: Pos2) -> Vec3 {
use crate::viewport::Hit;
for h in crate::viewport::pick(app, proj, at) {
match h {
- Hit::Face { point, .. } | Hit::Vertex { point, .. } => return point,
- Hit::Edge { mid, .. } => return mid,
+ Hit::Face { point, .. } | Hit::Vertex { point, .. } | Hit::Edge { point, .. } | Hit::SketchCurve { point, .. } => {
+ return point;
+ }
_ => {}
}
}
@@ -6086,6 +6089,56 @@ mod tests {
assert!(s.distance(at) < 2.0, "{s:?}");
}
+ /// A drag that starts on an edge turns around the edge's point under the cursor, not its
+ /// middle, and that point stays still on screen.
+ #[test]
+ fn orbiting_from_an_edge_turns_around_the_point_under_the_cursor() {
+ use crate::viewport::Hit;
+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
+ app.run("solid.box", json!({"length": 200, "width": 40, "height": 40})).unwrap();
+ app.preferences.orbit_around = OrbitAround::Cursor;
+ let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0));
+ app.fit_view();
+ let proj = crate::viewport::projection(&app, rect);
+ let st = app.session.world_state();
+ let m = st.bodies[0].mesh();
+ // A spot a fifth of the way along a visible edge, well away from its middle on screen.
+ let found = m.edges.iter().filter(|e| e.len() >= 2).find_map(|e| {
+ let (a, b) = (e[0], e[e.len() - 1]);
+ let at = proj.to_screen(a + (b - a) * 0.2)?;
+ let mid = proj.to_screen(crate::viewport::polyline_mid(e))?;
+ let first =
+ crate::viewport::pick(&app, &proj, at).into_iter().find(|h| matches!(h, Hit::Face { .. } | Hit::Edge { .. } | Hit::Vertex { .. }));
+ (rect.contains(at) && at.distance(mid) > 100.0 && matches!(first, Some(Hit::Edge { .. }))).then_some((at, mid))
+ });
+ let (at, mid) = found.unwrap();
+ let pivot = pivot_under(&app, &proj, at);
+ let on_screen = proj.to_screen(pivot).unwrap();
+ assert!(on_screen.distance(at) < 6.0, "the pivot is under the cursor: {on_screen:?} vs {at:?}");
+ assert!(on_screen.distance(mid) > 90.0, "not the edge's middle");
+ orbit(&mut app, rect, 40.0, 25.0, Some(at));
+ assert_eq!(app.viewport.orbit_pivot, Some(pivot));
+ let after = crate::viewport::projection(&app, rect).to_screen(pivot).unwrap();
+ assert!(after.distance(on_screen) < 2.0, "{on_screen:?} -> {after:?}");
+ }
+
+ /// On a sketch line too: the line's point under the cursor, not the view's middle.
+ #[test]
+ fn orbiting_from_a_sketch_line_turns_around_the_point_under_the_cursor() {
+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
+ app.run("sketch.create", json!({"plane": "XY"})).unwrap();
+ app.run("sketch.line", json!({"points": [[-100, 0], [100, 0]]})).unwrap();
+ app.run("sketch.finish", json!({})).unwrap();
+ app.preferences.orbit_around = OrbitAround::Cursor;
+ let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0));
+ app.fit_view();
+ let proj = crate::viewport::projection(&app, rect);
+ let on_line = Vec3::new(-60.0, 0.0, 0.0);
+ let at = proj.to_screen(on_line).unwrap();
+ let pivot = pivot_under(&app, &proj, at);
+ assert!(pivot.dist(on_line) < 2.0, "{pivot:?}");
+ }
+
/// A drag that starts over empty space turns around the visible geometry's centre, not a
/// point far off to the side; with nothing visible it turns around the view's centre.
#[test]
diff --git a/crates/ui-egui/src/round_pick_tests.rs b/crates/ui-egui/src/round_pick_tests.rs
index e3c50a911e..e4fdbdc9d5 100644
--- a/crates/ui-egui/src/round_pick_tests.rs
+++ b/crates/ui-egui/src/round_pick_tests.rs
@@ -51,7 +51,7 @@ fn picks(h: &Harness) -> (Sel, Sel, Sel) {
fn hit(sel: &Sel) -> Hit {
match sel {
Sel::Face { body, index, point } => Hit::Face { body: body.clone(), index: *index, point: *point },
- Sel::Edge { body, index, point } => Hit::Edge { body: body.clone(), index: *index, mid: *point },
+ Sel::Edge { body, index, point } => Hit::Edge { body: body.clone(), index: *index, mid: *point, point: *point },
_ => panic!("not a model pick"),
}
}
diff --git a/crates/ui-egui/src/select_other.rs b/crates/ui-egui/src/select_other.rs
index c085b0487c..9afd44cd02 100644
--- a/crates/ui-egui/src/select_other.rs
+++ b/crates/ui-egui/src/select_other.rs
@@ -194,7 +194,7 @@ fn hit_depth(
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 } => {
+ 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);
diff --git a/crates/ui-egui/src/selection.rs b/crates/ui-egui/src/selection.rs
index 2d4652c47d..54df89eda9 100644
--- a/crates/ui-egui/src/selection.rs
+++ b/crates/ui-egui/src/selection.rs
@@ -105,7 +105,7 @@ impl SelInput {
let a = self.accept;
match h {
Hit::Profile { sketch, index } if a & PROFILES != 0 => Some(Sel::Profile { sketch: *sketch, index: *index }),
- Hit::Edge { body, index, mid } if a & EDGES != 0 => Some(Sel::Edge { body: body.clone(), index: *index, point: *mid }),
+ Hit::Edge { body, index, mid, .. } if a & EDGES != 0 => Some(Sel::Edge { body: body.clone(), index: *index, point: *mid }),
Hit::Face { body, index, point } if a & FACES != 0 || (a & PLANAR_FACES != 0 && planar(body, *index)) => {
Some(Sel::Face { body: body.clone(), index: *index, point: *point })
}
diff --git a/crates/ui-egui/src/sketch_guidance_ui_tests.rs b/crates/ui-egui/src/sketch_guidance_ui_tests.rs
index 8f9426f151..8a659ec812 100644
--- a/crates/ui-egui/src/sketch_guidance_ui_tests.rs
+++ b/crates/ui-egui/src/sketch_guidance_ui_tests.rs
@@ -4,6 +4,7 @@ use crate::sketch_modify_ui::{Mod, candidate};
use crate::viewport::Hit;
use serde_json::{Value, json};
use solidcraft_engine::Sel;
+use solidcraft_engine::geom::Vec3;
fn step(h: &mut Harness, value: Value) {
h.step(&value).unwrap();
@@ -121,8 +122,8 @@ fn sketch_dialog_mirror_refuses_external_and_curved_references() {
h.app.start("sketch.mirror");
let d = h.app.dialog.as_mut().unwrap();
d.active = 1;
- assert!(candidate(d, &h.app.session, &Hit::SketchCurve { sketch: 2, id: "l1".into(), straight: true }).is_none());
- assert!(candidate(d, &h.app.session, &Hit::SketchCurve { sketch: 1, id: "c1".into(), straight: false }).is_none());
+ assert!(candidate(d, &h.app.session, &Hit::SketchCurve { sketch: 2, id: "l1".into(), straight: true, point: Vec3::ZERO }).is_none());
+ assert!(candidate(d, &h.app.session, &Hit::SketchCurve { sketch: 1, id: "c1".into(), straight: false, point: Vec3::ZERO }).is_none());
}
#[test]
@@ -161,7 +162,7 @@ fn sketch_modify_inputs_refuse_other_sketches_without_changing_hole_points() {
for command in ["sketch.mirror", "sketch.pattern.rectangular", "sketch.pattern.circular", "sketch.offset", "sketch.move", "sketch.scale"] {
h.app.start(command);
let d = h.app.dialog.as_ref().unwrap();
- assert!(candidate(d, &h.app.session, &Hit::SketchCurve { sketch: 2, id: "l1".into(), straight: true }).is_none());
+ assert!(candidate(d, &h.app.session, &Hit::SketchCurve { sketch: 2, id: "l1".into(), straight: true, point: Vec3::ZERO }).is_none());
assert!(candidate(d, &h.app.session, &Hit::SketchPoint { sketch: 2, id: "p1".into(), at: solidcraft_engine::geom::Vec2::ZERO }).is_none());
if command != "sketch.offset" {
assert!(d.inputs[0].accept & crate::selection::SKETCH_POINTS != 0);
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index e3ec72d0b9..eda2172f3a 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -231,20 +231,25 @@ pub enum Hit {
id: String,
at: Vec2,
},
- /// A sketch curve; `straight` for lines (usable as an axis).
+ /// A sketch curve; `straight` for lines (usable as an axis). `point` is the curve's point
+ /// under the cursor.
SketchCurve {
sketch: u64,
id: String,
straight: bool,
+ point: Vec3,
},
Vertex {
body: String,
point: Vec3,
},
+ /// A body edge: `mid` is its middle (what a selection of it records), `point` its point
+ /// under the cursor.
Edge {
body: String,
index: usize,
mid: Vec3,
+ point: Vec3,
},
Profile {
sketch: u64,
@@ -2388,11 +2393,12 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
// still offers both.
let helper = if c.construction || c.centerline { 0.5 } else { 0.0 };
for part in &crate::sketch_display::curve_sample(app, sid, i, &ss.plane, proj).parts {
- let pts: Vec<Pos2> = part.iter().filter_map(|q| proj.to_screen(*q)).collect();
+ let pts: Vec<(Pos2, Vec3)> = part.iter().filter_map(|q| proj.to_screen(*q).map(|sp| (sp, *q))).collect();
for w in pts.windows(2) {
- let dd = seg_dist(pos, w[0], w[1]) - bonus + helper;
+ let dd = seg_dist(pos, w[0].0, w[1].0) - bonus + helper;
if dd < 6.0 && bestc.wants((tier, dd)) {
- bestc.offer((tier, dd), Hit::SketchCurve { sketch: sid, id: c.id.clone(), straight });
+ let point = ray_segment_closest(o, d, w[0].1, w[1].1);
+ bestc.offer((tier, dd), Hit::SketchCurve { sketch: sid, id: c.id.clone(), straight, point });
}
}
}
@@ -2401,11 +2407,12 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
for w in &ss.sketch.wires {
let straight = w.fit.len() == 2;
let tier = tier(w.link.is_none());
- let pts: Vec<Pos2> = w.pts.iter().filter_map(|q| proj.to_screen(*q)).collect();
+ let pts: Vec<(Pos2, Vec3)> = w.pts.iter().filter_map(|q| proj.to_screen(*q).map(|sp| (sp, *q))).collect();
for seg in pts.windows(2) {
- let dd = seg_dist(pos, seg[0], seg[1]) - bonus;
+ let dd = seg_dist(pos, seg[0].0, seg[1].0) - bonus;
if dd < 6.0 && bestc.wants((tier, dd)) {
- bestc.offer((tier, dd), Hit::SketchCurve { sketch: sid, id: w.id.clone(), straight });
+ let point = ray_segment_closest(o, d, seg[0].1, seg[1].1);
+ bestc.offer((tier, dd), Hit::SketchCurve { sketch: sid, id: w.id.clone(), straight, point });
}
}
}
@@ -2538,8 +2545,9 @@ 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), body_slack) {
- beste.offer(dd, Hit::Edge { body: b.name.clone(), index: ei, mid: at(polyline_mid(e)) });
+ let point = ray_segment_closest(o, d, p0, p1);
+ if dd < 6.0 && beste.wants(dd) && visible(point, body_slack) {
+ beste.offer(dd, Hit::Edge { body: b.name.clone(), index: ei, mid: at(polyline_mid(e)), point });
}
}
}
@@ -2922,7 +2930,7 @@ pub fn hit_sel(h: &Hit) -> Option<Sel> {
Hit::SketchPoint { id, .. } => Sel::SketchPoint { id: id.clone() },
Hit::SketchCurve { id, .. } => Sel::SketchCurve { id: id.clone(), sketch: None },
Hit::Vertex { body, point } => Sel::Vertex { body: body.clone(), point: *point },
- Hit::Edge { body, index, mid } => Sel::Edge { body: body.clone(), index: *index, point: *mid },
+ Hit::Edge { body, index, mid, .. } => Sel::Edge { body: body.clone(), index: *index, point: *mid },
Hit::Profile { sketch, index } => Sel::Profile { sketch: *sketch, index: *index },
Hit::Face { body, index, point } => Sel::Face { body: body.clone(), index: *index, point: *point },
Hit::Plane { name, .. } => Sel::Plane { name: name.clone() },
@@ -3826,7 +3834,7 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {
}
}
}
- Some(Hit::Edge { body, index, mid }) => {
+ Some(Hit::Edge { body, index, mid, .. }) => {
if let Some(b) = st.body(body) {
// Lit over the drawn edges (finer away from Normal display quality).
let m = drawn_mesh(app, b);
@@ -6856,7 +6864,7 @@ mod tests {
let preview: f32 = output.shapes.iter().map(|s| length(&s.shape)).sum();
let r = h.app.run("sketch.line", json!({"points":[[0,0],[20,8]]})).unwrap();
let id = r["curves"][0].as_str().unwrap().to_string();
- h.app.viewport.hover = Some(Hit::SketchCurve { sketch: h.app.session.active_sketch.unwrap(), id, straight: true });
+ h.app.viewport.hover = Some(Hit::SketchCurve { sketch: h.app.session.active_sketch.unwrap(), id, straight: true, point: Vec3::ZERO });
let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
hover_highlight(&h.app, &ui.ctx().layer_painter(egui::LayerId::background()), &proj);
});
@@ -8631,9 +8639,9 @@ mod tests {
assert_eq!(build_highlight(&app).merged().tris, before.tris);
app.viewport.hover = Some(hover(1, 9.0));
assert_ne!(highlight_key(&app), key, "another face");
- app.viewport.hover = Some(Hit::Edge { body: "Body1".into(), index: 0, mid: Vec3::ZERO });
+ app.viewport.hover = Some(Hit::Edge { body: "Body1".into(), index: 0, mid: Vec3::ZERO, point: Vec3::ZERO });
let edge = highlight_key(&app);
- app.viewport.hover = Some(Hit::Edge { body: "Body1".into(), index: 0, mid: Vec3::X });
+ app.viewport.hover = Some(Hit::Edge { body: "Body1".into(), index: 0, mid: Vec3::X, point: Vec3::X });
assert_ne!(highlight_key(&app), edge, "an edge's middle picks which part of it is lit");
app.viewport.hover = Some(Hit::Plane { name: "XY".into(), point: Vec3::ZERO });
let plane = highlight_key(&app);
@@ -9061,7 +9069,7 @@ mod tests {
let line_count: usize = rim.iter().map(|&i| m.edges[i].len() - 1).sum();
for &i in &rim {
let mid = polyline_mid(&m.edges[i]);
- app.viewport.hover = Some(Hit::Edge { body: b.name.clone(), index: i, mid });
+ app.viewport.hover = Some(Hit::Edge { body: b.name.clone(), index: i, mid, point: mid });
assert_eq!(build_highlight(&app).merged().lines.len(), 2 * line_count * crate::gpu::LINE_SIZE);
let item = hit_sel(app.viewport.hover.as_ref().unwrap()).unwrap();
select(&mut app, Some(item.clone()), false, false);