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.
This commit is contained in:
parent
878d144b77
commit
190ef9cc4b
111 changed files with 14801 additions and 63 deletions
|
|
@ -1 +1,144 @@
|
|||
//! fabrilis-materials :: massprops - stub (see docs/ARCHITECTURE.md)
|
||||
//! Mass, centre of mass and inertia tensor of a closed triangle mesh.
|
||||
//!
|
||||
//! The mesh is decomposed into signed tetrahedra from the origin:
|
||||
//! `V = a · (b × c) / 6` per triangle `(a, b, c)`. Summing the signed
|
||||
//! contributions over a watertight surface gives the volume, the first moment and
|
||||
//! the second moment directly, without any absolute-value branch.
|
||||
//!
|
||||
//! The caller must validate that the mesh is watertight and consistently oriented
|
||||
//! outwards (signed volume positive). A closed mesh with inverted winding yields a
|
||||
//! negative signed volume and is rejected here; an open mesh yields a wrong volume
|
||||
//! and cannot be detected from the triangle soup alone.
|
||||
|
||||
use fabrilis_core::units::{CubicMeters, Kilograms, KilogramsPerCubicMeter};
|
||||
use fabrilis_core::vec::{Mat3, Vec3};
|
||||
|
||||
/// Volumes at or below this bound (m³) are treated as degenerate.
|
||||
const MIN_VOLUME: f64 = 1e-12;
|
||||
|
||||
/// Mass properties of a solid, in SI units.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct MassProperties {
|
||||
/// Enclosed volume, m³.
|
||||
pub volume: CubicMeters,
|
||||
/// Mass, kg.
|
||||
pub mass: Kilograms,
|
||||
/// Centre of mass, m, in the mesh's axes.
|
||||
pub center_of_mass: Vec3,
|
||||
/// Inertia tensor about the centre of mass, kg·m², in world axes.
|
||||
pub inertia: Mat3,
|
||||
}
|
||||
|
||||
/// The signed volume enclosed by a triangle soup, m³.
|
||||
///
|
||||
/// Positive for a closed mesh wound outwards. No validation is performed here;
|
||||
/// [`from_triangles`] is the checked entry point.
|
||||
#[must_use]
|
||||
pub fn signed_volume(triangles: &[[Vec3; 3]]) -> f64 {
|
||||
triangles
|
||||
.iter()
|
||||
.map(|&[a, b, c]| a.dot(b.cross(c)) / 6.0)
|
||||
.sum()
|
||||
}
|
||||
|
||||
/// Mass properties of a closed, outward-oriented triangle mesh.
|
||||
///
|
||||
/// Returns `None` for empty or degenerate meshes (|volume| at or below
|
||||
/// `MIN_VOLUME`), for a signed volume that is not positive (inverted or
|
||||
/// inconsistent winding), for non-finite vertices or density, and for a
|
||||
/// zero/negative density. Watertightness itself must be validated by the caller.
|
||||
#[must_use]
|
||||
pub fn from_triangles(
|
||||
triangles: &[[Vec3; 3]],
|
||||
density: KilogramsPerCubicMeter,
|
||||
) -> Option<MassProperties> {
|
||||
let density = density.get();
|
||||
if !density.is_finite() || density <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let mut accumulator = MomentAccumulator::default();
|
||||
for &[a, b, c] in triangles {
|
||||
if !a.is_finite() || !b.is_finite() || !c.is_finite() {
|
||||
return None;
|
||||
}
|
||||
accumulator.add(a, b, c);
|
||||
}
|
||||
accumulator.finish(density)
|
||||
}
|
||||
|
||||
/// Second moment about the origin, stored as `(xx, yy, zz, xy, xz, yz)`.
|
||||
#[derive(Default)]
|
||||
struct MomentAccumulator {
|
||||
volume: f64,
|
||||
first: Vec3,
|
||||
second: [f64; 6],
|
||||
}
|
||||
|
||||
impl MomentAccumulator {
|
||||
/// Adds the tetrahedron `(0, a, b, c)` with its signed volume.
|
||||
fn add(&mut self, a: Vec3, b: Vec3, c: Vec3) {
|
||||
let s = a + b + c;
|
||||
let tetra_volume = a.dot(b.cross(c)) / 6.0;
|
||||
self.volume += tetra_volume;
|
||||
// Centroid of the tetrahedron is `s / 4`.
|
||||
self.first += s * (tetra_volume * 0.25);
|
||||
let scale = tetra_volume / 20.0;
|
||||
let [xx, yy, zz, xy, xz, yz] = &mut self.second;
|
||||
*xx += scale * (a.x * a.x + b.x * b.x + c.x * c.x + s.x * s.x);
|
||||
*yy += scale * (a.y * a.y + b.y * b.y + c.y * c.y + s.y * s.y);
|
||||
*zz += scale * (a.z * a.z + b.z * b.z + c.z * c.z + s.z * s.z);
|
||||
*xy += scale * (a.x * a.y + b.x * b.y + c.x * c.y + s.x * s.y);
|
||||
*xz += scale * (a.x * a.z + b.x * b.z + c.x * c.z + s.x * s.z);
|
||||
*yz += scale * (a.y * a.z + b.y * b.z + c.y * c.z + s.y * s.z);
|
||||
}
|
||||
|
||||
/// Applies the parallel-axis theorem and builds the result.
|
||||
fn finish(&self, density: f64) -> Option<MassProperties> {
|
||||
if !self.volume.is_finite() || self.volume <= MIN_VOLUME {
|
||||
return None;
|
||||
}
|
||||
let mass = density * self.volume;
|
||||
let center = self.first / self.volume;
|
||||
if !mass.is_finite() || !center.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let inertia = self.inertia_about(center, mass);
|
||||
if !inertia_is_finite(inertia) {
|
||||
return None;
|
||||
}
|
||||
Some(MassProperties {
|
||||
volume: CubicMeters(self.volume),
|
||||
mass: Kilograms(mass),
|
||||
center_of_mass: center,
|
||||
inertia,
|
||||
})
|
||||
}
|
||||
|
||||
/// Inertia about the given point: `tr(C) I - C` shifted by the parallel axis.
|
||||
fn inertia_about(&self, center: Vec3, mass: f64) -> Mat3 {
|
||||
let [xx, yy, zz, xy, xz, yz] = self.second;
|
||||
let (dx, dy, dz) = (center.x, center.y, center.z);
|
||||
// Covariance is geometric (units of m⁵); density turns it into kg·m².
|
||||
let density = mass / self.volume;
|
||||
// `|d|² I - d dᵀ` moves the origin of the moments to `center`.
|
||||
let i_xx = density * (yy + zz) - mass * (dy * dy + dz * dz);
|
||||
let i_yy = density * (xx + zz) - mass * (dx * dx + dz * dz);
|
||||
let i_zz = density * (xx + yy) - mass * (dx * dx + dy * dy);
|
||||
let i_xy = mass * dx * dy - density * xy;
|
||||
let i_xz = mass * dx * dz - density * xz;
|
||||
let i_yz = mass * dy * dz - density * yz;
|
||||
Mat3::from_cols(
|
||||
Vec3::new(i_xx, i_xy, i_xz),
|
||||
Vec3::new(i_xy, i_yy, i_yz),
|
||||
Vec3::new(i_xz, i_yz, i_zz),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// True when every entry of the matrix is finite.
|
||||
fn inertia_is_finite(inertia: Mat3) -> bool {
|
||||
inertia.cols().iter().all(|column| column.is_finite())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue