diff --git a/crates/kernel/src/body.rs b/crates/kernel/src/body.rs
index b0bd29647..ad3661f05 100644
--- a/crates/kernel/src/body.rs
+++ b/crates/kernel/src/body.rs
@@ -1245,7 +1245,11 @@ impl Body {
// A face trimmed by a helix (a modelled thread from another CAD system) turns more than
// once around its cylinder; truck meshes it with triangles across the solid (#1289).
// Such a body is meshed from its display plan, which cuts the face along a seam.
- if guard("tessellate", || Ok(crate::tess::seam::turns_more_than_once(&self.solid))).unwrap_or(false) {
+ // So is one bounded by a single seam from pole to pole (a sphere from SMLib), which truck
+ // meshes as nothing.
+ if guard("tessellate", || Ok(crate::tess::seam::turns_more_than_once(&self.solid) || crate::tess::has_pole_to_pole_seam(&self.solid)))
+ .unwrap_or(false)
+ {
let turn = (angle.is_finite() && angle > 0.0)
.then(|| crate::tess::size::Turn { angle: angle.max(MIN_DISPLAY_ANGLE), floor: tol * DISPLAY_ANGLE_FLOOR });
match guard("tessellate", || crate::tess::plan(&self.solid, tol, turn)) {
diff --git a/crates/kernel/src/lib.rs b/crates/kernel/src/lib.rs
index be98761e6..2fbb76220 100644
--- a/crates/kernel/src/lib.rs
+++ b/crates/kernel/src/lib.rs
@@ -190,7 +190,7 @@ pub use measure::{BodyMeasure, MassMeasure, measure, measure_mass};
pub use mesh_convert::convert_prismatic_mesh;
pub use meshbody::{MAX_TRIANGLES, mesh_body};
#[cfg(feature = "smlib")]
-pub use native::smlib_body;
+pub use native::{body_from_smlib, 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::{
diff --git a/crates/kernel/src/native.rs b/crates/kernel/src/native.rs
index 4608e8622..7c06d3d91 100644
--- a/crates/kernel/src/native.rs
+++ b/crates/kernel/src/native.rs
@@ -144,6 +144,16 @@ pub fn decode_native_body(bytes: &[u8]) -> Result
{
})
}
+/// An SMLib body as a kernel body, through its schema 4 record: SMLib's export mapped by
+/// `solidcraft_smlib` (canonical order, radians, pcurves), then this adapter's decode of a
+/// foreign record (analytic surfaces as truck's revolutions). The body is oriented as the record
+/// says; `Body::new` only measures it.
+#[cfg(feature = "smlib")]
+pub fn body_from_smlib(brep: &solidcraft_smlib::Brep) -> Result {
+ let record = brep.to_native().map_err(|e| KernelError::Failed(e.to_string()))?;
+ guard("decode SMLib B-rep", || Body::new(adapter::decode_from(record, adapter::Producer::Foreign)?))
+}
+
fn validate_topology(saved: &SavedBody) -> Result<()> {
saved.solid.validate().map_err(invalid)?;
let (faces, edges) = (saved.solid.faces.len(), saved.solid.edges.len());
diff --git a/crates/kernel/src/native/adapter.rs b/crates/kernel/src/native/adapter.rs
index fc72665e8..fa2bd631b 100644
--- a/crates/kernel/src/native/adapter.rs
+++ b/crates/kernel/src/native/adapter.rs
@@ -162,7 +162,7 @@ pub(super) fn encode(body: &Body) -> Result {
}
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> {
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 {
+ decode_from(body, Producer::Own)
+}
+
+pub(super) fn decode_from(body: b::Brep, producer: Producer) -> Result {
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 = 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"));
diff --git a/crates/kernel/src/native/adapter_tests.rs b/crates/kernel/src/native/adapter_tests.rs
index fb68adaf5..0dbd1bc52 100644
--- a/crates/kernel/src/native/adapter_tests.rs
+++ b/crates/kernel/src/native/adapter_tests.rs
@@ -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 = 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 = body.solid.boundaries().iter().flat_map(|sh| sh.face_iter().map(|f| f.orientation()).collect::>()).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}"
+ );
+ }
+}
diff --git a/crates/kernel/src/tess/mod.rs b/crates/kernel/src/tess/mod.rs
index 8d87a199b..f7f43b653 100644
--- a/crates/kernel/src/tess/mod.rs
+++ b/crates/kernel/src/tess/mod.rs
@@ -224,6 +224,60 @@ fn relax_crowded_faces(faces: &[&mt::Face], jobs: &[(&mt::Edge, f64)], rules: &s
/// One face: boundary loops in 3D (shared edge nodes) to parameters and triangles.
/// `size`: the face's target side length (mm) and the surface's mean speed along u and v.
+/// Whether a face of `solid` is bounded by one seam edge alone, used both ways between two
+/// distinct vertices (poles): a whole sphere as one face, as SMLib writes it. Truck's mesher
+/// projects both uses onto one parameter line and meshes nothing; the display plan opens the
+/// loop across the period ([`open_pole_to_pole`]).
+pub(crate) fn has_pole_to_pole_seam(solid: &Solid) -> bool {
+ solid.face_iter().any(|f| {
+ f.boundaries().iter().any(|w| {
+ let edges: Vec<&mt::Edge> = w.edge_iter().collect();
+ matches!(edges.as_slice(), [a, b] if a.id() == b.id() && a.front() != a.back())
+ })
+ })
+}
+
+/// Twice the signed area of a parameter polygon.
+fn area2(uv: &[[f64; 2]]) -> f64 {
+ let n = uv.len();
+ (0..n).map(|k| uv.get(k).zip(uv.get((k + 1) % n)).map_or(0.0, |(a, b)| a[0] * b[1] - b[0] * a[1])).sum()
+}
+
+/// A loop that runs from one pole to the other and back along the same parameter line (both
+/// uses of a seam joined only at the poles) encloses nothing once projected: the run after the
+/// second pole is moved one period along the periodic direction, which way gives the loop a
+/// positive area, so the face spans the period between its two seam copies.
+fn open_pole_to_pole(s: &EvalSurface, dom: &uv::Domain, l: &mut seam::Loop) {
+ let n = l.uv.len();
+ let poles: Vec = (0..n).filter(|&k| l.uv.get(k).is_some_and(|q| (0..2).any(|d| uv::degenerate(s, *q, d)))).collect();
+ let [p0, p1] = poles.as_slice() else { return };
+ let (lo, hi) = l.uv.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), q| (a.min(q[0].min(q[1])), b.max(q[0].max(q[1]))));
+ let extent = (hi - lo).abs().max(1e-12);
+ if area2(&l.uv).abs() > 1e-9 * extent * extent {
+ return;
+ }
+ for d in 0..2 {
+ let Some(per) = dom.period[d] else { continue };
+ // Along the seam the periodic parameter is constant between the poles.
+ let second: Vec = (0..n).filter(|k| (k > p1 || k < p0) && *k != *p0 && *k != *p1).collect();
+ if second.is_empty() {
+ continue;
+ }
+ for sign in [1.0, -1.0] {
+ let mut moved = l.uv.clone();
+ for &k in &second {
+ if let Some(q) = moved.get_mut(k) {
+ q[d] += sign * per;
+ }
+ }
+ if area2(&moved) > 1e-9 * extent * extent {
+ l.uv = moved;
+ return;
+ }
+ }
+ }
+}
+
fn plan_face(
fi: usize,
s: &EvalSurface,
@@ -276,6 +330,13 @@ fn plan_face(
}
_ => return Err(fail("boundary winds around the surface both ways")),
}
+ // A loop from pole to pole and back along one seam (a whole sphere as one face, as another
+ // kernel writes it) projects onto one parameter line: its second run is a period along.
+ if !seamed {
+ for l in projected.iter_mut() {
+ open_pole_to_pole(s, &dom, l);
+ }
+ }
// Split poles.
let mut fixed: Vec = Vec::new();
let mut uvs: Vec<[f64; 2]> = Vec::new();
diff --git a/crates/smlib/SMLIB_REV b/crates/smlib/SMLIB_REV
index 957eddc8e..21b09b7a2 100644
--- a/crates/smlib/SMLIB_REV
+++ b/crates/smlib/SMLIB_REV
@@ -1 +1 @@
-40bb855d15391428697d086fd122409c10e5ffee
+0a48843fd764b0b4a6e8998716e800a91518fe7a
diff --git a/crates/smlib/src/export.rs b/crates/smlib/src/export.rs
new file mode 100644
index 000000000..38e3700da
--- /dev/null
+++ b/crates/smlib/src/export.rs
@@ -0,0 +1,1053 @@
+//! SMLib B-reps out to SolidCraft's schema 4 B-rep (`solidcraft_geom::brep`, docs/native-brep.md),
+//! and the primitive solids beyond the box.
+//!
+//! The C API hands out SMLib's B-rep as flat arrays (`smlib_brep_export`, laid out like USD's
+//! BrepArray: analytic surfaces as their kinds, ranges in radians, converted end by end from
+//! SMLib's NURB parameters). This module reads them as hostile input and maps them to schema 4:
+//! - **Geometry:** each analytic surface to its schema kind (SMLib's cone of semi-angle 0 is its
+//! cylinder), NURBS as splines. Edge curves come as their own kinds, or as NURBS where SMLib
+//! holds the edge as one (its primitives' circles do). As a fallback a NURBS line (order 2,
+//! two points) and a rational quadratic circle or arc are recognised exactly and written as
+//! `line` / `circle` with their interval mapped; other NURBS edges stay splines. The report
+//! counts both.
+//! - **Sides:** SMLib keeps a face's loops relative to its own natural normal, which may be
+//! opposite the STEP form's (a surface with SMLib's `insideOut` or `swapUV`). Schema 4 keeps
+//! them relative to the record's normal, outer loop counter-clockwise: a face whose outer loop
+//! comes out clockwise in the record's parameters has its loops reversed and its sides flipped.
+//! - **Shells and regions:** SMLib keeps a closed boundary as two runtime shells, one per side,
+//! each face's use in a shell facing into that shell's region; schema 4 keeps one record, its
+//! oriented faces as the side in the region that is not material uses them (out of the
+//! material). A sheet's one shell holds both uses of each face: the `same` one is kept. The
+//! infinite region is never a void (SMLib marks it one).
+//! - **Order:** faces, edges and vertices renumbered in the canonical walk; each edge's uses
+//! from that walk (`upper = !forward`), never SMLib's radial entry flags.
+//! - **Pcurves:** computed from each edge record on its face's surface record
+//! (`solidcraft_geom::brep::pcurve`), never SMLib's UV trims, moved by whole periods into the
+//! face's domain.
+//! - **Tolerances:** SMLib's intersection tolerance, or 10⁻⁶ of the body's size (at least 10⁻⁵ mm)
+//! when larger, for the body, every edge and every vertex.
+use std::f64::consts::TAU;
+
+use std::collections::HashMap;
+
+use solidcraft_geom::Vec3;
+use solidcraft_geom::brep as b;
+
+use crate::flat::{self, *};
+use crate::import::Arrays;
+use crate::{Brep, Result, error, ffi, init};
+
+impl Brep {
+ /// A cylinder about +Z: base centre, radius and height (mm, > 0).
+ pub fn make_cylinder(origin: [f64; 3], radius: f64, height: f64) -> Result {
+ init()?;
+ ffi::make_cylinder(&origin, radius, height).map(Brep).map_err(error)
+ }
+
+ /// A sphere: centre and radius (mm, > 0).
+ pub fn make_sphere(center: [f64; 3], radius: f64) -> Result {
+ init()?;
+ ffi::make_sphere(¢er, radius).map(Brep).map_err(error)
+ }
+
+ /// A cone or frustum about +Z: base centre, base radius (> 0), top radius (≥ 0, not the base
+ /// radius) and height (> 0), in mm.
+ pub fn make_cone(origin: [f64; 3], base_radius: f64, top_radius: f64, height: f64) -> Result {
+ init()?;
+ ffi::make_cone(&origin, base_radius, top_radius, height).map(Brep).map_err(error)
+ }
+
+ /// A ring torus about +Z: centre, major radius > minor radius > 0 (mm).
+ pub fn make_torus(center: [f64; 3], major_radius: f64, minor_radius: f64) -> Result {
+ init()?;
+ ffi::make_torus(¢er, major_radius, minor_radius).map(Brep).map_err(error)
+ }
+
+ /// The flat arrays of this body that the mapping reads ([`Brep::export`]), copied out.
+ pub fn arrays(&self) -> Result {
+ let f = self.export()?;
+ let mut out = Arrays::default();
+ for &id in I64_IDS {
+ out.ints.insert(id, f.get_i64(id)?);
+ }
+ for &id in F64_IDS {
+ out.reals.insert(id, f.get_f64(id)?);
+ }
+ Ok(out)
+ }
+
+ /// This body as a schema 4 B-rep, validated.
+ pub fn to_native(&self) -> std::result::Result {
+ self.to_native_report().map(|(brep, _)| brep)
+ }
+
+ /// [`Brep::to_native`], with what the mapping kept as SMLib wrote it.
+ pub fn to_native_report(&self) -> std::result::Result<(b::Brep, Report), ExportError> {
+ let arrays = self.arrays().map_err(ExportError::Smlib)?;
+ native(&arrays)
+ }
+}
+
+/// What the mapping could not put in its own kind, or turned.
+#[derive(Clone, Debug, Default, PartialEq, Eq)]
+pub struct Report {
+ /// NURBS edge curves recognised as lines or circular arcs and written as those.
+ pub recognised_edges: usize,
+ /// NURBS edge curves kept as splines (neither a line nor a circular arc).
+ pub nurbs_edges: usize,
+ /// Faces whose loops were reversed (SMLib's natural normal against the record's).
+ pub reversed_faces: usize,
+}
+
+/// Why an SMLib body has no schema 4 record.
+#[derive(Clone, Debug, PartialEq)]
+pub enum ExportError {
+ /// The SMLib call failed.
+ Smlib(super::Error),
+ /// SMLib holds something schema 4 (or this mapping) has no record for, named.
+ Unsupported(String),
+ /// The arrays are malformed or inconsistent.
+ Invalid(String),
+}
+
+impl std::fmt::Display for ExportError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ ExportError::Smlib(e) => write!(f, "SMLib export: {e}"),
+ ExportError::Unsupported(what) => write!(f, "SMLib export: not supported yet: {what}"),
+ ExportError::Invalid(what) => write!(f, "SMLib export: {what}"),
+ }
+ }
+}
+
+impl std::error::Error for ExportError {}
+
+type Res = std::result::Result;
+
+fn invalid(what: impl std::fmt::Display) -> ExportError {
+ ExportError::Invalid(what.to_string())
+}
+fn unsupported(what: impl std::fmt::Display) -> ExportError {
+ ExportError::Unsupported(what.to_string())
+}
+
+// ---- The arrays (ids and kinds from `crate::flat`, as in capi/include/smlib.h) ------------
+
+/// The int64 arrays this mapping reads.
+const I64_IDS: &[i32] = &[
+ REGION_SHELL_COUNT,
+ REGION_TYPE,
+ SHELL_FACEUSE_COUNT,
+ SHELL_WIRE_EDGE_COUNT,
+ SHELL_POINT_TYPE,
+ FACEUSE_FACE,
+ FACEUSE_ORIENTATION,
+ FACE_LOOP_COUNT,
+ FACE_SURFACE_TYPE,
+ LOOP_EDGEUSE_COUNT,
+ LOOP_VERTEX,
+ EDGEUSE_EDGE,
+ EDGEUSE_ORIENTATION,
+ EDGE_CURVE_TYPE,
+ EDGE_VERTICES,
+ EDGE_NURBS_VERTEX_COUNT,
+ EDGE_NURBS_ORDER,
+ FACE_NURBS_U_VERTEX_COUNT,
+ FACE_NURBS_V_VERTEX_COUNT,
+ FACE_NURBS_U_ORDER,
+ FACE_NURBS_V_ORDER,
+];
+
+/// The double arrays this mapping reads.
+const F64_IDS: &[i32] = &[
+ TOLERANCE,
+ EXTENT,
+ FACE_RANGE,
+ EDGE_RANGE,
+ VERTEX_POSITION,
+ EDGE_NURBS_CVS,
+ EDGE_NURBS_KNOTS,
+ EDGE_NURBS_WEIGHTS,
+ EDGE_CIRCLE_CENTER,
+ EDGE_CIRCLE_AXIS,
+ EDGE_CIRCLE_REF_DIRECTION,
+ EDGE_CIRCLE_RADIUS,
+ EDGE_LINE_ORIGIN,
+ EDGE_LINE_DIRECTION,
+ EDGE_ELLIPSE_CENTER,
+ EDGE_ELLIPSE_AXIS,
+ EDGE_ELLIPSE_REF_DIRECTION,
+ EDGE_ELLIPSE_X_RADIUS,
+ EDGE_ELLIPSE_Y_RADIUS,
+ FACE_NURBS_CVS,
+ FACE_NURBS_U_KNOTS,
+ FACE_NURBS_V_KNOTS,
+ FACE_NURBS_WEIGHTS,
+ FACE_SPHERE_CENTER,
+ FACE_SPHERE_AXIS,
+ FACE_SPHERE_REF_DIRECTION,
+ FACE_SPHERE_RADIUS,
+ FACE_PLANE_ORIGIN,
+ FACE_PLANE_AXIS,
+ FACE_PLANE_REF_DIRECTION,
+ FACE_CYLINDER_ORIGIN,
+ FACE_CYLINDER_AXIS,
+ FACE_CYLINDER_REF_DIRECTION,
+ FACE_CYLINDER_RADIUS,
+ FACE_CONE_ORIGIN,
+ FACE_CONE_AXIS,
+ FACE_CONE_REF_DIRECTION,
+ FACE_CONE_RADIUS,
+ FACE_CONE_SEMI_ANGLE,
+ FACE_TORUS_ORIGIN,
+ FACE_TORUS_AXIS,
+ FACE_TORUS_REF_DIRECTION,
+ FACE_TORUS_MAJOR_RADIUS,
+ FACE_TORUS_MINOR_RADIUS,
+];
+
+// The kind values as the arrays hold them (int64); a local item shadows the glob import.
+const SURFACE_NURBS: i64 = flat::SURFACE_NURBS as i64;
+const SURFACE_PLANE: i64 = flat::SURFACE_PLANE as i64;
+const SURFACE_CYLINDER: i64 = flat::SURFACE_CYLINDER as i64;
+const SURFACE_CONE: i64 = flat::SURFACE_CONE as i64;
+const SURFACE_SPHERE: i64 = flat::SURFACE_SPHERE as i64;
+const SURFACE_TORUS: i64 = flat::SURFACE_TORUS as i64;
+const CURVE_NURBS: i64 = flat::CURVE_NURBS as i64;
+const CURVE_LINE: i64 = flat::CURVE_LINE as i64;
+const CURVE_CIRCLE: i64 = flat::CURVE_CIRCLE as i64;
+const CURVE_ELLIPSE: i64 = flat::CURVE_ELLIPSE as i64;
+
+/// Read access to the arrays: a missing array reads as empty.
+#[derive(Clone, Copy)]
+struct View<'a>(&'a Arrays);
+
+impl View<'_> {
+ fn i(&self, id: i32) -> &[i64] {
+ self.0.ints.get(&id).map_or(&[], Vec::as_slice)
+ }
+ fn f(&self, id: i32) -> &[f64] {
+ self.0.reals.get(&id).map_or(&[], Vec::as_slice)
+ }
+}
+
+fn at(a: &[T], k: usize, what: &str) -> Res {
+ a.get(k).copied().ok_or_else(|| invalid(format!("{what}: index {k} out of range")))
+}
+fn idx(x: i64, n: usize, what: &str) -> Res {
+ usize::try_from(x).ok().filter(|i| *i < n).ok_or_else(|| invalid(format!("{what}: {x} out of range")))
+}
+fn count(x: i64, what: &str) -> Res {
+ usize::try_from(x).ok().filter(|n| *n <= b::MAX_ITEMS).ok_or_else(|| invalid(format!("{what}: count {x}")))
+}
+fn index(i: usize) -> Res {
+ u32::try_from(i).map_err(|_| invalid("index"))
+}
+fn v3(a: &[f64], k: usize, what: &str) -> Res<[f64; 3]> {
+ Ok([at(a, 3 * k, what)?, at(a, 3 * k + 1, what)?, at(a, 3 * k + 2, what)?])
+}
+/// Consecutive blocks of `counts` covering `0..total`.
+fn blocks(counts: &[i64], total: usize, what: &str) -> Res>> {
+ let mut start = 0usize;
+ let mut out = Vec::with_capacity(counts.len());
+ for &c in counts {
+ let end = start.checked_add(count(c, what)?).filter(|e| *e <= total).ok_or_else(|| invalid(format!("{what}: blocks overrun")))?;
+ out.push(start..end);
+ start = end;
+ }
+ if start != total {
+ return Err(invalid(format!("{what}: blocks do not cover the array")));
+ }
+ Ok(out)
+}
+
+fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
+ [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
+}
+fn arr(p: Vec3) -> [f64; 3] {
+ [p.x, p.y, p.z]
+}
+/// A frame from an origin, z axis and x axis (y = z × x).
+fn frame(origin: [f64; 3], axis: [f64; 3], x: [f64; 3]) -> b::Frame {
+ b::Frame { origin, x_axis: x, y_axis: cross(axis, x) }
+}
+
+// ---- Geometry ---------------------------------------------------------------------------
+
+/// The schema record of each face's surface, in face order.
+fn surfaces(flat: View, n_faces: usize) -> Res> {
+ let kinds = flat.i(FACE_SURFACE_TYPE);
+ if kinds.len() != n_faces {
+ return Err(invalid("face surface kinds"));
+ }
+ let mut k = [0usize; 6];
+ let (mut cv, mut uk, mut vk, mut w) = (0usize, 0usize, 0usize, 0usize);
+ let mut out = Vec::with_capacity(n_faces);
+ for &kind in kinds {
+ let slot = usize::try_from(kind).ok().filter(|s| *s < 6).ok_or_else(|| unsupported(format!("SMLib surface kind {kind}")))?;
+ let i = at(&k, slot, "kind")?;
+ if let Some(c) = k.get_mut(slot) {
+ *c += 1;
+ }
+ out.push(match kind {
+ SURFACE_PLANE => {
+ let f = frame(
+ v3(flat.f(FACE_PLANE_ORIGIN), i, "plane")?,
+ v3(flat.f(FACE_PLANE_AXIS), i, "plane")?,
+ v3(flat.f(FACE_PLANE_REF_DIRECTION), i, "plane")?,
+ );
+ b::Surface::Plane { origin: f.origin, u_axis: f.x_axis, v_axis: f.y_axis }
+ }
+ SURFACE_CYLINDER => b::Surface::Cylinder {
+ frame: frame(
+ v3(flat.f(FACE_CYLINDER_ORIGIN), i, "cylinder")?,
+ v3(flat.f(FACE_CYLINDER_AXIS), i, "cylinder")?,
+ v3(flat.f(FACE_CYLINDER_REF_DIRECTION), i, "cylinder")?,
+ ),
+ radius: at(flat.f(FACE_CYLINDER_RADIUS), i, "cylinder")?,
+ },
+ SURFACE_CONE => {
+ let f = frame(
+ v3(flat.f(FACE_CONE_ORIGIN), i, "cone")?,
+ v3(flat.f(FACE_CONE_AXIS), i, "cone")?,
+ v3(flat.f(FACE_CONE_REF_DIRECTION), i, "cone")?,
+ );
+ let (radius, half) = (at(flat.f(FACE_CONE_RADIUS), i, "cone")?, at(flat.f(FACE_CONE_SEMI_ANGLE), i, "cone")?);
+ // SMLib's cone of semi-angle 0 is its cylinder.
+ if half == 0.0 {
+ b::Surface::Cylinder { frame: f, radius }
+ } else {
+ b::Surface::Cone { frame: f, radius_at_origin: radius, half_angle: half }
+ }
+ }
+ SURFACE_SPHERE => b::Surface::Sphere {
+ frame: frame(
+ v3(flat.f(FACE_SPHERE_CENTER), i, "sphere")?,
+ v3(flat.f(FACE_SPHERE_AXIS), i, "sphere")?,
+ v3(flat.f(FACE_SPHERE_REF_DIRECTION), i, "sphere")?,
+ ),
+ radius: at(flat.f(FACE_SPHERE_RADIUS), i, "sphere")?,
+ },
+ SURFACE_TORUS => b::Surface::Torus {
+ frame: frame(
+ v3(flat.f(FACE_TORUS_ORIGIN), i, "torus")?,
+ v3(flat.f(FACE_TORUS_AXIS), i, "torus")?,
+ v3(flat.f(FACE_TORUS_REF_DIRECTION), i, "torus")?,
+ ),
+ major_radius: at(flat.f(FACE_TORUS_MAJOR_RADIUS), i, "torus")?,
+ minor_radius: at(flat.f(FACE_TORUS_MINOR_RADIUS), i, "torus")?,
+ },
+ SURFACE_NURBS => {
+ let nu = count(at(flat.i(FACE_NURBS_U_VERTEX_COUNT), i, "NURBS face")?, "NURBS face")?;
+ let nv = count(at(flat.i(FACE_NURBS_V_VERTEX_COUNT), i, "NURBS face")?, "NURBS face")?;
+ let ou = count(at(flat.i(FACE_NURBS_U_ORDER), i, "NURBS face")?, "NURBS face")?;
+ let ov = count(at(flat.i(FACE_NURBS_V_ORDER), i, "NURBS face")?, "NURBS face")?;
+ let n = nu.checked_mul(nv).filter(|n| *n <= b::MAX_ITEMS).ok_or_else(|| invalid("NURBS face size"))?;
+ let slice =
+ |a: &[f64], from: usize, len: usize| a.get(from..from + len).map(<[f64]>::to_vec).ok_or_else(|| invalid("NURBS face arrays"));
+ let pts = slice(flat.f(FACE_NURBS_CVS), 3 * cv, 3 * n)?;
+ let u_knots = slice(flat.f(FACE_NURBS_U_KNOTS), uk, nu + ou)?;
+ let v_knots = slice(flat.f(FACE_NURBS_V_KNOTS), vk, nv + ov)?;
+ let weights = if flat.f(FACE_NURBS_WEIGHTS).is_empty() { vec![1.0; n] } else { slice(flat.f(FACE_NURBS_WEIGHTS), w, n)? };
+ cv += n;
+ uk += nu + ou;
+ vk += nv + ov;
+ w += n;
+ let point = |p: usize| -> [f64; 3] { [pts[3 * p], pts[3 * p + 1], pts[3 * p + 2]] };
+ if weights.iter().all(|x| *x == 1.0) {
+ b::Surface::Spline { u_knots, v_knots, points: (0..nu).map(|a| (0..nv).map(|c| point(a * nv + c)).collect()).collect() }
+ } else {
+ let h = |p: usize| {
+ let (q, wt) = (point(p), weights[p]);
+ [q[0] * wt, q[1] * wt, q[2] * wt, wt]
+ };
+ b::Surface::RationalSpline { u_knots, v_knots, homogeneous: (0..nu).map(|a| (0..nv).map(|c| h(a * nv + c)).collect()).collect() }
+ }
+ }
+ other => return Err(unsupported(format!("SMLib surface kind {other}"))),
+ });
+ }
+ Ok(out)
+}
+
+/// A NURBS curve: full knots, cartesian control points and weights.
+struct Nurbs {
+ knots: Vec,
+ points: Vec<[f64; 3]>,
+ weights: Vec,
+}
+
+impl Nurbs {
+ fn record(&self) -> b::Curve {
+ if self.weights.iter().all(|w| *w == 1.0) {
+ b::Curve::Spline { knots: self.knots.clone(), points: self.points.clone() }
+ } else {
+ b::Curve::RationalSpline {
+ knots: self.knots.clone(),
+ homogeneous: self.points.iter().zip(&self.weights).map(|(p, w)| [p[0] * w, p[1] * w, p[2] * w, *w]).collect(),
+ }
+ }
+ }
+}
+
+/// A NURBS line (order 2, two points, equal weights) as a line, its parameter mapped onto the
+/// line's: t ↦ (t − k₁)/(k₂ − k₁).
+fn as_line(n: &Nurbs, range: [f64; 2]) -> Option<(b::Curve, [f64; 2])> {
+ let ([p0, p1], [k0, k1, k2, k3]) = (<[[f64; 3]; 2]>::try_from(n.points.as_slice()).ok()?, <[f64; 4]>::try_from(n.knots.as_slice()).ok()?);
+ if !(k0 == k1 && k2 == k3 && k1 < k2) || n.weights.first() != n.weights.get(1) || p0 == p1 {
+ return None;
+ }
+ let s = |t: f64| (t - k1) / (k2 - k1);
+ Some((b::Curve::Line { start: p0, end: p1 }, [s(range[0]), s(range[1])]))
+}
+
+/// A rational quadratic circle or circular arc as a circle: its frame's x axis at the curve's
+/// start and its normal the curve's turning direction, so the angle grows along the curve; the
+/// interval the angles of the range's ends. Checked: 65 samples lie on the circle within 10⁻⁹
+/// of its radius (at least 1 mm).
+fn as_circle(n: &Nurbs, range: [f64; 2]) -> Option<(b::Curve, [f64; 2])> {
+ if n.knots.len() != n.points.len() + 3 || n.points.len() < 3 {
+ return None;
+ }
+ let eval = b::eval::CurveEval::new(&n.record()).ok()?;
+ let (a, z) = (*n.knots.get(2)?, *n.knots.get(n.points.len())?);
+ let at = |t: f64| eval.point(t);
+ let (p, q, r) = (at(a)?, at(a + (z - a) / 3.0)?, at(a + 2.0 * (z - a) / 3.0)?);
+ // The circle through three points: centre p + (|w|² (u × w) × u + |u|² w × (u × w)) / (2 |u × w|²).
+ let (u, w) = (q - p, r - p);
+ let normal = u.cross(w);
+ let nn = normal.dot(normal);
+ if !(nn > 0.0) {
+ return None;
+ }
+ let center = p + (normal.cross(u) * w.dot(w) + w.cross(normal) * u.dot(u)) * (0.5 / nn);
+ let radius = (p - center).len();
+ let k = normal.normalized()?;
+ let slack = 1e-9 * radius.max(1.0);
+ for i in 0..=64 {
+ let s = at(a + (z - a) * f64::from(i) / 64.0)?;
+ if ((s - center).len() - radius).abs() > slack || (s - center).dot(k).abs() > slack {
+ return None;
+ }
+ }
+ let x = (p - center).normalized()?;
+ let y = k.cross(x);
+ let angle = |t: f64| at(t).map(|s| (s - center).dot(y).atan2((s - center).dot(x)));
+ // The angle at the range's start in [0, 2π), then unwrapped along the range.
+ let mut a0 = angle(range[0])?;
+ if a0 < 0.0 {
+ a0 += TAU;
+ }
+ let (mut prev, mut total) = (a0, a0);
+ for i in 1..=64 {
+ let mut t = angle(range[0] + (range[1] - range[0]) * f64::from(i) / 64.0)?;
+ while t - prev > std::f64::consts::PI {
+ t -= TAU;
+ }
+ while t - prev < -std::f64::consts::PI {
+ t += TAU;
+ }
+ total += t - prev;
+ prev = t;
+ }
+ if !(total > a0) {
+ return None;
+ }
+ Some((b::Curve::Circle { frame: b::Frame { origin: arr(center), x_axis: arr(x), y_axis: arr(y) }, radius }, [a0, total]))
+}
+
+/// Each edge's record and interval (in SMLib's direction along it), and how many NURBS edges
+/// were recognised as lines or circles and how many stayed splines.
+fn curves(flat: View, n_edges: usize) -> Res<(Vec<(b::Curve, [f64; 2])>, usize, usize)> {
+ let kinds = flat.i(EDGE_CURVE_TYPE);
+ let range = flat.f(EDGE_RANGE);
+ if kinds.len() != n_edges || range.len() != 2 * n_edges {
+ return Err(invalid("edge kinds or ranges"));
+ }
+ let (mut nurbs, mut lines, mut circles, mut ellipses) = (0usize, 0usize, 0usize, 0usize);
+ let (mut cv, mut kn) = (0usize, 0usize);
+ let (mut recognised, mut kept) = (0usize, 0usize);
+ let mut out = Vec::with_capacity(n_edges);
+ for (e, &kind) in kinds.iter().enumerate() {
+ let r = [at(range, 2 * e, "edge range")?, at(range, 2 * e + 1, "edge range")?];
+ out.push(match kind {
+ CURVE_LINE => {
+ let (o, d) = (v3(flat.f(EDGE_LINE_ORIGIN), lines, "line")?, v3(flat.f(EDGE_LINE_DIRECTION), lines, "line")?);
+ lines += 1;
+ (b::Curve::Line { start: o, end: [o[0] + d[0], o[1] + d[1], o[2] + d[2]] }, r)
+ }
+ CURVE_CIRCLE => {
+ let f = frame(
+ v3(flat.f(EDGE_CIRCLE_CENTER), circles, "circle")?,
+ v3(flat.f(EDGE_CIRCLE_AXIS), circles, "circle")?,
+ v3(flat.f(EDGE_CIRCLE_REF_DIRECTION), circles, "circle")?,
+ );
+ let radius = at(flat.f(EDGE_CIRCLE_RADIUS), circles, "circle")?;
+ circles += 1;
+ (b::Curve::Circle { frame: f, radius }, r)
+ }
+ CURVE_ELLIPSE => {
+ let f = frame(
+ v3(flat.f(EDGE_ELLIPSE_CENTER), ellipses, "ellipse")?,
+ v3(flat.f(EDGE_ELLIPSE_AXIS), ellipses, "ellipse")?,
+ v3(flat.f(EDGE_ELLIPSE_REF_DIRECTION), ellipses, "ellipse")?,
+ );
+ let radii = [at(flat.f(EDGE_ELLIPSE_X_RADIUS), ellipses, "ellipse")?, at(flat.f(EDGE_ELLIPSE_Y_RADIUS), ellipses, "ellipse")?];
+ ellipses += 1;
+ (b::Curve::Ellipse { frame: f, radii }, r)
+ }
+ CURVE_NURBS => {
+ let n = count(at(flat.i(EDGE_NURBS_VERTEX_COUNT), nurbs, "NURBS edge")?, "NURBS edge")?;
+ let o = count(at(flat.i(EDGE_NURBS_ORDER), nurbs, "NURBS edge")?, "NURBS edge")?;
+ let cvs = flat.f(EDGE_NURBS_CVS).get(3 * cv..3 * (cv + n)).ok_or_else(|| invalid("NURBS edge points"))?;
+ let knots = flat.f(EDGE_NURBS_KNOTS).get(kn..kn + n + o).ok_or_else(|| invalid("NURBS edge knots"))?.to_vec();
+ let weights = match flat.f(EDGE_NURBS_WEIGHTS) {
+ [] => vec![1.0; n],
+ w => w.get(cv..cv + n).ok_or_else(|| invalid("NURBS edge weights"))?.to_vec(),
+ };
+ nurbs += 1;
+ cv += n;
+ kn += n + o;
+ let c = Nurbs { knots, points: cvs.as_chunks::<3>().0.to_vec(), weights };
+ // A fallback: SMLib exports lines, circles and ellipses as their own kinds.
+ match as_line(&c, r).or_else(|| as_circle(&c, r)) {
+ Some(x) => {
+ recognised += 1;
+ x
+ }
+ None => {
+ kept += 1;
+ (c.record(), r)
+ }
+ }
+ }
+ other => return Err(unsupported(format!("SMLib curve kind {other}"))),
+ });
+ }
+ Ok((out, recognised, kept))
+}
+
+/// A domain side moved onto the natural range where it overshoots it by rounding (a sphere's
+/// latitudes, a cone's apex).
+fn onto(r: [f64; 2], natural: b::Natural) -> [f64; 2] {
+ match natural {
+ b::Natural::Range(lo, hi) => {
+ let slack = 1e-12 * (1.0 + r[0].abs().max(r[1].abs()));
+ let snap = |x: f64| {
+ if x < lo && x >= lo - slack {
+ lo
+ } else if x > hi && x <= hi + slack {
+ hi
+ } else {
+ x
+ }
+ };
+ [snap(r[0]), snap(r[1])]
+ }
+ b::Natural::Periodic => r,
+ }
+}
+
+// ---- The mapping ----------------------------------------------------------------------------
+
+/// One face before renumbering: loops relative to the record's normal, as (edge, forward).
+struct Face {
+ surface: b::Surface,
+ domain: [[f64; 2]; 2],
+ /// SMLib's natural normal is the record's.
+ agrees: bool,
+ loops: Vec,
+}
+
+#[derive(Clone)]
+enum Loop {
+ Edges(Vec<(u32, bool)>),
+ Vertex(u32),
+}
+
+/// One edge before renumbering, along its record's parameter.
+struct Edge {
+ curve: b::Curve,
+ interval: [f64; 2],
+ vertices: [u32; 2],
+ /// The record's parameter runs against SMLib's edge direction.
+ reversed: bool,
+}
+
+/// The schema 4 B-rep of SMLib's flat arrays: mapped, renumbered canonically and validated.
+pub fn native(arrays: &Arrays) -> Res<(b::Brep, Report)> {
+ let flat = View(arrays);
+ let mut report = Report::default();
+ if arrays.ints.values().map(Vec::len).chain(arrays.reals.values().map(Vec::len)).any(|n| n > flat::MAX_LEN) {
+ return Err(invalid("array too long"));
+ }
+
+ // Tolerances: SMLib's, or the size-relative floor where larger.
+ let size = match flat.f(EXTENT) {
+ [a, b, c, d, e, f] => ((d - a).powi(2) + (e - b).powi(2) + (f - c).powi(2)).sqrt(),
+ _ => return Err(invalid("extent")),
+ };
+ if !size.is_finite() {
+ return Err(invalid("extent"));
+ }
+ let floor = (1e-6 * size).max(1e-5);
+ let tolerance = match flat.f(TOLERANCE) {
+ [t] if t.is_finite() && *t > floor => *t,
+ [_] => floor,
+ _ => return Err(invalid("tolerance")),
+ };
+
+ // Topology blocks.
+ let n_faceuses = flat.i(FACEUSE_FACE).len();
+ let n_faces = flat.i(FACE_LOOP_COUNT).len();
+ let n_loops = flat.i(LOOP_EDGEUSE_COUNT).len();
+ let n_edgeuses = flat.i(EDGEUSE_EDGE).len();
+ let n_edges = flat.i(EDGE_CURVE_TYPE).len();
+ let n_vertices = flat.f(VERTEX_POSITION).len() / 3;
+ if flat.f(VERTEX_POSITION).len() != 3 * n_vertices
+ || flat.i(FACEUSE_ORIENTATION).len() != n_faceuses
+ || flat.i(EDGEUSE_ORIENTATION).len() != n_edgeuses
+ || flat.i(LOOP_VERTEX).len() != n_loops
+ || flat.i(EDGE_VERTICES).len() != 2 * n_edges
+ || [n_faceuses, n_faces, n_loops, n_edgeuses, n_edges, n_vertices].iter().any(|n| *n > b::MAX_ITEMS)
+ {
+ return Err(invalid("array lengths"));
+ }
+ let n_shells = flat.i(SHELL_FACEUSE_COUNT).len();
+ if n_shells > 2 * b::MAX_SHELLS || flat.i(SHELL_WIRE_EDGE_COUNT).len() != n_shells || flat.i(SHELL_POINT_TYPE).len() != n_shells {
+ return Err(invalid("shell arrays"));
+ }
+ if flat.i(SHELL_WIRE_EDGE_COUNT).iter().any(|c| *c != 0) || flat.i(SHELL_POINT_TYPE).iter().any(|c| *c != 0) {
+ return Err(unsupported("wire and point shells from SMLib"));
+ }
+ let region_shells = blocks(flat.i(REGION_SHELL_COUNT), n_shells, "region shells")?;
+ if region_shells.is_empty() || flat.i(REGION_TYPE).len() != region_shells.len() {
+ return Err(invalid("regions"));
+ }
+ let shell_faceuses = blocks(flat.i(SHELL_FACEUSE_COUNT), n_faceuses, "shell faceuses")?;
+ let face_loops = blocks(flat.i(FACE_LOOP_COUNT), n_loops, "face loops")?;
+ let loop_edgeuses = blocks(flat.i(LOOP_EDGEUSE_COUNT), n_edgeuses, "loop edgeuses")?;
+
+ // Edges along their records' parameters.
+ let (records, recognised, kept) = curves(flat, n_edges)?;
+ report.recognised_edges = recognised;
+ report.nurbs_edges = kept;
+ let mut edges = Vec::with_capacity(n_edges);
+ for (e, (curve, [a, z])) in records.into_iter().enumerate() {
+ if !(a.is_finite() && z.is_finite()) || a == z {
+ return Err(invalid("edge interval"));
+ }
+ let start = index(idx(at(flat.i(EDGE_VERTICES), 2 * e, "edge vertices")?, n_vertices, "edge vertex")?)?;
+ let end = index(idx(at(flat.i(EDGE_VERTICES), 2 * e + 1, "edge vertices")?, n_vertices, "edge vertex")?)?;
+ let reversed = a > z;
+ edges.push(Edge {
+ curve,
+ interval: if reversed { [z, a] } else { [a, z] },
+ vertices: if reversed { [end, start] } else { [start, end] },
+ reversed,
+ });
+ }
+
+ // Faces: loops as SMLib keeps them, then turned where SMLib's normal is not the record's.
+ let surfaces = surfaces(flat, n_faces)?;
+ let range = flat.f(FACE_RANGE);
+ if range.len() != 4 * n_faces {
+ return Err(invalid("face ranges"));
+ }
+ let mut faces = Vec::with_capacity(n_faces);
+ for (f, (surface, loops)) in surfaces.into_iter().zip(&face_loops).enumerate() {
+ let r = range.get(4 * f..4 * f + 4).ok_or_else(|| invalid("face range"))?;
+ let natural = surface.natural();
+ let domain = [onto([r[0], r[2]], natural[0]), onto([r[1], r[3]], natural[1])];
+ let mut out = Vec::with_capacity(loops.len());
+ for l in loops.clone() {
+ let uses = loop_edgeuses.get(l).ok_or_else(|| invalid("loop"))?;
+ if uses.is_empty() {
+ let v = idx(at(flat.i(LOOP_VERTEX), l, "loop vertex")?, n_vertices, "loop vertex")?;
+ out.push(Loop::Vertex(index(v)?));
+ continue;
+ }
+ let mut l = Vec::with_capacity(uses.len());
+ for u in uses.clone() {
+ let e = idx(at(flat.i(EDGEUSE_EDGE), u, "edgeuse")?, n_edges, "edgeuse edge")?;
+ let same = at(flat.i(EDGEUSE_ORIENTATION), u, "edgeuse")? == 1;
+ let edge = edges.get(e).ok_or_else(|| invalid("edge"))?;
+ l.push((index(e)?, same != edge.reversed));
+ }
+ out.push(Loop::Edges(l));
+ }
+ faces.push(Face { surface, domain, agrees: true, loops: out });
+ }
+ for face in &mut faces {
+ if outer_area(face, &edges, tolerance)? < 0.0 {
+ face.agrees = false;
+ for l in &mut face.loops {
+ if let Loop::Edges(uses) = l {
+ uses.reverse();
+ uses.iter_mut().for_each(|u| u.1 = !u.1);
+ }
+ }
+ report.reversed_faces += 1;
+ }
+ }
+
+ // Shells: SMLib's two runtime sides of a closed boundary become one record, its faces as the
+ // side in the region that is not material uses them.
+ let material = |r: usize| r > 0 && flat.i(REGION_TYPE).get(r) == Some(&0);
+ let mut region_of = vec![0usize; n_shells];
+ for (r, shells) in region_shells.iter().enumerate() {
+ for s in shells.clone() {
+ if let Some(slot) = region_of.get_mut(s) {
+ *slot = r;
+ }
+ }
+ }
+ let faceuse = |fu: usize| -> Res<(usize, bool)> {
+ Ok((idx(at(flat.i(FACEUSE_FACE), fu, "faceuse")?, n_faces, "faceuse face")?, at(flat.i(FACEUSE_ORIENTATION), fu, "faceuse")? == 1))
+ };
+ let mut face_uses: Vec> = vec![Vec::new(); n_faces];
+ for (s, uses) in shell_faceuses.iter().enumerate() {
+ for fu in uses.clone() {
+ let (f, same) = faceuse(fu)?;
+ face_uses.get_mut(f).ok_or_else(|| invalid("face"))?.push((s, same));
+ }
+ }
+ let mut shell_of: Vec