From e2f34023ed91084520fa0358b4ad10180844a7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Mon, 22 Jul 2013 23:42:35 +0200 Subject: [PATCH] Add traits MatCast and VecCast. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit VecCast casts a Vec to a Vec with N1 and N2 bounded by NumCast. MatCast casts a Mat to a Mat with N1 and N2 bounded by NumCast. --- src/mat.rs | 46 +++++++++++++++++++++++++++++++++--------- src/mat_macros.rs | 13 +++++++++++- src/nalgebra.rc | 2 ++ src/traits/mat_cast.rs | 2 ++ src/traits/vec_cast.rs | 2 ++ src/vec.rs | 7 +++++++ src/vec_macros.rs | 10 +++++++++ 7 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 src/traits/mat_cast.rs create mode 100644 src/traits/vec_cast.rs diff --git a/src/mat.rs b/src/mat.rs index 2073d18c..fbced08d 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -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 { 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,8 +57,10 @@ pub struct Mat2 m21: N, m22: N } -mat_impl!(Mat2, 2, m11, m12, - m21, m22) +mat_impl!(Mat2, m11, m12, + m21, m22) +mat_cast_impl!(Mat2, m11, m12, + m21, m22) one_impl!(Mat2, _1, _0, _0, _1) iterable_impl!(Mat2, 2) @@ -82,9 +86,12 @@ pub struct Mat3 m31: N, m32: N, m33: N } -mat_impl!(Mat3, 3, m11, m12, m13, - m21, m22, m23, - m31, m32, m33) +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, _0, _1, _0, _0, _0, _1) @@ -112,7 +119,13 @@ pub struct Mat4 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 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 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, diff --git a/src/mat_macros.rs b/src/mat_macros.rs index f5a996c6..f25b59ba 100644 --- a/src/mat_macros.rs +++ b/src/mat_macros.rs @@ -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 $t { #[inline] @@ -16,6 +16,17 @@ macro_rules! mat_impl( ) ) +macro_rules! mat_cast_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl MatCast<$t> for $t + { + #[inline] + pub fn from(m: $t) -> $t + { $t::new(NumCast::from(m.$comp0.clone()) $(, NumCast::from(m.$compN.clone()) )*) } + } + ) +) + macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( impl Iterable for $t diff --git a/src/nalgebra.rc b/src/nalgebra.rc index 5c959b40..5e27fadb 100644 --- a/src/nalgebra.rc +++ b/src/nalgebra.rc @@ -65,6 +65,8 @@ mod traits mod rlmul; mod scalar_op; mod homogeneous; + mod vec_cast; + mod mat_cast; } #[cfg(test)] diff --git a/src/traits/mat_cast.rs b/src/traits/mat_cast.rs new file mode 100644 index 00000000..dfa9e21a --- /dev/null +++ b/src/traits/mat_cast.rs @@ -0,0 +1,2 @@ +pub trait MatCast +{ fn from(Self) -> M; } diff --git a/src/traits/vec_cast.rs b/src/traits/vec_cast.rs new file mode 100644 index 00000000..e16ed03a --- /dev/null +++ b/src/traits/vec_cast.rs @@ -0,0 +1,2 @@ +pub trait VecCast +{ fn from(Self) -> V; } diff --git a/src/vec.rs b/src/vec.rs index 12d8259b..3f1da03c 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -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 { 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 } 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 } 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 } 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 } 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 } 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) diff --git a/src/vec_macros.rs b/src/vec_macros.rs index 85c3e20b..5c2c0a2d 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -15,6 +15,16 @@ macro_rules! new_impl( } ) ) +macro_rules! vec_cast_impl( + ($t: ident, $comp0: ident $(,$compN: ident)*) => ( + impl VecCast<$t> for $t + { + #[inline] + pub fn from(v: $t) -> $t + { $t::new(NumCast::from(v.$comp0.clone()) $(, NumCast::from(v.$compN.clone()))*) } + } + ) +) macro_rules! indexable_impl( ($t: ident, $dim: expr) => (