From 2ccd62d558b66b9f584e54270c8c3786b70f5198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 6 Sep 2013 08:48:08 +0200 Subject: [PATCH] Add methods to set the translation/rotation/transformation directly. --- src/adaptors/rotmat.rs | 10 ++++++++++ src/adaptors/transform.rs | 16 ++++++++++++++++ src/dvec.rs | 5 +++++ src/identity_spec.rs | 15 +++++++++++++++ src/traits/rotation.rs | 3 +++ src/traits/transformation.rs | 3 +++ src/traits/translation.rs | 3 +++ src/vec0_spec.rs | 4 ++++ src/vec_macros.rs | 5 +++++ 9 files changed, 64 insertions(+) diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index 61442d35..8e11f3b6 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -141,6 +141,11 @@ Rotation> for Rotmat> { fn rotated(&self, rot: &Vec1) -> Rotmat> { Rotmat::from_angle(rot.x.clone()) * *self } + + #[inline] + fn set_rotation(&mut self, rot: Vec1) { + *self = Rotmat::from_angle(rot.x) + } } impl @@ -165,6 +170,11 @@ Rotation> for Rotmat> { fn rotated(&self, axisangle: &Vec3) -> Rotmat> { Rotmat::from_axis_angle(axisangle.clone()) * *self } + + #[inline] + fn set_rotation(&mut self, axisangle: Vec3) { + *self = Rotmat::from_axis_angle(axisangle) + } } impl> Rand for Rotmat> { diff --git a/src/adaptors/transform.rs b/src/adaptors/transform.rs index 46cc19d5..39b57968 100644 --- a/src/adaptors/transform.rs +++ b/src/adaptors/transform.rs @@ -27,6 +27,7 @@ pub struct Transform { priv subtrans : V } +// FIXME: this should be Trasform impl Transform { /// Builds a new transform from a matrix and a vector. #[inline] @@ -158,6 +159,11 @@ impl> Translation for Transform { fn translated(&self, t: &V) -> Transform { Transform::new(self.submat.clone(), self.subtrans.translated(t)) } + + #[inline] + fn set_translation(&mut self, t: V) { + self.subtrans.set_translation(t) + } } impl, V, _0> Translate for Transform { @@ -202,6 +208,12 @@ Rotation for Transform { Transform::new(self.submat.rotated(rot), delta.rmul(&self.subtrans)) } + + #[inline] + fn set_rotation(&mut self, rot: AV) { + // FIXME: should the translation be changed too? + self.submat.set_rotation(rot) + } } impl, V, _0> Rotate for Transform { @@ -237,6 +249,10 @@ Transformation> for Transform { fn transformed(&self, t: &Transform) -> Transform { t * *self } + + fn set_transformation(&mut self, t: Transform) { + *self = t + } } impl, V: Add + Sub> diff --git a/src/dvec.rs b/src/dvec.rs index 1dc8bd68..d351556f 100644 --- a/src/dvec.rs +++ b/src/dvec.rs @@ -226,6 +226,11 @@ impl + Neg + Clone> Translation> for DVec { fn translated(&self, t: &DVec) -> DVec { self + *t } + + #[inline] + fn set_translation(&mut self, t: DVec) { + *self = t + } } impl DVec { diff --git a/src/identity_spec.rs b/src/identity_spec.rs index fca227d0..e68a0bf9 100644 --- a/src/identity_spec.rs +++ b/src/identity_spec.rs @@ -74,6 +74,11 @@ impl Translation for mat::Identity { fn translated(&self, _: &V) -> mat::Identity { fail!("Attempted to translate the identity matrix.") } + + #[inline] + fn set_translation(&mut self, _: V) { + fail!("Attempted to translate the identity matrix.") + } } impl Translate for mat::Identity { @@ -108,6 +113,11 @@ impl Rotation for mat::Identity { fn rotated(&self, _: &V) -> mat::Identity { fail!("Attempted to rotate the identity matrix.") } + + #[inline] + fn set_rotation(&mut self, _: V) { + fail!("Attempted to rotate the identity matrix.") + } } impl Rotate for mat::Identity { @@ -142,6 +152,11 @@ impl Transformation for mat::Identity { fn transformed(&self, _: &M) -> mat::Identity { fail!("Attempted to transform the identity matrix.") } + + #[inline] + fn set_transformation(&mut self, _: M) { + fail!("Attempted to transform the identity matrix.") + } } impl Transform for mat::Identity { diff --git a/src/traits/rotation.rs b/src/traits/rotation.rs index a383ba05..0f6b2bd6 100644 --- a/src/traits/rotation.rs +++ b/src/traits/rotation.rs @@ -15,6 +15,9 @@ pub trait Rotation { /// Appends a rotation. fn rotated(&self, &V) -> Self; + + /// Sets the rotation. + fn set_rotation(&mut self, V); } /// Trait of objects able to rotate other objects. This is typically implemented by matrices which diff --git a/src/traits/transformation.rs b/src/traits/transformation.rs index 05cea382..b8bae178 100644 --- a/src/traits/transformation.rs +++ b/src/traits/transformation.rs @@ -13,6 +13,9 @@ pub trait Transformation { /// Appends a transformation. fn transformed(&self, &M) -> Self; + + /// Sets the transformation. + fn set_transformation(&mut self, M); } /// Trait of objects able to transform other objects. This is typically implemented by matrices which diff --git a/src/traits/translation.rs b/src/traits/translation.rs index 88c13513..96b24627 100644 --- a/src/traits/translation.rs +++ b/src/traits/translation.rs @@ -13,6 +13,9 @@ pub trait Translation { /// Appends a translation. fn translated(&self, &V) -> Self; + + /// Sets the translation. + fn set_translation(&mut self, V); } /// Trait of objects able to rotate other objects. This is typically implemented by matrices which diff --git a/src/vec0_spec.rs b/src/vec0_spec.rs index adab375f..3181534c 100644 --- a/src/vec0_spec.rs +++ b/src/vec0_spec.rs @@ -161,6 +161,10 @@ impl + Neg> Translation> for vec::Vec0 { fn translated(&self, t: &vec::Vec0) -> vec::Vec0 { self + *t } + + #[inline] + fn set_translation(&mut self, _: vec::Vec0) { + } } impl AlgebraicVec for vec::Vec0 { diff --git a/src/vec_macros.rs b/src/vec_macros.rs index c79a5b4d..9f991c45 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -358,6 +358,11 @@ macro_rules! translation_impl( fn translated(&self, t: &$t) -> $t { self + *t } + + #[inline] + fn set_translation(&mut self, t: $t) { + *self = t + } } ) )