use std::fmt;
use rust_num::{Zero, One};
use structure::*;
use approx::ApproxEq;
use matrix::{Matrix2, Matrix3, Matrix4};
use num::{BaseFloat, BaseNum};
use point::{Point2, Point3};
use rotation::*;
use vector::{Vector2, Vector3};
pub trait Transform<P: EuclideanSpace>: Sized {
fn one() -> Self;
fn look_at(eye: P, center: P, up: P::Diff) -> Self;
fn transform_vector(&self, vec: P::Diff) -> P::Diff;
fn transform_point(&self, point: P) -> P;
#[inline]
fn transform_as_point(&self, vec: P::Diff) -> P::Diff {
self.transform_point(P::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(self, other);
}
#[inline]
fn invert_self(&mut self) {
*self = self.invert().unwrap()
}
}
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Decomposed<V: VectorSpace, R> {
pub scale: V::Scalar,
pub rot: R,
pub disp: V,
}
impl<P: EuclideanSpace, R: Rotation<P>> Transform<P> for Decomposed<P::Diff, R> where
<P as EuclideanSpace>::Scalar: BaseFloat,
<P as EuclideanSpace>::Diff: VectorSpace,
{
#[inline]
fn one() -> Decomposed<P::Diff, R> {
Decomposed {
scale: P::Scalar::one(),
rot: R::one(),
disp: P::Diff::zero(),
}
}
#[inline]
fn look_at(eye: P, center: P, up: P::Diff) -> Decomposed<P::Diff, R> {
let rot = R::look_at(center - eye, up);
let disp = rot.rotate_vector(P::origin() - eye);
Decomposed {
scale: P::Scalar::one(),
rot: rot,
disp: disp,
}
}
#[inline]
fn transform_vector(&self, vec: P::Diff) -> P::Diff {
self.rot.rotate_vector(vec * self.scale)
}
#[inline]
fn transform_point(&self, point: P) -> P {
self.rot.rotate_point(point * self.scale) + self.disp
}
fn concat(&self, other: &Decomposed<P::Diff, R>) -> Decomposed<P::Diff, R> {
Decomposed {
scale: self.scale * other.scale,
rot: self.rot.concat(&other.rot),
disp: self.transform_as_point(other.disp.clone()),
}
}
fn invert(&self) -> Option<Decomposed<P::Diff, R>> {
if self.scale.approx_eq(&P::Scalar::zero()) {
None
} else {
let s = P::Scalar::one() / self.scale;
let r = self.rot.invert();
let d = r.rotate_vector(self.disp.clone()) * -s;
Some(Decomposed {
scale: s,
rot: r,
disp: d,
})
}
}
}
pub trait Transform2<S: BaseNum>: Transform<Point2<S>> + Into<Matrix3<S>> {}
pub trait Transform3<S: BaseNum>: Transform<Point3<S>> + Into<Matrix4<S>> {}
impl<S: BaseFloat, R: Rotation2<S>> From<Decomposed<Vector2<S>, R>> for Matrix3<S> {
fn from(dec: Decomposed<Vector2<S>, R>) -> Matrix3<S> {
let m: Matrix2<_> = dec.rot.into();
let mut m: Matrix3<_> = (&m * dec.scale).into();
m.z = dec.disp.extend(S::one());
m
}
}
impl<S: BaseFloat, R: Rotation3<S>> From<Decomposed<Vector3<S>, R>> for Matrix4<S> {
fn from(dec: Decomposed<Vector3<S>, R>) -> Matrix4<S> {
let m: Matrix3<_> = dec.rot.into();
let mut m: Matrix4<_> = (&m * dec.scale).into();
m.w = dec.disp.extend(S::one());
m
}
}
impl<S: BaseFloat, R: Rotation2<S>> Transform2<S> for Decomposed<Vector2<S>, R> {}
impl<S: BaseFloat, R: Rotation3<S>> Transform3<S> for Decomposed<Vector3<S>, R> {}
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
pub struct AffineMatrix3<S> {
pub mat: Matrix4<S>,
}
impl<S: BaseFloat> Transform<Point3<S>> for AffineMatrix3<S> {
#[inline]
fn one() -> AffineMatrix3<S> {
AffineMatrix3 { mat: Matrix4::identity() }
}
#[inline]
fn look_at(eye: Point3<S>, center: Point3<S>, up: Vector3<S>) -> AffineMatrix3<S> {
AffineMatrix3 { mat: Matrix4::look_at(eye, center, up) }
}
#[inline]
fn transform_vector(&self, vec: Vector3<S>) -> Vector3<S> {
(self.mat * vec.extend(S::zero())).truncate()
}
#[inline]
fn transform_point(&self, point: Point3<S>) -> Point3<S> {
Point3::from_homogeneous(self.mat * point.to_homogeneous())
}
#[inline]
fn concat(&self, other: &AffineMatrix3<S>) -> AffineMatrix3<S> {
AffineMatrix3 { mat: self.mat * other.mat }
}
#[inline]
fn invert(&self) -> Option<AffineMatrix3<S>> {
self.mat.invert().map(|m| AffineMatrix3{ mat: m })
}
}
impl<S: BaseNum> From<AffineMatrix3<S>> for Matrix4<S> {
#[inline] fn from(aff: AffineMatrix3<S>) -> Matrix4<S> { aff.mat }
}
impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> {}
impl<S: fmt::Debug> fmt::Debug for AffineMatrix3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "AffineMatrix3 "));
<[[S; 4]; 4] as fmt::Debug>::fmt(self.mat.as_ref(), f)
}
}