ifc_geometry/lower/
mapped.rs1use axiolid_model::{GeometryNode, Instance, NodeId};
25use ifc_model::EntityId;
26
27use crate::error::GeometryResult;
28use crate::lower::session::LoweringSession;
29use crate::resource::mapped::{MappedItem, RepresentationMap};
30use crate::resource::operator::operator_transform;
31use crate::resource::placement::axis_placement_transform;
32use crate::transform::Transform;
33
34const MAPPED: &str = "mapped-item";
36
37const REPRESENTATION: &str = "representation";
39
40pub(crate) const KIND: &str = "mapped item";
42
43const REPRESENTATION_ITEMS: usize = 3;
45
46pub fn lower_mapped_item_node(
51 session: &mut LoweringSession<'_>,
52 id: EntityId,
53 world: Transform,
54) -> GeometryResult<NodeId> {
55 if let Some(node) = session.memoized(id, MAPPED, world) {
56 return Ok(node);
57 }
58 session.enter(id, KIND)?;
59 let node = lower_mapped_item_inner(session, id, world);
60 session.exit(id);
61 let node = node?;
62 session.memoize(id, MAPPED, world, node);
63 Ok(node)
64}
65
66fn lower_mapped_item_inner(
67 session: &mut LoweringSession<'_>,
68 id: EntityId,
69 world: Transform,
70) -> GeometryResult<NodeId> {
71 let occurrence = mapped_occurrence(session, id, world)?;
72
73 let source = lower_representation(session, occurrence.representation)?;
77
78 session.node_for(
79 id,
80 GeometryNode::Instance(Instance {
81 source,
82 transform: occurrence.placement.to_geom(),
83 }),
84 )
85}
86
87pub(crate) struct MappedOccurrence {
90 pub(crate) representation: EntityId,
92 pub(crate) placement: Transform,
94}
95
96pub(crate) fn mapped_occurrence(
99 session: &LoweringSession<'_>,
100 id: EntityId,
101 frame: Transform,
102) -> GeometryResult<MappedOccurrence> {
103 let entity = session.entity(id, id)?;
104 let item = MappedItem::new(id, entity);
105 let source_ref = item.mapping_source()?;
106 let target_ref = item.mapping_target()?;
107
108 let map_entity = session.entity(id, source_ref)?;
109 let map = RepresentationMap::new(source_ref, map_entity);
110 let origin_ref = map.mapping_origin()?;
111 let representation = map.mapped_representation()?;
112
113 let target_entity = session.entity(id, target_ref)?;
117 let target =
119 operator_transform(session.model(), target_ref, target_entity)?.to_metres(session.units());
120 let origin_entity = session.entity(source_ref, origin_ref)?;
121 let origin = axis_placement_transform(session.model(), origin_ref, origin_entity)?
122 .to_metres(session.units());
123
124 Ok(MappedOccurrence {
125 representation,
126 placement: frame.compose(&target).compose(&origin),
127 })
128}
129
130pub(crate) fn representation_items(
132 session: &LoweringSession<'_>,
133 id: EntityId,
134) -> GeometryResult<Vec<EntityId>> {
135 let slots = session.slots(id)?;
136 slots.req_ref_list(REPRESENTATION_ITEMS, "Items")
137}
138
139pub fn lower_representation(
145 session: &mut LoweringSession<'_>,
146 id: EntityId,
147) -> GeometryResult<NodeId> {
148 let frame = Transform::identity();
149 if let Some(node) = session.memoized(id, REPRESENTATION, frame) {
150 return Ok(node);
151 }
152 session.enter(id, KIND)?;
153 let node = lower_representation_inner(session, id);
154 session.exit(id);
155 let node = node?;
156 session.memoize(id, REPRESENTATION, frame, node);
157 Ok(node)
158}
159
160fn lower_representation_inner(
161 session: &mut LoweringSession<'_>,
162 id: EntityId,
163) -> GeometryResult<NodeId> {
164 let items = representation_items(session, id)?;
165
166 let mut members = Vec::with_capacity(items.len());
167 for item in items {
168 members.push(session.lower_operand(item, Transform::identity())?);
172 }
173 session.node_for(id, GeometryNode::Collection(members))
174}