Force the client to perform Degree->Rad conversions by taking Rad parameters instead of generic Angles

This should make the overhead of conversions evident to the client.
This commit is contained in:
Brendan Zabarauskas 2013-10-13 10:52:21 +11:00
parent 3862e867ba
commit 70d48ed37b
5 changed files with 30 additions and 30 deletions

View file

@ -166,19 +166,19 @@ impl<S: Float> Angle<S> for Deg<S> {
#[inline] fn full_turn() -> Deg<S> { deg(cast(360).unwrap()) } #[inline] fn full_turn() -> Deg<S> { deg(cast(360).unwrap()) }
} }
#[inline] pub fn sin<S: Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.sin() } #[inline] pub fn sin<S: Float>(theta: Rad<S>) -> S { theta.s.sin() }
#[inline] pub fn cos<S: Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.cos() } #[inline] pub fn cos<S: Float>(theta: Rad<S>) -> S { theta.s.cos() }
#[inline] pub fn tan<S: Float, A: Angle<S>>(theta: A) -> S { theta.to_rad().s.tan() } #[inline] pub fn tan<S: Float>(theta: Rad<S>) -> S { theta.s.tan() }
#[inline] pub fn sin_cos<S: Float, A: Angle<S>>(theta: A) -> (S, S) { theta.to_rad().s.sin_cos() } #[inline] pub fn sin_cos<S: Float>(theta: Rad<S>) -> (S, S) { theta.s.sin_cos() }
#[inline] pub fn cot<S: Float, A: Angle<S>>(theta: A) -> S { tan(theta).recip() } #[inline] pub fn cot<S: Float>(theta: Rad<S>) -> S { tan(theta).recip() }
#[inline] pub fn sec<S: Float, A: Angle<S>>(theta: A) -> S { cos(theta).recip() } #[inline] pub fn sec<S: Float>(theta: Rad<S>) -> S { cos(theta).recip() }
#[inline] pub fn csc<S: Float, A: Angle<S>>(theta: A) -> S { sin(theta).recip() } #[inline] pub fn csc<S: Float>(theta: Rad<S>) -> S { sin(theta).recip() }
#[inline] pub fn asin<S: Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.asin())) } #[inline] pub fn asin<S: Float>(s: S) -> Rad<S> { rad(s.asin()) }
#[inline] pub fn acos<S: Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.acos())) } #[inline] pub fn acos<S: Float>(s: S) -> Rad<S> { rad(s.acos()) }
#[inline] pub fn atan<S: Float, A: Angle<S>>(s: S) -> A { Angle::from(rad(s.atan())) } #[inline] pub fn atan<S: Float>(s: S) -> Rad<S> { rad(s.atan()) }
#[inline] pub fn atan2<S: Float, A: Angle<S>>(a: S, b: S) -> A { Angle::from(rad(a.atan2(&b))) } #[inline] pub fn atan2<S: Float>(a: S, b: S) -> Rad<S> { rad(a.atan2(&b)) }
impl<S: Float> ToStr for Rad<S> { fn to_str(&self) -> ~str { fmt!("%? rad", self.s) } } impl<S: Float> ToStr for Rad<S> { fn to_str(&self) -> ~str { fmt!("%? rad", self.s) } }
impl<S: Float> ToStr for Deg<S> { fn to_str(&self) -> ~str { fmt!("%?°", self.s) } } impl<S: Float> ToStr for Deg<S> { fn to_str(&self) -> ~str { fmt!("%?°", self.s) } }

View file

@ -17,7 +17,7 @@
use std::num::{Zero, zero, One, one, cast, sqrt}; use std::num::{Zero, zero, One, one, cast, sqrt};
use angle::{Angle, Rad, sin, cos, sin_cos}; use angle::{Rad, sin, cos, sin_cos};
use array::{Array, build}; use array::{Array, build};
use quaternion::{Quat, ToQuat}; use quaternion::{Quat, ToQuat};
use vector::{Vector, EuclideanVector}; use vector::{Vector, EuclideanVector};
@ -123,7 +123,7 @@ impl<S: Float> Mat3<S> {
} }
/// Create a matrix from a rotation around the `x` axis (pitch). /// Create a matrix from a rotation around the `x` axis (pitch).
pub fn from_angle_x<A: Angle<S>>(theta: A) -> Mat3<S> { pub fn from_angle_x(theta: Rad<S>) -> Mat3<S> {
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
let (s, c) = sin_cos(theta); let (s, c) = sin_cos(theta);
Mat3::new(one(), zero(), zero(), Mat3::new(one(), zero(), zero(),
@ -132,7 +132,7 @@ impl<S: Float> Mat3<S> {
} }
/// Create a matrix from a rotation around the `y` axis (yaw). /// Create a matrix from a rotation around the `y` axis (yaw).
pub fn from_angle_y<A: Angle<S>>(theta: A) -> Mat3<S> { pub fn from_angle_y(theta: Rad<S>) -> Mat3<S> {
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
let (s, c) = sin_cos(theta); let (s, c) = sin_cos(theta);
Mat3::new(c.clone(), zero(), -s.clone(), Mat3::new(c.clone(), zero(), -s.clone(),
@ -141,7 +141,7 @@ impl<S: Float> Mat3<S> {
} }
/// Create a matrix from a rotation around the `z` axis (roll). /// Create a matrix from a rotation around the `z` axis (roll).
pub fn from_angle_z<A: Angle<S>>(theta: A) -> Mat3<S> { pub fn from_angle_z(theta: Rad<S>) -> Mat3<S> {
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
let (s, c) = sin_cos(theta); let (s, c) = sin_cos(theta);
Mat3::new(c.clone(), s.clone(), zero(), Mat3::new(c.clone(), s.clone(), zero(),
@ -156,7 +156,7 @@ impl<S: Float> Mat3<S> {
/// - `x`: the angular rotation around the `x` axis (pitch). /// - `x`: the angular rotation around the `x` axis (pitch).
/// - `y`: the angular rotation around the `y` axis (yaw). /// - `y`: the angular rotation around the `y` axis (yaw).
/// - `z`: the angular rotation around the `z` axis (roll). /// - `z`: the angular rotation around the `z` axis (roll).
pub fn from_euler<A: Angle<S>>(x: A, y: A, z: A) -> Mat3<S> { pub fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Mat3<S> {
// http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations // http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations
let (sx, cx) = sin_cos(x); let (sx, cx) = sin_cos(x);
let (sy, cy) = sin_cos(y); let (sy, cy) = sin_cos(y);
@ -168,7 +168,7 @@ impl<S: Float> Mat3<S> {
} }
/// Create a matrix from a rotation around an arbitrary axis /// Create a matrix from a rotation around an arbitrary axis
pub fn from_axis_angle<A: Angle<S>>(axis: &Vec3<S>, angle: A) -> Mat3<S> { pub fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Mat3<S> {
let (s, c) = sin_cos(angle); let (s, c) = sin_cos(angle);
let _1subc = one::<S>() - c; let _1subc = one::<S>() - c;

View file

@ -79,7 +79,7 @@ pub struct PerspectiveFov<S, A> {
impl<S: Float, A: Angle<S>> PerspectiveFov<S, A> { impl<S: Float, A: Angle<S>> PerspectiveFov<S, A> {
pub fn to_perspective(&self) -> Perspective<S> { pub fn to_perspective(&self) -> Perspective<S> {
let angle = self.fovy.div_s(cast(2).unwrap()); let angle = self.fovy.div_s(cast(2).unwrap());
let ymax = self.near * tan(angle); let ymax = self.near * tan(angle.to_rad());
let xmax = ymax * self.aspect; let xmax = ymax * self.aspect;
Perspective { Perspective {
@ -111,7 +111,7 @@ impl<S: Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> {
assert!(self.far > zero(), "The far plane distance cannot be below zero, found: {:?}", self.far); assert!(self.far > zero(), "The far plane distance cannot be below zero, found: {:?}", self.far);
assert!(self.far > self.near, "The far plane cannot be closer than the near plane, found: far: {:?}, near: {:?}", self.far, self.near); assert!(self.far > self.near, "The far plane cannot be closer than the near plane, found: far: {:?}, near: {:?}", self.far, self.near);
let f = cot(self.fovy.div_s(cast(2).unwrap())); let f = cot(self.fovy.div_s(cast(2).unwrap()).to_rad());
let two: S = cast(2).unwrap(); let two: S = cast(2).unwrap();
let c0r0 = f / self.aspect; let c0r0 = f / self.aspect;

View file

@ -52,19 +52,19 @@ impl<S: Float> Quat<S> {
/// Create a matrix from a rotation around the `x` axis (pitch). /// Create a matrix from a rotation around the `x` axis (pitch).
#[inline] #[inline]
pub fn from_angle_x<A: Angle<S>>(theta: A) -> Quat<S> { pub fn from_angle_x(theta: Rad<S>) -> Quat<S> {
Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), sin(theta), zero(), zero()) Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), sin(theta), zero(), zero())
} }
/// Create a matrix from a rotation around the `y` axis (yaw). /// Create a matrix from a rotation around the `y` axis (yaw).
#[inline] #[inline]
pub fn from_angle_y<A: Angle<S>>(theta: A) -> Quat<S> { pub fn from_angle_y(theta: Rad<S>) -> Quat<S> {
Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), zero(), sin(theta), zero()) Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), zero(), sin(theta), zero())
} }
/// Create a matrix from a rotation around the `z` axis (roll). /// Create a matrix from a rotation around the `z` axis (roll).
#[inline] #[inline]
pub fn from_angle_z<A: Angle<S>>(theta: A) -> Quat<S> { pub fn from_angle_z(theta: Rad<S>) -> Quat<S> {
Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), zero(), zero(), sin(theta)) Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), zero(), zero(), sin(theta))
} }
@ -75,7 +75,7 @@ impl<S: Float> Quat<S> {
/// - `x`: the angular rotation around the `x` axis (pitch). /// - `x`: the angular rotation around the `x` axis (pitch).
/// - `y`: the angular rotation around the `y` axis (yaw). /// - `y`: the angular rotation around the `y` axis (yaw).
/// - `z`: the angular rotation around the `z` axis (roll). /// - `z`: the angular rotation around the `z` axis (roll).
pub fn from_euler<A: Angle<S>>(x: A, y: A, z: A) -> Quat<S> { pub fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Quat<S> {
// http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion
let (sx2, cx2) = sin_cos(x.mul_s(cast(0.5).unwrap())); let (sx2, cx2) = sin_cos(x.mul_s(cast(0.5).unwrap()));
let (sy2, cy2) = sin_cos(y.mul_s(cast(0.5).unwrap())); let (sy2, cy2) = sin_cos(y.mul_s(cast(0.5).unwrap()));
@ -89,7 +89,7 @@ impl<S: Float> Quat<S> {
/// Create a quaternion from a rotation around an arbitrary axis /// Create a quaternion from a rotation around an arbitrary axis
#[inline] #[inline]
pub fn from_axis_angle<A: Angle<S>>(axis: &Vec3<S>, angle: A) -> Quat<S> { pub fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Quat<S> {
let half = angle.mul_s(cast(0.5).unwrap()); let half = angle.mul_s(cast(0.5).unwrap());
Quat::from_sv(cos(half.clone()), Quat::from_sv(cos(half.clone()),
axis.mul_s(sin(half))) axis.mul_s(sin(half)))

View file

@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use angle::Angle; use angle::Rad;
use matrix::Matrix; use matrix::Matrix;
use matrix::{Mat2, ToMat2}; use matrix::{Mat2, ToMat2};
use matrix::{Mat3, ToMat3}; use matrix::{Mat3, ToMat3};
@ -154,17 +154,17 @@ impl<S: Float> Rot3<S> {
} }
/// Create a rotation matrix from a rotation around the `x` axis (pitch). /// Create a rotation matrix from a rotation around the `x` axis (pitch).
pub fn from_angle_x<A: Angle<S>>(theta: A) -> Rot3<S> { pub fn from_angle_x(theta: Rad<S>) -> Rot3<S> {
Rot3 { mat: Mat3::from_angle_x(theta) } Rot3 { mat: Mat3::from_angle_x(theta) }
} }
/// Create a rotation matrix from a rotation around the `y` axis (yaw). /// Create a rotation matrix from a rotation around the `y` axis (yaw).
pub fn from_angle_y<A: Angle<S>>(theta: A) -> Rot3<S> { pub fn from_angle_y(theta: Rad<S>) -> Rot3<S> {
Rot3 { mat: Mat3::from_angle_y(theta) } Rot3 { mat: Mat3::from_angle_y(theta) }
} }
/// Create a rotation matrix from a rotation around the `z` axis (roll). /// Create a rotation matrix from a rotation around the `z` axis (roll).
pub fn from_angle_z<A: Angle<S>>(theta: A) -> Rot3<S> { pub fn from_angle_z(theta: Rad<S>) -> Rot3<S> {
Rot3 { mat: Mat3::from_angle_z(theta) } Rot3 { mat: Mat3::from_angle_z(theta) }
} }
@ -175,12 +175,12 @@ impl<S: Float> Rot3<S> {
/// - `x`: the angular rotation around the `x` axis (pitch). /// - `x`: the angular rotation around the `x` axis (pitch).
/// - `y`: the angular rotation around the `y` axis (yaw). /// - `y`: the angular rotation around the `y` axis (yaw).
/// - `z`: the angular rotation around the `z` axis (roll). /// - `z`: the angular rotation around the `z` axis (roll).
pub fn from_euler<A: Angle<S>>(x: A, y: A, z: A) -> Rot3<S> { pub fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Rot3<S> {
Rot3 { mat: Mat3::from_euler(x, y ,z) } Rot3 { mat: Mat3::from_euler(x, y ,z) }
} }
/// Create a rotation matrix from a rotation around an arbitrary axis. /// Create a rotation matrix from a rotation around an arbitrary axis.
pub fn from_axis_angle<A: Angle<S>>(axis: &Vec3<S>, angle: A) -> Rot3<S> { pub fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Rot3<S> {
Rot3 { mat: Mat3::from_axis_angle(axis, angle) } Rot3 { mat: Mat3::from_axis_angle(axis, angle) }
} }