motion.list gives study keys in motion.study units (#9741) (#9760)

* claim: #9741

* motion.list gives study keys in motion.study units

Angle keys came back in radians while motion.study takes degrees, so an
agent that listed a study and wrote it back changed every angle by a
factor of 57.3, silently. Convert key values on the way out like
joint.list does (degrees for angles, mm for distances), give each key a
unit and its joint's name, and add a round-trip regression test.

---------

Co-authored-by: Dahak Builds <dahakbuilds@users.noreply.github.com>
This commit is contained in:
Dahak Builds
2026-10-30 23:03:40 +00:00
committed by GitHub
parent 2cf8df2ef6
commit 646eb97eaf
2 changed files with 53 additions and 1 deletions
+24 -1
View File
@@ -979,7 +979,30 @@ fn keep_sampled_trace(
}
fn list_studies(s: &mut Session, _p: &Value) -> Result<Value> {
Ok(json!({"studies": s.doc.assembly.studies, "traces": associative::list(s)}))
let studies: Vec<Value> = s.doc.assembly.studies.iter().map(|m| study_json(s, m)).collect();
Ok(json!({"studies": studies, "traces": associative::list(s)}))
}
/// A study as `motion.list` gives it: key values in the units `motion.study` takes — degrees
/// for angles, mm for distances, like `joint.list`'s values — each key with its joint's name,
/// so a listed study can be fed back to `motion.study` unchanged.
fn study_json(s: &Session, m: &MotionStudy) -> Value {
let keys: Vec<Value> = m
.keys
.iter()
.map(|k| {
let joint = s.doc.assembly.joints.iter().find(|j| j.id == k.joint);
let angle = joint.is_some_and(|j| j.kind.is_angle(k.index));
json!({
"joint": k.joint,
"joint_name": joint.map(|j| j.name.clone()).unwrap_or_else(|| k.joint.to_string()),
"index": k.index,
"unit": if angle { "deg" } else { "mm" },
"points": k.points.iter().map(|(step, v)| json!([step, if angle { v.to_degrees() } else { *v }])).collect::<Vec<_>>(),
})
})
.collect();
json!({"id": m.id, "name": m.name, "steps": m.steps, "keys": keys})
}
fn delete_study(s: &mut Session, p: &Value) -> Result<Value> {
+29
View File
@@ -639,6 +639,35 @@ fn motion_study_plays_and_exports() {
assert_eq!(run(&mut s, "motion.list", json!({}))["studies"].as_array().map(Vec::len), Some(0));
}
/// Regression (#9741): `motion.study` takes degrees for angle keys, so `motion.list` must give
/// the keys back in those same units — feeding a listed study straight back to `motion.study`
/// must not turn a 90° key into 1.57°.
#[test]
fn motion_list_round_trips_key_units_through_motion_study() {
let hinge_deg = |s: &mut Session| {
run(s, "joint.list", json!({}))["joints"]
.as_array()
.and_then(|a| a.iter().find(|j| j["name"] == "Hinge").cloned())
.and_then(|j| j["values"][0]["value"].as_f64())
.unwrap_or(f64::NAN)
};
let mut s = hinged_plates(0.0);
run(&mut s, "motion.study", json!({"name": "Turn", "steps": 10, "keys": [{"joint": "Hinge", "points": [[0, 0], [10, 90]]}]}));
run(&mut s, "motion.play", json!({"study": "Turn", "step": 10}));
assert!((hinge_deg(&mut s) - 90.0).abs() < 1e-6, "playing step 10 gave {}°", hinge_deg(&mut s));
let listed = run(&mut s, "motion.list", json!({}));
let key = &listed["studies"][0]["keys"][0];
assert_eq!(key["joint_name"], "Hinge");
assert_eq!(key["unit"], "deg");
assert!((key["points"][1][1].as_f64().unwrap_or(f64::NAN) - 90.0).abs() < 1e-6, "{listed}");
// The listed keys go back to `motion.study` unchanged and the study still plays 90°.
let keys = listed["studies"][0]["keys"].clone();
run(&mut s, "motion.study", json!({"study": "Turn", "keys": keys}));
run(&mut s, "motion.play", json!({"study": "Turn", "step": 0}));
run(&mut s, "motion.play", json!({"study": "Turn", "step": 10}));
assert!((hinge_deg(&mut s) - 90.0).abs() < 1e-6, "the round-tripped study played {}°", hinge_deg(&mut s));
}
#[test]
fn exploded_views_show_without_changing_the_design() {
let mut s = rail();