ifc-lite-geometry 3.1.1

Geometry processing and mesh generation for IFC models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! 2D Profile definitions and triangulation

use crate::error::{Error, Result};
use nalgebra::Point2;

/// 2D Profile with optional holes
#[derive(Debug, Clone)]
pub struct Profile2D {
    /// Outer boundary (counter-clockwise)
    pub outer: Vec<Point2<f64>>,
    /// Holes (clockwise)
    pub holes: Vec<Vec<Point2<f64>>>,
}

impl Profile2D {
    /// Create a new profile
    pub fn new(outer: Vec<Point2<f64>>) -> Self {
        Self {
            outer,
            holes: Vec::new(),
        }
    }

    /// Add a hole to the profile
    pub fn add_hole(&mut self, hole: Vec<Point2<f64>>) {
        self.holes.push(hole);
    }

    /// Translate the profile so the centre of its outer bounding box sits at the
    /// origin.
    ///
    /// IFC parameterised profiles (I/U/L/T/C/Z/…) are defined centred on their
    /// bounding box, and the swept-area `Position` placement is applied relative
    /// to that centred origin. Some per-shape builders are easier to read when
    /// written from a corner; centring them here in one place keeps every
    /// parametric profile consistent with the spec. Holes are shifted by the same
    /// offset so they stay aligned with the outer boundary. No-op for profiles
    /// that are already centred.
    pub fn center_on_bbox(&mut self) {
        if self.outer.is_empty() {
            return;
        }
        let mut min_x = f64::INFINITY;
        let mut min_y = f64::INFINITY;
        let mut max_x = f64::NEG_INFINITY;
        let mut max_y = f64::NEG_INFINITY;
        for p in &self.outer {
            min_x = min_x.min(p.x);
            min_y = min_y.min(p.y);
            max_x = max_x.max(p.x);
            max_y = max_y.max(p.y);
        }
        let cx = (min_x + max_x) / 2.0;
        let cy = (min_y + max_y) / 2.0;
        if cx == 0.0 && cy == 0.0 {
            return;
        }
        for p in &mut self.outer {
            p.x -= cx;
            p.y -= cy;
        }
        for hole in &mut self.holes {
            for p in hole {
                p.x -= cx;
                p.y -= cy;
            }
        }
    }

    /// Triangulate the profile using earcutr
    /// Returns triangle indices into the flattened vertex array
    pub fn triangulate(&self) -> Result<Triangulation> {
        if self.outer.len() < 3 {
            return Err(Error::InvalidProfile(
                "Profile must have at least 3 vertices".to_string(),
            ));
        }

        // Flatten vertices for earcutr
        let mut vertices = Vec::with_capacity(
            (self.outer.len() + self.holes.iter().map(|h| h.len()).sum::<usize>()) * 2,
        );

        // Add outer boundary
        for p in &self.outer {
            vertices.push(p.x);
            vertices.push(p.y);
        }

        // Add holes
        let mut hole_indices = Vec::with_capacity(self.holes.len());
        for hole in &self.holes {
            hole_indices.push(vertices.len() / 2);
            for p in hole {
                vertices.push(p.x);
                vertices.push(p.y);
            }
        }

        // Triangulate
        let indices = if hole_indices.is_empty() {
            earcutr::earcut(&vertices, &[], 2)
                .map_err(|e| Error::TriangulationError(format!("{:?}", e)))?
        } else {
            earcutr::earcut(&vertices, &hole_indices, 2)
                .map_err(|e| Error::TriangulationError(format!("{:?}", e)))?
        };

        // Convert to Point2 array
        let mut points = Vec::with_capacity(vertices.len() / 2);
        for i in (0..vertices.len()).step_by(2) {
            if i + 1 >= vertices.len() {
                break;
            }
            points.push(Point2::new(vertices[i], vertices[i + 1]));
        }

        Ok(Triangulation { points, indices })
    }
}

/// Triangulated profile result
#[derive(Debug, Clone)]
pub struct Triangulation {
    /// All vertices (outer + holes)
    pub points: Vec<Point2<f64>>,
    /// Triangle indices
    pub indices: Vec<usize>,
}

/// Void metadata for depth-aware extrusion
///
/// Tracks information about a void that has been projected to the 2D profile plane,
/// including its depth range for generating internal caps when the void doesn't
/// extend through the full extrusion depth.
#[derive(Debug, Clone)]
pub struct VoidInfo {
    /// Hole contour in 2D profile space (clockwise winding for holes)
    pub contour: Vec<Point2<f64>>,
    /// Start depth in extrusion space (0.0 = bottom cap)
    pub depth_start: f64,
    /// End depth in extrusion space (extrusion_depth = top cap)
    pub depth_end: f64,
    /// Whether void extends full depth (no internal caps needed)
    pub is_through: bool,
}

impl VoidInfo {
    /// Create a new void info
    pub fn new(
        contour: Vec<Point2<f64>>,
        depth_start: f64,
        depth_end: f64,
        is_through: bool,
    ) -> Self {
        Self {
            contour,
            depth_start,
            depth_end,
            is_through,
        }
    }

    /// Create a through void (extends full depth)
    pub fn through(contour: Vec<Point2<f64>>, depth: f64) -> Self {
        Self {
            contour,
            depth_start: 0.0,
            depth_end: depth,
            is_through: true,
        }
    }
}

/// Profile with void tracking for depth-aware extrusion
///
/// Extends Profile2D with metadata about voids that have been classified as
/// coplanar and can be handled at the profile level. This allows for:
/// - Through voids: Added as holes before single extrusion
/// - Partial-depth voids: Generate internal caps at depth boundaries
#[derive(Debug, Clone)]
pub struct Profile2DWithVoids {
    /// Base profile (outer boundary + any existing holes)
    pub profile: Profile2D,
    /// Void metadata for depth-aware extrusion
    pub voids: Vec<VoidInfo>,
}

impl Profile2DWithVoids {
    /// Create a new profile with voids
    pub fn new(profile: Profile2D, voids: Vec<VoidInfo>) -> Self {
        Self { profile, voids }
    }

    /// Create from a base profile with no voids
    pub fn from_profile(profile: Profile2D) -> Self {
        Self {
            profile,
            voids: Vec::new(),
        }
    }

    /// Add a void to the profile
    pub fn add_void(&mut self, void: VoidInfo) {
        self.voids.push(void);
    }

    /// Get all through voids (can be added as simple holes)
    pub fn through_voids(&self) -> impl Iterator<Item = &VoidInfo> {
        self.voids.iter().filter(|v| v.is_through)
    }

    /// Get all partial-depth voids (need internal caps)
    pub fn partial_voids(&self) -> impl Iterator<Item = &VoidInfo> {
        self.voids.iter().filter(|v| !v.is_through)
    }

    /// Check if there are any voids
    pub fn has_voids(&self) -> bool {
        !self.voids.is_empty()
    }

    /// Get number of voids
    pub fn void_count(&self) -> usize {
        self.voids.len()
    }

    /// Create a profile with through-voids merged as holes
    ///
    /// Returns a Profile2D where all through-voids have been added as holes,
    /// suitable for single-pass extrusion.
    pub fn profile_with_through_holes(&self) -> Profile2D {
        let mut profile = self.profile.clone();

        for void in self.through_voids() {
            profile.add_hole(void.contour.clone());
        }

        profile
    }
}

/// Common profile types
#[derive(Debug, Clone)]
pub enum ProfileType {
    Rectangle {
        width: f64,
        height: f64,
    },
    Circle {
        radius: f64,
    },
    HollowCircle {
        outer_radius: f64,
        inner_radius: f64,
    },
    Polygon {
        points: Vec<Point2<f64>>,
    },
}

impl ProfileType {
    /// Convert to Profile2D
    pub fn to_profile(&self) -> Profile2D {
        match self {
            Self::Rectangle { width, height } => create_rectangle(*width, *height),
            Self::Circle { radius } => create_circle(*radius, None),
            Self::HollowCircle {
                outer_radius,
                inner_radius,
            } => create_circle(*outer_radius, Some(*inner_radius)),
            Self::Polygon { points } => Profile2D::new(points.clone()),
        }
    }
}

/// Create a rectangular profile
#[inline]
pub fn create_rectangle(width: f64, height: f64) -> Profile2D {
    let half_w = width / 2.0;
    let half_h = height / 2.0;

    Profile2D::new(vec![
        Point2::new(-half_w, -half_h),
        Point2::new(half_w, -half_h),
        Point2::new(half_w, half_h),
        Point2::new(-half_w, half_h),
    ])
}

/// Create a circular profile (with optional hole)
/// segments: Number of segments (None = auto-calculate based on radius)
pub fn create_circle(radius: f64, hole_radius: Option<f64>) -> Profile2D {
    let segments = calculate_circle_segments(radius);

    let mut outer = Vec::with_capacity(segments);

    for i in 0..segments {
        let angle = 2.0 * std::f64::consts::PI * (i as f64) / (segments as f64);
        outer.push(Point2::new(radius * angle.cos(), radius * angle.sin()));
    }

    let mut profile = Profile2D::new(outer);

    // Add hole if specified
    if let Some(hole_r) = hole_radius {
        let hole_segments = calculate_circle_segments(hole_r);
        let mut hole = Vec::with_capacity(hole_segments);

        for i in 0..hole_segments {
            let angle = 2.0 * std::f64::consts::PI * (i as f64) / (hole_segments as f64);
            // Reverse winding for hole (clockwise)
            hole.push(Point2::new(hole_r * angle.cos(), hole_r * angle.sin()));
        }
        hole.reverse(); // Make clockwise

        profile.add_hole(hole);
    }

    profile
}

/// Calculate adaptive number of segments for a circle
/// Based on radius to maintain good visual quality
#[inline]
pub fn calculate_circle_segments(radius: f64) -> usize {
    // Adaptive segment calculation - optimized for performance
    // Smaller circles need fewer segments
    let segments = (radius.sqrt() * 8.0).ceil() as usize;

    // Clamp between 8 and 32 segments (reduced for performance)
    segments.clamp(8, 32)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rectangle_profile() {
        let profile = create_rectangle(10.0, 5.0);
        assert_eq!(profile.outer.len(), 4);
        assert_eq!(profile.holes.len(), 0);

        // Check bounds
        assert_eq!(profile.outer[0], Point2::new(-5.0, -2.5));
        assert_eq!(profile.outer[1], Point2::new(5.0, -2.5));
        assert_eq!(profile.outer[2], Point2::new(5.0, 2.5));
        assert_eq!(profile.outer[3], Point2::new(-5.0, 2.5));
    }

    #[test]
    fn test_circle_profile() {
        let profile = create_circle(5.0, None);
        assert!(profile.outer.len() >= 8);
        assert_eq!(profile.holes.len(), 0);

        // Check first point is on circle
        let first = profile.outer[0];
        let dist = (first.x * first.x + first.y * first.y).sqrt();
        assert!((dist - 5.0).abs() < 0.001);
    }

    #[test]
    fn test_hollow_circle() {
        let profile = create_circle(10.0, Some(5.0));
        assert!(profile.outer.len() >= 8);
        assert_eq!(profile.holes.len(), 1);

        // Check hole
        let hole = &profile.holes[0];
        assert!(hole.len() >= 8);
    }

    #[test]
    fn test_triangulate_rectangle() {
        let profile = create_rectangle(10.0, 5.0);
        let tri = profile.triangulate().unwrap();

        assert_eq!(tri.points.len(), 4);
        assert_eq!(tri.indices.len(), 6); // 2 triangles = 6 indices
    }

    #[test]
    fn test_triangulate_circle() {
        let profile = create_circle(5.0, None);
        let tri = profile.triangulate().unwrap();

        assert!(tri.points.len() >= 8);
        // Triangle count should be points - 2
        assert_eq!(tri.indices.len(), (tri.points.len() - 2) * 3);
    }

    #[test]
    fn test_triangulate_hollow_circle() {
        let profile = create_circle(10.0, Some(5.0));
        let tri = profile.triangulate().unwrap();

        // Should have vertices from both outer and inner circles
        let outer_count = calculate_circle_segments(10.0);
        let inner_count = calculate_circle_segments(5.0);
        assert_eq!(tri.points.len(), outer_count + inner_count);
    }

    #[test]
    fn test_circle_segments() {
        assert_eq!(calculate_circle_segments(1.0), 8); // sqrt(1)*8=8, clamped to min 8
        assert_eq!(calculate_circle_segments(4.0), 16); // sqrt(4)*8=16
        assert!(calculate_circle_segments(100.0) <= 32); // Max clamp at 32
        assert!(calculate_circle_segments(0.1) >= 8); // Min clamp
    }
}