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 df75e2c..199687c 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/rotation.rs b/src/cgmath/rotation.rs index 6eba8fc..91fdb47 100644 --- a/src/cgmath/rotation.rs +++ b/src/cgmath/rotation.rs @@ -20,7 +20,7 @@ use matrix::{Mat2, ToMat2}; use matrix::{Mat3, ToMat3}; use point::{Point, Point2, Point3}; use quaternion::{Quat, ToQuat}; -use ray::{Ray, Ray2, Ray3}; +use ray::Ray; use vector::{Vector, Vec2, Vec3}; /// A trait for generic rotation @@ -35,9 +35,13 @@ pub trait Rotation + ApproxEq { fn identity() -> Self; - fn rotate_point(&self, point: &P) -> P; 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] fn rotate_ray(&self, ray: &Ray) -> Ray { Ray::new( //FIXME: use clone derived from Array @@ -115,15 +119,9 @@ impl Rotation, Point2> for Basis2 { #[inline] fn identity() -> Basis2 { Basis2{ mat: Mat2::identity() } } - #[inline] - fn rotate_point(&self, _point: &Point2) -> Point2 { fail!("Not yet implemented") } - #[inline] fn rotate_vec(&self, vec: &Vec2) -> Vec2 { self.mat.mul_v(vec) } - #[inline] - fn rotate_ray(&self, _ray: &Ray2) -> Ray2 { fail!("Not yet implemented") } - #[inline] fn concat(&self, other: &Basis2) -> Basis2 { Basis2 { mat: self.mat.mul_m(&other.mat) } } @@ -236,15 +234,9 @@ impl Rotation, Point3> for Basis3 { #[inline] fn identity() -> Basis3 { Basis3{ mat: Mat3::identity() } } - #[inline] - fn rotate_point(&self, _point: &Point3) -> Point3 { fail!("Not yet implemented") } - #[inline] fn rotate_vec(&self, vec: &Vec3) -> Vec3 { self.mat.mul_v(vec) } - #[inline] - fn rotate_ray(&self, _ray: &Ray3) -> Ray3 { fail!("Not yet implemented") } - #[inline] fn concat(&self, other: &Basis3) -> Basis3 { Basis3 { mat: self.mat.mul_m(&other.mat) } } @@ -298,15 +290,9 @@ impl Rotation, Point3> for Quat { #[inline] fn identity() -> Quat { Quat::identity() } - #[inline] - fn rotate_point(&self, _point: &Point3) -> Point3 { fail!("Not yet implemented") } - #[inline] fn rotate_vec(&self, vec: &Vec3) -> Vec3 { self.mul_v(vec) } - #[inline] - fn rotate_ray(&self, _ray: &Ray3) -> Ray3 { fail!("Not yet implemented") } - #[inline] fn concat(&self, other: &Quat) -> Quat { self.mul_q(other) } diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index e7e7847..c6bf6b1 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::num; +use std::{fmt,num}; use matrix::{Matrix, Mat4, ToMat4}; use point::{Point, Point3}; @@ -32,15 +32,28 @@ 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) ) } + #[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; + + #[inline] + fn concat_self(&mut self, other: &Self) { + *self = self.concat(other); + } + #[inline] fn invert_self(&mut self)-> bool { match self.invert() { @@ -86,7 +99,14 @@ Transform for Decomposed { self.rot.rotate_point( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) } - #[inline] + fn concat(&self, other: &Decomposed) -> Decomposed { + Decomposed { + scale: self.scale * other.scale, + rot: self.rot.concat( &other.rot ), + disp: self.transform_as_point( &other.disp ), + } + } + fn invert(&self) -> Option> { if self.scale.approx_eq( &num::zero() ) { None @@ -121,6 +141,15 @@ ToMat4 for Decomposed, R> { impl> Transform3 for Decomposed,R> {} +impl> +ToStr for Decomposed,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. pub struct AffineMatrix3 { mat: Mat4, @@ -143,6 +172,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 +188,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,