bbox 0.15.0

Managing axis aligned Bounding Boxes.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! bbox is crate for managing axis aligned Bounding Boxes.
//! Bounding Boxes can be created, dilated, transformed and joined with other Bounding Boxes using
//! CSG operations.
//! Finally you can test whether or not a Bounding Box contains some point and what approximate
//! distance a Point has to the Box.
//! # Examples
//!
//! Insert points into a Bounding Box:
//!
//! ```rust
//! use nalgebra as na;
//! let mut bbox = bbox::BoundingBox::neg_infinity();
//! bbox.insert(&na::Point::from([0., 0., 0.]));
//! bbox.insert(&na::Point::from([1., 2., 3.]));
//!
//! // or insert multiple points at once
//!
//! let bbox = bbox::BoundingBox::from([na::Point::from([0., 0., 0.]),
//!                                     na::Point::from([1., 2., 3.])]);
//! ```
//! Intersect two Bounding Boxes:
//!
//! ```rust
//! use nalgebra as na;
//! let bbox1 = bbox::BoundingBox::new(&na::Point::from([0., 0., 0.]),
//!                                    &na::Point::from([1., 2., 3.]));
//! let bbox2 = bbox::BoundingBox::new(&na::Point::from([-1., -2., -3.]),
//!                                    &na::Point::from([3., 2., 1.]));
//! let intersection = bbox1.intersection(&bbox2);
//! ```
//! Rotate a Bounding Box:
//!
//! ```rust
//! use nalgebra as na;
//! let rotation = na::Rotation::from_euler_angles(10., 11., 12.);
//! let bbox = bbox::BoundingBox::new(&na::Point::from([0., 0., 0.]),
//!                                   &na::Point::from([1., 2., 3.]));
//! let rotated_box = bbox.transform(&rotation.to_homogeneous());
//! ```
//! Is a point contained in the Box?
//!
//! ```rust
//! use nalgebra as na;
//! let bbox = bbox::BoundingBox::new(&na::Point::from([0., 0., 0.]),
//!                                   &na::Point::from([1., 2., 3.]));
//! let result = bbox.contains(&na::Point::from([1., 1., 1.]));
//! ```
//! Calculate approximate distance of a point to the Box:
//!
//! ```rust
//! use nalgebra as na;
//! let bbox = bbox::BoundingBox::new(&na::Point::from([0., 0., 0.]),
//!                                   &na::Point::from([1., 2., 3.]));
//! let distance = bbox.distance(&na::Point::from([1., 1., 1.]));
//! ```
//! ## Cargo Features
//!
//! * `mint` - Enable interoperation with other math libraries through the
//!   [`mint`](https://crates.io/crates/mint) interface.

#![warn(missing_docs)]

use approx::{AbsDiffEq, RelativeEq};
use nalgebra as na;
use num_traits::Float;
use std::fmt::Debug;

/// Apply a binary operation to every coordinate of a and b
fn apply_binary_op<S: 'static + Float + Debug, const D: usize>(
    a: &na::Point<S, D>,
    b: &na::Point<S, D>,
    op: fn(S, S) -> S,
) -> na::Point<S, D> {
    let mut best: na::Point<S, D> = na::Point::default();
    for (best_i, (a_i, b_i)) in best.iter_mut().zip(a.iter().zip(b.iter())) {
        *best_i = op(*a_i, *b_i);
    }
    best
}

/// Fold points with a binary operation applied to every coordinate
fn fold_points<'a, S: 'static + Float + Debug, const D: usize>(
    initial: na::Point<S, D>,
    points: impl Iterator<Item = &'a na::Point<S, D>>,
    op: fn(S, S) -> S,
) -> na::Point<S, D> {
    points.fold(initial, |best, current| apply_binary_op(&best, current, op))
}

fn point_min<S: 'static + Float + Debug, const D: usize>(
    a: &na::Point<S, D>,
    b: &na::Point<S, D>,
) -> na::Point<S, D> {
    apply_binary_op(a, b, S::min)
}

fn point_max<S: 'static + Float + Debug, const D: usize>(
    a: &na::Point<S, D>,
    b: &na::Point<S, D>,
) -> na::Point<S, D> {
    apply_binary_op(a, b, S::max)
}

fn points_min<'a, S: 'static + Float + Debug, const D: usize>(
    points: impl Iterator<Item = &'a na::Point<S, D>>,
) -> na::Point<S, D> {
    fold_points([S::infinity(); D].into(), points, S::min)
}

fn points_max<'a, S: 'static + Float + Debug, const D: usize>(
    points: impl Iterator<Item = &'a na::Point<S, D>>,
) -> na::Point<S, D> {
    fold_points([S::neg_infinity(); D].into(), points, S::max)
}

/// 3D Bounding Box - defined by two diagonally opposing points.
#[derive(Clone, Debug, PartialEq)]
pub struct BoundingBox<S: 'static + Debug + Copy + PartialEq, const D: usize> {
    /// X-Y-Z-Minimum corner of the box.
    pub min: na::Point<S, D>,
    /// X-Y-Z-Maximum corner of the box.
    pub max: na::Point<S, D>,
}

impl<S: Float + na::RealField, T: AsRef<[na::Point<S, D>]>, const D: usize> From<T>
    for BoundingBox<S, D>
{
    fn from(points: T) -> Self {
        Self {
            min: points_min(points.as_ref().iter()),
            max: points_max(points.as_ref().iter()),
        }
    }
}

impl<S: Float + Debug + na::RealField, const D: usize> BoundingBox<S, D> {
    /// Returns an infinte sized box.
    pub fn infinity() -> Self {
        Self {
            min: na::Point::from([S::neg_infinity(); D]),
            max: na::Point::from([S::infinity(); D]),
        }
    }
    /// Returns a negatively infinte sized box.
    pub fn neg_infinity() -> Self {
        Self {
            min: na::Point::from([S::infinity(); D]),
            max: na::Point::from([S::neg_infinity(); D]),
        }
    }
    /// Create a new Bounding Box by supplying two points.
    pub fn new(a: &na::Point<S, D>, b: &na::Point<S, D>) -> Self {
        Self {
            min: point_min(a, b),
            max: point_max(a, b),
        }
    }
    /// Returns true if the Bounding Box is empty.
    pub fn is_empty(&self) -> bool {
        self.min > self.max
    }
    /// Returns true if the Bounding Box has finite size.
    pub fn is_finite(&self) -> bool {
        self.min
            .iter()
            .chain(self.max.iter())
            .all(|x| x.is_finite())
    }
    /// Returns the center point of the Bounding Box.
    pub fn center(&self) -> na::Point<S, D> {
        self.min + (self.max - self.min) / S::from(2.0).unwrap()
    }
    /// Create a CSG Union of two Bounding Boxes.
    pub fn union(&self, other: &Self) -> Self {
        Self {
            min: point_min(&self.min, &other.min),
            max: point_max(&self.max, &other.max),
        }
    }
    /// Create a CSG Intersection of two Bounding Boxes.
    pub fn intersection(&self, other: &Self) -> Self {
        Self {
            min: point_max(&self.min, &other.min),
            max: point_min(&self.max, &other.max),
        }
    }
    /// Get the corners of the Bounding Box
    ///
    /// Warning: bounding box of dimension D has 2^D corners
    pub fn get_corners(&self) -> Vec<na::Point<S, D>> {
        (0..2usize.pow(D as u32))
            .map(|i| {
                na::Point::from(std::array::from_fn(|d| match (i >> d) & 1 {
                    0 => self.min[d],
                    1 => self.max[d],
                    _ => unreachable!(),
                }))
            })
            .collect()
    }
    /// Dilate a Bounding Box by some amount in all directions.
    pub fn dilate(&mut self, d: S) -> &mut Self {
        self.min.iter_mut().for_each(|coord| *coord -= d);
        self.max.iter_mut().for_each(|coord| *coord += d);
        self
    }
    /// Add a Point to a Bounding Box, e.g. expand the Bounding Box to contain that point.
    pub fn insert(&mut self, o: &na::Point<S, D>) -> &mut Self {
        self.min = point_min(&self.min, o);
        self.max = point_max(&self.max, o);
        self
    }
    /// Return the size of the Box.
    pub fn dim(&self) -> na::SVector<S, D> {
        self.max - self.min
    }
    /// Returns the approximate distance of p to the box. The result is guarateed to be not less
    /// than the euclidean distance of p to the box.
    pub fn distance(&self, point: &na::Point<S, D>) -> S {
        // If p is not inside (neg), then it is outside (pos) on only one side.
        // So so calculating the max of the diffs on both sides should result in the true value,
        // if positive.
        point_max(&(point - self.max).into(), &(self.min - point).into())
            .iter()
            .fold(S::neg_infinity(), |a, b| Float::max(a, *b))
    }
    /// Return true if the Bounding Box contains p.
    pub fn contains(&self, point: &na::Point<S, D>) -> bool {
        let is_bigger_than_min = self
            .min
            .iter()
            .zip(point.iter())
            .all(|(min_i, point_i)| *min_i <= *point_i);

        let is_smaller_than_max = self
            .max
            .iter()
            .zip(point.iter())
            .all(|(max_i, point_i)| *max_i >= *point_i);

        is_bigger_than_min && is_smaller_than_max
    }
}

impl<S: Float + Debug + na::RealField> BoundingBox<S, 3> {
    /// Transform a Bounding Box - resulting in a enclosing axis aligned Bounding Box.
    pub fn transform(&self, mat: &na::Matrix4<S>) -> Self {
        let corners = self.get_corners();
        let transformed: Vec<_> = corners
            .into_iter()
            .map(|c| mat.transform_point(&c))
            .collect();
        Self::from(&transformed)
    }
}

impl<T: Float, const D: usize> AbsDiffEq for BoundingBox<T, D>
where
    <T as AbsDiffEq>::Epsilon: Copy,
    T: AbsDiffEq + Debug,
{
    type Epsilon = <T as AbsDiffEq>::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        <T as AbsDiffEq>::default_epsilon()
    }

    fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
        na::Point::abs_diff_eq(&self.min, &other.min, epsilon)
            && na::Point::abs_diff_eq(&self.max, &other.max, epsilon)
    }
}

impl<T: Float, const D: usize> RelativeEq for BoundingBox<T, D>
where
    <T as AbsDiffEq>::Epsilon: Copy,
    T: RelativeEq + Debug,
{
    fn default_max_relative() -> <T as AbsDiffEq>::Epsilon {
        <T as RelativeEq>::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: <T as AbsDiffEq>::Epsilon,
        max_relative: <T as AbsDiffEq>::Epsilon,
    ) -> bool {
        na::Point::relative_eq(&self.min, &other.min, epsilon, max_relative)
            && na::Point::relative_eq(&self.max, &other.max, epsilon, max_relative)
    }
}

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

    #[test]
    fn box_contains_points_inside() {
        let bbox = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([1., 2., 3.]),
        );
        assert!(bbox.contains(&na::Point::from([0., 0., 0.])));
        assert!(bbox.contains(&na::Point::from([0., 1., 0.])));
        assert!(bbox.contains(&na::Point::from([1., 0., 1.])));
        assert!(bbox.contains(&na::Point::from([1., 1., 1.])));
        assert!(!bbox.contains(&na::Point::from([2., 2., 2.])));
        assert!(!bbox.contains(&na::Point::from([-1., -1., -1.])));
    }

    #[test]
    fn box_from_points() {
        let points = [
            na::Point::from([0., 0., 0.]),
            na::Point::from([1., 1., 0.]),
            na::Point::from([0., -2., 2.]),
        ];
        for bbox in [
            BoundingBox::from(points),            // from array
            BoundingBox::from(&points[..]),       // from slice
            BoundingBox::from(Vec::from(points)), // from vector
        ] {
            assert_relative_eq!(bbox.min, na::Point::from([0., -2., 0.]));
            assert_relative_eq!(bbox.max, na::Point::from([1., 1., 2.]));
        }
    }

    #[test]
    fn get_corners() {
        let bbox = BoundingBox::new(
            &na::Point::from([1., 2., 3.]),
            &na::Point::from([4., 5., 6.]),
        );
        let corners = bbox.get_corners();
        assert!(corners.contains(&na::Point::from([1., 2., 3.])));
        assert!(corners.contains(&na::Point::from([1., 2., 6.])));
        assert!(corners.contains(&na::Point::from([1., 5., 3.])));
        assert!(corners.contains(&na::Point::from([1., 5., 6.])));
        assert!(corners.contains(&na::Point::from([4., 2., 3.])));
        assert!(corners.contains(&na::Point::from([4., 2., 6.])));
        assert!(corners.contains(&na::Point::from([4., 5., 3.])));
        assert!(corners.contains(&na::Point::from([4., 5., 6.])));
    }

    #[test]
    fn is_empty() {
        let bbox = BoundingBox::<f64, 3>::neg_infinity();
        assert!(bbox.is_empty());
        let bbox = BoundingBox::from([na::Point::from([0., 0., 0.])]);
        assert!(!bbox.is_empty());
    }

    #[test]
    fn is_finite() {
        let bbox = BoundingBox::<f64, 3>::neg_infinity();
        assert!(!bbox.is_finite());
        let bbox = BoundingBox::from([na::Point::from([0., 0., 0.])]);
        assert!(bbox.is_finite());
        let bbox = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([f64::INFINITY, 1., 1.]),
        );
        assert!(!bbox.is_finite());
    }

    #[test]
    fn center() {
        let bbox = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([1., 1., 1.]),
        );
        assert_relative_eq!(bbox.center(), na::Point::from([0.5, 0.5, 0.5]));
    }

    #[test]
    fn transform_with_translation() {
        let bbox = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([1., 1., 1.]),
        );
        assert_relative_eq!(
            bbox.transform(&na::Translation3::new(1., 2., 3.).to_homogeneous()),
            BoundingBox::new(
                &na::Point::from([1., 2., 3.]),
                &na::Point::from([2., 3., 4.]),
            )
        );
    }

    #[test]
    fn transform_with_rotation() {
        let bbox = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([1., 1., 1.]),
        );
        assert_relative_eq!(
            bbox.transform(
                &na::Rotation::from_euler_angles(std::f64::consts::PI / 2., 0., 0.)
                    .to_homogeneous()
            ),
            BoundingBox::new(
                &na::Point::from([0., -1., 0.]),
                &na::Point::from([1., 0., 1.]),
            )
        );
    }

    #[test]
    fn union_of_two_boxes() {
        let bbox1 = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([4., 8., 16.]),
        );
        let bbox2 = BoundingBox::new(
            &na::Point::from([2., 2., 2.]),
            &na::Point::from([16., 4., 8.]),
        );
        assert_relative_eq!(
            bbox1.union(&bbox2),
            BoundingBox::new(
                &na::Point::from([0., 0., 0.]),
                &na::Point::from([16., 8., 16.]),
            )
        );
    }

    #[test]
    fn intersection_of_two_boxes() {
        let bbox1 = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([4., 8., 16.]),
        );
        let bbox2 = BoundingBox::new(
            &na::Point::from([2., 2., 2.]),
            &na::Point::from([16., 4., 8.]),
        );
        assert_relative_eq!(
            bbox1.intersection(&bbox2),
            BoundingBox::new(
                &na::Point::from([2., 2., 2.]),
                &na::Point::from([4., 4., 8.]),
            )
        );
    }

    #[test]
    fn dilate() {
        let mut bbox = BoundingBox::new(
            &na::Point::from([0., 0., 0.]),
            &na::Point::from([1., 1., 1.]),
        );
        assert_relative_eq!(
            bbox.dilate(0.1),
            &mut BoundingBox::new(
                &na::Point::from([-0.1, -0.1, -0.1]),
                &na::Point::from([1.1, 1.1, 1.1]),
            )
        );
        assert_relative_eq!(
            bbox.dilate(-0.5),
            &mut BoundingBox::new(
                &na::Point::from([0.4, 0.4, 0.4]),
                &na::Point::from([0.6, 0.6, 0.6]),
            )
        );
    }

    #[test]
    fn box_contains_inserted_points() {
        let mut bbox = BoundingBox::neg_infinity();
        let p1 = na::Point::from([1., 0., 0.]);
        let p2 = na::Point::from([0., 2., 3.]);
        assert!(!bbox.contains(&p1));
        bbox.insert(&p1);
        assert!(bbox.contains(&p1));
        assert!(!bbox.contains(&p2));
        bbox.insert(&p2);
        assert!(bbox.contains(&p2));
    }
}