brepkit-math 3.2.2

Vector math, transforms, NURBS, and geometric predicates for brepkit
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
//! Flat-array AABB tree for broad-phase spatial queries.
//!
//! Uses SAH (Surface Area Heuristic) for tree construction, consistent
//! with the arena-based patterns used in the topology crate.

use crate::aabb::Aabb3;
use crate::vec::Point3;

/// A node in the flat-array BVH.
#[derive(Debug, Clone, Copy)]
struct BvhNode {
    /// Bounding box of this node.
    aabb: Aabb3,
    /// For leaf nodes: the index of the primitive. For internal nodes: `usize::MAX`.
    primitive: usize,
    /// Index of the left child. `usize::MAX` for leaf nodes.
    left: usize,
    /// Index of the right child. `usize::MAX` for leaf nodes.
    right: usize,
}

/// A flat-array AABB tree for spatial queries.
#[derive(Debug, Clone)]
pub struct Bvh {
    nodes: Vec<BvhNode>,
}

impl Bvh {
    /// Build a BVH from a set of `(primitive_id, aabb)` pairs.
    ///
    /// Uses SAH-based construction for good query performance. If `aabbs`
    /// is empty, returns an empty BVH.
    #[must_use]
    pub fn build(aabbs: &[(usize, Aabb3)]) -> Self {
        if aabbs.is_empty() {
            return Self { nodes: Vec::new() };
        }

        let mut indices: Vec<usize> = (0..aabbs.len()).collect();
        let mut nodes = Vec::with_capacity(2 * aabbs.len());
        build_recursive(aabbs, &mut indices, &mut nodes);
        Self { nodes }
    }

    /// Query all primitives whose AABB overlaps `test`.
    #[must_use]
    pub fn query_overlap(&self, test: &Aabb3) -> Vec<usize> {
        let mut results = Vec::new();
        self.query_overlap_into(test, &mut results);
        results
    }

    /// Query primitives overlapping `test`, reusing caller-provided buffers.
    ///
    /// `results` is cleared before use. This avoids per-call allocation when
    /// called in a tight loop (e.g. compound boolean inner loops).
    pub fn query_overlap_into(&self, test: &Aabb3, results: &mut Vec<usize>) {
        results.clear();
        if self.nodes.is_empty() {
            return;
        }
        let mut stack = vec![0usize];
        while let Some(idx) = stack.pop() {
            let node = &self.nodes[idx];
            if !node.aabb.intersects(*test) {
                continue;
            }
            if node.primitive == usize::MAX {
                stack.push(node.left);
                stack.push(node.right);
            } else {
                results.push(node.primitive);
            }
        }
    }

    /// Query all primitives whose AABB is intersected by a ray.
    ///
    /// `origin` is the ray start, `dir` is the ray direction (need not be
    /// normalised). Only positive-`t` hits are returned.
    #[must_use]
    pub fn query_ray(&self, origin: Point3, dir: crate::vec::Vec3) -> Vec<usize> {
        let mut results = Vec::new();
        self.query_ray_into(origin, dir, &mut results);
        results
    }

    /// Query ray-intersecting primitives, reusing caller-provided buffers.
    ///
    /// `results` is cleared before use. This avoids per-call allocation when
    /// called in a tight loop.
    pub fn query_ray_into(&self, origin: Point3, dir: crate::vec::Vec3, results: &mut Vec<usize>) {
        results.clear();
        if self.nodes.is_empty() {
            return;
        }
        // Pre-compute inverse direction for slab tests.
        let inv_dir = crate::vec::Vec3::new(1.0 / dir.x(), 1.0 / dir.y(), 1.0 / dir.z());
        let mut stack = vec![0usize];
        while let Some(idx) = stack.pop() {
            let node = &self.nodes[idx];
            if !node.aabb.ray_intersects(origin, inv_dir) {
                continue;
            }
            if node.primitive == usize::MAX {
                stack.push(node.left);
                stack.push(node.right);
            } else {
                results.push(node.primitive);
            }
        }
    }

    /// Find the primitive whose AABB is closest to `point`.
    ///
    /// **Note**: This returns the primitive with the closest *AABB*, which is
    /// a lower bound on the actual distance. For exact closest-primitive
    /// queries, use [`Bvh::query_closest_with_distance`] with a callback that
    /// computes the true distance to each primitive.
    ///
    /// Returns `None` if the BVH is empty.
    #[must_use]
    pub fn query_closest(&self, point: Point3) -> Option<usize> {
        if self.nodes.is_empty() {
            return None;
        }

        let mut best_id = None;
        let mut best_dist = f64::INFINITY;
        let mut stack = vec![0usize];

        while let Some(idx) = stack.pop() {
            let node = &self.nodes[idx];
            let node_dist = node.aabb.distance_squared_to_point(point);
            if node_dist >= best_dist {
                continue;
            }
            if node.primitive == usize::MAX {
                // Visit closer child first for better pruning.
                let dl = self.nodes[node.left].aabb.distance_squared_to_point(point);
                let dr = self.nodes[node.right].aabb.distance_squared_to_point(point);
                if dl < dr {
                    stack.push(node.right);
                    stack.push(node.left);
                } else {
                    stack.push(node.left);
                    stack.push(node.right);
                }
            } else {
                best_id = Some(node.primitive);
                best_dist = node_dist;
            }
        }

        best_id
    }

    /// Find the closest primitive to `point` using an exact distance callback.
    ///
    /// Unlike [`Bvh::query_closest`], this uses the provided `distance_sq` callback
    /// to compute the actual squared distance from `point` to each candidate
    /// primitive, ensuring the true closest primitive is returned. The BVH
    /// AABB distances are used only for pruning, so distant subtrees are
    /// skipped efficiently.
    ///
    /// Returns `None` if the BVH is empty.
    #[must_use]
    pub fn query_closest_with_distance(
        &self,
        point: Point3,
        distance_sq: &dyn Fn(usize) -> f64,
    ) -> Option<usize> {
        if self.nodes.is_empty() {
            return None;
        }

        let mut best_id = None;
        let mut best_dist = f64::INFINITY;
        let mut stack = vec![0usize];

        while let Some(idx) = stack.pop() {
            let node = &self.nodes[idx];
            let node_dist = node.aabb.distance_squared_to_point(point);
            if node_dist >= best_dist {
                continue;
            }
            if node.primitive == usize::MAX {
                let dl = self.nodes[node.left].aabb.distance_squared_to_point(point);
                let dr = self.nodes[node.right].aabb.distance_squared_to_point(point);
                if dl < dr {
                    stack.push(node.right);
                    stack.push(node.left);
                } else {
                    stack.push(node.left);
                    stack.push(node.right);
                }
            } else {
                let actual_dist = distance_sq(node.primitive);
                if actual_dist < best_dist {
                    best_dist = actual_dist;
                    best_id = Some(node.primitive);
                }
            }
        }

        best_id
    }
}

/// Recursively build the BVH, appending nodes to `nodes`.
#[allow(clippy::expect_used, clippy::cast_precision_loss)]
fn build_recursive(
    aabbs: &[(usize, Aabb3)],
    indices: &mut [usize],
    nodes: &mut Vec<BvhNode>,
) -> usize {
    let node_idx = nodes.len();

    if indices.len() == 1 {
        let i = indices[0];
        nodes.push(BvhNode {
            aabb: aabbs[i].1,
            primitive: aabbs[i].0,
            left: usize::MAX,
            right: usize::MAX,
        });
        return node_idx;
    }

    // Compute bounding box of all primitives in this subset.
    let combined = indices
        .iter()
        .map(|&i| aabbs[i].1)
        .reduce(super::aabb::Aabb3::union)
        .expect("non-empty");

    if indices.len() == 2 {
        // Direct leaf pair.
        nodes.push(BvhNode {
            aabb: combined,
            primitive: usize::MAX,
            left: 0,
            right: 0,
        });
        let left = build_recursive(aabbs, &mut indices[..1], nodes);
        let right = build_recursive(aabbs, &mut indices[1..], nodes);
        nodes[node_idx].left = left;
        nodes[node_idx].right = right;
        return node_idx;
    }

    // SAH split: try each axis and find the best split.
    let parent_area = combined.surface_area();
    let mut best_cost = f64::INFINITY;
    let mut best_axis = 0;
    let mut best_split = indices.len() / 2;

    for axis in 0..3 {
        indices.sort_by(|&a, &b| {
            let ca = centroid_axis(aabbs[a].1, axis);
            let cb = centroid_axis(aabbs[b].1, axis);
            ca.partial_cmp(&cb).unwrap_or(std::cmp::Ordering::Equal)
        });

        let n = indices.len();

        // Precompute suffix unions so each split candidate is O(1).
        let mut suffix = vec![aabbs[indices[n - 1]].1; n];
        for k in (0..n - 1).rev() {
            suffix[k] = suffix[k + 1].union(aabbs[indices[k]].1);
        }

        let mut left_aabb = aabbs[indices[0]].1;
        for split in 1..n {
            left_aabb = left_aabb.union(aabbs[indices[split - 1]].1);
            let right_aabb = suffix[split];

            let cost = (split as f64).mul_add(
                left_aabb.surface_area(),
                (n - split) as f64 * right_aabb.surface_area(),
            ) / parent_area;

            if cost < best_cost {
                best_cost = cost;
                best_axis = axis;
                best_split = split;
            }
        }
    }

    // Re-sort by the best axis.
    indices.sort_by(|&a, &b| {
        let ca = centroid_axis(aabbs[a].1, best_axis);
        let cb = centroid_axis(aabbs[b].1, best_axis);
        ca.partial_cmp(&cb).unwrap_or(std::cmp::Ordering::Equal)
    });

    // Allocate internal node.
    nodes.push(BvhNode {
        aabb: combined,
        primitive: usize::MAX,
        left: 0,
        right: 0,
    });

    let (left_indices, right_indices) = indices.split_at_mut(best_split);
    let left = build_recursive(aabbs, left_indices, nodes);
    let right = build_recursive(aabbs, right_indices, nodes);
    nodes[node_idx].left = left;
    nodes[node_idx].right = right;

    node_idx
}

/// Get the centroid coordinate along a specific axis.
fn centroid_axis(aabb: Aabb3, axis: usize) -> f64 {
    match axis {
        0 => aabb.center().x(),
        1 => aabb.center().y(),
        _ => aabb.center().z(),
    }
}

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

    fn make_box(id: usize, x: f64, y: f64, z: f64, size: f64) -> (usize, Aabb3) {
        (
            id,
            Aabb3::from_points([
                Point3::new(x, y, z),
                Point3::new(x + size, y + size, z + size),
            ]),
        )
    }

    #[test]
    fn bvh_empty() {
        let bvh = Bvh::build(&[]);
        assert!(
            bvh.query_overlap(&Aabb3::from_points([Point3::new(0.0, 0.0, 0.0)]))
                .is_empty()
        );
        assert!(bvh.query_closest(Point3::new(0.0, 0.0, 0.0)).is_none());
    }

    #[test]
    fn bvh_single() {
        let aabbs = vec![make_box(42, 0.0, 0.0, 0.0, 1.0)];
        let bvh = Bvh::build(&aabbs);

        let hits = bvh.query_overlap(&Aabb3::from_points([
            Point3::new(0.5, 0.5, 0.5),
            Point3::new(0.6, 0.6, 0.6),
        ]));
        assert_eq!(hits, vec![42]);

        let miss = bvh.query_overlap(&Aabb3::from_points([
            Point3::new(5.0, 5.0, 5.0),
            Point3::new(6.0, 6.0, 6.0),
        ]));
        assert!(miss.is_empty());
    }

    #[test]
    fn bvh_multiple_overlap() {
        let aabbs = vec![
            make_box(0, 0.0, 0.0, 0.0, 1.0),
            make_box(1, 2.0, 0.0, 0.0, 1.0),
            make_box(2, 4.0, 0.0, 0.0, 1.0),
            make_box(3, 0.0, 2.0, 0.0, 1.0),
        ];
        let bvh = Bvh::build(&aabbs);

        // Query that overlaps box 0 and 1
        let test = Aabb3::from_points([Point3::new(0.5, 0.5, 0.5), Point3::new(2.5, 0.5, 0.5)]);
        let mut hits = bvh.query_overlap(&test);
        hits.sort_unstable();
        assert_eq!(hits, vec![0, 1]);
    }

    #[test]
    fn bvh_closest() {
        let aabbs = vec![
            make_box(0, 0.0, 0.0, 0.0, 1.0),
            make_box(1, 10.0, 10.0, 10.0, 1.0),
            make_box(2, 5.0, 5.0, 5.0, 1.0),
        ];
        let bvh = Bvh::build(&aabbs);

        assert_eq!(bvh.query_closest(Point3::new(0.5, 0.5, 0.5)), Some(0));
        assert_eq!(bvh.query_closest(Point3::new(10.5, 10.5, 10.5)), Some(1));
    }

    #[test]
    #[allow(clippy::cast_precision_loss)]
    fn bvh_many_primitives() {
        let aabbs: Vec<(usize, Aabb3)> = (0..100)
            .map(|i| make_box(i, i as f64 * 2.0, 0.0, 0.0, 1.0))
            .collect();
        let bvh = Bvh::build(&aabbs);

        // Query around primitive 50
        let test = Aabb3::from_points([Point3::new(99.5, 0.0, 0.0), Point3::new(100.5, 1.0, 1.0)]);
        let hits = bvh.query_overlap(&test);
        assert!(
            hits.contains(&50),
            "expected primitive 50 in hits: {hits:?}"
        );
    }
}