ifc_lite_core/project_units/scales.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//! The file's length and plane-angle scales, resolved once (F3a, #7775).
6//!
7//! [`UnitScales`] is the scalar half of [`ProjectUnits`](super::ProjectUnits):
8//! the multipliers geometry and the data readers apply to file numbers. It
9//! moved here from `ifc-lite-processing`'s prepass so that core owns the one
10//! resolution ladder and every crate consumes it.
11
12use super::ProjectUnits;
13use crate::decoder::EntityDecoder;
14use crate::units::{
15 extract_plane_angle_to_radians, resolve_length_unit, try_extract_length_unit_scale,
16 try_extract_plane_angle_to_radians, LengthUnit,
17};
18
19/// The file's unit scales, resolved exactly once per parse.
20#[derive(Debug, Clone, Copy, PartialEq)]
21pub struct UnitScales {
22 /// Length unit → metres (1.0 for metre files, 0.001 for millimetre files).
23 pub length_unit_scale: f64,
24 /// Plane-angle unit → radians (1.0 for RADIAN files, π/180 for DEGREE).
25 pub plane_angle_to_radians: f64,
26 /// The `IFCPROJECT` express id the scales were resolved from, when found.
27 pub project_id: Option<u32>,
28 /// The unusable length-unit factor geometry must refuse (#7678); `length_unit_scale` is then data-only.
29 pub length_unit_refused: Option<u32>,
30}
31
32impl UnitScales {
33 /// The geometry scale: 1.0 for a refused unit, a finite placeholder no mesh uses (#7678).
34 pub fn geometry_length_scale(&self) -> f64 {
35 if self.length_unit_refused.is_some() { 1.0 } else { self.length_unit_scale }
36 }
37
38 /// The length unit as [`LengthUnit`]: the data reader's value (main's, an
39 /// overflowing factor reading as infinity) plus the refusal geometry must
40 /// follow. Never refuses itself.
41 pub fn length_unit(&self) -> LengthUnit {
42 LengthUnit { scale: self.length_unit_scale, unusable_factor: self.length_unit_refused }
43 }
44
45 /// Resolve BOTH scales against a COMPLETE entity index, with the data
46 /// path's policy: an undecodable project or assignment leaves the metre
47 /// and radian defaults, and an unusable length factor keeps main's value
48 /// in `length_unit_scale` while flagging `length_unit_refused` (D13: data
49 /// never refuses, geometry does).
50 pub fn resolve_complete(decoder: &mut EntityDecoder, project_id: u32) -> Self {
51 let unit = resolve_length_unit(decoder, project_id).ok();
52 Self {
53 length_unit_scale: unit.map_or(1.0, |unit| unit.scale),
54 length_unit_refused: unit.and_then(|unit| unit.unusable_factor),
55 plane_angle_to_radians: extract_plane_angle_to_radians(decoder, project_id).unwrap_or(1.0),
56 project_id: Some(project_id),
57 }
58 }
59
60 /// Resolve BOTH scales once, against the decoder's (possibly partial)
61 /// entity index, with the documented fallback ladder:
62 ///
63 /// 1. `project_id_hint` (recorded by the scan) — O(1) decode via the index.
64 /// 2. No hint? Locate `IFCPROJECT` with the one locator
65 /// ([`crate::find_project_id`], a SIMD substring search: it is a
66 /// singleton and many exporters — IfcOpenShell, Revit — emit it near
67 /// the END of the file, after the scan's early-meta point).
68 /// 3. Resolution chain incomplete on a PARTIAL index (streaming early-meta:
69 /// the IFCSIUNIT chain may sit past the scan point)? Re-resolve against a
70 /// freshly built FULL index rather than silently defaulting — a millimetre
71 /// model resolved as metres renders 1000× oversized.
72 ///
73 /// Per-element decoders are seeded from the result
74 /// ([`EntityDecoder::seed_unit_scales`]) so the historic
75 /// O(file)-scan-per-decoder stall class stays dead.
76 pub fn resolve(content: &[u8], project_id_hint: Option<u32>, decoder: &mut EntityDecoder) -> Self {
77 // The bare locator, not `decoder.project_id()`: the decoder's index may
78 // be partial here, and an unconfirmed candidate is still the answer.
79 let project_id = project_id_hint.or_else(|| crate::find_project_id(content));
80 let Some(pid) = project_id else {
81 return Self::default();
82 };
83
84 // Fast path: resolve on the caller's decoder/index. BOTH resolvers return
85 // `None` (not a masked default) when their chain is incomplete on a partial
86 // index, so an unresolved either-scale forces the full-index retry below
87 // rather than silently shipping radians/metres (issue #1367).
88 let length = try_extract_length_unit_scale(decoder, pid);
89 let angle = try_extract_plane_angle_to_radians(decoder, pid);
90
91 if let (Some(length_unit_scale), Some(plane_angle_to_radians)) = (length, angle) {
92 return Self {
93 length_unit_scale,
94 plane_angle_to_radians,
95 project_id,
96 length_unit_refused: None,
97 };
98 }
99
100 // Chain incomplete (partial index) — resolve against a full index.
101 let full_index = crate::build_entity_index(content);
102 let mut full_decoder = EntityDecoder::with_index(content, full_index);
103 // The one unit policy (#7678): an unusable factor is a refusal, not metres.
104 let unit = length
105 .map(|scale| LengthUnit { scale, unusable_factor: None })
106 .or_else(|| resolve_length_unit(&mut full_decoder, pid).ok());
107 Self {
108 length_unit_scale: unit.map_or(1.0, |unit| unit.scale),
109 length_unit_refused: unit.and_then(|unit| unit.unusable_factor),
110 plane_angle_to_radians: angle
111 .or_else(|| extract_plane_angle_to_radians(&mut full_decoder, pid).ok())
112 .unwrap_or(1.0),
113 project_id,
114 }
115 }
116}
117
118impl Default for UnitScales {
119 fn default() -> Self {
120 Self {
121 length_unit_scale: 1.0,
122 plane_angle_to_radians: 1.0,
123 project_id: None,
124 length_unit_refused: None,
125 }
126 }
127}
128
129impl ProjectUnits {
130 /// [`Self::resolve`] for the file's own project, located by the one
131 /// locator ([`EntityDecoder::project_id`], the first `IFCPROJECT` in file
132 /// order). A file without a project yields the empty table with metre and
133 /// radian defaults.
134 pub fn resolve_file(decoder: &mut EntityDecoder) -> Self {
135 match decoder.project_id() {
136 Some(project_id) => Self::resolve(decoder, project_id),
137 None => Self::default(),
138 }
139 }
140
141 /// The length and plane-angle scalars of the resolved project.
142 ///
143 /// They equal [`UnitScales::resolve`]'s on a complete index and carry the
144 /// data path's value: main's, never a refusal. Geometry follows
145 /// [`LengthUnit::for_geometry`] on [`Self::length_unit`].
146 pub fn scales(&self) -> UnitScales {
147 self.scales
148 }
149
150 /// Metres per file length unit (data path; see [`Self::scales`]).
151 pub fn length_unit_scale(&self) -> f64 {
152 self.scales.length_unit_scale
153 }
154
155 /// The length unit with the unusable-factor flag geometry must refuse on.
156 pub fn length_unit(&self) -> LengthUnit {
157 self.scales.length_unit()
158 }
159
160 /// Radians per file plane-angle unit.
161 pub fn plane_angle_to_radians(&self) -> f64 {
162 self.scales.plane_angle_to_radians
163 }
164
165 /// The `IFCPROJECT` these units were resolved from, if any.
166 pub fn project_id(&self) -> Option<u32> {
167 self.scales.project_id
168 }
169}