diff --git a/src/identity_spec.rs b/src/identity_spec.rs index 24cab908..fca227d0 100644 --- a/src/identity_spec.rs +++ b/src/identity_spec.rs @@ -10,13 +10,13 @@ use traits::transformation::{Transformation, Transform}; impl One for mat::Identity { #[inline] fn one() -> mat::Identity { - mat::Identity + mat::Identity::new() } } impl Inv for mat::Identity { fn inverse(&self) -> Option { - Some(mat::Identity) + Some(mat::Identity::new()) } fn inplace_inverse(&mut self) -> bool { @@ -46,7 +46,7 @@ impl Mul for mat::Identity { impl Transpose for mat::Identity { #[inline] fn transposed(&self) -> mat::Identity { - mat::Identity + mat::Identity::new() } #[inline] diff --git a/src/mat.rs b/src/mat.rs index af6062a0..cb66d189 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -30,13 +30,17 @@ mod mat_macros; /// Special identity matrix. All its operation are no-ops. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, Rand, Zero, ToStr)] -pub struct Identity; +pub struct Identity { + unused: uint // XXX: zero-sized structures ICE when used cross-crate. +} impl Identity { /// Creates a new identity matrix. #[inline] pub fn new() -> Identity { - Identity + Identity { + unused: 0 + } } } diff --git a/src/vec.rs b/src/vec.rs index ce9e8102..d591a3a3 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -6,7 +6,9 @@ use std::iterator::{Iterator, FromIterator}; use std::cmp::ApproxEq; use traits::basis::Basis; use traits::dim::Dim; -use traits::translation::Translation; +use traits::translation::{Translation, Translate}; +use traits::transformation::Transform; +use traits::rotation::Rotate; use traits::homogeneous::{FromHomogeneous, ToHomogeneous}; use traits::indexable::Indexable; use traits::scalar_op::{ScalarAdd, ScalarSub}; @@ -55,6 +57,9 @@ iterable_impl!(Vec1, 1) iterable_mut_impl!(Vec1, 1) to_homogeneous_impl!(Vec1, Vec2, y, x) from_homogeneous_impl!(Vec1, Vec2, y, x) +translate_impl!(Vec1) +rotate_impl!(Vec1) +transform_impl!(Vec1) /// Vector of dimension 2. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] @@ -93,6 +98,9 @@ iterable_impl!(Vec2, 2) iterable_mut_impl!(Vec2, 2) to_homogeneous_impl!(Vec2, Vec3, z, x, y) from_homogeneous_impl!(Vec2, Vec3, z, x, y) +translate_impl!(Vec2) +rotate_impl!(Vec2) +transform_impl!(Vec2) /// Vector of dimension 3. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] @@ -133,6 +141,9 @@ iterable_impl!(Vec3, 3) iterable_mut_impl!(Vec3, 3) to_homogeneous_impl!(Vec3, Vec4, w, x, y, z) from_homogeneous_impl!(Vec3, Vec4, w, x, y, z) +translate_impl!(Vec3) +rotate_impl!(Vec3) +transform_impl!(Vec3) /// Vector of dimension 4. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] @@ -175,6 +186,9 @@ iterable_impl!(Vec4, 4) iterable_mut_impl!(Vec4, 4) to_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w) from_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w) +translate_impl!(Vec4) +rotate_impl!(Vec4) +transform_impl!(Vec4) /// Vector of dimension 5. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] @@ -219,6 +233,9 @@ iterable_impl!(Vec5, 5) iterable_mut_impl!(Vec5, 5) to_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a) from_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a) +translate_impl!(Vec5) +rotate_impl!(Vec5) +transform_impl!(Vec5) /// Vector of dimension 6. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] @@ -263,3 +280,6 @@ from_iterator_impl!(Vec6, iterator, iterator, iterator, iterator, iterator, iter bounded_impl!(Vec6) iterable_impl!(Vec6, 6) iterable_mut_impl!(Vec6, 6) +translate_impl!(Vec6) +rotate_impl!(Vec6) +transform_impl!(Vec6) diff --git a/src/vec_macros.rs b/src/vec_macros.rs index 51e7c2b5..8b0ee6db 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -233,7 +233,7 @@ macro_rules! basis_impl( macro_rules! add_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> Add<$t, $t> for $t { + impl> Add<$t, $t> for $t { #[inline] fn add(&self, other: &$t) -> $t { $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*) @@ -244,7 +244,7 @@ macro_rules! add_impl( macro_rules! sub_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> Sub<$t, $t> for $t { + impl> Sub<$t, $t> for $t { #[inline] fn sub(&self, other: &$t) -> $t { $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*) @@ -512,3 +512,45 @@ macro_rules! from_homogeneous_impl( } ) ) + +macro_rules! translate_impl( + ($t: ident) => ( + impl + Sub> Translate<$t> for $t { + fn translate(&self, other: &$t) -> $t { + *other + *self + } + + fn inv_translate(&self, other: &$t) -> $t { + *other - *self + } + } + ) +) + +macro_rules! rotate_impl( + ($t: ident) => ( + impl Rotate for $t { + fn rotate(&self, other: &O) -> O { + other.clone() + } + + fn inv_rotate(&self, other: &O) -> O { + other.clone() + } + } + ) +) + +macro_rules! transform_impl( + ($t: ident) => ( + impl + Sub> Transform<$t> for $t { + fn transform(&self, other: &$t) -> $t { + self.translate(other) + } + + fn inv_transform(&self, other: &$t) -> $t { + self.inv_translate(other) + } + } + ) +)