A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server
strict: judge volume by volume / surface_area^1.5, not by the bounding box
Dead Internetaa45ed93dd2f
diff --git a/crates/forma-core/src/invariants.rs b/crates/forma-core/src/invariants.rsindex 5897bef..6b56c4e 100644--- a/crates/forma-core/src/invariants.rs+++ b/crates/forma-core/src/invariants.rs@@ -5,6 +5,9 @@ use crate::mesh::PolyMesh;use serde::{Deserialize, Serialize};use std::collections::BTreeMap;+/// Smallest `volume / surface_area^1.5` a solid may have; see [`MeshInvariants::compactness`].+pub(crate) const MIN_COMPACTNESS: f64 = 1e-9;+#[inline]pub(crate) fn dsu_find(parent: &mut [u32], mut x: u32) -> u32 {while parent[x as usize] != x {@@ -197,16 +200,19 @@ impl MeshInvariants {}}- /// Scale-invariant check for zero or collapsed/pancake volume (`mean thickness <= 1e-9 * bbox_diag`).+ /// `volume / surface_area^1.5`, a number with no unit: 0.068 for a cube, 0.094 for a sphere and+ /// about `thickness / (2.8 * width)` for a sheet. It does not change when the part is scaled, until a+ /// face area stops fitting in an f64 at sizes around 1e77.+ pub(crate) fn compactness(&self) -> f64 {+ self.signed_volume / self.surface_area.powf(1.5)+ }++ /// True for a part with no volume to build on: empty, inside out, or a sheet whose thickness is about+ /// a billionth of its width (`compactness() <= MIN_COMPACTNESS`). A 0.1 mm cube passes where an absolute+ /// floor such as `1e-12` cubic units would refuse it, and so do a long thin bar and copies far apart,+ /// which a floor on the bounding box would refuse.pub fn has_negligible_volume(&self) -> bool {- if !(self.signed_volume.is_finite() && self.signed_volume > 0.0) {- return true;- }- let diag = (self.size[0] * self.size[0] + self.size[1] * self.size[1] + self.size[2] * self.size[2]).sqrt();- if !(diag.is_finite() && diag > 0.0 && self.surface_area.is_finite() && self.surface_area > 0.0) {- return true;- }- self.volume <= 1e-9 * self.surface_area * diag+ !(self.signed_volume.is_finite() && self.surface_area > 0.0 && self.compactness() > MIN_COMPACTNESS)}pub fn is_valid_solid(&self) -> bool {diff --git a/crates/forma-core/src/scene.rs b/crates/forma-core/src/scene.rsindex f2cf2cb..8b9224f 100644--- a/crates/forma-core/src/scene.rs+++ b/crates/forma-core/src/scene.rs@@ -266,7 +266,12 @@ impl Scene {if inv.has_negligible_volume() {return Err(SceneError::InvariantViolation {part: name,- reason: format!("zero or negligible volume solid (vol={:.3e})", inv.signed_volume),+ reason: format!(+ "zero or negligible volume solid (vol={:.3e}, vol/area^1.5={:.1e}, minimum {:.0e})",+ inv.signed_volume,+ inv.compactness(),+ crate::invariants::MIN_COMPACTNESS+ ),});}if inv.components > 1 && crate::boolean::has_overlapping_components(&mesh) {diff --git a/crates/forma-core/tests/kernel_tests.rs b/crates/forma-core/tests/kernel_tests.rsindex 4b512f8..eb552b4 100644--- a/crates/forma-core/tests/kernel_tests.rs+++ b/crates/forma-core/tests/kernel_tests.rs@@ -858,3 +858,70 @@ fn test_issues_10_11_twisted_loft_shell_cut_and_scale_invariant_vol() {assert!(cinv.is_valid_solid() && cinv.genus == 0, "Issue #11 loft cut SUB={sub}: {cinv:?}");}}++#[test]+fn test_strict_volume_floor_is_relative_to_the_part() {+ use forma_core::primitives::make_box;+ let strict = || {+ let mut s = Scene::new();+ s.strict_manifold = true;+ s+ };++ // A real solid passes at any size, down to a 0.1 mm cube that an absolute 1e-12 floor refused.+ for size in [1e-5, 1e-4, 1e-2, 1.0, 1e3, 1e30] {+ strict()+ .set_part("cube", make_box(size, size, size, 1))+ .unwrap_or_else(|e| panic!("cube of {size} refused: {e:?}"));+ }+ // Long and thin is not flat.+ for (x, y, z) in [(1000.0, 1.0, 0.001), (1e5, 1.0, 1.0), (1e6, 1e-3, 1e-3)] {+ strict()+ .set_part("bar", make_box(x, y, z, 1))+ .unwrap_or_else(|e| panic!("{x} x {y} x {z} refused: {e:?}"));+ }+ // Copies far apart are small next to their bounding box, but each one is solid.+ for gap in [1e3, 1e6, 1e9, 1e12] {+ let mut s = strict();+ s.add_box("b", 1.0, 1.0, 1.0, Vec3::ZERO).unwrap();+ let inv = s+ .array_linear("b", 3, Vec3::new(gap, 0.0, 0.0), true)+ .unwrap_or_else(|e| panic!("3 copies {gap} apart refused: {e:?}"));+ assert_eq!(inv.components, 3);+ }+ // A sheet is refused once its thickness drops under about a billionth of its width. The first three+ // pairs sit a factor of ten either side of that line, the last pair about 1.8 above and 1.4 below.+ for (side, thick) in [(1000.0, 1e-5), (1e4, 1e-4), (100.0, 1e-6), (1000.0, 5e-6)] {+ strict()+ .set_part("sheet", make_box(side, side, thick, 1))+ .unwrap_or_else(|e| panic!("{side} x {side} x {thick} refused: {e:?}"));+ }+ for (side, thick) in [(1000.0, 1e-6), (1e4, 1e-5), (100.0, 1e-7), (1000.0, 2e-6)] {+ let err = strict().set_part("sheet", make_box(side, side, thick, 1)).unwrap_err();+ assert!(format!("{err:?}").contains("negligible volume"), "{side} x {side} x {thick}: {err:?}");+ }++ // Without strict the part is kept, and only the sheet is warned about.+ let mut s = Scene::new();+ s.set_part("cube", make_box(1e-4, 1e-4, 1e-4, 1)).unwrap();+ s.set_part("sheet", make_box(1e3, 1e3, 1e-6, 1)).unwrap();+ let obs = s.observe(0.0, None);+ assert!(obs.warnings.iter().any(|w| w.contains("part 'sheet' invalid solid")), "{:?}", obs.warnings);+ assert!(!obs.warnings.iter().any(|w| w.contains("part 'cube'")), "{:?}", obs.warnings);++ // The library predicate agrees with strict.+ assert!(MeshInvariants::compute(&make_box(1e-4, 1e-4, 1e-4, 1)).is_valid_solid());+ assert!(!MeshInvariants::compute(&make_box(1e3, 1e3, 1e-6, 1)).is_valid_solid());+ // An inside-out solid has negative volume, which is not a volume to build on.+ let mut inside_out = make_box(1.0, 1.0, 1.0, 1);+ inside_out.flip_winding();+ assert!(MeshInvariants::compute(&inside_out).has_negligible_volume());+ // A volume that is not a number, or an area of zero, is never a volume to build on.+ let mut inv = MeshInvariants::compute(&make_box(1.0, 1.0, 1.0, 1));+ assert!(!inv.has_negligible_volume());+ inv.signed_volume = f64::INFINITY;+ assert!(inv.has_negligible_volume());+ inv.signed_volume = 1.0;+ inv.surface_area = 0.0;+ assert!(inv.has_negligible_volume());+}