ifc_lite_geometry/material_layer_index.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Material Layer Index
6//!
7//! Maps building elements to the material buildup that lets us slice their
8//! single swept-solid mesh into per-layer sub-meshes (e.g. a wall's core,
9//! insulation, and finish showing up as separately coloured slabs).
10//!
11//! The index scans `IfcRelAssociatesMaterial` once per file and resolves each
12//! element to a [`LayerBuildup`] when the associated material is a
13//! [`IfcMaterialLayerSetUsage`] pointing at a plain
14//! [`IfcMaterialLayerSet`]. Other material representations (single
15//! `IfcMaterial`, `IfcMaterialConstituentSet`, `IfcMaterialProfileSet`,
16//! legacy `IfcMaterialList`, or layer sets with per-layer offsets used for
17//! tapered walls) do not map to a set of planar cutting planes and are
18//! recorded as [`LayerBuildup::NotSliceable`] so the caller can fall back
19//! to its existing path.
20
21use ifc_lite_core::{DecodedEntity, EntityDecoder, EntityScanner, IfcType};
22use rustc_hash::FxHashMap;
23
24/// Which local axis the material layers stack along.
25///
26/// Mirrors `IfcLayerSetDirectionEnum` in the spec:
27/// <https://standards.buildingsmart.org/IFC/RELEASE/IFC4_ADD2_TC1/HTML/schema/ifcmaterialresource/lexical/ifclayersetdirectionenum.htm>
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum LayerAxis {
30 /// Along local +X (rare for walls; used when the wall's "layers" run
31 /// along its length, e.g. horizontal segmentation).
32 Axis1,
33 /// Along local +Y — walls (thickness direction).
34 Axis2,
35 /// Along local +Z — slabs, roofs, coverings (through-depth).
36 Axis3,
37}
38
39impl LayerAxis {
40 /// Return the unit vector of this axis in the element's local frame.
41 pub fn unit_vector(self) -> [f64; 3] {
42 match self {
43 LayerAxis::Axis1 => [1.0, 0.0, 0.0],
44 LayerAxis::Axis2 => [0.0, 1.0, 0.0],
45 LayerAxis::Axis3 => [0.0, 0.0, 1.0],
46 }
47 }
48}
49
50/// One layer in a [`LayerBuildup`].
51///
52/// `material_id` is the express ID of the associated `IfcMaterial`, or `0`
53/// when the layer has no material reference (valid per spec — represents an
54/// air gap / ventilated cavity).
55#[derive(Debug, Clone)]
56pub struct LayerInfo {
57 /// `IfcMaterial` entity ID for color lookup. Zero means no material.
58 pub material_id: u32,
59 /// Layer thickness in the project's length unit (same unit as the IFC
60 /// file). The caller is responsible for applying the project unit scale
61 /// when mapping to world coordinates.
62 pub thickness: f64,
63}
64
65/// Layer buildup resolved for one element.
66///
67/// `Sliceable` carries everything needed to produce N-1 cutting planes in
68/// the element's local frame and color each slice by its material.
69/// `NotSliceable` is emitted when we identified a material association but
70/// it doesn't map cleanly to planar slicing (constituents, single material,
71/// profile set, tapered with offsets, etc.) — callers should fall back to
72/// the existing mesh path and apply a uniform element-level colour.
73#[derive(Debug, Clone)]
74pub enum LayerBuildup {
75 Sliceable {
76 /// Layers in the order they appear in `IfcMaterialLayerSet.MaterialLayers`.
77 layers: Vec<LayerInfo>,
78 /// Which local axis the layers stack along.
79 axis: LayerAxis,
80 /// `+1.0` for `POSITIVE`, `-1.0` for `NEGATIVE`.
81 direction_sense: f64,
82 /// Signed distance from the element's reference line to the start
83 /// face of the first layer, in the project's length unit.
84 offset_from_reference_line: f64,
85 },
86 NotSliceable,
87}
88
89impl LayerBuildup {
90 pub fn is_sliceable(&self) -> bool {
91 matches!(self, LayerBuildup::Sliceable { .. })
92 }
93}
94
95/// Map from element entity ID to its resolved [`LayerBuildup`].
96#[derive(Debug, Default, Clone)]
97pub struct MaterialLayerIndex {
98 element_to_buildup: FxHashMap<u32, LayerBuildup>,
99}
100
101impl MaterialLayerIndex {
102 pub fn new() -> Self {
103 Self::default()
104 }
105
106 /// Scan `content` for `IfcRelAssociatesMaterial` and build the index.
107 ///
108 /// One pass over the file: for each association we resolve the
109 /// `RelatingMaterial` once and insert the result under every related
110 /// object ID. Elements that associate with a non-sliceable material are
111 /// still inserted (as `NotSliceable`) so callers can distinguish
112 /// "has a material, can't slice" from "no material association at all".
113 pub fn from_content(content: &str, decoder: &mut EntityDecoder) -> Self {
114 let mut index = Self::new();
115 let mut scanner = EntityScanner::new(content);
116
117 while let Some((id, type_name, start, end)) = scanner.next_entity() {
118 if type_name != "IFCRELASSOCIATESMATERIAL" {
119 continue;
120 }
121 let entity = match decoder.decode_at_with_id(id, start, end) {
122 Ok(e) => e,
123 Err(_) => continue,
124 };
125
126 // IfcRelAssociatesMaterial:
127 // 4: RelatedObjects (list)
128 // 5: RelatingMaterial (IfcMaterialSelect ref)
129 let relating_id = match entity.get_ref(5) {
130 Some(id) => id,
131 None => continue,
132 };
133 let related_attr = match entity.get(4) {
134 Some(a) => a,
135 None => continue,
136 };
137 let related_ids: Vec<u32> = match related_attr.as_list() {
138 Some(list) => list.iter().filter_map(|v| v.as_entity_ref()).collect(),
139 None => continue,
140 };
141 if related_ids.is_empty() {
142 continue;
143 }
144
145 let buildup = resolve_buildup(relating_id, decoder);
146 for obj_id in related_ids {
147 // The same element may be associated twice (once via element,
148 // once via its type). Prefer the Sliceable entry if we see
149 // one; never overwrite Sliceable with NotSliceable.
150 match index.element_to_buildup.get(&obj_id) {
151 Some(LayerBuildup::Sliceable { .. }) => continue,
152 _ => {
153 index.element_to_buildup.insert(obj_id, buildup.clone());
154 }
155 }
156 }
157 }
158
159 index
160 }
161
162 /// Get the resolved buildup for an element, or `None` if the element
163 /// has no material association at all.
164 pub fn get(&self, element_id: u32) -> Option<&LayerBuildup> {
165 self.element_to_buildup.get(&element_id)
166 }
167
168 /// Number of elements with a recorded buildup (sliceable or not).
169 pub fn len(&self) -> usize {
170 self.element_to_buildup.len()
171 }
172
173 pub fn is_empty(&self) -> bool {
174 self.element_to_buildup.is_empty()
175 }
176
177 /// Count how many of the recorded buildups are actually sliceable.
178 /// Useful for logging / statistics — not on the hot path.
179 pub fn sliceable_count(&self) -> usize {
180 self.element_to_buildup
181 .values()
182 .filter(|b| b.is_sliceable())
183 .count()
184 }
185}
186
187/// Resolve an `IfcMaterialSelect` ID into a [`LayerBuildup`].
188///
189/// Follows the one path that maps to planar cutting: `LayerSetUsage ->
190/// LayerSet -> Layers`. Anything else is `NotSliceable`.
191fn resolve_buildup(material_select_id: u32, decoder: &mut EntityDecoder) -> LayerBuildup {
192 let entity = match decoder.decode_by_id(material_select_id) {
193 Ok(e) => e,
194 Err(_) => return LayerBuildup::NotSliceable,
195 };
196
197 match entity.ifc_type {
198 IfcType::IfcMaterialLayerSetUsage => resolve_layer_set_usage(&entity, decoder),
199 // All other material representations either carry no geometry
200 // (IfcMaterial, IfcMaterialList, IfcMaterialConstituentSet) or
201 // describe cross-section rather than layers (IfcMaterialProfileSet,
202 // IfcMaterialProfileSetUsage). Caller falls back to uniform colour.
203 _ => LayerBuildup::NotSliceable,
204 }
205}
206
207/// Decode an `IfcMaterialLayerSetUsage` into a sliceable buildup.
208///
209/// Attribute layout
210/// (<https://standards.buildingsmart.org/IFC/RELEASE/IFC4_ADD2_TC1/HTML/schema/ifcproductextension/lexical/ifcmateriallayersetusage.htm>):
211/// 0: ForLayerSet (ref IfcMaterialLayerSet)
212/// 1: LayerSetDirection (IfcLayerSetDirectionEnum)
213/// 2: DirectionSense (IfcDirectionSenseEnum)
214/// 3: OffsetFromReferenceLine (IfcLengthMeasure)
215fn resolve_layer_set_usage(
216 usage: &DecodedEntity,
217 decoder: &mut EntityDecoder,
218) -> LayerBuildup {
219 let layer_set_id = match usage.get_ref(0) {
220 Some(id) => id,
221 None => return LayerBuildup::NotSliceable,
222 };
223 let axis = match usage.get(1).and_then(|a| a.as_enum()).map(str::to_ascii_uppercase) {
224 Some(s) if s == "AXIS1" => LayerAxis::Axis1,
225 Some(s) if s == "AXIS2" => LayerAxis::Axis2,
226 Some(s) if s == "AXIS3" => LayerAxis::Axis3,
227 // Missing or unrecognised → walls default to AXIS2 per spec, but
228 // rather than guess, treat as unsliceable.
229 _ => return LayerBuildup::NotSliceable,
230 };
231 let direction_sense = match usage.get(2).and_then(|a| a.as_enum()).map(str::to_ascii_uppercase)
232 {
233 Some(s) if s == "POSITIVE" => 1.0_f64,
234 Some(s) if s == "NEGATIVE" => -1.0_f64,
235 _ => return LayerBuildup::NotSliceable,
236 };
237 let offset = usage.get_float(3).unwrap_or(0.0);
238
239 let layer_set_entity = match decoder.decode_by_id(layer_set_id) {
240 Ok(e) => e,
241 Err(_) => return LayerBuildup::NotSliceable,
242 };
243 if layer_set_entity.ifc_type != IfcType::IfcMaterialLayerSet {
244 return LayerBuildup::NotSliceable;
245 }
246
247 // IfcMaterialLayerSet.MaterialLayers at attr 0
248 let layer_ids: Vec<u32> = match layer_set_entity.get(0).and_then(|a| a.as_list()) {
249 Some(list) => list.iter().filter_map(|v| v.as_entity_ref()).collect(),
250 None => return LayerBuildup::NotSliceable,
251 };
252 if layer_ids.is_empty() {
253 return LayerBuildup::NotSliceable;
254 }
255
256 let mut layers = Vec::with_capacity(layer_ids.len());
257 for layer_id in &layer_ids {
258 let layer = match decoder.decode_by_id(*layer_id) {
259 Ok(e) => e,
260 Err(_) => return LayerBuildup::NotSliceable,
261 };
262 // Tapered walls use IfcMaterialLayerWithOffsets (subtype of
263 // IfcMaterialLayer). The interface between such layers is a ruled
264 // surface, not a plane — bail to uniform fallback.
265 if layer.ifc_type != IfcType::IfcMaterialLayer {
266 return LayerBuildup::NotSliceable;
267 }
268 // IfcMaterialLayer:
269 // 0: Material (IfcMaterial ref, OPTIONAL)
270 // 1: LayerThickness (IfcPositiveLengthMeasure)
271 let material_id = layer.get_ref(0).unwrap_or(0);
272 let thickness = layer.get_float(1).unwrap_or(0.0);
273 if !thickness.is_finite() || thickness <= 0.0 {
274 // Spec forbids zero/negative thickness but malformed files exist.
275 // Skip the layer rather than the whole buildup.
276 continue;
277 }
278 layers.push(LayerInfo { material_id, thickness });
279 }
280
281 if layers.len() < 2 {
282 // A single-layer wall doesn't need slicing — uniform fallback is fine.
283 return LayerBuildup::NotSliceable;
284 }
285
286 LayerBuildup::Sliceable {
287 layers,
288 axis,
289 direction_sense,
290 offset_from_reference_line: offset,
291 }
292}