ifc_lite_geometry/
styled_parts.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! A styled walk's sub-meshes paired with the `IfcMappedItem` occurrences each
6//! was produced under (#10698).
7//!
8//! One shared item placed by two occurrences is two sub-meshes with the same
9//! `geometry_id`; only the occurrence chain tells them apart, and a style on
10//! an occurrence colours its own sub-mesh only. The public [`SubMesh`] and
11//! [`SubMeshCollection`] stay as they are (their fields are public, so a new
12//! one would break callers that build them), and the chain travels here.
13//!
14//! INVARIANT: a chain is a field of its sub-mesh's own [`StyledPart`]. It is
15//! created with the sub-mesh and moves, is cut, dropped and rebuilt with it;
16//! no code holds one without the other, so no index can fall out of step. The
17//! public collection is made only by [`StyledParts::into_collection`], an
18//! order-preserving 1:1 projection, the same one [`StyledSubMeshes`] exposes.
19
20use crate::mesh::{Mesh, SubMesh, SubMeshCollection};
21
22/// One sub-mesh and its occurrence chain.
23#[derive(Debug, Clone)]
24pub(crate) struct StyledPart {
25    pub(crate) sub: SubMesh,
26    /// The `IfcMappedItem` ids this sub-mesh was produced under, inner to
27    /// outer; empty for a direct item, a layer slice or a material id.
28    pub(crate) occurrences: Vec<u32>,
29}
30
31impl StyledPart {
32    pub(crate) fn direct(sub: SubMesh) -> Self {
33        Self { sub, occurrences: Vec::new() }
34    }
35}
36
37/// The styled walk's carrier: [`SubMeshCollection`]'s rules (an empty mesh is
38/// not added; `ids_are_materials` is uniform), one [`StyledPart`] per entry.
39#[derive(Debug, Clone, Default)]
40pub(crate) struct StyledParts {
41    pub(crate) parts: Vec<StyledPart>,
42    pub(crate) ids_are_materials: bool,
43}
44
45impl StyledParts {
46    pub(crate) fn new() -> Self {
47        Self::default()
48    }
49
50    /// Add a direct sub-mesh; an empty mesh is dropped, as
51    /// [`SubMeshCollection::add`] drops it.
52    pub(crate) fn add(&mut self, geometry_id: u32, mesh: Mesh) {
53        if !mesh.is_empty() {
54            self.parts.push(StyledPart::direct(SubMesh::new(geometry_id, mesh)));
55        }
56    }
57
58    /// [`Self::add`] for a textured sub-mesh.
59    pub(crate) fn add_textured(
60        &mut self,
61        geometry_id: u32,
62        mesh: Mesh,
63        uvs: Vec<f32>,
64        texture: crate::processors::texture::TextureAttachment,
65    ) {
66        if !mesh.is_empty() {
67            self.parts.push(StyledPart::direct(SubMesh::textured(geometry_id, mesh, uvs, texture)));
68        }
69    }
70
71    pub(crate) fn len(&self) -> usize {
72        self.parts.len()
73    }
74
75    pub(crate) fn is_empty(&self) -> bool {
76        self.parts.is_empty()
77    }
78
79    /// The sub-meshes from `start` on, for an in-place pass (a bake, a
80    /// placement, a weld) that changes geometry and never the chain.
81    pub(crate) fn subs_mut(&mut self, start: usize) -> impl Iterator<Item = &mut SubMesh> {
82        self.parts.get_mut(start..).unwrap_or_default().iter_mut().map(|part| &mut part.sub)
83    }
84
85    /// The chains from `start` on: a closing mapping level appends its own
86    /// occurrence to everything produced under it.
87    pub(crate) fn chains_mut(&mut self, start: usize) -> impl Iterator<Item = &mut Vec<u32>> {
88        self.parts.get_mut(start..).unwrap_or_default().iter_mut().map(|part| &mut part.occurrences)
89    }
90
91    /// A don't-bake occurrence's placeholder: its mesh is intentionally empty
92    /// (the instance metadata is the payload), so it bypasses [`Self::add`]'s
93    /// empty-mesh drop, and its chain starts at its own occurrence `item_id`.
94    pub(crate) fn push_placeholder(&mut self, sub: SubMesh, item_id: u32) {
95        self.parts.push(StyledPart { sub, occurrences: vec![item_id] });
96    }
97
98    /// The public collection: each sub-mesh in order, chains dropped.
99    pub(crate) fn into_collection(self) -> SubMeshCollection {
100        SubMeshCollection {
101            sub_meshes: self.parts.into_iter().map(|part| part.sub).collect(),
102            ids_are_materials: self.ids_are_materials,
103        }
104    }
105}
106
107/// A styled host's parts while the openings cut them one at a time. `take`
108/// lends the next part's mesh out to be cut and keeps the part itself (its id
109/// and chain); `keep` puts the cut mesh back into that part. A cut rebuilds
110/// vertices, so UVs and texture are dropped (#1781), and an empty cut result
111/// is dropped with its part.
112pub(crate) struct CutQueue {
113    rest: std::vec::IntoIter<StyledPart>,
114    cutting: Option<StyledPart>,
115    done: StyledParts,
116}
117
118impl CutQueue {
119    pub(crate) fn new(parts: StyledParts) -> Self {
120        let done = StyledParts { parts: Vec::new(), ids_are_materials: parts.ids_are_materials };
121        Self { rest: parts.parts.into_iter(), cutting: None, done }
122    }
123
124    pub(crate) fn take(&mut self) -> Option<(u32, Mesh)> {
125        let mut part = self.rest.next()?;
126        let taken = (part.sub.geometry_id, std::mem::take(&mut part.sub.mesh));
127        self.cutting = Some(part);
128        Some(taken)
129    }
130
131    /// `id` is the id [`Self::take`] lent the mesh out under.
132    pub(crate) fn keep(&mut self, id: u32, mesh: Mesh) {
133        if let Some(part) = self.cutting.take().filter(|_| !mesh.is_empty()) {
134            let sub = SubMesh::new(id, mesh);
135            self.done.parts.push(StyledPart { sub, occurrences: part.occurrences });
136        }
137    }
138
139    pub(crate) fn into_done(self) -> StyledParts {
140        self.done
141    }
142}
143
144/// Layer slices and other chain-free output join the styled walk with empty
145/// chains: they are produced under no `IfcMappedItem`.
146impl From<SubMeshCollection> for StyledParts {
147    fn from(collection: SubMeshCollection) -> Self {
148        Self {
149            parts: collection.sub_meshes.into_iter().map(StyledPart::direct).collect(),
150            ids_are_materials: collection.ids_are_materials,
151        }
152    }
153}
154
155/// An element's sub-meshes, each with the `IfcMappedItem` occurrences it was
156/// produced under (#10698), from the `*_styled_*` [`GeometryRouter`] entries.
157///
158/// The sub-meshes are exactly those the matching plain entry returns, in the
159/// same order. [`Self::iter`] pairs each sub-mesh with its occurrence chain,
160/// the mapped items it was produced under, inner to outer. A style on one of
161/// them styles this occurrence of the shared geometry, so two sub-meshes with
162/// the same `geometry_id` can be coloured apart. The chain is empty for a
163/// direct item, a material-layer slice (`ids_are_materials`) and a cut part of
164/// either.
165///
166/// [`GeometryRouter`]: crate::GeometryRouter
167#[derive(Debug, Clone, Default)]
168#[non_exhaustive]
169pub struct StyledSubMeshes {
170    parts: StyledParts,
171}
172
173impl StyledSubMeshes {
174    pub(crate) fn new(parts: StyledParts) -> Self {
175        Self { parts }
176    }
177
178    /// Number of sub-meshes.
179    pub fn len(&self) -> usize {
180        self.parts.len()
181    }
182
183    /// Whether there are no sub-meshes.
184    pub fn is_empty(&self) -> bool {
185        self.parts.is_empty()
186    }
187
188    /// Whether `SubMesh::geometry_id` is an `IfcMaterial` id rather than an
189    /// `IfcRepresentationItem` id ([`SubMeshCollection::ids_are_materials`]).
190    pub fn ids_are_materials(&self) -> bool {
191        self.parts.ids_are_materials
192    }
193
194    /// Each sub-mesh with its occurrence chain, in order.
195    pub fn iter(&self) -> impl Iterator<Item = (&SubMesh, &[u32])> {
196        self.parts.parts.iter().map(|part| (&part.sub, part.occurrences.as_slice()))
197    }
198
199    /// Each sub-mesh with its occurrence chain, in order, by value.
200    pub fn into_iter_with_occurrences(self) -> impl Iterator<Item = (SubMesh, Vec<u32>)> {
201        self.parts.parts.into_iter().map(|part| (part.sub, part.occurrences))
202    }
203
204    /// The plain collection the matching plain entry returns.
205    pub fn into_collection(self) -> SubMeshCollection {
206        self.parts.into_collection()
207    }
208}
209
210#[cfg(test)]
211impl crate::program::GeometryProgram<'_> {
212    /// `drain_styled_parts` projected to the public collection, for tests.
213    pub(crate) fn drain_styled(
214        self,
215        router: &crate::GeometryRouter,
216        decoder: &mut ifc_lite_core::EntityDecoder,
217    ) -> crate::Result<SubMeshCollection> {
218        self.drain_styled_parts(router, decoder).map(StyledParts::into_collection)
219    }
220}