Generalized transform trait over any-dimensional vectors and points

This commit is contained in:
kvark 2013-11-01 08:29:01 -04:00
parent 0c41aa3951
commit 6dd06103fc
3 changed files with 48 additions and 19 deletions

View file

@ -44,6 +44,18 @@ pub trait Array
fn each_mut(&mut self, f: &fn(i: uint, x: &mut T)); fn each_mut(&mut self, f: &fn(i: uint, x: &mut T));
} }
/*impl //TODO
<
T: Clone,
Slice,
A: Array<T,Slice>
>
Clone for A {
fn clone(&self) -> A {
self.build(|i| self.i(i).clone())
}
}*/
macro_rules! array( macro_rules! array(
(impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr] $_n:ident) => ( (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr] $_n:ident) => (
impl<$S: Clone> Array<$T, [$T,..$n]> for $Self { impl<$S: Clone> Array<$T, [$T,..$n]> for $Self {

View file

@ -14,6 +14,7 @@
// limitations under the License. // limitations under the License.
use angle::Rad; use angle::Rad;
use array::Array;
use matrix::Matrix; use matrix::Matrix;
use matrix::{Mat2, ToMat2}; use matrix::{Mat2, ToMat2};
use matrix::{Mat3, ToMat3}; use matrix::{Mat3, ToMat3};
@ -25,10 +26,10 @@ use vector::{Vector, Vec2, Vec3};
/// A trait for generic rotation /// A trait for generic rotation
pub trait Rotation pub trait Rotation
< <
S: Primitive + Clone, S: Primitive,
Slice, Slice,
V: Vector<S,Slice>, V: Vector<S,Slice>,
P: Point<S,V,Slice> + Clone P: Point<S,V,Slice>
> >
: Eq : Eq
+ ApproxEq<S> + ApproxEq<S>
@ -38,7 +39,9 @@ pub trait Rotation
#[inline] #[inline]
fn rotate_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> { fn rotate_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
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; fn concat(&self, other: &Self) -> Self;

View file

@ -14,9 +14,10 @@
// limitations under the License. // limitations under the License.
use matrix::Mat4; use matrix::Mat4;
use point::{Point, Point3}; use point::Point;
use ray::Ray; use ray::Ray;
use rotation::Rotation3; use rotation::Rotation;
use quaternion::Quat;
use vector::{Vector, Vec3}; use vector::{Vector, Vec3};
/// A trait of affine transformation, that can be applied to points or vectors /// A trait of affine transformation, that can be applied to points or vectors
@ -42,29 +43,42 @@ pub struct AffineMatrix3<S> {
mat: Mat4<S>, mat: Mat4<S>,
} }
/// A transformation in three dimensions consisting of a rotation, /// A generic transformation consisting of a rotation,
/// displacement vector and scale amount. /// displacement vector and scale amount.
pub struct Transform3<S, R> { pub struct Decomposed<S,V,R> {
rot: R,
disp: Vec3<S>,
scale: S, scale: S,
rot: R,
disp: V,
} }
impl<S: Float, R: Rotation3<S>> Transform3<S, R> { impl
<
S: Float,
Slice,
V: Vector<S, Slice>,
P: Point<S, V, Slice>,
R: Rotation<S, Slice, V, P>
>
Transform<S, Slice, V, P> for Decomposed<S,V,R> {
#[inline] #[inline]
pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3<S, R> { fn transform_vec(&self, vec: &V) -> V {
Transform3 { rot: rot, disp: disp, scale: scale }
}
}
impl <S: Float, R: Rotation3<S>> Transform<S, [S, .. 3], Vec3<S>, Point3<S>> for Transform3<S,R> {
#[inline]
fn transform_vec(&self, vec: &Vec3<S>) -> Vec3<S> {
self.rot.rotate_vec( &vec.mul_s( self.scale.clone() )) self.rot.rotate_vec( &vec.mul_s( self.scale.clone() ))
} }
#[inline] #[inline]
fn transform_point(&self, point: &Point3<S>) -> Point3<S> { fn transform_point(&self, point: &P) -> P {
self.rot.rotate_point( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) 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<S>( Decomposed<S,Vec3<S>,Quat<S>> );
impl<S: Float> Transform3<S> {
#[inline]
pub fn new(scale: S, rot: Quat<S>, disp: Vec3<S>) -> Transform3<S> {
Transform3( Decomposed { scale: scale, rot: rot, disp: disp })
}
}