use std::fmt;
use structure::*;
use angle::Rad;
use approx::ApproxEq;
use matrix::{Matrix2, Matrix3};
use num::BaseFloat;
use point::{Point2, Point3};
use quaternion::Quaternion;
use vector::{Vector2, Vector3};
pub trait Rotation<P: EuclideanSpace>: PartialEq + Sized where
Self: ApproxEq<Epsilon = <P as EuclideanSpace>::Scalar>,
<P as EuclideanSpace>::Scalar: BaseFloat,
{
fn one() -> Self;
fn look_at(dir: P::Diff, up: P::Diff) -> Self;
fn between_vectors(a: P::Diff, b: P::Diff) -> Self;
fn rotate_vector(&self, vec: P::Diff) -> P::Diff;
#[inline]
fn rotate_point(&self, point: P) -> P {
P::from_vec(self.rotate_vector(point.to_vec()))
}
fn concat(&self, other: &Self) -> Self;
fn invert(&self) -> Self;
#[inline]
fn concat_self(&mut self, other: &Self) {
*self = Self::concat(self, other);
}
#[inline]
fn invert_self(&mut self) {
*self = self.invert();
}
}
pub trait Rotation2<S: BaseFloat>: Rotation<Point2<S>>
+ Into<Matrix2<S>>
+ Into<Basis2<S>> {
fn from_angle(theta: Rad<S>) -> Self;
}
pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
+ Into<Matrix3<S>>
+ Into<Basis3<S>>
+ Into<Quaternion<S>> {
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Self;
fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Self;
#[inline]
fn from_angle_x(theta: Rad<S>) -> Self {
Rotation3::from_axis_angle(Vector3::unit_x(), theta)
}
#[inline]
fn from_angle_y(theta: Rad<S>) -> Self {
Rotation3::from_axis_angle(Vector3::unit_y(), theta)
}
#[inline]
fn from_angle_z(theta: Rad<S>) -> Self {
Rotation3::from_axis_angle(Vector3::unit_z(), theta)
}
}
#[derive(PartialEq, Copy, Clone, RustcEncodable, RustcDecodable)]
pub struct Basis2<S> {
mat: Matrix2<S>
}
impl<S: BaseFloat> AsRef<Matrix2<S>> for Basis2<S> {
#[inline]
fn as_ref(&self) -> &Matrix2<S> {
&self.mat
}
}
impl<S: BaseFloat> From<Basis2<S>> for Matrix2<S> {
#[inline]
fn from(b: Basis2<S>) -> Matrix2<S> { b.mat }
}
impl<S: BaseFloat> Rotation<Point2<S>> for Basis2<S> {
#[inline]
fn one() -> Basis2<S> { Basis2 { mat: Matrix2::identity() } }
#[inline]
fn look_at(dir: Vector2<S>, up: Vector2<S>) -> Basis2<S> {
Basis2 { mat: Matrix2::look_at(dir, up) }
}
#[inline]
fn between_vectors(a: Vector2<S>, b: Vector2<S>) -> Basis2<S> {
Rotation2::from_angle(Rad::acos(a.dot(b)) )
}
#[inline]
fn rotate_vector(&self, vec: Vector2<S>) -> Vector2<S> { self.mat * vec }
#[inline]
fn concat(&self, other: &Basis2<S>) -> Basis2<S> { Basis2 { mat: self.mat * other.mat } }
#[inline]
fn concat_self(&mut self, other: &Basis2<S>) { self.mat = self.mat * other.mat; }
#[inline]
fn invert(&self) -> Basis2<S> { Basis2 { mat: self.mat.invert().unwrap() } }
#[inline]
fn invert_self(&mut self) { self.mat.invert_self(); }
}
impl<S: BaseFloat> ApproxEq for Basis2<S> {
type Epsilon = S;
#[inline]
fn approx_eq_eps(&self, other: &Basis2<S>, epsilon: &S) -> bool {
self.mat.approx_eq_eps(&other.mat, epsilon)
}
}
impl<S: BaseFloat> Rotation2<S> for Basis2<S> {
fn from_angle(theta: Rad<S>) -> Basis2<S> { Basis2 { mat: Matrix2::from_angle(theta) } }
}
impl<S: fmt::Debug> fmt::Debug for Basis2<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Basis2 "));
<[[S; 2]; 2] as fmt::Debug>::fmt(self.mat.as_ref(), f)
}
}
#[derive(PartialEq, Copy, Clone, RustcEncodable, RustcDecodable)]
pub struct Basis3<S> {
mat: Matrix3<S>
}
impl<S: BaseFloat> Basis3<S> {
#[inline]
pub fn from_quaternion(quaternion: &Quaternion<S>) -> Basis3<S> {
Basis3 { mat: quaternion.clone().into() }
}
}
impl<S> AsRef<Matrix3<S>> for Basis3<S> {
#[inline]
fn as_ref(&self) -> &Matrix3<S> {
&self.mat
}
}
impl<S: BaseFloat> From<Basis3<S>> for Matrix3<S> {
#[inline]
fn from(b: Basis3<S>) -> Matrix3<S> { b.mat }
}
impl<S: BaseFloat> From<Basis3<S>> for Quaternion<S> {
#[inline]
fn from(b: Basis3<S>) -> Quaternion<S> { b.mat.into() }
}
impl<S: BaseFloat> Rotation<Point3<S>> for Basis3<S> {
#[inline]
fn one() -> Basis3<S> { Basis3 { mat: Matrix3::identity() } }
#[inline]
fn look_at(dir: Vector3<S>, up: Vector3<S>) -> Basis3<S> {
Basis3 { mat: Matrix3::look_at(dir, up) }
}
#[inline]
fn between_vectors(a: Vector3<S>, b: Vector3<S>) -> Basis3<S> {
let q: Quaternion<S> = Rotation::between_vectors(a, b);
q.into()
}
#[inline]
fn rotate_vector(&self, vec: Vector3<S>) -> Vector3<S> { self.mat * vec }
#[inline]
fn concat(&self, other: &Basis3<S>) -> Basis3<S> { Basis3 { mat: self.mat * other.mat } }
#[inline]
fn concat_self(&mut self, other: &Basis3<S>) { self.mat = self.mat * other.mat; }
#[inline]
fn invert(&self) -> Basis3<S> { Basis3 { mat: self.mat.invert().unwrap() } }
#[inline]
fn invert_self(&mut self) { self.mat.invert_self(); }
}
impl<S: BaseFloat> ApproxEq for Basis3<S> {
type Epsilon = S;
#[inline]
fn approx_eq_eps(&self, other: &Basis3<S>, epsilon: &S) -> bool {
self.mat.approx_eq_eps(&other.mat, epsilon)
}
}
impl<S: BaseFloat> Rotation3<S> for Basis3<S> {
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Basis3<S> {
Basis3 { mat: Matrix3::from_axis_angle(axis, angle) }
}
fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Basis3<S> {
Basis3 { mat: Matrix3::from_euler(x, y ,z) }
}
fn from_angle_x(theta: Rad<S>) -> Basis3<S> {
Basis3 { mat: Matrix3::from_angle_x(theta) }
}
fn from_angle_y(theta: Rad<S>) -> Basis3<S> {
Basis3 { mat: Matrix3::from_angle_y(theta) }
}
fn from_angle_z(theta: Rad<S>) -> Basis3<S> {
Basis3 { mat: Matrix3::from_angle_z(theta) }
}
}
impl<S: fmt::Debug> fmt::Debug for Basis3<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Basis3 "));
<[[S; 3]; 3] as fmt::Debug>::fmt(self.mat.as_ref(), f)
}
}