Add Rotate, Transform, Translate impl for vectors.

This commit is contained in:
Sébastien Crozet 2013-08-23 16:29:08 +02:00
parent 833378d83f
commit 4635d6ebac
4 changed files with 74 additions and 8 deletions

View File

@ -10,13 +10,13 @@ use traits::transformation::{Transformation, Transform};
impl One for mat::Identity { impl One for mat::Identity {
#[inline] #[inline]
fn one() -> mat::Identity { fn one() -> mat::Identity {
mat::Identity mat::Identity::new()
} }
} }
impl Inv for mat::Identity { impl Inv for mat::Identity {
fn inverse(&self) -> Option<mat::Identity> { fn inverse(&self) -> Option<mat::Identity> {
Some(mat::Identity) Some(mat::Identity::new())
} }
fn inplace_inverse(&mut self) -> bool { fn inplace_inverse(&mut self) -> bool {
@ -46,7 +46,7 @@ impl<M: Clone> Mul<M, M> for mat::Identity {
impl Transpose for mat::Identity { impl Transpose for mat::Identity {
#[inline] #[inline]
fn transposed(&self) -> mat::Identity { fn transposed(&self) -> mat::Identity {
mat::Identity mat::Identity::new()
} }
#[inline] #[inline]

View File

@ -30,13 +30,17 @@ mod mat_macros;
/// Special identity matrix. All its operation are no-ops. /// Special identity matrix. All its operation are no-ops.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, Rand, Zero, ToStr)] #[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 { impl Identity {
/// Creates a new identity matrix. /// Creates a new identity matrix.
#[inline] #[inline]
pub fn new() -> Identity { pub fn new() -> Identity {
Identity Identity {
unused: 0
}
} }
} }

View File

@ -6,7 +6,9 @@ use std::iterator::{Iterator, FromIterator};
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
use traits::basis::Basis; use traits::basis::Basis;
use traits::dim::Dim; 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::homogeneous::{FromHomogeneous, ToHomogeneous};
use traits::indexable::Indexable; use traits::indexable::Indexable;
use traits::scalar_op::{ScalarAdd, ScalarSub}; use traits::scalar_op::{ScalarAdd, ScalarSub};
@ -55,6 +57,9 @@ iterable_impl!(Vec1, 1)
iterable_mut_impl!(Vec1, 1) iterable_mut_impl!(Vec1, 1)
to_homogeneous_impl!(Vec1, Vec2, y, x) to_homogeneous_impl!(Vec1, Vec2, y, x)
from_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. /// Vector of dimension 2.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -93,6 +98,9 @@ iterable_impl!(Vec2, 2)
iterable_mut_impl!(Vec2, 2) iterable_mut_impl!(Vec2, 2)
to_homogeneous_impl!(Vec2, Vec3, z, x, y) to_homogeneous_impl!(Vec2, Vec3, z, x, y)
from_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. /// Vector of dimension 3.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -133,6 +141,9 @@ iterable_impl!(Vec3, 3)
iterable_mut_impl!(Vec3, 3) iterable_mut_impl!(Vec3, 3)
to_homogeneous_impl!(Vec3, Vec4, w, x, y, z) to_homogeneous_impl!(Vec3, Vec4, w, x, y, z)
from_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. /// Vector of dimension 4.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -175,6 +186,9 @@ iterable_impl!(Vec4, 4)
iterable_mut_impl!(Vec4, 4) iterable_mut_impl!(Vec4, 4)
to_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w) to_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w)
from_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. /// Vector of dimension 5.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)]
@ -219,6 +233,9 @@ iterable_impl!(Vec5, 5)
iterable_mut_impl!(Vec5, 5) iterable_mut_impl!(Vec5, 5)
to_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a) to_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a)
from_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. /// Vector of dimension 6.
#[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] #[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) bounded_impl!(Vec6)
iterable_impl!(Vec6, 6) iterable_impl!(Vec6, 6)
iterable_mut_impl!(Vec6, 6) iterable_mut_impl!(Vec6, 6)
translate_impl!(Vec6)
rotate_impl!(Vec6)
transform_impl!(Vec6)

View File

@ -233,7 +233,7 @@ macro_rules! basis_impl(
macro_rules! add_impl( macro_rules! add_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Add<N, N>> Add<$t<N>, $t<N>> for $t<N> { impl<N: Add<N, N>> Add<$t<N>, $t<N>> for $t<N> {
#[inline] #[inline]
fn add(&self, other: &$t<N>) -> $t<N> { fn add(&self, other: &$t<N>) -> $t<N> {
$t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*) $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*)
@ -244,7 +244,7 @@ macro_rules! add_impl(
macro_rules! sub_impl( macro_rules! sub_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone + Sub<N, N>> Sub<$t<N>, $t<N>> for $t<N> { impl<N: Sub<N, N>> Sub<$t<N>, $t<N>> for $t<N> {
#[inline] #[inline]
fn sub(&self, other: &$t<N>) -> $t<N> { fn sub(&self, other: &$t<N>) -> $t<N> {
$t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*) $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<N: Add<N, N> + Sub<N, N>> Translate<$t<N>> for $t<N> {
fn translate(&self, other: &$t<N>) -> $t<N> {
*other + *self
}
fn inv_translate(&self, other: &$t<N>) -> $t<N> {
*other - *self
}
}
)
)
macro_rules! rotate_impl(
($t: ident) => (
impl<N, O: Clone> Rotate<O> for $t<N> {
fn rotate(&self, other: &O) -> O {
other.clone()
}
fn inv_rotate(&self, other: &O) -> O {
other.clone()
}
}
)
)
macro_rules! transform_impl(
($t: ident) => (
impl<N: Add<N, N> + Sub<N, N>> Transform<$t<N>> for $t<N> {
fn transform(&self, other: &$t<N>) -> $t<N> {
self.translate(other)
}
fn inv_transform(&self, other: &$t<N>) -> $t<N> {
self.inv_translate(other)
}
}
)
)