Skip to main content

openbim_ifc_binding_core/geometry/
curves.rs

1//! Polylines per product for the hosts (#458): the curves of a curve
2//! representation (`Axis`, `FootPrint`, an annotation's curves), compiled
3//! by the reference backend. The facade's `product_curves`.
4//!
5//! One product is one record and one `f32` array: every polyline's
6//! vertices back to back, `x, y, z` in metres relative to the product's
7//! world placement, as a mesh's are, so a host draws them with the same
8//! matrix. Each [`ProductPolyline`] names its slice (`first_vertex`,
9//! `vertex_count`), whether it is closed (its last vertex repeats its
10//! first), and the certified bound on how far the exact curve lies from it.
11//! Trimmed, composite, offset and intrinsic curves compile (#506). A
12//! product the backend refuses carries a typed [`GeometryRefusal`]: a
13//! curve the reference compiler has no certified polyline for (an
14//! implicit curve, an offset of an offset) is `unsupported`, naming the
15//! product, its message the compiler's reason. A representation that also
16//! holds text or a fill area (a dimension and its value) gives its curves:
17//! each `IfcTextLiteral`, `IfcTextLiteralWithExtent` and
18//! `IfcAnnotationFillArea` among its items is not drawn and is listed by
19//! id in [`ProductCurves::skipped`] (#512).
20
21use super::GeometryRefusal;
22use crate::record::{Field, Record, ToRecord};
23use crate::{BindingError, IfcModel};
24
25/// One curve of a product, as a slice of its positions.
26#[derive(Debug, Clone, PartialEq)]
27pub struct ProductPolyline {
28    /// The IFC curve it was compiled from, when the lowering names one.
29    pub curve: Option<u64>,
30    /// Its first vertex in the product's positions.
31    pub first_vertex: usize,
32    /// How many vertices it has.
33    pub vertex_count: usize,
34    /// Whether it is closed: its last vertex is its first.
35    pub closed: bool,
36    /// The certified bound, metres, on the distance from every point of
37    /// the exact curve to the polyline: `0` for a polyline the file
38    /// authored, at most `chord_requested` otherwise.
39    pub chord_bound: f64,
40    /// The chord budget, metres: one millimetre.
41    pub chord_requested: f64,
42}
43
44/// One product's curves as polylines.
45#[derive(Debug, Clone, PartialEq)]
46pub struct ProductCurves {
47    /// The product's entity id.
48    pub id: u64,
49    /// Its `GlobalId`.
50    pub global_id: Option<String>,
51    /// Its entity type, upper-case.
52    pub type_name: String,
53    /// The world placement, 4x4 column-major, metres. `None` when refused.
54    pub transform: Option<[f64; 16]>,
55    /// `x, y, z` per vertex, metres, relative to `transform`: every
56    /// polyline's vertices in turn.
57    pub positions: Vec<f32>,
58    /// The curves, in authored order. Empty with no refusal: a product
59    /// without the representation, or one with no curves.
60    pub polylines: Vec<ProductPolyline>,
61    /// Why there are no polylines.
62    pub refusal: Option<GeometryRefusal>,
63    /// The representation's text literals and annotation fill areas, by
64    /// entity id in authored order: not drawn, read through the style
65    /// views (#512).
66    pub skipped: Vec<u64>,
67}
68
69impl IfcModel {
70    /// The curves of each of `products`' `representation` as polylines
71    /// (#458), or, with `None`, of every product that has a shape, in id
72    /// order. `representation` is `Plan` by default in the hosts (the best
73    /// drawing curves), or `Axis`, `FootPrint`, `Annotation`, `Body`,
74    /// `Surface`, `Reference`. Chord budget: one millimetre.
75    ///
76    /// Refused as a whole as [`Self::product_meshes`] is: `unsupported-schema`,
77    /// `feature-disabled` without the `mesh` feature, and `invalid-value`
78    /// for a representation name it does not know.
79    pub fn product_curves(
80        &self,
81        products: Option<&[u64]>,
82        representation: &str,
83    ) -> Result<Vec<ProductCurves>, BindingError> {
84        #[cfg(feature = "mesh")]
85        {
86            curves(self, products, super::purpose(representation)?)
87        }
88        #[cfg(not(feature = "mesh"))]
89        {
90            let _ = (products, representation);
91            Err(BindingError::FeatureDisabled("mesh"))
92        }
93    }
94}
95
96#[cfg(feature = "mesh")]
97fn curves(
98    model: &IfcModel,
99    products: Option<&[u64]>,
100    purpose: ifc::RepresentationPurpose,
101) -> Result<Vec<ProductCurves>, BindingError> {
102    let products = super::ids(products);
103    let mut out = Vec::new();
104    for (product, curves) in ifc::product_curves(&model.inner, products.as_deref(), purpose) {
105        let (global_id, type_name) = super::product_identity(model, product.0)?;
106        let mut record = ProductCurves {
107            id: product.0,
108            global_id,
109            type_name,
110            transform: None,
111            positions: Vec::new(),
112            polylines: Vec::new(),
113            refusal: None,
114            skipped: Vec::new(),
115        };
116        match curves {
117            Ok(curves) => {
118                record.transform = Some(ifc::column_major(&curves.world));
119                record.skipped = curves
120                    .skipped
121                    .iter()
122                    .map(|skipped| skipped.item.0)
123                    .collect();
124                for polyline in curves.polylines {
125                    record.polylines.push(ProductPolyline {
126                        curve: polyline.curve.map(|id| id.0),
127                        first_vertex: record.positions.len() / 3,
128                        vertex_count: polyline.positions.len() / 3,
129                        closed: polyline.closed,
130                        chord_bound: polyline.chord_bound,
131                        chord_requested: polyline.chord_requested,
132                    });
133                    record.positions.extend(polyline.positions);
134                }
135            }
136            Err(error) => record.refusal = Some(super::refusal(&error)),
137        }
138        out.push(record);
139    }
140    Ok(out)
141}
142
143impl ToRecord for ProductPolyline {
144    fn to_record(&self) -> Record {
145        Record::new(
146            "ProductPolyline",
147            vec![
148                ("curve", Field::id(self.curve)),
149                ("first_vertex", Field::Count(self.first_vertex)),
150                ("vertex_count", Field::Count(self.vertex_count)),
151                ("closed", Field::Bool(self.closed)),
152                ("chord_bound", Field::Real(self.chord_bound)),
153                ("chord_requested", Field::Real(self.chord_requested)),
154            ],
155        )
156    }
157}
158
159/// The curves' metadata: the positions cross separately, in each host's
160/// typed-array form, as a mesh's do.
161impl ToRecord for ProductCurves {
162    fn to_record(&self) -> Record {
163        Record::new(
164            "ProductCurves",
165            vec![
166                ("id", Field::Id(self.id)),
167                ("global_id", Field::text(self.global_id.clone())),
168                ("type_name", Field::Text(self.type_name.clone())),
169                ("transform", super::matrix(self.transform.as_ref())),
170                ("vertex_count", Field::Count(self.positions.len() / 3)),
171                ("polylines", Field::records(&self.polylines)),
172                ("refusal", Field::record(self.refusal.as_ref())),
173                ("skipped", Field::ids(self.skipped.iter().copied())),
174            ],
175        )
176    }
177}