ifc_lite_geometry/router/
processor_registry.rs1use super::GeometryProcessor;
9use crate::processors::{
10 AdvancedBrepProcessor, BSplineSurfaceProcessor, BlockProcessor, BooleanClippingProcessor,
11 CsgSolidProcessor, ExtrudedAreaSolidProcessor, ExtrudedAreaSolidTaperedProcessor,
12 FaceBasedSurfaceModelProcessor, FacetedBrepProcessor, IfcAlignmentProcessor,
13 IfcEdgeProcessor, IfcFaceSurfaceProcessor, PolygonalFaceSetProcessor,
14 RevolvedAreaSolidProcessor, SectionedSolidHorizontalProcessor,
15 ShellBasedSurfaceModelProcessor, SphereProcessor,
16 SurfaceCurveSweptAreaSolidProcessor, SurfaceOfLinearExtrusionProcessor, SweptDiskSolidProcessor, TriangulatedFaceSetProcessor,
17};
18use ifc_lite_core::{IfcSchema, IfcType};
19use std::cell::OnceCell;
20use std::collections::HashMap;
21use std::rc::Rc;
22
23macro_rules! processor_families {
25 ($schema:ident; $( $family:ident => [$($ifc_type:ident),+] => $constructor:expr; )+) => {
26 #[non_exhaustive]
29 #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
30 #[serde(rename_all = "snake_case")]
31 #[repr(usize)]
32 pub enum ConstructionFamily { $( $family, )+ }
33 const FAMILIES: &[ConstructionFamily] = &[$(ConstructionFamily::$family),+];
34 pub(super) const TYPES: [&[IfcType]; FAMILIES.len()] = [
35 $(&[$(IfcType::$ifc_type),+]),+
36 ];
37 pub(crate) fn builtin_family(ifc_type: &IfcType) -> Option<ConstructionFamily> {
38 Some(match ifc_type {
39 $($(IfcType::$ifc_type)|+ => ConstructionFamily::$family,)+
40 _ => return None,
41 })
42 }
43 fn create(index: usize, $schema: &IfcSchema) -> Rc<dyn GeometryProcessor> {
44 match FAMILIES[index] { $(ConstructionFamily::$family => Rc::new($constructor),)+ }
45 }
46 };
47}
48
49processor_families! { schema;
50 Extrusion => [IfcExtrudedAreaSolid] => ExtrudedAreaSolidProcessor::new(schema.clone());
51 TaperedExtrusion => [IfcExtrudedAreaSolidTapered] => ExtrudedAreaSolidTaperedProcessor::new(schema.clone());
52 TriangulatedFaceSet => [IfcTriangulatedFaceSet, IfcTriangulatedIrregularNetwork] => TriangulatedFaceSetProcessor::new();
53 PolygonalFaceSet => [IfcPolygonalFaceSet] => PolygonalFaceSetProcessor::new();
54 FacetedBrep => [IfcFacetedBrep, IfcFacetedBrepWithVoids] => FacetedBrepProcessor::new();
56 Boolean => [IfcBooleanResult, IfcBooleanClippingResult] => BooleanClippingProcessor::new();
57 SweptDisk => [IfcSweptDiskSolid] => SweptDiskSolidProcessor::new(schema.clone());
58 RevolvedAreaSolid => [IfcRevolvedAreaSolid] => RevolvedAreaSolidProcessor::new(schema.clone());
59 SweptArea => [IfcSurfaceCurveSweptAreaSolid, IfcFixedReferenceSweptAreaSolid] => SurfaceCurveSweptAreaSolidProcessor::new(schema.clone());
60 SectionedSolid => [IfcSectionedSolidHorizontal] => SectionedSolidHorizontalProcessor::new(schema.clone());
61 AdvancedBrep => [IfcAdvancedBrep, IfcAdvancedBrepWithVoids] => AdvancedBrepProcessor::new();
62 BSplineSurface => [IfcBSplineSurfaceWithKnots, IfcRationalBSplineSurfaceWithKnots] => BSplineSurfaceProcessor::new();
63 ShellSurfaceModel => [IfcShellBasedSurfaceModel] => ShellBasedSurfaceModelProcessor::new();
64 FaceSurfaceModel => [IfcFaceBasedSurfaceModel] => FaceBasedSurfaceModelProcessor::new();
65 Block => [IfcBlock] => BlockProcessor::new();
66 Sphere => [IfcSphere] => SphereProcessor::new();
67 CsgSolid => [IfcCsgSolid] => CsgSolidProcessor::new();
68 Alignment => [IfcAlignment] => IfcAlignmentProcessor::new();
69 Edge => [IfcEdge, IfcEdgeCurve, IfcOrientedEdge] => IfcEdgeProcessor::new();
70 FaceSurface => [IfcFaceSurface, IfcAdvancedFace] => IfcFaceSurfaceProcessor::new();
71 LinearExtrusionSurface => [IfcSurfaceOfLinearExtrusion] => SurfaceOfLinearExtrusionProcessor::new();
73}
74
75fn slot(ifc_type: IfcType) -> Option<usize> {
76 builtin_family(&ifc_type).map(|family| family as usize)
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub(crate) enum ProcessorOrigin { Builtin, ConfiguredBuiltin, Custom }
83
84struct RegisteredProcessor {
85 processor: Rc<dyn GeometryProcessor>,
86 origin: ProcessorOrigin,
87}
88
89pub(crate) fn builtin_processor(
101 ifc_type: IfcType,
102 schema: &IfcSchema,
103) -> Option<Rc<dyn GeometryProcessor>> {
104 slot(ifc_type).map(|index| create(index, schema))
105}
106
107pub(super) struct ProcessorRegistry {
108 defaults: [OnceCell<Rc<dyn GeometryProcessor>>; TYPES.len()],
109 overrides: HashMap<IfcType, RegisteredProcessor>,
110}
111
112impl ProcessorRegistry {
113 pub(super) fn new() -> Self {
114 Self {
115 defaults: std::array::from_fn(|_| OnceCell::new()),
116 overrides: HashMap::new(),
117 }
118 }
119
120 pub(super) fn get(&self, ifc_type: &IfcType, schema: &IfcSchema) -> Option<&Rc<dyn GeometryProcessor>> {
121 if let Some(processor) = self.overrides.get(ifc_type) {
122 return Some(&processor.processor);
123 }
124 let index = slot(ifc_type.clone())?;
125 Some(self.defaults[index].get_or_init(|| create(index, schema)))
126 }
127
128 pub(super) fn insert(&mut self, ifc_type: IfcType, processor: Rc<dyn GeometryProcessor>, origin: ProcessorOrigin) {
129 self.overrides.insert(ifc_type.clone(), RegisteredProcessor { processor, origin });
130 if let Some(index) = slot(ifc_type) {
131 if TYPES[index].iter().all(|t| self.overrides.contains_key(t)) {
134 self.defaults[index].take();
135 }
136 }
137 }
138
139 pub(super) fn origin(&self, ifc_type: &IfcType) -> Option<ProcessorOrigin> {
140 self.overrides.get(ifc_type).map(|entry| entry.origin)
141 .or_else(|| builtin_family(ifc_type).map(|_| ProcessorOrigin::Builtin))
142 }
143
144 pub(super) fn values(&self) -> impl Iterator<Item = &Rc<dyn GeometryProcessor>> {
145 self.overrides.values().map(|entry| &entry.processor).chain(self.defaults.iter().filter_map(OnceCell::get))
149 }
150}
151#[cfg(test)]
152mod typed_family_tests {
153 use super::*;
154 use crate::GeometryRouter;
155
156 #[test]
157 fn family_aliases_match_the_processors_actual_dispatch_contract() {
158 let schema = IfcSchema::new();
159 let mut seen = std::collections::HashSet::new();
160 for (index, types) in TYPES.iter().enumerate() {
161 let processor = create(index, &schema);
162 let supported = processor.supported_types();
163 for ifc_type in *types {
164 assert!(seen.insert(ifc_type.clone()), "an alias cannot belong to two families");
165 assert_eq!(builtin_family(ifc_type), Some(FAMILIES[index]));
166 assert!(supported.contains(ifc_type), "family advertises a type its processor cannot execute");
167 }
168 for ifc_type in supported {
169 assert!(types.contains(&ifc_type), "processor type missing from typed family");
170 }
171 }
172 assert_eq!(builtin_family(&IfcType::IfcWall), None);
173 }
174
175 #[test]
176 fn metadata_queries_do_not_initialize_or_drain_processors() {
177 let registry = ProcessorRegistry::new();
178 assert_eq!(registry.origin(&IfcType::IfcBlock), Some(ProcessorOrigin::Builtin));
179 assert_eq!(registry.origin(&IfcType::IfcWall), None);
180 assert_eq!(registry.values().count(), 0, "metadata-only planning must preserve lazy construction");
181 let schema = IfcSchema::new();
182 registry.get(&IfcType::IfcBlock, &schema).unwrap();
183 assert_eq!(registry.values().count(), 1);
184 assert_eq!(registry.origin(&IfcType::IfcBlock), Some(ProcessorOrigin::Builtin));
185 assert_eq!(registry.values().count(), 1);
186 }
187
188 #[test]
189 fn configured_builtins_and_public_overrides_keep_distinct_origins() {
190 let mut router = GeometryRouter::new();
191 assert_eq!(router.processor_origin(&IfcType::IfcBooleanResult), Some(ProcessorOrigin::Builtin));
192 router.register(Box::new(BlockProcessor::new()));
193 assert_eq!(router.processor_origin(&IfcType::IfcBlock), Some(ProcessorOrigin::Custom));
196 let source = "#1=IFCBLOCK($,2.,3.,4.);";
197 let mut decoder = ifc_lite_core::EntityDecoder::new(source);
198 let entity = decoder.decode_by_id(1).unwrap();
199 let mesh = router.process_representation_item(&entity, &mut decoder).unwrap();
200 assert!(!mesh.is_empty(), "origin tracking must preserve public dispatch");
201 router.set_skip_small_cuts(true);
202 assert_eq!(router.processor_origin(&IfcType::IfcBooleanResult), Some(ProcessorOrigin::ConfiguredBuiltin));
203 assert_eq!(router.processor_origin(&IfcType::IfcBooleanClippingResult), Some(ProcessorOrigin::ConfiguredBuiltin));
204 assert_eq!(router.processor_origin(&IfcType::IfcCsgSolid), Some(ProcessorOrigin::ConfiguredBuiltin));
205 assert_eq!(router.processor_origin(&IfcType::IfcBlock), Some(ProcessorOrigin::Custom));
206 router.register(Box::new(BooleanClippingProcessor::new()));
207 assert_eq!(router.processor_origin(&IfcType::IfcBooleanResult), Some(ProcessorOrigin::Custom));
208 router.set_skip_small_cuts(false);
209 assert_eq!(router.processor_origin(&IfcType::IfcBooleanResult), Some(ProcessorOrigin::ConfiguredBuiltin));
210 }
211}