Use Angle type with triganomic functions

This commit is contained in:
Brendan Zabarauskas 2012-11-26 03:44:24 +10:00
parent 035d9e751a
commit eda42604b7
4 changed files with 253 additions and 290 deletions

View file

@ -1,4 +1,5 @@
use funs::trig::*; use funs::trig::*;
use angle::Angle;
use mat::Mat4; use mat::Mat4;
use num::cast::cast; use num::cast::cast;
use num::consts::pi; use num::consts::pi;
@ -11,8 +12,8 @@ use num::ext::FloatExt;
// http://www.opengl.org/wiki/GluPerspective_code // http://www.opengl.org/wiki/GluPerspective_code
// //
#[inline(always)] #[inline(always)]
pure fn perspective<T:Copy FloatExt Trig>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> { pure fn perspective<T:Copy FloatExt>(fovy: Angle<T>, aspectRatio: T, near: T, far: T) -> Mat4<T> {
let ymax = near * tan(&(fovy * pi() / cast(360))); let ymax = near * tan(&fovy);
let xmax = ymax * aspectRatio; let xmax = ymax * aspectRatio;
return frustum(-xmax, xmax, -ymax, ymax, near, far); return frustum(-xmax, xmax, -ymax, ymax, near, far);
} }

View file

@ -3,10 +3,9 @@ use angle::Angle;
use mat::{Mat3, Mat4}; use mat::{Mat3, Mat4};
use num::cast::*; use num::cast::*;
pub pure fn mat3_from_rotation<T:Copy Num NumCast Trig>(theta: Angle<T>, axis: Vec3<T>) -> Mat3<T> { pub pure fn mat3_from_rotation<T:Copy Num NumCast>(theta: Angle<T>, axis: Vec3<T>) -> Mat3<T> {
let rad = theta.radians(); let c: T = cos(&theta);
let c: T = cos(&rad); let s: T = sin(&theta);
let s: T = sin(&rad);
let _0: T = cast(0); let _0: T = cast(0);
let _1: T = cast(1); let _1: T = cast(1);
let t: T = _1 - c; let t: T = _1 - c;
@ -16,6 +15,6 @@ pub pure fn mat3_from_rotation<T:Copy Num NumCast Trig>(theta: Angle<T>, axis: V
t * axis.x * axis.z - s - axis.y, t * axis.y * axis.z - s * axis.x, t * axis.z * axis.z + c) 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 Trig>(theta: Angle<T>, axis: Vec3<T>) -> Mat4<T> { pub pure fn mat4_from_rotation<T:Copy Num NumCast>(theta: Angle<T>, axis: Vec3<T>) -> Mat4<T> {
mat3_from_rotation(theta, axis).to_mat4() mat3_from_rotation(theta, axis).to_mat4()
} }

View file

@ -1,327 +1,290 @@
use num::cast::*; use num::cast::*;
use angle::*;
use vec::{Vec3, Vec2, Vec4}; use vec::{Vec3, Vec2, Vec4};
pub trait AngleConv { ///
pure fn to_degrees() -> self; /// Triganomic functions
pure fn to_radians() -> self; ///
/// http://en.wikipedia.org/wiki/Trigonometric_functions
///
priv trait Trig<T> {
pure fn sin() -> T;
pure fn cos() -> T;
pure fn tan() -> T;
} }
#[inline(always)] pub pure fn degrees<T:AngleConv>(theta: &T) -> T { theta.to_degrees() } #[inline(always)] pub pure fn sin<T:Trig<R>, R>(theta: &T) -> R { theta.sin() }
#[inline(always)] pub pure fn radians<T:AngleConv>(theta: &T) -> T { theta.to_radians() } #[inline(always)] pub pure fn cos<T:Trig<R>, R>(theta: &T) -> R { theta.cos() }
#[inline(always)] pub pure fn tan<T:Trig<R>, R>(theta: &T) -> R { theta.tan() }
pub impl f32: AngleConv { priv impl<T:Copy Num NumCast> Angle<T>: Trig<T> {
#[inline(always)] pure fn to_degrees() -> f32 { self * (180f32 / f32::consts::pi) } #[inline(always)] pure fn sin() -> T { cast(f64::sin(cast(self.radians()))) }
#[inline(always)] pure fn to_radians() -> f32 { self * (f32::consts::pi / 180f32) } #[inline(always)] pure fn cos() -> T { cast(f64::cos(cast(self.radians()))) }
#[inline(always)] pure fn tan() -> T { cast(f64::tan(cast(self.radians()))) }
} }
pub impl f64: AngleConv { ///
#[inline(always)] pure fn to_degrees() -> f64 { self * (180f64 / f64::consts::pi) } /// Inverse triganomic functions
#[inline(always)] pure fn to_radians() -> f64 { self * (f64::consts::pi / 180f64) } ///
/// http://en.wikipedia.org/wiki/Inverse_trigonometric_functions
///
pub trait InvTrig {
pure fn asin() -> Angle<self>;
pure fn acos() -> Angle<self>;
pure fn atan() -> Angle<self>;
} }
pub impl float: AngleConv { #[inline(always)] pub pure fn asin<T:InvTrig>(x: &T) -> Angle<T> { x.asin() }
#[inline(always)] pure fn to_degrees() -> float { self * (180f / float::consts::pi) } #[inline(always)] pub pure fn acos<T:InvTrig>(x: &T) -> Angle<T> { x.acos() }
#[inline(always)] pure fn to_radians() -> float { self * (float::consts::pi / 180f) } #[inline(always)] pub pure fn atan<T:InvTrig>(x: &T) -> Angle<T> { x.atan() }
pub impl f32: InvTrig {
#[inline(always)] pure fn asin() -> Angle<f32> { radians(f32::asin(self)) }
#[inline(always)] pure fn acos() -> Angle<f32> { radians(f32::acos(self)) }
#[inline(always)] pure fn atan() -> Angle<f32> { radians(f32::atan(self)) }
} }
pub impl<T:Copy AngleConv> Vec2<T>: AngleConv { pub impl f64: InvTrig {
#[inline(always)] #[inline(always)] pure fn asin() -> Angle<f64> { radians(f64::asin(self)) }
pure fn to_degrees() -> Vec2<T> { #[inline(always)] pure fn acos() -> Angle<f64> { radians(f64::acos(self)) }
Vec2::new(degrees(&self[0]), #[inline(always)] pure fn atan() -> Angle<f64> { radians(f64::atan(self)) }
degrees(&self[1]))
} }
#[inline(always)] pub impl float: InvTrig {
pure fn to_radians() -> Vec2<T> { #[inline(always)] pure fn asin() -> Angle<float> { radians(f64::asin(cast(self)).to_float()) }
Vec2::new(radians(&self[0]), #[inline(always)] pure fn acos() -> Angle<float> { radians(f64::acos(cast(self)).to_float()) }
radians(&self[1])) #[inline(always)] pure fn atan() -> Angle<float> { radians(f64::atan(cast(self)).to_float()) }
}
} }
pub impl<T:Copy AngleConv> Vec3<T>: AngleConv { ///
#[inline(always)] /// Hyperbolic functions
pure fn to_degrees() -> Vec3<T> { ///
Vec3::new(degrees(&self[0]), /// http://en.wikipedia.org/wiki/Hyperbolic_function
degrees(&self[1]), ///
degrees(&self[2])) pub trait Hyp {
}
#[inline(always)]
pure fn to_radians() -> Vec3<T> {
Vec3::new(radians(&self[0]),
radians(&self[1]),
radians(&self[2]))
}
}
pub impl<T:Copy AngleConv> Vec4<T>: AngleConv {
#[inline(always)]
pure fn to_degrees() -> Vec4<T> {
Vec4::new(degrees(&self[0]),
degrees(&self[1]),
degrees(&self[2]),
degrees(&self[3]))
}
#[inline(always)]
pure fn to_radians() -> Vec4<T> {
Vec4::new(radians(&self[0]),
radians(&self[1]),
radians(&self[2]),
radians(&self[3]))
}
}
pub trait Trig {
pure fn sin() -> self;
pure fn cos() -> self;
pure fn tan() -> self;
pure fn asin() -> self;
pure fn acos() -> self;
pure fn atan() -> self;
pure fn sinh() -> self; pure fn sinh() -> self;
pure fn cosh() -> self; pure fn cosh() -> self;
pure fn tanh() -> self; pure fn tanh() -> self;
// pure fn asinh() -> self;
// pure fn acosh() -> self;
// pure fn atanh() -> self;
} }
#[inline(always)] pub pure fn sin<T:Trig>(theta: &T) -> T { theta.sin() } #[inline(always)] pub pure fn sinh<T:Hyp>(x: &T) -> T { x.sinh() }
#[inline(always)] pub pure fn cos<T:Trig>(theta: &T) -> T { theta.cos() } #[inline(always)] pub pure fn cosh<T:Hyp>(x: &T) -> T { x.cosh() }
#[inline(always)] pub pure fn tan<T:Trig>(theta: &T) -> T { theta.tan() } #[inline(always)] pub pure fn tanh<T:Hyp>(x: &T) -> T { x.tanh() }
#[inline(always)] pub pure fn asin<T:Trig>(x: &T) -> T { x.asin() }
#[inline(always)] pub pure fn acos<T:Trig>(x: &T) -> T { x.acos() }
#[inline(always)] pub pure fn atan<T:Trig>(x: &T) -> T { x.atan() }
#[inline(always)] pub pure fn sinh<T:Trig>(x: &T) -> T { x.sinh() }
#[inline(always)] pub pure fn cosh<T:Trig>(x: &T) -> T { x.cosh() }
#[inline(always)] pub pure fn tanh<T:Trig>(x: &T) -> T { x.tanh() }
pub impl f32: Trig { pub impl f32: Hyp {
#[inline(always)] pure fn sin() -> f32 { f32::sin (self) }
#[inline(always)] pure fn cos() -> f32 { f32::cos (self) }
#[inline(always)] pure fn tan() -> f32 { f32::tan (self) }
#[inline(always)] pure fn asin() -> f32 { f32::asin(self) }
#[inline(always)] pure fn acos() -> f32 { f32::acos(self) }
#[inline(always)] pure fn atan() -> f32 { f32::atan(self) }
#[inline(always)] pure fn sinh() -> f32 { f32::sinh(self) } #[inline(always)] pure fn sinh() -> f32 { f32::sinh(self) }
#[inline(always)] pure fn cosh() -> f32 { f32::cosh(self) } #[inline(always)] pure fn cosh() -> f32 { f32::cosh(self) }
#[inline(always)] pure fn tanh() -> f32 { f32::tanh(self) } #[inline(always)] pure fn tanh() -> f32 { f32::tanh(self) }
} }
pub impl f64: Trig { pub impl f64: Hyp {
#[inline(always)] pure fn sin() -> f64 { f64::sin (self) }
#[inline(always)] pure fn cos() -> f64 { f64::cos (self) }
#[inline(always)] pure fn tan() -> f64 { f64::tan (self) }
#[inline(always)] pure fn asin() -> f64 { f64::asin(self) }
#[inline(always)] pure fn acos() -> f64 { f64::acos(self) }
#[inline(always)] pure fn atan() -> f64 { f64::atan(self) }
#[inline(always)] pure fn sinh() -> f64 { f64::sinh(self) } #[inline(always)] pure fn sinh() -> f64 { f64::sinh(self) }
#[inline(always)] pure fn cosh() -> f64 { f64::cosh(self) } #[inline(always)] pure fn cosh() -> f64 { f64::cosh(self) }
#[inline(always)] pure fn tanh() -> f64 { f64::tanh(self) } #[inline(always)] pure fn tanh() -> f64 { f64::tanh(self) }
} }
pub impl float: Trig { pub impl float: Hyp {
#[inline(always)] pure fn sin() -> float { cast(f64::sin (cast(self))) }
#[inline(always)] pure fn cos() -> float { cast(f64::cos (cast(self))) }
#[inline(always)] pure fn tan() -> float { cast(f64::tan (cast(self))) }
#[inline(always)] pure fn asin() -> float { cast(f64::asin(cast(self))) }
#[inline(always)] pure fn acos() -> float { cast(f64::acos(cast(self))) }
#[inline(always)] pure fn atan() -> float { cast(f64::atan(cast(self))) }
#[inline(always)] pure fn sinh() -> float { cast(f64::sinh(cast(self))) } #[inline(always)] pure fn sinh() -> float { cast(f64::sinh(cast(self))) }
#[inline(always)] pure fn cosh() -> float { cast(f64::cosh(cast(self))) } #[inline(always)] pure fn cosh() -> float { cast(f64::cosh(cast(self))) }
#[inline(always)] pure fn tanh() -> float { cast(f64::tanh(cast(self))) } #[inline(always)] pure fn tanh() -> float { cast(f64::tanh(cast(self))) }
} }
pub impl <T:Copy Trig> Vec2<T>: Trig {
#[inline(always)]
pure fn sin() -> Vec2<T> {
Vec2::new(sin(&self[0]),
sin(&self[1]))
}
#[inline(always)]
pure fn cos() -> Vec2<T> {
Vec2::new(cos(&self[0]),
cos(&self[1]))
}
#[inline(always)] // pub impl <T:Copy Trig> Vec2<T>: Trig {
pure fn tan() -> Vec2<T> { // #[inline(always)]
Vec2::new(tan(&self[0]), // pure fn sin() -> Vec2<T> {
tan(&self[1])) // Vec2::new(sin(&self[0]),
} // sin(&self[1]))
// }
#[inline(always)] // #[inline(always)]
pure fn asin() -> Vec2<T> { // pure fn cos() -> Vec2<T> {
Vec2::new(asin(&self[0]), // Vec2::new(cos(&self[0]),
asin(&self[1])) // cos(&self[1]))
} // }
#[inline(always)] // #[inline(always)]
pure fn acos() -> Vec2<T> { // pure fn tan() -> Vec2<T> {
Vec2::new(acos(&self[0]), // Vec2::new(tan(&self[0]),
acos(&self[1])) // tan(&self[1]))
} // }
#[inline(always)] // #[inline(always)]
pure fn atan() -> Vec2<T> { // pure fn asin() -> Vec2<T> {
Vec2::new(atan(&self[0]), // Vec2::new(asin(&self[0]),
atan(&self[1])) // asin(&self[1]))
} // }
#[inline(always)] // #[inline(always)]
pure fn sinh() -> Vec2<T> { // pure fn acos() -> Vec2<T> {
Vec2::new(sinh(&self[0]), // Vec2::new(acos(&self[0]),
sinh(&self[1])) // acos(&self[1]))
} // }
#[inline(always)] // #[inline(always)]
pure fn cosh() -> Vec2<T> { // pure fn atan() -> Vec2<T> {
Vec2::new(cosh(&self[0]), // Vec2::new(atan(&self[0]),
cosh(&self[1])) // atan(&self[1]))
} // }
#[inline(always)] // #[inline(always)]
pure fn tanh() -> Vec2<T> { // pure fn sinh() -> Vec2<T> {
Vec2::new(tanh(&self[0]), // Vec2::new(sinh(&self[0]),
tanh(&self[1])) // sinh(&self[1]))
} // }
}
pub impl <T:Copy Trig> Vec3<T>: Trig { // #[inline(always)]
#[inline(always)] // pure fn cosh() -> Vec2<T> {
pure fn sin() -> Vec3<T> { // Vec2::new(cosh(&self[0]),
Vec3::new(sin(&self[0]), // cosh(&self[1]))
sin(&self[1]), // }
sin(&self[2]))
}
#[inline(always)] // #[inline(always)]
pure fn cos() -> Vec3<T> { // pure fn tanh() -> Vec2<T> {
Vec3::new(cos(&self[0]), // Vec2::new(tanh(&self[0]),
cos(&self[1]), // tanh(&self[1]))
cos(&self[2])) // }
} // }
#[inline(always)] // pub impl <T:Copy Trig> Vec3<T>: Trig {
pure fn tan() -> Vec3<T> { // #[inline(always)]
Vec3::new(tan(&self[0]), // pure fn sin() -> Vec3<T> {
tan(&self[1]), // Vec3::new(sin(&self[0]),
tan(&self[2])) // sin(&self[1]),
} // sin(&self[2]))
// }
#[inline(always)] // #[inline(always)]
pure fn asin() -> Vec3<T> { // pure fn cos() -> Vec3<T> {
Vec3::new(asin(&self[0]), // Vec3::new(cos(&self[0]),
asin(&self[1]), // cos(&self[1]),
asin(&self[2])) // cos(&self[2]))
} // }
#[inline(always)] // #[inline(always)]
pure fn acos() -> Vec3<T> { // pure fn tan() -> Vec3<T> {
Vec3::new(acos(&self[0]), // Vec3::new(tan(&self[0]),
acos(&self[1]), // tan(&self[1]),
acos(&self[2])) // tan(&self[2]))
} // }
#[inline(always)] // #[inline(always)]
pure fn atan() -> Vec3<T> { // pure fn asin() -> Vec3<T> {
Vec3::new(atan(&self[0]), // Vec3::new(asin(&self[0]),
atan(&self[1]), // asin(&self[1]),
atan(&self[2])) // asin(&self[2]))
} // }
#[inline(always)] // #[inline(always)]
pure fn sinh() -> Vec3<T> { // pure fn acos() -> Vec3<T> {
Vec3::new(sinh(&self[0]), // Vec3::new(acos(&self[0]),
sinh(&self[1]), // acos(&self[1]),
sinh(&self[2])) // acos(&self[2]))
} // }
#[inline(always)] // #[inline(always)]
pure fn cosh() -> Vec3<T> { // pure fn atan() -> Vec3<T> {
Vec3::new(cosh(&self[0]), // Vec3::new(atan(&self[0]),
cosh(&self[1]), // atan(&self[1]),
cosh(&self[2])) // atan(&self[2]))
} // }
#[inline(always)] // #[inline(always)]
pure fn tanh() -> Vec3<T> { // pure fn sinh() -> Vec3<T> {
Vec3::new(tanh(&self[0]), // Vec3::new(sinh(&self[0]),
tanh(&self[1]), // sinh(&self[1]),
tanh(&self[2])) // sinh(&self[2]))
} // }
}
pub impl <T:Copy Trig> Vec4<T>: Trig { // #[inline(always)]
#[inline(always)] // pure fn cosh() -> Vec3<T> {
pure fn sin() -> Vec4<T> { // Vec3::new(cosh(&self[0]),
Vec4::new(sin(&self[0]), // cosh(&self[1]),
sin(&self[1]), // cosh(&self[2]))
sin(&self[2]), // }
sin(&self[3]))
}
#[inline(always)] // #[inline(always)]
pure fn cos() -> Vec4<T> { // pure fn tanh() -> Vec3<T> {
Vec4::new(cos(&self[0]), // Vec3::new(tanh(&self[0]),
cos(&self[1]), // tanh(&self[1]),
cos(&self[2]), // tanh(&self[2]))
cos(&self[3])) // }
} // }
#[inline(always)] // pub impl <T:Copy Trig> Vec4<T>: Trig {
pure fn tan() -> Vec4<T> { // #[inline(always)]
Vec4::new(tan(&self[0]), // pure fn sin() -> Vec4<T> {
tan(&self[1]), // Vec4::new(sin(&self[0]),
tan(&self[2]), // sin(&self[1]),
tan(&self[3])) // sin(&self[2]),
} // sin(&self[3]))
// }
#[inline(always)] // #[inline(always)]
pure fn asin() -> Vec4<T> { // pure fn cos() -> Vec4<T> {
Vec4::new(asin(&self[0]), // Vec4::new(cos(&self[0]),
asin(&self[1]), // cos(&self[1]),
asin(&self[2]), // cos(&self[2]),
asin(&self[3])) // cos(&self[3]))
} // }
#[inline(always)] // #[inline(always)]
pure fn acos() -> Vec4<T> { // pure fn tan() -> Vec4<T> {
Vec4::new(acos(&self[0]), // Vec4::new(tan(&self[0]),
acos(&self[1]), // tan(&self[1]),
acos(&self[2]), // tan(&self[2]),
acos(&self[3])) // tan(&self[3]))
} // }
#[inline(always)] // #[inline(always)]
pure fn atan() -> Vec4<T> { // pure fn asin() -> Vec4<T> {
Vec4::new(atan(&self[0]), // Vec4::new(asin(&self[0]),
atan(&self[1]), // asin(&self[1]),
atan(&self[2]), // asin(&self[2]),
atan(&self[3])) // asin(&self[3]))
} // }
#[inline(always)] // #[inline(always)]
pure fn sinh() -> Vec4<T> { // pure fn acos() -> Vec4<T> {
Vec4::new(sinh(&self[0]), // Vec4::new(acos(&self[0]),
sinh(&self[1]), // acos(&self[1]),
sinh(&self[2]), // acos(&self[2]),
sinh(&self[3])) // acos(&self[3]))
} // }
#[inline(always)] // #[inline(always)]
pure fn cosh() -> Vec4<T> { // pure fn atan() -> Vec4<T> {
Vec4::new(cosh(&self[0]), // Vec4::new(atan(&self[0]),
cosh(&self[1]), // atan(&self[1]),
cosh(&self[2]), // atan(&self[2]),
cosh(&self[3])) // atan(&self[3]))
} // }
#[inline(always)] // #[inline(always)]
pure fn tanh() -> Vec4<T> { // pure fn sinh() -> Vec4<T> {
Vec4::new(tanh(&self[0]), // Vec4::new(sinh(&self[0]),
tanh(&self[1]), // sinh(&self[1]),
tanh(&self[2]), // sinh(&self[2]),
tanh(&self[3])) // sinh(&self[3]))
} // }
}
// #[inline(always)]
// pure fn cosh() -> Vec4<T> {
// Vec4::new(cosh(&self[0]),
// cosh(&self[1]),
// cosh(&self[2]),
// cosh(&self[3]))
// }
// #[inline(always)]
// pure fn tanh() -> Vec4<T> {
// Vec4::new(tanh(&self[0]),
// tanh(&self[1]),
// tanh(&self[2]),
// tanh(&self[3]))
// }
// }

View file

@ -70,7 +70,7 @@ pub impl<T> Quat<T> {
} }
} }
pub impl<T:Copy Num NumCast Trig Exp Clamp Ord AngleConv> Quat<T>: Quaternion<T> { pub impl<T:Copy Num NumCast Exp Clamp Ord InvTrig> Quat<T>: Quaternion<T> {
#[inline(always)] #[inline(always)]
static pure fn identity() -> Quat<T> { static pure fn identity() -> Quat<T> {
Quat::new(NumCast::one(), Quat::new(NumCast::one(),
@ -222,8 +222,8 @@ pub impl<T:Copy Num NumCast Trig Exp Clamp Ord AngleConv> Quat<T>: Quaternion<T>
} }
#[inline(always)] #[inline(always)]
pub pure fn from_axis_angle(axis: Vec3<T>, theta: T) -> Quat<T> { pub pure fn from_axis_angle(axis: Vec3<T>, theta: Angle<T>) -> Quat<T> {
let half = radians(&theta) / cast(2); let half = theta / cast(2);
Quat::from_sv(cos(&half), axis.mul_t(sin(&half))) Quat::from_sv(cos(&half), axis.mul_t(sin(&half)))
} }