1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
10 use super::UnknownUnit;
11 use crate::approxord::{max, min};
13 use crate::point::{point3, Point3D};
14 use crate::scale::Scale;
15 use crate::size::Size3D;
16 use crate::vector::Vector3D;
18 use num_traits::{NumCast, Float};
19 #[cfg(feature = "serde")]
20 use serde::{Deserialize, Serialize};
21 #[cfg(feature = "bytemuck")]
22 use bytemuck::{Zeroable, Pod};
24 use core::borrow::Borrow;
25 use core::cmp::PartialOrd;
27 use core::hash::{Hash, Hasher};
28 use core::ops::{Add, Div, DivAssign, Mul, MulAssign, Sub, Range};
30 /// An axis aligned 3D box represented by its minimum and maximum coordinates.
32 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
35 serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>"))
37 pub struct Box3D<T, U> {
38 pub min: Point3D<T, U>,
39 pub max: Point3D<T, U>,
42 impl<T: Hash, U> Hash for Box3D<T, U> {
43 fn hash<H: Hasher>(&self, h: &mut H) {
49 impl<T: Copy, U> Copy for Box3D<T, U> {}
51 impl<T: Clone, U> Clone for Box3D<T, U> {
52 fn clone(&self) -> Self {
53 Self::new(self.min.clone(), self.max.clone())
57 impl<T: PartialEq, U> PartialEq for Box3D<T, U> {
58 fn eq(&self, other: &Self) -> bool {
59 self.min.eq(&other.min) && self.max.eq(&other.max)
63 impl<T: Eq, U> Eq for Box3D<T, U> {}
65 impl<T: fmt::Debug, U> fmt::Debug for Box3D<T, U> {
66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67 f.debug_tuple("Box3D")
74 #[cfg(feature = "bytemuck")]
75 unsafe impl<T: Zeroable, U> Zeroable for Box3D<T, U> {}
77 #[cfg(feature = "bytemuck")]
78 unsafe impl<T: Pod, U: 'static> Pod for Box3D<T, U> {}
80 impl<T, U> Box3D<T, U> {
83 pub const fn new(min: Point3D<T, U>, max: Point3D<T, U>) -> Self {
87 /// Creates a Box3D of the given size, at offset zero.
89 pub fn from_size(size: Size3D<T, U>) -> Self where T: Zero {
92 max: point3(size.width, size.height, size.depth),
97 impl<T, U> Box3D<T, U>
101 /// Returns true if the box has a negative volume.
103 /// The common interpretation for a negative box is to consider it empty. It can be obtained
104 /// by calculating the intersection of two boxes that do not intersect.
106 pub fn is_negative(&self) -> bool {
107 self.max.x < self.min.x || self.max.y < self.min.y || self.max.z < self.min.z
110 /// Returns true if the size is zero, negative or NaN.
112 pub fn is_empty(&self) -> bool {
113 !(self.max.x > self.min.x && self.max.y > self.min.y && self.max.z > self.min.z)
117 pub fn intersects(&self, other: &Self) -> bool {
118 self.min.x < other.max.x
119 && self.max.x > other.min.x
120 && self.min.y < other.max.y
121 && self.max.y > other.min.y
122 && self.min.z < other.max.z
123 && self.max.z > other.min.z
126 /// Returns `true` if this box3d contains the point. Points are considered
127 /// in the box3d if they are on the front, left or top faces, but outside if they
128 /// are on the back, right or bottom faces.
130 pub fn contains(&self, other: Point3D<T, U>) -> bool {
131 self.min.x <= other.x
132 && other.x < self.max.x
133 && self.min.y <= other.y
134 && other.y < self.max.y
135 && self.min.z <= other.z
136 && other.z < self.max.z
139 /// Returns `true` if this box3d contains the interior of the other box3d. Always
140 /// returns `true` if other is empty, and always returns `false` if other is
141 /// nonempty but this box3d is empty.
143 pub fn contains_box(&self, other: &Self) -> bool {
145 || (self.min.x <= other.min.x
146 && other.max.x <= self.max.x
147 && self.min.y <= other.min.y
148 && other.max.y <= self.max.y
149 && self.min.z <= other.min.z
150 && other.max.z <= self.max.z)
154 impl<T, U> Box3D<T, U>
156 T: Copy + PartialOrd,
159 pub fn to_non_empty(&self) -> Option<Self> {
168 pub fn intersection(&self, other: &Self) -> Option<Self> {
169 let b = self.intersection_unchecked(other);
178 pub fn intersection_unchecked(&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),
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),
191 Box3D::new(intersection_min, intersection_max)
194 /// Computes the union of two boxes.
196 /// If either of the boxes is empty, the other one is returned.
198 pub fn union(&self, other: &Self) -> Self {
199 if other.is_empty() {
208 min(self.min.x, other.min.x),
209 min(self.min.y, other.min.y),
210 min(self.min.z, other.min.z),
213 max(self.max.x, other.max.x),
214 max(self.max.y, other.max.y),
215 max(self.max.z, other.max.z),
221 impl<T, U> Box3D<T, U>
223 T: Copy + Add<T, Output = T>,
225 /// Returns the same box3d, translated by a vector.
228 pub fn translate(&self, by: Vector3D<T, U>) -> Self {
236 impl<T, U> Box3D<T, U>
238 T: Copy + Sub<T, Output = T>,
241 pub fn size(&self) -> Size3D<T, U> {
243 self.max.x - self.min.x,
244 self.max.y - self.min.y,
245 self.max.z - self.min.z,
250 pub fn width(&self) -> T {
251 self.max.x - self.min.x
255 pub fn height(&self) -> T {
256 self.max.y - self.min.y
260 pub fn depth(&self) -> T {
261 self.max.z - self.min.z
265 impl<T, U> Box3D<T, U>
267 T: Copy + Add<T, Output = T> + Sub<T, Output = T>,
269 /// Inflates the box by the specified sizes on each side respectively.
272 pub fn inflate(&self, width: T, height: T, depth: T) -> Self {
274 Point3D::new(self.min.x - width, self.min.y - height, self.min.z - depth),
275 Point3D::new(self.max.x + width, self.max.y + height, self.max.z + depth),
280 impl<T, U> Box3D<T, U>
282 T: Copy + Zero + PartialOrd,
284 /// Returns the smallest box containing all of the provided points.
285 pub fn from_points<I>(points: I) -> Self
288 I::Item: Borrow<Point3D<T, U>>,
290 let mut points = points.into_iter();
292 let (mut min_x, mut min_y, mut min_z) = match points.next() {
293 Some(first) => first.borrow().to_tuple(),
294 None => return Box3D::zero(),
296 let (mut max_x, mut max_y, mut max_z) = (min_x, min_y, min_z);
298 for point in points {
299 let p = point.borrow();
321 min: point3(min_x, min_y, min_z),
322 max: point3(max_x, max_y, max_z),
327 impl<T, U> Box3D<T, U>
329 T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
331 /// Linearly interpolate between this box3d and another box3d.
333 pub fn lerp(&self, other: Self, t: T) -> Self {
334 Self::new(self.min.lerp(other.min, t), self.max.lerp(other.max, t))
338 impl<T, U> Box3D<T, U>
340 T: Copy + One + Add<Output = T> + Div<Output = T>,
342 pub fn center(&self) -> Point3D<T, U> {
343 let two = T::one() + T::one();
344 (self.min + self.max.to_vector()) / two
348 impl<T, U> Box3D<T, U>
350 T: Copy + Mul<T, Output = T> + Sub<T, Output = T>,
353 pub fn volume(&self) -> T {
354 let size = self.size();
355 size.width * size.height * size.depth
359 pub fn xy_area(&self) -> T {
360 let size = self.size();
361 size.width * size.height
365 pub fn yz_area(&self) -> T {
366 let size = self.size();
367 size.depth * size.height
371 pub fn xz_area(&self) -> T {
372 let size = self.size();
373 size.depth * size.width
377 impl<T, U> Box3D<T, U>
381 /// Constructor, setting all sides to zero.
382 pub fn zero() -> Self {
383 Box3D::new(Point3D::zero(), Point3D::zero())
387 impl<T: Copy + Mul, U> Mul<T> for Box3D<T, U> {
388 type Output = Box3D<T::Output, U>;
391 fn mul(self, scale: T) -> Self::Output {
392 Box3D::new(self.min * scale, self.max * scale)
396 impl<T: Copy + MulAssign, U> MulAssign<T> for Box3D<T, U> {
398 fn mul_assign(&mut self, scale: T) {
404 impl<T: Copy + Div, U> Div<T> for Box3D<T, U> {
405 type Output = Box3D<T::Output, U>;
408 fn div(self, scale: T) -> Self::Output {
409 Box3D::new(self.min / scale.clone(), self.max / scale)
413 impl<T: Copy + DivAssign, U> DivAssign<T> for Box3D<T, U> {
415 fn div_assign(&mut self, scale: T) {
421 impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for Box3D<T, U1> {
422 type Output = Box3D<T::Output, U2>;
425 fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output {
426 Box3D::new(self.min * scale.clone(), self.max * scale)
430 impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for Box3D<T, U> {
432 fn mul_assign(&mut self, scale: Scale<T, U, U>) {
433 self.min *= scale.clone();
438 impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for Box3D<T, U2> {
439 type Output = Box3D<T::Output, U1>;
442 fn div(self, scale: Scale<T, U1, U2>) -> Self::Output {
443 Box3D::new(self.min / scale.clone(), self.max / scale)
447 impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for Box3D<T, U> {
449 fn div_assign(&mut self, scale: Scale<T, U, U>) {
450 self.min /= scale.clone();
455 impl<T, U> Box3D<T, U>
460 pub fn x_range(&self) -> Range<T> {
461 self.min.x..self.max.x
465 pub fn y_range(&self) -> Range<T> {
466 self.min.y..self.max.y
470 pub fn z_range(&self) -> Range<T> {
471 self.min.z..self.max.z
474 /// Drop the units, preserving only the numeric value.
476 pub fn to_untyped(&self) -> Box3D<T, UnknownUnit> {
478 min: self.min.to_untyped(),
479 max: self.max.to_untyped(),
483 /// Tag a unitless value with units.
485 pub fn from_untyped(c: &Box3D<T, UnknownUnit>) -> Box3D<T, U> {
487 min: Point3D::from_untyped(c.min),
488 max: Point3D::from_untyped(c.max),
494 pub fn cast_unit<V>(&self) -> Box3D<T, V> {
495 Box3D::new(self.min.cast_unit(), self.max.cast_unit())
499 pub fn scale<S: Copy>(&self, x: S, y: S, z: S) -> Self
501 T: Mul<S, Output = T>,
504 Point3D::new(self.min.x * x, self.min.y * y, self.min.z * z),
505 Point3D::new(self.max.x * x, self.max.y * y, self.max.z * z),
510 impl<T: NumCast + Copy, U> Box3D<T, U> {
511 /// Cast from one numeric representation to another, preserving the units.
513 /// When casting from floating point to integer coordinates, the decimals are truncated
514 /// as one would expect from a simple cast, but this behavior does not always make sense
515 /// geometrically. Consider using round(), round_in or round_out() before casting.
517 pub fn cast<NewT: NumCast>(&self) -> Box3D<NewT, U> {
518 Box3D::new(self.min.cast(), self.max.cast())
521 /// Fallible cast from one numeric representation to another, preserving the units.
523 /// When casting from floating point to integer coordinates, the decimals are truncated
524 /// as one would expect from a simple cast, but this behavior does not always make sense
525 /// geometrically. Consider using round(), round_in or round_out() before casting.
526 pub fn try_cast<NewT: NumCast>(&self) -> Option<Box3D<NewT, U>> {
527 match (self.min.try_cast(), self.max.try_cast()) {
528 (Some(a), Some(b)) => Some(Box3D::new(a, b)),
533 // Convenience functions for common casts
535 /// Cast into an `f32` box3d.
537 pub fn to_f32(&self) -> Box3D<f32, U> {
541 /// Cast into an `f64` box3d.
543 pub fn to_f64(&self) -> Box3D<f64, U> {
547 /// Cast into an `usize` box3d, truncating decimals if any.
549 /// When casting from floating point cuboids, it is worth considering whether
550 /// to `round()`, `round_in()` or `round_out()` before the cast in order to
551 /// obtain the desired conversion behavior.
553 pub fn to_usize(&self) -> Box3D<usize, U> {
557 /// Cast into an `u32` box3d, truncating decimals if any.
559 /// When casting from floating point cuboids, it is worth considering whether
560 /// to `round()`, `round_in()` or `round_out()` before the cast in order to
561 /// obtain the desired conversion behavior.
563 pub fn to_u32(&self) -> Box3D<u32, U> {
567 /// Cast into an `i32` box3d, truncating decimals if any.
569 /// When casting from floating point cuboids, it is worth considering whether
570 /// to `round()`, `round_in()` or `round_out()` before the cast in order to
571 /// obtain the desired conversion behavior.
573 pub fn to_i32(&self) -> Box3D<i32, U> {
577 /// Cast into an `i64` box3d, truncating decimals if any.
579 /// When casting from floating point cuboids, it is worth considering whether
580 /// to `round()`, `round_in()` or `round_out()` before the cast in order to
581 /// obtain the desired conversion behavior.
583 pub fn to_i64(&self) -> Box3D<i64, U> {
588 impl<T: Float, U> Box3D<T, U> {
589 /// Returns true if all members are finite.
591 pub fn is_finite(self) -> bool {
592 self.min.is_finite() && self.max.is_finite()
596 impl<T, U> Box3D<T, U>
600 /// Return a box3d with edges rounded to integer coordinates, such that
601 /// the returned box3d has the same set of pixel centers as the original
603 /// Values equal to 0.5 round up.
604 /// Suitable for most places where integral device coordinates
605 /// are needed, but note that any translation should be applied first to
606 /// avoid pixel rounding errors.
607 /// Note that this is *not* rounding to nearest integer if the values are negative.
608 /// They are always rounding as floor(n + 0.5).
610 pub fn round(&self) -> Self {
611 Box3D::new(self.min.round(), self.max.round())
615 impl<T, U> Box3D<T, U>
619 /// Return a box3d with faces/edges rounded to integer coordinates, such that
620 /// the original box3d contains the resulting box3d.
622 pub fn round_in(&self) -> Self {
624 min: self.min.ceil(),
625 max: self.max.floor(),
629 /// Return a box3d with faces/edges rounded to integer coordinates, such that
630 /// the original box3d is contained in the resulting box3d.
632 pub fn round_out(&self) -> Self {
634 min: self.min.floor(),
635 max: self.max.ceil(),
640 impl<T, U> From<Size3D<T, U>> for Box3D<T, U>
642 T: Copy + Zero + PartialOrd,
644 fn from(b: Size3D<T, U>) -> Self {
649 impl<T: Default, U> Default for Box3D<T, U> {
650 fn default() -> Self {
652 min: Default::default(),
653 max: Default::default(),
658 /// Shorthand for `Box3D::new(Point3D::new(x1, y1, z1), Point3D::new(x2, y2, z2))`.
659 pub fn box3d<T: Copy, U>(
668 Point3D::new(min_x, min_y, min_z),
669 Point3D::new(max_x, max_y, max_z),
675 use crate::default::{Box3D, Point3D};
676 use crate::{point3, size3, vec3};
680 let b = Box3D::new(point3(-1.0, -1.0, -1.0), point3(1.0, 1.0, 1.0));
681 assert!(b.min.x == -1.0);
682 assert!(b.min.y == -1.0);
683 assert!(b.min.z == -1.0);
684 assert!(b.max.x == 1.0);
685 assert!(b.max.y == 1.0);
686 assert!(b.max.z == 1.0);
691 let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
692 assert!(b.size().width == 20.0);
693 assert!(b.size().height == 20.0);
694 assert!(b.size().depth == 20.0);
698 fn test_width_height_depth() {
699 let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
700 assert!(b.width() == 20.0);
701 assert!(b.height() == 20.0);
702 assert!(b.depth() == 20.0);
707 let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
708 assert!(b.center() == Point3D::zero());
713 let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
714 assert!(b.volume() == 8000.0);
719 let b = Box3D::new(point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0));
720 assert!(b.xy_area() == 400.0);
721 assert!(b.yz_area() == 400.0);
722 assert!(b.xz_area() == 400.0);
726 fn test_from_points() {
727 let b = Box3D::from_points(&[point3(50.0, 160.0, 12.5), point3(100.0, 25.0, 200.0)]);
728 assert!(b.min == point3(50.0, 25.0, 12.5));
729 assert!(b.max == point3(100.0, 160.0, 200.0));
734 let b = Box3D::from_points(&[point3(50.0, 25.0, 12.5), point3(100.0, 160.0, 200.0)]);
735 assert!(b.min.x == 50.0);
736 assert!(b.min.y == 25.0);
737 assert!(b.min.z == 12.5);
738 assert!(b.max.x == 100.0);
739 assert!(b.max.y == 160.0);
740 assert!(b.max.z == 200.0);
746 Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]).round_in();
747 assert!(b.min.x == -25.0);
748 assert!(b.min.y == -40.0);
749 assert!(b.min.z == -70.0);
750 assert!(b.max.x == 60.0);
751 assert!(b.max.y == 36.0);
752 assert!(b.max.z == 89.0);
756 fn test_round_out() {
757 let b = Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)])
759 assert!(b.min.x == -26.0);
760 assert!(b.min.y == -41.0);
761 assert!(b.min.z == -71.0);
762 assert!(b.max.x == 61.0);
763 assert!(b.max.y == 37.0);
764 assert!(b.max.z == 90.0);
770 Box3D::from_points(&[point3(-25.5, -40.4, -70.9), point3(60.3, 36.5, 89.8)]).round();
771 assert!(b.min.x == -25.0);
772 assert!(b.min.y == -40.0);
773 assert!(b.min.z == -71.0);
774 assert!(b.max.x == 60.0);
775 assert!(b.max.y == 37.0);
776 assert!(b.max.z == 90.0);
780 fn test_from_size() {
781 let b = Box3D::from_size(size3(30.0, 40.0, 50.0));
782 assert!(b.min == Point3D::zero());
783 assert!(b.size().width == 30.0);
784 assert!(b.size().height == 40.0);
785 assert!(b.size().depth == 50.0);
789 fn test_translate() {
790 let size = size3(15.0, 15.0, 200.0);
791 let mut center = (size / 2.0).to_vector().to_point();
792 let b = Box3D::from_size(size);
793 assert!(b.center() == center);
794 let translation = vec3(10.0, 2.5, 9.5);
795 let b = b.translate(translation);
796 center += translation;
797 assert!(b.center() == center);
798 assert!(b.max.x == 25.0);
799 assert!(b.max.y == 17.5);
800 assert!(b.max.z == 209.5);
801 assert!(b.min.x == 10.0);
802 assert!(b.min.y == 2.5);
803 assert!(b.min.z == 9.5);
808 let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(0.0, 20.0, 20.0)]);
809 let b2 = Box3D::from_points(&[point3(0.0, 20.0, 20.0), point3(20.0, -20.0, -20.0)]);
810 let b = b1.union(&b2);
811 assert!(b.max.x == 20.0);
812 assert!(b.max.y == 20.0);
813 assert!(b.max.z == 20.0);
814 assert!(b.min.x == -20.0);
815 assert!(b.min.y == -20.0);
816 assert!(b.min.z == -20.0);
817 assert!(b.volume() == (40.0 * 40.0 * 40.0));
821 fn test_intersects() {
822 let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]);
823 let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
824 assert!(b1.intersects(&b2));
828 fn test_intersection_unchecked() {
829 let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]);
830 let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
831 let b = b1.intersection_unchecked(&b2);
832 assert!(b.max.x == 10.0);
833 assert!(b.max.y == 20.0);
834 assert!(b.max.z == 20.0);
835 assert!(b.min.x == -10.0);
836 assert!(b.min.y == -20.0);
837 assert!(b.min.z == -20.0);
838 assert!(b.volume() == (20.0 * 40.0 * 40.0));
842 fn test_intersection() {
843 let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(10.0, 20.0, 20.0)]);
844 let b2 = Box3D::from_points(&[point3(-10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
845 assert!(b1.intersection(&b2).is_some());
847 let b1 = Box3D::from_points(&[point3(-15.0, -20.0, -20.0), point3(-10.0, 20.0, 20.0)]);
848 let b2 = Box3D::from_points(&[point3(10.0, 20.0, 20.0), point3(15.0, -20.0, -20.0)]);
849 assert!(b1.intersection(&b2).is_none());
854 let b = Box3D::from_points(&[point3(-10.0, -10.0, -10.0), point3(10.0, 10.0, 10.0)]);
855 let b = b.scale(0.5, 0.5, 0.5);
856 assert!(b.max.x == 5.0);
857 assert!(b.max.y == 5.0);
858 assert!(b.max.z == 5.0);
859 assert!(b.min.x == -5.0);
860 assert!(b.min.y == -5.0);
861 assert!(b.min.z == -5.0);
866 let b = Box3D::<f64>::zero();
867 assert!(b.max.x == 0.0);
868 assert!(b.max.y == 0.0);
869 assert!(b.max.z == 0.0);
870 assert!(b.min.x == 0.0);
871 assert!(b.min.y == 0.0);
872 assert!(b.min.z == 0.0);
877 let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(-10.0, -10.0, -10.0)]);
878 let b2 = Box3D::from_points(&[point3(10.0, 10.0, 10.0), point3(20.0, 20.0, 20.0)]);
879 let b = b1.lerp(b2, 0.5);
880 assert!(b.center() == Point3D::zero());
881 assert!(b.size().width == 10.0);
882 assert!(b.size().height == 10.0);
883 assert!(b.size().depth == 10.0);
888 let b = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]);
889 assert!(b.contains(point3(-15.3, 10.5, 18.4)));
893 fn test_contains_box() {
894 let b1 = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]);
895 let b2 = Box3D::from_points(&[point3(-14.3, -16.5, -19.3), point3(6.7, 17.6, 2.5)]);
896 assert!(b1.contains_box(&b2));
901 let b = Box3D::from_points(&[point3(-20.0, -20.0, -20.0), point3(20.0, 20.0, 20.0)]);
902 let b = b.inflate(10.0, 5.0, 2.0);
903 assert!(b.size().width == 60.0);
904 assert!(b.size().height == 50.0);
905 assert!(b.size().depth == 44.0);
906 assert!(b.center() == Point3D::zero());
912 let mut coords_neg = [-20.0, -20.0, -20.0];
913 let mut coords_pos = [20.0, 20.0, 20.0];
916 let b = Box3D::from_points(&[Point3D::from(coords_neg), Point3D::from(coords_pos)]);
917 assert!(b.is_empty());
922 fn test_nan_empty_or_negative() {
924 assert!(Box3D { min: point3(NAN, 2.0, 1.0), max: point3(1.0, 3.0, 5.0) }.is_empty());
925 assert!(Box3D { min: point3(0.0, NAN, 1.0), max: point3(1.0, 2.0, 5.0) }.is_empty());
926 assert!(Box3D { min: point3(1.0, -2.0, NAN), max: point3(3.0, 2.0, 5.0) }.is_empty());
927 assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(NAN, 2.0, 5.0) }.is_empty());
928 assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(0.0, NAN, 5.0) }.is_empty());
929 assert!(Box3D { min: point3(1.0, -2.0, 1.0), max: point3(0.0, 1.0, NAN) }.is_empty());