Fixed bug and add test

This commit is contained in:
Thomas Forgione 2018-03-12 11:23:11 +01:00
parent 2748c16f55
commit c32a378caf
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
1 changed files with 27 additions and 2 deletions

View File

@ -71,10 +71,10 @@ macro_rules! make_bounding_box {
pub fn intersection(&self, other: &$name<T>) -> $name<T> {
let mut ret = self.clone();
for i in 0..$size {
if ret.min()[i] > other.min()[i] {
if ret.min()[i] < other.min()[i] {
ret.min_mut()[i] = other.min()[i];
}
if ret.max()[i] < other.max()[i] {
if ret.max()[i] > other.max()[i] {
ret.max_mut()[i] = other.max()[i];
}
}
@ -101,6 +101,9 @@ mod tests {
use math::vector::Vector2;
use math::bounding_box::BoundingBox2;
use math::vector::Vector3;
use math::bounding_box::BoundingBox3;
#[test]
fn initialization() {
use std::f64::{MIN, MAX};
@ -121,4 +124,26 @@ mod tests {
assert_delta!(bounding_box.max().x(), v1.x(), 0.01);
assert_delta!(bounding_box.max().y(), v2.y(), 0.01);
}
#[test]
fn intersection() {
let b1 = BoundingBox3::new(
Vector3::new(0, 0, 0),
Vector3::new(2, 2, 2),
);
let b2 = BoundingBox3::new(
Vector3::new(1, 1, 1),
Vector3::new(3, 3, 3),
);
let b = b1.intersection(&b2);
assert_eq!(b.min().x(), 1);
assert_eq!(b.min().y(), 1);
assert_eq!(b.min().z(), 1);
assert_eq!(b.max().x(), 2);
assert_eq!(b.max().y(), 2);
assert_eq!(b.max().z(), 2);
}
}