From 7e9bb98823383e40322fbdc7bc7e58d3b9511d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 10 Oct 2014 12:19:37 +0200 Subject: [PATCH] Add `to_pnt` and `to_vec` methods. --- src/structs/pnt.rs | 12 ++++++------ src/structs/pnt_macros.rs | 15 ++++++++++++++- src/structs/vec.rs | 12 ++++++------ src/structs/vec_macros.rs | 15 ++++++++++++++- src/traits/structure.rs | 6 ++++++ 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/structs/pnt.rs b/src/structs/pnt.rs index 4a861744..9820e6b9 100644 --- a/src/structs/pnt.rs +++ b/src/structs/pnt.rs @@ -59,7 +59,7 @@ at_fast_impl!(Pnt1, 1) new_repeat_impl!(Pnt1, val, x) dim_impl!(Pnt1, 1) container_impl!(Pnt1) -pnt_as_vec_impl!(Pnt1, Vec1) +pnt_as_vec_impl!(Pnt1, Vec1, x) pnt_sub_impl!(Pnt1, Vec1, Pnt1SubRhs) neg_impl!(Pnt1, x) pnt_add_vec_impl!(Pnt1, Vec1, Pnt1AddRhs, x) @@ -150,7 +150,7 @@ at_fast_impl!(Pnt2, 2) new_repeat_impl!(Pnt2, val, x, y) dim_impl!(Pnt2, 2) container_impl!(Pnt2) -pnt_as_vec_impl!(Pnt2, Vec2) +pnt_as_vec_impl!(Pnt2, Vec2, x, y) pnt_sub_impl!(Pnt2, Vec2, Pnt2SubRhs) neg_impl!(Pnt2, x, y) pnt_add_vec_impl!(Pnt2, Vec2, Pnt2AddRhs, x, y) @@ -243,7 +243,7 @@ at_fast_impl!(Pnt3, 3) new_repeat_impl!(Pnt3, val, x, y, z) dim_impl!(Pnt3, 3) container_impl!(Pnt3) -pnt_as_vec_impl!(Pnt3, Vec3) +pnt_as_vec_impl!(Pnt3, Vec3, x, y, z) pnt_sub_impl!(Pnt3, Vec3, Pnt3SubRhs) neg_impl!(Pnt3, x, y, z) pnt_add_vec_impl!(Pnt3, Vec3, Pnt3AddRhs, x, y, z) @@ -338,7 +338,7 @@ at_fast_impl!(Pnt4, 4) new_repeat_impl!(Pnt4, val, x, y, z, w) dim_impl!(Pnt4, 4) container_impl!(Pnt4) -pnt_as_vec_impl!(Pnt4, Vec4) +pnt_as_vec_impl!(Pnt4, Vec4, x, y, z, w) pnt_sub_impl!(Pnt4, Vec4, Pnt4SubRhs) neg_impl!(Pnt4, x, y, z, w) pnt_add_vec_impl!(Pnt4, Vec4, Pnt4AddRhs, x, y, z, w) @@ -435,7 +435,7 @@ at_fast_impl!(Pnt5, 5) new_repeat_impl!(Pnt5, val, x, y, z, w, a) dim_impl!(Pnt5, 5) container_impl!(Pnt5) -pnt_as_vec_impl!(Pnt5, Vec5) +pnt_as_vec_impl!(Pnt5, Vec5, x, y, z, w, a) pnt_sub_impl!(Pnt5, Vec5, Pnt5SubRhs) neg_impl!(Pnt5, x, y, z, w, a) pnt_add_vec_impl!(Pnt5, Vec5, Pnt5AddRhs, x, y, z, w, a) @@ -534,7 +534,7 @@ at_fast_impl!(Pnt6, 6) new_repeat_impl!(Pnt6, val, x, y, z, w, a, b) dim_impl!(Pnt6, 6) container_impl!(Pnt6) -pnt_as_vec_impl!(Pnt6, Vec6) +pnt_as_vec_impl!(Pnt6, Vec6, x, y, z, w, a, b) pnt_sub_impl!(Pnt6, Vec6, Pnt6SubRhs) neg_impl!(Pnt6, x, y, z, w, a, b) pnt_add_vec_impl!(Pnt6, Vec6, Pnt6AddRhs, x, y, z, w, a, b) diff --git a/src/structs/pnt_macros.rs b/src/structs/pnt_macros.rs index bbfd100b..1d326a9c 100644 --- a/src/structs/pnt_macros.rs +++ b/src/structs/pnt_macros.rs @@ -53,8 +53,16 @@ macro_rules! pnt_sub_vec_impl( ) macro_rules! pnt_as_vec_impl( - ($t: ident, $tv: ident) => ( + ($t: ident, $tv: ident, $comp0: ident $(,$compN: ident)*) => ( impl $t { + #[inline] + pub fn to_vec(self) -> $tv { + $tv::new( + self.$comp0 + $(, self.$compN)* + ) + } + #[inline] pub fn as_vec<'a>(&'a self) -> &'a $tv { unsafe { @@ -64,6 +72,11 @@ macro_rules! pnt_as_vec_impl( } impl PntAsVec<$tv> for $t { + #[inline] + fn to_vec(self) -> $tv { + self.to_vec() + } + #[inline] fn as_vec<'a>(&'a self) -> &'a $tv { self.as_vec() diff --git a/src/structs/vec.rs b/src/structs/vec.rs index 81f31752..25b0a44b 100644 --- a/src/structs/vec.rs +++ b/src/structs/vec.rs @@ -129,7 +129,7 @@ translate_impl!(Vec1, Pnt1) rotate_impl!(Vec1) rotate_impl!(Pnt1) transform_impl!(Vec1, Pnt1) -vec_as_pnt_impl!(Vec1, Pnt1) +vec_as_pnt_impl!(Vec1, Pnt1, x) /// Vector of dimension 2. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -230,7 +230,7 @@ translate_impl!(Vec2, Pnt2) rotate_impl!(Vec2) rotate_impl!(Pnt2) transform_impl!(Vec2, Pnt2) -vec_as_pnt_impl!(Vec2, Pnt2) +vec_as_pnt_impl!(Vec2, Pnt2, x, y) /// Vector of dimension 3. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -336,7 +336,7 @@ translate_impl!(Vec3, Pnt3) rotate_impl!(Vec3) rotate_impl!(Pnt3) transform_impl!(Vec3, Pnt3) -vec_as_pnt_impl!(Vec3, Pnt3) +vec_as_pnt_impl!(Vec3, Pnt3, x, y, z) /// Vector of dimension 4. @@ -442,7 +442,7 @@ translate_impl!(Vec4, Pnt4) rotate_impl!(Vec4) rotate_impl!(Pnt4) transform_impl!(Vec4, Pnt4) -vec_as_pnt_impl!(Vec4, Pnt4) +vec_as_pnt_impl!(Vec4, Pnt4, x, y, z, w) /// Vector of dimension 5. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -549,7 +549,7 @@ translate_impl!(Vec5, Pnt5) rotate_impl!(Vec5) rotate_impl!(Pnt5) transform_impl!(Vec5, Pnt5) -vec_as_pnt_impl!(Vec5, Pnt5) +vec_as_pnt_impl!(Vec5, Pnt5, x, y, z, w, a) /// Vector of dimension 6. #[deriving(Eq, PartialEq, Encodable, Decodable, Clone, Hash, Rand, Zero, Show)] @@ -656,4 +656,4 @@ translate_impl!(Vec6, Pnt6) rotate_impl!(Vec6) rotate_impl!(Pnt6) transform_impl!(Vec6, Pnt6) -vec_as_pnt_impl!(Vec6, Pnt6) +vec_as_pnt_impl!(Vec6, Pnt6, x, y, z, w, a, b) diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index b4aa58fd..e73ee5c3 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -682,8 +682,16 @@ macro_rules! transform_impl( ) macro_rules! vec_as_pnt_impl( - ($tv: ident, $t: ident) => ( + ($tv: ident, $t: ident, $comp0: ident $(,$compN: ident)*) => ( impl $tv { + #[inline] + pub fn to_pnt(self) -> $t { + $t::new( + self.$comp0 + $(, self.$compN)* + ) + } + #[inline] pub fn as_pnt<'a>(&'a self) -> &'a $t { unsafe { @@ -693,6 +701,11 @@ macro_rules! vec_as_pnt_impl( } impl VecAsPnt<$t> for $tv { + #[inline] + fn to_pnt(self) -> $t { + self.to_pnt() + } + #[inline] fn as_pnt<'a>(&'a self) -> &'a $t { self.as_pnt() diff --git a/src/traits/structure.rs b/src/traits/structure.rs index 679b1b02..34864891 100644 --- a/src/traits/structure.rs +++ b/src/traits/structure.rs @@ -147,6 +147,9 @@ pub trait IterableMut { */ /// Trait that relates a point of an affine space to a vector of the associated vector space. pub trait VecAsPnt

{ + /// Converts this point to its associated vector. + fn to_pnt(self) -> P; + /// Converts a reference to this point to a reference to its associated vector. fn as_pnt<'a>(&'a self) -> &'a P; } @@ -187,6 +190,9 @@ impl + VecExt + Basis> FloatVecExt for V { } */ /// Trait that relates a point of an affine space to a vector of the associated vector space. pub trait PntAsVec { + /// Converts this point to its associated vector. + fn to_vec(self) -> V; + /// Converts a reference to this point to a reference to its associated vector. fn as_vec<'a>(&'a self) -> &'a V; }