Merge pull request #8 from kvark/master

Transform::concat implemented
This commit is contained in:
Brendan Zabarauskas 2013-11-08 06:34:51 -08:00
commit c916b65c6f
4 changed files with 49 additions and 35 deletions

View file

@ -44,18 +44,6 @@ 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

@ -61,6 +61,7 @@ impl<S: Clone + Num + Primitive> Point3<S> {
let e = v.truncate().mul_s( _1 / v.w ); let e = v.truncate().mul_s( _1 / v.w );
Point3::new(e.x.clone(), e.y.clone(), e.z.clone()) //FIXME Point3::new(e.x.clone(), e.y.clone(), e.z.clone()) //FIXME
} }
#[inline] #[inline]
pub fn to_homogeneous(&self) -> Vec4<S> { pub fn to_homogeneous(&self) -> Vec4<S> {
Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), one()) Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), one())
@ -76,6 +77,9 @@ pub trait Point
> >
: Array<S, Slice> : Array<S, Slice>
{ {
#[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 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 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)) } #[inline] fn rem_s(&self, s: S) -> Self { build(|i| self.i(i).rem(&s)) }

View file

@ -20,7 +20,7 @@ use matrix::{Mat2, ToMat2};
use matrix::{Mat3, ToMat3}; use matrix::{Mat3, ToMat3};
use point::{Point, Point2, Point3}; use point::{Point, Point2, Point3};
use quaternion::{Quat, ToQuat}; use quaternion::{Quat, ToQuat};
use ray::{Ray, Ray2, Ray3}; use ray::Ray;
use vector::{Vector, Vec2, Vec3}; use vector::{Vector, Vec2, Vec3};
/// A trait for generic rotation /// A trait for generic rotation
@ -35,9 +35,13 @@ pub trait Rotation
+ ApproxEq<S> + ApproxEq<S>
{ {
fn identity() -> Self; fn identity() -> Self;
fn rotate_point(&self, point: &P) -> P;
fn rotate_vec(&self, vec: &V) -> V; fn rotate_vec(&self, vec: &V) -> V;
#[inline]
fn rotate_point(&self, point: &P) -> P {
Point::from_vec( &self.rotate_vec( &point.to_vec() ) )
}
#[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( //FIXME: use clone derived from Array Ray::new( //FIXME: use clone derived from Array
@ -115,15 +119,9 @@ impl<S: Float> Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> {
#[inline] #[inline]
fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } } fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } }
#[inline]
fn rotate_point(&self, _point: &Point2<S>) -> Point2<S> { fail!("Not yet implemented") }
#[inline] #[inline]
fn rotate_vec(&self, vec: &Vec2<S>) -> Vec2<S> { self.mat.mul_v(vec) } fn rotate_vec(&self, vec: &Vec2<S>) -> Vec2<S> { self.mat.mul_v(vec) }
#[inline]
fn rotate_ray(&self, _ray: &Ray2<S>) -> Ray2<S> { fail!("Not yet implemented") }
#[inline] #[inline]
fn concat(&self, other: &Basis2<S>) -> Basis2<S> { Basis2 { mat: self.mat.mul_m(&other.mat) } } fn concat(&self, other: &Basis2<S>) -> Basis2<S> { Basis2 { mat: self.mat.mul_m(&other.mat) } }
@ -236,15 +234,9 @@ impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> {
#[inline] #[inline]
fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } } fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } }
#[inline]
fn rotate_point(&self, _point: &Point3<S>) -> Point3<S> { fail!("Not yet implemented") }
#[inline] #[inline]
fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mat.mul_v(vec) } fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mat.mul_v(vec) }
#[inline]
fn rotate_ray(&self, _ray: &Ray3<S>) -> Ray3<S> { fail!("Not yet implemented") }
#[inline] #[inline]
fn concat(&self, other: &Basis3<S>) -> Basis3<S> { Basis3 { mat: self.mat.mul_m(&other.mat) } } fn concat(&self, other: &Basis3<S>) -> Basis3<S> { Basis3 { mat: self.mat.mul_m(&other.mat) } }
@ -298,15 +290,9 @@ impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> {
#[inline] #[inline]
fn identity() -> Quat<S> { Quat::identity() } fn identity() -> Quat<S> { Quat::identity() }
#[inline]
fn rotate_point(&self, _point: &Point3<S>) -> Point3<S> { fail!("Not yet implemented") }
#[inline] #[inline]
fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mul_v(vec) } fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mul_v(vec) }
#[inline]
fn rotate_ray(&self, _ray: &Ray3<S>) -> Ray3<S> { fail!("Not yet implemented") }
#[inline] #[inline]
fn concat(&self, other: &Quat<S>) -> Quat<S> { self.mul_q(other) } fn concat(&self, other: &Quat<S>) -> Quat<S> { self.mul_q(other) }

View file

@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::num; use std::{fmt,num};
use matrix::{Matrix, Mat4, ToMat4}; use matrix::{Matrix, Mat4, ToMat4};
use point::{Point, Point3}; use point::{Point, Point3};
@ -32,15 +32,28 @@ pub trait Transform
> >
{ {
fn identity() -> Self; fn identity() -> Self;
fn transform_vec(&self, vec: &V) -> V; fn transform_vec(&self, vec: &V) -> V;
fn transform_point(&self, point: &P) -> P; fn transform_point(&self, point: &P) -> P;
fn invert(&self) -> Option<Self>;
#[inline] #[inline]
fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> { fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) ) Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) )
} }
#[inline]
fn transform_as_point(&self, vec: &V)-> V {
self.transform_point( &Point::from_vec(vec) ).to_vec()
}
fn concat(&self, other: &Self) -> Self;
fn invert(&self) -> Option<Self>;
#[inline]
fn concat_self(&mut self, other: &Self) {
*self = self.concat(other);
}
#[inline] #[inline]
fn invert_self(&mut self)-> bool { fn invert_self(&mut self)-> bool {
match self.invert() { match self.invert() {
@ -86,7 +99,14 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
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 )
} }
#[inline] fn concat(&self, other: &Decomposed<S,V,R>) -> Decomposed<S,V,R> {
Decomposed {
scale: self.scale * other.scale,
rot: self.rot.concat( &other.rot ),
disp: self.transform_as_point( &other.disp ),
}
}
fn invert(&self) -> Option<Decomposed<S,V,R>> { fn invert(&self) -> Option<Decomposed<S,V,R>> {
if self.scale.approx_eq( &num::zero() ) { if self.scale.approx_eq( &num::zero() ) {
None None
@ -121,6 +141,15 @@ ToMat4<S> for Decomposed<S, Vec3<S>, R> {
impl<S: Float, R: Rotation3<S>> impl<S: Float, R: Rotation3<S>>
Transform3<S> for Decomposed<S,Vec3<S>,R> {} Transform3<S> for Decomposed<S,Vec3<S>,R> {}
impl<S: fmt::Default + Float, R: ToStr + Rotation3<S>>
ToStr for Decomposed<S,Vec3<S>,R> {
fn to_str(&self) -> ~str {
format!("(scale({}), rot({:s}), disp{:s})",
self.scale, self.rot.to_str(), self.disp.to_str())
}
}
/// A homogeneous transformation matrix. /// A homogeneous transformation matrix.
pub struct AffineMatrix3<S> { pub struct AffineMatrix3<S> {
mat: Mat4<S>, mat: Mat4<S>,
@ -143,6 +172,11 @@ Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> {
Point3::from_homogeneous( &self.mat.mul_v( &point.to_homogeneous() )) Point3::from_homogeneous( &self.mat.mul_v( &point.to_homogeneous() ))
} }
#[inline]
fn concat(&self, other: &AffineMatrix3<S>) -> AffineMatrix3<S> {
AffineMatrix3 { mat: self.mat.mul_m( &other.mat ) }
}
#[inline] #[inline]
fn invert(&self) -> Option<AffineMatrix3<S>> { fn invert(&self) -> Option<AffineMatrix3<S>> {
self.mat.invert().map(|m| AffineMatrix3{ mat: m }) self.mat.invert().map(|m| AffineMatrix3{ mat: m })
@ -154,6 +188,8 @@ ToMat4<S> for AffineMatrix3<S> {
#[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() } #[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() }
} }
impl<S: Float>
Transform3<S> for AffineMatrix3<S> {}
/// A transformation in three dimensions consisting of a rotation, /// A transformation in three dimensions consisting of a rotation,