Navigation: opt-in free orbit and shared Normal to Selection (#1912)

* Navigation: opt-in free orbit and shared Normal to Selection

Preserve the complete reviewed feature, shared camera setting propagation,
rigid pivot behavior and focus/drag regressions from PR #1912.

Consolidate the prior main-integration commits before rebasing, so their
merge-resolution fixes remain in the linear feature history.

* docs: move free orbit help beside viewport controls

* Merge main into fix/solidworks-free-orbit

Camera animation is gone on main, and with it the orbit snap spring: the Shift
snap keeps its unsnapped orbit in orbit_free, which the free-orbit checks now read.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-21 23:29:29 +00:00
1 parent ba4f7864ff
commit dfd58293a6
16 files changed
+740 -29

No files matched your search

+202 -4
View File
@@ -4,7 +4,7 @@ use solidcraft_render::{Camera, StandardView};
use super::CommandSpec;
use crate::params::{bad, num, req_vec2, req_vec3, vec3};
use crate::{Result, Session};
use crate::{Result, Sel, Session};
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("view.history_layout", "History layout", history_layout)
@@ -17,7 +17,48 @@ CommandSpec::new("view.navigate", "Navigate View", navigate)
.icon("orbit")
.noundo()
.doc_only()
.params("camera: Camera; direction?: [x,y,z] in the camera's up-axis frame; or action: left|right|up|down|clockwise|counterclockwise|home|fit, degrees?: 0..180 (how far left|right|up|down turn; default 90); or motion: pan|orbit|zoom, delta?: [dx,dy] (pixels, each ±10000), viewport?: [width,height] (1..1000000, required for pan or pivot), pivot?: [x,y,z], factor?: 0.05..20 (required for zoom), anchor?: [x,y,z] → camera, fit, continuous?")];
.params("camera: Camera; direction?: [x,y,z] in the camera's up-axis frame; or action: left|right|up|down|clockwise|counterclockwise|home|fit, degrees?: 0..180 (how far left|right|up|down turn; default 90); or motion: pan|orbit|zoom, delta?: [dx,dy] (pixels, each ±10000), free_orbit?: bool (orbit through poles, default false), viewport?: [width,height] (1..1000000, required for pan or pivot), pivot?: [x,y,z], factor?: 0.05..20 (required for zoom), anchor?: [x,y,z] → camera, fit, continuous?"),
CommandSpec::new("view.normal_to", "Normal to Selection", normal_to)
.icon("camera")
.noundo()
.enabled(|s| normal_target(s).map(|_| ()).ok_or_else(|| "select a planar face or plane".into()))
.params("camera?: Camera (default: current session camera or home view) → camera; Look At the first selected planar face or plane, centered on the picked point; repeat while aligned to flip the side; preserves scale and projection")];
fn normal_target(s: &Session) -> Option<(solidcraft_geom::Vec3, solidcraft_geom::Vec3)> {
let state = s.world_state();
s.selection.iter().find_map(|selection| match selection {
Sel::Face { body, index, point } if point.is_finite() => {
let plane = state.body(body)?.body.planar_face(*index)?;
Some((*point, plane.normal()))
}
Sel::Plane { name } => solidcraft_geom::Plane::named(name).map(|plane| (plane.origin, plane.normal())),
_ => None,
})
}
fn normal_to(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "view.normal_to";
let (point, normal) = normal_target(s).ok_or_else(|| bad(cmd, "select a planar face or plane"))?;
let cam: Camera = match p.get("camera") {
Some(value) => serde_json::from_value(value.clone()).map_err(|_| bad(cmd, "invalid camera"))?,
None => s.camera.unwrap_or_else(|| crate::view::home_camera(s)),
};
if !cam.is_valid()
|| !cam.fov.is_finite()
|| !(0.0..std::f64::consts::PI).contains(&cam.fov)
|| cam.yaw.abs() > 1e6
|| cam.roll.abs() > 1e6
|| cam.pitch.abs() > std::f64::consts::FRAC_PI_2
{
return Err(bad(cmd, "invalid camera"));
}
// Promoted from viewport::look_at_selection: asking again from the normal side flips it.
let direction = if cam.back().dot(normal) > 0.999 { -normal } else { normal };
let mut to = cam.looking_from(direction);
to.target = point;
s.camera = Some(to);
Ok(json!({"camera": to}))
}
fn history_layout(_s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "view.history_layout";
@@ -105,6 +146,10 @@ fn navigate(_s: &mut Session, p: &Value) -> Result<Value> {
/// Direct manipulation is stateless, bounded and independent of document evaluation.
fn continuous(mut cam: Camera, p: &Value) -> Result<Value> {
let cmd = "view.navigate";
let free_orbit = match p.get("free_orbit") {
None => false,
Some(value) => value.as_bool().ok_or_else(|| bad(cmd, "free_orbit must be a boolean"))?,
};
let viewport = || {
let size = req_vec2(cmd, p, "viewport")?;
if !(1.0..=1e6).contains(&size.x) || !(1.0..=1e6).contains(&size.y) {
@@ -118,14 +163,23 @@ fn continuous(mut cam: Camera, p: &Value) -> Result<Value> {
if delta.x.abs() > 1e4 || delta.y.abs() > 1e4 {
return Err(bad(cmd, "motion deltas must be within ±10000 pixels"));
}
let orbit = |cam: &mut Camera| {
if free_orbit {
cam.rotate_in_view(solidcraft_geom::Vec3::new(-delta.y * 0.008, -delta.x * 0.008, 0.0));
} else {
cam.orbit(delta.x, delta.y);
}
};
if motion == "pan" {
cam.pan(delta.x, delta.y, viewport()?.y);
} else if p.get("pivot").is_some() {
let pivot = req_vec3(cmd, p, "pivot")?;
viewport()?;
cam.orbit_about(delta.x, delta.y, pivot);
let coords = cam.frame_coords(pivot);
orbit(&mut cam);
cam.place(pivot, coords);
} else {
cam.orbit(delta.x, delta.y);
orbit(&mut cam);
}
}
Some("zoom") => {
@@ -165,6 +219,100 @@ mod tests {
use solidcraft_geom::Vec3;
use solidcraft_render::UpAxis;
#[test]
fn look_at_planes_uses_the_first_supported_selection_and_flips_on_repeat() {
let original = Camera { yaw: 0.3, pitch: 0.4, roll: 0.7, fov: 0.6, distance: 123.0, ..Default::default() };
let mut s = Session {
selection: vec![Sel::Plane { name: "missing".into() }, Sel::Plane { name: "XY".into() }, Sel::Plane { name: "YZ".into() }],
camera: Some(original),
..Default::default()
};
for normal in [Vec3::Z, -Vec3::Z, Vec3::Z] {
let result = s.execute("view.normal_to", &json!({})).unwrap();
let to: Camera = serde_json::from_value(result["camera"].clone()).unwrap();
assert!(to.back().dist(normal) < 2e-6);
assert_eq!((to.target, to.distance, to.fov), (Vec3::ZERO, original.distance, original.fov));
}
assert!(s.undo.is_empty());
}
#[test]
fn normal_to_selection_faces_each_planar_surface_without_editing_the_design() {
let mut s = Session::default();
s.execute("solid.box", &json!({"length":40,"width":30,"height":20})).unwrap();
let state = s.world_state();
let body = state.bodies.first().unwrap();
let faces = body.body.faces(0.01).unwrap();
let document = s.doc.to_json();
let undo = s.undo.len();
for up in [UpAxis::Z, UpAxis::Y] {
for fov in [0.0, 0.6] {
let cam = Camera { up, fov, roll: 1.2, distance: 123.0, ..Default::default() };
for face in &faces {
s.selection = vec![Sel::Face { body: body.name.clone(), index: face.index, point: face.centroid }];
let selected = s.selection.clone();
let result = s.execute("view.normal_to", &json!({"camera":cam})).unwrap();
let to: Camera = serde_json::from_value(result["camera"].clone()).unwrap();
assert!(to.back().dist(face.plane_normal.unwrap()) < 2e-6);
assert_eq!(to.target, face.centroid);
assert_eq!((to.distance, to.fov, to.up, to.roll), (cam.distance, cam.fov, cam.up, 0.0));
assert_eq!(s.camera, Some(to));
assert_eq!(s.selection, selected);
}
}
}
assert_eq!(s.doc.to_json(), document);
assert_eq!(s.undo.len(), undo);
}
#[test]
fn normal_to_selection_rejects_curved_missing_and_invalid_inputs() {
let mut s = Session::default();
s.execute("solid.cylinder", &json!({"diameter":20,"height":30})).unwrap();
let state = s.world_state();
let body = state.bodies.first().unwrap();
let faces = body.body.faces(0.1).unwrap();
let flat = faces.iter().find(|f| f.plane_normal.is_some()).unwrap();
let curved = faces.iter().find(|f| f.plane_normal.is_none()).unwrap();
let selected = |index, point| Sel::Face { body: body.name.clone(), index, point };
for selection in [vec![], vec![selected(curved.index, curved.centroid)], vec![selected(usize::MAX, Vec3::ZERO)]] {
s.selection = selection;
assert!(s.execute("view.normal_to", &json!({})).is_err());
assert!(s.camera.is_none());
}
s.selection = vec![selected(flat.index, flat.centroid)];
for camera in [
json!({}),
json!({"distance":0}),
json!(Camera { distance: -1.0, ..Default::default() }),
json!(Camera { fov: 4.0, ..Default::default() }),
] {
assert!(s.execute("view.normal_to", &json!({"camera":camera})).is_err());
assert!(s.camera.is_none());
}
// Programmatic callers can use the current session view without supplying a camera.
assert!(s.execute("view.normal_to", &json!({})).is_ok());
}
#[test]
fn normal_to_selection_uses_the_world_normal_of_a_moved_component() {
let mut s = Session::default();
s.execute("component.create", &json!({"name":"Part"})).unwrap();
s.execute("solid.box", &json!({"length":40,"width":30,"height":20})).unwrap();
s.execute("occurrence.move", &json!({"occurrence":"Part:1","translate":[50,20,10],"axis":[1,2,3],"angle":"37 deg"})).unwrap();
let state = s.world_state();
let body = state.bodies.first().unwrap();
let faces = body.body.faces(0.01).unwrap();
let cam = Camera { roll: 1.4, ..Default::default() };
for face in faces {
s.selection = vec![Sel::Face { body: body.name.clone(), index: face.index, point: face.centroid }];
let result = s.execute("view.normal_to", &json!({"camera":cam})).unwrap();
let to: Camera = serde_json::from_value(result["camera"].clone()).unwrap();
assert!(to.back().dist(face.plane_normal.unwrap()) < 2e-6);
assert!(to.target.dist(face.centroid) < 1e-9);
}
}
/// #1541: left, right, up and down turn a quarter turn, or `degrees` (the arrow keys' 15°).
#[test]
fn turns_take_degrees_and_default_to_a_quarter_turn() {
@@ -228,6 +376,53 @@ mod tests {
assert!(s.undo.is_empty());
}
#[test]
fn continuous_free_orbit_is_opt_in_and_preserves_framing_and_document() {
for up in [UpAxis::Z, UpAxis::Y] {
let mut s = Session::default();
let design = s.doc.to_json();
let cam = Camera { pitch: 1.4, roll: 0.7, up, ..Default::default() };
let mut p = json!({"camera":cam,"motion":"orbit","delta":[0,80]});
let default = s.execute("view.navigate", &p).unwrap();
p["free_orbit"] = json!(false);
assert_eq!(s.execute("view.navigate", &p).unwrap(), default);
let clamped: Camera = serde_json::from_value(default["camera"].clone()).unwrap();
assert!(clamped.pitch > cam.pitch && clamped.pitch < std::f64::consts::FRAC_PI_2);
p["free_orbit"] = json!(true);
let result = s.execute("view.navigate", &p).unwrap();
let free: Camera = serde_json::from_value(result["camera"].clone()).unwrap();
assert!((free.back().dot(cam.back()) - 0.64_f64.cos()).abs() < 1e-8);
assert_eq!((free.target, free.distance, free.fov), (cam.target, cam.distance, cam.fov));
assert_eq!(s.doc.to_json(), design);
assert!(s.undo.is_empty() && s.selection.is_empty());
}
}
#[test]
fn continuous_free_orbit_keeps_the_pivot_in_the_camera_frame() {
for up in [UpAxis::Y, UpAxis::Z] {
for fov in [0.0, 0.6] {
let mut s = Session::default();
let cam = Camera { target: Vec3::new(2.0, 3.0, 4.0), pitch: 1.4, roll: 0.7, fov, up, ..Default::default() };
for pivot in [Vec3::ZERO, Vec3::new(10.0, 20.0, 30.0)] {
let result = s
.execute(
"view.navigate",
&json!({"camera":cam,"motion":"orbit","delta":[30,80],"free_orbit":true,"viewport":[800,600],"pivot":pivot}),
)
.unwrap();
let to: Camera = serde_json::from_value(result["camera"].clone()).unwrap();
assert!(to.frame_coords(pivot).dist(cam.frame_coords(pivot)) < 1e-8);
assert!(((to.eye() - pivot).len() - (cam.eye() - pivot).len()).abs() < 1e-8);
let (ax, ay, _) = cam.to_screen(pivot, 800.0, 600.0, 200.0).unwrap();
let (bx, by, _) = to.to_screen(pivot, 800.0, 600.0, 200.0).unwrap();
assert!((ax - bx).abs() < 1e-6 && (ay - by).abs() < 1e-6);
assert_eq!((to.distance, to.fov), (cam.distance, cam.fov));
}
}
}
}
#[test]
fn continuous_perspective_orbit_keeps_near_and_far_pivots_on_screen() {
let mut s = Session::default();
@@ -257,6 +452,9 @@ mod tests {
json!({"motion":"pan","delta":[1e100,2],"viewport":[800,600]}),
json!({"motion":"orbit","delta":[1]}),
json!({"motion":"orbit","delta":[1,2],"pivot":[0,0,0]}),
json!({"motion":"orbit","delta":[1,2],"free_orbit":"true"}),
json!({"motion":"orbit","delta":[1,2],"free_orbit":1}),
json!({"motion":"orbit","delta":[1,2],"free_orbit":null}),
json!({"motion":"zoom","factor":0}),
json!({"motion":"zoom","factor":1e100}),
json!({"motion":"zoom","factor":1,"anchor":[1e100,0,0]}),