From 907165075cdc9a876704930dad579be7b8ebd953 Mon Sep 17 00:00:00 2001 From: Risto Saarelma Date: Sat, 1 Feb 2014 10:29:59 +0200 Subject: [PATCH] Translate and scale methods for AABB --- src/cgmath/aabb.rs | 12 +++++++++++- src/test/aabb.rs | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cgmath/aabb.rs b/src/cgmath/aabb.rs index 57eec39..f80df23 100644 --- a/src/cgmath/aabb.rs +++ b/src/cgmath/aabb.rs @@ -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)] diff --git a/src/test/aabb.rs b/src/test/aabb.rs index f58797e..2eb2475 100644 --- a/src/test/aabb.rs +++ b/src/test/aabb.rs @@ -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))); }