Add design persistence, world rendering, metrics overlay and logs
Some checks failed
ci / check (macos-latest) (push) Has been cancelled
ci / check (ubuntu-latest) (push) Has been cancelled
ci / check (windows-latest) (push) Has been cancelled

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.
This commit is contained in:
Loki 2026-11-16 23:34:05 +01:00
parent 79928b645b
commit 93075ab1fd
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
107 changed files with 14785 additions and 46 deletions

View file

@ -0,0 +1,87 @@
//! 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());
}

View file

@ -0,0 +1,229 @@
//! 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}"
);
}