Files
solidcraft/crates/engine/src/cmd/motion_tests.rs
T

408 lines
21 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use serde_json::{Value, json};
use solidcraft_geom::Vec3;
use crate::Session;
fn run(s: &mut Session, id: &str, p: Value) -> Value {
match s.execute(id, &p) {
Ok(v) => v,
Err(e) => panic!("{id} {p}: {e}"),
}
}
fn occ(s: &Session, component: &str) -> u64 {
let c = s.doc.find_component(component).unwrap_or(0);
s.doc.occurrence_of(c).map(|o| o.id).unwrap_or(0)
}
/// Where B's corner (20, 0, 0) is now.
fn b_x(s: &Session) -> f64 {
super::world_point(s, occ(s, "B"), Vec3::new(20.0, 0.0, 0.0)).x
}
/// Cubes A (grounded, x 0…10) and B (x 20…30) on a slider along x: B's −x face on A's +x face
/// at value 0, B 10 mm away to start.
fn rail() -> Session {
let mut s = Session::default();
for (name, x) in [("A", 0), ("B", 20)] {
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": name}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [x, 0, 0]}));
}
run(&mut s, "component.activate", json!({"component": "root"}));
let a = occ(&s, "A");
run(&mut s, "occurrence.ground", json!({"occurrence": a, "grounded": true}));
run(&mut s, "joint.create", json!({"type": "slider", "a": {"face": [10, 5, 5]}, "b": {"face": [20, 5, 5]}, "name": "Rail"}));
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 10}));
assert!((b_x(&s) - 20.0).abs() < 1e-6, "{}", b_x(&s));
s
}
#[test]
fn contact_sets_stop_a_driven_joint_at_touching() {
let mut s = rail();
// No contact: B passes into A.
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": -5}));
assert!((b_x(&s) - 5.0).abs() < 1e-6);
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 10}));
run(&mut s, "contact.create", json!({"occurrences": ["A", "B"]}));
let r = run(&mut s, "joint.drive", json!({"joint": "Rail", "value": -5}));
assert!(r["stopped_by_contact"].is_object(), "{r}");
// Stopped where the faces touch (to the search's resolution, 15 mm / 2^14).
assert!((b_x(&s) - 10.0).abs() < 2e-3, "{}", b_x(&s));
assert!(b_x(&s) >= 10.0 - 2e-3, "a touch, not a pass");
assert_eq!(run(&mut s, "contact.check", json!({}))["collisions"].as_array().map(Vec::len), Some(0));
// Away is free; contact off lets it through again.
let r = run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 3}));
assert!(r["stopped_by_contact"].is_null() && (b_x(&s) - 13.0).abs() < 1e-6, "{r}");
run(&mut s, "contact.disable_all", json!({}));
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": -5}));
assert!((b_x(&s) - 5.0).abs() < 1e-6);
// Suppressed sets don't count; Enable All Contact puts every occurrence in one.
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 10}));
run(&mut s, "contact.enable_sets", json!({}));
run(&mut s, "contact.edit", json!({"set": "Contact Set1", "suppressed": true}));
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": -5}));
assert!((b_x(&s) - 5.0).abs() < 1e-6);
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 10}));
run(&mut s, "contact.enable_all", json!({}));
run(&mut s, "joint.drive", json!({"joint": "Rail", "value": -5}));
assert!((b_x(&s) - 10.0).abs() < 2e-3);
assert!(s.execute("contact.create", &json!({"occurrences": ["A"]})).is_err());
}
#[test]
fn contact_uses_preserved_body_placements_after_component_reassignment() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Transferred"}));
let component = run(&mut s, "component.create", json!({"name": "Carrier"}));
run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "translate": [100, 0, 0]}));
run(&mut s, "component.move_bodies", json!({"bodies": ["Transferred"], "component": component["component"]}));
run(&mut s, "component.activate", json!({"component": "root"}));
let probe = run(&mut s, "component.create", json!({"name": "Probe"}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [5, 0, 0]}));
run(&mut s, "contact.create", json!({"occurrences": [component["occurrence"], probe["occurrence"]]}));
let collisions = run(&mut s, "contact.check", json!({}));
assert_eq!(collisions["collisions"].as_array().map(Vec::len), Some(1), "body stayed at x=0: {collisions}");
run(&mut s, "occurrence.move", json!({"occurrence": probe["occurrence"], "translate": [100, 0, 0]}));
let collisions = run(&mut s, "contact.check", json!({}));
assert_eq!(collisions["collisions"].as_array().map(Vec::len), Some(0), "probe moved away: {collisions}");
}
#[test]
fn automatic_explode_centres_follow_reassigned_bodies() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Transferred"}));
let component = run(&mut s, "component.create", json!({"name": "Carrier"}));
run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "translate": [100, 0, 0]}));
run(&mut s, "component.move_bodies", json!({"bodies": ["Transferred"], "component": component["component"]}));
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": "Neighbour"}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [20, 0, 0]}));
let exploded = run(&mut s, "explode.create", json!({"scale": 1}));
let moves = exploded["moves"].as_array().unwrap();
let carrier = moves.iter().find(|m| m["occurrence"] == component["occurrence"]).unwrap();
assert!((carrier["translate"][0].as_f64().unwrap() + 10.0).abs() < 1e-6, "{exploded}");
}
#[test]
fn motion_study_plays_and_exports() {
let mut s = rail();
let r = run(&mut s, "motion.study", json!({"name": "Push", "steps": 100, "keys": [{"joint": "Rail", "points": [[0, 10], [100, -5]]}]}));
assert_eq!(r["keys"], 1);
run(&mut s, "motion.play", json!({"study": "Push", "step": 50}));
assert!((b_x(&s) - 12.5).abs() < 1e-6, "{}", b_x(&s));
// With contact, the end of the study stops at touching.
run(&mut s, "contact.create", json!({"occurrences": ["A", "B"]}));
let r = run(&mut s, "motion.play", json!({"study": "Push", "step": 100}));
assert!(r["stopped_by_contact"].is_object() && (b_x(&s) - 10.0).abs() < 2e-3, "{r}");
// Export: positions per sample; the design keeps its joint value.
run(&mut s, "motion.play", json!({"study": "Push", "step": 0}));
let before = s.doc.clone();
let e = run(&mut s, "motion.export", json!({"study": "Push", "samples": 5}));
assert_eq!(*s.doc, *before);
let rows = e["samples"].as_array().cloned().unwrap_or_default();
assert_eq!(rows.len(), 5);
let bx = |row: &Value| {
let p = row["positions"].as_array().and_then(|a| a.iter().find(|p| p["occurrence"] == "B:1")).cloned().unwrap_or_default();
p["pose"]["origin"][0].as_f64().unwrap_or(f64::NAN)
};
// The B occurrence's origin moves by the slider value (it starts at 0 offset with value 0 at 10 mm… so 10 → −5).
assert!((bx(&rows[1]) - bx(&rows[0]) + 3.75).abs() < 1e-6, "{e}");
assert!(rows[4]["stopped_by_contact"].is_object());
let dir = solidcraft_io::vfs::temp_dir().join(format!("solidcraft-motion-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let csv = dir.join("push.csv");
run(&mut s, "motion.export", json!({"study": "Push", "samples": 3, "path": csv.to_string_lossy()}));
let text = std::fs::read_to_string(&csv).unwrap();
assert!(text.starts_with("step,occurrence,x,y,z") && text.lines().count() == 1 + 3 * 2, "{text}");
// For an agent (#205) the export lands only inside its folders.
let root = dir.join("root");
std::fs::create_dir_all(&root).unwrap();
solidcraft_io::vfs::scoped(Some(solidcraft_io::vfs::PathScope::new([root.clone()])), || {
run(&mut s, "motion.export", json!({"study": "Push", "samples": 2, "path": root.join("in.json").to_string_lossy()}));
let e = s.execute("motion.export", &json!({"study": "Push", "samples": 2, "path": dir.join("out.json").to_string_lossy()})).unwrap_err();
assert!(e.to_string().contains("outside the folders this agent may use"), "{e}");
});
assert!(root.join("in.json").exists() && !dir.join("out.json").exists());
let _ = std::fs::remove_dir_all(&dir);
assert!(s.execute("motion.study", &json!({"keys": [{"joint": "Rail", "index": 3, "points": [[0, 1]]}]})).is_err());
run(&mut s, "motion.delete", json!({"study": "Push"}));
assert_eq!(run(&mut s, "motion.list", json!({}))["studies"].as_array().map(Vec::len), Some(0));
}
#[test]
fn exploded_views_show_without_changing_the_design() {
let mut s = rail();
let before = s.doc.occurrences.clone();
let r = run(&mut s, "explode.create", json!({"name": "Apart", "scale": 1}));
assert_eq!(r["moves"].as_array().map(Vec::len), Some(2), "{r}");
run(&mut s, "explode.show", json!({"view": "Apart"}));
// Centres at x 5 and 25 → middle 15: each moves 10 mm further out.
let bb = |s: &mut Session, b: &str| run(s, "inspect.measure", json!({"bodies": [b]}))["bodies"][0]["bbox"]["min"][0].as_f64().unwrap_or(f64::NAN);
let names: Vec<String> = s.world_state().bodies.iter().map(|b| b.name.clone()).collect();
assert!((bb(&mut s, &names[0]) + 10.0).abs() < 1e-6, "{names:?}");
assert!((bb(&mut s, &names[1]) - 30.0).abs() < 1e-6);
assert_eq!(s.doc.occurrences, before, "shown only");
run(&mut s, "explode.show", json!({"view": null}));
assert!(bb(&mut s, &names[0]).abs() < 1e-6);
run(&mut s, "explode.create", json!({"name": "Hand", "moves": [{"occurrence": "B", "translate": [0, 0, 50]}]}));
run(&mut s, "explode.show", json!({"view": "Hand"}));
assert!(
(run(&mut s, "inspect.measure", json!({"bodies": [names[1]]}))["bodies"][0]["bbox"]["min"][2].as_f64().unwrap_or(0.0) - 50.0).abs() < 1e-6
);
assert_eq!(run(&mut s, "explode.list", json!({}))["views"].as_array().map(Vec::len), Some(2));
}
/// Pictures drawn by the CPU rasterizer (the engine has no GPU): real views of the scene.
fn cpu(scene: &crate::render::Scene, cam: &crate::render::Camera, w: u32, h: u32) -> Result<crate::capture::Picture, String> {
Ok(crate::capture::Picture { rgba: crate::render::render(scene, cam, w as usize, h as usize).rgba, renderer: "cpu" })
}
/// A folder of its own for one test's files.
fn out_dir(test: &str) -> std::path::PathBuf {
let dir = solidcraft_io::vfs::temp_dir().join(format!("solidcraft-anim-{test}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// A GIF's logical screen size and the number of its frames (image descriptors).
fn gif_frames(gif: &[u8]) -> (usize, usize, usize) {
let u16_at = |i: usize| usize::from(u16::from_le_bytes([gif[i], gif[i + 1]]));
assert_eq!(&gif[..6], b"GIF89a");
let mut at = 13 + if gif[10] & 0x80 != 0 { 3 << ((gif[10] & 7) + 1) } else { 0 };
let mut frames = 0;
// Each block: a size byte and that many bytes, up to a 0.
let skip_blocks = |mut at: usize| {
while gif[at] != 0 {
at += usize::from(gif[at]) + 1;
}
at + 1
};
loop {
match gif[at] {
0x3B => return (u16_at(6), u16_at(8), frames),
0x21 => at = skip_blocks(at + 2),
0x2C => {
frames += 1;
at = skip_blocks(at + 11);
}
b => panic!("unexpected GIF block {b:#x}"),
}
}
}
/// A PNG's width and height (its IHDR).
fn png_size(png: &[u8]) -> (u32, u32) {
let be = |i: usize| u32::from_be_bytes([png[i], png[i + 1], png[i + 2], png[i + 3]]);
(be(16), be(20))
}
#[test]
fn an_animation_has_the_frames_and_the_size_asked_for() {
let mut s = rail();
run(&mut s, "motion.study", json!({"name": "Push", "steps": 10, "keys": [{"joint": "Rail", "points": [[0, 10], [10, 0]]}]}));
let dir = out_dir("size");
let before = s.doc.clone();
let gif = dir.join("push.gif");
let p = json!({"study": "Push", "path": gif.to_string_lossy(), "samples": 6, "width": 64, "height": 40, "fps": 25});
let r = super::animation::export_with(&s, &p, Some(&cpu)).unwrap();
assert_eq!(
(r["format"].as_str(), r["frames"].as_u64(), r["width"].as_u64(), r["height"].as_u64()),
(Some("gif"), Some(6), Some(64), Some(40)),
"{r}"
);
assert_eq!(r["fps"], 25.0, "{r}");
let bytes = std::fs::read(&gif).unwrap();
assert_eq!(r["bytes"].as_u64(), Some(bytes.len() as u64));
assert_eq!(gif_frames(&bytes), (64, 40, 6));
// Without samples: the study's steps + 1.
let r =
super::animation::export_with(&s, &json!({"study": "Push", "path": gif.to_string_lossy(), "width": 32, "height": 32}), Some(&cpu)).unwrap();
assert_eq!(gif_frames(&std::fs::read(&gif).unwrap()), (32, 32, 11), "{r}");
// A folder of PNG frames, made where missing; a joint driven instead of a study.
let frames = dir.join("frames").join("rail");
let p = json!({"joint": "Rail", "from": 10, "to": 0, "path": frames.to_string_lossy(), "samples": 3, "width": 48, "height": 30});
let r = super::animation::export_with(&s, &p, Some(&cpu)).unwrap();
assert_eq!((r["format"].as_str(), r["frames"].as_u64()), (Some("png"), Some(3)), "{r}");
let mut names: Vec<String> = std::fs::read_dir(&frames).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().to_string()).collect();
names.sort();
assert_eq!(names, ["frame-0001.png", "frame-0002.png", "frame-0003.png"]);
assert!(names.iter().all(|n| png_size(&std::fs::read(frames.join(n)).unwrap()) == (48, 30)));
// A shorter export into the same folder leaves no frames of a longer one behind, past a gap
// too; other files stay.
std::fs::write(frames.join("notes.txt"), "mine").unwrap();
std::fs::write(frames.join("frame-0005.png"), "older").unwrap();
super::animation::export_with(&s, &json!({"joint": "Rail", "from": 10, "to": 0, "path": frames.to_string_lossy(), "samples": 2}), Some(&cpu))
.unwrap();
let mut names: Vec<String> = std::fs::read_dir(&frames).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().to_string()).collect();
names.sort();
assert_eq!(names, ["frame-0001.png", "frame-0002.png", "notes.txt"]);
assert_eq!(*s.doc, *before, "the design is not changed");
// Through the registry, with the renderer the host installed.
crate::capture::install_test_renderer();
let r = run(&mut s, "motion.export_animation", json!({"study": "Push", "path": gif.to_string_lossy(), "samples": 2, "width": 16, "height": 16}));
assert_eq!(r["frames"], 2, "{r}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_two_frame_drive_gives_two_different_frames() {
let s = rail();
let dir = out_dir("two");
let frames = |name: &str, to: f64| {
let folder = dir.join(name);
let p = json!({"joint": "Rail", "from": 10, "to": to, "path": folder.to_string_lossy(), "samples": 2, "width": 120, "height": 80});
super::animation::export_with(&s, &p, Some(&cpu)).unwrap();
(std::fs::read(folder.join("frame-0001.png")).unwrap(), std::fs::read(folder.join("frame-0002.png")).unwrap())
};
let (a, b) = frames("moved", -5.0);
assert_ne!(a, b, "B moved 15 mm between the frames");
// Held still, the two frames are the same picture: the difference is the motion.
let (c, d) = frames("still", 10.0);
assert_eq!(c, d);
assert_eq!(a, c, "both start at 10 mm");
// Without the ground grid and the axes, the same pose is another picture.
let bare = dir.join("bare");
let p = json!({"joint": "Rail", "from": 10, "to": 10, "path": bare.to_string_lossy(), "samples": 2, "width": 120, "height": 80, "grid": false, "axes": false});
super::animation::export_with(&s, &p, Some(&cpu)).unwrap();
assert_ne!(std::fs::read(bare.join("frame-0001.png")).unwrap(), c);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn an_animation_is_drawn_from_the_camera_given() {
let mut s = rail();
let dir = out_dir("camera");
// A renderer is 'static (hosts install it for the process): it shares what it saw.
let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let record = {
let seen = seen.clone();
move |scene: &crate::render::Scene, cam: &crate::render::Camera, w: u32, h: u32| {
seen.lock().unwrap().push(*cam);
cpu(scene, cam, w, h)
}
};
let path = dir.join("cam.gif");
let base = json!({"joint": "Rail", "from": 10, "to": 0, "path": path.to_string_lossy(), "samples": 3, "width": 40, "height": 30});
let with = |camera: Value| {
let mut p = base.clone();
p["camera"] = camera;
p
};
// ui.camera's reply, every field.
let reply = json!({"target": [1.0, 2.0, 3.0], "yaw": 0.5, "pitch": -0.25, "roll": 0.1, "distance": 300.0, "fov": 0.6, "up": "y"});
super::animation::export_with(&s, &with(reply.clone()), Some(&record)).unwrap();
let cams = std::mem::take(&mut *seen.lock().unwrap());
assert_eq!(cams.len(), 3);
assert!(cams.iter().all(|c| json!(c) == reply), "{cams:?}");
// Fields left out keep the default view: view.apply's camera.
run(&mut s, "view.apply", json!({"camera": {"target": [0, 0, 0], "yaw": 1, "pitch": 0.2, "distance": 80}}));
super::animation::export_with(&s, &with(json!({"yaw": 2.0})), Some(&record)).unwrap();
let cams = std::mem::take(&mut *seen.lock().unwrap());
assert!(cams.iter().all(|c| c.yaw == 2.0 && c.pitch == 0.2 && c.distance == 80.0), "{cams:?}");
// Without one: view.apply's camera as it is; and another camera draws another picture.
super::animation::export_with(&s, &base, Some(&record)).unwrap();
assert!(seen.lock().unwrap().iter().all(|c| Some(*c) == s.camera));
let pictures = |camera: Value| {
let folder = dir.join(format!("p{}", camera["yaw"]));
let mut p = with(camera);
p["path"] = json!(folder.to_string_lossy());
super::animation::export_with(&s, &p, Some(&cpu)).unwrap();
std::fs::read(folder.join("frame-0001.png")).unwrap()
};
assert_ne!(pictures(json!({"yaw": 0.3})), pictures(json!({"yaw": 1.3})));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn hostile_animation_params_are_refused() {
let mut s = rail();
run(&mut s, "motion.study", json!({"name": "Push", "steps": 10, "keys": [{"joint": "Rail", "points": [[0, 10], [10, 0]]}]}));
let dir = out_dir("hostile");
let gif = dir.join("x.gif").to_string_lossy().to_string();
let base = json!({"study": "Push", "path": gif, "samples": 2, "width": 16, "height": 16});
let refused = |change: Value| {
let mut p = base.clone();
if let (Some(p), Some(change)) = (p.as_object_mut(), change.as_object()) {
for (k, v) in change {
if v.is_null() {
p.remove(k);
} else {
p.insert(k.clone(), v.clone());
}
}
}
super::animation::export_with(&s, &p, Some(&cpu)).err().map(|e| e.to_string())
};
for change in [
json!({"samples": 0}),
json!({"samples": 1}),
json!({"samples": 1e9}),
json!({"samples": "many"}),
json!({"samples": 1001}),
json!({"width": 1e9}),
json!({"width": -16}),
json!({"height": 3}),
json!({"height": f64::MAX}),
json!({"fps": 0}),
json!({"fps": 1e9}),
json!({"path": ""}),
json!({"path": null}),
json!({"path": "one.png"}),
json!({"path": "a\u{0}b.gif"}),
json!({"path": "x".repeat(5000)}),
json!({"camera": "iso"}),
json!({"camera": {"distance": -1}}),
json!({"camera": {"distance": 1e300}}),
json!({"camera": {"pitch": 4}}),
json!({"camera": {"fov": 9}}),
json!({"camera": {"target": [1, 2]}}),
json!({"camera": {"fit": true}}),
json!({"grid": "no"}),
json!({"axes": 1}),
json!({"study": null}),
json!({"study": "Nope"}),
json!({"study": null, "joint": "Rail"}),
// 4096 × 4096 frames: a GIF holds four of them.
json!({"width": 4096, "height": 4096, "samples": 5}),
] {
assert!(refused(change.clone()).is_some(), "{change} was accepted");
}
let e = refused(json!({"width": 4096, "height": 4096, "samples": 5})).unwrap();
assert!(e.contains("2…4 for 4096 × 4096"), "{e}");
assert!(super::animation::export_with(&s, &base, None).unwrap_err().to_string().contains("no renderer"));
assert!(!std::path::Path::new(&gif).exists(), "nothing refused was written");
// An agent's export lands only inside its folders.
let root = dir.join("root");
std::fs::create_dir_all(&root).unwrap();
solidcraft_io::vfs::scoped(Some(solidcraft_io::vfs::PathScope::new([root.clone()])), || {
let e = refused(json!({"path": dir.join("out").to_string_lossy()})).unwrap();
assert!(e.contains("outside the folders this agent may use"), "{e}");
assert!(refused(json!({"path": root.join("in.gif").to_string_lossy()})).is_none());
});
assert!(root.join("in.gif").exists() && !dir.join("out").exists());
let _ = std::fs::remove_dir_all(&dir);
}