From 7f39520aa1aa61b7b0e84023cc45ecb15831698d Mon Sep 17 00:00:00 2001 From: kvark Date: Sat, 2 Nov 2013 09:11:13 -0400 Subject: [PATCH] Transform::concat implemented --- src/cgmath/array.rs | 12 ------------ src/cgmath/point.rs | 4 ++++ src/cgmath/transform.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index b50c9c4..6e06df9 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -44,18 +44,6 @@ 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/point.rs b/src/cgmath/point.rs index 388d7be..8267b56 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -61,6 +61,7 @@ impl Point3 { let e = v.truncate().mul_s( _1 / v.w ); Point3::new(e.x.clone(), e.y.clone(), e.z.clone()) //FIXME } + #[inline] pub fn to_homogeneous(&self) -> Vec4 { Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), one()) @@ -76,6 +77,9 @@ pub trait Point > : Array { + #[inline] fn from_vec(v: &V) -> Self { build(|i| v.i(i).clone()) } + #[inline] fn to_vec(&self) -> V { build(|i| self.i(i).clone()) } + #[inline] fn mul_s(&self, s: S) -> Self { build(|i| self.i(i).mul(&s)) } #[inline] fn div_s(&self, s: S) -> Self { build(|i| self.i(i).div(&s)) } #[inline] fn rem_s(&self, s: S) -> Self { build(|i| self.i(i).rem(&s)) } diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index e7e7847..5a1e844 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -32,15 +32,23 @@ pub trait Transform > { fn identity() -> Self; + fn transform_vec(&self, vec: &V) -> V; fn transform_point(&self, point: &P) -> P; - fn invert(&self) -> Option; #[inline] fn transform_ray(&self, ray: &Ray) -> Ray { Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) ) } + fn concat(&self, other: &Self) -> Self; + fn invert(&self) -> Option; + + #[inline] + fn concat_self(&mut self, other: &Self) { + *self = self.concat(other); + } + #[inline] fn invert_self(&mut self)-> bool { match self.invert() { @@ -86,7 +94,15 @@ Transform for Decomposed { self.rot.rotate_point( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) } - #[inline] + fn concat(&self, other: &Decomposed) -> Decomposed { + let p = Point::from_vec( &other.disp ); + Decomposed { + scale: self.scale * other.scale, + rot: self.rot.concat( &other.rot ), + disp: self.transform_point( &p ).to_vec(), + } + } + fn invert(&self) -> Option> { if self.scale.approx_eq( &num::zero() ) { None @@ -143,6 +159,11 @@ Transform, Point3> for AffineMatrix3 { Point3::from_homogeneous( &self.mat.mul_v( &point.to_homogeneous() )) } + #[inline] + fn concat(&self, other: &AffineMatrix3) -> AffineMatrix3 { + AffineMatrix3 { mat: self.mat.mul_m( &other.mat ) } + } + #[inline] fn invert(&self) -> Option> { self.mat.invert().map(|m| AffineMatrix3{ mat: m }) @@ -154,6 +175,8 @@ ToMat4 for AffineMatrix3 { #[inline] fn to_mat4(&self) -> Mat4 { self.mat.clone() } } +impl +Transform3 for AffineMatrix3 {} /// A transformation in three dimensions consisting of a rotation,