diff --git a/src/identity_spec.rs b/src/identity_spec.rs new file mode 100644 index 00000000..24cab908 --- /dev/null +++ b/src/identity_spec.rs @@ -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 { + Some(mat::Identity) + } + + fn inplace_inverse(&mut self) -> bool { + true + } +} + +impl RMul for mat::Identity { + fn rmul(&self, m: &M) -> M { + m.clone() + } +} + +impl LMul for mat::Identity { + fn lmul(&self, m: &M) -> M { + m.clone() + } +} + +impl Mul 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 Translation 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 Translate for mat::Identity { + #[inline] + fn translate(&self, v: &V) -> V { + v.clone() + } + + #[inline] + fn inv_translate(&self, v: &V) -> V { + v.clone() + } +} + +impl Rotation 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 Rotate for mat::Identity { + #[inline] + fn rotate(&self, v: &V) -> V { + v.clone() + } + + #[inline] + fn inv_rotate(&self, v: &V) -> V { + v.clone() + } +} + +impl Transformation 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 Transform for mat::Identity { + #[inline] + fn transform(&self, v: &V) -> V { + v.clone() + } + + #[inline] + fn inv_transform(&self, v: &V) -> V { + v.clone() + } +} diff --git a/src/mat.rs b/src/mat.rs index 847b6a4c..af6062a0 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -28,6 +28,18 @@ pub use traits::transpose::*; 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. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] pub struct Mat1 { diff --git a/src/nalgebra.rc b/src/nalgebra.rc index 7454a396..1e0adecf 100644 --- a/src/nalgebra.rc +++ b/src/nalgebra.rc @@ -26,6 +26,7 @@ pub mod mat; mod mat_spec; mod vec_spec; mod vec0_spec; +mod identity_spec; /// Wrappers around raw matrices to restrict their behaviour. pub mod adaptors diff --git a/src/traits/rlmul.rs b/src/traits/rlmul.rs index f85624e0..a6685039 100644 --- a/src/traits/rlmul.rs +++ b/src/traits/rlmul.rs @@ -3,7 +3,7 @@ */ pub trait RMul { /// Computes self * v - fn rmul(&self, v : &V) -> V; + fn rmul(&self, v: &V) -> V; } /** diff --git a/src/vec0_spec.rs b/src/vec0_spec.rs index 83aedb57..c7774876 100644 --- a/src/vec0_spec.rs +++ b/src/vec0_spec.rs @@ -20,7 +20,7 @@ impl vec::Vec0 { } } -impl Indexable for vec::Vec0 { +impl Indexable for vec::Vec0 { #[inline] fn at(&self, _: uint) -> N { fail!("Cannot index a Vec0.") @@ -37,7 +37,7 @@ impl Indexable for vec::Vec0 { } } -impl vec::Vec0 { +impl vec::Vec0 { /// Creates a new vector. The parameter is not taken in account. #[inline] pub fn new_repeat(_: N) -> vec::Vec0 { @@ -66,7 +66,7 @@ impl Dim for vec::Vec0 { } } -impl> Basis for vec::Vec0 { +impl Basis for vec::Vec0 { #[inline(always)] fn canonical_basis(_: &fn(vec::Vec0) -> bool) { }