use std::f64::consts::FRAC_PI_2;
use std::fmt;
use std::ops::Mul;
use crate::model::Graph;
use crate::p21::{Exchange, Instance, Literal, Param, Record};
use crate::policy::{MIN_AXIS_SINE, SEMI_ANGLE_MARGIN};
use crate::record;
use crate::units::{ContextUnits, UnitError};
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Rigid {
pub rotation: [[f64; 3]; 3],
pub translation: [f64; 3],
}
impl Rigid {
pub const IDENTITY: Self = Self {
rotation: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
translation: [0.0, 0.0, 0.0],
};
pub fn apply_point(&self, p: [f64; 3]) -> [f64; 3] {
let rotated = self.apply_vector(p);
[
rotated[0] + self.translation[0],
rotated[1] + self.translation[1],
rotated[2] + self.translation[2],
]
}
pub fn apply_vector(&self, v: [f64; 3]) -> [f64; 3] {
self.rotation.map(|row| dot(row, v))
}
#[must_use]
pub fn inverse(&self) -> Self {
let r = &self.rotation;
let rotation = [
[r[0][0], r[1][0], r[2][0]],
[r[0][1], r[1][1], r[2][1]],
[r[0][2], r[1][2], r[2][2]],
];
let moved = rotation.map(|row| dot(row, self.translation));
Self {
rotation,
translation: [-moved[0], -moved[1], -moved[2]],
}
}
pub fn is_identity(&self) -> bool {
*self == Self::IDENTITY
}
}
impl Mul for Rigid {
type Output = Rigid;
fn mul(self, rhs: Rigid) -> Rigid {
let a = &self.rotation;
let b = &rhs.rotation;
let column = |col: usize| [b[0][col], b[1][col], b[2][col]];
let rotation = [0, 1, 2].map(|row| [0, 1, 2].map(|col| dot(a[row], column(col))));
Rigid {
rotation,
translation: self.apply_point(rhs.translation),
}
}
}
#[derive(Debug, Clone)]
pub struct Reader<'g, 'a> {
graph: &'g Graph<'a>,
units: ContextUnits,
}
impl<'g, 'a> Reader<'g, 'a> {
pub fn for_representation(
graph: &'g Graph<'a>,
representation: u64,
) -> Result<Self, UnitError> {
Ok(Self {
graph,
units: ContextUnits::of_representation(graph, representation)?,
})
}
pub fn for_item(graph: &'g Graph<'a>, item: u64) -> Result<Self, UnitError> {
Ok(Self {
graph,
units: ContextUnits::of_item(graph, item)?,
})
}
pub fn units(&self) -> &ContextUnits {
&self.units
}
pub fn point(&self, id: u64) -> Result<[f64; 3], GeometryError> {
self.read_point(id, None)
}
pub fn direction(&self, id: u64) -> Result<[f64; 3], GeometryError> {
self.read_direction(id, None)
}
pub fn vector(&self, id: u64) -> Result<Vector, GeometryError> {
self.read_vector(id, None)
}
pub fn axis1_placement(&self, id: u64) -> Result<Axis1Placement, GeometryError> {
let params = self.simple(id, None, "AXIS1_PLACEMENT", 3)?;
let location = self.read_point(
self.required(id, ¶ms[1], "location")?,
Some((id, "location")),
)?;
let axis = self.optional_direction(id, ¶ms[2], "axis")?;
Ok(Axis1Placement {
instance: id,
location,
axis: axis.unwrap_or(Z),
axis_stated: axis.is_some(),
})
}
pub fn axis2_placement_3d(&self, id: u64) -> Result<Axis2Placement3d, GeometryError> {
self.read_axis2_placement_3d(id, None)
}
pub fn surface(&self, id: u64) -> Result<Surface, GeometryError> {
let instance = self.entry(id, None)?;
let Some(record) = self.simple_record(instance) else {
return Ok(Surface::Other(self.other(id, instance)));
};
let surface = if record.is("PLANE") {
let params = self.params(id, record, 2)?;
Surface::Plane(Plane {
instance: id,
position: self.position(id, ¶ms)?,
})
} else if record.is("CYLINDRICAL_SURFACE") {
let params = self.params(id, record, 3)?;
Surface::Cylindrical(CylindricalSurface {
instance: id,
position: self.position(id, ¶ms)?,
radius: self.positive_length(id, ¶ms[2], "radius")?,
})
} else if record.is("CONICAL_SURFACE") {
let params = self.params(id, record, 4)?;
let position = self.position(id, ¶ms)?;
let radius = self.number(id, ¶ms[2], "radius")?;
if radius < 0.0 {
return Err(self.error(id, Some("radius"), out_of_range(radius)));
}
Surface::Conical(ConicalSurface {
instance: id,
position,
radius: self.length(id, "radius", radius)?,
semi_angle: self.semi_angle(id, ¶ms[3])?,
})
} else if record.is("SPHERICAL_SURFACE") {
let params = self.params(id, record, 3)?;
Surface::Spherical(SphericalSurface {
instance: id,
position: self.position(id, ¶ms)?,
radius: self.positive_length(id, ¶ms[2], "radius")?,
})
} else if record.is("TOROIDAL_SURFACE") {
let params = self.params(id, record, 4)?;
Surface::Toroidal(ToroidalSurface {
instance: id,
position: self.position(id, ¶ms)?,
major_radius: self.positive_length(id, ¶ms[2], "major_radius")?,
minor_radius: self.positive_length(id, ¶ms[3], "minor_radius")?,
})
} else {
Surface::Other(self.other(id, instance))
};
Ok(surface)
}
pub fn curve(&self, id: u64) -> Result<Curve, GeometryError> {
self.read_curve(id, None, true)
}
fn exchange(&self) -> &'g Exchange<'a> {
self.graph.exchange()
}
fn error(
&self,
id: u64,
attribute: Option<&'static str>,
kind: GeometryErrorKind,
) -> GeometryError {
let exchange = self.exchange();
GeometryError {
instance: id,
entity: exchange
.get(id)
.map(|instance| record::entity(exchange, instance))
.unwrap_or_default(),
attribute,
kind,
}
}
fn entry(&self, id: u64, from: Origin) -> Result<&'g Instance, GeometryError> {
self.exchange().get(id).ok_or_else(|| match from {
Some((holder, attribute)) => {
self.error(holder, Some(attribute), GeometryErrorKind::Missing)
}
None => self.error(id, None, GeometryErrorKind::Missing),
})
}
fn simple_record(&self, instance: &Instance) -> Option<Record<'g>> {
if instance.is_complex() {
return None;
}
self.exchange().records(instance).next()
}
fn other(&self, id: u64, instance: &Instance) -> OtherGeometry {
OtherGeometry {
instance: id,
entity: record::entity(self.exchange(), instance),
}
}
fn simple(
&self,
id: u64,
from: Origin,
name: &'static str,
count: usize,
) -> Result<Vec<Param<'g>>, GeometryError> {
let instance = self.entry(id, from)?;
if instance.is_complex() {
return Err(self.error(id, None, GeometryErrorKind::Complex));
}
match self.exchange().records(instance).next() {
Some(record) if record.is(name) => self.params(id, record, count),
_ => Err(self.error(id, None, GeometryErrorKind::WrongType { expected: name })),
}
}
fn params(
&self,
id: u64,
record: Record<'g>,
count: usize,
) -> Result<Vec<Param<'g>>, GeometryError> {
record::exact_params(record, count).ok_or_else(|| {
self.error(
id,
None,
GeometryErrorKind::ParameterCount {
expected: count,
found: record.params().count(),
},
)
})
}
fn reference(
&self,
id: u64,
param: &Param<'_>,
attribute: &'static str,
) -> Result<Option<u64>, GeometryError> {
match param {
Param::Reference(target) => Ok(Some(*target)),
Param::Unset => Ok(None),
_ => Err(self.error(id, Some(attribute), GeometryErrorKind::NotAReference)),
}
}
fn required(
&self,
id: u64,
param: &Param<'_>,
attribute: &'static str,
) -> Result<u64, GeometryError> {
self.reference(id, param, attribute)?
.ok_or_else(|| self.error(id, Some(attribute), GeometryErrorKind::MissingAttribute))
}
fn number(
&self,
id: u64,
param: &Param<'_>,
attribute: &'static str,
) -> Result<f64, GeometryError> {
if let Some(value) = record::real(param) {
return Ok(value);
}
let kind = match param {
Param::Integer(literal) | Param::Real(literal) if literal.to_f64().is_some() => {
GeometryErrorKind::NotFinite
}
Param::Unset => GeometryErrorKind::MissingAttribute,
_ => GeometryErrorKind::NotANumber,
};
Err(self.error(id, Some(attribute), kind))
}
fn triple(
&self,
id: u64,
param: &Param<'_>,
attribute: &'static str,
) -> Result<[f64; 3], GeometryError> {
let Some(items) = param.list() else {
let kind = if param.is_unset() {
GeometryErrorKind::MissingAttribute
} else {
GeometryErrorKind::NotAList
};
return Err(self.error(id, Some(attribute), kind));
};
let items: Vec<Param<'_>> = items.collect();
let [a, b, c] = items.as_slice() else {
return Err(self.error(
id,
Some(attribute),
GeometryErrorKind::Dimension { found: items.len() },
));
};
Ok([
self.number(id, a, attribute)?,
self.number(id, b, attribute)?,
self.number(id, c, attribute)?,
])
}
fn length(&self, id: u64, attribute: &'static str, value: f64) -> Result<f64, GeometryError> {
if value == 0.0 {
return Ok(value);
}
let unit = self
.units
.length()
.map_err(|e| self.error(id, Some(attribute), GeometryErrorKind::Unit(e)))?;
let millimetres = value * unit.factor;
if !millimetres.is_finite() {
return Err(self.error(id, Some(attribute), GeometryErrorKind::NotFinite));
}
Ok(millimetres)
}
fn positive_length(
&self,
id: u64,
param: &Param<'_>,
attribute: &'static str,
) -> Result<f64, GeometryError> {
let value = self.number(id, param, attribute)?;
let refuse = || self.error(id, Some(attribute), out_of_range(value));
if value <= 0.0 {
return Err(refuse());
}
let millimetres = self.length(id, attribute, value)?;
if millimetres <= 0.0 {
return Err(refuse());
}
Ok(millimetres)
}
fn semi_angle(&self, id: u64, param: &Param<'_>) -> Result<f64, GeometryError> {
const ATTRIBUTE: &str = "semi_angle";
let value = self.number(id, param, ATTRIBUTE)?;
let refuse = || self.error(id, Some(ATTRIBUTE), out_of_range(value));
if value <= 0.0 {
return Err(refuse());
}
let unit = self
.units
.plane_angle()
.map_err(|e| self.error(id, Some(ATTRIBUTE), GeometryErrorKind::Unit(e)))?;
let radians = value * unit.factor;
if !(SEMI_ANGLE_MARGIN..=FRAC_PI_2 - SEMI_ANGLE_MARGIN).contains(&radians) {
return Err(refuse());
}
Ok(radians)
}
fn read_point(&self, id: u64, from: Origin) -> Result<[f64; 3], GeometryError> {
const ATTRIBUTE: &str = "coordinates";
let params = self.simple(id, from, "CARTESIAN_POINT", 2)?;
let [x, y, z] = self.triple(id, ¶ms[1], ATTRIBUTE)?;
Ok([
self.length(id, ATTRIBUTE, x)?,
self.length(id, ATTRIBUTE, y)?,
self.length(id, ATTRIBUTE, z)?,
])
}
fn read_direction(&self, id: u64, from: Origin) -> Result<[f64; 3], GeometryError> {
const ATTRIBUTE: &str = "direction_ratios";
let params = self.simple(id, from, "DIRECTION", 2)?;
let ratios = self.triple(id, ¶ms[1], ATTRIBUTE)?;
normalise(ratios)
.ok_or_else(|| self.error(id, Some(ATTRIBUTE), GeometryErrorKind::ZeroDirection))
}
fn optional_direction(
&self,
id: u64,
param: &Param<'_>,
attribute: &'static str,
) -> Result<Option<[f64; 3]>, GeometryError> {
self.reference(id, param, attribute)?
.map(|direction| self.read_direction(direction, Some((id, attribute))))
.transpose()
}
fn read_vector(&self, id: u64, from: Origin) -> Result<Vector, GeometryError> {
let params = self.simple(id, from, "VECTOR", 3)?;
let orientation = self.read_direction(
self.required(id, ¶ms[1], "orientation")?,
Some((id, "orientation")),
)?;
let magnitude = self.number(id, ¶ms[2], "magnitude")?;
if magnitude < 0.0 {
return Err(self.error(id, Some("magnitude"), out_of_range(magnitude)));
}
Ok(Vector {
instance: id,
orientation,
magnitude: self.length(id, "magnitude", magnitude)?,
})
}
fn read_axis2_placement_3d(
&self,
id: u64,
from: Origin,
) -> Result<Axis2Placement3d, GeometryError> {
let instance = self.entry(id, from)?;
if self
.simple_record(instance)
.is_some_and(|record| record.is("AXIS2_PLACEMENT_2D"))
{
return Err(self.error(id, None, GeometryErrorKind::Dimension { found: 2 }));
}
let params = self.simple(id, from, "AXIS2_PLACEMENT_3D", 4)?;
let location = self.read_point(
self.required(id, ¶ms[1], "location")?,
Some((id, "location")),
)?;
let axis = self.optional_direction(id, ¶ms[2], "axis")?;
let ref_direction = self.optional_direction(id, ¶ms[3], "ref_direction")?;
let z = axis.unwrap_or(Z);
let [x, y] = build_axes(z, ref_direction)
.ok_or_else(|| self.error(id, Some("ref_direction"), GeometryErrorKind::Parallel))?;
Ok(Axis2Placement3d {
instance: id,
location,
x,
y,
z,
axis_stated: axis.is_some(),
ref_direction_stated: ref_direction.is_some(),
})
}
fn position(&self, id: u64, params: &[Param<'_>]) -> Result<Axis2Placement3d, GeometryError> {
let position = self.required(id, ¶ms[1], "position")?;
self.read_axis2_placement_3d(position, Some((id, "position")))
}
fn read_curve(&self, id: u64, from: Origin, unwrap: bool) -> Result<Curve, GeometryError> {
let instance = self.entry(id, from)?;
let Some(record) = self.simple_record(instance) else {
return Ok(Curve::Other(self.other(id, instance)));
};
let curve = if record.is("LINE") {
let params = self.params(id, record, 3)?;
let pnt = self.read_point(self.required(id, ¶ms[1], "pnt")?, Some((id, "pnt")))?;
let dir = self.read_vector(self.required(id, ¶ms[2], "dir")?, Some((id, "dir")))?;
Curve::Line(Line {
instance: id,
pnt,
dir,
})
} else if record.is("CIRCLE") {
let params = self.params(id, record, 3)?;
Curve::Circle(Circle {
instance: id,
position: self.position(id, ¶ms)?,
radius: self.positive_length(id, ¶ms[2], "radius")?,
})
} else if let (true, Some(subtype)) = (unwrap, SurfaceCurveKind::of(record)) {
let params = self.params(id, record, 4)?;
let curve_3d = self.required(id, ¶ms[1], "curve_3d")?;
let curve_3d = self.read_curve(curve_3d, Some((id, "curve_3d")), false)?;
if params[2].list().is_none() {
return Err(self.error(
id,
Some("associated_geometry"),
GeometryErrorKind::NotAList,
));
}
Curve::SurfaceCurve(SurfaceCurve {
instance: id,
subtype,
curve_3d: Box::new(curve_3d),
master_representation: self.master_representation(id, ¶ms[3])?,
})
} else {
Curve::Other(self.other(id, instance))
};
Ok(curve)
}
fn master_representation(
&self,
id: u64,
param: &Param<'_>,
) -> Result<MasterRepresentation, GeometryError> {
const ATTRIBUTE: &str = "master_representation";
if param.is_unset() {
return Err(self.error(id, Some(ATTRIBUTE), GeometryErrorKind::MissingAttribute));
}
let value = param.literal().and_then(Literal::enumeration);
[
("CURVE_3D", MasterRepresentation::Curve3d),
("PCURVE_S1", MasterRepresentation::PcurveS1),
("PCURVE_S2", MasterRepresentation::PcurveS2),
]
.into_iter()
.find(|(name, _)| value.is_some_and(|value| value.eq_ignore_ascii_case(name)))
.map(|(_, master)| master)
.ok_or_else(|| self.error(id, Some(ATTRIBUTE), GeometryErrorKind::UnknownEnumeration))
}
}
type Origin = Option<(u64, &'static str)>;
const Z: [f64; 3] = [0.0, 0.0, 1.0];
fn out_of_range(value: f64) -> GeometryErrorKind {
GeometryErrorKind::OutOfRange { value }
}
fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
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 normalise(v: [f64; 3]) -> Option<[f64; 3]> {
let largest = v.iter().fold(0.0f64, |m, c| m.max(c.abs()));
if largest == 0.0 {
return None;
}
let scaled = v.map(|c| c / largest);
let length = dot(scaled, scaled).sqrt();
Some(scaled.map(|c| c / length))
}
fn build_axes(z: [f64; 3], ref_direction: Option<[f64; 3]>) -> Option<[[f64; 3]; 2]> {
let projected = if let Some(v) = ref_direction {
let normal = cross(v, z);
if dot(normal, normal).sqrt() < MIN_AXIS_SINE {
return None;
}
let along = dot(v, z);
[
v[0] - along * z[0],
v[1] - along * z[1],
v[2] - along * z[2],
]
} else {
#[expect(
clippy::float_cmp,
reason = "the schema's first_proj_axis compares the normalised axis exactly"
)]
let along_x = z == [1.0, 0.0, 0.0] || z == [-1.0, 0.0, 0.0];
let k = usize::from(along_x);
let mut projected = [0.0; 3];
for (i, component) in projected.iter_mut().enumerate() {
*component = if i == k {
(0..3).filter(|&j| j != k).map(|j| z[j] * z[j]).sum()
} else {
0.0 - z[k] * z[i]
};
}
projected
};
let x = normalise(projected)?;
let y = normalise(cross(z, x))?;
Some([x, y])
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Vector {
pub instance: u64,
pub orientation: [f64; 3],
pub magnitude: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Axis1Placement {
pub instance: u64,
pub location: [f64; 3],
pub axis: [f64; 3],
pub axis_stated: bool,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Axis2Placement3d {
pub instance: u64,
pub location: [f64; 3],
pub x: [f64; 3],
pub y: [f64; 3],
pub z: [f64; 3],
pub axis_stated: bool,
pub ref_direction_stated: bool,
}
impl Axis2Placement3d {
pub fn to_rigid(&self) -> Rigid {
let (x, y, z) = (self.x, self.y, self.z);
Rigid {
rotation: [[x[0], y[0], z[0]], [x[1], y[1], z[1]], [x[2], y[2], z[2]]],
translation: self.location,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
#[non_exhaustive]
pub enum Surface {
Plane(Plane),
Cylindrical(CylindricalSurface),
Conical(ConicalSurface),
Spherical(SphericalSurface),
Toroidal(ToroidalSurface),
Other(OtherGeometry),
}
impl Surface {
pub fn instance(&self) -> u64 {
match self {
Self::Plane(s) => s.instance,
Self::Cylindrical(s) => s.instance,
Self::Conical(s) => s.instance,
Self::Spherical(s) => s.instance,
Self::Toroidal(s) => s.instance,
Self::Other(s) => s.instance,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Plane {
pub instance: u64,
pub position: Axis2Placement3d,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct CylindricalSurface {
pub instance: u64,
pub position: Axis2Placement3d,
pub radius: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct ConicalSurface {
pub instance: u64,
pub position: Axis2Placement3d,
pub radius: f64,
pub semi_angle: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct SphericalSurface {
pub instance: u64,
pub position: Axis2Placement3d,
pub radius: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct ToroidalSurface {
pub instance: u64,
pub position: Axis2Placement3d,
pub major_radius: f64,
pub minor_radius: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
#[non_exhaustive]
pub enum Curve {
Line(Line),
Circle(Circle),
SurfaceCurve(SurfaceCurve),
Other(OtherGeometry),
}
impl Curve {
pub fn instance(&self) -> u64 {
match self {
Self::Line(c) => c.instance,
Self::Circle(c) => c.instance,
Self::SurfaceCurve(c) => c.instance,
Self::Other(c) => c.instance,
}
}
pub fn basis(&self) -> &Curve {
match self {
Self::SurfaceCurve(c) => &c.curve_3d,
_ => self,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Line {
pub instance: u64,
pub pnt: [f64; 3],
pub dir: Vector,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Circle {
pub instance: u64,
pub position: Axis2Placement3d,
pub radius: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct SurfaceCurve {
pub instance: u64,
pub subtype: SurfaceCurveKind,
pub curve_3d: Box<Curve>,
pub master_representation: MasterRepresentation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum SurfaceCurveKind {
SurfaceCurve,
SeamCurve,
IntersectionCurve,
}
impl SurfaceCurveKind {
fn of(record: Record<'_>) -> Option<Self> {
[
("SURFACE_CURVE", Self::SurfaceCurve),
("SEAM_CURVE", Self::SeamCurve),
("INTERSECTION_CURVE", Self::IntersectionCurve),
]
.into_iter()
.find(|(name, _)| record.is(name))
.map(|(_, kind)| kind)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum MasterRepresentation {
#[cfg_attr(feature = "serde", serde(rename = "curve_3d"))]
Curve3d,
#[cfg_attr(feature = "serde", serde(rename = "pcurve_s1"))]
PcurveS1,
#[cfg_attr(feature = "serde", serde(rename = "pcurve_s2"))]
PcurveS2,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct OtherGeometry {
pub instance: u64,
pub entity: String,
}
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct GeometryError {
pub instance: u64,
pub entity: String,
pub attribute: Option<&'static str>,
pub kind: GeometryErrorKind,
}
impl fmt::Display for GeometryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "#{}", self.instance)?;
if !self.entity.is_empty() {
write!(f, " ({})", self.entity)?;
}
if let Some(attribute) = self.attribute {
write!(f, " {attribute}")?;
}
write!(f, ": {}", self.kind)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
#[non_exhaustive]
pub enum GeometryErrorKind {
Missing,
WrongType {
expected: &'static str,
},
Complex,
ParameterCount {
expected: usize,
found: usize,
},
MissingAttribute,
NotANumber,
NotAReference,
NotAList,
NotFinite,
Dimension {
found: usize,
},
ZeroDirection,
Parallel,
OutOfRange {
value: f64,
},
UnknownEnumeration,
#[cfg_attr(feature = "serde", serde(serialize_with = "unit_error"))]
Unit(UnitError),
}
#[cfg(feature = "serde")]
fn unit_error<S: serde::Serializer>(error: &UnitError, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeMap;
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("error", error)?;
map.end()
}
impl fmt::Display for GeometryErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Missing => f.write_str("not defined"),
Self::WrongType { expected } => write!(f, "not a {expected}"),
Self::Complex => f.write_str("a complex instance, which is not read as one entity"),
Self::ParameterCount { expected, found } => write!(
f,
"{found} attributes where the schema has {expected}, so it is not read"
),
Self::MissingAttribute => f.write_str("unset, but the schema requires a value"),
Self::NotANumber => f.write_str("not a number"),
Self::NotAReference => f.write_str("not a reference"),
Self::NotAList => f.write_str("not a list"),
Self::NotFinite => f.write_str("not a finite number"),
Self::Dimension { found } => {
write!(f, "{found} coordinates; only 3D geometry is read")
}
Self::ZeroDirection => f.write_str("every direction ratio is zero"),
Self::Parallel => f.write_str("parallel to the axis (a sine below 1e-6)"),
Self::OutOfRange { value } => write!(f, "{value} is out of range"),
Self::UnknownEnumeration => f.write_str("not .CURVE_3D., .PCURVE_S1. or .PCURVE_S2."),
Self::Unit(error) => write!(f, "its unit cannot be read: {error}"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::p21::parse;
use crate::units::UnitKind;
const UNITS: &str = "#1=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));\
#2=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));\
#3=DIMENSIONAL_EXPONENTS(1.,0.,0.,0.,0.,0.,0.);\
#4=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(25.4),#1);\
#5=(CONVERSION_BASED_UNIT('INCH',#4)LENGTH_UNIT()NAMED_UNIT(#3));\
#6=DIMENSIONAL_EXPONENTS(0.,0.,0.,0.,0.,0.,0.);\
#7=PLANE_ANGLE_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(0.0174532925),#2);\
#8=(CONVERSION_BASED_UNIT('DEGREE',#7)NAMED_UNIT(#6)PLANE_ANGLE_UNIT());\
#9=PLANE_ANGLE_MEASURE_WITH_UNIT(PLANE_ANGLE_MEASURE(0.01745329251994),#2);\
#10=(CONVERSION_BASED_UNIT('DEGREE',#9)NAMED_UNIT(#6)PLANE_ANGLE_UNIT());\
#20=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#2))\
REPRESENTATION_CONTEXT('',''));\
#21=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#5,#8))\
REPRESENTATION_CONTEXT('',''));\
#22=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#1,#10))\
REPRESENTATION_CONTEXT('',''));\
#23=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#1))\
REPRESENTATION_CONTEXT('',''));\
#24=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#2))\
REPRESENTATION_CONTEXT('',''));\
#30=SHAPE_REPRESENTATION('',(),#20);\
#31=SHAPE_REPRESENTATION('',(),#21);\
#32=SHAPE_REPRESENTATION('',(),#22);\
#33=SHAPE_REPRESENTATION('',(),#23);\
#34=SHAPE_REPRESENTATION('',(),#24);";
const MM: u64 = 30;
const INCH: u64 = 31;
const MM_DEGREE: u64 = 32;
const NO_ANGLE: u64 = 33;
const NO_LENGTH: u64 = 34;
const FRAME: &str = "#100=CARTESIAN_POINT('',(0.,0.,0.));\
#101=DIRECTION('',(1.,0.,0.));#102=DIRECTION('',(0.,1.,0.));\
#103=DIRECTION('',(0.,0.,1.));#104=AXIS2_PLACEMENT_3D('',#100,#103,#101);";
fn graph_in(schema: &str, data: &str) -> &'static Graph<'static> {
let src = format!(
"ISO-10303-21;HEADER;FILE_SCHEMA(('{schema}'));ENDSEC;DATA;\
{UNITS}{FRAME}{data}ENDSEC;END-ISO-10303-21;"
);
let src: &'static [u8] = Box::leak(src.into_bytes().into_boxed_slice());
Box::leak(Box::new(Graph::build(parse(src).unwrap())))
}
fn reader(representation: u64, data: &str) -> Reader<'static, 'static> {
Reader::for_representation(
graph_in("AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF", data),
representation,
)
.unwrap()
}
fn close(a: [f64; 3], b: [f64; 3]) -> bool {
a.iter().zip(b).all(|(a, b)| (a - b).abs() <= 1e-15)
}
fn bits(v: [f64; 3]) -> [u64; 3] {
v.map(f64::to_bits)
}
fn kind<T: fmt::Debug>(result: Result<T, GeometryError>) -> GeometryErrorKind {
result.unwrap_err().kind
}
fn surface(representation: u64, data: &str, id: u64) -> Surface {
reader(representation, data).surface(id).unwrap()
}
#[test]
fn points_are_read_in_millimetres() {
let data = "#200=CARTESIAN_POINT('',(1.,-2.5,0.5));#201=CARTESIAN_POINT('',(1,2,3));";
assert_eq!(reader(MM, data).point(200), Ok([1.0, -2.5, 0.5]));
assert_eq!(
bits(reader(INCH, data).point(200).unwrap()),
bits([25.4, -2.5 * 25.4, 0.5 * 25.4])
);
assert_eq!(
reader(MM, data).point(201),
Ok([1.0, 2.0, 3.0]),
"INTEGER tokens are read as numbers"
);
}
#[test]
fn directions_are_normalised_and_never_scaled() {
let data = "#200=DIRECTION('',(0.,0.,5.));#201=DIRECTION('',(0.,0.,0.));\
#202=DIRECTION('',(1.,0.));#203=DIRECTION('',(1.E999,0.,0.));\
#204=DIRECTION('',(1.E-200,0.,0.));#205=DIRECTION('',(-1.E200,0.,0.));\
#206=DIRECTION('',(3.E-162,4.E-162,0.));#207=DIRECTION('',(1.E155,1.E155,0.));\
#208=DIRECTION('',(0.,-0.,-0.03937));";
let mm = reader(MM, data);
let inch = reader(INCH, data);
assert_eq!(mm.direction(200), Ok([0.0, 0.0, 1.0]));
assert_eq!(
inch.direction(200),
Ok([0.0, 0.0, 1.0]),
"ratios have no unit"
);
let zero = mm.direction(201).unwrap_err();
assert_eq!(
(zero.instance, zero.attribute, zero.kind),
(
201,
Some("direction_ratios"),
GeometryErrorKind::ZeroDirection
)
);
assert_eq!(
kind(mm.direction(202)),
GeometryErrorKind::Dimension { found: 2 }
);
assert_eq!(kind(mm.direction(203)), GeometryErrorKind::NotFinite);
assert_eq!(mm.direction(204), Ok([1.0, 0.0, 0.0]));
assert_eq!(mm.direction(205), Ok([-1.0, 0.0, 0.0]));
let tiny = mm.direction(206).unwrap();
assert!(close(tiny, [0.6, 0.8, 0.0]), "{tiny:?}");
assert!((dot(tiny, tiny) - 1.0).abs() <= 1e-15);
let huge = mm.direction(207).unwrap();
assert!(close(huge, [0.5f64.sqrt(), 0.5f64.sqrt(), 0.0]), "{huge:?}");
assert_eq!(
mm.direction(208),
Ok([0.0, -0.0, -1.0]),
"a ratio scaled as if it were inches is still a direction"
);
}
#[test]
fn vectors_have_a_length_and_a_direction() {
let data = "#200=VECTOR('',#103,2.);#201=DIRECTION('',(0.,3.,4.));#202=VECTOR('',#201,1);\
#203=VECTOR('',#103,-1.);#204=VECTOR('',#103,0.);";
let inch = reader(INCH, data);
let vector = inch.vector(200).unwrap();
assert_eq!(
(vector.instance, vector.orientation, vector.magnitude),
(200, [0.0, 0.0, 1.0], 2.0 * 25.4)
);
let tilted = inch.vector(202).unwrap();
assert!(close(tilted.orientation, [0.0, 0.6, 0.8]));
assert_eq!(tilted.magnitude, 25.4);
let negative = inch.vector(203).unwrap_err();
assert_eq!(
(negative.attribute, negative.kind),
(
Some("magnitude"),
GeometryErrorKind::OutOfRange { value: -1.0 }
)
);
assert_eq!(
reader(NO_LENGTH, data).vector(204).map(|v| v.magnitude),
Ok(0.0),
"a zero length needs no unit"
);
}
#[test]
fn axis1_placements_default_their_axis() {
let data = "#200=CARTESIAN_POINT('',(1.,2.,3.));#201=AXIS1_PLACEMENT('',#200,$);\
#202=DIRECTION('',(0.,-2.,0.));#203=AXIS1_PLACEMENT('',#200,#202);";
let inch = reader(INCH, data);
let unset = inch.axis1_placement(201).unwrap();
assert_eq!(
(unset.location, unset.axis, unset.axis_stated),
([25.4, 2.0 * 25.4, 3.0 * 25.4], [0.0, 0.0, 1.0], false)
);
let stated = inch.axis1_placement(203).unwrap();
assert_eq!((stated.axis, stated.axis_stated), ([0.0, -1.0, 0.0], true));
}
fn placement(
representation: u64,
axis: &str,
ref_direction: &str,
) -> Result<Axis2Placement3d, GeometryError> {
let direction = |id, ratios: &str| {
if ratios == "$" {
("$".to_owned(), String::new())
} else {
(format!("#{id}"), format!("#{id}=DIRECTION('',({ratios}));"))
}
};
let (axis, axis_line) = direction(201, axis);
let (ref_direction, ref_line) = direction(202, ref_direction);
reader(
representation,
&format!(
"{axis_line}{ref_line}#200=AXIS2_PLACEMENT_3D('',#100,{axis},{ref_direction});"
),
)
.axis2_placement_3d(200)
}
#[test]
fn placements_without_directions_are_the_identity_frame() {
let frame = placement(MM, "$", "$").unwrap();
assert_eq!(
(frame.x, frame.y, frame.z),
([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0])
);
assert!(!frame.axis_stated && !frame.ref_direction_stated);
assert!(frame.to_rigid().is_identity());
assert!(
placement(NO_LENGTH, "$", "$")
.unwrap()
.to_rigid()
.is_identity()
);
}
#[test]
fn a_defaulted_ref_direction_follows_the_schema() {
for (axis, x) in [
("0.,0.,1.", [1.0, 0.0, 0.0]),
("1.,0.,0.", [0.0, 1.0, 0.0]),
("-1.,0.,0.", [0.0, 1.0, 0.0]),
("0.,1.,0.", [1.0, 0.0, 0.0]),
("0.,0.,-1.", [1.0, 0.0, 0.0]),
("2.,0.,0.", [0.0, 1.0, 0.0]),
] {
let frame = placement(MM, axis, "$").unwrap();
assert_eq!(frame.x, x, "axis {axis}");
assert!(frame.axis_stated && !frame.ref_direction_stated);
assert_eq!(cross(frame.x, frame.y), frame.z, "axis {axis}");
}
let tilted = placement(MM, "0.6,0.,0.8", "$").unwrap();
assert!(close(tilted.x, [0.8, 0.0, -0.6]), "{:?}", tilted.x);
assert!(close(tilted.y, [0.0, 1.0, 0.0]), "{:?}", tilted.y);
for axis in [
"1.,2.E-8,0.",
"-1.,0.,3.E-9",
"1.,1.E-300,0.",
"1.,1.E-12,1.E-12",
] {
let frame = placement(MM, axis, "$").unwrap();
for (a, b) in [(frame.x, frame.z), (frame.y, frame.z), (frame.x, frame.y)] {
assert!(dot(a, b).abs() <= 1e-15, "axis {axis}: {frame:?}");
}
assert!(close(cross(frame.x, frame.y), frame.z), "axis {axis}");
}
let near = placement(MM, "1.,2.E-8,0.", "$").unwrap();
assert!(close(near.x, [2e-8, -1.0, 0.0]), "{:?}", near.x);
}
#[test]
fn ap203_edition_1_minus_x_reads_the_later_default() {
let graph = graph_in(
"CONFIG_CONTROL_DESIGN",
"#200=DIRECTION('',(-1.,0.,0.));#201=AXIS2_PLACEMENT_3D('',#100,#200,$);\
#202=PLANE('',#201);",
);
let reader = Reader::for_representation(graph, MM).unwrap();
let Surface::Plane(plane) = reader.surface(202).unwrap() else {
panic!("a plane");
};
let frame = plane.position;
assert_eq!(
(frame.x, frame.y, frame.z),
([0.0, 1.0, 0.0], [0.0, 0.0, -1.0], [-1.0, 0.0, 0.0])
);
assert!(frame.axis_stated);
assert!(!frame.ref_direction_stated, "x is the schema's default");
}
#[test]
fn a_stated_ref_direction_is_projected_off_the_axis() {
let frame = placement(MM, "0.,0.,1.", "1.,0.,3.3E-6").unwrap();
assert_eq!(frame.x, [1.0, 0.0, 0.0]);
assert_eq!(frame.y, [0.0, 1.0, 0.0]);
assert!(frame.ref_direction_stated);
for (axis, reference) in [
("0.,0.,1.", "1.,0.,0."),
("0.,1.,0.", "0.,0.,1."),
("1.,0.,0.", "0.,-1.,0."),
("0.,0.,-1.", "0.,1.,0."),
] {
let frame = placement(MM, axis, reference).unwrap();
assert_eq!(
cross(frame.x, frame.y),
frame.z,
"x × y = z exactly for axis {axis}, ref {reference}"
);
}
let skew = placement(MM, "1.,1.,1.", "1.,0.,0.").unwrap();
for (a, b) in [(skew.x, skew.y), (skew.y, skew.z), (skew.z, skew.x)] {
assert!(dot(a, b).abs() <= 1e-15);
}
assert!(close(cross(skew.x, skew.y), skew.z));
}
#[test]
fn a_ref_direction_parallel_to_the_axis_is_refused() {
for reference in ["0.,0.,2.", "0.,0.,-1.", "5.E-7,0.,1."] {
let error = placement(MM, "0.,0.,1.", reference).unwrap_err();
assert_eq!(
(error.instance, error.attribute, error.kind),
(200, Some("ref_direction"), GeometryErrorKind::Parallel),
"{reference}"
);
}
assert_eq!(
placement(MM, "0.,0.,1.", "2.E-6,0.,1.").map(|frame| frame.x),
Ok([1.0, 0.0, 0.0]),
"a sine above 1e-6 is read"
);
}
#[test]
fn each_surface_is_read_in_the_context_units() {
let data = "#200=PLANE('',#104);#201=CYLINDRICAL_SURFACE('',#104,0.25);\
#202=CONICAL_SURFACE('',#104,0.25,0.5);#203=SPHERICAL_SURFACE('',#104,0.25);\
#204=TOROIDAL_SURFACE('',#104,2.,0.25);#205=CONICAL_SURFACE('',#104,0.25,59.);\
#206=CONICAL_SURFACE('',#104,0.,1.);";
let frame = reader(MM, data).axis2_placement_3d(104).unwrap();
assert_eq!(
surface(MM, data, 200),
Surface::Plane(Plane {
instance: 200,
position: frame.clone()
})
);
let Surface::Cylindrical(cylinder) = surface(INCH, data, 201) else {
panic!("a cylinder");
};
assert_eq!((cylinder.instance, cylinder.radius), (201, 6.35));
assert_eq!(cylinder.position, frame);
let Surface::Conical(cone) = surface(MM, data, 202) else {
panic!("a cone");
};
assert_eq!((cone.radius, cone.semi_angle), (0.25, 0.5));
let Surface::Spherical(sphere) = surface(INCH, data, 203) else {
panic!("a sphere");
};
assert_eq!(sphere.radius, 6.35);
let Surface::Toroidal(torus) = surface(INCH, data, 204) else {
panic!("a torus");
};
assert_eq!((torus.major_radius, torus.minor_radius), (2.0 * 25.4, 6.35));
let Surface::Conical(drill) = surface(INCH, data, 205) else {
panic!("a cone");
};
assert_eq!(
drill.semi_angle.to_bits(),
(59.0 * 0.017_453_292_5f64).to_bits()
);
assert_eq!(drill.radius, 6.35);
let Surface::Conical(small) = surface(INCH, data, 206) else {
panic!("a cone");
};
assert_eq!(small.semi_angle.to_bits(), 0.017_453_292_5f64.to_bits());
assert_eq!(small.radius, 0.0, "a cone's radius may be zero");
}
#[test]
fn values_out_of_range_are_refused_as_written() {
let data = "#200=CYLINDRICAL_SURFACE('',#104,0.);#201=CYLINDRICAL_SURFACE('',#104,-1.);\
#202=TOROIDAL_SURFACE('',#104,2.,0.);#203=CONICAL_SURFACE('',#104,-1.,0.5);\
#204=CONICAL_SURFACE('',#104,1.,0.);#205=CONICAL_SURFACE('',#104,1.,90.);\
#206=CONICAL_SURFACE('',#104,1.,1.5709);#207=CONICAL_SURFACE('',#104,1.,-10.);\
#208=SPHERICAL_SURFACE('',#104,-2.);#209=CIRCLE('',#104,0.);\
#210=CONICAL_SURFACE('',#104,1.,89.9999);";
let refused = |representation, id, attribute, value: f64| {
let error = reader(representation, data).surface(id).unwrap_err();
assert_eq!(
(error.instance, error.attribute, error.kind),
(id, Some(attribute), GeometryErrorKind::OutOfRange { value }),
"#{id}"
);
};
refused(MM, 200, "radius", 0.0);
refused(MM, 201, "radius", -1.0);
refused(MM, 202, "minor_radius", 0.0);
refused(MM, 203, "radius", -1.0);
refused(MM, 204, "semi_angle", 0.0);
refused(MM, 206, "semi_angle", 1.5709);
refused(MM, 207, "semi_angle", -10.0);
refused(MM, 208, "radius", -2.0);
refused(INCH, 205, "semi_angle", 90.0);
refused(MM_DEGREE, 205, "semi_angle", 90.0);
assert!(
matches!(surface(MM_DEGREE, data, 210), Surface::Conical(_)),
"89.9999 degrees is π/2 − 1.7e-6"
);
assert_eq!(
kind(reader(MM, data).curve(209)),
GeometryErrorKind::OutOfRange { value: 0.0 }
);
}
#[test]
fn lengths_that_leave_the_range_once_converted_are_refused() {
let data = "#40=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.ATTO.,.METRE.));\
#41=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#40,#2))\
REPRESENTATION_CONTEXT('',''));#42=SHAPE_REPRESENTATION('',(),#41);\
#43=LENGTH_MEASURE_WITH_UNIT(LENGTH_MEASURE(1.E-300),#1);\
#44=(CONVERSION_BASED_UNIT('TINY',#43)LENGTH_UNIT()NAMED_UNIT(#3));\
#45=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#44,#2))\
REPRESENTATION_CONTEXT('',''));#46=SHAPE_REPRESENTATION('',(),#45);\
#200=CYLINDRICAL_SURFACE('',#104,1.E-310);#201=SPHERICAL_SURFACE('',#104,1.E-310);\
#202=TOROIDAL_SURFACE('',#104,1.,1.E-310);#203=CIRCLE('',#104,1.E-310);\
#204=CONICAL_SURFACE('',#104,1.E-310,0.5);#205=CYLINDRICAL_SURFACE('',#104,1.E-9);\
#210=CYLINDRICAL_SURFACE('',#104,1.E-30);\
#211=TOROIDAL_SURFACE('',#104,2.E-30,1.E-30);#212=CIRCLE('',#104,1.E-30);\
#220=CYLINDRICAL_SURFACE('',#104,1.E307);#221=CARTESIAN_POINT('',(0.,1.E307,0.));";
let refusal = |error: GeometryError| (error.instance, error.attribute, error.kind);
let out = |id, attribute, value| (id, Some(attribute), out_of_range(value));
let atto = reader(42, data);
for (id, attribute) in [(200, "radius"), (201, "radius"), (202, "minor_radius")] {
assert_eq!(
refusal(atto.surface(id).unwrap_err()),
out(id, attribute, 1e-310),
"#{id}"
);
}
assert_eq!(
refusal(atto.curve(203).unwrap_err()),
out(203, "radius", 1e-310)
);
let Surface::Conical(cone) = atto.surface(204).unwrap() else {
panic!("a cone");
};
assert_eq!(cone.radius, 0.0, "a cone's radius may be zero");
let Surface::Cylindrical(small) = atto.surface(205).unwrap() else {
panic!("a cylinder");
};
assert!(small.radius > 0.0, "1e-9 am is {} mm", small.radius);
let tiny = reader(46, data);
assert_eq!(
refusal(tiny.surface(210).unwrap_err()),
out(210, "radius", 1e-30)
);
assert_eq!(
refusal(tiny.surface(211).unwrap_err()),
out(211, "major_radius", 2e-30)
);
assert_eq!(
refusal(tiny.curve(212).unwrap_err()),
out(212, "radius", 1e-30)
);
let inch = reader(INCH, data);
assert_eq!(
refusal(inch.surface(220).unwrap_err()),
(220, Some("radius"), GeometryErrorKind::NotFinite)
);
assert_eq!(
refusal(inch.point(221).unwrap_err()),
(221, Some("coordinates"), GeometryErrorKind::NotFinite)
);
let Surface::Cylindrical(large) = surface(MM, data, 220) else {
panic!("a cylinder");
};
assert_eq!(large.radius, 1e307, "1e307 mm is finite");
}
#[test]
fn a_unit_is_needed_only_by_what_uses_it() {
let data = "#200=CARTESIAN_POINT('',(1.,0.,0.));#201=CYLINDRICAL_SURFACE('',#104,1.);\
#202=CONICAL_SURFACE('',#104,1.,0.5);#203=CONICAL_SURFACE('',#104,1.,0.);";
let no_length = reader(NO_LENGTH, data);
assert_eq!(
no_length.point(100),
Ok([0.0; 3]),
"the origin needs no unit"
);
let error = no_length.point(200).unwrap_err();
assert_eq!(
(error.instance, error.attribute, error.kind),
(
200,
Some("coordinates"),
GeometryErrorKind::Unit(UnitError::Missing {
context: 24,
kind: UnitKind::Length
})
)
);
assert!(matches!(
kind(no_length.surface(201)),
GeometryErrorKind::Unit(UnitError::Missing { .. })
));
let no_angle = reader(NO_ANGLE, data);
assert!(matches!(no_angle.surface(201), Ok(Surface::Cylindrical(_))));
let cone = no_angle.surface(202).unwrap_err();
assert_eq!(
(cone.attribute, cone.kind),
(
Some("semi_angle"),
GeometryErrorKind::Unit(UnitError::Missing {
context: 23,
kind: UnitKind::PlaneAngle
})
)
);
assert_eq!(
kind(no_angle.surface(203)),
GeometryErrorKind::OutOfRange { value: 0.0 },
"a zero angle is out of range in any unit"
);
}
#[test]
fn unread_entities_and_complex_instances_are_other() {
let data = "#200=B_SPLINE_SURFACE_WITH_KNOTS('',1,1,((#100,#100),(#100,#100)),.UNSPECIFIED.,\
.F.,.F.,.F.,(2),(2),(0.,1.),(0.,1.),.UNSPECIFIED.);\
#201=SURFACE_OF_REVOLUTION('',#300,#302);#202=ELLIPSE('',#104,2.,1.);\
#203=TRIMMED_CURVE('',#300,(#100),(#100),.T.,.CARTESIAN.);\
#204=(BOUNDED_SURFACE()B_SPLINE_SURFACE(1,1,((#100,#100),(#100,#100)),.UNSPECIFIED.,.F.,.F.,.F.)\
B_SPLINE_SURFACE_WITH_KNOTS((2),(2),(0.,1.),(0.,1.),.UNSPECIFIED.)GEOMETRIC_REPRESENTATION_ITEM()\
RATIONAL_B_SPLINE_SURFACE(((1.,1.),(1.,1.)))REPRESENTATION_ITEM('')SURFACE());\
#205=(CYLINDRICAL_SURFACE('',#104,1.)GEOMETRIC_REPRESENTATION_ITEM());\
#206=DEGENERATE_TOROIDAL_SURFACE('',#104,1.,2.,.T.);\
#300=LINE('',#100,#301);#301=VECTOR('',#101,1.);#302=AXIS1_PLACEMENT('',#100,#103);";
let mm = reader(MM, data);
let other = |entity: &str, id| OtherGeometry {
instance: id,
entity: entity.to_owned(),
};
assert_eq!(
mm.surface(200),
Ok(Surface::Other(other("B_SPLINE_SURFACE_WITH_KNOTS", 200)))
);
assert_eq!(
mm.surface(201),
Ok(Surface::Other(other("SURFACE_OF_REVOLUTION", 201)))
);
assert_eq!(mm.curve(202), Ok(Curve::Other(other("ELLIPSE", 202))));
assert_eq!(mm.curve(203), Ok(Curve::Other(other("TRIMMED_CURVE", 203))));
assert_eq!(
mm.surface(204),
Ok(Surface::Other(other(
"BOUNDED_SURFACE+B_SPLINE_SURFACE+B_SPLINE_SURFACE_WITH_KNOTS+\
GEOMETRIC_REPRESENTATION_ITEM+RATIONAL_B_SPLINE_SURFACE+REPRESENTATION_ITEM+SURFACE",
204
)))
);
assert_eq!(
mm.surface(205),
Ok(Surface::Other(other(
"CYLINDRICAL_SURFACE+GEOMETRIC_REPRESENTATION_ITEM",
205
))),
"a complex instance is not read, even around an elementary record"
);
assert_eq!(
mm.surface(206),
Ok(Surface::Other(other("DEGENERATE_TOROIDAL_SURFACE", 206)))
);
assert_eq!(mm.surface(205).unwrap().instance(), 205);
}
#[test]
fn lines_and_circles() {
let data = "#200=CARTESIAN_POINT('',(1.,0.,0.));#201=VECTOR('',#102,3.);\
#202=LINE('',#200,#201);#203=CIRCLE('',#104,0.5);\
#204=AXIS2_PLACEMENT_2D('',#205,$);#205=CARTESIAN_POINT('',(0.,0.));\
#206=CIRCLE('',#204,1.);";
let inch = reader(INCH, data);
let Curve::Line(line) = inch.curve(202).unwrap() else {
panic!("a line");
};
assert_eq!(line.pnt, [25.4, 0.0, 0.0]);
assert_eq!(
(line.dir.instance, line.dir.orientation, line.dir.magnitude),
(201, [0.0, 1.0, 0.0], 3.0 * 25.4)
);
let Curve::Circle(circle) = inch.curve(203).unwrap() else {
panic!("a circle");
};
assert_eq!((circle.instance, circle.radius), (203, 0.5 * 25.4));
assert_eq!(circle.position.instance, 104);
let flat = inch.curve(206).unwrap_err();
assert_eq!(
(flat.instance, flat.entity.as_str(), flat.kind),
(
204,
"AXIS2_PLACEMENT_2D",
GeometryErrorKind::Dimension { found: 2 }
),
"a circle in a 2D placement is not read"
);
}
#[test]
fn surface_curves_are_unwrapped_one_level() {
let data = "#200=LINE('',#100,#201);#201=VECTOR('',#101,1.);#202=CIRCLE('',#104,2.);\
#203=B_SPLINE_CURVE_WITH_KNOTS('',1,(#100,#100),.UNSPECIFIED.,.F.,.F.,(2),(0.,1.),.UNSPECIFIED.);\
#210=SURFACE_CURVE('',#200,(#300),.PCURVE_S2.);\
#211=SEAM_CURVE('',#202,(#300,#300),.PCURVE_S1.);\
#212=INTERSECTION_CURVE('',#203,(#300,#300),.CURVE_3D.);\
#213=SURFACE_CURVE('',#211,(#300),.CURVE_3D.);\
#214=SURFACE_CURVE('',#200,(#300),.FOO.);\
#215=surface_curve('',#202,(#300),.curve_3d.);\
#216=SURFACE_CURVE('',#200,(#300),$);\
#217=SURFACE_CURVE('',#200,#300,.CURVE_3D.);\
#300=PLANE('',#104);";
let mm = reader(MM, data);
let unwrap = |id| match mm.curve(id).unwrap() {
Curve::SurfaceCurve(curve) => curve,
other => panic!("#{id}: {other:?}"),
};
let surface = unwrap(210);
assert_eq!(
(surface.subtype, surface.master_representation),
(
SurfaceCurveKind::SurfaceCurve,
MasterRepresentation::PcurveS2
)
);
assert!(matches!(*surface.curve_3d, Curve::Line(_)));
let seam = unwrap(211);
assert_eq!(
(seam.subtype, seam.master_representation),
(SurfaceCurveKind::SeamCurve, MasterRepresentation::PcurveS1)
);
assert!(matches!(*seam.curve_3d, Curve::Circle(_)));
let intersection = unwrap(212);
assert_eq!(
(intersection.subtype, intersection.master_representation),
(
SurfaceCurveKind::IntersectionCurve,
MasterRepresentation::Curve3d
)
);
assert_eq!(
*intersection.curve_3d,
Curve::Other(OtherGeometry {
instance: 203,
entity: "B_SPLINE_CURVE_WITH_KNOTS".to_owned()
})
);
assert_eq!(
*unwrap(213).curve_3d,
Curve::Other(OtherGeometry {
instance: 211,
entity: "SEAM_CURVE".to_owned()
}),
"a surface curve inside a surface curve is not read"
);
let bad = mm.curve(214).unwrap_err();
assert_eq!(
(bad.instance, bad.attribute, bad.kind),
(
214,
Some("master_representation"),
GeometryErrorKind::UnknownEnumeration
)
);
assert_eq!(
unwrap(215).master_representation,
MasterRepresentation::Curve3d,
"names and enumerations in lower case"
);
assert_eq!(kind(mm.curve(216)), GeometryErrorKind::MissingAttribute);
assert_eq!(kind(mm.curve(217)), GeometryErrorKind::NotAList);
let seam = mm.curve(211).unwrap();
assert_eq!(seam.basis().instance(), 202);
let line = mm.curve(200).unwrap();
assert_eq!(line.basis(), &line);
}
#[test]
fn errors_name_the_innermost_instance() {
let data = "#200=DIRECTION('',(0.,0.,0.));#201=AXIS2_PLACEMENT_3D('',#100,#200,$);\
#202=CYLINDRICAL_SURFACE('',#201,1.);#203=CYLINDRICAL_SURFACE('',#998,1.);\
#204=AXIS2_PLACEMENT_3D('',#100,#103,#999);#205=PLANE('',#204);";
let mm = reader(MM, data);
let error = mm.surface(202).unwrap_err();
assert_eq!(
error,
GeometryError {
instance: 200,
entity: "DIRECTION".to_owned(),
attribute: Some("direction_ratios"),
kind: GeometryErrorKind::ZeroDirection,
}
);
assert_eq!(
mm.surface(203).unwrap_err(),
GeometryError {
instance: 203,
entity: "CYLINDRICAL_SURFACE".to_owned(),
attribute: Some("position"),
kind: GeometryErrorKind::Missing,
}
);
let deep = mm.surface(205).unwrap_err();
assert_eq!(
(deep.instance, deep.attribute, deep.kind),
(204, Some("ref_direction"), GeometryErrorKind::Missing)
);
assert_eq!(
mm.surface(997).unwrap_err(),
GeometryError {
instance: 997,
entity: String::new(),
attribute: None,
kind: GeometryErrorKind::Missing,
}
);
}
#[test]
fn malformed_records_are_refused() {
let data = "#200=CYLINDRICAL_SURFACE('',#104,$);\
#201=CYLINDRICAL_SURFACE('',#104,LENGTH_MEASURE(5.));\
#202=CYLINDRICAL_SURFACE('',#104,'5');#203=CYLINDRICAL_SURFACE('',#104);\
#204=CYLINDRICAL_SURFACE('',#104,5.,7.);#205=CYLINDRICAL_SURFACE('',#104,5);\
#206=cylindrical_surface('',#104,5.);#207=CYLINDRICAL_SURFACE('','x',5.);\
#208=CARTESIAN_POINT('',(1.,2.));#209=(CARTESIAN_POINT('',(1.,2.,3.))\
GEOMETRIC_REPRESENTATION_ITEM());#210=PLANE('',#104,#104);\
#211=CARTESIAN_POINT('',$);#212=CARTESIAN_POINT('',(1.,'2',3.));\
#213=AXIS2_PLACEMENT_3D('',$,$,$);#214=CYLINDRICAL_SURFACE('',#104,1.E999);";
let mm = reader(MM, data);
let refusal = |id| {
let error = mm.surface(id).unwrap_err();
(error.instance, error.attribute, error.kind)
};
assert_eq!(
refusal(200),
(200, Some("radius"), GeometryErrorKind::MissingAttribute)
);
assert_eq!(
refusal(201),
(201, Some("radius"), GeometryErrorKind::NotANumber)
);
assert_eq!(
refusal(202),
(202, Some("radius"), GeometryErrorKind::NotANumber)
);
assert_eq!(
refusal(203),
(
203,
None,
GeometryErrorKind::ParameterCount {
expected: 3,
found: 2
}
)
);
assert_eq!(
refusal(204),
(
204,
None,
GeometryErrorKind::ParameterCount {
expected: 3,
found: 4
}
),
"one parameter too many"
);
assert!(
matches!(mm.surface(205), Ok(Surface::Cylindrical(c)) if c.radius.to_bits() == 5.0f64.to_bits())
);
assert!(matches!(mm.surface(206), Ok(Surface::Cylindrical(_))));
assert_eq!(
refusal(207),
(207, Some("position"), GeometryErrorKind::NotAReference)
);
assert_eq!(
refusal(210),
(
210,
None,
GeometryErrorKind::ParameterCount {
expected: 2,
found: 3
}
)
);
assert_eq!(
refusal(214),
(214, Some("radius"), GeometryErrorKind::NotFinite)
);
assert_eq!(
kind(mm.point(208)),
GeometryErrorKind::Dimension { found: 2 }
);
assert_eq!(kind(mm.point(209)), GeometryErrorKind::Complex);
assert_eq!(kind(mm.point(211)), GeometryErrorKind::MissingAttribute);
assert_eq!(kind(mm.point(212)), GeometryErrorKind::NotANumber);
assert_eq!(
kind(mm.point(103)),
GeometryErrorKind::WrongType {
expected: "CARTESIAN_POINT"
}
);
assert_eq!(
kind(mm.axis2_placement_3d(213)),
GeometryErrorKind::MissingAttribute,
"location is required"
);
assert_eq!(
kind(mm.vector(103)),
GeometryErrorKind::WrongType { expected: "VECTOR" }
);
}
#[test]
fn a_reader_for_an_item_takes_the_units_of_its_holders() {
let data = "#200=CYLINDRICAL_SURFACE('',#104,0.5);#201=ADVANCED_FACE('',(),#200,.T.);\
#202=CLOSED_SHELL('',(#201));#203=MANIFOLD_SOLID_BREP('',#202);\
#204=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#203),#21);";
let graph = graph_in("AP214", data);
let reader = Reader::for_item(graph, 201).unwrap();
assert_eq!(reader.units().length().unwrap().factor, 25.4);
assert!(
matches!(reader.surface(200), Ok(Surface::Cylindrical(c)) if c.radius.to_bits() == (0.5 * 25.4f64).to_bits())
);
assert_eq!(
Reader::for_item(graph, 200).unwrap_err(),
UnitError::NotHeld { item: 200 }
);
assert!(Reader::for_representation(graph, 203).is_err());
}
#[test]
fn rigid_transforms_compose_child_to_parent() {
let turn = Rigid {
rotation: [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
translation: [10.0, 0.0, 0.0],
};
let shift = Rigid {
translation: [0.0, 0.0, 5.0],
..Rigid::IDENTITY
};
assert!(Rigid::IDENTITY.is_identity());
assert!(!turn.is_identity());
assert!(
Rigid {
translation: [-0.0, 0.0, 0.0],
..Rigid::IDENTITY
}
.is_identity()
);
assert_eq!(turn.apply_point([1.0, 0.0, 0.0]), [10.0, 1.0, 0.0]);
assert_eq!(turn.apply_vector([1.0, 0.0, 0.0]), [0.0, 1.0, 0.0]);
let point = [1.0, 2.0, 3.0];
assert_eq!(
(turn * shift).apply_point(point),
turn.apply_point(shift.apply_point(point))
);
assert_eq!((turn * shift).translation, [10.0, 0.0, 5.0]);
assert_eq!((shift * turn).translation, [10.0, 0.0, 5.0]);
assert_ne!((turn * turn).translation, (turn * shift).translation);
assert!((turn.inverse() * turn).is_identity());
assert!((turn * turn.inverse()).is_identity());
assert_eq!(
turn.inverse().apply_point([10.0, 1.0, 0.0]),
[1.0, 0.0, 0.0]
);
assert_eq!(Rigid::IDENTITY * turn, turn);
}
#[test]
fn a_placement_maps_its_frame_into_the_enclosing_one() {
let data = "#200=CARTESIAN_POINT('',(1.,2.,3.));\
#201=AXIS2_PLACEMENT_3D('',#200,#101,#102);";
let frame = reader(INCH, data).axis2_placement_3d(201).unwrap();
assert_eq!(
(frame.x, frame.y, frame.z),
([0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0])
);
let rigid = frame.to_rigid();
assert_eq!(rigid.apply_vector([1.0, 0.0, 0.0]), frame.x);
assert_eq!(rigid.apply_vector([0.0, 0.0, 1.0]), frame.z);
assert_eq!(rigid.apply_point([0.0, 0.0, 0.0]), frame.location);
assert_eq!(frame.location, [25.4, 2.0 * 25.4, 3.0 * 25.4]);
}
#[test]
fn errors_format_and_wrap() {
let mm = reader(MM, "#200=CYLINDRICAL_SURFACE('',#104,-1.);");
let error = mm.surface(200).unwrap_err();
assert_eq!(
error.to_string(),
"#200 (CYLINDRICAL_SURFACE) radius: -1 is out of range"
);
assert_eq!(
crate::Error::from(error.clone()).to_string(),
error.to_string()
);
assert_eq!(
mm.surface(999).unwrap_err().to_string(),
"#999: not defined"
);
let unit = reader(NO_LENGTH, "#200=CARTESIAN_POINT('',(1.,0.,0.));")
.point(200)
.unwrap_err();
assert_eq!(
unit.to_string(),
"#200 (CARTESIAN_POINT) coordinates: its unit cannot be read: \
context #24 assigns no length unit"
);
}
#[cfg(feature = "serde")]
mod serialized {
use super::*;
use crate::testing::unique;
#[test]
fn every_result_type_has_unique_keys() {
let data = "#200=PLANE('',#104);#201=CYLINDRICAL_SURFACE('',#104,1.);\
#202=CONICAL_SURFACE('',#104,1.,0.5);#203=SPHERICAL_SURFACE('',#104,1.);\
#204=TOROIDAL_SURFACE('',#104,2.,1.);#205=ELLIPSE('',#104,2.,1.);\
#206=LINE('',#100,#207);#207=VECTOR('',#101,1.);#208=CIRCLE('',#104,1.);\
#209=SEAM_CURVE('',#208,(#200,#200),.PCURVE_S1.);\
#210=AXIS1_PLACEMENT('',#100,$);";
let mm = reader(MM, data);
for id in 200..=205 {
let json = unique(&mm.surface(id).unwrap());
assert!(json.starts_with("{\"kind\":"), "{json}");
}
for id in [205, 206, 208, 209] {
let json = unique(&mm.curve(id).unwrap());
assert!(json.starts_with("{\"kind\":"), "{json}");
}
let seam = unique(&mm.curve(209).unwrap());
assert!(
seam.starts_with(
r#"{"kind":"surface_curve","instance":209,"subtype":"seam_curve","curve_3d":{"kind":"circle""#
),
"{seam}"
);
assert!(
seam.ends_with(r#""master_representation":"pcurve_s1"}"#),
"{seam}"
);
unique(&mm.vector(207).unwrap());
unique(&mm.axis1_placement(210).unwrap());
unique(&mm.axis2_placement_3d(104).unwrap().to_rigid());
}
#[test]
fn errors_have_unique_keys() {
let kinds = [
GeometryErrorKind::Missing,
GeometryErrorKind::WrongType { expected: "X" },
GeometryErrorKind::Complex,
GeometryErrorKind::ParameterCount {
expected: 2,
found: 3,
},
GeometryErrorKind::MissingAttribute,
GeometryErrorKind::NotANumber,
GeometryErrorKind::NotAReference,
GeometryErrorKind::NotAList,
GeometryErrorKind::NotFinite,
GeometryErrorKind::Dimension { found: 2 },
GeometryErrorKind::ZeroDirection,
GeometryErrorKind::Parallel,
GeometryErrorKind::OutOfRange { value: -1.0 },
GeometryErrorKind::UnknownEnumeration,
GeometryErrorKind::Unit(UnitError::Missing {
context: 4,
kind: UnitKind::Length,
}),
];
for kind in kinds {
let error = GeometryError {
instance: 1,
entity: "X".to_owned(),
attribute: Some("radius"),
kind,
};
unique(&error);
assert!(unique(&error.kind).starts_with("{\"kind\":"));
}
assert_eq!(
unique(&GeometryErrorKind::Unit(UnitError::Missing {
context: 4,
kind: UnitKind::Length,
})),
r#"{"kind":"unit","error":{"kind":"missing","context":4,"unit_kind":"length"}}"#
);
}
}
}