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    let origin = mesh.origin;
141
142    // One accumulator per palette entry; built lazily so empty groups vanish.
143    #[derive(Default)]
144    struct Group {
145        positions: Vec<f32>,
146        normals: Vec<f32>,
147        indices: Vec<u32>,
148    }
149    let mut groups: Vec<Option<Group>> = (0..map.colours.len()).map(|_| None).collect();
150
151    for (tri, &palette) in map.triangle_palette.iter().enumerate() {
152        // Defensive: drop the *whole* triangle if any of its three vertices is
153        // out of range — skipping a single vertex would emit a malformed
154        // 1- or 2-vertex triangle.
155        let tri_in_range = (0..3).all(|k| {
156            let vi = mesh.indices[tri * 3 + k] as usize;
157            vi * 3 + 2 < mesh.positions.len()
158        });
159        if !tri_in_range {
160            continue;
161        }
162
163        let group = groups[palette].get_or_insert_with(Group::default);
164        for k in 0..3 {
165            let vi = mesh.indices[tri * 3 + k] as usize;
166            let base = vi * 3;
167            let new_index = (group.positions.len() / 3) as u32;
168            group.positions.push(mesh.positions[base]);
169            group.positions.push(mesh.positions[base + 1]);
170            group.positions.push(mesh.positions[base + 2]);
171            if has_normals {
172                group.normals.push(mesh.normals[base]);
173                group.normals.push(mesh.normals[base + 1]);
174                group.normals.push(mesh.normals[base + 2]);
175            }
176            group.indices.push(new_index);
177        }
178    }
179
180    let out: Vec<(Rgba, Mesh)> = groups
181        .into_iter()
182        .enumerate()
183        .filter_map(|(palette, group)| {
184            let group = group?;
185            if group.indices.is_empty() {
186                return None;
187            }
188            let mesh = Mesh {
189                positions: group.positions,
190                normals: group.normals,
191                indices: group.indices,
192                rtc_applied,
193                origin,
194            };
195            Some((map.colours[palette], mesh))
196        })
197        .collect();
198
199    (out.len() >= 2).then_some(out)
200}
201
202#[cfg(test)]
203mod tests {
204    use super::{split_mesh_by_indexed_colour, FullIndexedColourMap};
205    use crate::style::Rgba;
206    use ifc_lite_geometry::Mesh;
207
208    #[test]
209    fn split_drops_out_of_range_triangle_without_partial_geometry() {
210        // 6 in-range vertices (0..=5); the third triangle references vertex 99,
211        // which is out of range. The split must drop that whole triangle, never
212        // emit a 1- or 2-vertex fragment.
213        let positions: Vec<f32> = (0..6).flat_map(|i| [i as f32, 0.0, 0.0]).collect();
214        let mesh = Mesh {
215            positions,
216            normals: Vec::new(),
217            indices: vec![0, 1, 2, 3, 4, 5, 0, 1, 99],
218            rtc_applied: false, 
219            origin: [0.0; 3],        };
220        let map = FullIndexedColourMap {
221            geometry_id: 1,
222            colours: vec![Rgba::new(1.0, 0.0, 0.0, 1.0), Rgba::new(0.0, 1.0, 0.0, 1.0)],
223            // tri0 → red, tri1 → green, tri2 (out of range) → red
224            triangle_palette: vec![0, 1, 0],
225        };
226
227        let parts = split_mesh_by_indexed_colour(&mesh, &map)
228            .expect("two valid palette groups survive after dropping the OOB triangle");
229
230        let total_tris: usize = parts
231            .iter()
232            .map(|(_, m)| {
233                assert_eq!(m.indices.len() % 3, 0, "index buffer must be whole triangles");
234                assert_eq!(m.positions.len() % 3, 0, "positions must be whole vertices");
235                m.indices.len() / 3
236            })
237            .sum();
238        assert_eq!(
239            total_tris, 2,
240            "the out-of-range triangle must be dropped, not partially emitted"
241        );
242    }
243}