Translate and scale methods for AABB

This commit is contained in:
Risto Saarelma 2014-02-01 10:29:59 +02:00
parent ff9fc767d4
commit 907165075c
2 changed files with 18 additions and 2 deletions

View file

@ -53,9 +53,19 @@ pub trait Aabb
}
// Returns a new AABB that has its points translated by the given vector.
fn translate(&self, v: &V) -> Self {
fn add_v(&self, v: &V) -> Self {
Aabb::new(&self.min().add_v(v), &self.max().add_v(v))
}
fn mul_s(&self, s: S) -> Self {
Aabb::new(&self.min().mul_s(s.clone()), &self.max().mul_s(s.clone()))
}
fn mul_v(&self, v: &V) -> Self {
let mn : P = Point::from_vec(&self.min().to_vec().mul_v(v));
let mx : P = Point::from_vec(&self.max().to_vec().mul_v(v));
Aabb::new(&mn, &mx)
}
}
#[deriving(Clone, Eq)]

View file

@ -36,6 +36,12 @@ fn test_aabb() {
assert!(aabb.contains(&Point3::new(-20, -10, -5)));
assert!(!aabb.contains(&Point3::new(-21, -11, -6)));
assert_eq!(aabb.translate(&Vec3::new(1, 2, 3)),
assert_eq!(aabb.add_v(&Vec3::new(1, 2, 3)),
Aabb3::new(&Point3::new(-19, 32, 8), &Point3::new(11, -8, -2)));
assert_eq!(aabb.mul_s(2),
Aabb3::new(&Point3::new(-40, -20, -10), &Point3::new(20, 60, 10)));
assert_eq!(aabb.mul_v(&Vec3::new(1, 2, 3)),
Aabb3::new(&Point3::new(-20, -20, -15), &Point3::new(10, 60, 15)));
}