1use axiolid_core::{Interval, Transform2, Vec2};
17use axiolid_curve::{Curve2, Line2};
18use axiolid_model::{GeometryNode, NodeId};
19use axiolid_profile::{CenterLineProfile, Contour, ContourProfile, Profile, ProfileSegment};
20use ifc_model::{EntityId, Model};
21
22use crate::error::{GeometryError, GeometryResult};
23use crate::input::profile::{
24 describe_profile, ProfileDescription, ProfileOperator, ProfileParameters, ProfilePosition,
25};
26use crate::lower::session::LoweringSession;
27use crate::slots::Slots;
28use crate::units::UnitScale;
29
30mod composite;
31mod indexed;
32mod open;
33mod role;
34mod sections;
35pub use open::lower_open_profile_node;
36pub(crate) use role::BoundaryRole;
37
38pub const IMPLEMENTED_PROFILES: &[&str] = &[
40 "IFCARBITRARYCLOSEDPROFILEDEF",
41 "IFCARBITRARYOPENPROFILEDEF",
42 "IFCARBITRARYPROFILEDEFWITHVOIDS",
43 "IFCASYMMETRICISHAPEPROFILEDEF",
44 "IFCCENTERLINEPROFILEDEF",
45 "IFCCIRCLEHOLLOWPROFILEDEF",
46 "IFCCIRCLEPROFILEDEF",
47 "IFCCOMPOSITEPROFILEDEF",
48 "IFCCSHAPEPROFILEDEF",
49 "IFCDERIVEDPROFILEDEF",
50 "IFCELLIPSEPROFILEDEF",
51 "IFCISHAPEPROFILEDEF",
52 "IFCLSHAPEPROFILEDEF",
53 "IFCMIRROREDPROFILEDEF",
54 "IFCOPENCROSSPROFILEDEF",
56 "IFCRECTANGLEHOLLOWPROFILEDEF",
57 "IFCRECTANGLEPROFILEDEF",
58 "IFCROUNDEDRECTANGLEPROFILEDEF",
59 "IFCTSHAPEPROFILEDEF",
60 "IFCTRAPEZIUMPROFILEDEF",
61 "IFCUSHAPEPROFILEDEF",
62 "IFCZSHAPEPROFILEDEF",
63];
64
65pub const PLANNED_PROFILES: &[(&str, &str)] = &[(
76 "IFCPROFILEDEF",
77 "generic profile declaration carries no concrete geometry to lower",
78)];
79
80pub(crate) use crate::slots::profile_slot as slot;
81
82pub const UNLOWERED: &[(&str, &str)] = &[];
93
94const PROFILE: &str = "profile";
96const OPEN_PROFILE: &str = "open-profile";
97
98pub fn lower_profile_node(
106 session: &mut LoweringSession<'_>,
107 id: EntityId,
108) -> GeometryResult<NodeId> {
109 let frame = crate::transform::Transform::identity();
110 if let Some(node) = session.memoized(id, PROFILE, frame) {
111 return Ok(node);
112 }
113 let profile = lower_profile(session.model(), id, session.units())?;
114 let node = session.node_for(id, GeometryNode::Profile(profile))?;
115 session.memoize(id, PROFILE, frame, node);
116 Ok(node)
117}
118
119pub fn lower_profile(model: &Model, id: EntityId, units: &UnitScale) -> GeometryResult<Profile> {
124 let description = describe_profile(model, units, id)?;
125 build(model, units, &description)
126}
127
128fn build(
133 model: &Model,
134 units: &UnitScale,
135 description: &ProfileDescription,
136) -> GeometryResult<Profile> {
137 let profile = match &description.parameters {
138 ProfileParameters::ArbitraryClosed { outer_curve } => Profile::Contour(ContourProfile {
139 outer: curve_to_contour(model, *outer_curve, units, BoundaryRole::Profile)?,
140 holes: Vec::new(),
141 }),
142 ProfileParameters::ArbitraryWithVoids {
143 outer_curve,
144 inner_curves,
145 } => {
146 let outer = curve_to_contour(model, *outer_curve, units, BoundaryRole::Profile)?;
147 let mut holes = Vec::with_capacity(inner_curves.len());
148 for curve in inner_curves {
149 holes.push(curve_to_contour(
150 model,
151 *curve,
152 units,
153 BoundaryRole::Profile,
154 )?);
155 }
156 Profile::Contour(ContourProfile { outer, holes })
157 }
158 ProfileParameters::ArbitraryOpen { .. } | ProfileParameters::OpenCross { .. } => {
164 return Err(GeometryError::Unsupported {
165 entity: description.entity,
166 type_name: description.type_name.clone(),
167 detail: "open profiles have no area; use lower_open_profile_node",
168 });
169 }
170 ProfileParameters::CenterLine { curve, thickness } => {
173 let path = open::open_polyline_path(model, *curve, units)?;
174 Profile::CenterLine(CenterLineProfile::from_width(path, *thickness))
175 }
176 ProfileParameters::Composite { profiles, .. } => {
179 let mut members = Vec::with_capacity(profiles.len());
180 for member in profiles {
181 members.push(build(model, units, member)?);
182 }
183 Profile::Composite(members)
184 }
185 ProfileParameters::Derived {
186 parent, operator, ..
187 } => Profile::Derived {
188 basis: Box::new(build(model, units, parent)?),
189 transform: operator_2d(operator),
190 },
191 ProfileParameters::Mirrored { parent, .. } => Profile::Derived {
194 basis: Box::new(build(model, units, parent)?),
195 transform: Transform2::from_scale(Vec2::new(-1.0, 1.0)),
196 },
197 parameterized => sections::parameterized(description, parameterized)?,
198 };
199 Ok(match &description.position {
202 Some(position) => Profile::Derived {
203 basis: Box::new(profile),
204 transform: position_2d(position),
205 },
206 None => profile,
207 })
208}
209
210fn position_2d(position: &ProfilePosition) -> Transform2 {
212 Transform2::from_cols(
213 Vec2::from_array(position.x_axis),
214 Vec2::from_array(position.y_axis()),
215 Vec2::from_array(position.origin),
216 )
217}
218
219fn operator_2d(operator: &ProfileOperator) -> Transform2 {
221 Transform2::from_cols(
222 Vec2::from_array(operator.x_axis),
223 Vec2::from_array(operator.y_axis),
224 Vec2::from_array(operator.origin),
225 )
226}
227
228pub(crate) fn curve_to_contour(
241 model: &Model,
242 id: EntityId,
243 units: &UnitScale,
244 role: BoundaryRole,
245) -> GeometryResult<Contour> {
246 let entity = model.get(id).ok_or(GeometryError::MissingEntity {
247 referrer: id,
248 missing: id,
249 })?;
250 let type_name = entity.type_name.to_ascii_uppercase();
251 match type_name.as_str() {
252 "IFCPOLYLINE" => {}
253 "IFCCOMPOSITECURVE" => return composite::composite_contour(model, id, units, role),
254 "IFCINDEXEDPOLYCURVE" => return indexed::indexed_contour(model, id, units, role),
255 _ => {
256 return Err(GeometryError::Unsupported {
257 entity: id,
258 type_name,
259 detail: "profile boundaries lower IfcPolyline, IfcCompositeCurve and \
260 IfcIndexedPolyCurve only",
261 })
262 }
263 }
264
265 let slots = Slots::new(id, entity);
266 let mut points = polyline_points(model, id, units, role)?;
267 drop_closing_duplicate(&mut points);
268 if points.len() < 3 {
269 return Err(slots.degenerate("profile boundary has fewer than 3 distinct points"));
270 }
271
272 let segments = (0..points.len())
273 .map(|index| {
274 let origin = points[index];
275 let next = points[(index + 1) % points.len()];
276 ProfileSegment {
277 curve: Curve2::Line(Line2 {
278 origin,
279 direction: next - origin,
280 }),
281 domain: Interval::UNIT,
282 same_sense: true,
283 }
284 })
285 .collect();
286 Ok(Contour::new(segments))
287}
288
289fn polyline_points(
294 model: &Model,
295 id: EntityId,
296 units: &UnitScale,
297 role: BoundaryRole,
298) -> GeometryResult<Vec<Vec2>> {
299 let entity = model.get(id).ok_or(GeometryError::MissingEntity {
300 referrer: id,
301 missing: id,
302 })?;
303 let slots = Slots::new(id, entity);
304 let mut points = Vec::new();
305 for point_id in slots.req_ref_list(0, "Points")? {
306 let point = model.get(point_id).ok_or(GeometryError::MissingEntity {
307 referrer: id,
308 missing: point_id,
309 })?;
310 let coordinates = Slots::new(point_id, point).req_f64_list(0, "Coordinates")?;
311 let [x, y] = role.planar(point_id, &point.type_name, &coordinates)?;
312 points.push(Vec2::new(units.length(x), units.length(y)));
313 }
314 Ok(points)
315}
316
317fn drop_closing_duplicate(points: &mut Vec<Vec2>) {
318 if points.len() >= 2 && points[0].distance(*points.last().expect("length checked")) < 1e-12 {
319 points.pop();
320 }
321}
322
323#[cfg(test)]
324mod tests {
325 #[test]
327 fn the_generic_profile_refusal_matches_the_declared_reason() {
328 assert_eq!(
329 super::PLANNED_PROFILES[0].1,
330 crate::input::profile::GENERIC_PROFILE
331 );
332 }
333}