From 5cb98d09cfd471751c219f6401bc118ba6ac551c Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 12 Jun 2013 11:30:18 +1000 Subject: [PATCH] Add conversion traits --- src/mat.rs | 6 +++--- src/mat2.rs | 11 ++++++++++- src/mat3.rs | 12 ++++++++++-- src/mat4.rs | 4 ++++ src/quat.rs | 26 ++++++++++++++++---------- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index ea03488..2e6d5ed 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -13,9 +13,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use self::mat2::Mat2; -pub use self::mat3::Mat3; -pub use self::mat4::Mat4; +pub use self::mat2::{Mat2, ToMat2}; +pub use self::mat3::{Mat3, ToMat3}; +pub use self::mat4::{Mat4, ToMat4}; pub mod mat2; pub mod mat3; diff --git a/src/mat2.rs b/src/mat2.rs index c80b460..9b2fe09 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -18,7 +18,8 @@ use std::cmp::ApproxEq; use std::num::{Zero, One}; use vec::*; -use super::{Mat3, Mat4}; +use super::{Mat3, ToMat3}; +use super::{Mat4, ToMat4}; #[deriving(Eq)] pub struct Mat2 { @@ -26,6 +27,10 @@ pub struct Mat2 { y: Vec2, } +pub trait ToMat2 { + pub fn to_mat2(&self) -> Mat2; +} + impl Mat2 { /// Construct a 2 x 2 matrix /// @@ -265,7 +270,9 @@ impl Mat2 { pub fn to_zero(&mut self) { *self = Mat2::zero(); } +} +impl ToMat3 for Mat2 { /// Returns the the matrix with an extra row and column added /// ~~~ /// c0 c1 c0 c1 c2 @@ -283,7 +290,9 @@ impl Mat2 { *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), Zero::zero(), Zero::zero(), One::one()) } +} +impl ToMat4 for Mat2 { /// Returns the the matrix with an extra two rows and columns added /// ~~~ /// c0 c1 c0 c1 c2 c3 diff --git a/src/mat3.rs b/src/mat3.rs index f612ac2..839a2dc 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -18,8 +18,8 @@ use std::cmp::ApproxEq; use std::num::{Zero, One}; use vec::*; -use quat::Quat; -use super::Mat4; +use quat::{Quat, ToQuat}; +use super::{Mat4, ToMat4}; #[deriving(Eq)] pub struct Mat3 { @@ -28,6 +28,10 @@ pub struct Mat3 { z: Vec3, } +pub trait ToMat3 { + pub fn to_mat3(&self) -> Mat3; +} + impl Mat3 { /// Construct a 3 x 3 matrix /// @@ -320,7 +324,9 @@ impl Mat3 { pub fn to_zero(&mut self) { *self = Mat3::zero(); } +} +impl ToMat4 for Mat3 { /// Returns the the matrix with an extra row and column added /// ~~~ /// c0 c1 c2 c0 c1 c2 c3 @@ -432,7 +438,9 @@ impl Mat3 { Mat3::from_axes(up_, side, dir_) } +} +impl ToQuat for Mat3 { /// Convert the matrix to a quaternion pub fn to_quat(&self) -> Quat { // Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's diff --git a/src/mat4.rs b/src/mat4.rs index 49b032a..babb2b1 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -29,6 +29,10 @@ pub struct Mat4 { w: Vec4, } +pub trait ToMat4 { + pub fn to_mat4(&self) -> Mat4; +} + impl Mat4 { /// Construct a 4 x 4 matrix /// diff --git a/src/quat.rs b/src/quat.rs index ab229ec..88e29a0 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -17,7 +17,7 @@ use std::cast::transmute; use std::cmp::ApproxEq; use std::num::{Zero, One, cast}; -use mat::Mat3; +use mat::{Mat3, ToMat3}; use vec::Vec3; // GLSL-style type aliases @@ -44,6 +44,10 @@ pub type Quatf64 = Quat; #[deriving(Eq)] pub struct Quat { s: T, v: Vec3 } +pub trait ToQuat { + pub fn to_quat(&self) -> Quat; +} + impl Quat { /// Construct the quaternion from one scalar component and three /// imaginary components @@ -267,6 +271,17 @@ impl Quat { self.mul_t(One::one::() / self.magnitude()) } + /// Normalised linear interpolation + /// + /// # Return value + /// + /// The intoperlated quaternion + pub fn nlerp(&self, other: &Quat, amount: T) -> Quat { + self.mul_t(One::one::() - amount).add_q(&other.mul_t(amount)).normalize() + } +} + +impl ToMat3 for Quat { /// Convert the quaternion to a 3 x 3 rotation matrix pub fn to_mat3(&self) -> Mat3 { let x2 = self.v.x + self.v.x; @@ -291,15 +306,6 @@ impl Quat { xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2, xz2 + sy2, yz2 - sx2, _1 - xx2 - yy2) } - - /// Normalised linear interpolation - /// - /// # Return value - /// - /// The intoperlated quaternion - pub fn nlerp(&self, other: &Quat, amount: T) -> Quat { - self.mul_t(One::one::() - amount).add_q(&other.mul_t(amount)).normalize() - } } impl Neg> for Quat {