From 53ad086e5e32ce4a75bd962efe72a88a27741acc Mon Sep 17 00:00:00 2001 From: kvark Date: Thu, 31 Oct 2013 17:36:21 -0400 Subject: [PATCH] Enabled Transform3, implemented it for Transform3D --- src/cgmath/lib.rs | 1 + src/cgmath/transform.rs | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/cgmath/lib.rs b/src/cgmath/lib.rs index 29f05db..f8f73f5 100644 --- a/src/cgmath/lib.rs +++ b/src/cgmath/lib.rs @@ -35,6 +35,7 @@ pub mod plane; pub mod point; pub mod ray; pub mod rotation; +pub mod transform; pub mod projection; diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index 5e30dac..bb60df7 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -13,10 +13,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub trait Transform { - fn transform_vec(&self, point: Point3) -> Point3; - fn transform_point(&self, point: Point3) -> Point3; - fn transform_ray(&self, ray: Ray3) -> Ray3; +use matrix::Mat4; +use point::{Point,Point3}; +use ray::Ray3; +use rotation::Rotation3; +use vector::{Vector,Vec3}; + + +pub trait Transform3 { + fn transform_vec3(&self, vec: &Vec3) -> Vec3; + fn transform_point3(&self, point: &Point3) -> Point3; + + #[inline] + fn transform_ray3(&self, ray: &Ray3) -> Ray3 { + Ray3::new( self.transform_point3(&ray.origin), self.transform_vec3(&ray.direction) ) + } } /// A homogeneous transformation matrix. @@ -26,15 +37,27 @@ pub struct AffineMatrix3 { /// A transformation in three dimensions consisting of a rotation, /// displacement vector and scale amount. -pub struct Transform3 { +pub struct Transform3D { rot: R, disp: Vec3, scale: S, } -impl> Transform3 { +impl> Transform3D { #[inline] - pub fn new(rot: R, disp: Vec3, scale: S) -> Transform3 { - Transform3 { rot: rot, disp: disp, scale: S } + pub fn new(rot: R, disp: Vec3, scale: S) -> Transform3D { + Transform3D { rot: rot, disp: disp, scale: scale } + } +} + +impl > Transform3 for Transform3D { + #[inline] + fn transform_vec3(&self, vec: &Vec3) -> Vec3 { + self.rot.rotate_vec3( &vec.mul_s( self.scale.clone() )) + } + + #[inline] + fn transform_point3(&self, point: &Point3) -> Point3 { + self.rot.rotate_point3( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) } }