Skip to main content

ifc_geometry/solid/swept/
trim.rs

1//! The trim of a directrix-driven sweep, and which kind of measure it is.
2//!
3//! `StartParam`/`EndParam` of `IfcSurfaceCurveSweptAreaSolid` and
4//! `IfcFixedReferenceSweptAreaSolid` change declaration between releases:
5//!
6//! ```text
7//! IFC2X3  IfcParameterValue
8//! IFC4    OPTIONAL IfcParameterValue
9//! IFC4X3  OPTIONAL IfcCurveMeasureSelect
10//!         = SELECT (IfcLengthMeasure, IfcParameterValue)
11//! ```
12//!
13//! In IFC4X3 the file says, through the SELECT member it writes, whether the
14//! trim is a curve parameter or a distance along the directrix. The two are
15//! different quantities: a parameter lives in the directrix's own
16//! parameterisation (an angle on a conic, a vector multiple on a line), a
17//! length in the model's length unit. Merging them reads a 5 m trim on a
18//! circle as five radians. [`TrimMeasure`] keeps them apart (#210).
19
20use ifc_model::Value;
21
22use crate::error::GeometryResult;
23use crate::slots::Slots;
24
25/// One end of a directrix sweep's trim, as the file states it.
26///
27/// Values are in the file's units, as everywhere in [`crate::solid`];
28/// converting a [`Self::Length`] is lowering's job.
29#[derive(Debug, Clone, Copy, PartialEq)]
30#[non_exhaustive]
31pub enum TrimMeasure {
32    /// An `IfcParameterValue` in the directrix's own parameter space: a bare
33    /// value in IFC2X3 and IFC4, `IFCPARAMETERVALUE(..)` in IFC4X3.
34    Parameter(f64),
35    /// An `IfcLengthMeasure`, a distance along the directrix in the file's
36    /// length unit: `IFCLENGTHMEASURE(..)`, IFC4X3 only.
37    Length(f64),
38}
39
40impl TrimMeasure {
41    /// The parameter value, or `None` for a length.
42    pub fn parameter(self) -> Option<f64> {
43        match self {
44            Self::Parameter(value) => Some(value),
45            Self::Length(_) => None,
46        }
47    }
48}
49
50/// Read an optional trim slot.
51///
52/// `$` is `None`. A bare number is the `IfcParameterValue` of IFC2X3 and
53/// IFC4. A typed value must be one of the two `IfcCurveMeasureSelect`
54/// members; any other wrapper is refused rather than guessed at.
55pub(super) fn read_trim(
56    slots: &Slots<'_>,
57    index: usize,
58    attribute: &'static str,
59) -> GeometryResult<Option<TrimMeasure>> {
60    let Some(value) = slots.opt(index) else {
61        return Ok(None);
62    };
63    const EXPECTED: &str = "an IfcParameterValue or IFCLENGTHMEASURE(..)";
64    let kind_error = || slots.kind_error(attribute, EXPECTED, value);
65    let measure = match value {
66        Value::Typed {
67            type_name,
68            value: inner,
69        } => {
70            let number = inner.as_f64().ok_or_else(kind_error)?;
71            if type_name.eq_ignore_ascii_case("IFCPARAMETERVALUE") {
72                TrimMeasure::Parameter(number)
73            } else if type_name.eq_ignore_ascii_case("IFCLENGTHMEASURE") {
74                TrimMeasure::Length(number)
75            } else {
76                return Err(kind_error());
77            }
78        }
79        other => TrimMeasure::Parameter(other.as_f64().ok_or_else(kind_error)?),
80    };
81    Ok(Some(measure))
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87    use crate::solid::testkit::entity;
88    use ifc_model::EntityId;
89
90    fn typed(name: &str, value: f64) -> Value {
91        Value::Typed {
92            type_name: name.into(),
93            value: Box::new(Value::Real(value)),
94        }
95    }
96
97    fn read(value: Value) -> GeometryResult<Option<TrimMeasure>> {
98        let e = entity("IFCSURFACECURVESWEPTAREASOLID", vec![value]);
99        read_trim(&Slots::new(EntityId(1), &e), 0, "StartParam")
100    }
101
102    #[test]
103    fn the_select_member_decides_the_kind() {
104        assert_eq!(read(Value::Null).unwrap(), None);
105        assert_eq!(
106            read(Value::Real(0.5)).unwrap(),
107            Some(TrimMeasure::Parameter(0.5))
108        );
109        assert_eq!(
110            read(Value::Integer(2)).unwrap(),
111            Some(TrimMeasure::Parameter(2.0))
112        );
113        assert_eq!(
114            read(typed("IFCPARAMETERVALUE", 0.5)).unwrap(),
115            Some(TrimMeasure::Parameter(0.5))
116        );
117        assert_eq!(
118            read(typed("IfcLengthMeasure", 5.0)).unwrap(),
119            Some(TrimMeasure::Length(5.0))
120        );
121    }
122
123    #[test]
124    fn a_wrapper_outside_the_select_is_refused() {
125        for value in [
126            typed("IFCNONNEGATIVELENGTHMEASURE", 1.0),
127            typed("IFCREAL", 1.0),
128            Value::Text("1".into()),
129        ] {
130            assert!(read(value).is_err());
131        }
132    }
133}