ifc_geometry/compile/items.rs
1//! Per-item meshes: one gross mesh for each leaf item of a Body (#656).
2
3use axiolid_contracts::ExecutionOptions;
4use axiolid_mesh_compile_contract::MeshCompiler;
5use axiolid_model::GeometryNode;
6use ifc_model::{EntityId, Model};
7
8use super::{refused, MeshClosure, Tolerance, TriMesh};
9use crate::error::GeometryResult;
10use crate::lower::items::lower_product_leaves;
11use crate::lower::LoweringSession;
12use crate::units;
13
14/// One leaf item of a product's Body, meshed on its own (#656).
15#[derive(Debug, Clone)]
16#[non_exhaustive]
17pub struct ItemMesh {
18 /// The representation item; never an `IfcMappedItem`.
19 pub item: EntityId,
20 /// The `IfcMappedItem`s it was reached through, outermost first; empty
21 /// when it is authored directly in the Body.
22 pub mapped_by: Vec<EntityId>,
23 /// World-space triangles in metres, this occurrence's placement applied.
24 pub mesh: TriMesh,
25 /// As [`CompiledMesh::closure`](super::CompiledMesh::closure).
26 pub closure: MeshClosure,
27}
28
29/// Mesh each leaf item of a product's Body on its own, gross (#656).
30///
31/// Uses [`default_backend`](super::default_backend);
32/// [`compile_product_item_meshes_with`] takes your own.
33///
34/// # Errors
35///
36/// As [`compile_product_item_meshes_with`].
37#[cfg(feature = "compile-reference-backend")]
38pub fn compile_product_item_meshes(
39 model: &Model,
40 product: EntityId,
41 tolerance: Tolerance,
42) -> GeometryResult<Option<Vec<ItemMesh>>> {
43 compile_product_item_meshes_with(&super::default_backend(), model, product, tolerance)
44}
45
46/// Mesh each leaf item of a product's Body with a caller-supplied backend.
47///
48/// A viewer that colours per item needs one mesh per styled item, in the
49/// order its colours come: index `i` here and index `i` of
50/// `ColourResolver::item_colours` name the same `item`.
51///
52/// # Return values
53///
54/// * `Ok(None)`: the product has no Body representation, in the cases where
55/// [`compile_product_mesh_with`](super::compile_product_mesh_with) returns
56/// `None`.
57/// * `Ok(Some(vec![]))`: the Body's items expand to no leaves. Unlike this,
58/// `compile_product_mesh_with` returns `Ok(None)` for an empty `Items`.
59///
60/// # Order
61///
62/// One entry per leaf, in authored `Items` order, depth first, every
63/// `IfcMappedItem` expanded through `MappingSource.MappedRepresentation`.
64/// Every other item is a leaf, including booleans, CSG and geometric sets.
65/// The representation is the one `select_product_representation` picks for
66/// the Body. Two occurrences of one map give two entries with the same
67/// `item` and different `mapped_by`.
68///
69/// # Geometry
70///
71/// Each leaf compiles the nodes and transforms the whole Body does, so for a
72/// single-item Body without maps the mesh equals
73/// [`compile_product_mesh_with`](super::compile_product_mesh_with)'s bit for
74/// bit. For several items the triangle counts sum to the product mesh's and
75/// the bounds join to its bounds. The meshes are gross: openings are not
76/// subtracted (ADR 0014). A curve leaf is compiled as the backend treats a
77/// one-item Body of that curve.
78///
79/// # Errors
80///
81/// All or nothing; no partial list is returned. Any lowering error of any
82/// leaf is returned unchanged (`MissingEntity`, `Unsupported`, `CyclicChain`
83/// or `ChainTooDeep` of kind `"mapped item"`, units errors), as
84/// `compile_product_mesh_with` raises them. A backend refusal of any leaf is
85/// [`GeometryError::CompilationRefused`](crate::error::GeometryError::CompilationRefused)
86/// naming `product`, or its seam-window variants.
87///
88/// # Cost
89///
90/// One lowering per product and one compilation per leaf. A map's source is
91/// lowered once per product and instanced.
92pub fn compile_product_item_meshes_with<B: MeshCompiler>(
93 backend: &B,
94 model: &Model,
95 product: EntityId,
96 tolerance: Tolerance,
97) -> GeometryResult<Option<Vec<ItemMesh>>> {
98 let scale = units::resolve(model)?;
99 let mut session = LoweringSession::new(model, &scale);
100 let Some(leaves) = lower_product_leaves(&mut session, product)? else {
101 return Ok(None);
102 };
103 if leaves.is_empty() {
104 return Ok(Some(Vec::new()));
105 }
106 // `finish` needs one root; the leaves are compiled from their own.
107 let root = if let [only] = leaves.as_slice() {
108 only.root
109 } else {
110 let mut distinct: Vec<_> = leaves.iter().map(|leaf| leaf.root).collect();
111 distinct.sort();
112 distinct.dedup();
113 session.node_for(product, GeometryNode::Collection(distinct))?
114 };
115 let lowered = session.finish(root)?;
116
117 let options = ExecutionOptions::new(tolerance);
118 leaves
119 .into_iter()
120 .map(|leaf| {
121 backend
122 .compile_mesh_reported(&lowered.graph, leaf.root, &options)
123 .map(|outcome| ItemMesh {
124 item: leaf.item,
125 mapped_by: leaf.mapped_by,
126 mesh: outcome.mesh,
127 closure: outcome.closure,
128 })
129 .map_err(|error| refused(product, error, &lowered.provenance))
130 })
131 .collect::<GeometryResult<Vec<_>>>()
132 .map(Some)
133}