From 70d48ed37ba51cedd0751777cc9be1727b8fb984 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 13 Oct 2013 10:52:21 +1100 Subject: [PATCH] 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. --- src/cgmath/angle.rs | 22 +++++++++++----------- src/cgmath/matrix.rs | 12 ++++++------ src/cgmath/projection.rs | 4 ++-- src/cgmath/quaternion.rs | 10 +++++----- src/cgmath/rotation.rs | 12 ++++++------ 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/cgmath/angle.rs b/src/cgmath/angle.rs index 7b80e96..38a1ec9 100644 --- a/src/cgmath/angle.rs +++ b/src/cgmath/angle.rs @@ -166,19 +166,19 @@ impl Angle for Deg { #[inline] fn full_turn() -> Deg { deg(cast(360).unwrap()) } } -#[inline] pub fn sin>(theta: A) -> S { theta.to_rad().s.sin() } -#[inline] pub fn cos>(theta: A) -> S { theta.to_rad().s.cos() } -#[inline] pub fn tan>(theta: A) -> S { theta.to_rad().s.tan() } -#[inline] pub fn sin_cos>(theta: A) -> (S, S) { theta.to_rad().s.sin_cos() } +#[inline] pub fn sin(theta: Rad) -> S { theta.s.sin() } +#[inline] pub fn cos(theta: Rad) -> S { theta.s.cos() } +#[inline] pub fn tan(theta: Rad) -> S { theta.s.tan() } +#[inline] pub fn sin_cos(theta: Rad) -> (S, S) { theta.s.sin_cos() } -#[inline] pub fn cot>(theta: A) -> S { tan(theta).recip() } -#[inline] pub fn sec>(theta: A) -> S { cos(theta).recip() } -#[inline] pub fn csc>(theta: A) -> S { sin(theta).recip() } +#[inline] pub fn cot(theta: Rad) -> S { tan(theta).recip() } +#[inline] pub fn sec(theta: Rad) -> S { cos(theta).recip() } +#[inline] pub fn csc(theta: Rad) -> S { sin(theta).recip() } -#[inline] pub fn asin>(s: S) -> A { Angle::from(rad(s.asin())) } -#[inline] pub fn acos>(s: S) -> A { Angle::from(rad(s.acos())) } -#[inline] pub fn atan>(s: S) -> A { Angle::from(rad(s.atan())) } -#[inline] pub fn atan2>(a: S, b: S) -> A { Angle::from(rad(a.atan2(&b))) } +#[inline] pub fn asin(s: S) -> Rad { rad(s.asin()) } +#[inline] pub fn acos(s: S) -> Rad { rad(s.acos()) } +#[inline] pub fn atan(s: S) -> Rad { rad(s.atan()) } +#[inline] pub fn atan2(a: S, b: S) -> Rad { rad(a.atan2(&b)) } impl ToStr for Rad { fn to_str(&self) -> ~str { fmt!("%? rad", self.s) } } impl ToStr for Deg { fn to_str(&self) -> ~str { fmt!("%?°", self.s) } } diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 9322f32..7d3642a 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -17,7 +17,7 @@ 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 quaternion::{Quat, ToQuat}; use vector::{Vector, EuclideanVector}; @@ -123,7 +123,7 @@ impl Mat3 { } /// Create a matrix from a rotation around the `x` axis (pitch). - pub fn from_angle_x>(theta: A) -> Mat3 { + pub fn from_angle_x(theta: Rad) -> Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let (s, c) = sin_cos(theta); Mat3::new(one(), zero(), zero(), @@ -132,7 +132,7 @@ impl Mat3 { } /// Create a matrix from a rotation around the `y` axis (yaw). - pub fn from_angle_y>(theta: A) -> Mat3 { + pub fn from_angle_y(theta: Rad) -> Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let (s, c) = sin_cos(theta); Mat3::new(c.clone(), zero(), -s.clone(), @@ -141,7 +141,7 @@ impl Mat3 { } /// Create a matrix from a rotation around the `z` axis (roll). - pub fn from_angle_z>(theta: A) -> Mat3 { + pub fn from_angle_z(theta: Rad) -> Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations let (s, c) = sin_cos(theta); Mat3::new(c.clone(), s.clone(), zero(), @@ -156,7 +156,7 @@ impl Mat3 { /// - `x`: the angular rotation around the `x` axis (pitch). /// - `y`: the angular rotation around the `y` axis (yaw). /// - `z`: the angular rotation around the `z` axis (roll). - pub fn from_euler>(x: A, y: A, z: A) -> Mat3 { + pub fn from_euler(x: Rad, y: Rad, z: Rad) -> Mat3 { // http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations let (sx, cx) = sin_cos(x); let (sy, cy) = sin_cos(y); @@ -168,7 +168,7 @@ impl Mat3 { } /// Create a matrix from a rotation around an arbitrary axis - pub fn from_axis_angle>(axis: &Vec3, angle: A) -> Mat3 { + pub fn from_axis_angle(axis: &Vec3, angle: Rad) -> Mat3 { let (s, c) = sin_cos(angle); let _1subc = one::() - c; diff --git a/src/cgmath/projection.rs b/src/cgmath/projection.rs index 98f931f..c308e26 100644 --- a/src/cgmath/projection.rs +++ b/src/cgmath/projection.rs @@ -79,7 +79,7 @@ pub struct PerspectiveFov { impl> PerspectiveFov { pub fn to_perspective(&self) -> Perspective { 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; Perspective { @@ -111,7 +111,7 @@ impl> ToMat4 for PerspectiveFov { 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); - 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 c0r0 = f / self.aspect; diff --git a/src/cgmath/quaternion.rs b/src/cgmath/quaternion.rs index 4d99872..3407011 100644 --- a/src/cgmath/quaternion.rs +++ b/src/cgmath/quaternion.rs @@ -52,19 +52,19 @@ impl Quat { /// Create a matrix from a rotation around the `x` axis (pitch). #[inline] - pub fn from_angle_x>(theta: A) -> Quat { + pub fn from_angle_x(theta: Rad) -> Quat { 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). #[inline] - pub fn from_angle_y>(theta: A) -> Quat { + pub fn from_angle_y(theta: Rad) -> Quat { 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). #[inline] - pub fn from_angle_z>(theta: A) -> Quat { + pub fn from_angle_z(theta: Rad) -> Quat { Quat::new(cos(theta.mul_s(cast(0.5).unwrap())), zero(), zero(), sin(theta)) } @@ -75,7 +75,7 @@ impl Quat { /// - `x`: the angular rotation around the `x` axis (pitch). /// - `y`: the angular rotation around the `y` axis (yaw). /// - `z`: the angular rotation around the `z` axis (roll). - pub fn from_euler>(x: A, y: A, z: A) -> Quat { + pub fn from_euler(x: Rad, y: Rad, z: Rad) -> Quat { // 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 (sy2, cy2) = sin_cos(y.mul_s(cast(0.5).unwrap())); @@ -89,7 +89,7 @@ impl Quat { /// Create a quaternion from a rotation around an arbitrary axis #[inline] - pub fn from_axis_angle>(axis: &Vec3, angle: A) -> Quat { + pub fn from_axis_angle(axis: &Vec3, angle: Rad) -> Quat { let half = angle.mul_s(cast(0.5).unwrap()); Quat::from_sv(cos(half.clone()), axis.mul_s(sin(half))) diff --git a/src/cgmath/rotation.rs b/src/cgmath/rotation.rs index 64193db..699a43b 100644 --- a/src/cgmath/rotation.rs +++ b/src/cgmath/rotation.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use angle::Angle; +use angle::Rad; use matrix::Matrix; use matrix::{Mat2, ToMat2}; use matrix::{Mat3, ToMat3}; @@ -154,17 +154,17 @@ impl Rot3 { } /// Create a rotation matrix from a rotation around the `x` axis (pitch). - pub fn from_angle_x>(theta: A) -> Rot3 { + pub fn from_angle_x(theta: Rad) -> Rot3 { Rot3 { mat: Mat3::from_angle_x(theta) } } /// Create a rotation matrix from a rotation around the `y` axis (yaw). - pub fn from_angle_y>(theta: A) -> Rot3 { + pub fn from_angle_y(theta: Rad) -> Rot3 { Rot3 { mat: Mat3::from_angle_y(theta) } } /// Create a rotation matrix from a rotation around the `z` axis (roll). - pub fn from_angle_z>(theta: A) -> Rot3 { + pub fn from_angle_z(theta: Rad) -> Rot3 { Rot3 { mat: Mat3::from_angle_z(theta) } } @@ -175,12 +175,12 @@ impl Rot3 { /// - `x`: the angular rotation around the `x` axis (pitch). /// - `y`: the angular rotation around the `y` axis (yaw). /// - `z`: the angular rotation around the `z` axis (roll). - pub fn from_euler>(x: A, y: A, z: A) -> Rot3 { + pub fn from_euler(x: Rad, y: Rad, z: Rad) -> Rot3 { Rot3 { mat: Mat3::from_euler(x, y ,z) } } /// Create a rotation matrix from a rotation around an arbitrary axis. - pub fn from_axis_angle>(axis: &Vec3, angle: A) -> Rot3 { + pub fn from_axis_angle(axis: &Vec3, angle: Rad) -> Rot3 { Rot3 { mat: Mat3::from_axis_angle(axis, angle) } }