fabrilis/crates/scene/tests/mesh_mass.rs
loki5512344 190ef9cc4b Add design persistence, world rendering, metrics overlay and logs
M3: STL/OBJ import with mesh validation, mass/COM/inertia, material database, RON scene with content-addressed blobs and v0->v1 migration. M4: deterministic heightmap terrain, trimesh colliders, wgpu renderer with instancing and depth, winit app with 1 kHz physics, fly camera, on-screen metrics overlay and per-frame CSV/dev logs. Docs: UI, logging, network and headless CLI plans.
2026-11-16 23:40:39 +01:00

87 lines
2.5 KiB
Rust

//! M3 seam: an STL part is validated as a closed mesh and its mass is computed from
//! the material density.
#![allow(
clippy::unwrap_used,
reason = "test helper: a failure should abort the test"
)]
use fabrilis_core::units::KilogramsPerCubicMeter;
use fabrilis_materials::db::MaterialDb;
use fabrilis_materials::massprops::from_triangles;
use fabrilis_scene::import::{Mesh, from_stl};
/// A cube of the given side, centred at the origin, as an ASCII STL.
fn cube_stl(side: f64) -> String {
let h = side * 0.5;
let vertices = [
(-h, -h, -h),
(h, -h, -h),
(h, h, -h),
(-h, h, -h),
(-h, -h, h),
(h, -h, h),
(h, h, h),
(-h, h, h),
];
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 cube\n");
for triangle in triangles {
text.push_str(" facet normal 0 0 0\n outer loop\n");
for index in triangle {
let (x, y, z) = vertices[index];
text.push_str(&format!(" vertex {x} {y} {z}\n"));
}
text.push_str(" endloop\n endfacet\n");
}
text.push_str("endsolid cube\n");
text
}
#[test]
fn stl_cube_mass_matches_the_analytic_value() {
let mesh = from_stl(cube_stl(1.0).as_bytes()).expect("the cube parses");
let report = mesh.validate().expect("the cube is closed");
assert!((report.volume.get() - 1.0).abs() < 1e-12);
let steel = MaterialDb::with_builtins();
let density = steel.get("steel").expect("steel is built in").density;
let mass = from_triangles(&mesh.triangles().collect::<Vec<_>>(), density)
.expect("a closed cube has mass properties");
assert!((mass.mass.get() - 7_850.0).abs() < 1.0);
assert!(mass.center_of_mass.length() < 1e-12);
let inertia = mass.mass.get() / 6.0;
assert!((mass.inertia.cols()[0].x - inertia).abs() < 1e-6);
}
#[test]
fn millimetre_stl_scaled_to_metres_keeps_the_mass() {
let mesh = from_stl(cube_stl(1_000.0).as_bytes())
.expect("the cube parses")
.scaled(0.001);
let report = mesh.validate().expect("the cube is closed");
assert!((report.volume.get() - 1.0).abs() < 1e-9);
let density = KilogramsPerCubicMeter::new(2_700.0);
let mass = from_triangles(&mesh.triangles().collect::<Vec<_>>(), density)
.expect("a closed cube has mass properties");
assert!((mass.mass.get() - 2_700.0).abs() < 1.0);
}
#[test]
fn an_open_mesh_is_rejected_before_mass_is_computed() {
let mut mesh: Mesh = from_stl(cube_stl(1.0).as_bytes()).expect("the cube parses");
mesh.indices.pop();
assert!(mesh.validate().is_err());
}