ifc_lite_geometry/placement/
mod.rs1mod chain;
19mod chain_walk;
20mod grid;
21mod grid_frame;
22mod heading;
23pub(crate) mod linear;
24mod validate;
25mod validated_chain;
26#[cfg(test)]
27mod validated_chain_tests;
28
29#[cfg(test)]
30mod axis_frame_tests;
31#[cfg(test)]
32mod chain_policy_tests;
33#[cfg(test)]
34mod chain_tests;
35#[cfg(test)]
36mod grid_tests;
37#[cfg(test)]
38mod linear_tests;
39#[cfg(test)]
40mod leaf_policy_tests;
41#[cfg(test)]
42mod mod_tests;
43
44pub use chain::{chain, resolve_chain, resolve_chain_relative, resolve_object_placement, Link, LinkKind};
45pub use heading::rotation_angle_about_z;
46pub use validate::{validate_chain, validate_spatial_frame, ChainPolicy};
47pub use validated_chain::ChainValidator;
48
49use crate::math::tol;
50use crate::axis_projection::perpendicular_x_axis;
51use ifc_lite_core::{slot, DecodedEntity, EntityDecoder, IfcType};
52use nalgebra::{Matrix4, Point3, Vector3};
53
54#[derive(Debug, thiserror::Error)]
56#[non_exhaustive]
57pub enum PlacementError {
58 #[error("placement chain is cyclic at #{at}")]
60 Cyclic { at: u32 },
61 #[error("#{at}: unsupported placement record {found}")]
63 Unsupported { at: u32, found: IfcType },
64 #[error("#{at}: missing {attr}")]
66 Missing { at: u32, attr: &'static str },
67 #[error("#{at}: {rule}")]
70 Policy { at: u32, rule: &'static str },
71 #[error(transparent)]
73 ProgramStopped(#[from] crate::resource::ProgramStop),
74 #[error(transparent)]
76 Decode(#[from] ifc_lite_core::Error),
77}
78
79impl From<PlacementError> for crate::Error {
80 fn from(error: PlacementError) -> Self {
81 match error {
82 PlacementError::Decode(core) => Self::CoreError(core),
83 PlacementError::ProgramStopped(stop) => Self::ProgramStopped(stop),
84 other => Self::GeometryError(other.to_string()),
85 }
86 }
87}
88
89impl PlacementError {
90 pub fn is_admission_refusal(&self) -> bool {
95 matches!(
96 self,
97 Self::Missing { .. }
98 | Self::Decode(
99 ifc_lite_core::Error::UnrepresentableReal(_)
100 | ifc_lite_core::Error::MalformedRecord(_)
101 )
102 )
103 }
104}
105
106pub type PlacementResult<T> = std::result::Result<T, PlacementError>;
108
109const Z: [f64; 3] = [0.0, 0.0, 1.0];
110const X: [f64; 3] = [1.0, 0.0, 0.0];
111
112#[derive(Debug, Clone, Copy, PartialEq)]
120pub struct Axis2Placement {
121 pub location: [f64; 3],
122 pub axis: Option<[f64; 3]>,
123 pub ref_direction: Option<[f64; 3]>,
124 pub planar: bool,
126}
127
128impl Axis2Placement {
129 pub fn read_3d(placement: &DecodedEntity, decoder: &mut EntityDecoder) -> PlacementResult<Self> {
132 if placement.ifc_type != IfcType::IfcAxis2Placement3D {
133 return Err(unsupported(decoder, placement.id, placement.ifc_type.clone()));
134 }
135 Ok(Self {
136 location: location(placement, decoder)?,
137 axis: decoder.direction3_at(placement, slot::IfcAxis2Placement3D::AXIS.index())?,
138 ref_direction: decoder.direction3_at(placement, slot::IfcAxis2Placement3D::REF_DIRECTION.index())?,
139 planar: false,
140 })
141 }
142
143 pub fn read_2d(placement: &DecodedEntity, decoder: &mut EntityDecoder) -> PlacementResult<Self> {
147 if placement.ifc_type != IfcType::IfcAxis2Placement2D {
148 return Err(unsupported(decoder, placement.id, placement.ifc_type.clone()));
149 }
150 Ok(Self {
151 location: location(placement, decoder)?,
152 axis: Some(Z),
153 ref_direction: decoder.direction3_at(placement, slot::IfcAxis2Placement2D::REF_DIRECTION.index())?,
154 planar: true,
155 })
156 }
157
158 pub fn read(placement: &DecodedEntity, decoder: &mut EntityDecoder) -> PlacementResult<Self> {
161 match &placement.ifc_type {
162 IfcType::IfcAxis2Placement3D => Self::read_3d(placement, decoder),
163 IfcType::IfcAxis2Placement2D => Self::read_2d(placement, decoder),
164 found => Err(unsupported(decoder, placement.id, found.clone())),
165 }
166 }
167
168 pub fn read_id(id: u32, decoder: &mut EntityDecoder) -> PlacementResult<Self> {
170 let placement = decoder.decode_by_id(id)?;
171 Self::read(&placement, decoder)
172 }
173
174 pub fn matrix(&self) -> Matrix4<f64> {
177 build_axis2_matrix(
178 Point3::from(self.location),
179 Vector3::from(self.axis.unwrap_or(Z)),
180 Vector3::from(self.ref_direction.unwrap_or(X)),
181 )
182 }
183}
184
185#[derive(Debug, Clone, Copy, PartialEq)]
187pub struct Axis1Placement {
188 pub location: [f64; 3],
189 pub axis: Option<[f64; 3]>,
190}
191
192impl Axis1Placement {
193 pub fn read(placement: &DecodedEntity, decoder: &mut EntityDecoder) -> PlacementResult<Self> {
194 if placement.ifc_type != IfcType::IfcAxis1Placement {
195 return Err(unsupported(decoder, placement.id, placement.ifc_type.clone()));
196 }
197 Ok(Self { location: location(placement, decoder)?, axis: decoder.direction3_at(placement, slot::IfcAxis1Placement::AXIS.index())? })
198 }
199
200 pub fn axis_or_ifc_default(&self) -> [f64; 3] {
203 self.axis.unwrap_or(Z)
204 }
205}
206
207fn location(placement: &DecodedEntity, decoder: &mut EntityDecoder) -> PlacementResult<[f64; 3]> {
209 decoder
210 .point3_at(placement, slot::IfcPlacement::LOCATION.index())?
211 .ok_or_else(|| missing(decoder, placement.id, "Location"))
212}
213
214pub(super) fn missing(decoder: &EntityDecoder, at: u32, attr: &'static str) -> PlacementError {
218 let _counted = decoder.refuse_malformed(at);
219 PlacementError::Missing { at, attr }
220}
221
222pub(super) fn unsupported(decoder: &EntityDecoder, at: u32, found: IfcType) -> PlacementError {
224 let _counted = decoder.refuse_malformed(at);
225 PlacementError::Unsupported { at, found }
226}
227
228fn sampled<T>(result: crate::Result<T>) -> PlacementResult<Option<T>> {
231 crate::resource::check_current()?;
232 match result {
233 Ok(value) => Ok(Some(value)),
234 Err(crate::Error::ProgramStopped(stop)) => Err(PlacementError::ProgramStopped(stop)),
235 Err(crate::Error::CoreError(error)) => Err(PlacementError::Decode(error)),
236 Err(_) => Ok(None), }
238}
239
240pub(crate) fn build_axis2_matrix(
251 location: Point3<f64>,
252 z_axis: Vector3<f64>,
253 x_axis: Vector3<f64>,
254) -> Matrix4<f64> {
255 let z_axis_final = z_axis
260 .try_normalize(tol::AXIS_MIN_LEN)
261 .unwrap_or_else(|| Vector3::new(0.0, 0.0, 1.0));
262 let x_axis_normalized = x_axis
263 .try_normalize(tol::AXIS_MIN_LEN)
264 .unwrap_or_else(|| Vector3::new(1.0, 0.0, 0.0));
265
266 let x_axis_final = perpendicular_x_axis(z_axis_final, x_axis_normalized);
267
268 let y_axis = z_axis_final.cross(&x_axis_final).normalize();
270
271 let mut transform = Matrix4::identity();
273 transform[(0, 0)] = x_axis_final.x;
274 transform[(1, 0)] = x_axis_final.y;
275 transform[(2, 0)] = x_axis_final.z;
276 transform[(0, 1)] = y_axis.x;
277 transform[(1, 1)] = y_axis.y;
278 transform[(2, 1)] = y_axis.z;
279 transform[(0, 2)] = z_axis_final.x;
280 transform[(1, 2)] = z_axis_final.y;
281 transform[(2, 2)] = z_axis_final.z;
282 transform[(0, 3)] = location.x;
283 transform[(1, 3)] = location.y;
284 transform[(2, 3)] = location.z;
285
286 transform
287}