From 17d98af64f0064eb8cf9413910e5cc788967a0bf Mon Sep 17 00:00:00 2001 From: Andrew Dudney Date: Tue, 26 Jul 2016 17:30:05 -0700 Subject: [PATCH 1/2] Made uses of Rad more generic using Into> --- src/matrix.rs | 41 ++++++++++++++++++++--------------------- src/quaternion.rs | 4 ++-- src/rotation.rs | 20 ++++++++++---------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 6cf355a..50fb8a2 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -103,12 +103,11 @@ impl Matrix2 { } #[inline] - pub fn from_angle(theta: Rad) -> Matrix2 { - let cos_theta = Rad::cos(theta); - let sin_theta = Rad::sin(theta); + pub fn from_angle>>(theta: A) -> Matrix2 { + let (s, c) = Rad::sin_cos(theta.into()); - Matrix2::new(cos_theta, sin_theta, - -sin_theta, cos_theta) + Matrix2::new(c, s, + -s, c) } } @@ -140,27 +139,27 @@ impl Matrix3 { } /// Create a rotation matrix from a rotation around the `x` axis (pitch). - pub fn from_angle_x(theta: Rad) -> Matrix3 { + pub fn from_angle_x>>(theta: A) -> Matrix3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let (s, c) = Rad::sin_cos(theta); + let (s, c) = Rad::sin_cos(theta.into()); Matrix3::new(S::one(), S::zero(), S::zero(), S::zero(), c, s, S::zero(), -s, c) } /// Create a rotation matrix from a rotation around the `y` axis (yaw). - pub fn from_angle_y(theta: Rad) -> Matrix3 { + pub fn from_angle_y>>(theta: A) -> Matrix3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let (s, c) = Rad::sin_cos(theta); + let (s, c) = Rad::sin_cos(theta.into()); Matrix3::new(c, S::zero(), -s, S::zero(), S::one(), S::zero(), s, S::zero(), c) } /// Create a rotation matrix from a rotation around the `z` axis (roll). - pub fn from_angle_z(theta: Rad) -> Matrix3 { + pub fn from_angle_z>>(theta: A) -> Matrix3 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let (s, c) = Rad::sin_cos(theta); + let (s, c) = Rad::sin_cos(theta.into()); Matrix3::new( c, s, S::zero(), -s, c, S::zero(), S::zero(), S::zero(), S::one()) @@ -169,8 +168,8 @@ impl Matrix3 { /// Create a rotation matrix from an angle around an arbitrary axis. /// /// The specified axis **must be normalized**, or it represents an invalid rotation. - pub fn from_axis_angle(axis: Vector3, angle: Rad) -> Matrix3 { - let (s, c) = Rad::sin_cos(angle); + pub fn from_axis_angle>>(axis: Vector3, angle: A) -> Matrix3 { + let (s, c) = Rad::sin_cos(angle.into()); let _1subc = S::one() - c; Matrix3::new(_1subc * axis.x * axis.x + c, @@ -244,9 +243,9 @@ impl Matrix4 { } /// Create a homogeneous transformation matrix from a rotation around the `x` axis (pitch). - pub fn from_angle_x(theta: Rad) -> Matrix4 { + pub fn from_angle_x>>(theta: A) -> Matrix4 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let (s, c) = Rad::sin_cos(theta); + let (s, c) = Rad::sin_cos(theta.into()); Matrix4::new(S::one(), S::zero(), S::zero(), S::zero(), S::zero(), c, s, S::zero(), S::zero(), -s, c, S::zero(), @@ -254,9 +253,9 @@ impl Matrix4 { } /// Create a homogeneous transformation matrix from a rotation around the `y` axis (yaw). - pub fn from_angle_y(theta: Rad) -> Matrix4 { + pub fn from_angle_y>>(theta: A) -> Matrix4 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let (s, c) = Rad::sin_cos(theta); + let (s, c) = Rad::sin_cos(theta.into()); Matrix4::new(c, S::zero(), -s, S::zero(), S::zero(), S::one(), S::zero(), S::zero(), s, S::zero(), c, S::zero(), @@ -264,9 +263,9 @@ impl Matrix4 { } /// Create a homogeneous transformation matrix from a rotation around the `z` axis (roll). - pub fn from_angle_z(theta: Rad) -> Matrix4 { + pub fn from_angle_z>>(theta: A) -> Matrix4 { // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let (s, c) = Rad::sin_cos(theta); + let (s, c) = Rad::sin_cos(theta.into()); Matrix4::new( c, s, S::zero(), S::zero(), -s, c, S::zero(), S::zero(), S::zero(), S::zero(), S::one(), S::zero(), @@ -276,8 +275,8 @@ impl Matrix4 { /// Create a homogeneous transformation matrix from an angle around an arbitrary axis. /// /// The specified axis **must be normalized**, or it represents an invalid rotation. - pub fn from_axis_angle(axis: Vector3, angle: Rad) -> Matrix4 { - let (s, c) = Rad::sin_cos(angle); + pub fn from_axis_angle>>(axis: Vector3, angle: A) -> Matrix4 { + let (s, c) = Rad::sin_cos(angle.into()); let _1subc = S::one() - c; Matrix4::new(_1subc * axis.x * axis.x + c, diff --git a/src/quaternion.rs b/src/quaternion.rs index 3414eb6..bef4851 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -387,8 +387,8 @@ impl Rotation> for Quaternion { impl Rotation3 for Quaternion { #[inline] - fn from_axis_angle(axis: Vector3, angle: Rad) -> Quaternion { - let (s, c) = Rad::sin_cos(angle * cast(0.5f64).unwrap()); + fn from_axis_angle>>(axis: Vector3, angle: A) -> Quaternion { + let (s, c) = Rad::sin_cos(angle.into() * cast(0.5f64).unwrap()); Quaternion::from_sv(c, axis * s) } } diff --git a/src/rotation.rs b/src/rotation.rs index 0f93ad0..829e204 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -62,7 +62,7 @@ pub trait Rotation2: Rotation> + Into> { /// Create a rotation by a given angle. Thus is a redundant case of both /// from_axis_angle() and from_euler() for 2D space. - fn from_angle(theta: Rad) -> Self; + fn from_angle>>(theta: A) -> Self; } /// A three-dimensional rotation. @@ -74,23 +74,23 @@ pub trait Rotation3: Rotation> /// Create a rotation using an angle around a given axis. /// /// The specified axis **must be normalized**, or it represents an invalid rotation. - fn from_axis_angle(axis: Vector3, angle: Rad) -> Self; + fn from_axis_angle>>(axis: Vector3, angle: A) -> Self; /// Create a rotation from an angle around the `x` axis (pitch). #[inline] - fn from_angle_x(theta: Rad) -> Self { + fn from_angle_x>>(theta: A) -> Self { Rotation3::from_axis_angle(Vector3::unit_x(), theta) } /// Create a rotation from an angle around the `y` axis (yaw). #[inline] - fn from_angle_y(theta: Rad) -> Self { + fn from_angle_y>>(theta: A) -> Self { Rotation3::from_axis_angle(Vector3::unit_y(), theta) } /// Create a rotation from an angle around the `z` axis (roll). #[inline] - fn from_angle_z(theta: Rad) -> Self { + fn from_angle_z>>(theta: A) -> Self { Rotation3::from_axis_angle(Vector3::unit_z(), theta) } } @@ -197,7 +197,7 @@ impl ApproxEq for Basis2 { } impl Rotation2 for Basis2 { - fn from_angle(theta: Rad) -> Basis2 { Basis2 { mat: Matrix2::from_angle(theta) } } + fn from_angle>>(theta: A) -> Basis2 { Basis2 { mat: Matrix2::from_angle(theta) } } } impl fmt::Debug for Basis2 { @@ -285,19 +285,19 @@ impl ApproxEq for Basis3 { } impl Rotation3 for Basis3 { - fn from_axis_angle(axis: Vector3, angle: Rad) -> Basis3 { + fn from_axis_angle>>(axis: Vector3, angle: A) -> Basis3 { Basis3 { mat: Matrix3::from_axis_angle(axis, angle) } } - fn from_angle_x(theta: Rad) -> Basis3 { + fn from_angle_x>>(theta: A) -> Basis3 { Basis3 { mat: Matrix3::from_angle_x(theta) } } - fn from_angle_y(theta: Rad) -> Basis3 { + fn from_angle_y>>(theta: A) -> Basis3 { Basis3 { mat: Matrix3::from_angle_y(theta) } } - fn from_angle_z(theta: Rad) -> Basis3 { + fn from_angle_z>>(theta: A) -> Basis3 { Basis3 { mat: Matrix3::from_angle_z(theta) } } } From 1b77875cc3825939ae14ee34f4d03307d9f05eb9 Mon Sep 17 00:00:00 2001 From: Andrew Dudney Date: Tue, 26 Jul 2016 17:53:16 -0700 Subject: [PATCH 2/2] Fixed tests, now most use deg, and none use deg(x).into() --- tests/matrix.rs | 28 ++++++++++++++-------------- tests/quaternion.rs | 28 ++++++++++++++-------------- tests/rotation.rs | 4 ++-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/matrix.rs b/tests/matrix.rs index 3af48f0..9d7674f 100644 --- a/tests/matrix.rs +++ b/tests/matrix.rs @@ -406,10 +406,10 @@ pub mod matrix3 { fn test_x() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Matrix3::from(Euler::new(deg(90.0).into(), rad(0.0), rad(0.0))); + let rot = Matrix3::from(Euler::new(deg(90.0), deg(0.0), deg(0.0))); assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec); - let rot = Matrix3::from(Euler::new(deg(-90.0).into(), rad(0.0), rad(0.0))); + let rot = Matrix3::from(Euler::new(deg(-90.0), deg(0.0), deg(0.0))); assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec); } @@ -417,10 +417,10 @@ pub mod matrix3 { fn test_y() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Matrix3::from(Euler::new(rad(0.0), deg(90.0).into(), rad(0.0))); + let rot = Matrix3::from(Euler::new(deg(0.0), deg(90.0), deg(0.0))); assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec); - let rot = Matrix3::from(Euler::new(rad(0.0), deg(-90.0).into(), rad(0.0))); + let rot = Matrix3::from(Euler::new(deg(0.0), deg(-90.0), deg(0.0))); assert_approx_eq!(vec3(-1.0, 0.0, 0.0), rot * vec); } @@ -428,10 +428,10 @@ pub mod matrix3 { fn test_z() { let vec = vec3(1.0, 0.0, 0.0); - let rot = Matrix3::from(Euler::new(rad(0.0), rad(0.0), deg(90.0).into())); + let rot = Matrix3::from(Euler::new(deg(0.0), deg(0.0), deg(90.0))); assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec); - let rot = Matrix3::from(Euler::new(rad(0.0), rad(0.0), deg(-90.0).into())); + let rot = Matrix3::from(Euler::new(deg(0.0), deg(0.0), deg(-90.0))); assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec); } @@ -441,7 +441,7 @@ pub mod matrix3 { fn test_x_then_y() { let vec = vec3(0.0, 1.0, 0.0); - let rot = Matrix3::from(Euler::new(deg(90.0).into(), deg(90.0).into(), rad(0.0))); + let rot = Matrix3::from(Euler::new(deg(90.0), deg(90.0), deg(0.0))); assert_approx_eq!(vec3(0.0, 0.0, 1.0), rot * vec); } @@ -450,7 +450,7 @@ pub mod matrix3 { fn test_y_then_z() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Matrix3::from(Euler::new(rad(0.0), deg(90.0).into(), deg(90.0).into())); + let rot = Matrix3::from(Euler::new(deg(0.0), deg(90.0), deg(90.0))); assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec); } } @@ -462,7 +462,7 @@ pub mod matrix3 { fn test_x() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Matrix3::from_angle_x(deg(90.0).into()); + let rot = Matrix3::from_angle_x(deg(90.0)); println!("x mat: {:?}", rot); assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec); } @@ -471,7 +471,7 @@ pub mod matrix3 { fn test_y() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Matrix3::from_angle_y(deg(90.0).into()); + let rot = Matrix3::from_angle_y(deg(90.0)); assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec); } @@ -479,7 +479,7 @@ pub mod matrix3 { fn test_z() { let vec = vec3(1.0, 0.0, 0.0); - let rot = Matrix3::from_angle_z(deg(90.0).into()); + let rot = Matrix3::from_angle_z(deg(90.0)); assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec); } @@ -487,7 +487,7 @@ pub mod matrix3 { fn test_xy() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Matrix3::from_axis_angle(vec3(1.0, 1.0, 0.0).normalize(), deg(90.0).into()); + let rot = Matrix3::from_axis_angle(vec3(1.0, 1.0, 0.0).normalize(), deg(90.0)); assert_approx_eq!(vec3(2.0f32.sqrt() / 2.0, -2.0f32.sqrt() / 2.0, 0.0), rot * vec); } @@ -495,7 +495,7 @@ pub mod matrix3 { fn test_yz() { let vec = vec3(1.0, 0.0, 0.0); - let rot = Matrix3::from_axis_angle(vec3(0.0, 1.0, 1.0).normalize(), deg(-90.0).into()); + let rot = Matrix3::from_axis_angle(vec3(0.0, 1.0, 1.0).normalize(), deg(-90.0)); assert_approx_eq!(vec3(0.0, -2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0), rot * vec); } @@ -503,7 +503,7 @@ pub mod matrix3 { fn test_xz() { let vec = vec3(0.0, 1.0, 0.0); - let rot = Matrix3::from_axis_angle(vec3(1.0, 0.0, 1.0).normalize(), deg(90.0).into()); + let rot = Matrix3::from_axis_angle(vec3(1.0, 0.0, 1.0).normalize(), deg(90.0)); assert_approx_eq!(vec3(-2.0f32.sqrt() / 2.0, 0.0, 2.0f32.sqrt() / 2.0), rot * vec); } } diff --git a/tests/quaternion.rs b/tests/quaternion.rs index bfa9cee..c73ad07 100644 --- a/tests/quaternion.rs +++ b/tests/quaternion.rs @@ -156,10 +156,10 @@ mod rotate_from_euler { fn test_x() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Quaternion::from(Euler::new(deg(90.0).into(), rad(0.0), rad(0.0))); + let rot = Quaternion::from(Euler::new(deg(90.0), deg(0.0), deg(0.0))); assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec); - let rot = Quaternion::from(Euler::new(deg(-90.0).into(), rad(0.0), rad(0.0))); + let rot = Quaternion::from(Euler::new(deg(-90.0), deg(0.0), deg(0.0))); assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec); } @@ -167,10 +167,10 @@ mod rotate_from_euler { fn test_y() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Quaternion::from(Euler::new(rad(0.0), deg(90.0).into(), rad(0.0))); + let rot = Quaternion::from(Euler::new(deg(0.0), deg(90.0), deg(0.0))); assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec); - let rot = Quaternion::from(Euler::new(rad(0.0), deg(-90.0).into(), rad(0.0))); + let rot = Quaternion::from(Euler::new(deg(0.0), deg(-90.0), deg(0.0))); assert_approx_eq!(vec3(-1.0, 0.0, 0.0), rot * vec); } @@ -178,10 +178,10 @@ mod rotate_from_euler { fn test_z() { let vec = vec3(1.0, 0.0, 0.0); - let rot = Quaternion::from(Euler::new(rad(0.0), rad(0.0), deg(90.0).into())); + let rot = Quaternion::from(Euler::new(deg(0.0), deg(0.0), deg(90.0))); assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec); - let rot = Quaternion::from(Euler::new(rad(0.0), rad(0.0), deg(-90.0).into())); + let rot = Quaternion::from(Euler::new(deg(0.0), deg(0.0), deg(-90.0))); assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec); } @@ -191,7 +191,7 @@ mod rotate_from_euler { fn test_x_then_y() { let vec = vec3(0.0, 1.0, 0.0); - let rot = Quaternion::from(Euler::new(deg(90.0).into(), deg(90.0).into(), rad(0.0))); + let rot = Quaternion::from(Euler::new(deg(90.0), deg(90.0), deg(0.0))); assert_approx_eq!(vec3(0.0, 0.0, 1.0), rot * vec); } @@ -200,7 +200,7 @@ mod rotate_from_euler { fn test_y_then_z() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Quaternion::from(Euler::new(rad(0.0), deg(90.0).into(), deg(90.0).into())); + let rot = Quaternion::from(Euler::new(deg(0.0), deg(90.0), deg(90.0))); assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec); } } @@ -212,7 +212,7 @@ mod rotate_from_axis_angle { fn test_x() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Quaternion::from_angle_x(deg(90.0).into()); + let rot = Quaternion::from_angle_x(deg(90.0)); assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec); } @@ -220,7 +220,7 @@ mod rotate_from_axis_angle { fn test_y() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Quaternion::from_angle_y(deg(90.0).into()); + let rot = Quaternion::from_angle_y(deg(90.0)); assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec); } @@ -228,7 +228,7 @@ mod rotate_from_axis_angle { fn test_z() { let vec = vec3(1.0, 0.0, 0.0); - let rot = Quaternion::from_angle_z(deg(90.0).into()); + let rot = Quaternion::from_angle_z(deg(90.0)); assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec); } @@ -236,7 +236,7 @@ mod rotate_from_axis_angle { fn test_xy() { let vec = vec3(0.0, 0.0, 1.0); - let rot = Quaternion::from_axis_angle(vec3(1.0, 1.0, 0.0).normalize(), deg(90.0).into()); + let rot = Quaternion::from_axis_angle(vec3(1.0, 1.0, 0.0).normalize(), deg(90.0)); assert_approx_eq!(vec3(2.0f32.sqrt() / 2.0, -2.0f32.sqrt() / 2.0, 0.0), rot * vec); } @@ -244,7 +244,7 @@ mod rotate_from_axis_angle { fn test_yz() { let vec = vec3(1.0, 0.0, 0.0); - let rot = Quaternion::from_axis_angle(vec3(0.0, 1.0, 1.0).normalize(), deg(-90.0).into()); + let rot = Quaternion::from_axis_angle(vec3(0.0, 1.0, 1.0).normalize(), deg(-90.0)); assert_approx_eq!(vec3(0.0, -2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0), rot * vec); } @@ -252,7 +252,7 @@ mod rotate_from_axis_angle { fn test_xz() { let vec = vec3(0.0, 1.0, 0.0); - let rot = Quaternion::from_axis_angle(vec3(1.0, 0.0, 1.0).normalize(), deg(90.0).into()); + let rot = Quaternion::from_axis_angle(vec3(1.0, 0.0, 1.0).normalize(), deg(90.0)); assert_approx_eq!(vec3(-2.0f32.sqrt() / 2.0, 0.0, 2.0f32.sqrt() / 2.0), rot * vec); } } diff --git a/tests/rotation.rs b/tests/rotation.rs index 0f41611..97d3169 100644 --- a/tests/rotation.rs +++ b/tests/rotation.rs @@ -21,12 +21,12 @@ mod rotation { use super::cgmath::*; pub fn a2>() -> R { - Rotation2::from_angle(deg(30.0).into()) + Rotation2::from_angle(deg(30.0)) } pub fn a3>() -> R { let axis = Vector3::new(1.0, 1.0, 0.0).normalize(); - Rotation3::from_axis_angle(axis, deg(30.0).into()) + Rotation3::from_axis_angle(axis, deg(30.0)) } }