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