1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9 
10 use super::UnknownUnit;
11 use crate::approxord::{max, min};
12 use crate::num::*;
13 use crate::point::{point3, Point3D};
14 use crate::scale::Scale;
15 use crate::size::Size3D;
16 use crate::vector::Vector3D;
17 
18 use num_traits::{NumCast, Float};
19 #[cfg(feature = "serde")]
20 use serde::{Deserialize, Serialize};
21 
22 use core::borrow::Borrow;
23 use core::cmp::PartialOrd;
24 use core::fmt;
25 use core::hash::{Hash, Hasher};
26 use core::ops::{Add, Div, DivAssign, Mul, MulAssign, Sub, Range};
27 
28 /// An axis aligned 3D box represented by its minimum and maximum coordinates.
29 #[repr(C)]
30 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31 #[cfg_attr(
32     feature = "serde",
33     serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>"))
34 )]
35 pub struct Box3D<T, U> {
36     pub min: Point3D<T, U>,
37     pub max: Point3D<T, U>,
38 }
39 
40 impl<T: Hash, U> Hash for Box3D<T, U> {
hash<H: Hasher>(&self, h: &mut H)41     fn hash<H: Hasher>(&self, h: &mut H) {
42         self.min.hash(h);
43         self.max.hash(h);
44     }
45 }
46 
47 impl<T: Copy, U> Copy for Box3D<T, U> {}
48 
49 impl<T: Clone, U> Clone for Box3D<T, U> {
clone(&self) -> Self50     fn clone(&self) -> Self {
51         Self::new(self.min.clone(), self.max.clone())
52     }
53 }
54 
55 impl<T: PartialEq, U> PartialEq for Box3D<T, U> {
eq(&self, other: &Self) -> bool56     fn eq(&self, other: &Self) -> bool {
57         self.min.eq(&other.min) && self.max.eq(&other.max)
58     }
59 }
60 
61 impl<T: Eq, U> Eq for Box3D<T, U> {}
62 
63 impl<T: fmt::Debug, U> fmt::Debug for Box3D<T, U> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result64     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65         f.debug_tuple("Box3D")
66             .field(&self.min)
67             .field(&self.max)
68             .finish()
69     }
70 }
71 
72 impl<T, U> Box3D<T, U> {
73     /// Constructor.
74     #[inline]
new(min: Point3D<T, U>, max: Point3D<T, U>) -> Self75     pub const fn new(min: Point3D<T, U>, max: Point3D<T, U>) -> Self {
76         Box3D { min, max }
77     }
78 
79     /// Creates a Box3D of the given size, at offset zero.
80     #[inline]
from_size(size: Size3D<T, U>) -> Self where T: Zero81     pub fn from_size(size: Size3D<T, U>) -> Self where T: Zero {
82         Box3D {
83             min: Point3D::zero(),
84             max: point3(size.width, size.height, size.depth),
85         }
86     }
87 }
88 
89 impl<T, U> Box3D<T, U>
90 where
91     T: PartialOrd,
92 {
93     /// Returns true if the box has a negative volume.
94     ///
95     /// The common interpretation for a negative box is to consider it empty. It can be obtained
96     /// by calculating the intersection of two boxes that do not intersect.
97     #[inline]
is_negative(&self) -> bool98     pub fn is_negative(&self) -> bool {
99         self.max.x < self.min.x || self.max.y < self.min.y || self.max.z < self.min.z
100     }
101 
102     /// Returns true if the size is zero, negative or NaN.
103     #[inline]
is_empty(&self) -> bool104     pub fn is_empty(&self) -> bool {
105         !(self.max.x > self.min.x && self.max.y > self.min.y && self.max.z > self.min.z)
106     }
107 
108     #[inline]
intersects(&self, other: &Self) -> bool109     pub fn intersects(&self, other: &Self) -> bool {
110         self.min.x < other.max.x
111             && self.max.x > other.min.x
112             && self.min.y < other.max.y
113             && self.max.y > other.min.y
114             && self.min.z < other.max.z
115             && self.max.z > other.min.z
116     }
117 
118     /// Returns `true` if this box3d contains the point. Points are considered
119     /// in the box3d if they are on the front, left or top faces, but outside if they
120     /// are on the back, right or bottom faces.
121     #[inline]
contains(&self, other: Point3D<T, U>) -> bool122     pub fn contains(&self, other: Point3D<T, U>) -> bool {
123         self.min.x <= other.x
124             && other.x < self.max.x
125             && self.min.y <= other.y
126             && other.y < self.max.y
127             && self.min.z <= other.z
128             && other.z < self.max.z
129     }
130 
131     /// Returns `true` if this box3d contains the interior of the other box3d. Always
132     /// returns `true` if other is empty, and always returns `false` if other is
133     /// nonempty but this box3d is empty.
134     #[inline]
contains_box(&self, other: &Self) -> bool135     pub fn contains_box(&self, other: &Self) -> bool {
136         other.is_empty()
137             || (self.min.x <= other.min.x
138                 && other.max.x <= self.max.x
139                 && self.min.y <= other.min.y
140                 && other.max.y <= self.max.y
141                 && self.min.z <= other.min.z
142                 && other.max.z <= self.max.z)
143     }
144 }
145 
146 impl<T, U> Box3D<T, U>
147 where
148     T: Copy + PartialOrd,
149 {
150     #[inline]
to_non_empty(&self) -> Option<Self>151     pub fn to_non_empty(&self) -> Option<Self> {
152         if self.is_empty() {
153             return None;
154         }
155 
156         Some(*self)
157     }
158 
159     #[inline]
intersection(&self, other: &Self) -> Option<Self>160     pub fn intersection(&self, other: &Self) -> Option<Self> {
161         let b = self.intersection_unchecked(other);
162 
163         if b.is_empty() {
164             return None;
165         }
166 
167         Some(b)
168     }
169 
intersection_unchecked(&self, other: &Self) -> Self170     pub fn intersection_unchecked(&self, other: &Self) -> Self {
171         let intersection_min = Point3D::new(
172             max(self.min.x, other.min.x),
173             max(self.min.y, other.min.y),
174             max(self.min.z, other.min.z),
175         );
176 
177         let intersection_max = Point3D::new(
178             min(self.max.x, other.max.x),
179             min(self.max.y, other.max.y),
180             min(self.max.z, other.max.z),
181         );
182 
183         Box3D::new(intersection_min, intersection_max)
184     }
185 
186     /// Computes the union of two boxes.
187     ///
188     /// If either of the boxes is empty, the other one is returned.
189     #[inline]
union(&self, other: &Self) -> Self190     pub fn union(&self, other: &Self) -> Self {
191         if other.is_empty() {
192             return *self;
193         }
194         if self.is_empty() {
195             return *other;
196         }
197 
198         Box3D::new(
199             Point3D::new(
200                 min(self.min.x, other.min.x),
201                 min(self.min.y, other.min.y),
202                 min(self.min.z, other.min.z),
203             ),
204             Point3D::new(
205                 max(self.max.x, other.max.x),
206                 max(self.max.y, other.max.y),
207                 max(self.max.z, other.max.z),
208             ),
209         )
210     }
211 }
212 
213 impl<T, U> Box3D<T, U>
214 where
215     T: Copy + Add<T, Output = T>,
216 {
217     /// Returns the same box3d, translated by a vector.
218     #[inline]
219     #[must_use]
translate(&self, by: Vector3D<T, U>) -> Self220     pub fn translate(&self, by: Vector3D<T, U>) -> Self {
221         Box3D {
222             min: self.min + by,
223             max: self.max + by,
224         }
225     }
226 }
227 
228 impl<T, U> Box3D<T, U>
229 where
230     T: Copy + Sub<T, Output = T>,
231 {
232     #[inline]
size(&self) -> Size3D<T, U>233     pub fn size(&self) -> Size3D<T, U> {
234         Size3D::new(
235             self.max.x - self.min.x,
236             self.max.y - self.min.y,
237             self.max.z - self.min.z,
238         )
239     }
240 
241     #[inline]
width(&self) -> T242     pub fn width(&self) -> T {
243         self.max.x - self.min.x
244     }
245 
246     #[inline]
height(&self) -> T247     pub fn height(&self) -> T {
248         self.max.y - self.min.y
249     }
250 
251     #[inline]
depth(&self) -> T252     pub fn depth(&self) -> T {
253         self.max.z - self.min.z
254     }
255 }
256 
257 impl<T, U> Box3D<T, U>
258 where
259     T: Copy + Add<T, Output = T> + Sub<T, Output = T>,
260 {
261     /// Inflates the box by the specified sizes on each side respectively.
262     #[inline]
263     #[must_use]
inflate(&self, width: T, height: T, depth: T) -> Self264     pub fn inflate(&self, width: T, height: T, depth: T) -> Self {
265         Box3D::new(
266             Point3D::new(self.min.x - width, self.min.y - height, self.min.z - depth),
267             Point3D::new(self.max.x + width, self.max.y + height, self.max.z + depth),
268         )
269     }
270 }
271 
272 impl<T, U> Box3D<T, U>
273 where
274     T: Copy + Zero + PartialOrd,
275 {
276     /// Returns the smallest box containing all of the provided points.
from_points<I>(points: I) -> Self where I: IntoIterator, I::Item: Borrow<Point3D<T, U>>,277     pub fn from_points<I>(points: I) -> Self
278     where
279         I: IntoIterator,
280         I::Item: Borrow<Point3D<T, U>>,
281     {
282         let mut points = points.into_iter();
283 
284         let (mut min_x, mut min_y, mut min_z) = match points.next() {
285             Some(first) => first.borrow().to_tuple(),
286             None => return Box3D::zero(),
287         };
288         let (mut max_x, mut max_y, mut max_z) = (min_x, min_y, min_z);
289 
290         for point in points {
291             let p = point.borrow();
292             if p.x < min_x {
293                 min_x = p.x
294             }
295             if p.x > max_x {
296                 max_x = p.x
297             }
298             if p.y < min_y {
299                 min_y = p.y
300             }
301             if p.y > max_y {
302                 max_y = p.y
303             }
304             if p.z < min_z {
305                 min_z = p.z
306             }
307             if p.z > max_z {
308                 max_z = p.z
309             }
310         }
311 
312         Box3D {
313             min: point3(min_x, min_y, min_z),
314             max: point3(max_x, max_y, max_z),
315         }
316     }
317 }
318 
319 impl<T, U> Box3D<T, U>
320 where
321     T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
322 {
323     /// Linearly interpolate between this box3d and another box3d.
324     #[inline]
lerp(&self, other: Self, t: T) -> Self325     pub fn lerp(&self, other: Self, t: T) -> Self {
326         Self::new(self.min.lerp(other.min, t), self.max.lerp(other.max, t))
327     }
328 }
329 
330 impl<T, U> Box3D<T, U>
331 where
332     T: Copy + One + Add<Output = T> + Div<Output = T>,
333 {
center(&self) -> Point3D<T, U>334     pub fn center(&self) -> Point3D<T, U> {
335         let two = T::one() + T::one();
336         (self.min + self.max.to_vector()) / two
337     }
338 }
339 
340 impl<T, U> Box3D<T, U>
341 where
342     T: Copy + Mul<T, Output = T> + Sub<T, Output = T>,
343 {
344     #[inline]
volume(&self) -> T345     pub fn volume(&self) -> T {
346         let size = self.size();
347         size.width * size.height * size.depth
348     }
349 
350     #[inline]
xy_area(&self) -> T351     pub fn xy_area(&self) -> T {
352         let size = self.size();
353         size.width * size.height
354     }
355 
356     #[inline]
yz_area(&self) -> T357     pub fn yz_area(&self) -> T {
358         let size = self.size();
359         size.depth * size.height
360     }
361 
362     #[inline]
xz_area(&self) -> T363     pub fn xz_area(&self) -> T {
364         let size = self.size();
365         size.depth * size.width
366     }
367 }
368 
369 impl<T, U> Box3D<T, U>
370 where
371     T: Zero,
372 {
373     /// Constructor, setting all sides to zero.
zero() -> Self374     pub fn zero() -> Self {
375         Box3D::new(Point3D::zero(), Point3D::zero())
376     }
377 }
378 
379 impl<T: Copy + Mul, U> Mul<T> for Box3D<T, U> {
380     type Output = Box3D<T::Output, U>;
381 
382     #[inline]
mul(self, scale: T) -> Self::Output383     fn mul(self, scale: T) -> Self::Output {
384         Box3D::new(self.min * scale, self.max * scale)
385     }
386 }
387 
388 impl<T: Copy + MulAssign, U> MulAssign<T> for Box3D<T, U> {
389     #[inline]
mul_assign(&mut self, scale: T)390     fn mul_assign(&mut self, scale: T) {
391         self.min *= scale;
392         self.max *= scale;
393     }
394 }
395 
396 impl<T: Copy + Div, U> Div<T> for Box3D<T, U> {
397     type Output = Box3D<T::Output, U>;
398 
399     #[inline]
div(self, scale: T) -> Self::Output400     fn div(self, scale: T) -> Self::Output {
401         Box3D::new(self.min / scale.clone(), self.max / scale)
402     }
403 }
404 
405 impl<T: Copy + DivAssign, U> DivAssign<T> for Box3D<T, U> {
406     #[inline]
div_assign(&mut self, scale: T)407     fn div_assign(&mut self, scale: T) {
408         self.min /= scale;
409         self.max /= scale;
410     }
411 }
412 
413 impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Box3D<T, U1> {
414     type Output = Box3D<T::Output, U2>;
415 
416     #[inline]
mul(self, scale: Scale<T, U1, U2>) -> Self::Output417     fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output {
418         Box3D::new(self.min * scale.clone(), self.max * scale)
419     }
420 }
421 
422 impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Box3D<T, U> {
423     #[inline]
mul_assign(&mut self, scale: Scale<T, U, U>)424     fn mul_assign(&mut self, scale: Scale<T, U, U>) {
425         self.min *= scale.clone();
426         self.max *= scale;
427     }
428 }
429 
430 impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Box3D<T, U2> {
431     type Output = Box3D<T::Output, U1>;
432 
433     #[inline]
div(self, scale: Scale<T, U1, U2>) -> Self::Output434     fn div(self, scale: Scale<T, U1, U2>) -> Self::Output {
435         Box3D::new(self.min / scale.clone(), self.max / scale)
436     }
437 }
438 
439 impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Box3D<T, U> {
440     #[inline]
div_assign(&mut self, scale: Scale<T, U, U>)441     fn div_assign(&mut self, scale: Scale<T, U, U>) {
442         self.min /= scale.clone();
443         self.max /= scale;
444     }
445 }
446 
447 impl<T, U> Box3D<T, U>
448 where
449     T: Copy,
450 {
451     #[inline]
x_range(&self) -> Range<T>452     pub fn x_range(&self) -> Range<T> {
453         self.min.x..self.max.x
454     }
455 
456     #[inline]
y_range(&self) -> Range<T>457     pub fn y_range(&self) -> Range<T> {
458         self.min.y..self.max.y
459     }
460 
461     #[inline]
z_range(&self) -> Range<T>462     pub fn z_range(&self) -> Range<T> {
463         self.min.z..self.max.z
464     }
465 
466     /// Drop the units, preserving only the numeric value.
467     #[inline]
to_untyped(&self) -> Box3D<T, UnknownUnit>468     pub fn to_untyped(&self) -> Box3D<T, UnknownUnit> {
469         Box3D {
470             min: self.min.to_untyped(),
471             max: self.max.to_untyped(),
472         }
473     }
474 
475     /// Tag a unitless value with units.
476     #[inline]
from_untyped(c: &Box3D<T, UnknownUnit>) -> Box3D<T, U>477     pub fn from_untyped(c: &Box3D<T, UnknownUnit>) -> Box3D<T, U> {
478         Box3D {
479             min: Point3D::from_untyped(c.min),
480             max: Point3D::from_untyped(c.max),
481         }
482     }
483 
484     /// Cast the unit
485     #[inline]
cast_unit<V>(&self) -> Box3D<T, V>486     pub fn cast_unit<V>(&self) -> Box3D<T, V> {
487         Box3D::new(self.min.cast_unit(), self.max.cast_unit())
488     }
489 
490     #[inline]
scale<S: Copy>(&self, x: S, y: S, z: S) -> Self where T: Mul<S, Output = T>,491     pub fn scale<S: Copy>(&self, x: S, y: S, z: S) -> Self
492     where
493         T: Mul<S, Output = T>,
494     {
495         Box3D::new(
496             Point3D::new(self.min.x * x, self.min.y * y, self.min.z * z),
497             Point3D::new(self.max.x * x, self.max.y * y, self.max.z * z),
498         )
499     }
500 }
501 
502 impl<T: NumCast + Copy, U> Box3D<T, U> {
503     /// Cast from one numeric representation to another, preserving the units.
504     ///
505     /// When casting from floating point to integer coordinates, the decimals are truncated
506     /// as one would expect from a simple cast, but this behavior does not always make sense
507     /// geometrically. Consider using round(), round_in or round_out() before casting.
508     #[inline]
cast<NewT: NumCast>(&self) -> Box3D<NewT, U>509     pub fn cast<NewT: NumCast>(&self) -> Box3D<NewT, U> {
510         Box3D::new(self.min.cast(), self.max.cast())
511     }
512 
513     /// Fallible cast from one numeric representation to another, preserving the units.
514     ///
515     /// When casting from floating point to integer coordinates, the decimals are truncated
516     /// as one would expect from a simple cast, but this behavior does not always make sense
517     /// geometrically. Consider using round(), round_in or round_out() before casting.
try_cast<NewT: NumCast>(&self) -> Option<Box3D<NewT, U>>518     pub fn try_cast<NewT: NumCast>(&self) -> Option<Box3D<NewT, U>> {
519         match (self.min.try_cast(), self.max.try_cast()) {
520             (Some(a), Some(b)) => Some(Box3D::new(a, b)),
521             _ => None,
522         }
523     }
524 
525     // Convenience functions for common casts
526 
527     /// Cast into an `f32` box3d.
528     #[inline]
to_f32(&self) -> Box3D<f32, U>529     pub fn to_f32(&self) -> Box3D<f32, U> {
530         self.cast()
531     }
532 
533     /// Cast into an `f64` box3d.
534     #[inline]
to_f64(&self) -> Box3D<f64, U>535     pub fn to_f64(&self) -> Box3D<f64, U> {
536         self.cast()
537     }
538 
539     /// Cast into an `usize` box3d, truncating decimals if any.
540     ///
541     /// When casting from floating point cuboids, it is worth considering whether
542     /// to `round()`, `round_in()` or `round_out()` before the cast in order to
543     /// obtain the desired conversion behavior.
544     #[inline]
to_usize(&self) -> Box3D<usize, U>545     pub fn to_usize(&self) -> Box3D<usize, U> {
546         self.cast()
547     }
548 
549     /// Cast into an `u32` box3d, truncating decimals if any.
550     ///
551     /// When casting from floating point cuboids, it is worth considering whether
552     /// to `round()`, `round_in()` or `round_out()` before the cast in order to
553     /// obtain the desired conversion behavior.
554     #[inline]
to_u32(&self) -> Box3D<u32, U>555     pub fn to_u32(&self) -> Box3D<u32, U> {
556         self.cast()
557     }
558 
559     /// Cast into an `i32` box3d, truncating decimals if any.
560     ///
561     /// When casting from floating point cuboids, it is worth considering whether
562     /// to `round()`, `round_in()` or `round_out()` before the cast in order to
563     /// obtain the desired conversion behavior.
564     #[inline]
to_i32(&self) -> Box3D<i32, U>565     pub fn to_i32(&self) -> Box3D<i32, U> {
566         self.cast()
567     }
568 
569     /// Cast into an `i64` box3d, truncating decimals if any.
570     ///
571     /// When casting from floating point cuboids, it is worth considering whether
572     /// to `round()`, `round_in()` or `round_out()` before the cast in order to
573     /// obtain the desired conversion behavior.
574     #[inline]
to_i64(&self) -> Box3D<i64, U>575     pub fn to_i64(&self) -> Box3D<i64, U> {
576         self.cast()
577     }
578 }
579 
580 impl<T: Float, U> Box3D<T, U> {
581     /// Returns true if all members are finite.
582     #[inline]
is_finite(self) -> bool583     pub fn is_finite(self) -> bool {
584         self.min.is_finite() && self.max.is_finite()
585     }
586 }
587 
588 impl<T, U> Box3D<T, U>
589 where
590     T: Round,
591 {
592     /// Return a box3d with edges rounded to integer coordinates, such that
593     /// the returned box3d has the same set of pixel centers as the original
594     /// one.
595     /// Values equal to 0.5 round up.
596     /// Suitable for most places where integral device coordinates
597     /// are needed, but note that any translation should be applied first to
598     /// avoid pixel rounding errors.
599     /// Note that this is *not* rounding to nearest integer if the values are negative.
600     /// They are always rounding as floor(n + 0.5).
601     #[must_use]
round(&self) -> Self602     pub fn round(&self) -> Self {
603         Box3D::new(self.min.round(), self.max.round())
604     }
605 }
606 
607 impl<T, U> Box3D<T, U>
608 where
609     T: Floor + Ceil,
610 {
611     /// Return a box3d with faces/edges rounded to integer coordinates, such that
612     /// the original box3d contains the resulting box3d.
613     #[must_use]
round_in(&self) -> Self614     pub fn round_in(&self) -> Self {
615         Box3D {
616             min: self.min.ceil(),
617             max: self.max.floor(),
618         }
619     }
620 
621     /// Return a box3d with faces/edges rounded to integer coordinates, such that
622     /// the original box3d is contained in the resulting box3d.
623     #[must_use]
round_out(&self) -> Self624     pub fn round_out(&self) -> Self {
625         Box3D {
626             min: self.min.floor(),
627             max: self.max.ceil(),
628         }
629     }
630 }
631 
632 impl<T, U> From<Size3D<T, U>> for Box3D<T, U>
633 where
634     T: Copy + Zero + PartialOrd,
635 {
from(b: Size3D<T, U>) -> Self636     fn from(b: Size3D<T, U>) -> Self {
637         Self::from_size(b)
638     }
639 }
640 
641 impl<T: Default, U> Default for Box3D<T, U> {
default() -> Self642     fn default() -> Self {
643         Box3D {
644             min: Default::default(),
645             max: Default::default(),
646         }
647     }
648 }
649 
650 /// Shorthand for `Box3D::new(Point3D::new(x1, y1, z1), Point3D::new(x2, y2, z2))`.
box3d<T: Copy, U>( min_x: T, min_y: T, min_z: T, max_x: T, max_y: T, max_z: T, ) -> Box3D<T, U>651 pub fn box3d<T: Copy, U>(
652     min_x: T,
653     min_y: T,
654     min_z: T,
655     max_x: T,
656     max_y: T,
657     max_z: T,
658 ) -> Box3D<T, U> {
659     Box3D::new(
660         Point3D::new(min_x, min_y, min_z),
661         Point3D::new(max_x, max_y, max_z),
662     )
663 }
664 
665 #[cfg(test)]
666 mod tests {
667     use crate::default::{Box3D, Point3D};
668     use crate::{point3, size3, vec3};
669 
670     #[test]
test_new()671     fn test_new() {
672         let b = Box3D::new(point3(-1.0, -1.0, -1.0), point3(1.0, 1.0, 1.0));
673         assert!(b.min.x == -1.0);
674         assert!(b.min.y == -1.0);
675         assert!(b.min.z == -1.0);
676         assert!(b.max.x == 1.0);
677         assert!(b.max.y == 1.0);
678         assert!(b.max.z == 1.0);
679     }
680 
681     #[test]
test_size()682     fn test_size() {
683         let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
684         assert!(b.size().width == 20.0);
685         assert!(b.size().height == 20.0);
686         assert!(b.size().depth == 20.0);
687     }
688 
689     #[test]
test_width_height_depth()690     fn test_width_height_depth() {
691         let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
692         assert!(b.width() == 20.0);
693         assert!(b.height() == 20.0);
694         assert!(b.depth() == 20.0);
695     }
696 
697     #[test]
test_center()698     fn test_center() {
699         let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
700         assert!(b.center() == Point3D::zero());
701     }
702 
703     #[test]
test_volume()704     fn test_volume() {
705         let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
706         assert!(b.volume() == 8000.0);
707     }
708 
709     #[test]
test_area()710     fn test_area() {
711         let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
712         assert!(b.xy_area() == 400.0);
713         assert!(b.yz_area() == 400.0);
714         assert!(b.xz_area() == 400.0);
715     }
716 
717     #[test]
test_from_points()718     fn test_from_points() {
719         let b = Box3D::from_points(&[point3(50.0, 160.0, 12.5), point3(100.0, 25.0, 200.0)]);
720         assert!(b.min == point3(50.0, 25.0, 12.5));
721         assert!(b.max == point3(100.0, 160.0, 200.0));
722     }
723 
724     #[test]
test_min_max()725     fn test_min_max() {
726         let b = Box3D::from_points(&[point3(50.0, 25.0, 12.5), point3(100.0, 160.0, 200.0)]);
727         assert!(b.min.x == 50.0);
728         assert!(b.min.y == 25.0);
729         assert!(b.min.z == 12.5);
730         assert!(b.max.x == 100.0);
731         assert!(b.max.y == 160.0);
732         assert!(b.max.z == 200.0);
733     }
734 
735     #[test]
test_round_in()736     fn test_round_in() {
737         let b =
738             Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]).round_in();
739         assert!(b.min.x == -25.0);
740         assert!(b.min.y == -40.0);
741         assert!(b.min.z == -70.0);
742         assert!(b.max.x == 60.0);
743         assert!(b.max.y == 36.0);
744         assert!(b.max.z == 89.0);
745     }
746 
747     #[test]
test_round_out()748     fn test_round_out() {
749         let b = Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)])
750             .round_out();
751         assert!(b.min.x == -26.0);
752         assert!(b.min.y == -41.0);
753         assert!(b.min.z == -71.0);
754         assert!(b.max.x == 61.0);
755         assert!(b.max.y == 37.0);
756         assert!(b.max.z == 90.0);
757     }
758 
759     #[test]
test_round()760     fn test_round() {
761         let b =
762             Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]).round();
763         assert!(b.min.x == -25.0);
764         assert!(b.min.y == -40.0);
765         assert!(b.min.z == -71.0);
766         assert!(b.max.x == 60.0);
767         assert!(b.max.y == 37.0);
768         assert!(b.max.z == 90.0);
769     }
770 
771     #[test]
test_from_size()772     fn test_from_size() {
773         let b = Box3D::from_size(size3(30.0, 40.0, 50.0));
774         assert!(b.min == Point3D::zero());
775         assert!(b.size().width == 30.0);
776         assert!(b.size().height == 40.0);
777         assert!(b.size().depth == 50.0);
778     }
779 
780     #[test]
test_translate()781     fn test_translate() {
782         let size = size3(15.0, 15.0, 200.0);
783         let mut center = (size / 2.0).to_vector().to_point();
784         let b = Box3D::from_size(size);
785         assert!(b.center() == center);
786         let translation = vec3(10.0, 2.5, 9.5);
787         let b = b.translate(translation);
788         center += translation;
789         assert!(b.center() == center);
790         assert!(b.max.x == 25.0);
791         assert!(b.max.y == 17.5);
792         assert!(b.max.z == 209.5);
793         assert!(b.min.x == 10.0);
794         assert!(b.min.y == 2.5);
795         assert!(b.min.z == 9.5);
796     }
797 
798     #[test]
test_union()799     fn test_union() {
800         let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(0.0, 20.0, 20.0)]);
801         let b2 = Box3D::from_points(&[point3(0.0, 20.0, 20.0), point3(20.0, -20.0, -20.0)]);
802         let b = b1.union(&b2);
803         assert!(b.max.x == 20.0);
804         assert!(b.max.y == 20.0);
805         assert!(b.max.z == 20.0);
806         assert!(b.min.x == -20.0);
807         assert!(b.min.y == -20.0);
808         assert!(b.min.z == -20.0);
809         assert!(b.volume() == (40.0 * 40.0 * 40.0));
810     }
811 
812     #[test]
test_intersects()813     fn test_intersects() {
814         let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]);
815         let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
816         assert!(b1.intersects(&b2));
817     }
818 
819     #[test]
test_intersection_unchecked()820     fn test_intersection_unchecked() {
821         let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]);
822         let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
823         let b = b1.intersection_unchecked(&b2);
824         assert!(b.max.x == 10.0);
825         assert!(b.max.y == 20.0);
826         assert!(b.max.z == 20.0);
827         assert!(b.min.x == -10.0);
828         assert!(b.min.y == -20.0);
829         assert!(b.min.z == -20.0);
830         assert!(b.volume() == (20.0 * 40.0 * 40.0));
831     }
832 
833     #[test]
test_intersection()834     fn test_intersection() {
835         let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]);
836         let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
837         assert!(b1.intersection(&b2).is_some());
838 
839         let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(-10.0, 20.0, 20.0)]);
840         let b2 = Box3D::from_points(&[point3(10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
841         assert!(b1.intersection(&b2).is_none());
842     }
843 
844     #[test]
test_scale()845     fn test_scale() {
846         let b = Box3D::from_points(&[point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)]);
847         let b = b.scale(0.5, 0.5, 0.5);
848         assert!(b.max.x == 5.0);
849         assert!(b.max.y == 5.0);
850         assert!(b.max.z == 5.0);
851         assert!(b.min.x == -5.0);
852         assert!(b.min.y == -5.0);
853         assert!(b.min.z == -5.0);
854     }
855 
856     #[test]
test_zero()857     fn test_zero() {
858         let b = Box3D::<f64>::zero();
859         assert!(b.max.x == 0.0);
860         assert!(b.max.y == 0.0);
861         assert!(b.max.z == 0.0);
862         assert!(b.min.x == 0.0);
863         assert!(b.min.y == 0.0);
864         assert!(b.min.z == 0.0);
865     }
866 
867     #[test]
test_lerp()868     fn test_lerp() {
869         let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(-10.0, -10.0, -10.0)]);
870         let b2 = Box3D::from_points(&[point3(10.0, 10.0, 10.0), point3(20.0, 20.0, 20.0)]);
871         let b = b1.lerp(b2, 0.5);
872         assert!(b.center() == Point3D::zero());
873         assert!(b.size().width == 10.0);
874         assert!(b.size().height == 10.0);
875         assert!(b.size().depth == 10.0);
876     }
877 
878     #[test]
test_contains()879     fn test_contains() {
880         let b = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]);
881         assert!(b.contains(point3(-15.3, 10.5, 18.4)));
882     }
883 
884     #[test]
test_contains_box()885     fn test_contains_box() {
886         let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]);
887         let b2 = Box3D::from_points(&[point3(-14.3, -16.5, -19.3), point3(6.7, 17.6, 2.5)]);
888         assert!(b1.contains_box(&b2));
889     }
890 
891     #[test]
test_inflate()892     fn test_inflate() {
893         let b = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]);
894         let b = b.inflate(10.0, 5.0, 2.0);
895         assert!(b.size().width == 60.0);
896         assert!(b.size().height == 50.0);
897         assert!(b.size().depth == 44.0);
898         assert!(b.center() == Point3D::zero());
899     }
900 
901     #[test]
test_is_empty()902     fn test_is_empty() {
903         for i in 0..3 {
904             let mut coords_neg = [-20.0, -20.0, -20.0];
905             let mut coords_pos = [20.0, 20.0, 20.0];
906             coords_neg[i] = 0.0;
907             coords_pos[i] = 0.0;
908             let b = Box3D::from_points(&[Point3D::from(coords_neg), Point3D::from(coords_pos)]);
909             assert!(b.is_empty());
910         }
911     }
912 
913     #[test]
test_nan_empty_or_negative()914     fn test_nan_empty_or_negative() {
915         use std::f32::NAN;
916         assert!(Box3D { min: point3(NAN, 2.0, 1.0), max: point3(1.0, 3.0, 5.0) }.is_empty());
917         assert!(Box3D { min: point3(0.0, NAN, 1.0), max: point3(1.0, 2.0, 5.0) }.is_empty());
918         assert!(Box3D { min: point3(1.0, -2.0, NAN), max: point3(3.0, 2.0, 5.0) }.is_empty());
919         assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(NAN, 2.0, 5.0) }.is_empty());
920         assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(0.0, NAN, 5.0) }.is_empty());
921         assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(0.0, 1.0, NAN) }.is_empty());
922     }
923 }
924