use crate::prelude::*;
pub fn test_aabb3_from_point<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let point = V::new(_1, _2, _0);
let aabb = V::Aabb::from_point(point);
assert_eq!(aabb.min(), point);
assert_eq!(aabb.max(), point);
assert!(!aabb.is_empty());
}
pub fn test_aabb3_from_corners<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let min_corner = V::new(_0, _1, _0);
let max_corner = V::new(_2, _3, _1);
let aabb = V::Aabb::from_corners(min_corner, max_corner);
assert_eq!(aabb.min(), min_corner);
assert_eq!(aabb.max(), max_corner);
assert!(!aabb.is_empty());
let aabb_swapped = V::Aabb::from_corners(max_corner, min_corner);
assert_eq!(aabb_swapped.min(), min_corner);
assert_eq!(aabb_swapped.max(), max_corner);
}
pub fn test_aabb3_from_center_and_half_extents<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let center = V::new(_1, _1, _1);
let half_extents = V::new(_1, _1, _1);
let aabb = V::Aabb::from_center_and_half_extents(center, half_extents);
assert_eq!(aabb.min(), V::new(_0, _0, _0));
assert_eq!(aabb.max(), V::new(_2, _2, _2));
assert!(!aabb.is_empty());
}
pub fn test_aabb3_from_center_and_size<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let center = V::new(_1, _1, _1);
let size = V::new(_2, _2, _2);
let aabb = V::Aabb::from_center_and_size(center, size);
assert_eq!(aabb.min(), V::new(_0, _0, _0));
assert_eq!(aabb.max(), V::new(_2, _2, _2));
assert!(!aabb.is_empty());
let center2 = V::new(_1, _2, _3);
let size2 = V::new(_2, _2, _2);
let aabb2 = V::Aabb::from_center_and_size(center2, size2);
assert_eq!(aabb2.min(), V::new(_0, _1, _2));
assert_eq!(aabb2.max(), V::new(_2, _3, V::Scalar::from(4.0)));
}
pub fn test_aabb3_from_points<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let _4: V::Scalar = 4.0.into();
let points = vec![
V::new(_1, _1, _1),
V::new(_2, _3, _2),
V::new(_0, _2, _4),
V::new(_3, _0, _2),
];
let aabb = V::Aabb::from_points(points.iter());
assert_eq!(aabb.min(), V::new(_0, _0, _1));
assert_eq!(aabb.max(), V::new(_3, _3, _4));
assert!(!aabb.is_empty());
let aabb2 = V::Aabb::from_points(points.clone());
assert_eq!(aabb2.min(), V::new(_0, _0, _1));
assert_eq!(aabb2.max(), V::new(_3, _3, _4));
let empty_points: Vec<V> = vec![];
let empty_aabb = V::Aabb::from_points(empty_points.iter());
assert!(empty_aabb.is_empty());
let mut aabb = V::Aabb::from_points(points);
aabb.apply(&|v: V| v * _2);
assert_eq!(aabb.min(), V::new(_0, _0, _1 * _2));
assert_eq!(aabb.max(), V::new(_3 * _2, _3 * _2, _4 * _2));
}
pub fn test_aabb3_extend<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let mut aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));
aabb.add_point(V::new(_0, _1, _0));
assert_eq!(aabb.min(), V::new(_0, _0, _0));
assert_eq!(aabb.max(), V::new(_1, _1, _1));
aabb.add_point(V::new(_2, _3, _0));
assert_eq!(aabb.min(), V::new(_0, _0, _0));
assert_eq!(aabb.max(), V::new(_2, _3, _1));
aabb.add_point(V::new(-_1, -_1, -_1));
assert_eq!(aabb.min(), V::new(-_1, -_1, -_1));
assert_eq!(aabb.max(), V::new(_2, _3, _1));
}
pub fn test_aabb3_extend_with<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let _4: V::Scalar = 4.0.into();
let mut aabb1 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
let aabb2 = V::Aabb::from_corners(V::new(_1, _1, _1), V::new(_3, _4, _3));
aabb1.add_aabb(&aabb2);
assert_eq!(aabb1.min(), V::new(_0, _0, _0));
assert_eq!(aabb1.max(), V::new(_3, _4, _3));
let mut aabb3 = V::Aabb::from_corners(V::new(-_3, -_3, -_3), V::new(-_1, -_1, -_1));
aabb3.add_aabb(&aabb1);
assert_eq!(aabb3.min(), V::new(-_3, -_3, -_3));
assert_eq!(aabb3.max(), V::new(_3, _4, _3));
}
pub fn test_aabb3_pad<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let half: V::Scalar = 0.5.into();
let mut aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
let padding = V::new(_1, _1, _1);
aabb.pad(padding);
assert_eq!(aabb.min(), V::new(-_1, -_1, -_1));
assert_eq!(aabb.max(), V::new(_3, _3, _3));
let mut aabb2 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
let shrink = V::new(-half, -half, -half);
aabb2.pad(shrink);
assert_eq!(aabb2.min(), V::new(half, half, half));
assert_eq!(aabb2.max(), V::new(_1 + half, _1 + half, _1 + half));
let mut aabb3 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
let extreme_shrink = V::new(-_2, -_2, -_2);
aabb3.pad(extreme_shrink);
let center = V::new(_1, _1, _1);
assert_eq!(aabb3.min(), center);
assert_eq!(aabb3.max(), center);
assert!(!aabb3.is_empty());
}
pub fn test_aabb3_fast_pad<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let mut aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
let padding = V::new(_1, _1, _1);
aabb.fast_pad(padding);
assert_eq!(aabb.min(), V::new(-_1, -_1, -_1));
assert_eq!(aabb.max(), V::new(_3, _3, _3));
let mut aabb2 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_3, _3, _3));
let shrink = V::new(-_1, -_1, -_1);
aabb2.fast_pad(shrink);
assert_eq!(aabb2.min(), V::new(_1, _1, _1));
assert_eq!(aabb2.max(), V::new(_2, _2, _2));
let mut aabb3 = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));
let extreme_shrink = V::new(-_2, -_2, -_2);
aabb3.fast_pad(extreme_shrink);
assert_eq!(aabb3.min(), V::new(_2, _2, _2));
assert_eq!(aabb3.max(), V::new(-_1, -_1, -_1));
assert!(aabb3.is_empty());
}
pub fn test_aabb3_is_empty<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let valid_aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));
assert!(!valid_aabb.is_empty());
let point_aabb = V::Aabb::from_point(V::new(_1, _1, _1));
assert!(!point_aabb.is_empty());
let mut inverted_aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _1, _1));
inverted_aabb.fast_pad(V::new(-_2, -_2, -_2));
assert!(inverted_aabb.is_empty());
let partial_empty_aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_1, _0, _1));
assert!(!partial_empty_aabb.is_empty());
}
pub fn test_aabb3_comprehensive<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let _3: V::Scalar = 3.0.into();
let _4: V::Scalar = 4.0.into();
let _5: V::Scalar = 5.0.into();
let _6: V::Scalar = 6.0.into();
let _7: V::Scalar = 7.0.into();
let mut aabb = V::Aabb::from_point(V::new(_0, _0, _0));
assert!(!aabb.is_empty());
aabb.add_point(V::new(_1, _1, _1));
assert_eq!(aabb.min(), V::new(_0, _0, _0));
assert_eq!(aabb.max(), V::new(_1, _1, _1));
assert!(!aabb.is_empty());
aabb.add_point(V::new(-_1, _2, _3));
assert_eq!(aabb.min(), V::new(-_1, _0, _0));
assert_eq!(aabb.max(), V::new(_1, _2, _3));
let other_aabb = V::Aabb::from_corners(V::new(_2, -_2, -_1), V::new(_5, _0, _4));
aabb.add_aabb(&other_aabb);
assert_eq!(aabb.min(), V::new(-_1, -_2, -_1));
assert_eq!(aabb.max(), V::new(_5, _2, _4));
aabb.pad(V::new(_1, _1, _1));
assert_eq!(aabb.min(), V::new(-_2, -_3, -_2));
assert_eq!(aabb.max(), V::new(_6, _3, _5));
let points = [
V::new(-_2, -_3, -_2),
V::new(_6, _3, _5),
V::new(_0, _0, _0),
];
let new_aabb = V::Aabb::from_points(points.iter());
assert_eq!(new_aabb.min(), V::new(-_2, -_3, -_2));
assert_eq!(new_aabb.max(), V::new(_6, _3, _5));
assert_eq!(new_aabb.min(), aabb.min());
assert_eq!(new_aabb.max(), aabb.max());
}
pub fn test_aabb3_contains_point_inclusive<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let half: V::Scalar = 0.5.into();
let neg_one: V::Scalar = (-1.0).into();
let aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
assert!(aabb.contains_point_inclusive(V::new(_1, _1, _1)));
assert!(aabb.contains_point_inclusive(V::new(half, half, half)));
assert!(aabb.contains_point_inclusive(V::new(_1, half, _2)));
assert!(aabb.contains_point_inclusive(V::new(_0, _0, _0))); assert!(aabb.contains_point_inclusive(V::new(_2, _2, _2))); assert!(aabb.contains_point_inclusive(V::new(_0, _1, _2))); assert!(aabb.contains_point_inclusive(V::new(_2, _0, _1))); assert!(aabb.contains_point_inclusive(V::new(_1, _2, _0))); assert!(aabb.contains_point_inclusive(V::new(_0, _0, _1))); assert!(aabb.contains_point_inclusive(V::new(_1, _2, _2)));
assert!(!aabb.contains_point_inclusive(V::new(neg_one, _1, _1)));
assert!(!aabb.contains_point_inclusive(V::new(_1, neg_one, _1)));
assert!(!aabb.contains_point_inclusive(V::new(_1, _1, neg_one)));
assert!(!aabb.contains_point_inclusive(V::new(_2 + half, _1, _1)));
assert!(!aabb.contains_point_inclusive(V::new(_1, _2 + half, _1)));
assert!(!aabb.contains_point_inclusive(V::new(_1, _1, _2 + half)));
let point_aabb = V::Aabb::from_point(V::new(_1, _1, _1));
assert!(point_aabb.contains_point_inclusive(V::new(_1, _1, _1)));
assert!(!point_aabb.contains_point_inclusive(V::new(_1 + half, _1, _1)));
let empty_aabb = V::Aabb::default();
assert!(!empty_aabb.contains_point_inclusive(V::new(_0, _0, _0)));
assert!(!empty_aabb.contains_point_inclusive(V::new(_1, _1, _1)));
let inverted_aabb = V::Aabb::from_corners(V::new(_1, _1, _1), V::new(_0, _0, _0));
assert!(inverted_aabb.contains_point_inclusive(V::new(half, half, half)));
assert!(inverted_aabb.contains_point_inclusive(V::new(_0, _0, _0)));
assert!(inverted_aabb.contains_point_inclusive(V::new(_1, _1, _1)));
assert!(!inverted_aabb.contains_point_inclusive(V::new(_2, _1, _1)));
}
pub fn test_extents<V: GenericVector3>() {
let _0: V::Scalar = 0.0.into();
let _1: V::Scalar = 1.0.into();
let _2: V::Scalar = 2.0.into();
let aabb = V::Aabb::from_corners(V::new(_0, _0, _0), V::new(_2, _2, _2));
let (min, max, delta) = aabb.extents();
let aabb2 = V::Aabb::from_corners(min, min + delta / _2);
let aabb3 = V::Aabb::from_corners(min, min + delta / _2);
assert_eq!(aabb2, aabb3);
assert_ne!(aabb, aabb3);
assert!(!aabb2.contains_point_inclusive(max));
assert!(aabb.contains_aabb_inclusive(&aabb2));
let empty_aabb = V::Aabb::default();
let empty_aabb2 = V::Aabb::default();
assert_eq!(empty_aabb, empty_aabb2);
assert_ne!(aabb2, empty_aabb2);
}