1use crate::EntityScanner;
12use std::collections::HashSet;
13
14#[derive(Debug, Clone)]
16pub struct ModelBounds {
17 pub min_x: f64,
19 pub min_y: f64,
21 pub min_z: f64,
23 pub max_x: f64,
25 pub max_y: f64,
27 pub max_z: f64,
29 pub sample_count: usize,
31}
32
33impl ModelBounds {
34 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 #[inline]
49 pub fn is_valid(&self) -> bool {
50 self.sample_count > 0
51 }
52
53 #[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 #[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 #[inline]
80 pub fn has_large_coordinates(&self) -> bool {
81 const THRESHOLD: f64 = 10000.0; 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 #[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
110pub fn scan_model_bounds(content: &str) -> ModelBounds {
120 let mut bounds = ModelBounds::new();
121
122 let mut scanner = EntityScanner::new(content);
124
125 while let Some((_id, type_name, start, end)) = scanner.next_entity() {
126 if type_name != "IFCCARTESIANPOINT" {
128 continue;
129 }
130
131 let entity_text = &content[start..end];
133
134 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 if x.is_finite() && y.is_finite() && z.is_finite() {
142 bounds.expand(x, y, z);
143 }
144 }
145 }
146
147 bounds
148}
149
150fn extract_point_coordinates(text: &str) -> Option<(f64, f64, Option<f64>)> {
153 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 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
181pub fn scan_placement_bounds(content: &str) -> ModelBounds {
187 let mut bounds = ModelBounds::new();
188 let mut scanner = EntityScanner::new(content);
189
190 let mut placement_point_ids: HashSet<u32> = HashSet::new();
192
193 while let Some((_id, type_name, start, end)) = scanner.next_entity() {
195 if type_name == "IFCAXIS2PLACEMENT3D" {
196 let entity_text = &content[start..end];
197 if let Some(ref_id) = extract_first_reference(entity_text) {
199 placement_point_ids.insert(ref_id);
200 }
201 }
202 if type_name == "IFCSITE" {
204 }
208 if type_name == "IFCCARTESIANPOINT" {
210 continue;
212 }
213 }
214
215 scanner = EntityScanner::new(content);
217 while let Some((id, type_name, start, end)) = scanner.next_entity() {
218 if type_name == "IFCCARTESIANPOINT" {
219 let is_placement_point = placement_point_ids.contains(&id);
221
222 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 if !x.is_finite() || !y.is_finite() || !z.is_finite() {
232 continue;
233 }
234
235 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 !bounds.is_valid() {
245 return scan_model_bounds(content);
246 }
247
248 bounds
249}
250
251fn extract_first_reference(text: &str) -> Option<u32> {
254 let start = text.find('(')?;
256 let rest = &text[start + 1..];
257
258 let hash_pos = rest.find('#')?;
260 let after_hash = &rest[hash_pos + 1..];
261
262 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); 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); }
393
394 #[test]
395 fn test_precision_preserved_with_rtc() {
396 let x1 = 2679012.123456_f64;
400 let x2 = 2679012.223456_f64; let expected_diff = 0.1;
402
403 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 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 assert!(
423 error_rtc < error_direct * 0.1 || error_rtc < 0.0001,
424 "RTC should significantly improve precision"
425 );
426 }
427}