Wave 21/509: solidcraft-smlib: SMLib bodies into SolidCraft through schema 4
1 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - 144b56924 jyviko: solidcraft-smlib: SMLib bodies into SolidCraft through schema 4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
@@ -162,7 +162,7 @@ pub(super) fn encode(body: &Body) -> Result<b::Brep> {
|
||||
}
|
||||
let result = b::Brep { version: b::VERSION, tolerance, regions, shells: shell_records, faces, edges: edge_records, vertices };
|
||||
result.validate().map_err(invalid)?;
|
||||
compatible(&result)?;
|
||||
compatible(&result, Producer::Own)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -303,17 +303,36 @@ fn regions(body: &Body) -> Result<Vec<b::Region>> {
|
||||
Ok(regions)
|
||||
}
|
||||
|
||||
fn compatible(body: &b::Brep) -> Result<()> {
|
||||
fn compatible(body: &b::Brep, producer: Producer) -> Result<()> {
|
||||
body.edges.iter().try_for_each(|e| compatible_curve(&e.curve))?;
|
||||
body.faces.iter().try_for_each(|f| compatible_surface(&f.surface))
|
||||
body.faces.iter().try_for_each(|f| match (&f.surface, producer) {
|
||||
// Truck holds a plane by three points: another kernel's axes load rounded once.
|
||||
(b::Surface::Plane { .. }, Producer::Foreign) => Ok(()),
|
||||
(s, _) => compatible_surface(s),
|
||||
})
|
||||
}
|
||||
|
||||
/// Who wrote a record, which sets how exactly it must load.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Producer {
|
||||
/// This adapter: the record loads back number for number, or is refused.
|
||||
Own,
|
||||
/// Another kernel (SMLib): its records load as truck holds them, analytic surfaces as
|
||||
/// revolutions (as for our own) and planes through three points rounded once.
|
||||
#[cfg_attr(not(feature = "smlib"), allow(dead_code))]
|
||||
Foreign,
|
||||
}
|
||||
|
||||
/// The solid of a record: validated, its geometry checked against its topology (truck builds
|
||||
/// edges and faces from it unchecked), then built as the kernel built it.
|
||||
pub(super) fn decode(body: b::Brep) -> Result<mt::Solid> {
|
||||
decode_from(body, Producer::Own)
|
||||
}
|
||||
|
||||
pub(super) fn decode_from(body: b::Brep, producer: Producer) -> Result<mt::Solid> {
|
||||
body.validate().map_err(invalid)?;
|
||||
body.check_geometry().map_err(invalid)?;
|
||||
compatible(&body)?;
|
||||
compatible(&body, producer)?;
|
||||
let b::Brep { shells, faces, edges, vertices, .. } = body;
|
||||
let vertices: Vec<mt::Vertex> = vertices.into_iter().map(|v| mt::Vertex::new(p(v.point))).collect();
|
||||
let vertex = |i: u32| vertices.get(i as usize).ok_or_else(|| invalid("edge vertices"));
|
||||
|
||||
@@ -212,3 +212,42 @@ fn every_pcurve_lifts_onto_its_own_edge() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// SMLib's primitives load through their schema 4 records as bodies of SMLib's volume, oriented
|
||||
/// as the records say (outward without `Body::new` turning them).
|
||||
#[cfg(feature = "smlib")]
|
||||
#[test]
|
||||
fn smlib_primitives_decode_with_their_volume_and_orientation() {
|
||||
use solidcraft_smlib::Brep as S;
|
||||
let cases = [
|
||||
("box", S::make_box([1., 2., 3.], [10., 20., 30.]).unwrap()),
|
||||
("cylinder", S::make_cylinder([1., 2., 3.], 5., 10.).unwrap()),
|
||||
("sphere", S::make_sphere([1., 2., 3.], 5.).unwrap()),
|
||||
("frustum", S::make_cone([0.; 3], 5., 2., 10.).unwrap()),
|
||||
("widening frustum", S::make_cone([0.; 3], 2., 5., 10.).unwrap()),
|
||||
("cone", S::make_cone([0.; 3], 5., 0., 10.).unwrap()),
|
||||
("torus", S::make_torus([0.; 3], 10., 3.).unwrap()),
|
||||
];
|
||||
for (name, s) in cases {
|
||||
let record = s.to_native().unwrap();
|
||||
let body = crate::native::body_from_smlib(&s).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
let forward: Vec<bool> = record
|
||||
.shells
|
||||
.iter()
|
||||
.flat_map(|sh| match sh {
|
||||
b::Shell::Faces { faces, .. } => faces.iter().map(|f| f.forward).collect(),
|
||||
_ => Vec::new(),
|
||||
})
|
||||
.collect();
|
||||
let loaded: Vec<bool> = body.solid.boundaries().iter().flat_map(|sh| sh.face_iter().map(|f| f.orientation()).collect::<Vec<_>>()).collect();
|
||||
assert_eq!(loaded, forward, "{name}: the body was turned inside out");
|
||||
let smlib = s.mass_properties(solidcraft_smlib::MEASURE_ACCURACY).unwrap().volume;
|
||||
let ours = crate::measure(&body).unwrap().volume;
|
||||
assert!((ours - smlib).abs() < 2e-3 * smlib, "{name}: decoded volume {ours}, SMLib's {smlib}");
|
||||
assert_eq!(
|
||||
(body.face_count(), body.edge_count(), body.vertex_count()),
|
||||
(record.faces.len(), record.edges.len(), record.vertices.len()),
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user