Wave 20/509: solidcraft-smlib: schema 4 records into SMLib through the flat arrays

1 commits, squashed; the originals stay reachable from tag archive/merge-main-develop.

- 2a22f4641 Brennen Herbruck: solidcraft-smlib: schema 4 records into SMLib through the flat arrays

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
Brennen Herbruck
2026-10-21 01:10:59 -04:00
committed by jyviko
parent c72062a96a
commit e0fb4ef757
10 changed files with 918 additions and 3 deletions
+3 -1
View File
@@ -190,7 +190,9 @@ pub use matchsurf::{match_surface, seam_continuity};
pub use measure::{BodyMeasure, MassMeasure, measure, measure_mass};
pub use mesh_convert::convert_prismatic_mesh;
pub use meshbody::{MAX_TRIANGLES, mesh_body};
pub use native::{decode_native_body, encode_native_body, native_preview_mesh};
#[cfg(feature = "smlib")]
pub use native::smlib_body;
pub use native::{brep_record, decode_native_body, encode_native_body, native_preview_mesh};
pub use offset::with_same_surface;
pub use ops::{
BoolOp, boolean, can_join_all_touching_faces, can_join_touching_faces, compound_disjoint_extrusions, half_space, is_planar, join_apart,
+23
View File
@@ -82,6 +82,26 @@ pub fn encode_native_body(body: &Body) -> Result<Vec<u8>> {
})
}
/// The schema 4 record of a solid body, in canonical order with its pcurves: what another
/// backend (SMLib) builds the body from. Mesh bodies have none.
pub fn brep_record(body: &Body) -> Result<solidcraft_geom::brep::Brep> {
guard("B-rep record", || {
if body.mesh.is_some() {
return Err(invalid("mesh bodies are source blobs, not B-rep"));
}
adapter::encode(body)
})
}
/// The body in SMLib, built from its schema 4 record. The record is trusted (SMLib's healer is
/// skipped): the kernel's records keep SMLib's rules, and healing moved a cylinder's volume by
/// 9e-7 where the record as given is exact to 1e-7.
#[cfg(feature = "smlib")]
pub fn smlib_body(body: &Body) -> Result<solidcraft_smlib::Brep> {
let record = brep_record(body)?;
solidcraft_smlib::Brep::from_record(&record, true).map_err(|e| KernelError::Failed(format!("SMLib: {e}")))
}
/// Decode bounded untrusted data. Validate indices before topology extraction.
/// Only a caller that has checked who produced the data may treat it as evaluated geometry.
pub fn decode_native_body(bytes: &[u8]) -> Result<Body> {
@@ -194,6 +214,9 @@ fn value_nodes(v: &Value) -> usize {
_ => 1,
}
}
#[cfg(all(test, feature = "smlib"))]
mod smlib_import_tests;
#[cfg(test)]
mod tests {
use super::*;
@@ -0,0 +1,38 @@
//! Truck bodies into SMLib through their schema 4 records: the kernel's primitives keep their
//! analytic volume. Truck holds cylinders, spheres and tori as rational B-splines, which SMLib
//! integrates to a few 1e-7 (truck's own mesh measure is 1e-4 off).
use std::f64::consts::PI;
use solidcraft_geom::Vec3;
use crate::{Body, box_solid, cylinder, sphere, torus};
fn v(x: f64, y: f64, z: f64) -> Vec3 {
Vec3::new(x, y, z)
}
/// The body's SMLib volume, and whether SMLib finds it a manifold solid.
fn in_smlib(body: &Body) -> Result<(f64, bool), String> {
let s = super::smlib_body(body).map_err(|e| e.to_string())?;
Ok((s.volume(1e-7).map_err(|e| e.to_string())?, s.is_manifold_solid().unwrap_or(false)))
}
#[test]
fn primitives_keep_their_analytic_volume() {
let (got, solid) = in_smlib(&box_solid(v(0., 0., 0.), v(10., 20., 30.)).unwrap()).unwrap();
assert!(solid && (got - 6000.0).abs() <= 1e-9 * 6000.0, "box: SMLib volume {got}, manifold {solid}");
let (got, solid) = in_smlib(&cylinder(v(1., 2., 3.), v(0., 0., 1.), 5.0, 10.0).unwrap()).unwrap();
assert!(solid && (got - 250.0 * PI).abs() <= 1e-6 * 250.0 * PI, "cylinder: SMLib volume {got}, manifold {solid}");
let (got, solid) = in_smlib(&cylinder(v(0., 0., 0.), v(1., 1., 1.), 2.0, 7.0).unwrap()).unwrap();
assert!(solid && (got - 28.0 * PI).abs() <= 1e-6 * 28.0 * PI, "tilted cylinder: SMLib volume {got}, manifold {solid}");
}
/// Truck holds spheres and tori as revolutions (`PlacedRevolution`), which the SMLib import does
/// not carry yet: a clear error, never a wrong body.
#[test]
fn revolutions_are_refused_for_now() {
for (name, body) in [("sphere", sphere(v(0., 0., 0.), 5.0).unwrap()), ("torus", torus(v(0., 0., 0.), 10.0, 2.0).unwrap())] {
let e = super::smlib_body(&body).err().map(|e| e.to_string()).unwrap_or_default();
assert!(e.contains("not supported yet: revolved surfaces"), "{name}: {e}");
}
}