ifc_lite_processing/shard_classes.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//! Per-record prepass classification for the sharded scan (split from
6//! `parallel_scan.rs` — the byte-identical shard/stitch protocol lives there;
7//! this module owns the class codes and the classified scan variant the
8//! browser's sharded pre-pass consumes).
9
10use crate::parallel_scan::ShardRecords;
11use ifc_lite_core::EntityScanner;
12
13/// Per-record prepass class emitted by [`scan_shard_classified`].
14///
15/// Only the codes a downstream consumer needs are defined; everything else is
16/// [`PREPASS_CLASS_NONE`]. Classification happens AT SCAN TIME from the same
17/// `type_name` string the serial pre-pass matches on, so a consumer that
18/// filters records by class reproduces the serial pre-pass's span collection
19/// byte-for-byte (same keyword compare, same file order).
20pub const PREPASS_CLASS_NONE: u8 = 0;
21/// `IFCSTYLEDITEM` — the styled-item spans the pre-pass resolver classifies
22/// into orphan (material appearance) vs geometry-attached styles.
23pub const PREPASS_CLASS_STYLED_ITEM: u8 = 4;
24/// `IFCINDEXEDCOLOURMAP` (#663/#858).
25pub const PREPASS_CLASS_INDEXED_COLOUR_MAP: u8 = 5;
26/// `IFCMATERIALDEFINITIONREPRESENTATION` (#407).
27pub const PREPASS_CLASS_MATERIAL_DEF_REPR: u8 = 6;
28/// `IFCRELASSOCIATESMATERIAL` (#407).
29pub const PREPASS_CLASS_REL_ASSOCIATES_MATERIAL: u8 = 7;
30/// `IFCRELVOIDSELEMENT`.
31pub const PREPASS_CLASS_REL_VOIDS: u8 = 8;
32/// `IFCRELFILLSELEMENT`.
33pub const PREPASS_CLASS_REL_FILLS: u8 = 9;
34/// `IFCRELAGGREGATES`.
35pub const PREPASS_CLASS_REL_AGGREGATES: u8 = 10;
36/// `IFCPROJECT`.
37pub const PREPASS_CLASS_PROJECT: u8 = 2;
38/// `IFCSITE` (also a geometry job — the pre-pass buffers it like one).
39pub const PREPASS_CLASS_SITE: u8 = 3;
40/// `IFCMATERIALLAYERSET` / `IFCMATERIALLAYERSETUSAGE` (arms the layer index).
41pub const PREPASS_CLASS_MATERIAL_LAYER_SET: u8 = 13;
42/// FLAG bit: geometry-bearing entity (`has_geometry_by_name`) — a pre-pass
43/// geometry job. Composes with the named codes' nibble range (2..=13) and
44/// with [`PREPASS_CLASS_FLAG_TYPE_CANDIDATE`].
45pub const PREPASS_CLASS_FLAG_GEOMETRY_JOB: u8 = 0x80;
46/// FLAG bit: `IfcTypeProduct` subtype candidate (name ends TYPE/STYLE) for the
47/// #957 orphan type-geometry pass.
48pub const PREPASS_CLASS_FLAG_TYPE_CANDIDATE: u8 = 0x40;
49/// `IFCMAPPEDITEM` (#957/#1623 repmap plans).
50pub const PREPASS_CLASS_MAPPED_ITEM: u8 = 11;
51/// `IFCRELDEFINESBYTYPE` (#957 instantiated-type ids).
52pub const PREPASS_CLASS_REL_DEFINES_BY_TYPE: u8 = 12;
53/// Mask extracting the named-arm code from a class byte (drops the flag bits).
54pub const PREPASS_CLASS_CODE_MASK: u8 = 0x3F;
55
56/// [`scan_shard`] plus a parallel per-record class column (see the
57/// `PREPASS_CLASS_*` codes). Same records, same handoff; the class byte lets
58/// the browser host extract pre-pass span lists (today: styled items) from the
59/// stitched shard columns WITHOUT waiting for the serial pre-pass scan.
60pub fn scan_shard_classified(
61 content: &[u8],
62 range_start: usize,
63 range_end: usize,
64) -> (ShardRecords, Vec<u8>, Option<usize>) {
65 let mut scanner = if range_start == 0 {
66 EntityScanner::new(content)
67 } else {
68 EntityScanner::new_at(content, range_start)
69 };
70 let mut records = Vec::new();
71 let mut classes = Vec::new();
72 let mut handoff = None;
73 while let Some((id, type_name, start, entity_end)) = scanner.next_entity() {
74 if start >= range_end {
75 handoff = Some(start);
76 break;
77 }
78 records.push((id, start, entity_end));
79 classes.push(classify_type_name_with_content(
80 type_name,
81 &content[start..entity_end],
82 ));
83 }
84 (records, classes, handoff)
85}
86
87/// [`classify_type_name`] plus the #1910 instance-level exception: a spatial
88/// container `has_geometry_by_name` blocks by name (`IfcBuilding` et al.) is
89/// still classified as a geometry job when THIS instance's `Representation`
90/// attribute (index 6) is exceptionally non-null -- mirrors the identical
91/// exception applied to the serial scan loop
92/// (`rust/wasm-bindings/src/api/gpu_meshes/prepass.rs`) and the streaming
93/// processor (`rust/processing/src/processor/mod.rs`), so all three
94/// discovery paths agree on what counts as geometry. `entity_bytes` is the
95/// full `#id=KEYWORD(...)` span for this record.
96pub fn classify_type_name_with_content(type_name: &str, entity_bytes: &[u8]) -> u8 {
97 let mut class = classify_type_name(type_name);
98 if class & PREPASS_CLASS_FLAG_GEOMETRY_JOB == 0
99 && ifc_lite_core::is_representationless_spatial_container_by_name(type_name)
100 && ifc_lite_core::nth_attribute_is_present(entity_bytes, 6)
101 {
102 class |= PREPASS_CLASS_FLAG_GEOMETRY_JOB;
103 }
104 class
105}
106
107/// Classify a scanned STEP keyword into the prepass class byte: a named-arm
108/// code for the exact keywords the serial pre-pass matches, plus the
109/// geometry-job / type-candidate FLAG bits from the same helpers it calls
110/// (`has_geometry_by_name`, `IfcType::is_subtype_of`). Byte-identical span
111/// collection and job discovery follow from using the identical predicates at
112/// scan time.
113///
114/// Deliberately name-only (cannot see the entity bytes) -- the #1910
115/// instance-level exception lives one layer up, in
116/// [`classify_type_name_with_content`], which is what [`scan_shard_classified`]
117/// actually calls. Kept as a separate function (rather than inlining) so
118/// every other named/flag arm here stays byte-identical to what it was
119/// before #1910 -- the only new code path is the explicit OR-in above.
120pub fn classify_type_name(type_name: &str) -> u8 {
121 use ifc_lite_core::{has_geometry_by_name, IfcType};
122 let named = match type_name {
123 "IFCPROJECT" => PREPASS_CLASS_PROJECT,
124 "IFCSITE" => return PREPASS_CLASS_SITE, // site is job + site-record; flags implied
125 "IFCSTYLEDITEM" => PREPASS_CLASS_STYLED_ITEM,
126 "IFCINDEXEDCOLOURMAP" => PREPASS_CLASS_INDEXED_COLOUR_MAP,
127 "IFCMATERIALDEFINITIONREPRESENTATION" => PREPASS_CLASS_MATERIAL_DEF_REPR,
128 "IFCRELASSOCIATESMATERIAL" => PREPASS_CLASS_REL_ASSOCIATES_MATERIAL,
129 "IFCRELVOIDSELEMENT" => PREPASS_CLASS_REL_VOIDS,
130 "IFCRELFILLSELEMENT" => PREPASS_CLASS_REL_FILLS,
131 "IFCRELAGGREGATES" => PREPASS_CLASS_REL_AGGREGATES,
132 "IFCMAPPEDITEM" => PREPASS_CLASS_MAPPED_ITEM,
133 "IFCRELDEFINESBYTYPE" => PREPASS_CLASS_REL_DEFINES_BY_TYPE,
134 "IFCMATERIALLAYERSET" | "IFCMATERIALLAYERSETUSAGE" => PREPASS_CLASS_MATERIAL_LAYER_SET,
135 _ => PREPASS_CLASS_NONE,
136 };
137 if named != PREPASS_CLASS_NONE {
138 // The named keywords are mutually exclusive with the flag predicates in
139 // the serial match (its arms return before the `_` arm runs them).
140 return named;
141 }
142 let mut class = PREPASS_CLASS_NONE;
143 if type_name.ends_with("TYPE") || type_name.ends_with("STYLE") {
144 let ty = IfcType::from_str(type_name);
145 if ty.is_subtype_of(IfcType::IfcTypeProduct) {
146 class |= PREPASS_CLASS_FLAG_TYPE_CANDIDATE;
147 }
148 }
149 if has_geometry_by_name(type_name) {
150 class |= PREPASS_CLASS_FLAG_GEOMETRY_JOB;
151 }
152 class
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 /// The type-candidate check ORs two independent suffix tests
160 /// (`ends_with("TYPE")` and `ends_with("STYLE")`) before confirming the
161 /// resolved `IfcType` is a subtype of `IfcTypeProduct`. Pin that the TYPE
162 /// arm alone is load-bearing: `IFCWALLTYPE` (a real `IfcTypeProduct`
163 /// subtype) must set the flag even though it does NOT end in "STYLE", so a
164 /// mutation that drops the `ends_with("TYPE")` disjunct and keeps only the
165 /// STYLE check would misclassify it and lose #957's orphan type-geometry
166 /// pass for every walltype-shaped keyword.
167 #[test]
168 fn type_suffix_sets_type_candidate_flag() {
169 let class = classify_type_name("IFCWALLTYPE");
170 assert_eq!(
171 class & PREPASS_CLASS_FLAG_TYPE_CANDIDATE,
172 PREPASS_CLASS_FLAG_TYPE_CANDIDATE,
173 "IFCWALLTYPE is an IfcTypeProduct subtype ending in TYPE, not STYLE — \
174 it must set the type-candidate flag via the TYPE arm alone"
175 );
176 }
177
178 /// The STYLE arm is deliberately distinct from the TYPE arm: a keyword
179 /// ending in "STYLE" that is NOT an `IfcTypeProduct` subtype (`IFCSURFACESTYLE`
180 /// is an `IfcPresentationStyle`) must NOT set the flag. Combined with
181 /// `type_suffix_sets_type_candidate_flag` above, this pins that the two
182 /// suffix checks gate genuinely different keyword sets rather than one
183 /// subsuming the other.
184 #[test]
185 fn style_suffix_without_type_product_subtype_does_not_set_flag() {
186 let class = classify_type_name("IFCSURFACESTYLE");
187 assert_eq!(
188 class & PREPASS_CLASS_FLAG_TYPE_CANDIDATE,
189 0,
190 "IFCSURFACESTYLE is not an IfcTypeProduct subtype, so the type-candidate \
191 flag must stay clear even though the name ends in STYLE"
192 );
193 }
194
195 /// Pins every named-arm keyword to its own distinct class code. The two
196 /// tests above cover only the TYPE/STYLE suffix flag; nothing asserted
197 /// the named-arm lookup table itself, so this is a textbook lookup-table
198 /// vacuity: any two of the 12 named arms could be swapped
199 /// (e.g. `IFCRELVOIDSELEMENT` ↔ `IFCRELFILLSELEMENT`) and the full suite
200 /// stayed green. A wrong class here means the sharded pre-pass hands the
201 /// browser host the wrong span list for a record (voids treated as
202 /// fills, materials treated as aggregates, etc.), silently diverging
203 /// from the serial pre-pass it must reproduce byte-for-byte.
204 #[test]
205 fn classify_type_name_pins_every_named_arm_to_a_distinct_code() {
206 let cases = [
207 ("IFCPROJECT", PREPASS_CLASS_PROJECT),
208 ("IFCSITE", PREPASS_CLASS_SITE),
209 ("IFCSTYLEDITEM", PREPASS_CLASS_STYLED_ITEM),
210 ("IFCINDEXEDCOLOURMAP", PREPASS_CLASS_INDEXED_COLOUR_MAP),
211 ("IFCMATERIALDEFINITIONREPRESENTATION", PREPASS_CLASS_MATERIAL_DEF_REPR),
212 ("IFCRELASSOCIATESMATERIAL", PREPASS_CLASS_REL_ASSOCIATES_MATERIAL),
213 ("IFCRELVOIDSELEMENT", PREPASS_CLASS_REL_VOIDS),
214 ("IFCRELFILLSELEMENT", PREPASS_CLASS_REL_FILLS),
215 ("IFCRELAGGREGATES", PREPASS_CLASS_REL_AGGREGATES),
216 ("IFCMAPPEDITEM", PREPASS_CLASS_MAPPED_ITEM),
217 ("IFCRELDEFINESBYTYPE", PREPASS_CLASS_REL_DEFINES_BY_TYPE),
218 ("IFCMATERIALLAYERSET", PREPASS_CLASS_MATERIAL_LAYER_SET),
219 ("IFCMATERIALLAYERSETUSAGE", PREPASS_CLASS_MATERIAL_LAYER_SET),
220 ];
221 for (keyword, expected) in cases {
222 assert_eq!(
223 classify_type_name(keyword),
224 expected,
225 "{keyword} classified as {} not {expected}",
226 classify_type_name(keyword)
227 );
228 }
229
230 // Golden totals: every expected code above (except the two that
231 // intentionally share MATERIAL_LAYER_SET) is pairwise distinct, so a
232 // swap between any two arms is caught by the per-case assertion —
233 // not just an aggregate that a swap could still satisfy.
234 let distinct_codes: std::collections::HashSet<u8> =
235 cases.iter().map(|(_, c)| *c).collect();
236 assert_eq!(distinct_codes.len(), 12, "expected 12 distinct codes across 13 keywords (2 share MATERIAL_LAYER_SET)");
237
238 // An unrecognised keyword with no geometry/type-candidate signal is NONE.
239 assert_eq!(classify_type_name("IFCUNKNOWNTHING"), PREPASS_CLASS_NONE);
240 }
241}