Skip to main content

ifc_geometry/lower/
bbox.rs

1//! `IfcBoundingBox` lowering.
2//!
3//! # The box is not world-aligned
4//!
5//! `IfcBoundingBox` is axis-aligned in ITS OWN representation's coordinate
6//! system, which is usually the element's local placement and is routinely
7//! rotated relative to the world. `GeometryNode::BoundingBox` is an
8//! `Aabb`: world-axis-aligned by definition. Passing the corner and extents
9//! straight through would therefore claim a box the file never described --
10//! correct only when the placement happens to be axis-parallel.
11//!
12//! So the eight local corners are transformed and the world AABB is recomputed
13//! from them. For a rotated element that yields a LARGER box than the local
14//! extents, which is the honest answer: it is the tightest world-aligned box
15//! that still contains the element.
16
17use axiolid_core::{Aabb, Point3};
18use axiolid_model::GeometryNode;
19use ifc_model::EntityId;
20
21use crate::error::GeometryResult;
22use crate::lower::session::LoweringSession;
23use crate::resource::point::CartesianPoint;
24use crate::solid::bbox::BoundingBox;
25use crate::transform::Transform;
26
27/// Chain kind reported when a bounding box nests too deeply or cycles.
28const KIND: &str = "bounding box";
29
30/// Lower one `IfcBoundingBox` into a world-axis-aligned `Aabb`.
31pub fn lower_bounding_box_node(
32    session: &mut LoweringSession<'_>,
33    id: EntityId,
34    frame: Transform,
35) -> GeometryResult<axiolid_model::NodeId> {
36    if let Some(node) = session.memoized(id, KIND, frame) {
37        return Ok(node);
38    }
39    session.enter(id, KIND)?;
40    let result = build(session, id, frame);
41    session.exit(id);
42    let node = result?;
43    session.memoize(id, KIND, frame, node);
44    Ok(node)
45}
46
47fn build(
48    session: &mut LoweringSession<'_>,
49    id: EntityId,
50    frame: Transform,
51) -> GeometryResult<axiolid_model::NodeId> {
52    let entity = session.entity(id, id)?;
53    let view = BoundingBox::new(id, entity);
54    let corner_ref = view.corner()?;
55    let corner_entity = session.entity(id, corner_ref)?;
56    let raw = CartesianPoint::new(corner_ref, corner_entity).coordinates_3d()?;
57    let min_local = raw.map(|value| session.units().length(value));
58    // checked_dimensions rejects zero, negative and NaN extents: all three are
59    // malformed for IfcPositiveLengthMeasure, not degenerate-but-usable.
60    let dims = view.checked_dimensions()?;
61    let dims = dims.map(|value| session.units().length(value));
62
63    // Every one of the eight corners, not just min and max: under rotation the
64    // local minimum corner is generally NOT the world minimum corner, so a
65    // two-point transform produces an inverted or truncated box.
66    let mut bounds = Aabb::empty();
67    for xi in [0.0, dims[0]] {
68        for yi in [0.0, dims[1]] {
69            for zi in [0.0, dims[2]] {
70                let local = [min_local[0] + xi, min_local[1] + yi, min_local[2] + zi];
71                bounds.extend(Point3::from_array(frame.apply(local)));
72            }
73        }
74    }
75    session.node_for(id, GeometryNode::BoundingBox(bounds))
76}