commit
1382c2ea4b
1 changed files with 26 additions and 58 deletions
|
@ -13,9 +13,7 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{fmt,num};
|
||||
|
||||
use std::num::one;
|
||||
use std::{fmt, num};
|
||||
|
||||
use approx::ApproxEq;
|
||||
use matrix::{Matrix, Matrix4, ToMatrix4};
|
||||
|
@ -23,7 +21,6 @@ use num::{BaseNum, BaseFloat};
|
|||
use point::{Point, Point3};
|
||||
use ray::Ray;
|
||||
use rotation::{Rotation, Rotation3};
|
||||
use quaternion::Quaternion;
|
||||
use vector::{Vector, Vector3};
|
||||
|
||||
/// A trait representing an [affine
|
||||
|
@ -47,13 +44,13 @@ pub trait Transform<S: BaseNum, V: Vector<S>, P: Point<S,V>> {
|
|||
/// Transform a ray using this transform.
|
||||
#[inline]
|
||||
fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
|
||||
Ray::new( self.transform_point(&ray.origin), self.transform_vector(&ray.direction) )
|
||||
Ray::new(self.transform_point(&ray.origin), self.transform_vector(&ray.direction))
|
||||
}
|
||||
|
||||
/// Transform a vector as a point using this transform.
|
||||
#[inline]
|
||||
fn transform_as_point(&self, vec: &V) -> V {
|
||||
self.transform_point( &Point::from_vec(vec) ).to_vec()
|
||||
self.transform_point(&Point::from_vec(vec)).to_vec()
|
||||
}
|
||||
|
||||
/// Combine this transform with another, yielding a new transformation
|
||||
|
@ -79,15 +76,15 @@ pub trait Transform<S: BaseNum, V: Vector<S>, P: Point<S,V>> {
|
|||
|
||||
/// A generic transformation consisting of a rotation,
|
||||
/// displacement vector and scale amount.
|
||||
pub struct Decomposed<S,V,R> {
|
||||
pub struct Decomposed<S, V, R> {
|
||||
pub scale: S,
|
||||
pub rot: R,
|
||||
pub disp: V,
|
||||
}
|
||||
|
||||
impl<S: BaseFloat, V: Vector<S>, P: Point<S, V>, R: Rotation<S, V, P>> Transform<S, V, P> for Decomposed<S,V,R> {
|
||||
impl<S: BaseFloat, V: Vector<S>, P: Point<S, V>, R: Rotation<S, V, P>> Transform<S, V, P> for Decomposed<S, V, R> {
|
||||
#[inline]
|
||||
fn identity() -> Decomposed<S,V,R> {
|
||||
fn identity() -> Decomposed<S, V, R> {
|
||||
Decomposed {
|
||||
scale: num::one(),
|
||||
rot: Rotation::identity(),
|
||||
|
@ -96,10 +93,10 @@ impl<S: BaseFloat, V: Vector<S>, P: Point<S, V>, R: Rotation<S, V, P>> Transform
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn look_at(eye: &P, center: &P, up: &V) -> Decomposed<S,V,R> {
|
||||
let origin :P = Point::origin();
|
||||
let rot :R = Rotation::look_at( ¢er.sub_p(eye), up );
|
||||
let disp :V = rot.rotate_vector( &origin.sub_p(eye) );
|
||||
fn look_at(eye: &P, center: &P, up: &V) -> Decomposed<S, V, R> {
|
||||
let origin: P = Point::origin();
|
||||
let rot: R = Rotation::look_at(¢er.sub_p(eye), up);
|
||||
let disp: V = rot.rotate_vector(&origin.sub_p(eye));
|
||||
Decomposed {
|
||||
scale: num::one(),
|
||||
rot: rot,
|
||||
|
@ -109,31 +106,30 @@ impl<S: BaseFloat, V: Vector<S>, P: Point<S, V>, R: Rotation<S, V, P>> Transform
|
|||
|
||||
#[inline]
|
||||
fn transform_vector(&self, vec: &V) -> V {
|
||||
self.rot.rotate_vector( &vec.mul_s( self.scale.clone() ))
|
||||
self.rot.rotate_vector(&vec.mul_s(self.scale.clone()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
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)
|
||||
}
|
||||
|
||||
fn concat(&self, other: &Decomposed<S,V,R>) -> Decomposed<S,V,R> {
|
||||
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 ),
|
||||
rot: self.rot.concat(&other.rot),
|
||||
disp: self.transform_as_point(&other.disp),
|
||||
}
|
||||
}
|
||||
|
||||
fn invert(&self) -> Option<Decomposed<S,V,R>> {
|
||||
if self.scale.approx_eq( &num::zero() ) {
|
||||
fn invert(&self) -> Option<Decomposed<S, V, R>> {
|
||||
if self.scale.approx_eq(&num::zero()) {
|
||||
None
|
||||
}else {
|
||||
let _1 : S = num::one();
|
||||
let s = _1 / self.scale;
|
||||
} else {
|
||||
let s = num::one::<S>() / self.scale;
|
||||
let r = self.rot.invert();
|
||||
let d = r.rotate_vector( &self.disp ).mul_s( -s );
|
||||
Some( Decomposed {
|
||||
let d = r.rotate_vector(&self.disp).mul_s(-s);
|
||||
Some(Decomposed {
|
||||
scale: s,
|
||||
rot: r,
|
||||
disp: d,
|
||||
|
@ -146,8 +142,8 @@ pub trait Transform3<S>: Transform<S, Vector3<S>, Point3<S>>+ ToMatrix4<S> {}
|
|||
|
||||
impl<S: BaseFloat, R: Rotation3<S>> ToMatrix4<S> for Decomposed<S, Vector3<S>, R> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
let mut m = self.rot.to_matrix3().mul_s( self.scale.clone() ).to_matrix4();
|
||||
m.w = self.disp.extend( num::one() );
|
||||
let mut m = self.rot.to_matrix3().mul_s(self.scale.clone()).to_matrix4();
|
||||
m.w = self.disp.extend(num::one());
|
||||
m
|
||||
}
|
||||
}
|
||||
|
@ -161,7 +157,6 @@ impl<S: BaseFloat, R: fmt::Show + Rotation3<S>> fmt::Show for Decomposed<S,Vecto
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// A homogeneous transformation matrix.
|
||||
pub struct AffineMatrix3<S> {
|
||||
pub mat: Matrix4<S>,
|
||||
|
@ -180,17 +175,17 @@ impl<S : BaseFloat> Transform<S, Vector3<S>, Point3<S>> for AffineMatrix3<S> {
|
|||
|
||||
#[inline]
|
||||
fn transform_vector(&self, vec: &Vector3<S>) -> Vector3<S> {
|
||||
self.mat.mul_v( &vec.extend(num::zero()) ).truncate()
|
||||
self.mat.mul_v(&vec.extend(num::zero())).truncate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn transform_point(&self, point: &Point3<S>) -> Point3<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 ) }
|
||||
AffineMatrix3 { mat: self.mat.mul_m(&other.mat) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -204,30 +199,3 @@ impl<S: BaseNum> ToMatrix4<S> for AffineMatrix3<S> {
|
|||
}
|
||||
|
||||
impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> {}
|
||||
|
||||
|
||||
/// A transformation in three dimensions consisting of a rotation,
|
||||
/// displacement vector and scale amount.
|
||||
pub struct Transform3D<S>(Decomposed<S,Vector3<S>,Quaternion<S>>);
|
||||
|
||||
impl<S: BaseFloat> Transform3D<S> {
|
||||
#[inline]
|
||||
pub fn new(scale: S, rot: Quaternion<S>, disp: Vector3<S>) -> Transform3D<S> {
|
||||
Transform3D( Decomposed { scale: scale, rot: rot, disp: disp })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn translate(x: S, y: S, z: S) -> Transform3D<S> {
|
||||
Transform3D( Decomposed { scale: one(), rot: Quaternion::zero(), disp: Vector3::new(x, y, z) })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get<'a>(&'a self) -> &'a Decomposed<S,Vector3<S>,Quaternion<S>> {
|
||||
let &Transform3D(ref d) = self;
|
||||
d
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> ToMatrix4<S> for Transform3D<S> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> { self.get().to_matrix4() }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue