From 6dd06103fcad869dc5edfef13de310787ae1509c Mon Sep 17 00:00:00 2001 From: kvark Date: Fri, 1 Nov 2013 08:29:01 -0400 Subject: [PATCH] Generalized transform trait over any-dimensional vectors and points --- src/cgmath/array.rs | 12 +++++++++++ src/cgmath/rotation.rs | 9 +++++--- src/cgmath/transform.rs | 46 +++++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index 6e06df9..b50c9c4 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -44,6 +44,18 @@ pub trait Array fn each_mut(&mut self, f: &fn(i: uint, x: &mut T)); } +/*impl //TODO +< + T: Clone, + Slice, + A: Array +> +Clone for A { + fn clone(&self) -> A { + self.build(|i| self.i(i).clone()) + } +}*/ + macro_rules! array( (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr] $_n:ident) => ( impl<$S: Clone> Array<$T, [$T,..$n]> for $Self { diff --git a/src/cgmath/rotation.rs b/src/cgmath/rotation.rs index 4da5b96..1673055 100644 --- a/src/cgmath/rotation.rs +++ b/src/cgmath/rotation.rs @@ -14,6 +14,7 @@ // limitations under the License. use angle::Rad; +use array::Array; use matrix::Matrix; use matrix::{Mat2, ToMat2}; use matrix::{Mat3, ToMat3}; @@ -25,10 +26,10 @@ use vector::{Vector, Vec2, Vec3}; /// A trait for generic rotation pub trait Rotation < - S: Primitive + Clone, + S: Primitive, Slice, V: Vector, - P: Point + Clone + P: Point > : Eq + ApproxEq @@ -38,7 +39,9 @@ pub trait Rotation #[inline] fn rotate_ray(&self, ray: &Ray) -> Ray { - Ray::new( ray.origin.clone(), self.rotate_vec(&ray.direction) ) + Ray::new( //FIXME: use clone derived from Array + Array::build(|i| ray.origin.i(i).clone()), + self.rotate_vec(&ray.direction) ) } fn concat(&self, other: &Self) -> Self; diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index 08ae398..1fd94af 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -14,9 +14,10 @@ // limitations under the License. use matrix::Mat4; -use point::{Point, Point3}; +use point::Point; use ray::Ray; -use rotation::Rotation3; +use rotation::Rotation; +use quaternion::Quat; use vector::{Vector, Vec3}; /// A trait of affine transformation, that can be applied to points or vectors @@ -42,29 +43,42 @@ pub struct AffineMatrix3 { mat: Mat4, } -/// A transformation in three dimensions consisting of a rotation, +/// A generic transformation consisting of a rotation, /// displacement vector and scale amount. -pub struct Transform3 { - rot: R, - disp: Vec3, +pub struct Decomposed { scale: S, + rot: R, + disp: V, } -impl> Transform3 { +impl +< + S: Float, + Slice, + V: Vector, + P: Point, + R: Rotation +> +Transform for Decomposed { #[inline] - pub fn new(rot: R, disp: Vec3, scale: S) -> Transform3 { - Transform3 { rot: rot, disp: disp, scale: scale } - } -} - -impl > Transform, Point3> for Transform3 { - #[inline] - fn transform_vec(&self, vec: &Vec3) -> Vec3 { + fn transform_vec(&self, vec: &V) -> V { self.rot.rotate_vec( &vec.mul_s( self.scale.clone() )) } #[inline] - fn transform_point(&self, point: &Point3) -> Point3 { + fn transform_point(&self, point: &P) -> P { self.rot.rotate_point( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) } } + +/// A transformation in three dimensions consisting of a rotation, +/// displacement vector and scale amount. +pub struct Transform3( Decomposed,Quat> ); + +impl Transform3 { + #[inline] + pub fn new(scale: S, rot: Quat, disp: Vec3) -> Transform3 { + Transform3( Decomposed { scale: scale, rot: rot, disp: disp }) + } +} +