use ifc_lite_core::{DecodedEntity, EntityDecoder, IfcType};
#[path = "operator.rs"]
mod operator;
pub(super) use operator::parse_cartesian_transformation_operator;
#[derive(Clone, Copy, Debug)]
pub(super) struct Transform2D {
pub(super) tx: f32,
pub(super) ty: f32,
pub(super) tz: f32,
pub(super) m00: f32,
pub(super) m01: f32,
pub(super) m10: f32,
pub(super) m11: f32,
}
impl Transform2D {
pub(super) fn identity() -> Self {
Self {
tx: 0.0,
ty: 0.0,
tz: 0.0,
m00: 1.0,
m01: 0.0,
m10: 0.0,
m11: 1.0,
}
}
pub(super) fn unresolved() -> Self {
Self { tz: f32::NAN, ..Self::identity() }
}
pub(super) fn scale(&self) -> f32 {
(self.m00 * self.m11 - self.m01 * self.m10).abs().sqrt()
}
pub(super) fn transform_point(&self, x: f32, y: f32) -> (f32, f32) {
let rx = self.m00 * x + self.m01 * y;
let ry = self.m10 * x + self.m11 * y;
(rx + self.tx, ry + self.ty)
}
}
pub(super) fn compose_transforms(a: &Transform2D, b: &Transform2D) -> Transform2D {
let m00 = a.m00 * b.m00 + a.m01 * b.m10;
let m01 = a.m00 * b.m01 + a.m01 * b.m11;
let m10 = a.m10 * b.m00 + a.m11 * b.m10;
let m11 = a.m10 * b.m01 + a.m11 * b.m11;
let rtx = a.m00 * b.tx + a.m01 * b.ty;
let rty = a.m10 * b.tx + a.m11 * b.ty;
Transform2D {
tx: rtx + a.tx,
ty: rty + a.ty,
tz: a.tz + b.tz,
m00,
m01,
m10,
m11,
}
}
pub(super) fn resolve_object_placement(
entity: &DecodedEntity,
decoder: &mut EntityDecoder,
unit_scale: f32,
) -> Transform2D {
let Some(attr) = entity.get(5) else {
return Transform2D::identity();
};
if attr.is_null() {
return Transform2D::identity();
}
let Ok(Some(placement)) = decoder.resolve_ref(attr) else {
return Transform2D::unresolved(); };
resolve_placement_for_symbolic(&placement, decoder, unit_scale, 0)
}
fn resolve_placement_for_symbolic(
placement: &DecodedEntity,
decoder: &mut EntityDecoder,
unit_scale: f32,
depth: usize,
) -> Transform2D {
if depth > 50 || placement.ifc_type != IfcType::IfcLocalPlacement {
return Transform2D::unresolved(); }
let parent_transform = match placement.get(0) {
Some(parent_attr) if !parent_attr.is_null() => match decoder.resolve_ref(parent_attr) {
Ok(Some(parent)) => {
resolve_placement_for_symbolic(&parent, decoder, unit_scale, depth + 1)
}
_ => Transform2D::unresolved(), },
_ => Transform2D::identity(), };
let local_transform = match placement.get(1) {
Some(rel_attr) if !rel_attr.is_null() => match decoder.resolve_ref(rel_attr) {
Ok(Some(rel))
if rel.ifc_type == IfcType::IfcAxis2Placement3D
|| rel.ifc_type == IfcType::IfcAxis2Placement2D =>
{
parse_axis2_placement_2d(&rel, decoder, unit_scale)
}
_ => Transform2D::unresolved(), },
_ => Transform2D::unresolved(), };
compose_transforms(&parent_transform, &local_transform)
}
pub(super) fn parse_axis2_placement_2d(
placement: &DecodedEntity,
decoder: &mut EntityDecoder,
unit_scale: f32,
) -> Transform2D {
if placement.ifc_type != IfcType::IfcAxis2Placement3D
&& placement.ifc_type != IfcType::IfcAxis2Placement2D
{
return Transform2D::unresolved();
}
let is_3d = placement.ifc_type == IfcType::IfcAxis2Placement3D;
let (tx, ty, tz) = match placement.get_ref(0) {
Some(loc_ref) => match decoder.decode_by_id(loc_ref) {
Ok(loc) if loc.ifc_type == IfcType::IfcCartesianPoint => {
let coords = loc
.get(0)
.and_then(|a| a.as_list())
.map(|l| l.to_vec())
.unwrap_or_default();
let raw_x = coords.first().and_then(|v| v.as_float()).unwrap_or(0.0) as f32;
let raw_y = coords.get(1).and_then(|v| v.as_float()).unwrap_or(0.0) as f32;
let raw_z = coords.get(2).and_then(|v| v.as_float()).unwrap_or(0.0) as f32;
(raw_x * unit_scale, raw_y * unit_scale, raw_z * unit_scale)
}
_ => return Transform2D::unresolved(), },
None => return Transform2D::unresolved(), };
let ref_dir_attr = if is_3d {
placement.get(2)
} else {
placement.get(1)
};
let (dx, dy) = match ref_dir_attr {
Some(attr) if !attr.is_null() => match attr.as_entity_ref() {
Some(ref_dir_id) => match decoder.decode_by_id(ref_dir_id) {
Ok(ref_dir) if ref_dir.ifc_type == IfcType::IfcDirection => {
let ratios = ref_dir
.get(0)
.and_then(|a| a.as_list())
.map(|l| l.to_vec())
.unwrap_or_default();
let dx = ratios.first().and_then(|v| v.as_float()).unwrap_or(1.0) as f32;
let dy = ratios.get(1).and_then(|v| v.as_float()).unwrap_or(0.0) as f32;
let dz = ratios.get(2).and_then(|v| v.as_float()).unwrap_or(0.0) as f32;
let len = (dx * dx + dy * dy).sqrt();
if len > 0.0001 {
(dx / len, dy / len)
} else if is_3d && dz.abs() > 0.0001 {
(1.0, 0.0)
} else {
(1.0, 0.0)
}
}
_ => (1.0, 0.0),
},
None => (1.0, 0.0),
},
_ => (1.0, 0.0),
};
Transform2D {
tx,
ty,
tz,
m00: dx,
m01: -dy,
m10: dy,
m11: dx,
}
}
pub(super) fn circle_center(
item: &DecodedEntity,
decoder: &mut EntityDecoder,
unit_scale: f32,
) -> (f32, f32, f32) {
let Some(pos_ref) = item.get_ref(0) else {
return (0.0, 0.0, 0.0);
};
let Ok(placement) = decoder.decode_by_id(pos_ref) else {
return (0.0, 0.0, 0.0);
};
let Some(loc_ref) = placement.get_ref(0) else {
return (0.0, 0.0, 0.0);
};
let Ok(loc) = decoder.decode_by_id(loc_ref) else {
return (0.0, 0.0, 0.0);
};
let Some(coords) = loc.get(0).and_then(|a| a.as_list()) else {
return (0.0, 0.0, 0.0);
};
let x = coords.first().and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let y = coords.get(1).and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let z = coords.get(2).and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
(x, y, z)
}
#[cfg(test)]
#[path = "transform_tests.rs"]
mod transform_tests;