Skip to main content

ifc_lite_core/
model_bounds.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//! Model bounds calculation for large coordinate handling
6//!
7//! Scans IFC content to determine model bounding box in f64 precision.
8//! Used for calculating RTC (Relative-to-Center) offset before geometry processing
9//! to avoid Float32 precision loss with large coordinates (e.g., Swiss UTM).
10
11use crate::EntityScanner;
12use std::collections::HashSet;
13
14/// Model bounds in f64 precision
15#[derive(Debug, Clone)]
16pub struct ModelBounds {
17    /// Minimum X coordinate found
18    pub min_x: f64,
19    /// Minimum Y coordinate found
20    pub min_y: f64,
21    /// Minimum Z coordinate found
22    pub min_z: f64,
23    /// Maximum X coordinate found
24    pub max_x: f64,
25    /// Maximum Y coordinate found
26    pub max_y: f64,
27    /// Maximum Z coordinate found
28    pub max_z: f64,
29    /// Number of points sampled
30    pub sample_count: usize,
31}
32
33impl ModelBounds {
34    /// Create new bounds initialized to invalid state
35    pub fn new() -> Self {
36        Self {
37            min_x: f64::MAX,
38            min_y: f64::MAX,
39            min_z: f64::MAX,
40            max_x: f64::MIN,
41            max_y: f64::MIN,
42            max_z: f64::MIN,
43            sample_count: 0,
44        }
45    }
46
47    /// Check if bounds are valid (at least one point added)
48    #[inline]
49    pub fn is_valid(&self) -> bool {
50        self.sample_count > 0
51    }
52
53    /// Expand bounds to include a point
54    #[inline]
55    pub fn expand(&mut self, x: f64, y: f64, z: f64) {
56        self.min_x = self.min_x.min(x);
57        self.min_y = self.min_y.min(y);
58        self.min_z = self.min_z.min(z);
59        self.max_x = self.max_x.max(x);
60        self.max_y = self.max_y.max(y);
61        self.max_z = self.max_z.max(z);
62        self.sample_count += 1;
63    }
64
65    /// Get centroid (center of bounding box)
66    #[inline]
67    pub fn centroid(&self) -> (f64, f64, f64) {
68        if !self.is_valid() {
69            return (0.0, 0.0, 0.0);
70        }
71        (
72            (self.min_x + self.max_x) / 2.0,
73            (self.min_y + self.max_y) / 2.0,
74            (self.min_z + self.max_z) / 2.0,
75        )
76    }
77
78    /// Check if bounds contain large coordinates (>10km from origin)
79    #[inline]
80    pub fn has_large_coordinates(&self) -> bool {
81        const THRESHOLD: f64 = 10000.0; // 10km
82        if !self.is_valid() {
83            return false;
84        }
85        self.min_x.abs() > THRESHOLD
86            || self.min_y.abs() > THRESHOLD
87            || self.max_x.abs() > THRESHOLD
88            || self.max_y.abs() > THRESHOLD
89            || self.min_z.abs() > THRESHOLD
90            || self.max_z.abs() > THRESHOLD
91    }
92
93    /// Get the RTC offset (same as centroid for large coordinates, zero otherwise)
94    #[inline]
95    pub fn rtc_offset(&self) -> (f64, f64, f64) {
96        if self.has_large_coordinates() {
97            self.centroid()
98        } else {
99            (0.0, 0.0, 0.0)
100        }
101    }
102}
103
104impl Default for ModelBounds {
105    fn default() -> Self {
106        Self::new()
107    }
108}
109
110/// Scan IFC content to extract model bounds from IfcCartesianPoint entities
111///
112/// This is a fast first-pass scan that extracts coordinate values directly from
113/// the IFC text without full entity decoding. It samples points to determine
114/// if the model has large coordinates that need RTC shifting.
115///
116/// # Performance
117/// This scans through the file once, looking for IFCCARTESIANPOINT patterns.
118/// It's much faster than full entity parsing since it only extracts coordinates.
119pub fn scan_model_bounds(content: &str) -> ModelBounds {
120    let mut bounds = ModelBounds::new();
121
122    // Use EntityScanner for efficient scanning
123    let mut scanner = EntityScanner::new(content);
124
125    while let Some((_id, type_name, start, end)) = scanner.next_entity() {
126        // Only process cartesian points
127        if type_name != "IFCCARTESIANPOINT" {
128            continue;
129        }
130
131        // Extract the entity content
132        let entity_text = &content[start..end];
133
134        // Parse coordinates from IFCCARTESIANPOINT((x,y,z));
135        if let Some(coords) = extract_point_coordinates(entity_text) {
136            let x = coords.0;
137            let y = coords.1;
138            let z = coords.2.unwrap_or(0.0);
139
140            // Skip obviously invalid coordinates
141            if x.is_finite() && y.is_finite() && z.is_finite() {
142                bounds.expand(x, y, z);
143            }
144        }
145    }
146
147    bounds
148}
149
150/// Extract coordinates from IfcCartesianPoint text
151/// Format: IFCCARTESIANPOINT((x,y)) or IFCCARTESIANPOINT((x,y,z))
152fn extract_point_coordinates(text: &str) -> Option<(f64, f64, Option<f64>)> {
153    // Find the coordinate list between (( and ))
154    let start = text.find("((")?;
155    let end = text.rfind("))")?;
156
157    if start >= end {
158        return None;
159    }
160
161    let coord_str = &text[start + 2..end];
162
163    // Split by comma and parse
164    let parts: Vec<&str> = coord_str.split(',').collect();
165
166    if parts.len() < 2 {
167        return None;
168    }
169
170    let x = parts[0].trim().parse::<f64>().ok()?;
171    let y = parts[1].trim().parse::<f64>().ok()?;
172    let z = if parts.len() > 2 {
173        parts[2].trim().parse::<f64>().ok()
174    } else {
175        None
176    };
177
178    Some((x, y, z))
179}
180
181/// Scan model bounds focusing on placement coordinates
182///
183/// This variant specifically looks at IfcLocalPlacement and transformation
184/// coordinates, which are more representative of where geometry will be placed.
185/// Useful for models where cartesian points include local/relative coordinates.
186pub fn scan_placement_bounds(content: &str) -> ModelBounds {
187    let mut bounds = ModelBounds::new();
188    let mut scanner = EntityScanner::new(content);
189
190    // Track which cartesian point IDs are referenced by placements (HashSet for O(1) lookups)
191    let mut placement_point_ids: HashSet<u32> = HashSet::new();
192
193    // First pass: find cartesian points referenced by Axis2Placement3D
194    while let Some((_id, type_name, start, end)) = scanner.next_entity() {
195        if type_name == "IFCAXIS2PLACEMENT3D" {
196            let entity_text = &content[start..end];
197            // Extract the Location reference (first attribute)
198            if let Some(ref_id) = extract_first_reference(entity_text) {
199                placement_point_ids.insert(ref_id);
200            }
201        }
202        // Also include IfcSite coordinates which often have real-world coords
203        if type_name == "IFCSITE" {
204            // IfcSite has RefLatitude, RefLongitude, RefElevation
205            // These are stored as IfcCompoundPlaneAngleMeasure, not coords
206            // But we can get bounds from the site's placement
207        }
208        // Store the entity ID for cartesian points
209        if type_name == "IFCCARTESIANPOINT" {
210            // Will be checked in second pass
211            continue;
212        }
213    }
214
215    // Second pass: extract coordinates from referenced points
216    scanner = EntityScanner::new(content);
217    while let Some((id, type_name, start, end)) = scanner.next_entity() {
218        if type_name == "IFCCARTESIANPOINT" {
219            // Check if this point is referenced by a placement
220            let is_placement_point = placement_point_ids.contains(&id);
221
222            // For placement points, always include them
223            // For other points, only include if they have large coordinates
224            let entity_text = &content[start..end];
225            if let Some(coords) = extract_point_coordinates(entity_text) {
226                let x = coords.0;
227                let y = coords.1;
228                let z = coords.2.unwrap_or(0.0);
229
230                // Skip invalid coordinates
231                if !x.is_finite() || !y.is_finite() || !z.is_finite() {
232                    continue;
233                }
234
235                // Include placement points and points with large coordinates (including Z axis)
236                if is_placement_point || x.abs() > 1000.0 || y.abs() > 1000.0 || z.abs() > 1000.0 {
237                    bounds.expand(x, y, z);
238                }
239            }
240        }
241    }
242
243    // If no placement points found, fall back to full scan
244    if !bounds.is_valid() {
245        return scan_model_bounds(content);
246    }
247
248    bounds
249}
250
251/// Extract first entity reference from text
252/// Looks for #xxx pattern
253fn extract_first_reference(text: &str) -> Option<u32> {
254    // Find opening paren of attribute list
255    let start = text.find('(')?;
256    let rest = &text[start + 1..];
257
258    // Find first # character
259    let hash_pos = rest.find('#')?;
260    let after_hash = &rest[hash_pos + 1..];
261
262    // Parse the number
263    let end_pos = after_hash
264        .find(|c: char| !c.is_ascii_digit())
265        .unwrap_or(after_hash.len());
266
267    if end_pos == 0 {
268        return None;
269    }
270
271    after_hash[..end_pos].parse().ok()
272}
273
274#[cfg(test)]
275mod tests {
276    use super::*;
277
278    #[test]
279    fn test_bounds_creation() {
280        let bounds = ModelBounds::new();
281        assert!(!bounds.is_valid());
282        assert!(!bounds.has_large_coordinates());
283    }
284
285    #[test]
286    fn test_bounds_expand() {
287        let mut bounds = ModelBounds::new();
288        bounds.expand(100.0, 200.0, 50.0);
289        bounds.expand(150.0, 250.0, 75.0);
290
291        assert!(bounds.is_valid());
292        assert_eq!(bounds.min_x, 100.0);
293        assert_eq!(bounds.max_x, 150.0);
294        assert_eq!(bounds.min_y, 200.0);
295        assert_eq!(bounds.max_y, 250.0);
296
297        let centroid = bounds.centroid();
298        assert_eq!(centroid.0, 125.0);
299        assert_eq!(centroid.1, 225.0);
300    }
301
302    #[test]
303    fn test_large_coordinates_detection() {
304        let mut bounds = ModelBounds::new();
305        bounds.expand(2679012.0, 1247892.0, 432.0); // Swiss UTM coordinates
306
307        assert!(bounds.has_large_coordinates());
308
309        let offset = bounds.rtc_offset();
310        assert_eq!(offset.0, 2679012.0);
311        assert_eq!(offset.1, 1247892.0);
312    }
313
314    #[test]
315    fn test_small_coordinates_no_shift() {
316        let mut bounds = ModelBounds::new();
317        bounds.expand(0.0, 0.0, 0.0);
318        bounds.expand(100.0, 100.0, 10.0);
319
320        assert!(!bounds.has_large_coordinates());
321
322        let offset = bounds.rtc_offset();
323        assert_eq!(offset.0, 0.0);
324        assert_eq!(offset.1, 0.0);
325        assert_eq!(offset.2, 0.0);
326    }
327
328    #[test]
329    fn test_extract_point_coordinates_3d() {
330        let text = "IFCCARTESIANPOINT((2679012.123,1247892.456,432.789))";
331        let coords = extract_point_coordinates(text).unwrap();
332
333        assert!((coords.0 - 2679012.123).abs() < 0.001);
334        assert!((coords.1 - 1247892.456).abs() < 0.001);
335        assert!((coords.2.unwrap() - 432.789).abs() < 0.001);
336    }
337
338    #[test]
339    fn test_extract_point_coordinates_2d() {
340        let text = "IFCCARTESIANPOINT((100.5,200.5))";
341        let coords = extract_point_coordinates(text).unwrap();
342
343        assert_eq!(coords.0, 100.5);
344        assert_eq!(coords.1, 200.5);
345        assert!(coords.2.is_none());
346    }
347
348    #[test]
349    fn test_scan_model_bounds() {
350        let ifc_content = r#"
351ISO-10303-21;
352HEADER;
353FILE_DESCRIPTION((''),'2;1');
354ENDSEC;
355DATA;
356#1=IFCCARTESIANPOINT((2679012.0,1247892.0,432.0));
357#2=IFCCARTESIANPOINT((2679112.0,1247992.0,442.0));
358#3=IFCWALL('guid',$,$,$,$,$,$,$);
359ENDSEC;
360END-ISO-10303-21;
361"#;
362
363        let bounds = scan_model_bounds(ifc_content);
364
365        assert!(bounds.is_valid());
366        assert!(bounds.has_large_coordinates());
367        assert_eq!(bounds.sample_count, 2);
368
369        let centroid = bounds.centroid();
370        assert!((centroid.0 - 2679062.0).abs() < 0.001);
371        assert!((centroid.1 - 1247942.0).abs() < 0.001);
372    }
373
374    #[test]
375    fn test_scan_model_bounds_small_model() {
376        let ifc_content = r#"
377ISO-10303-21;
378DATA;
379#1=IFCCARTESIANPOINT((0.0,0.0,0.0));
380#2=IFCCARTESIANPOINT((10.0,10.0,5.0));
381ENDSEC;
382END-ISO-10303-21;
383"#;
384
385        let bounds = scan_model_bounds(ifc_content);
386
387        assert!(bounds.is_valid());
388        assert!(!bounds.has_large_coordinates());
389
390        let offset = bounds.rtc_offset();
391        assert_eq!(offset.0, 0.0); // No shift needed for small coordinates
392    }
393
394    #[test]
395    fn test_precision_preserved_with_rtc() {
396        // Simulate what happens with and without RTC
397
398        // Large Swiss UTM coordinates
399        let x1 = 2679012.123456_f64;
400        let x2 = 2679012.223456_f64; // 0.1m apart
401        let expected_diff = 0.1;
402
403        // WITHOUT RTC: Convert directly to f32 (loses precision)
404        let x1_f32_direct = x1 as f32;
405        let x2_f32_direct = x2 as f32;
406        let diff_direct = x2_f32_direct - x1_f32_direct;
407        let error_direct = (diff_direct as f64 - expected_diff).abs();
408
409        // WITH RTC: Subtract centroid first (in f64), then convert
410        let centroid = (x1 + x2) / 2.0;
411        let x1_shifted = (x1 - centroid) as f32;
412        let x2_shifted = (x2 - centroid) as f32;
413        let diff_rtc = x2_shifted - x1_shifted;
414        let error_rtc = (diff_rtc as f64 - expected_diff).abs();
415
416        println!("Without RTC: diff={}, error={}", diff_direct, error_direct);
417        println!("With RTC: diff={}, error={}", diff_rtc, error_rtc);
418
419        // RTC should give much better precision
420        // At ~2.7M magnitude, f32 has ~0.25m precision
421        // After shifting to small values, f32 has sub-mm precision
422        assert!(
423            error_rtc < error_direct * 0.1 || error_rtc < 0.0001,
424            "RTC should significantly improve precision"
425        );
426    }
427}