Merge pull request #367 from adudney/MoreInto

Made uses of Rad<S> more generic using Into<Rad<S>>
This commit is contained in:
Brendan Zabarauskas 2016-07-29 14:12:03 +10:00 committed by GitHub
commit 5a1656a461
6 changed files with 62 additions and 63 deletions

View file

@ -103,12 +103,11 @@ impl<S: BaseFloat> Matrix2<S> {
}
#[inline]
pub fn from_angle(theta: Rad<S>) -> Matrix2<S> {
let cos_theta = Rad::cos(theta);
let sin_theta = Rad::sin(theta);
pub fn from_angle<A: Into<Rad<S>>>(theta: A) -> Matrix2<S> {
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<S: BaseFloat> Matrix3<S> {
}
/// Create a rotation matrix from a rotation around the `x` axis (pitch).
pub fn from_angle_x(theta: Rad<S>) -> Matrix3<S> {
pub fn from_angle_x<A: Into<Rad<S>>>(theta: A) -> Matrix3<S> {
// 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<S>) -> Matrix3<S> {
pub fn from_angle_y<A: Into<Rad<S>>>(theta: A) -> Matrix3<S> {
// 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<S>) -> Matrix3<S> {
pub fn from_angle_z<A: Into<Rad<S>>>(theta: A) -> Matrix3<S> {
// 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<S: BaseFloat> Matrix3<S> {
/// 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<S>, angle: Rad<S>) -> Matrix3<S> {
let (s, c) = Rad::sin_cos(angle);
pub fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Matrix3<S> {
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<S: BaseFloat> Matrix4<S> {
}
/// Create a homogeneous transformation matrix from a rotation around the `x` axis (pitch).
pub fn from_angle_x(theta: Rad<S>) -> Matrix4<S> {
pub fn from_angle_x<A: Into<Rad<S>>>(theta: A) -> Matrix4<S> {
// 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<S: BaseFloat> Matrix4<S> {
}
/// Create a homogeneous transformation matrix from a rotation around the `y` axis (yaw).
pub fn from_angle_y(theta: Rad<S>) -> Matrix4<S> {
pub fn from_angle_y<A: Into<Rad<S>>>(theta: A) -> Matrix4<S> {
// 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<S: BaseFloat> Matrix4<S> {
}
/// Create a homogeneous transformation matrix from a rotation around the `z` axis (roll).
pub fn from_angle_z(theta: Rad<S>) -> Matrix4<S> {
pub fn from_angle_z<A: Into<Rad<S>>>(theta: A) -> Matrix4<S> {
// 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<S: BaseFloat> Matrix4<S> {
/// 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<S>, angle: Rad<S>) -> Matrix4<S> {
let (s, c) = Rad::sin_cos(angle);
pub fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Matrix4<S> {
let (s, c) = Rad::sin_cos(angle.into());
let _1subc = S::one() - c;
Matrix4::new(_1subc * axis.x * axis.x + c,

View file

@ -387,8 +387,8 @@ impl<S: BaseFloat> Rotation<Point3<S>> for Quaternion<S> {
impl<S: BaseFloat> Rotation3<S> for Quaternion<S> {
#[inline]
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Quaternion<S> {
let (s, c) = Rad::sin_cos(angle * cast(0.5f64).unwrap());
fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Quaternion<S> {
let (s, c) = Rad::sin_cos(angle.into() * cast(0.5f64).unwrap());
Quaternion::from_sv(c, axis * s)
}
}

View file

@ -62,7 +62,7 @@ pub trait Rotation2<S: BaseFloat>: Rotation<Point2<S>>
+ Into<Basis2<S>> {
/// 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<S>) -> Self;
fn from_angle<A: Into<Rad<S>>>(theta: A) -> Self;
}
/// A three-dimensional rotation.
@ -74,23 +74,23 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
/// 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<S>, angle: Rad<S>) -> Self;
fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Self;
/// Create a rotation from an angle around the `x` axis (pitch).
#[inline]
fn from_angle_x(theta: Rad<S>) -> Self {
fn from_angle_x<A: Into<Rad<S>>>(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<S>) -> Self {
fn from_angle_y<A: Into<Rad<S>>>(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<S>) -> Self {
fn from_angle_z<A: Into<Rad<S>>>(theta: A) -> Self {
Rotation3::from_axis_angle(Vector3::unit_z(), theta)
}
}
@ -197,7 +197,7 @@ impl<S: BaseFloat> ApproxEq for Basis2<S> {
}
impl<S: BaseFloat> Rotation2<S> for Basis2<S> {
fn from_angle(theta: Rad<S>) -> Basis2<S> { Basis2 { mat: Matrix2::from_angle(theta) } }
fn from_angle<A: Into<Rad<S>>>(theta: A) -> Basis2<S> { Basis2 { mat: Matrix2::from_angle(theta) } }
}
impl<S: fmt::Debug> fmt::Debug for Basis2<S> {
@ -285,19 +285,19 @@ impl<S: BaseFloat> ApproxEq for Basis3<S> {
}
impl<S: BaseFloat> Rotation3<S> for Basis3<S> {
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Basis3<S> {
fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Basis3<S> {
Basis3 { mat: Matrix3::from_axis_angle(axis, angle) }
}
fn from_angle_x(theta: Rad<S>) -> Basis3<S> {
fn from_angle_x<A: Into<Rad<S>>>(theta: A) -> Basis3<S> {
Basis3 { mat: Matrix3::from_angle_x(theta) }
}
fn from_angle_y(theta: Rad<S>) -> Basis3<S> {
fn from_angle_y<A: Into<Rad<S>>>(theta: A) -> Basis3<S> {
Basis3 { mat: Matrix3::from_angle_y(theta) }
}
fn from_angle_z(theta: Rad<S>) -> Basis3<S> {
fn from_angle_z<A: Into<Rad<S>>>(theta: A) -> Basis3<S> {
Basis3 { mat: Matrix3::from_angle_z(theta) }
}
}

View file

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

View file

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

View file

@ -21,12 +21,12 @@ mod rotation {
use super::cgmath::*;
pub fn a2<R: Rotation2<f64>>() -> R {
Rotation2::from_angle(deg(30.0).into())
Rotation2::from_angle(deg(30.0))
}
pub fn a3<R: Rotation3<f64>>() -> 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))
}
}