Skip to main content

ifc_lite_processing/
element.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 per-element mesh production — THE single decision tree that turns
6//! one IFC product (or type-product RepresentationMap) into renderable meshes.
7//!
8//! Both pipelines run this exact code:
9//! - the native orchestrator (`processor.rs`) calls [`produce_element_meshes`]
10//!   from its rayon loop with a fresh seeded decoder + router per element;
11//! - the browser batch path (`wasm-bindings` `processGeometryBatch`) calls it
12//!   per job with a warm per-batch decoder + router.
13//!
14//! History: the two pipelines used to carry diverging inline copies of this
15//! tree, and fixes had to land twice (#858, #913, #957, #961, #1071). Any
16//! change to mesh-production behaviour belongs HERE, exactly once. The only
17//! sanctioned behavioural fork is [`TypeGeometryMode`] — a product
18//! requirement, not drift: an export must never duplicate type geometry,
19//! while the interactive viewer renders it tagged for its Model/Types switch.
20//!
21//! The converged decision tree (union of the strongest behaviours of both
22//! former copies):
23//!
24//! ```text
25//! representation gate (IfcAlignment exempt)
26//! ├─ TypeProduct job (#957): render each planned RepresentationMap
27//! │    (textures #961, geometry_class tag, styled-item colour)
28//! └─ Product job:
29//!    ├─ has openings → submesh-aware void cut (per-part colours survive)
30//!    ├─ else        → submesh path for ALL types (per-item colours,
31//!    │                per-item error skipping, #858 palette split per item)
32//!    └─ fallback chain when the submesh path produced nothing:
33//!         void-aware single mesh → plain element → element-level #858 split
34//!         → single coloured mesh
35//! ```
36
37use crate::style::{FullIndexedColourMap, GeometryStyleInfo};
38use crate::types::mesh::{MeshData, MeshTextureData, RawInstanceOccurrence};
39use ifc_lite_core::{DecodedEntity, EntityDecoder, IfcType};
40use ifc_lite_geometry::{
41    calculate_normals, compose_instance_world_row_major, orient_mesh_outward_verdict, BoolFailure,
42    GeometryHasher, GeometryRouter, Mesh, ResolvedTextureMap, SubMeshCollection,
43};
44use rustc_hash::{FxHashMap, FxHashSet};
45use std::collections::BTreeMap;
46
47use crate::processor::convert_mesh_to_site_local;
48
49/// The f32-collapse degenerate backstop, its per-element tally, and the reason
50/// that tally now gates the closure verdict. A CHILD module: it exists only to
51/// serve this file's produce/emit cycle.
52#[path = "element_degenerate.rs"]
53mod degenerate;
54mod element_color;
55use element_color::{find_indexed_colour_for_element, infer_opening_subpart_material_name};
56// Re-exported because these two have callers outside this module:
57// `find_geometry_item_color` from processor/color_layer.rs, and
58// `resolve_color_for_representation_map` from processor/jobs.rs.
59pub(crate) use element_color::{find_geometry_item_color, resolve_color_for_representation_map};
60
61/// Element-level metadata stamped on every produced [`MeshData`]. The native
62/// pipeline resolves these during its metadata phase; the browser passes
63/// `None` (its viewer gets metadata from the parser worker instead).
64#[derive(Debug, Clone, Default)]
65pub struct ElementMeshMetadata {
66    pub global_id: Option<String>,
67    pub name: Option<String>,
68    pub presentation_layer: Option<String>,
69    pub space_zone_properties: Option<BTreeMap<String, String>>,
70}
71
72/// What the job renders.
73#[derive(Debug, Clone)]
74pub enum ElementJobKind {
75    /// Ordinary product occurrence — walk its IfcProductDefinitionShape.
76    Product,
77    /// #957 type geometry: render these RepresentationMaps directly (baking
78    /// their MappingOrigin), each pre-tagged with its geometry_class
79    /// (1 = orphan, 2 = instanced). Produce the list with
80    /// [`plan_type_geometry`] — callers must not hand-roll the filter.
81    TypeProduct { rep_maps: Vec<(u32, u8)> },
82}
83
84/// One unit of mesh production.
85pub struct ElementMeshJob<'a> {
86    pub id: u32,
87    pub ifc_type: IfcType,
88    /// The decoded product (or type-product) entity. Callers decode it —
89    /// they own skip-set checks and decode-failure policy.
90    pub entity: &'a DecodedEntity,
91    pub kind: ElementJobKind,
92    /// Caller-resolved element fallback colour (direct style > material
93    /// chain > type default). `None` ⇒ `default_color_for_type`.
94    pub element_color: Option<[f32; 4]>,
95    pub metadata: Option<&'a ElementMeshMetadata>,
96}
97
98/// Read-only shared state for one production run. Every field is a borrow of
99/// `Sync` data, so `&MeshProductionContext` can be captured by a rayon
100/// closure (native) or used serially (wasm).
101pub struct MeshProductionContext<'a> {
102    /// Host element id → opening ids (post void-propagation / opening filter).
103    pub void_index: &'a FxHashMap<u32, Vec<u32>>,
104    /// Geometry item id → resolved style (styled-item index).
105    pub geometry_style_index: &'a FxHashMap<u32, GeometryStyleInfo>,
106    /// Geometry item id → full per-triangle palette (#858).
107    pub indexed_colour_full: &'a FxHashMap<u32, FullIndexedColourMap>,
108    /// Element id → material colour list (#407/#913 transparent/opaque
109    /// alternation). Empty map when the caller has no material chain data.
110    pub element_material_colors: &'a FxHashMap<u32, Vec<[f32; 4]>>,
111    /// Surface textures + UV maps keyed by face-set id (#961).
112    pub texture_index: &'a FxHashMap<u32, ResolvedTextureMap>,
113    /// Site-local rotation (native `site_local` coordinate space only).
114    /// `None` for the browser — its Z-up→Y-up swap happens at the FFI
115    /// boundary, after this function.
116    pub site_local_rotation: Option<&'a Vec<f64>>,
117}
118
119/// RTC-invariant per-element fingerprint configuration (#971/#924).
120#[derive(Debug, Clone, Copy)]
121pub struct GeometryHashConfig {
122    /// Quantization grid in metres.
123    pub tolerance: f64,
124    /// World-reconstruction offset added back to local positions (the batch
125    /// RTC when a shift was applied, else zeros) so the file's RTC choice
126    /// never registers as a geometry change.
127    pub world_rtc: [f64; 3],
128}
129
130#[derive(Debug, Clone, Copy, Default)]
131pub struct MeshProductionOptions {
132    /// `Some` ⇒ compute one fingerprint per element (browser diff feature).
133    /// Type-product jobs are never hashed (diffing type-library shapes is a
134    /// separate feature decision).
135    pub geometry_hash: Option<GeometryHashConfig>,
136}
137
138/// The #957 suppress-vs-tag decision — an explicit product-requirement fork,
139/// not drift. See [`plan_type_geometry`].
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub enum TypeGeometryMode {
142    /// Native/export: instanced types are suppressed entirely (an export must
143    /// never duplicate geometry); orphan maps emit with geometry_class 1.
144    SuppressInstanced,
145    /// Viewer: instanced types emit too, tagged geometry_class 2, so the
146    /// Model/Types view switch can filter at render time.
147    EmitTagged,
148}
149
150/// The single home of the #957 orphan/instanced RepresentationMap decision.
151///
152/// A map referenced by an `IfcMappedItem` always draws through its occurrence
153/// — emitting it again would double-render at the MappingOrigin (the
154/// AC20/ArchiCAD duplicate-boxes regression), so referenced maps are filtered
155/// in every mode. What remains is classified by whether the type has an
156/// occurrence (`IfcRelDefinesByType`): orphans are class 1 (part of the
157/// model — nothing else renders them), instanced types are class 2 (the
158/// type-library shape) and only emitted in [`TypeGeometryMode::EmitTagged`].
159pub fn plan_type_geometry(
160    rep_map_ids: &[u32],
161    referenced_representation_maps: &FxHashSet<u32>,
162    type_is_instantiated: bool,
163    mode: TypeGeometryMode,
164) -> Vec<(u32, u8)> {
165    if mode == TypeGeometryMode::SuppressInstanced && type_is_instantiated {
166        return Vec::new();
167    }
168    let class: u8 = if type_is_instantiated { 2 } else { 1 };
169    rep_map_ids
170        .iter()
171        .filter(|rm| !referenced_representation_maps.contains(rm))
172        .map(|rm| (*rm, class))
173        .collect()
174}
175
176/// Everything one element produced.
177pub struct ProducedElementMeshes {
178    pub meshes: Vec<MeshData>,
179    /// #1623 Phase 2 don't-bake output: this element's occurrences of a repeated
180    /// `IfcRepresentationMap` that skipped the per-occurrence materialize. Empty
181    /// unless the router was armed with an instancing plan
182    /// (`GeometryRouter::enable_output_instancing`); the streaming finalize resolves
183    /// each into a [`crate::InstanceRecord`] against the shared template MeshData.
184    pub instance_occurrences: Vec<RawInstanceOccurrence>,
185    /// Per-ELEMENT fingerprint, accumulated across all of the element's
186    /// meshes in the native IFC frame (pre-split, pre-site-rotation).
187    /// `None` when hashing is off, nothing was produced, or the job is a
188    /// TypeProduct.
189    pub geometry_hash: Option<u64>,
190    /// The same pass's world-space AABB, `[minx, miny, minz, maxx, maxy, maxz]`
191    /// in unquantized `f64` world coordinates (the file's RTC folded back in),
192    /// over every triangle corner the hasher saw. `Some` exactly when
193    /// [`Self::geometry_hash`] is `Some`, so the two stay index-parallel at the
194    /// FFI boundary.
195    ///
196    /// Why the diff engine needs it: the hash conflates moved / reshaped /
197    /// re-tessellated into one "different" bit. The box separates them — same
198    /// extent at a new centre is a MOVE, a different extent is a reshape, an
199    /// identical box with a different hash is retriangulation.
200    pub geometry_aabb: Option<[f64; 6]>,
201    /// The element's enclosed volume in m³ from the SAME pass — `Some` ONLY
202    /// when the produced geometry was provably a single closed orientable
203    /// solid, `None` otherwise (#1891). `None` is the common case for
204    /// material-layered walls, open `SurfaceModel` geometry, and any element
205    /// assembled from more than one representation item.
206    ///
207    /// Read `ifc_lite_geometry::GeometryHasher::volume` before widening any
208    /// clause of that gate: the alternative is not a slightly-off volume, it is
209    /// a confidently wrong one with nothing about it that looks wrong.
210    pub geometry_volume: Option<f64>,
211    /// The folded per-segment topology verdict behind [`Self::geometry_volume`]
212    /// — which clause held and which refused. `Some` exactly when
213    /// [`Self::geometry_hash`] is. A model checker wants it: "open shell" and
214    /// "multi-item assembly" are different findings with different fixes.
215    pub geometry_closure: Option<ifc_lite_geometry::GeometryClosure>,
216    /// CSG diagnostics recorded while producing THIS element, attributed by
217    /// product id. The router is fully drained on return, so a warm router
218    /// reused across a batch never leaks one element's failures into the
219    /// next. Failures from a superseded strategy (a fallback re-attempting
220    /// the same cuts) are discarded — only the path that produced the
221    /// returned meshes contributes.
222    pub csg_failures: FxHashMap<u32, Vec<BoolFailure>>,
223    /// Triangles dropped by the f32-collapse degenerate-triangle backstop
224    /// (see the `degenerate` child module) across ALL of this element's meshes.
225    /// Zero when the backstop is disabled or nothing was degenerate.
226    /// Request-local (scoped per `produce_element_meshes` call) so concurrent
227    /// passes never cross-contaminate. Non-zero also RETRACTS
228    /// [`Self::geometry_closure`] and [`Self::geometry_volume`] — the drop
229    /// happens after the verdict was taken and can open a certified shell.
230    pub degenerate_triangles_dropped: u64,
231}
232
233/// THE canonical per-element mesh producer.
234///
235/// Decoder and router are caller-supplied so each pipeline keeps its reuse
236/// policy: the native rayon loop builds a fresh seeded decoder + router per
237/// element; the browser batch path reuses one warm pair per batch. The
238/// decoder MUST have its unit-scale caches seeded
239/// (`EntityDecoder::seed_unit_scales`) — otherwise arc tessellation re-pays
240/// an O(file) IFCPROJECT scan per fresh decoder.
241pub fn produce_element_meshes(
242    job: &ElementMeshJob<'_>,
243    ctx: &MeshProductionContext<'_>,
244    opts: &MeshProductionOptions,
245    decoder: &mut EntityDecoder,
246    router: &GeometryRouter,
247) -> ProducedElementMeshes {
248    // Open a per-element CSG escalation scope (#1109). Every boolean this element
249    // issues (one per opening, plus clip cuts) accumulates into ONE deterministic
250    // budget, so a boolean-heavy element (a slab cut by 24+ openings, a Tekla
251    // member with stacked half-space clips) degrades as a UNIT — its remaining
252    // cuts bail to the #635 AABB fallback — instead of grinding the geometry
253    // stream past the 95% watchdog. The per-boolean cap alone could not see this
254    // distributed cost. Unbounded under the server/offline-export profile.
255    ifc_lite_geometry::kernel::budget::begin_element();
256
257    // Open this element's degenerate-backstop scope (same begin/drain shape as
258    // the kernel budget above); see the `degenerate` child module.
259    degenerate::begin_element();
260
261    let mut hasher = match (&job.kind, opts.geometry_hash) {
262        (ElementJobKind::Product, Some(cfg)) => {
263            Some(GeometryHasher::new(cfg.tolerance, cfg.world_rtc))
264        }
265        _ => None,
266    };
267
268    let (meshes, instance_occurrences) = produce_inner(job, ctx, decoder, router, &mut hasher);
269
270    // Drain the router's per-element CSG diagnostics on EVERY return path so
271    // a warm (batch-reused) router starts the next element clean.
272    let csg_failures = router.take_csg_failures();
273
274    // A hash with NO box is reachable and deliberately KEPT (a NaN axis hashes
275    // but never accumulates); `push_geometry_hash` reserves NaN slots so the FFI
276    // arrays still cannot misalign. Box-without-hash is impossible. VOLUME may
277    // likewise be `None` within an emitted entry (landing as NaN) — the normal
278    // answer for most elements. See `world_aabb` / `GeometryHasher::volume`.
279    let degenerate_triangles_dropped = degenerate::dropped_this_element();
280
281    // The verdict was taken where the orienter runs; `build_mesh_data` then ran
282    // the degenerate backstop over the same triangles, and a dropped triangle
283    // opens every neighbour along its three edges. Retract before reading, so
284    // what ships describes the mesh actually returned (see
285    // `retract_closure_if_mesh_edited`).
286    let (geometry_hash, geometry_aabb, geometry_volume, geometry_closure) = match hasher {
287        Some(mut h) if !h.is_empty() => {
288            h.retract_closure_if_mesh_edited(degenerate_triangles_dropped);
289            (Some(h.finish()), h.world_aabb(), h.volume(), Some(h.closure()))
290        }
291        _ => (None, None, None, None),
292    };
293
294    ProducedElementMeshes {
295        meshes,
296        instance_occurrences,
297        geometry_hash,
298        geometry_aabb,
299        geometry_volume,
300        geometry_closure,
301        csg_failures,
302        degenerate_triangles_dropped,
303    }
304}
305
306fn produce_inner(
307    job: &ElementMeshJob<'_>,
308    ctx: &MeshProductionContext<'_>,
309    decoder: &mut EntityDecoder,
310    router: &GeometryRouter,
311    hasher: &mut Option<GeometryHasher>,
312) -> (Vec<MeshData>, Vec<RawInstanceOccurrence>) {
313    // Representation gate, with the IfcAlignment exception: alignments carry
314    // their geometry on IfcAlignment*Segment children, so a null
315    // Representation attribute does not mean "nothing to render".
316    let has_representation = job.entity.get(6).is_some_and(|a| !a.is_null());
317    if !has_representation && job.ifc_type != IfcType::IfcAlignment {
318        return (Vec::new(), Vec::new());
319    }
320
321    let element_color = job
322        .element_color
323        .unwrap_or_else(|| crate::style::default_color_for_type(job.ifc_type).to_array());
324
325    if let ElementJobKind::TypeProduct { rep_maps } = &job.kind {
326        // Type-product geometry (orphan/instanced RepresentationMaps) never rides the
327        // don't-bake path — it is view-mode-gated by geometry_class, not instanced.
328        return (
329            produce_type_geometry(job, rep_maps, element_color, ctx, decoder, router),
330            Vec::new(),
331        );
332    }
333
334    let has_openings = ctx
335        .void_index
336        .get(&job.id)
337        .is_some_and(|openings| !openings.is_empty());
338
339    // Material-layer wall: tag its per-layer slices GEOM_CLASS_LAYER_SLICE so the
340    // 2D/section cut can split the cut into per-layer fills (one sub-mesh = one
341    // layer = one colour). Since #1311 the slices are OPEN bands whose union is
342    // the wall's watertight outer skin (no coincident interface caps), and the
343    // renderer draws them DOUBLE-SIDED like all other IFC geometry — IFC winding
344    // is not reliably outward, so the previous backface-culling of these slices
345    // dropped inward-wound faces and made the wall read hollow. The tag no longer
346    // drives any culling; it is purely the per-layer-fill marker.
347    let layer_class = if router.is_material_layer_sliceable(job.id) {
348        GEOM_CLASS_LAYER_SLICE
349    } else {
350        0
351    };
352
353    if has_openings {
354        // Voided elements: submesh-aware cut FIRST, so per-part colours
355        // survive the void subtraction (a voided window keeps frame/glass
356        // split; a voided multi-layer wall keeps its layer colours).
357        if let Ok(sub_meshes) =
358            router.process_element_with_submeshes_and_voids(job.entity, decoder, ctx.void_index)
359        {
360            if !sub_meshes.is_empty() {
361                let (out, occ) =
362                    emit_sub_meshes(job, sub_meshes, element_color, ctx, decoder, hasher, layer_class);
363                if !out.is_empty() || !occ.is_empty() {
364                    return (out, occ);
365                }
366            }
367        }
368    } else {
369        // Submesh path for ALL types: per-geometry-item colours (window glass
370        // transparency, multi-material doors) and per-item error skipping —
371        // one unsupported representation item no longer blanks the whole
372        // element (`process_element` aborts with `?`). #858 palette split
373        // happens per item inside `emit_sub_meshes`.
374        if let Ok(sub_meshes) =
375            router.process_element_with_submeshes_textured(job.entity, decoder, ctx.texture_index)
376        {
377            if !sub_meshes.is_empty() {
378                let (out, occ) =
379                    emit_sub_meshes(job, sub_meshes, element_color, ctx, decoder, hasher, layer_class);
380                // #1623 Phase 2: a pure don't-bake occurrence produces NO flat mesh
381                // (only instance placeholders); treat that as success so the fallback
382                // chain below does not re-materialize the element flat.
383                if !out.is_empty() || !occ.is_empty() {
384                    return (out, occ);
385                }
386            }
387        }
388    }
389
390    // Fallback chain. A superseding strategy is about to re-process this
391    // element's representation and re-attempt the same (deterministic)
392    // cuts/booleans; discard the abandoned attempt's diagnostics so
393    // re-failures aren't double-counted. (The voids→plain-element
394    // mini-fallback below intentionally keeps its records: a failed/emptying
395    // cut that leaves the host uncut IS the diagnostic.)
396    let _ = router.take_csg_failures();
397
398    let mut mesh_candidate = router
399        .process_element_with_voids(job.entity, decoder, ctx.void_index)
400        .ok();
401    let needs_fallback = match mesh_candidate.as_ref() {
402        // An empty void-cut result normally means the cut FAILED and emptied
403        // the host, so we re-render it un-cut. But when a containing void
404        // genuinely CONSUMED the host (`host_consumed_by_void`), the empty
405        // result is correct — keep it, or the un-cut host re-appears as a
406        // spurious solid.
407        Some(mesh) => mesh.is_empty() && !router.host_consumed_by_void(job.id),
408        None => true,
409    };
410    if needs_fallback {
411        mesh_candidate = router.process_element(job.entity, decoder).ok();
412    }
413
414    let Some(mut mesh) = mesh_candidate else {
415        return (Vec::new(), Vec::new());
416    };
417    if mesh.is_empty() {
418        return (Vec::new(), Vec::new());
419    }
420
421    // Make the assembled body consistently outward-wound. A faceted brep (IFC
422    // face loops are not reliably outward) or a merged multi-item body (extrusion
423    // unioned with a boolean cut) can carry MIXED winding that corrupts signed
424    // volume and the smooth normals computed below. No-op for already-consistent
425    // bodies (every extrusion), so their index buffer + normals are untouched; a
426    // flip invalidates any baked normals, so recompute them.
427    //
428    // The verdict rides along to the hasher below: this pass is the only place
429    // that knows whether the assembled body is a closed orientable solid, and
430    // without that a per-element volume cannot be emitted honestly (#1891).
431    let verdict = orient_mesh_outward_verdict(&mut mesh);
432    if verdict.flipped {
433        calculate_normals(&mut mesh);
434    }
435
436    // Multi-colour IfcIndexedColourMap → one mesh per palette group (#858),
437    // resolved by walking the element's representation for the colour-mapped
438    // face set. Only applies while the produced triangle count still matches
439    // the face set's CoordIndex (no CSG/void retopology) — the splitter
440    // guards this; otherwise the single dominant-coloured mesh below wins.
441    if !ctx.indexed_colour_full.is_empty() {
442        if let Some(full) =
443            find_indexed_colour_for_element(job.entity, ctx.indexed_colour_full, decoder)
444        {
445            let geometry_id = full.geometry_id;
446            if let Some(groups) = crate::style::split_mesh_by_indexed_colour(&mesh, full) {
447                if let Some(h) = hasher.as_mut() {
448                    // The palette split below only partitions triangles; the
449                    // verdict from the un-split body is the one that describes
450                    // this hashed buffer.
451                    h.add_oriented_mesh(&mesh.positions, &mesh.indices, mesh.origin, verdict);
452                }
453                let mut out: Vec<MeshData> = Vec::with_capacity(groups.len());
454                for (color, mut part) in groups {
455                    if part.normals.len() != part.positions.len() {
456                        calculate_normals(&mut part);
457                    }
458                    out.push(build_mesh_data(
459                        job,
460                        part,
461                        color.to_array(),
462                        None,
463                        Some(geometry_id),
464                        false,
465                        0,
466                        ctx,
467                        None,
468                    ));
469                }
470                if !out.is_empty() {
471                    return (out, Vec::new());
472                }
473            }
474        }
475    }
476
477    if mesh.normals.len() != mesh.positions.len() {
478        calculate_normals(&mut mesh);
479    }
480    if let Some(h) = hasher.as_mut() {
481        h.add_oriented_mesh(&mesh.positions, &mesh.indices, mesh.origin, verdict);
482    }
483    (
484        vec![build_mesh_data(job, mesh, element_color, None, None, false, 0, ctx, None)],
485        Vec::new(),
486    )
487}
488
489/// Emit a sub-mesh collection: per-item colour resolution through the
490/// canonical `resolve_submesh_color` precedence (#913 §4.2), material-name
491/// inference for window/door parts, and the #858 per-item palette split.
492fn emit_sub_meshes(
493    job: &ElementMeshJob<'_>,
494    sub_meshes: SubMeshCollection,
495    element_color: [f32; 4],
496    ctx: &MeshProductionContext<'_>,
497    decoder: &mut EntityDecoder,
498    hasher: &mut Option<GeometryHasher>,
499    // geometry_class stamped on every emitted sub-mesh. 0 for normal occurrence
500    // geometry; GEOM_CLASS_LAYER_SLICE (3) when these are the per-layer slices of
501    // a material-layer wall — a section-only detail the 3D renderer skips (the
502    // wall renders as one solid) but the 2D/section cut consumes.
503    slice_class: u8,
504) -> (Vec<MeshData>, Vec<RawInstanceOccurrence>) {
505    // Read ONCE, before the loop consumes the collection: what the ids MEAN is
506    // a property of the collection, not of any individual sub-mesh (#3199).
507    let ids_are_materials = sub_meshes.ids_are_materials;
508    let mut out: Vec<MeshData> = Vec::with_capacity(sub_meshes.len());
509    let mut occurrences: Vec<RawInstanceOccurrence> = Vec::new();
510    // Material colours for this element, used when a sub-mesh has no direct
511    // style — alternated so frame (opaque) and glazing (transparent) split
512    // across the window's parts (#913 §2.3).
513    let material_colors = ctx.element_material_colors.get(&job.id);
514    let mut mat_color_idx = 0usize;
515
516    for sub in sub_meshes.sub_meshes {
517        let mut sub_mesh = sub.mesh;
518        if sub_mesh.is_empty() {
519            // #1623 Phase 2 don't-bake: an EMPTY sub-mesh carrying instanceable
520            // InstanceMeta is a non-template occurrence of a shared template. Convert
521            // it to a RawInstanceOccurrence (resolving its colour EXACTLY as a
522            // materialized sub-mesh would, keyed on the same nested-solid geometry_id)
523            // instead of dropping it. `transform` was folded into `im.transform` by
524            // `apply_submesh_placement`; we compose the full pre-RTC world transform
525            // here and let the streaming finalize derive the template-relative mat4.
526            if let Some(im) = sub_mesh.instance_meta.as_ref().filter(|im| im.instanceable) {
527                let style = ctx.geometry_style_index.get(&sub.geometry_id);
528                let direct_color = style.map(|s| s.color).or_else(|| {
529                    find_geometry_item_color(sub.geometry_id, ctx.geometry_style_index, decoder)
530                });
531                let color = crate::style::resolve_submesh_color(
532                    direct_color,
533                    material_colors.map(|v| v.as_slice()),
534                    &mut mat_color_idx,
535                    element_color,
536                );
537                occurrences.push(RawInstanceOccurrence {
538                    express_id: job.id,
539                    ifc_type: job.ifc_type.name().to_string(),
540                    global_id: job.metadata.and_then(|m| m.global_id.clone()),
541                    name: job.metadata.and_then(|m| m.name.clone()),
542                    presentation_layer: job.metadata.and_then(|m| m.presentation_layer.clone()),
543                    color,
544                    rep_identity: im.rep_identity,
545                    world_transform: compose_instance_world_row_major(im),
546                });
547            }
548            continue;
549        }
550        // Consistently outward-wind each sub-body (see the single-mesh path); a
551        // flip invalidates baked normals, so recompute on flip or when absent.
552        // The verdict is per SUB-BODY, which is also the hasher's segment
553        // granularity, so closedness is attributed to exactly what it describes.
554        let verdict = orient_mesh_outward_verdict(&mut sub_mesh);
555        if verdict.flipped || sub_mesh.normals.len() != sub_mesh.positions.len() {
556            calculate_normals(&mut sub_mesh);
557        }
558
559        let style = ctx.geometry_style_index.get(&sub.geometry_id);
560        // Direct style wins; else chase IfcMappedItem so mapped sub-geometry
561        // inherits its underlying style (#913 §2.7).
562        let direct_color = style.map(|s| s.color).or_else(|| {
563            find_geometry_item_color(sub.geometry_id, ctx.geometry_style_index, decoder)
564        });
565        let color = crate::style::resolve_submesh_color(
566            direct_color,
567            material_colors.map(|v| v.as_slice()),
568            &mut mat_color_idx,
569            element_color,
570        );
571        let material_name = style
572            .and_then(|s| s.material_name.as_ref())
573            .map(ToString::to_string)
574            .or_else(|| infer_opening_subpart_material_name(&job.ifc_type, color, sub.geometry_id));
575
576        if let Some(h) = hasher.as_mut() {
577            h.add_oriented_mesh(&sub_mesh.positions, &sub_mesh.indices, sub_mesh.origin, verdict);
578        }
579
580        // Textured face set (#1781): thread the per-vertex UVs through the
581        // weld (kept 1:1 with positions, seams stay split) and attach the
582        // texture, mirroring the type-geometry path (#961). The length guard
583        // drops the texture instead of sampling garbage if any upstream step
584        // rebuilt vertices without maintaining the UV channel.
585        if let (Some(uvs), Some(texture)) = (sub.uvs, sub.texture.as_ref()) {
586            if uvs.len() / 2 == sub_mesh.positions.len() / 3 {
587                let mut mesh_data = build_mesh_data(
588                    job,
589                    sub_mesh,
590                    color,
591                    material_name,
592                    Some(sub.geometry_id),
593                    ids_are_materials,
594                    slice_class,
595                    ctx,
596                    Some(uvs),
597                );
598                mesh_data.texture = Some(MeshTextureData::from_attachment(texture));
599                out.push(mesh_data);
600                continue;
601            }
602        }
603
604        // #858: a face set with a per-triangle colour map splits into one
605        // mesh per palette group (guards inside the splitter: triangle count
606        // must still match, ≥2 distinct colours). Palette colours supersede
607        // the resolved style colour for the split parts.
608        if let Some(full) = ctx.indexed_colour_full.get(&sub.geometry_id) {
609            if let Some(groups) = crate::style::split_mesh_by_indexed_colour(&sub_mesh, full) {
610                for (rgba, mut part) in groups {
611                    if part.normals.len() != part.positions.len() {
612                        calculate_normals(&mut part);
613                    }
614                    out.push(build_mesh_data(
615                        job,
616                        part,
617                        rgba.to_array(),
618                        None,
619                        Some(sub.geometry_id),
620                        ids_are_materials,
621                        slice_class,
622                        ctx,
623                        None,
624                    ));
625                }
626                continue;
627            }
628        }
629
630        out.push(build_mesh_data(
631            job,
632            sub_mesh,
633            color,
634            material_name,
635            Some(sub.geometry_id),
636            ids_are_materials,
637            slice_class,
638            ctx,
639            None,
640        ));
641    }
642    (out, occurrences)
643}
644
645/// geometry_class for the per-layer slices of a material-layer wall. The wall's
646/// slices have verified outward winding, so the 3D renderer draws THIS class
647/// BACKFACE-CULLED — the build-up shows on the faces/edges but the interior
648/// coincident caps never rasterise, so the thin stacked solids don't z-fight
649/// into a hollow shell. The 2D/section cut consumes the same class (never
650/// culled) for its per-layer fills.
651pub const GEOM_CLASS_LAYER_SLICE: u8 = 3;
652
653/// Render a type-product's planned RepresentationMaps (#957), texture-aware
654/// (#961), each mesh tagged with its planned geometry_class.
655fn produce_type_geometry(
656    job: &ElementMeshJob<'_>,
657    rep_maps: &[(u32, u8)],
658    element_color: [f32; 4],
659    ctx: &MeshProductionContext<'_>,
660    decoder: &mut EntityDecoder,
661    router: &GeometryRouter,
662) -> Vec<MeshData> {
663    let mut out: Vec<MeshData> = Vec::new();
664    for &(rep_map_id, geometry_class) in rep_maps {
665        let Ok(rep_map) = decoder.decode_by_id(rep_map_id) else {
666            continue;
667        };
668        // One part per output mesh: each textured face set carries its own
669        // UVs + decoded image; untextured items merge into one part (#961).
670        let Ok(parts) =
671            router.process_representation_map_with_texture(&rep_map, decoder, ctx.texture_index)
672        else {
673            continue;
674        };
675        if parts.is_empty() {
676            continue;
677        }
678
679        let color =
680            resolve_color_for_representation_map(rep_map_id, ctx.geometry_style_index, decoder)
681                .unwrap_or(element_color);
682
683        for (mut mesh, uvs, texture) in parts {
684            if mesh.is_empty() {
685                continue;
686            }
687            if mesh.normals.len() != mesh.positions.len() {
688                calculate_normals(&mut mesh);
689            }
690            // Thread the per-vertex UVs through `build_mesh_data` so the source
691            // weld remaps them WITH the deduped positions (and keeps texture
692            // seams split). Only textured parts carry UVs; untextured parts pass
693            // `None` and get the full position+normal weld.
694            let part_uvs = if texture.is_some() { Some(uvs) } else { None };
695            let mut mesh_data =
696                build_mesh_data(job, mesh, color, None, None, false, geometry_class, ctx, part_uvs);
697            if let Some(tex) = texture {
698                // UVs were already welded onto `mesh_data`; attach only the
699                // texture (decoded image or #1781 external reference) here.
700                mesh_data.texture = Some(MeshTextureData::from_attachment(&tex));
701            }
702            out.push(mesh_data);
703        }
704    }
705    out
706}
707
708/// Construct the final [`MeshData`]: metadata stamp, style metadata,
709/// geometry-class tag, and the optional site-local rotation. ALWAYS the last
710/// step — geometry hashing happens before this (native IFC frame), which is why
711/// the degenerate drop below has to report what it removed: it edits a mesh the
712/// hasher has already ruled on.
713#[allow(clippy::too_many_arguments)] // distinct per-mesh funnel inputs
714fn build_mesh_data(
715    job: &ElementMeshJob<'_>,
716    mut mesh: Mesh,
717    color: [f32; 4],
718    material_name: Option<String>,
719    // The sub-mesh's source id, plus WHAT IT IS. Routed to `geometry_item_id`
720    // or `material_id` by `with_style_metadata`, never both (#3199).
721    source_id: Option<u32>,
722    id_is_material: bool,
723    geometry_class: u8,
724    ctx: &MeshProductionContext<'_>,
725    // Per-vertex texture coordinates (2 per vertex, 1:1 with `mesh.positions`),
726    // present only for textured type geometry (#961). Threaded through the weld
727    // so the UVs are remapped WITH the deduped positions and stay aligned; a UV
728    // difference also keeps a texture seam's coincident corners split.
729    uvs: Option<Vec<f32>>,
730) -> MeshData {
731    // Backstop for f32 vertex-storage collapse, at the single funnel for every
732    // element MeshData, tallying what it removed — `produce_element_meshes`
733    // drains that tally both into the result and into the closure retraction.
734    degenerate::clean(&mut mesh);
735    // Source vertex weld (see `mesh_weld::weld_indexed`): the faceted-brep
736    // mesher emits per-`IfcFace` geometry duplicating every shared corner once
737    // per incident face (~3-6x). Collapse coincident vertices (identical f32
738    // position + quantized normal + quantized UV) at this single per-element
739    // funnel — the normal/UV keys keep creases and texture seams split (flat
740    // shading, no torn textures), and UVs are remapped WITH the positions.
741    // `None` = nothing merged (already-welded swept solids): keep originals, no
742    // realloc; triangles, winding, and AABB unchanged either way.
743    let welded_uvs = match ifc_lite_geometry::mesh_weld::weld_indexed(
744        &mesh.positions,
745        &mesh.normals,
746        uvs.as_deref(),
747        &mesh.indices,
748    ) {
749        Some((wp, wn, wuv, wi)) => {
750            mesh.positions = wp;
751            mesh.normals = wn;
752            mesh.indices = wi;
753            wuv
754        }
755        None => uvs,
756    };
757    let mesh_origin = mesh.origin;
758    // Instancing: capture before the fields are moved into MeshData. A site-local
759    // rotation (below) re-transforms positions/origin and would invalidate the
760    // captured transform, so drop instancing when one is active (rare; conservative).
761    let instance = if ctx.site_local_rotation.is_none() {
762        mesh.instance_meta.take()
763    } else {
764        None
765    };
766    // Local bounds/placement transform (issue #1474): same caveat as instancing
767    // above — a site-local rotation re-transforms positions and would invalidate
768    // the captured placement, so drop both when one is active.
769    let (local_bounds, local_to_world) = if ctx.site_local_rotation.is_none() {
770        (mesh.local_bounds, mesh.local_to_world)
771    } else {
772        (None, None)
773    };
774    let mut mesh_data = MeshData::new(
775        job.id,
776        job.ifc_type.name().to_string(),
777        mesh.positions,
778        mesh.normals,
779        mesh.indices,
780        color,
781    )
782    .with_origin(mesh_origin)
783    .with_instance(instance)
784    .with_local_bounds(local_bounds)
785    .with_local_to_world(local_to_world);
786    if let Some(meta) = job.metadata {
787        mesh_data = mesh_data
788            .with_element_metadata(
789                meta.global_id.clone(),
790                meta.name.clone(),
791                meta.presentation_layer.clone(),
792            )
793            .with_properties(meta.space_zone_properties.clone());
794    }
795    if material_name.is_some() || source_id.is_some() {
796        mesh_data =
797            mesh_data.with_style_metadata(material_name, source_id, id_is_material);
798    }
799    if geometry_class != 0 {
800        mesh_data = mesh_data.with_geometry_class(geometry_class);
801    }
802    // Attach the welded UVs (kept 1:1 with the welded positions by the weld).
803    // The texture IMAGE is attached by the caller; here we only carry the
804    // per-vertex coordinates through the funnel so they can't desync.
805    mesh_data.uvs = welded_uvs;
806    convert_mesh_to_site_local(&mut mesh_data, ctx.site_local_rotation);
807    mesh_data
808}
809
810#[cfg(test)]
811#[path = "element_tests.rs"]
812mod tests;