ifc_geometry/surface/basis.rs
1//! `IfcGetBasisSurface`: the surfaces a curve-on-surface lies on.
2//!
3//! # A curve-on-surface reaches its surface three different ways
4//!
5//! - an `IfcPcurve` names its `BasisSurface` directly;
6//! - an `IfcSurfaceCurve` collects one surface per associated p-curve
7//! (this is `IfcAssociatedSurface`, which is just that field read);
8//! - an `IfcCompositeCurveOnSurface` intersects what its segments carry.
9//!
10//! # A schema erratum in the composite branch
11//!
12//! The normative body loops `Segments[1]` rather than `Segments[i]`:
13//!
14//! ```text
15//! Surfs := IfcGetBasisSurface(C\IfcCompositeCurve.Segments[1].ParentCurve);
16//! IF N > 1 THEN
17//! REPEAT i := 2 TO N;
18//! Surfs := Surfs * IfcGetBasisSurface(C\IfcCompositeCurve.Segments[1].ParentCurve);
19//! END_REPEAT;
20//! END_IF;
21//! ```
22//!
23//! Intersecting segment one with itself is the identity, so read literally
24//! the loop can never narrow the set and the rule it feeds (`SameSurface`)
25//! becomes unfalsifiable. The prose directly above the loop states the
26//! intent: "the BasisSurface is the intersection of the BasisSurface of all
27//! the segments". This module implements that stated intent, so
28//! `SameSurface` can actually fail on a composite curve whose segments sit
29//! on different surfaces.
30
31use std::collections::BTreeSet;
32
33use ifc_model::{Entity, EntityId, Model, Value};
34
35/// Maximum nesting depth, matching the resolver in `rules::dimension`.
36const MAX_DEPTH: usize = 16;
37
38/// The surfaces `curve` lies on, per `IfcGetBasisSurface`.
39///
40/// Returns an empty set when the curve is not a curve-on-surface, when the
41/// file omits the surface, or when nesting exceeds the depth cap. The schema
42/// types the result `SET[0:2]`, so emptiness is a legal answer, not an error.
43pub fn basis_surfaces(model: &Model, curve: EntityId) -> BTreeSet<EntityId> {
44 basis_with_depth(model, curve, 0)
45}
46
47fn basis_with_depth(model: &Model, curve: EntityId, depth: usize) -> BTreeSet<EntityId> {
48 let mut out = BTreeSet::new();
49 if depth > MAX_DEPTH {
50 return out;
51 }
52 let Some(entity) = model.get(curve) else {
53 return out;
54 };
55 let name = entity.type_name.to_ascii_uppercase();
56
57 // An IfcPcurve names its surface in slot 0. IfcAssociatedSurface is
58 // exactly this read, which is why it needs no separate function.
59 if name == "IFCPCURVE" {
60 if let Some(surface) = slot_ref(entity, 0) {
61 out.insert(surface);
62 }
63 return out;
64 }
65
66 // Checked before the surface-curve branch: the two are disjoint, and a
67 // composite curve on surface is an IfcCompositeCurve, not a surface curve.
68 if crate::select::is_a(&name, "IFCCOMPOSITECURVEONSURFACE") {
69 return composite_intersection(model, entity, depth);
70 }
71
72 if crate::select::is_a(&name, "IFCSURFACECURVE") {
73 // AssociatedGeometry is slot 1: Curve3D, AssociatedGeometry, ...
74 for pcurve in list_refs(entity, 1) {
75 out.extend(basis_with_depth(model, pcurve, depth + 1));
76 }
77 }
78 out
79}
80
81/// The intersection of every segment's basis surfaces.
82///
83/// Implements the documented intent rather than the errant `Segments[1]`
84/// index; see the module header.
85fn composite_intersection(model: &Model, entity: &Entity, depth: usize) -> BTreeSet<EntityId> {
86 let mut acc: Option<BTreeSet<EntityId>> = None;
87 // Segments is slot 0 on IfcCompositeCurve; ParentCurve is slot 2 on
88 // IfcCompositeCurveSegment (Transition, SameSense, ParentCurve) and slot
89 // 4 on IFC4X3 ADD2's IfcCurveSegment (Transition, Placement,
90 // SegmentStart, SegmentLength, ParentCurve), whose IfcGetBasisSurface
91 // reads `Segments[i]\IfcCurveSegment.ParentCurve`. Reading slot 2 of a
92 // curve segment would find no curve and empty the intersection.
93 for segment in list_refs(entity, 0) {
94 let parent = model.get(segment).and_then(|s| {
95 let kind = s.type_name.to_ascii_uppercase();
96 if crate::select::is_a(&kind, "IFCCURVESEGMENT") {
97 slot_ref(s, 4)
98 } else {
99 slot_ref(s, 2)
100 }
101 });
102 let Some(parent) = parent else {
103 continue;
104 };
105 let surfaces = basis_with_depth(model, parent, depth + 1);
106 acc = Some(match acc {
107 None => surfaces,
108 Some(prev) => prev.intersection(&surfaces).copied().collect(),
109 });
110 }
111 acc.unwrap_or_default()
112}
113
114/// The entity referenced at `slot`, unwrapping any defined-type wrapper.
115fn slot_ref(entity: &Entity, slot: usize) -> Option<EntityId> {
116 match entity.attribute(slot).map(|v| v.unwrap_typed()) {
117 Some(Value::Ref(id)) => Some(*id),
118 _ => None,
119 }
120}
121
122/// Every entity reference in the list at `slot`.
123fn list_refs(entity: &Entity, slot: usize) -> Vec<EntityId> {
124 match entity.attribute(slot).map(|v| v.unwrap_typed()) {
125 Some(Value::List(items)) => items
126 .iter()
127 .filter_map(|v| match v.unwrap_typed() {
128 Value::Ref(id) => Some(*id),
129 _ => None,
130 })
131 .collect(),
132 _ => Vec::new(),
133 }
134}