From 287fae71142246eda9ae3b4420df7552ce9b6b87 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 26 Nov 2012 02:26:01 +1000 Subject: [PATCH] Add angle module --- src/angle.rs | 34 +++++++++++++++++++++++++++++++++ src/euler.rs | 21 -------------------- src/funs/test/test_transform.rs | 7 ++++--- src/funs/transform.rs | 7 ++++--- src/lmath.rc | 1 + 5 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 src/angle.rs delete mode 100644 src/euler.rs diff --git a/src/angle.rs b/src/angle.rs new file mode 100644 index 0000000..2353c58 --- /dev/null +++ b/src/angle.rs @@ -0,0 +1,34 @@ +use num::cast::*; +use vec::Vec3; + +pub enum Angle { + degrees(T), + radians(T), +} + +pub impl Angle { + pure fn degrees() -> T { + match self { + degrees(theta) => theta, + radians(theta) => theta * cast(180f64 / f64::consts::pi) + } + } + + pure fn radians() -> T { + match self { + degrees(theta) => theta * cast(f64::consts::pi / 180f64), + radians(theta) => theta + } + } +} + +pub struct AxisRotation { + axis: Vec3, + theta: Angle, +} + +pub struct Euler { + x: T, // pitch + y: T, // yaw + z: T, // roll +} \ No newline at end of file diff --git a/src/euler.rs b/src/euler.rs deleted file mode 100644 index d2b4fd5..0000000 --- a/src/euler.rs +++ /dev/null @@ -1,21 +0,0 @@ -// TODO - -pub trait EulerAngles { - // to_mat3 - // to_mat4 - // to_quat -} - -pub struct Euler { x: T, y: T, z: T } // pitch / yaw / roll - -pub mod Euler { - #[inline(always)] - pub pure fn new(x: T, y: T, z: T) -> Euler { - Euler { x: move x, y: move y, z: move z } - } - - // from_mat3 - // from_quat -} - -pub impl Euler: EulerAngles {} \ No newline at end of file diff --git a/src/funs/test/test_transform.rs b/src/funs/test/test_transform.rs index d12e325..9b0b9e1 100644 --- a/src/funs/test/test_transform.rs +++ b/src/funs/test/test_transform.rs @@ -1,4 +1,5 @@ use funs::transform::*; +use angle::degrees; use mat::Mat4; use vec::{Vec3, Vec4}; @@ -7,7 +8,7 @@ fn test_mat4_from_rotation() { { let pos = Vec4::new(1f32, 0f32, 0f32, 1f32); // let tform = mat4_from_rotation(180f32, Vec3::unit_z()); - let tform = mat4_from_rotation(180f32, Vec3::new(0f32, 0f32, 1f32)); + let tform = mat4_from_rotation(degrees(180f32), Vec3::new(0f32, 0f32, 1f32)); let newpos = tform.mul_v(&pos); let expected = Vec4::new(-1f32, 0f32, 0f32, 1f32); @@ -19,8 +20,8 @@ fn test_mat4_from_rotation() { // let tform_a = mat4_from_rotation(90f32, Vec3::unit_y()); // let tform_b = mat4_from_rotation(90f32, -Vec3::unit_y()); - let tform_a = mat4_from_rotation(90f32, Vec3::new(0f32, 1f32, 0f32)); - let tform_b = mat4_from_rotation(90f32, -Vec3::new(0f32, 1f32, 0f32)); + let tform_a = mat4_from_rotation(degrees(90f32), Vec3::new(0f32, 1f32, 0f32)); + let tform_b = mat4_from_rotation(degrees(90f32), -Vec3::new(0f32, 1f32, 0f32)); let newpos_a = tform_a.mul_v(&pos); let newpos_b = tform_b.mul_v(&pos); diff --git a/src/funs/transform.rs b/src/funs/transform.rs index cce8efb..3520371 100644 --- a/src/funs/transform.rs +++ b/src/funs/transform.rs @@ -1,9 +1,10 @@ use funs::trig::*; +use angle::Angle; use mat::{Mat3, Mat4}; use num::cast::*; -pub pure fn mat3_from_rotation(theta: T, axis: Vec3) -> Mat3 { - let rad = radians(&theta); +pub pure fn mat3_from_rotation(theta: Angle, axis: Vec3) -> Mat3 { + let rad = theta.radians(); let c: T = cos(&rad); let s: T = sin(&rad); let _0: T = cast(0); @@ -15,6 +16,6 @@ pub pure fn mat3_from_rotation(theta: T, axis t * axis.x * axis.z - s - axis.y, t * axis.y * axis.z - s * axis.x, t * axis.z * axis.z + c) } -pub pure fn mat4_from_rotation(theta: T, axis: Vec3) -> Mat4 { +pub pure fn mat4_from_rotation(theta: Angle, axis: Vec3) -> Mat4 { mat3_from_rotation(theta, axis).to_mat4() } \ No newline at end of file diff --git a/src/lmath.rc b/src/lmath.rc index 7d36b62..b264e6b 100644 --- a/src/lmath.rc +++ b/src/lmath.rc @@ -10,6 +10,7 @@ extern mod std; +pub mod angle; pub mod dim; pub mod gltypes; pub mod mat;