//! M3 seam: an STL cart is saved, loaded back and its mass is compared with the //! analytic value. #![allow( clippy::unwrap_used, reason = "a test aborts on failure, which is the intent" )] use std::path::PathBuf; use std::sync::atomic::{AtomicU64, Ordering}; use fabrilis_materials::db::MaterialDb; use fabrilis_scene::model::{DESIGN_FILE, Design, Pose, add_part_from_stl}; use fabrilis_storage::project::Project; /// A directory removed on drop. struct TempDir { path: PathBuf, } impl TempDir { fn new(label: &str) -> Self { static COUNTER: AtomicU64 = AtomicU64::new(0); let counter = COUNTER.fetch_add(1, Ordering::Relaxed); let path = std::env::temp_dir().join(format!( "fabrilis-persistence-{label}-{}-{counter}", std::process::id() )); std::fs::create_dir_all(&path).unwrap(); Self { path } } } impl Drop for TempDir { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.path); } } /// A box with the given full extents, centred at the origin, as an ASCII STL. fn box_stl(x: f64, y: f64, z: f64) -> String { let (hx, hy, hz) = (x * 0.5, y * 0.5, z * 0.5); let vertices = [ (-hx, -hy, -hz), (hx, -hy, -hz), (hx, hy, -hz), (-hx, hy, -hz), (-hx, -hy, hz), (hx, -hy, hz), (hx, hy, hz), (-hx, hy, hz), ]; let triangles = [ [0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7], [0, 1, 5], [0, 5, 4], [3, 7, 6], [3, 6, 2], [0, 4, 7], [0, 7, 3], [1, 2, 6], [1, 6, 5], ]; let mut text = String::from("solid box\n"); for triangle in triangles { text.push_str("facet normal 0 0 0\nouter loop\n"); for index in triangle { let (vx, vy, vz) = vertices[index]; text.push_str(&format!("vertex {vx} {vy} {vz}\n")); } text.push_str("endloop\nendfacet\n"); } text.push_str("endsolid box\n"); text } fn pose_at(x: f64, y: f64, z: f64) -> Pose { Pose { position: [x, y, z], rotation: [1.0, 0.0, 0.0, 0.0], } } #[test] fn a_cart_round_trips_and_its_mass_matches_the_analytic_value() { let temp = TempDir::new("cart"); let project = Project::create(&temp.path).unwrap(); let mut blobs = project.blobs(); let mut design = Design::new("cart"); let body = box_stl(0.4, 0.2, 0.1); design.push( add_part_from_stl( &mut blobs, "body", body.as_bytes(), 1.0, "steel", pose_at(0.0, 0.0, 0.0), ) .unwrap(), ); let wheel = box_stl(0.1, 0.1, 0.1); for (name, y) in [("left", 0.15), ("right", -0.15)] { design.push( add_part_from_stl( &mut blobs, name, wheel.as_bytes(), 1.0, "aluminium", pose_at(0.0, y, -0.05), ) .unwrap(), ); } design.save(&project).unwrap(); let text = project.read_text(DESIGN_FILE).unwrap(); assert!(text.contains("format_version: 1")); assert!(text.contains("payload:")); assert!(project.path(DESIGN_FILE).is_file()); let loaded = Design::load(&project).unwrap(); assert_eq!(loaded.name, design.name); assert_eq!(loaded.parts, design.parts); let materials = MaterialDb::with_builtins(); let properties = loaded.mass_properties(&materials, &blobs).unwrap(); let expected = 7_850.0 * 0.4 * 0.2 * 0.1 + 2.0 * 2_700.0 * 0.1 * 0.1 * 0.1; let error = (properties.mass.get() - expected).abs() / expected; assert!(error < 0.01, "mass error {error} exceeds 1 %"); assert!(properties.center_of_mass.is_finite()); assert!( properties .inertia .cols() .iter() .all(|column| column.is_finite()) ); assert!(properties.inertia.determinant() > 0.0); } #[test] fn identical_meshes_share_one_blob_and_one_table_entry() { let temp = TempDir::new("dedup"); let project = Project::create(&temp.path).unwrap(); let mut blobs = project.blobs(); let wheel = box_stl(0.1, 0.1, 0.1); let first = add_part_from_stl( &mut blobs, "left", wheel.as_bytes(), 1.0, "aluminium", Pose::default(), ) .unwrap(); let second = add_part_from_stl( &mut blobs, "right", wheel.as_bytes(), 1.0, "aluminium", Pose::default(), ) .unwrap(); assert_eq!(first.mesh, second.mesh); let files = std::fs::read_dir(project.path("blobs")).unwrap().count(); assert_eq!(files, 1); } #[test] fn a_version_zero_scene_is_migrated_to_identity_poses() { let temp = TempDir::new("migrate"); let project = Project::create(&temp.path).unwrap(); let version_zero = "\ ( format_version: 0, payload: ( name: \"cart\", parts: [ ( name: \"body\", mesh: \"0123456789abcdef\", material: \"steel\", ), ], ), ) "; project.write_text(DESIGN_FILE, version_zero).unwrap(); let design = Design::load(&project).unwrap(); assert_eq!(design.parts.len(), 1); assert_eq!(design.parts[0].pose, Pose::default()); design.save(&project).unwrap(); let upgraded = project.read_text(DESIGN_FILE).unwrap(); assert!(upgraded.contains("format_version: 1")); assert_eq!(Design::load(&project).unwrap().parts, design.parts); } #[test] fn a_newer_scene_version_is_rejected() { let temp = TempDir::new("future"); let project = Project::create(&temp.path).unwrap(); let version_two = "(format_version: 2, payload: (name: \"cart\", parts: []))"; project.write_text(DESIGN_FILE, version_two).unwrap(); assert!(Design::load(&project).is_err()); } #[test] fn a_missing_scene_file_is_a_not_found_error() { let temp = TempDir::new("missing"); let project = Project::create(&temp.path).unwrap(); let error = Design::load(&project).unwrap_err(); let message = error.to_string(); assert!( message.contains("No such file") || message.contains("os error 2"), "{message}" ); }