Skip to main content

ifc_lite_processing/style/
mod.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//! Canonical IFC styling — the single source of truth for mesh colors,
6//! shared between the HTTP server, the native pipeline, and the browser-side
7//! WASM bindings (issue #913).
8//!
9//! This mirrors the [`crate::symbolic`] split: presentation logic that used
10//! to be copied into every consumer (`wasm-bindings`, `processing`,
11//! `apps/server`, the now-discontinued desktop app) lives here exactly once.
12//! See issue #913 for the design and rationale.
13//!
14//! Phase 0 (this commit) introduces only the two pieces with no decoder or
15//! geometry dependency — the canonical [`Rgba`] color type and the single
16//! [`default_color_for_type`] table — and wires nothing into the pipeline
17//! yet. The decoder-driven resolver (`StyleIndex`, `IfcStyledItem` /
18//! `IfcIndexedColourMap` / material-chain resolution) arrives in Phase 2.
19//!
20//! ## Canonical contracts
21//!
22//! - **`f32` is canonical; `u8` is transport-only.** Colors are [`Rgba`]
23//!   ([`f32; 4`], straight-alpha, `0.0..=1.0`) end-to-end. The browser's
24//!   8-bit SharedArrayBuffer transport is expressed *only* through
25//!   [`Rgba::to_rgba8`] / [`Rgba::from_rgba8`]; the backend stays exact.
26//!   (Decision §8.3 of the plan.)
27//! - **One default table.** [`default_color_for_type`] is the only
28//!   IFC-type → color map in Rust. A CI guard (Phase 1) fails the build if a
29//!   second one appears.
30
31use ifc_lite_core::IfcType;
32
33mod indexed_colour;
34mod material;
35mod surface;
36// Public styling resolvers — the single shared implementation that both the
37// native pipeline and the browser `wasm-bindings` call (issue #913, Phase 2e).
38// `split_mesh_by_indexed_colour` is also public so the browser `processGeometryBatch`
39// path can restore the per-triangle palette split it lost in the #874 mesh-pipeline
40// unification (issue #858) — keeping one shared splitter rather than a wasm copy.
41pub use indexed_colour::{
42    resolve_indexed_colour_map_full, split_mesh_by_indexed_colour, FullIndexedColourMap,
43};
44pub use material::{
45    build_element_material_colors, build_material_style_index, flatten_material_color_index,
46    pick_material_style_for_submesh, pick_opaque_first, resolve_material_ids,
47    resolve_submesh_color,
48};
49pub use surface::extract_surface_style_colors;
50
51/// Alpha at or above which a color is treated as opaque.
52///
53/// Used by submesh material selection (Phase 2) to prefer glass (transparent)
54/// vs frame (opaque) styles. Matches the browser's `TRANSPARENCY_ALPHA_THRESHOLD`.
55pub const TRANSPARENCY_ALPHA_THRESHOLD: f32 = 0.95;
56
57/// Resolved appearance of one geometry item (the value side of the
58/// styled-item index keyed by geometry express id).
59///
60/// Lives here — not in `processor.rs` — because it is shared by the native
61/// pipeline, the canonical per-element producer ([`crate::element`]), and the
62/// browser `wasm-bindings` batch path, which lifts its flat `(id, rgba8)`
63/// wire arrays into this richer form via [`GeometryStyleInfo::from_color`].
64#[derive(Debug, Clone)]
65pub struct GeometryStyleInfo {
66    /// Apparent colour for rendering: IfcSurfaceStyleRendering.DiffuseColour
67    /// when authored, otherwise the SurfaceColour. Matches what most IFC
68    /// viewers display.
69    pub color: [f32; 4],
70    /// SurfaceColour, populated only when the file authored a distinct
71    /// DiffuseColour. Read by the WASM bridge's parallel extractor so the
72    /// GLB exporter can offer "Shading" as a colour source; the
73    /// processing-crate `MeshData` doesn't propagate it (server pipeline
74    /// has no GLB consumer yet).
75    pub shading_color: Option<[f32; 4]>,
76    pub material_name: Option<String>,
77}
78
79impl GeometryStyleInfo {
80    /// Lift a bare RGBA colour (e.g. from the browser prepass's flat
81    /// `styleIds`/`styleColors` wire arrays) into the rich form. No shading
82    /// colour, no material name — exactly the fidelity the wire carries.
83    pub fn from_color(color: [f32; 4]) -> Self {
84        Self {
85            color,
86            shading_color: None,
87            material_name: None,
88        }
89    }
90}
91
92/// Canonical straight-alpha RGBA color, components in `0.0..=1.0`.
93///
94/// Serializes transparently as a bare `[f32; 4]` JSON array, so it is a
95/// drop-in replacement for the `[f32; 4]` colors used across the pipeline
96/// today (e.g. `MeshData.color`).
97#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
98#[serde(transparent)]
99pub struct Rgba(pub [f32; 4]);
100
101impl Rgba {
102    /// Construct from components.
103    pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
104        Rgba([r, g, b, a])
105    }
106
107    /// Construct from a raw `[f32; 4]`.
108    pub const fn from_array(c: [f32; 4]) -> Self {
109        Rgba(c)
110    }
111
112    /// The underlying `[f32; 4]`.
113    pub const fn to_array(self) -> [f32; 4] {
114        self.0
115    }
116
117    /// The alpha component.
118    pub const fn alpha(self) -> f32 {
119        self.0[3]
120    }
121
122    /// `true` when alpha is below [`TRANSPARENCY_ALPHA_THRESHOLD`].
123    pub fn is_transparent(self) -> bool {
124        self.0[3] < TRANSPARENCY_ALPHA_THRESHOLD
125    }
126
127    /// Quantize to 8-bit RGBA for the browser SAB transport.
128    ///
129    /// Components are clamped to `0.0..=1.0` then rounded to nearest 1/255.
130    /// This is the *only* sanctioned quantization point (plan §8.3); the
131    /// backend never calls it.
132    pub fn to_rgba8(self) -> [u8; 4] {
133        let q = |c: f32| (c.clamp(0.0, 1.0) * 255.0).round() as u8;
134        [q(self.0[0]), q(self.0[1]), q(self.0[2]), q(self.0[3])]
135    }
136
137    /// Reconstruct from 8-bit RGBA (the inverse of [`Rgba::to_rgba8`],
138    /// modulo the 1/255 quantization step).
139    pub fn from_rgba8(c: [u8; 4]) -> Self {
140        Rgba([
141            c[0] as f32 / 255.0,
142            c[1] as f32 / 255.0,
143            c[2] as f32 / 255.0,
144            c[3] as f32 / 255.0,
145        ])
146    }
147}
148
149impl From<[f32; 4]> for Rgba {
150    fn from(c: [f32; 4]) -> Self {
151        Rgba(c)
152    }
153}
154
155impl From<Rgba> for [f32; 4] {
156    fn from(c: Rgba) -> Self {
157        c.0
158    }
159}
160
161/// The canonical default color for an IFC type.
162///
163/// This is the **union** of the historical `wasm-bindings` and `processing`
164/// tables (plan §8.1): every type keeps the value from whichever table
165/// defined it, and `IfcFurnishingElement` resolves to the browser's lighter
166/// wood (the value users see today). Types not listed fall through to neutral
167/// gray.
168pub fn default_color_for_type(ifc_type: IfcType) -> Rgba {
169    match ifc_type {
170        // Walls — light gray
171        IfcType::IfcWall | IfcType::IfcWallStandardCase => Rgba::new(0.85, 0.85, 0.85, 1.0),
172
173        // Slabs — darker gray
174        IfcType::IfcSlab => Rgba::new(0.7, 0.7, 0.7, 1.0),
175
176        // Roofs — brown-ish
177        IfcType::IfcRoof => Rgba::new(0.6, 0.5, 0.4, 1.0),
178
179        // Columns / beams / members — steel gray
180        IfcType::IfcColumn | IfcType::IfcBeam | IfcType::IfcMember => Rgba::new(0.6, 0.65, 0.7, 1.0),
181
182        // Windows — light blue, transparent
183        IfcType::IfcWindow => Rgba::new(0.6, 0.8, 1.0, 0.4),
184
185        // Doors — wood brown
186        IfcType::IfcDoor => Rgba::new(0.6, 0.45, 0.3, 1.0),
187
188        // Stairs (incl. stair flights — from the processing table)
189        IfcType::IfcStair | IfcType::IfcStairFlight => Rgba::new(0.75, 0.75, 0.75, 1.0),
190
191        // Railings
192        IfcType::IfcRailing => Rgba::new(0.4, 0.4, 0.45, 1.0),
193
194        // Plates / coverings
195        IfcType::IfcPlate | IfcType::IfcCovering => Rgba::new(0.8, 0.8, 0.8, 1.0),
196
197        // Curtain walls — glass blue (from the wasm table)
198        IfcType::IfcCurtainWall => Rgba::new(0.5, 0.7, 0.9, 0.5),
199
200        // Furniture — light wood (from the wasm table; §8.1)
201        IfcType::IfcFurnishingElement => Rgba::new(0.7, 0.55, 0.4, 1.0),
202
203        // Spaces — cyan, transparent
204        IfcType::IfcSpace => Rgba::new(0.2, 0.85, 1.0, 0.3),
205
206        // Spatial zones (modelled GFA volumes) — violet, transparent. A
207        // distinct hue from IfcSpace's cyan so net (room) vs gross (zone)
208        // areas read apart when both are shown (#1075).
209        IfcType::IfcSpatialZone => Rgba::new(0.72, 0.35, 0.95, 0.28),
210
211        // Opening elements — red-orange, transparent
212        IfcType::IfcOpeningElement => Rgba::new(1.0, 0.42, 0.29, 0.4),
213
214        // Site — green
215        IfcType::IfcSite => Rgba::new(0.4, 0.8, 0.3, 1.0),
216
217        // Building element proxy — generic gray (from the processing table)
218        IfcType::IfcBuildingElementProxy => Rgba::new(0.6, 0.6, 0.6, 1.0),
219
220        // Default — neutral gray
221        _ => Rgba::new(0.8, 0.8, 0.8, 1.0),
222    }
223}
224
225#[cfg(test)]
226mod tests {
227    use super::*;
228
229    #[test]
230    fn rgba_array_round_trip() {
231        let c = Rgba::new(0.1, 0.2, 0.3, 0.4);
232        assert_eq!(c.to_array(), [0.1, 0.2, 0.3, 0.4]);
233        assert_eq!(Rgba::from_array([0.1, 0.2, 0.3, 0.4]), c);
234        let back: [f32; 4] = c.into();
235        assert_eq!(back, [0.1, 0.2, 0.3, 0.4]);
236        assert_eq!(Rgba::from([0.5, 0.6, 0.7, 0.8]).alpha(), 0.8);
237    }
238
239    #[test]
240    fn quantization_clamps_and_rounds() {
241        assert_eq!(Rgba::new(0.0, 1.0, 0.5, 1.0).to_rgba8(), [0, 255, 128, 255]);
242        // out-of-range components clamp, not wrap
243        assert_eq!(Rgba::new(-0.5, 1.5, 0.0, 2.0).to_rgba8(), [0, 255, 0, 255]);
244    }
245
246    #[test]
247    fn quantization_within_one_step() {
248        // Every 8-bit round trip stays within the documented 1/255 tolerance.
249        for raw in [0.0_f32, 0.123, 0.4, 0.42, 0.5, 0.555, 0.95, 1.0] {
250            let back = Rgba::from_rgba8(Rgba::new(raw, raw, raw, raw).to_rgba8()).to_array();
251            assert!((back[0] - raw).abs() <= 1.0 / 255.0, "drift too large for {raw}");
252        }
253    }
254
255    #[test]
256    fn transparency_threshold() {
257        assert!(Rgba::new(0.6, 0.8, 1.0, 0.4).is_transparent());
258        assert!(!Rgba::new(0.85, 0.85, 0.85, 1.0).is_transparent());
259        // exactly the threshold counts as opaque
260        assert!(!Rgba::new(0.0, 0.0, 0.0, TRANSPARENCY_ALPHA_THRESHOLD).is_transparent());
261    }
262
263    #[test]
264    fn defaults_cover_known_types() {
265        assert_eq!(
266            default_color_for_type(IfcType::IfcWall).to_array(),
267            [0.85, 0.85, 0.85, 1.0]
268        );
269        // IfcWindow is transparent by default
270        assert!(default_color_for_type(IfcType::IfcWindow).is_transparent());
271        // unmapped type → neutral gray fallback
272        assert_eq!(
273            default_color_for_type(IfcType::IfcProject).to_array(),
274            [0.8, 0.8, 0.8, 1.0]
275        );
276    }
277
278    #[test]
279    fn union_resolves_the_four_contested_types() {
280        // The four types that diverged between the historical tables (§2.2).
281        assert_eq!(
282            default_color_for_type(IfcType::IfcCurtainWall).to_array(),
283            [0.5, 0.7, 0.9, 0.5],
284            "curtain wall = wasm glass blue"
285        );
286        assert_eq!(
287            default_color_for_type(IfcType::IfcStairFlight).to_array(),
288            [0.75, 0.75, 0.75, 1.0],
289            "stair flight = processing gray (grouped with IfcStair)"
290        );
291        assert_eq!(
292            default_color_for_type(IfcType::IfcBuildingElementProxy).to_array(),
293            [0.6, 0.6, 0.6, 1.0],
294            "proxy = processing gray"
295        );
296        assert_eq!(
297            default_color_for_type(IfcType::IfcFurnishingElement).to_array(),
298            [0.7, 0.55, 0.4, 1.0],
299            "furnishing = wasm light wood, not processing's darker brown"
300        );
301        // IfcStair and IfcStairFlight must agree.
302        assert_eq!(
303            default_color_for_type(IfcType::IfcStair),
304            default_color_for_type(IfcType::IfcStairFlight)
305        );
306    }
307}