Add angle module

This commit is contained in:
Brendan Zabarauskas 2012-11-26 02:26:01 +10:00
parent 10f2b9b0b1
commit 287fae7114
5 changed files with 43 additions and 27 deletions

34
src/angle.rs Normal file
View file

@ -0,0 +1,34 @@
use num::cast::*;
use vec::Vec3;
pub enum Angle<T> {
degrees(T),
radians(T),
}
pub impl<T:Copy Num NumCast> Angle<T> {
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<T> {
axis: Vec3<T>,
theta: Angle<T>,
}
pub struct Euler<T> {
x: T, // pitch
y: T, // yaw
z: T, // roll
}

View file

@ -1,21 +0,0 @@
// TODO
pub trait EulerAngles<T> {
// to_mat3
// to_mat4
// to_quat
}
pub struct Euler<T> { x: T, y: T, z: T } // pitch / yaw / roll
pub mod Euler {
#[inline(always)]
pub pure fn new<T>(x: T, y: T, z: T) -> Euler<T> {
Euler { x: move x, y: move y, z: move z }
}
// from_mat3
// from_quat
}
pub impl<T> Euler<T>: EulerAngles {}

View file

@ -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);

View file

@ -1,9 +1,10 @@
use funs::trig::*;
use angle::Angle;
use mat::{Mat3, Mat4};
use num::cast::*;
pub pure fn mat3_from_rotation<T:Copy Num NumCast AngleConv Trig>(theta: T, axis: Vec3<T>) -> Mat3<T> {
let rad = radians(&theta);
pub pure fn mat3_from_rotation<T:Copy Num NumCast Trig>(theta: Angle<T>, axis: Vec3<T>) -> Mat3<T> {
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<T:Copy Num NumCast AngleConv Trig>(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<T:Copy Num NumCast AngleConv Trig>(theta: T, axis: Vec3<T>) -> Mat4<T> {
pub pure fn mat4_from_rotation<T:Copy Num NumCast Trig>(theta: Angle<T>, axis: Vec3<T>) -> Mat4<T> {
mat3_from_rotation(theta, axis).to_mat4()
}

View file

@ -10,6 +10,7 @@
extern mod std;
pub mod angle;
pub mod dim;
pub mod gltypes;
pub mod mat;