From 7f39520aa1aa61b7b0e84023cc45ecb15831698d Mon Sep 17 00:00:00 2001 From: kvark Date: Sat, 2 Nov 2013 09:11:13 -0400 Subject: [PATCH 1/4] 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, From efd3403bf2e2caf885843ba581b9be43333774e5 Mon Sep 17 00:00:00 2001 From: kvark Date: Sat, 2 Nov 2013 09:51:20 -0400 Subject: [PATCH 2/4] ToStr implemented for transform::Decomposed --- src/cgmath/transform.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index 5a1e844..7dd2bbe 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}; @@ -137,6 +137,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, From c13ebf57ab5493140b1e6b72ecb7d022d01ebea1 Mon Sep 17 00:00:00 2001 From: kvark Date: Sat, 2 Nov 2013 10:18:37 -0400 Subject: [PATCH 3/4] Added transform_as_point --- src/cgmath/transform.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index 7dd2bbe..c6bf6b1 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -41,6 +41,11 @@ pub trait Transform 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; @@ -95,11 +100,10 @@ Transform for Decomposed { } 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(), + disp: self.transform_as_point( &other.disp ), } } From 42e38017152a60ba666f37007b0d4ec6b453af3a Mon Sep 17 00:00:00 2001 From: kvark Date: Sat, 2 Nov 2013 11:16:18 -0400 Subject: [PATCH 4/4] Implemented missing rotation functions (rotate_point, rotate_ray) --- src/cgmath/rotation.rs | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) 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) }