Skip to main content

ifc_lite_processing/style/
indexed_colour.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//! `IfcIndexedColourMap` resolution (issue #913, Phase 2).
6//!
7//! Ported from the browser pipeline so the backend resolves the same authored
8//! colors. CATIA / 3DEXPERIENCE exports color tessellated geometry through
9//! `IFCINDEXEDCOLOURMAP` + `IFCCOLOURRGBLIST` with no `IFCSTYLEDITEM` chain;
10//! pre-fix the backend ignored them and fell back to the default type color
11//! (issue #663).
12//!
13//! Two levels of fidelity:
14//! - [`FullIndexedColourMap::dominant`] — one colour per face set, used to fill
15//!   the element style index (#663, the common single-colour case).
16//! - [`split_mesh_by_indexed_colour`] — one sub-mesh per palette group, so a
17//!   face set whose `ColourIndex` assigns different colours to different
18//!   triangles renders correctly (issue #858).
19
20use super::Rgba;
21use ifc_lite_core::{DecodedEntity, EntityDecoder};
22use ifc_lite_geometry::Mesh;
23
24/// A fully resolved `IfcIndexedColourMap`: the palette plus a per-triangle
25/// index into it (in `CoordIndex` order, which the triangulated-face-set
26/// processor preserves 1:1).
27#[derive(Debug, Clone)]
28pub struct FullIndexedColourMap {
29    /// The face set this map colours (`MappedTo`).
30    pub geometry_id: u32,
31    /// The colour palette (`IfcColourRgbList.ColourList`).
32    pub colours: Vec<Rgba>,
33    /// Per-triangle 0-based index into `colours`, one entry per triangle.
34    pub triangle_palette: Vec<usize>,
35}
36
37impl FullIndexedColourMap {
38    /// Number of distinct palette entries actually referenced by triangles.
39    pub(crate) fn distinct_used(&self) -> usize {
40        let mut seen = self.triangle_palette.clone();
41        seen.sort_unstable();
42        seen.dedup();
43        seen.len()
44    }
45
46    /// The most-frequently-referenced colour (single-colour maps return their
47    /// only colour). Used to fill the element style index.
48    pub fn dominant(&self) -> Rgba {
49        let mut counts: rustc_hash::FxHashMap<usize, u32> = rustc_hash::FxHashMap::default();
50        for &p in &self.triangle_palette {
51            *counts.entry(p).or_insert(0) += 1;
52        }
53        let idx = counts
54            .iter()
55            .max_by_key(|(_, c)| *c)
56            .map(|(&i, _)| i)
57            .unwrap_or(0);
58        self.colours.get(idx).copied().unwrap_or(Rgba::new(0.8, 0.8, 0.8, 1.0))
59    }
60}
61
62/// Resolve an `IfcIndexedColourMap` to its palette + per-triangle indices.
63///
64/// Schema (IFC4):
65/// - attr 0: `MappedTo` → `IfcTessellatedFaceSet`
66/// - attr 1: `Opacity` (optional `0..=1`, `1.0` when omitted)
67/// - attr 2: `Colours` → `IfcColourRgbList` (attr 0 = `ColourList`)
68/// - attr 3: `ColourIndex` → 1-based palette index per triangle
69pub fn resolve_indexed_colour_map_full(
70    entity: &DecodedEntity,
71    decoder: &mut EntityDecoder,
72) -> Option<FullIndexedColourMap> {
73    let geometry_id = entity.get_ref(0)?;
74    let opacity = entity
75        .get(1)
76        .and_then(|a| a.as_float())
77        .map(|v| v as f32)
78        .unwrap_or(1.0)
79        .clamp(0.0, 1.0);
80    let colours_id = entity.get_ref(2)?;
81    let index_attr = entity.get(3)?;
82    let index_list = index_attr.as_list()?;
83    if index_list.is_empty() {
84        return None;
85    }
86
87    let colours_entity = decoder.decode_by_id(colours_id).ok()?;
88    let colour_list = colours_entity.get(0)?.as_list()?;
89    let colours: Vec<Rgba> = colour_list
90        .iter()
91        .filter_map(|c| {
92            let rgb = c.as_list()?;
93            let r = rgb.first().and_then(|v| v.as_float())? as f32;
94            let g = rgb.get(1).and_then(|v| v.as_float())? as f32;
95            let b = rgb.get(2).and_then(|v| v.as_float())? as f32;
96            Some(Rgba::new(r, g, b, opacity))
97        })
98        .collect();
99    if colours.is_empty() {
100        return None;
101    }
102
103    let max_idx = colours.len() - 1;
104    let triangle_palette: Vec<usize> = index_list
105        .iter()
106        .map(|v| {
107            let one_based = v.as_int().unwrap_or(1).max(1) as usize;
108            (one_based - 1).min(max_idx)
109        })
110        .collect();
111
112    Some(FullIndexedColourMap {
113        geometry_id,
114        colours,
115        triangle_palette,
116    })
117}
118
119/// Split a flat-shaded mesh into one sub-mesh per palette group.
120///
121/// Returns `None` (caller keeps the single dominant-coloured mesh) unless the
122/// mesh triangle count matches `map.triangle_palette` exactly — a mismatch
123/// means CSG/void cutting changed the topology, so the per-triangle mapping no
124/// longer applies. Triangle `i` of the mesh corresponds to `CoordIndex[i]`
125/// because the triangulated-face-set processor preserves triangle order.
126pub fn split_mesh_by_indexed_colour(
127    mesh: &Mesh,
128    map: &FullIndexedColourMap,
129) -> Option<Vec<(Rgba, Mesh)>> {
130    let tri_count = mesh.indices.len() / 3;
131    if tri_count == 0 || tri_count != map.triangle_palette.len() {
132        return None;
133    }
134    if map.distinct_used() < 2 {
135        return None; // single colour — nothing to split
136    }
137
138    let has_normals = mesh.normals.len() == mesh.positions.len();
139    let rtc_applied = mesh.rtc_applied;
140
141    // One accumulator per palette entry; built lazily so empty groups vanish.
142    #[derive(Default)]
143    struct Group {
144        positions: Vec<f32>,
145        normals: Vec<f32>,
146        indices: Vec<u32>,
147    }
148    let mut groups: Vec<Option<Group>> = (0..map.colours.len()).map(|_| None).collect();
149
150    for (tri, &palette) in map.triangle_palette.iter().enumerate() {
151        // Defensive: drop the *whole* triangle if any of its three vertices is
152        // out of range — skipping a single vertex would emit a malformed
153        // 1- or 2-vertex triangle.
154        let tri_in_range = (0..3).all(|k| {
155            let vi = mesh.indices[tri * 3 + k] as usize;
156            vi * 3 + 2 < mesh.positions.len()
157        });
158        if !tri_in_range {
159            continue;
160        }
161
162        let group = groups[palette].get_or_insert_with(Group::default);
163        for k in 0..3 {
164            let vi = mesh.indices[tri * 3 + k] as usize;
165            let base = vi * 3;
166            let new_index = (group.positions.len() / 3) as u32;
167            group.positions.push(mesh.positions[base]);
168            group.positions.push(mesh.positions[base + 1]);
169            group.positions.push(mesh.positions[base + 2]);
170            if has_normals {
171                group.normals.push(mesh.normals[base]);
172                group.normals.push(mesh.normals[base + 1]);
173                group.normals.push(mesh.normals[base + 2]);
174            }
175            group.indices.push(new_index);
176        }
177    }
178
179    let out: Vec<(Rgba, Mesh)> = groups
180        .into_iter()
181        .enumerate()
182        .filter_map(|(palette, group)| {
183            let group = group?;
184            if group.indices.is_empty() {
185                return None;
186            }
187            let mesh = Mesh {
188                positions: group.positions,
189                normals: group.normals,
190                indices: group.indices,
191                rtc_applied,
192            };
193            Some((map.colours[palette], mesh))
194        })
195        .collect();
196
197    (out.len() >= 2).then_some(out)
198}
199
200#[cfg(test)]
201mod tests {
202    use super::{split_mesh_by_indexed_colour, FullIndexedColourMap};
203    use crate::style::Rgba;
204    use ifc_lite_geometry::Mesh;
205
206    #[test]
207    fn split_drops_out_of_range_triangle_without_partial_geometry() {
208        // 6 in-range vertices (0..=5); the third triangle references vertex 99,
209        // which is out of range. The split must drop that whole triangle, never
210        // emit a 1- or 2-vertex fragment.
211        let positions: Vec<f32> = (0..6).flat_map(|i| [i as f32, 0.0, 0.0]).collect();
212        let mesh = Mesh {
213            positions,
214            normals: Vec::new(),
215            indices: vec![0, 1, 2, 3, 4, 5, 0, 1, 99],
216            rtc_applied: false,
217        };
218        let map = FullIndexedColourMap {
219            geometry_id: 1,
220            colours: vec![Rgba::new(1.0, 0.0, 0.0, 1.0), Rgba::new(0.0, 1.0, 0.0, 1.0)],
221            // tri0 → red, tri1 → green, tri2 (out of range) → red
222            triangle_palette: vec![0, 1, 0],
223        };
224
225        let parts = split_mesh_by_indexed_colour(&mesh, &map)
226            .expect("two valid palette groups survive after dropping the OOB triangle");
227
228        let total_tris: usize = parts
229            .iter()
230            .map(|(_, m)| {
231                assert_eq!(m.indices.len() % 3, 0, "index buffer must be whole triangles");
232                assert_eq!(m.positions.len() % 3, 0, "positions must be whole vertices");
233                m.indices.len() / 3
234            })
235            .sum();
236        assert_eq!(
237            total_tris, 2,
238            "the out-of-range triangle must be dropped, not partially emitted"
239        );
240    }
241}