Add and Identity matrix type.

It implements most matrix traits but all its operations are no-ops.
This commit is contained in:
Sébastien Crozet 2013-08-23 15:59:26 +02:00
parent 6999444575
commit 833378d83f
5 changed files with 174 additions and 4 deletions

157
src/identity_spec.rs Normal file
View File

@ -0,0 +1,157 @@
use std::num::{One, Zero};
use mat;
use traits::inv::Inv;
use traits::transpose::Transpose;
use traits::rlmul::{RMul, LMul};
use traits::translation::{Translation, Translate};
use traits::rotation::{Rotation, Rotate};
use traits::transformation::{Transformation, Transform};
impl One for mat::Identity {
#[inline]
fn one() -> mat::Identity {
mat::Identity
}
}
impl Inv for mat::Identity {
fn inverse(&self) -> Option<mat::Identity> {
Some(mat::Identity)
}
fn inplace_inverse(&mut self) -> bool {
true
}
}
impl<M: Clone> RMul<M> for mat::Identity {
fn rmul(&self, m: &M) -> M {
m.clone()
}
}
impl<M: Clone> LMul<M> for mat::Identity {
fn lmul(&self, m: &M) -> M {
m.clone()
}
}
impl<M: Clone> Mul<M, M> for mat::Identity {
#[inline]
fn mul(&self, other: &M) -> M {
other.clone()
}
}
impl Transpose for mat::Identity {
#[inline]
fn transposed(&self) -> mat::Identity {
mat::Identity
}
#[inline]
fn transpose(&mut self) {
}
}
impl<V: Zero> Translation<V> for mat::Identity {
#[inline]
fn translation(&self) -> V {
Zero::zero()
}
#[inline]
fn inv_translation(&self) -> V {
Zero::zero()
}
#[inline]
fn translate_by(&mut self, _: &V) {
fail!("Attempted to translate the identity matrix.")
}
#[inline]
fn translated(&self, _: &V) -> mat::Identity {
fail!("Attempted to translate the identity matrix.")
}
}
impl<V: Clone> Translate<V> for mat::Identity {
#[inline]
fn translate(&self, v: &V) -> V {
v.clone()
}
#[inline]
fn inv_translate(&self, v: &V) -> V {
v.clone()
}
}
impl<V: Zero> Rotation<V> for mat::Identity {
#[inline]
fn rotation(&self) -> V {
Zero::zero()
}
#[inline]
fn inv_rotation(&self) -> V {
Zero::zero()
}
#[inline]
fn rotate_by(&mut self, _: &V) {
fail!("Attempted to rotate the identity matrix.")
}
#[inline]
fn rotated(&self, _: &V) -> mat::Identity {
fail!("Attempted to rotate the identity matrix.")
}
}
impl<V: Clone> Rotate<V> for mat::Identity {
#[inline]
fn rotate(&self, v: &V) -> V {
v.clone()
}
#[inline]
fn inv_rotate(&self, v: &V) -> V {
v.clone()
}
}
impl<M: One> Transformation<M> for mat::Identity {
#[inline]
fn transformation(&self) -> M {
One::one()
}
#[inline]
fn inv_transformation(&self) -> M {
One::one()
}
#[inline]
fn transform_by(&mut self, _: &M) {
fail!("Attempted to transform the identity matrix.")
}
#[inline]
fn transformed(&self, _: &M) -> mat::Identity {
fail!("Attempted to transform the identity matrix.")
}
}
impl<V: Clone> Transform<V> for mat::Identity {
#[inline]
fn transform(&self, v: &V) -> V {
v.clone()
}
#[inline]
fn inv_transform(&self, v: &V) -> V {
v.clone()
}
}

View File

@ -28,6 +28,18 @@ pub use traits::transpose::*;
mod mat_macros; mod mat_macros;
/// Special identity matrix. All its operation are no-ops.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, Rand, Zero, ToStr)]
pub struct Identity;
impl Identity {
/// Creates a new identity matrix.
#[inline]
pub fn new() -> Identity {
Identity
}
}
/// Square matrix of dimension 1. /// Square matrix of dimension 1.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
pub struct Mat1<N> { pub struct Mat1<N> {

View File

@ -26,6 +26,7 @@ pub mod mat;
mod mat_spec; mod mat_spec;
mod vec_spec; mod vec_spec;
mod vec0_spec; mod vec0_spec;
mod identity_spec;
/// Wrappers around raw matrices to restrict their behaviour. /// Wrappers around raw matrices to restrict their behaviour.
pub mod adaptors pub mod adaptors

View File

@ -3,7 +3,7 @@
*/ */
pub trait RMul<V> { pub trait RMul<V> {
/// Computes self * v /// Computes self * v
fn rmul(&self, v : &V) -> V; fn rmul(&self, v: &V) -> V;
} }
/** /**

View File

@ -20,7 +20,7 @@ impl<N> vec::Vec0<N> {
} }
} }
impl<N: Clone> Indexable<uint, N> for vec::Vec0<N> { impl<N> Indexable<uint, N> for vec::Vec0<N> {
#[inline] #[inline]
fn at(&self, _: uint) -> N { fn at(&self, _: uint) -> N {
fail!("Cannot index a Vec0.") fail!("Cannot index a Vec0.")
@ -37,7 +37,7 @@ impl<N: Clone> Indexable<uint, N> for vec::Vec0<N> {
} }
} }
impl<N: Clone> vec::Vec0<N> { impl<N> vec::Vec0<N> {
/// Creates a new vector. The parameter is not taken in account. /// Creates a new vector. The parameter is not taken in account.
#[inline] #[inline]
pub fn new_repeat(_: N) -> vec::Vec0<N> { pub fn new_repeat(_: N) -> vec::Vec0<N> {
@ -66,7 +66,7 @@ impl<N> Dim for vec::Vec0<N> {
} }
} }
impl<N: Clone + Num + Algebraic + ApproxEq<N>> Basis for vec::Vec0<N> { impl<N> Basis for vec::Vec0<N> {
#[inline(always)] #[inline(always)]
fn canonical_basis(_: &fn(vec::Vec0<N>) -> bool) { } fn canonical_basis(_: &fn(vec::Vec0<N>) -> bool) { }