ifc_lite_processing/processor/
sink.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//! [`ProcessingSink`]: what a pass tells its caller while it runs.
6//!
7//! Every event has a no-op default, so a consumer implements the ones it
8//! reads. `()` is the sink that listens to nothing.
9//!
10//! Ordering contract (pinned by `tests/entry_point_trace.rs`):
11//! `on_omitted_type_records` fires at most once, before everything else, and
12//! only when the scan omitted a type record. Then
13//! `on_bootstrap` fires at most once, then `on_frame` exactly once, then any
14//! number of `on_batch` interleaved with `on_color_update`. `on_frame` fires
15//! before the first `on_batch`, also for a file with no jobs. A
16//! `on_color_update` replays deferred styles after the first batch, never
17//! before it. Events fire once per chunk, never per element. With
18//! `Features::finishes` on, `on_finishes` fires immediately before every
19//! `on_batch`, also for a batch whose meshes author no finish.
20//! Recovered instances are emitted as a final mesh batch. After all mesh
21//! batches, `on_instance_finishes` reports finishes for retained instances.
22
23use crate::mesh_frame::MeshFrame;
24use crate::style::SpecularMaterial;
25use crate::types::mesh::MeshData;
26use crate::types::response::QuickMetadataBootstrap;
27use std::sync::OnceLock;
28
29/// The frame a pass bakes its vertices in (#5407).
30///
31/// Delivered once, after frame selection and before the first batch, so a
32/// streaming consumer can collate each batch in the baked frame as it arrives.
33#[non_exhaustive]
34#[derive(Debug, Clone, Copy, PartialEq)]
35pub struct FrameEvent {
36    /// The frame itself: its tag, the subtracted translation, the removed
37    /// site rotation. The returned result's `frame` is this value.
38    pub frame: MeshFrame,
39    /// [`native_to_baked`](super::native_to_baked) for `frame` and the site
40    /// transform: the column-major basis that maps native coordinates to the
41    /// baked frame.
42    pub baked_basis: [f64; 16],
43}
44
45/// Events of one [`process`](super::process) pass. See the module docs.
46pub trait ProcessingSink {
47    /// Opt in to per-job refusal receipts without changing the mesh pipeline.
48    /// The returned handle belongs to this request; ordinary sinks allocate none.
49    fn product_geometry_audit(&self) -> Option<crate::ProductGeometryAudit> { None }
50
51    /// F2 (#8341): distinct type records, ascending, whose planned type
52    /// geometry was omitted because the full decoder rejects the record. A
53    /// plain decode failure, never an input-admission refusal, so it is in
54    /// neither `ProcessingStats::refused_*` counter. Its length is the native
55    /// `PipelineDiagnosticsReport::omitted_type_records`.
56    fn on_omitted_type_records(&mut self, _records: &[u32]) {}
57    /// The lightweight spatial bootstrap, when `Features::quick_metadata_bootstrap` is on.
58    fn on_bootstrap(&mut self, _bootstrap: &QuickMetadataBootstrap) {}
59    /// The frame this pass bakes vertices in.
60    fn on_frame(&mut self, _frame: &FrameEvent) {}
61    /// A chunk of meshes, with the jobs done so far and the job total.
62    fn on_batch(&mut self, _meshes: &[MeshData], _processed: usize, _total: usize) {}
63    /// Colours resolved after the first batch, when styles were deferred.
64    fn on_color_update(&mut self, _updates: &[(u32, [f32; 4])]) {}
65    /// The IFC-authored finish of each mesh of the `on_batch` that follows,
66    /// when `Features::finishes` is on: `finishes[i]` belongs to its
67    /// `meshes[i]`, `None` where nothing authored one. The join is
68    /// [`ModelFinishes`](crate::style::ModelFinishes)'s, over the pass's own
69    /// scan and index.
70    fn on_finishes(&mut self, _finishes: &[Option<SpecularMaterial>]) {}
71    /// With `Features::finishes` on, called once after the mesh batches when
72    /// the result contains instances. Each entry belongs to the instance at
73    /// the same index in `ProcessingResult::instances`, with `None` when
74    /// its style authors no finish.
75    fn on_instance_finishes(&mut self, _finishes: &[Option<SpecularMaterial>]) {}
76}
77
78impl ProcessingSink for () {}
79
80/// A sink that listens to batches only: `OnBatch::new(|meshes, processed, total| ..)`.
81///
82/// The other events keep their no-op defaults. For a consumer that streams
83/// meshes out as they are emitted and reads everything else off the result.
84pub struct OnBatch<F>(F);
85
86impl<F> OnBatch<F>
87where
88    F: FnMut(&[MeshData], usize, usize),
89{
90    pub fn new(on_batch: F) -> Self {
91        Self(on_batch)
92    }
93}
94
95impl<F> ProcessingSink for OnBatch<F>
96where
97    F: FnMut(&[MeshData], usize, usize),
98{
99    fn on_batch(&mut self, meshes: &[MeshData], processed: usize, total: usize) {
100        (self.0)(meshes, processed, total);
101    }
102}
103
104/// The adapter the deprecated entry points use to turn their closure
105/// arguments into a sink. Deleted with them in C10.
106#[doc(hidden)]
107pub struct ClosureSink<'a, B, C, Q> {
108    pub on_batch: B,
109    pub on_color_update: C,
110    pub on_bootstrap: Q,
111    /// Receives [`FrameEvent::baked_basis`] when set.
112    pub baked_basis_out: Option<&'a OnceLock<[f64; 16]>>,
113}
114
115impl<B, C, Q> ProcessingSink for ClosureSink<'_, B, C, Q>
116where
117    B: FnMut(&[MeshData], usize, usize),
118    C: FnMut(&[(u32, [f32; 4])]),
119    Q: FnMut(&QuickMetadataBootstrap),
120{
121    fn on_bootstrap(&mut self, bootstrap: &QuickMetadataBootstrap) {
122        (self.on_bootstrap)(bootstrap);
123    }
124
125    fn on_frame(&mut self, frame: &FrameEvent) {
126        if let Some(out) = self.baked_basis_out {
127            // `set` fails only if already set: one pass publishes one frame.
128            let _ = out.set(frame.baked_basis);
129        }
130    }
131
132    fn on_batch(&mut self, meshes: &[MeshData], processed: usize, total: usize) {
133        (self.on_batch)(meshes, processed, total);
134    }
135
136    fn on_color_update(&mut self, updates: &[(u32, [f32; 4])]) {
137        (self.on_color_update)(updates);
138    }
139}