Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Wave 22/509: solidcraft-step: representation context units and uncertainty, shared with the kernel's STEP reader (and 3 more)

bherbruck517471d0d319
diff --git a/crates/kernel/examples/smlib_booleans.rs b/crates/kernel/examples/smlib_booleans.rs
index defccf4f40..b214fc06c4 100644
--- a/crates/kernel/examples/smlib_booleans.rs
+++ b/crates/kernel/examples/smlib_booleans.rs
@@ -174,9 +174,12 @@ mod run {
o
}
- /// The SMLib side, through `solidcraft-smlib`. Primitives and booleans arrive with the
- /// operation shims (smlib_primitives.cpp, smlib_ops.cpp); until a call exists it answers
- /// "not supported yet" and the report counts the case as not run on SMLib.
+ /// The SMLib side, through `solidcraft-smlib`. Boxes and cylinders are the very B-reps truck
+ /// starts from, through their schema 4 records imported into SMLib (`smlib_body`), so both
+ /// kernels get the same input whatever the axis. Spheres and tori are SMLib's own primitives
+ /// (about +Z, like truck's): the import doesn't take truck's revolved surfaces yet. The
+ /// boolean arrives with the operation shims (smlib_ops.cpp); until then it answers "not
+ /// supported yet" and the report counts the case as not run on SMLib.
mod side {
use solidcraft_kernel::BoolOp;
use solidcraft_kernel::fuzz::Shape;
@@ -188,11 +191,12 @@ mod run {
pub fn shape(s: &Shape) -> Result<Brep, String> {
match s {
- Shape::Box { a, b } => Brep::make_box(*a, [b[0] - a[0], b[1] - a[1], b[2] - a[2]]).map_err(|e| e.to_string()),
- Shape::Cylinder { .. } => Err(not_yet("cylinder")),
- Shape::Sphere { .. } => Err(not_yet("sphere")),
- Shape::Torus { .. } => Err(not_yet("torus")),
+ Shape::Sphere { center, radius } => return Brep::make_sphere(*center, *radius).map_err(|e| e.to_string()),
+ Shape::Torus { center, major, minor } => return Brep::make_torus(*center, *major, *minor).map_err(|e| e.to_string()),
+ Shape::Box { .. } | Shape::Cylinder { .. } => {}
}
+ let body = s.body().map_err(|e| format!("truck primitive: {e}"))?;
+ solidcraft_kernel::smlib_body(&body).map_err(|e| format!("import: {e}"))
}
/// `a op b`; `None` when the result is empty.
diff --git a/crates/kernel/src/step_in/mod.rs b/crates/kernel/src/step_in/mod.rs
index 2454ce1439..7e1143e7fa 100644
--- a/crates/kernel/src/step_in/mod.rs
+++ b/crates/kernel/src/step_in/mod.rs
@@ -88,10 +88,6 @@ pub struct StepImport {
pub schema: String,
/// FILE_NAME originating system.
pub system: String,
- /// FILE_NAME preprocessor version.
- pub preprocessor: String,
- /// FILE_DESCRIPTION text.
- pub description: String,
}
/// A solid the walk found, converted later.
@@ -118,9 +114,6 @@ pub(crate) struct Ctx<'a> {
pub ang: f64,
/// Vertex-on-curve tolerance (mm).
pub tol: f64,
- /// Farthest an edge's vertex may sit off its curve and still be moved onto it (mm, at least
- /// `tol`): see [`snap_limit`].
- pub snap: f64,
}
#[derive(Clone, Copy)]
@@ -128,115 +121,21 @@ struct Units {
len: f64,
ang: f64,
tol: f64,
- snap: f64,
}
-const DEFAULT_UNITS: Units = Units { len: 1.0, ang: 1.0, tol: 0.01, snap: 0.01 };
+const DEFAULT_UNITS: Units = Units { len: 1.0, ang: 1.0, tol: 0.01 };
-/// Largest tolerance a file can ask for (mm), however coarse the accuracy it declares.
-const MAX_TOL: f64 = 1.0;
-
-/// Gap between an edge's vertex and its curve that is still read as the same point, as a share
-/// of the model's size: the chord error the kernel's own coarse meshes allow at that size
-/// (`Body::size() * 1e-3`), so moving the vertex onto the curve changes the shape by less than
-/// those meshes already do.
-const SNAP_SHARE: f64 = 1e-3;
-
-/// Farthest a vertex may sit off its edge's curve and be moved onto it (mm): the file's own
-/// tolerance `tol`, or up to [`SNAP_SHARE`] of the model's size `extent` (mm) when that is
-/// larger. Some exporters write vertices further off than the accuracy they declare (a surface
-/// export may declare 0.01 mm and join two curves 0.38 mm apart at a vertex between them).
-/// Never more than [`MAX_TOL`], so a far stray point cannot turn the check off.
-fn snap_limit(tol: f64, extent: f64) -> f64 {
- let share = if extent.is_finite() { extent * SNAP_SHARE } else { 0.0 };
- tol.max(share.min(MAX_TOL))
-}
-
-fn si_prefix(p: Option<&Param>) -> f64 {
- match p.and_then(Param::as_enum) {
- Some("EXA") => 1e18,
- Some("PETA") => 1e15,
- Some("TERA") => 1e12,
- Some("GIGA") => 1e9,
- Some("MEGA") => 1e6,
- Some("KILO") => 1e3,
- Some("HECTO") => 1e2,
- Some("DECA") => 1e1,
- Some("DECI") => 1e-1,
- Some("CENTI") => 1e-2,
- Some("MILLI") => 1e-3,
- Some("MICRO") => 1e-6,
- Some("NANO") => 1e-9,
- Some("PICO") => 1e-12,
- Some("FEMTO") => 1e-15,
- Some("ATTO") => 1e-18,
- _ => 1.0,
- }
-}
-
-/// A unit's size: millimetres for lengths, radians for plane angles.
-fn unit_factor(ex: &Exchange, id: u64, depth: usize) -> Option<(&'static str, f64)> {
- if depth > 4 {
- return None;
- }
- let e = ex.get(id)?;
- let kind = if e.has("LENGTH_UNIT") {
- "length"
- } else if e.has("PLANE_ANGLE_UNIT") {
- "angle"
- } else {
- "other"
- };
- if let Some(si) = e.record("SI_UNIT") {
- let base = match si.get(1).and_then(Param::as_enum) {
- Some("METRE") => 1000.0,
- Some("RADIAN") => 1.0,
- _ => return Some((kind, 1.0)),
- };
- return Some((kind, si_prefix(si.first()) * base));
- }
- if let Some(cb) = e.record("CONVERSION_BASED_UNIT") {
- let m = ex.get(cb.get(1)?.as_ref_id()?)?;
- let mp = m.params();
- let v = mp.first()?.as_f64()?;
- let (_, f) = unit_factor(ex, mp.get(1)?.as_ref_id()?, depth + 1)?;
- let x = v * f;
- return (x.is_finite() && x > 0.0).then_some((kind, x));
- }
- Some((kind, 1.0))
-}
-
-/// Units of a representation context (GLOBAL_UNIT_ASSIGNED_CONTEXT, uncertainty).
+/// Units of a representation context (GLOBAL_UNIT_ASSIGNED_CONTEXT, uncertainty), read by
+/// [`solidcraft_step::units::context`]; the vertex-on-curve tolerance is ten times the
+/// uncertainty (at least 1e-3 mm), else a hundredth of the length unit, within [1e-4, 1] mm.
fn context_units(ex: &Exchange, ctx: Option<u64>, cache: &mut HashMap<u64, Units>) -> Units {
let Some(id) = ctx else { return DEFAULT_UNITS };
if let Some(u) = cache.get(&id) {
return *u;
}
- let mut u = DEFAULT_UNITS;
- let mut tol_file = None;
- if let Some(e) = ex.get(id) {
- if let Some(units) = e.record("GLOBAL_UNIT_ASSIGNED_CONTEXT").and_then(|r| r.first()).and_then(Param::as_list) {
- for uid in units.iter().filter_map(Param::as_ref_id) {
- match unit_factor(ex, uid, 0) {
- Some(("length", f)) if f.is_finite() && f > 0.0 => u.len = f,
- Some(("angle", f)) if f.is_finite() && f > 0.0 => u.ang = f,
- _ => {}
- }
- }
- }
- if let Some(unc) = e.record("GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT").and_then(|r| r.first()).and_then(Param::as_list) {
- for uid in unc.iter().filter_map(Param::as_ref_id) {
- if let Some(m) = ex.get(uid)
- && let Some(v) = m.params().first().and_then(Param::as_f64)
- {
- let f = m.params().get(1).and_then(Param::as_ref_id).and_then(|x| unit_factor(ex, x, 0)).map(|x| x.1);
- tol_file = Some(v * f.unwrap_or(u.len));
- }
- }
- }
- }
- u.tol = tol_file.filter(|t| t.is_finite() && *t > 0.0).map(|t| (t * 10.0).max(1e-3)).unwrap_or(u.len * 1e-2).clamp(1e-4, MAX_TOL);
- u.snap = snap_limit(u.tol, ex.vertex_extent() * u.len);
+ let c = solidcraft_step::units::context(ex, id);
+ let tol = c.uncertainty.map(|t| (t * 10.0).max(1e-3)).unwrap_or(c.units.len * 1e-2).clamp(1e-4, 1.0);
+ let u = Units { len: c.units.len, ang: c.units.ang, tol };
cache.insert(id, u);
u
}
@@ -588,41 +487,12 @@ impl Reader {
}
fn placement_matrix(&mut self, item: u64, u: Units) -> R<mt::Matrix4> {
- let cx = Ctx { ex: &self.ex, len: u.len, ang: u.ang, tol: u.tol, snap: u.snap };
+ let cx = Ctx { ex: &self.ex, len: u.len, ang: u.ang, tol: u.tol };
let e = geom::entity(&cx, item)?;
- if matches!(e.name(), "CARTESIAN_TRANSFORMATION_OPERATOR_3D" | "CARTESIAN_TRANSFORMATION_OPERATOR_3D_NON_UNIFORM") {
+ if e.name() == "CARTESIAN_TRANSFORMATION_OPERATOR_3D" {
let p = e.params();
let o = geom::point(&cx, p.get(4).and_then(Param::as_ref_id).ok_or("transformation without origin")?)?;
- let direction = |i: usize, fallback: mt::Vector3| -> R<mt::Vector3> {
- match p.get(i) {
- None | Some(Param::Null) => Ok(fallback),
- Some(Param::Ref(id)) => geom::direction(&cx, *id),
- _ => Err("invalid transformation direction".into()),
- }
- };
- let scale = |i: usize, fallback: f64| -> R<f64> {
- match p.get(i) {
- None | Some(Param::Null) => Ok(fallback),
- Some(v) => v.as_f64().filter(|x| x.is_finite() && *x != 0.0).ok_or_else(|| "invalid transformation scale".into()),
- }
- };
- let sx = scale(5, 1.0)?;
- let (sy, sz) = (scale(7, sx)?, scale(8, sx)?);
- let m = mt::Matrix4::from_cols(
- (direction(2, mt::Vector3::unit_x())? * sx).extend(0.0),
- (direction(3, mt::Vector3::unit_y())? * sy).extend(0.0),
- (direction(6, mt::Vector3::unit_z())? * sz).extend(0.0),
- o.to_homogeneous(),
- );
- if !to_array(&m).iter().flatten().all(|x| x.is_finite()) {
- return Err("invalid transformation scale".into());
- }
- use mt::SquareMatrix as _;
- let det = m.determinant();
- if !det.is_finite() || det == 0.0 {
- return Err("singular transformation".into());
- }
- return Ok(m);
+ return Ok(mt::Matrix4::from_translation(o.to_homogeneous().truncate()));
}
Ok(geom::frame(&cx, item)?.matrix())
}
@@ -789,7 +659,7 @@ impl Reader {
};
let placement = self.batches.get(p.batch).map_or(mt::Matrix4::from_scale(1.0), |b| b.m);
sources.push((i, item));
- jobs.push(parallel::Job { source, placement, check_mesh: !self.unmeshed_checked.contains(&item), tol: units.tol });
+ jobs.push(parallel::Job { source, placement, check_mesh: !self.unmeshed_checked.contains(&item) });
}
// Reading and orienting new parts is shared out between the workers.
let prepared = parallel::bodies(&ex, &jobs, self.workers);
@@ -1322,15 +1192,7 @@ impl StepReading {
return Err(KernelError::Failed(format!("STEP import: no bodies ({why})")));
}
let bodies = std::mem::take(&mut rd.bodies);
- Ok(StepImport {
- bodies,
- tree,
- warnings,
- schema: ex.schemas.join(", "),
- system: ex.originating_system.clone(),
- preprocessor: ex.preprocessor.clone(),
- description: ex.description.clone(),
- })
+ Ok(StepImport { bodies, tree, warnings, schema: ex.schemas.join(", "), system: ex.originating_system.clone() })
}
/// [`Self::finish`], and the import is cached for the next read of the same text.
diff --git a/crates/smlib/src/check.rs b/crates/smlib/src/check.rs
index 145bf66f3c..8fa247b14e 100644
--- a/crates/smlib/src/check.rs
+++ b/crates/smlib/src/check.rs
@@ -69,6 +69,24 @@ mod tests {
}
}
+ #[test]
+ fn every_primitive_is_valid_at_every_level() {
+ let o = [1.0, 2.0, 3.0];
+ let bodies = [
+ ("cylinder", Brep::make_cylinder(o, 15.0, 50.0).unwrap()),
+ ("sphere", Brep::make_sphere(o, 12.0).unwrap()),
+ ("frustum", Brep::make_cone(o, 10.0, 4.0, 20.0).unwrap()),
+ ("cone", Brep::make_cone(o, 10.0, 0.0, 20.0).unwrap()),
+ ("torus", Brep::make_torus(o, 22.0, 4.0).unwrap()),
+ ];
+ for (name, b) in &bodies {
+ for level in [CheckLevel::Basic, CheckLevel::Geometry, CheckLevel::All] {
+ let r = b.check(level).unwrap();
+ assert!(r.is_valid(), "{name}, {level:?}: {r}");
+ }
+ }
+ }
+
#[test]
fn a_copy_and_a_native_file_round_trip_stay_valid() {
let b = Brep::make_box([0.0; 3], [2.0, 3.0, 4.0]).unwrap();
diff --git a/crates/smlib/src/measure.rs b/crates/smlib/src/measure.rs
index b19c250c10..e0e2f3fdf2 100644
--- a/crates/smlib/src/measure.rs
+++ b/crates/smlib/src/measure.rs
@@ -51,6 +51,7 @@ impl Brep {
mod tests {
use super::*;
use crate::Error;
+ use std::f64::consts::PI;
fn rel(a: f64, b: f64) -> f64 {
if b == 0.0 { a.abs() } else { (a / b - 1.0).abs() }
@@ -97,6 +98,133 @@ mod tests {
assert!(t[0][1].abs() < 1e-9 * m.moments[2]);
}
+ /// Reads `tests/oracle/<case>/expected.json`.
+ #[allow(clippy::unwrap_used)] // test fixture
+ fn oracle(case: &str) -> serde_json::Value {
+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/oracle").join(case).join("expected.json");
+ let v: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
+ assert_eq!(v["source"], "analytic", "{case}");
+ v
+ }
+
+ /// Closed forms: area, volume, centroid and Ixx, Iyy, Izz about it (products are 0 for all).
+ struct Expected {
+ name: &'static str,
+ body: Brep,
+ area: f64,
+ volume: f64,
+ centroid: [f64; 3],
+ moments: [f64; 3],
+ }
+
+ #[allow(clippy::unwrap_used)] // test fixture
+ fn cylinder(name: &'static str, o: [f64; 3], r: f64, h: f64) -> Expected {
+ let v = PI * r * r * h;
+ let ixx = v * (3.0 * r * r + h * h) / 12.0;
+ Expected {
+ name,
+ body: Brep::make_cylinder(o, r, h).unwrap(),
+ area: 2.0 * PI * r * r + 2.0 * PI * r * h,
+ volume: v,
+ centroid: [o[0], o[1], o[2] + h / 2.0],
+ moments: [ixx, ixx, v * r * r / 2.0],
+ }
+ }
+
+ #[allow(clippy::unwrap_used)] // test fixture
+ fn sphere(name: &'static str, o: [f64; 3], r: f64) -> Expected {
+ let v = 4.0 / 3.0 * PI * r * r * r;
+ let i = 0.4 * v * r * r;
+ Expected { name, body: Brep::make_sphere(o, r).unwrap(), area: 4.0 * PI * r * r, volume: v, centroid: o, moments: [i; 3] }
+ }
+
+ /// A frustum (a cone when `rt` is 0) about +Z on its base centre. Its volume integrals are
+ /// polynomials in z of degree at most 4, so three-point Gauss-Legendre gives them exactly.
+ #[allow(clippy::unwrap_used)] // test fixture
+ fn cone(name: &'static str, o: [f64; 3], rb: f64, rt: f64, h: f64) -> Expected {
+ let (x, w) = ([-(0.6f64).sqrt(), 0.0, (0.6f64).sqrt()], [5.0 / 9.0, 8.0 / 9.0, 5.0 / 9.0]);
+ let (mut v, mut mz, mut zz, mut izz) = (0.0, 0.0, 0.0, 0.0);
+ for i in 0..3 {
+ let (z, wi) = (h / 2.0 * (1.0 + x[i]), w[i] * h / 2.0);
+ let rho = rb + (rt - rb) * z / h;
+ let disc = PI * rho * rho;
+ v += wi * disc;
+ mz += wi * disc * z;
+ zz += wi * disc * z * z;
+ izz += wi * disc * rho * rho / 2.0;
+ }
+ let zc = mz / v;
+ let ixx = izz / 2.0 + zz - v * zc * zc;
+ let s = ((rb - rt).powi(2) + h * h).sqrt();
+ Expected {
+ name,
+ body: Brep::make_cone(o, rb, rt, h).unwrap(),
+ area: PI * (rb * rb + rt * rt + (rb + rt) * s),
+ volume: v,
+ centroid: [o[0], o[1], o[2] + zc],
+ moments: [ixx, ixx, izz],
+ }
+ }
+
+ /// A ring torus about +Z (Pappus for area and volume).
+ #[allow(clippy::unwrap_used)] // test fixture
+ fn torus(name: &'static str, o: [f64; 3], big: f64, r: f64) -> Expected {
+ let v = 2.0 * PI * PI * big * r * r;
+ let ixx = v * (big * big / 2.0 + 5.0 * r * r / 8.0);
+ Expected {
+ name,
+ body: Brep::make_torus(o, big, r).unwrap(),
+ area: 4.0 * PI * PI * big * r,
+ volume: v,
+ centroid: o,
+ moments: [ixx, ixx, v * (big * big + 0.75 * r * r)],
+ }
+ }
+
+ /// Every primitive against its closed form, at the origin and 2e5 mm out, to 1e-6 (tests/oracle
+ /// accepts 1e-3). Cylinders as oracle 02 and 38 give them, against their expected.json too.
+ #[test]
+ fn primitives_match_their_closed_forms() {
+ for (case, r) in [("02-cylinder", 15.0), ("38-coincident-cylinders", 10.0)] {
+ let e = oracle(case);
+ let m = Brep::make_cylinder([0.0; 3], r, 50.0).unwrap().mass_properties(MEASURE_ACCURACY).unwrap();
+ assert!(rel(m.volume, e["volume_mm3"].as_f64().unwrap()) < 1e-6, "{case}: {m:?}");
+ assert!(rel(m.area, e["area_mm2"].as_f64().unwrap()) < 1e-6, "{case}: {m:?}");
+ for i in 0..3 {
+ assert!((m.centroid[i] - e["center_of_mass"][i].as_f64().unwrap()).abs() < 1e-6, "{case}: {m:?}");
+ }
+ }
+ let far = [1.0e5, -2.0e5, 3.0e4];
+ let cases = [
+ cylinder("cylinder r15 h50", [0.0; 3], 15.0, 50.0),
+ cylinder("cylinder r0.25 h80, far", far, 0.25, 80.0),
+ // The primitives of oracle 40-box-sphere-torus, whole.
+ sphere("sphere r12", [0.0; 3], 12.0),
+ sphere("sphere r7, far", far, 7.0),
+ torus("torus R22 r4", [0.0; 3], 22.0, 4.0),
+ torus("torus R30 r0.5, far", far, 30.0, 0.5),
+ cone("frustum r10..4 h20", [0.0; 3], 10.0, 4.0, 20.0),
+ cone("cone r10 h20", [0.0; 3], 10.0, 0.0, 20.0),
+ cone("inverted frustum r3..9 h7", [0.0; 3], 3.0, 9.0, 7.0),
+ cone("frustum r6..2 h9, far", far, 6.0, 2.0, 9.0),
+ ];
+ for e in &cases {
+ let m = e.body.mass_properties(MEASURE_ACCURACY).unwrap();
+ let name = e.name;
+ assert!(rel(m.area, e.area) < 1e-6, "{name}: {m:?}");
+ assert!(rel(e.body.area(MEASURE_ACCURACY).unwrap(), e.area) < 1e-6, "{name}");
+ assert!(rel(m.volume, e.volume) < 1e-6, "{name}: {m:?}");
+ // The centroid against the body's size: twice its largest radius of gyration.
+ let size = 2.0 * e.moments.iter().fold(0.0f64, |a, &b| a.max(b)).sqrt() / e.volume.sqrt();
+ let imax = e.moments.iter().fold(0.0f64, |a, &b| a.max(b));
+ for i in 0..3 {
+ assert!((m.centroid[i] - e.centroid[i]).abs() < 1e-6 * size, "{name}: {m:?}");
+ assert!(rel(m.moments[i], e.moments[i]) < 1e-6, "{name}: {m:?}");
+ assert!(m.products[i].abs() < 1e-6 * imax, "{name}: {m:?}");
+ }
+ }
+ }
+
#[test]
fn bad_accuracies_are_errors() {
let b = Brep::make_box([0.0; 3], [1.0; 3]).unwrap();
diff --git a/crates/step/src/lib.rs b/crates/step/src/lib.rs
index 2d66f20e8d..edbed383df 100644
--- a/crates/step/src/lib.rs
+++ b/crates/step/src/lib.rs
@@ -10,6 +10,7 @@ mod eval;
pub mod geom;
pub mod p21;
pub mod read;
+pub mod units;
pub mod write;
#[cfg(test)]
diff --git a/crates/step/src/units.rs b/crates/step/src/units.rs
new file mode 100644
index 0000000000..be83d09eb5
--- /dev/null
+++ b/crates/step/src/units.rs
@@ -0,0 +1,115 @@
+//! The units of a representation context (ISO 10303-41 measures and units, ISO 10303-43
+//! representation contexts): GLOBAL_UNIT_ASSIGNED_CONTEXT's length and plane angle units and
+//! GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT's distance uncertainty. Missing or malformed units read
+//! as millimetres and radians; nothing here fails.
+
+use crate::p21::{Exchange, Param};
+use crate::read::Units;
+
+/// What a representation context says about its numbers.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Context {
+ pub units: Units,
+ /// The context's distance uncertainty in millimetres (the last UNCERTAINTY_MEASURE_WITH_UNIT
+ /// it lists), if any.
+ pub uncertainty: Option<f64>,
+}
+
+impl Default for Context {
+ fn default() -> Self {
+ Context { units: Units::default(), uncertainty: None }
+ }
+}
+
+fn si_prefix(p: Option<&Param>) -> f64 {
+ match p.and_then(Param::as_enum) {
+ Some("EXA") => 1e18,
+ Some("PETA") => 1e15,
+ Some("TERA") => 1e12,
+ Some("GIGA") => 1e9,
+ Some("MEGA") => 1e6,
+ Some("KILO") => 1e3,
+ Some("HECTO") => 1e2,
+ Some("DECA") => 1e1,
+ Some("DECI") => 1e-1,
+ Some("CENTI") => 1e-2,
+ Some("MILLI") => 1e-3,
+ Some("MICRO") => 1e-6,
+ Some("NANO") => 1e-9,
+ Some("PICO") => 1e-12,
+ Some("FEMTO") => 1e-15,
+ Some("ATTO") => 1e-18,
+ _ => 1.0,
+ }
+}
+
+/// What a unit measures.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum UnitKind {
+ Length,
+ Angle,
+ Other,
+}
+
+/// A unit's kind and size: millimetres for lengths, radians for plane angles (1 for anything
+/// else, or a unit that cannot be read).
+pub fn unit_factor(ex: &Exchange, id: u64) -> Option<(UnitKind, f64)> {
+ factor(ex, id, 0)
+}
+
+fn factor(ex: &Exchange, id: u64, depth: usize) -> Option<(UnitKind, f64)> {
+ if depth > 4 {
+ return None;
+ }
+ let e = ex.get(id)?;
+ let kind = if e.has("LENGTH_UNIT") {
+ UnitKind::Length
+ } else if e.has("PLANE_ANGLE_UNIT") {
+ UnitKind::Angle
+ } else {
+ UnitKind::Other
+ };
+ if let Some(si) = e.record("SI_UNIT") {
+ let base = match si.get(1).and_then(Param::as_enum) {
+ Some("METRE") => 1000.0,
+ Some("RADIAN") => 1.0,
+ _ => return Some((kind, 1.0)),
+ };
+ return Some((kind, si_prefix(si.first()) * base));
+ }
+ if let Some(cb) = e.record("CONVERSION_BASED_UNIT") {
+ let m = ex.get(cb.get(1)?.as_ref_id()?)?;
+ let mp = m.params();
+ let v = mp.first()?.as_f64()?;
+ let (_, f) = factor(ex, mp.get(1)?.as_ref_id()?, depth + 1)?;
+ let x = v * f;
+ return (x.is_finite() && x > 0.0).then_some((kind, x));
+ }
+ Some((kind, 1.0))
+}
+
+/// The units and uncertainty of the representation context `id`.
+pub fn context(ex: &Exchange, id: u64) -> Context {
+ let mut c = Context::default();
+ let Some(e) = ex.get(id) else { return c };
+ if let Some(units) = e.record("GLOBAL_UNIT_ASSIGNED_CONTEXT").and_then(|r| r.first()).and_then(Param::as_list) {
+ for uid in units.iter().filter_map(Param::as_ref_id) {
+ match unit_factor(ex, uid) {
+ Some((UnitKind::Length, f)) if f.is_finite() && f > 0.0 => c.units.len = f,
+ Some((UnitKind::Angle, f)) if f.is_finite() && f > 0.0 => c.units.ang = f,
+ _ => {}
+ }
+ }
+ }
+ if let Some(unc) = e.record("GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT").and_then(|r| r.first()).and_then(Param::as_list) {
+ for uid in unc.iter().filter_map(Param::as_ref_id) {
+ if let Some(m) = ex.get(uid)
+ && let Some(v) = m.params().first().and_then(Param::as_f64)
+ {
+ let f = m.params().get(1).and_then(Param::as_ref_id).and_then(|x| unit_factor(ex, x)).map(|x| x.1);
+ c.uncertainty = Some(v * f.unwrap_or(c.units.len)).filter(|t| t.is_finite() && *t > 0.0);
+ }
+ }
+ }
+ c
+}