Add traits MatCast and VecCast.

VecCast casts a Vec<N1> to a Vec<N2> with N1 and N2 bounded by NumCast.
MatCast casts a Mat<N1> to a Mat<N2> with N1 and N2 bounded by NumCast.
This commit is contained in:
Sébastien Crozet 2013-07-22 23:42:35 +02:00
parent 81389a911a
commit e2f34023ed
7 changed files with 72 additions and 10 deletions

View File

@ -17,6 +17,7 @@ use traits::indexable::Indexable;
use traits::column::Column;
use traits::iterable::{Iterable, IterableMut};
pub use traits::mat_cast::*;
pub use traits::column::*;
pub use traits::inv::*;
pub use traits::rlmul::*;
@ -31,7 +32,8 @@ mod mat_macros;
pub struct Mat1<N>
{ m11: N }
mat_impl!(Mat1, 1, m11)
mat_impl!(Mat1, m11)
mat_cast_impl!(Mat1, m11)
one_impl!(Mat1, _1)
iterable_impl!(Mat1, 1)
iterable_mut_impl!(Mat1, 1)
@ -55,7 +57,9 @@ pub struct Mat2<N>
m21: N, m22: N
}
mat_impl!(Mat2, 2, m11, m12,
mat_impl!(Mat2, m11, m12,
m21, m22)
mat_cast_impl!(Mat2, m11, m12,
m21, m22)
one_impl!(Mat2, _1, _0,
_0, _1)
@ -82,7 +86,10 @@ pub struct Mat3<N>
m31: N, m32: N, m33: N
}
mat_impl!(Mat3, 3, m11, m12, m13,
mat_impl!(Mat3, m11, m12, m13,
m21, m22, m23,
m31, m32, m33)
mat_cast_impl!(Mat3, m11, m12, m13,
m21, m22, m23,
m31, m32, m33)
one_impl!(Mat3, _1, _0, _0,
@ -112,7 +119,13 @@ pub struct Mat4<N>
m41: N, m42: N, m43: N, m44: N
}
mat_impl!(Mat4, 4,
mat_impl!(Mat4,
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44
)
mat_cast_impl!(Mat4,
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
@ -147,7 +160,14 @@ pub struct Mat5<N>
m51: N, m52: N, m53: N, m54: N, m55: N
}
mat_impl!(Mat5, 5,
mat_impl!(Mat5,
m11, m12, m13, m14, m15,
m21, m22, m23, m24, m25,
m31, m32, m33, m34, m35,
m41, m42, m43, m44, m45,
m51, m52, m53, m54, m55
)
mat_cast_impl!(Mat5,
m11, m12, m13, m14, m15,
m21, m22, m23, m24, m25,
m31, m32, m33, m34, m35,
@ -187,7 +207,15 @@ pub struct Mat6<N>
m61: N, m62: N, m63: N, m64: N, m65: N, m66: N
}
mat_impl!(Mat6, 6,
mat_impl!(Mat6,
m11, m12, m13, m14, m15, m16,
m21, m22, m23, m24, m25, m26,
m31, m32, m33, m34, m35, m36,
m41, m42, m43, m44, m45, m46,
m51, m52, m53, m54, m55, m56,
m61, m62, m63, m64, m65, m66
)
mat_cast_impl!(Mat6,
m11, m12, m13, m14, m15, m16,
m21, m22, m23, m24, m25, m26,
m31, m32, m33, m34, m35, m36,

View File

@ -1,7 +1,7 @@
#[macro_escape];
macro_rules! mat_impl(
($t: ident, $dim: expr, $comp0: ident $(,$compN: ident)*) => (
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N> $t<N>
{
#[inline]
@ -16,6 +16,17 @@ macro_rules! mat_impl(
)
)
macro_rules! mat_cast_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<Nin: NumCast + Clone, Nout: NumCast> MatCast<$t<Nout>> for $t<Nin>
{
#[inline]
pub fn from(m: $t<Nin>) -> $t<Nout>
{ $t::new(NumCast::from(m.$comp0.clone()) $(, NumCast::from(m.$compN.clone()) )*) }
}
)
)
macro_rules! iterable_impl(
($t: ident, $dim: expr) => (
impl<N> Iterable<N> for $t<N>

View File

@ -65,6 +65,8 @@ mod traits
mod rlmul;
mod scalar_op;
mod homogeneous;
mod vec_cast;
mod mat_cast;
}
#[cfg(test)]

2
src/traits/mat_cast.rs Normal file
View File

@ -0,0 +1,2 @@
pub trait MatCast<M>
{ fn from(Self) -> M; }

2
src/traits/vec_cast.rs Normal file
View File

@ -0,0 +1,2 @@
pub trait VecCast<V>
{ fn from(Self) -> V; }

View File

@ -13,6 +13,7 @@ use traits::division_ring::DivisionRing;
use traits::homogeneous::{FromHomogeneous, ToHomogeneous};
use traits::indexable::Indexable;
pub use traits::vec_cast::*;
pub use traits::basis::*;
pub use traits::cross::*;
pub use traits::dot::*;
@ -34,6 +35,7 @@ pub struct Vec1<N>
{ x: N }
new_impl!(Vec1, x)
vec_cast_impl!(Vec1, x)
indexable_impl!(Vec1, 1)
new_repeat_impl!(Vec1, val, x)
dim_impl!(Vec1, 1)
@ -67,6 +69,7 @@ pub struct Vec2<N>
}
new_impl!(Vec2, x, y)
vec_cast_impl!(Vec2, x, y)
indexable_impl!(Vec2, 2)
new_repeat_impl!(Vec2, val, x, y)
dim_impl!(Vec2, 2)
@ -101,6 +104,7 @@ pub struct Vec3<N>
}
new_impl!(Vec3, x, y, z)
vec_cast_impl!(Vec3, x, y, z)
indexable_impl!(Vec3, 3)
new_repeat_impl!(Vec3, val, x, y, z)
dim_impl!(Vec3, 3)
@ -136,6 +140,7 @@ pub struct Vec4<N>
}
new_impl!(Vec4, x, y, z, w)
vec_cast_impl!(Vec4, x, y, z, w)
indexable_impl!(Vec4, 4)
new_repeat_impl!(Vec4, val, x, y, z, w)
dim_impl!(Vec4, 4)
@ -172,6 +177,7 @@ pub struct Vec5<N>
}
new_impl!(Vec5, x, y, z, w, a)
vec_cast_impl!(Vec5, x, y, z, w, a)
indexable_impl!(Vec5, 5)
new_repeat_impl!(Vec5, val, x, y, z, w, a)
dim_impl!(Vec5, 5)
@ -209,6 +215,7 @@ pub struct Vec6<N>
}
new_impl!(Vec6, x, y, z, w, a, b)
vec_cast_impl!(Vec6, x, y, z, w, a, b)
indexable_impl!(Vec6, 6)
new_repeat_impl!(Vec6, val, x, y, z, w, a, b)
dim_impl!(Vec6, 6)

View File

@ -15,6 +15,16 @@ macro_rules! new_impl(
}
)
)
macro_rules! vec_cast_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<Nin: NumCast + Clone, Nout: NumCast> VecCast<$t<Nout>> for $t<Nin>
{
#[inline]
pub fn from(v: $t<Nin>) -> $t<Nout>
{ $t::new(NumCast::from(v.$comp0.clone()) $(, NumCast::from(v.$compN.clone()))*) }
}
)
)
macro_rules! indexable_impl(
($t: ident, $dim: expr) => (