Skip to main content

ifc_geometry/lower/
provenance.rs

1//! IFC source attribution for format-neutral geometry nodes.
2//!
3//! Provenance is deliberately a side table. `axiolid-model` stays IFC-agnostic,
4//! while diagnostics and consumers of a lowered result can still trace a node
5//! back to the entity that emitted it.
6
7use axiolid_model::NodeId;
8use ifc_model::EntityId;
9use std::collections::{BTreeMap, BTreeSet};
10
11/// Source IFC entities for nodes in one lowered geometry graph.
12///
13/// The map is partial: caller-synthesized nodes created outside an active IFC
14/// entity scope have no source instead of receiving a fabricated entity id.
15#[derive(Debug, Clone, Default, PartialEq, Eq)]
16pub struct ProvenanceMap {
17    sources: BTreeMap<NodeId, EntityId>,
18    /// Faces left out under `DegenerateFacePolicy::DropAndReport` (#46).
19    dropped_faces: BTreeSet<EntityId>,
20    /// Stations stored with the kernel's seam-snapping window (#423).
21    seam_windows: BTreeSet<EntityId>,
22    /// Runs whose stations carry the kernel's seam-snapping window (#429)
23    /// or stand at a basis end the kernel measures (#433).
24    seam_window_runs: BTreeSet<EntityId>,
25}
26
27impl ProvenanceMap {
28    /// Return the IFC entity that emitted `node`, if it has an IFC source.
29    pub fn source(&self, node: NodeId) -> Option<EntityId> {
30        self.sources.get(&node).copied()
31    }
32
33    /// Number of attributed graph nodes.
34    pub fn len(&self) -> usize {
35        self.sources.len()
36    }
37
38    /// Whether no graph nodes carry IFC attribution.
39    pub fn is_empty(&self) -> bool {
40        self.sources.is_empty()
41    }
42
43    /// Iterate deterministically over `(node, source entity)` pairs.
44    pub fn iter(&self) -> impl Iterator<Item = (NodeId, EntityId)> + '_ {
45        self.sources.iter().map(|(&node, &source)| (node, source))
46    }
47
48    /// `IfcFace` entities left out because their outer loop collapsed, in
49    /// ascending id.
50    ///
51    /// Always empty under the default `DegenerateFacePolicy::Refuse`. A face
52    /// shared by several items is listed once. A non-empty list means the
53    /// lowered geometry is NOT exactly what the file authored: a consumer
54    /// using it as evidence decides whether that is acceptable.
55    pub fn dropped_faces(&self) -> impl Iterator<Item = EntityId> + '_ {
56        self.dropped_faces.iter().copied()
57    }
58
59    /// The stations lowered with the kernel's seam-snapping window (#423),
60    /// in ascending id: each `IfcPointByDistanceExpression` or
61    /// `IfcAxis2PlacementLinear` on a basis whose seams lie at arc-length
62    /// integrals, which the kernel snaps onto a seam within the model's
63    /// precision when it resolves the station. A kernel refusal of such a
64    /// window names these stations
65    /// (`GeometryError::StationSeamWindowRefused`, with `compile`).
66    pub fn seam_windows(&self) -> impl Iterator<Item = EntityId> + '_ {
67        self.seam_windows.iter().copied()
68    }
69
70    pub(crate) fn record_seam_window(&mut self, station: EntityId) {
71        self.seam_windows.insert(station);
72    }
73
74    /// The runs whose stations carry the kernel's seam-snapping window
75    /// (#429), in ascending id: each `IfcSectionedSolidHorizontal`,
76    /// `IfcSectionedSurface` or `IfcOffsetCurveByDistances` along a basis
77    /// whose seams lie at arc-length integrals, whose stations the kernel
78    /// snaps onto a seam within the model's precision; and each
79    /// `IfcOffsetCurveByDistances` continued to a basis end whose distance
80    /// the kernel measures (#433). A kernel refusal of such a window, or of
81    /// the stations' order once snapped or once the end is resolved, names
82    /// these runs (`GeometryError::RunSeamWindowRefused`, with `compile`).
83    pub fn seam_window_runs(&self) -> impl Iterator<Item = EntityId> + '_ {
84        self.seam_window_runs.iter().copied()
85    }
86
87    pub(crate) fn record_seam_window_run(&mut self, run: EntityId) {
88        self.seam_window_runs.insert(run);
89    }
90
91    pub(crate) fn record_dropped_face(&mut self, face: EntityId) {
92        self.dropped_faces.insert(face);
93    }
94
95    pub(crate) fn record(&mut self, node: NodeId, source: EntityId) {
96        if let Some(previous) = self.sources.insert(node, source) {
97            debug_assert_eq!(
98                previous, source,
99                "one graph node cannot have two IFC sources"
100            );
101        }
102    }
103}