Made uses of Rad<S> more generic using Into<Rad<S>>

This commit is contained in:
Andrew Dudney 2016-07-26 17:30:05 -07:00
parent 3e6974baa8
commit 17d98af64f
3 changed files with 32 additions and 33 deletions

View file

@ -103,12 +103,11 @@ impl<S: BaseFloat> Matrix2<S> {
} }
#[inline] #[inline]
pub fn from_angle(theta: Rad<S>) -> Matrix2<S> { pub fn from_angle<A: Into<Rad<S>>>(theta: A) -> Matrix2<S> {
let cos_theta = Rad::cos(theta); let (s, c) = Rad::sin_cos(theta.into());
let sin_theta = Rad::sin(theta);
Matrix2::new(cos_theta, sin_theta, Matrix2::new(c, s,
-sin_theta, cos_theta) -s, c)
} }
} }
@ -140,27 +139,27 @@ impl<S: BaseFloat> Matrix3<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(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 // 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(), Matrix3::new(S::one(), S::zero(), S::zero(),
S::zero(), c, s, S::zero(), c, s,
S::zero(), -s, c) S::zero(), -s, c)
} }
/// 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(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 // 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, Matrix3::new(c, S::zero(), -s,
S::zero(), S::one(), S::zero(), S::zero(), S::one(), S::zero(),
s, S::zero(), c) s, S::zero(), c)
} }
/// 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(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 // 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(), Matrix3::new( c, s, S::zero(),
-s, c, S::zero(), -s, c, S::zero(),
S::zero(), S::zero(), S::one()) 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. /// Create a rotation matrix from an angle around an arbitrary axis.
/// ///
/// The specified axis **must be normalized**, or it represents an invalid rotation. /// 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> { pub fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Matrix3<S> {
let (s, c) = Rad::sin_cos(angle); let (s, c) = Rad::sin_cos(angle.into());
let _1subc = S::one() - c; let _1subc = S::one() - c;
Matrix3::new(_1subc * axis.x * axis.x + 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). /// 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 // 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(), Matrix4::new(S::one(), S::zero(), S::zero(), S::zero(),
S::zero(), c, s, S::zero(), S::zero(), c, s, S::zero(),
S::zero(), -s, c, 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). /// 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 // 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(), Matrix4::new(c, S::zero(), -s, S::zero(),
S::zero(), S::one(), S::zero(), S::zero(), S::zero(), S::one(), S::zero(), S::zero(),
s, S::zero(), c, 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). /// 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 // 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(), Matrix4::new( c, s, S::zero(), S::zero(),
-s, c, S::zero(), S::zero(), -s, c, S::zero(), S::zero(),
S::zero(), S::zero(), S::one(), 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. /// Create a homogeneous transformation matrix from an angle around an arbitrary axis.
/// ///
/// The specified axis **must be normalized**, or it represents an invalid rotation. /// 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> { pub fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Matrix4<S> {
let (s, c) = Rad::sin_cos(angle); let (s, c) = Rad::sin_cos(angle.into());
let _1subc = S::one() - c; let _1subc = S::one() - c;
Matrix4::new(_1subc * axis.x * axis.x + 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> { impl<S: BaseFloat> Rotation3<S> for Quaternion<S> {
#[inline] #[inline]
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Quaternion<S> { fn from_axis_angle<A: Into<Rad<S>>>(axis: Vector3<S>, angle: A) -> Quaternion<S> {
let (s, c) = Rad::sin_cos(angle * cast(0.5f64).unwrap()); let (s, c) = Rad::sin_cos(angle.into() * cast(0.5f64).unwrap());
Quaternion::from_sv(c, axis * s) Quaternion::from_sv(c, axis * s)
} }
} }

View file

@ -62,7 +62,7 @@ pub trait Rotation2<S: BaseFloat>: Rotation<Point2<S>>
+ Into<Basis2<S>> { + Into<Basis2<S>> {
/// Create a rotation by a given angle. Thus is a redundant case of both /// Create a rotation by a given angle. Thus is a redundant case of both
/// from_axis_angle() and from_euler() for 2D space. /// 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. /// 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. /// Create a rotation using an angle around a given axis.
/// ///
/// The specified axis **must be normalized**, or it represents an invalid rotation. /// 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). /// Create a rotation from an angle around the `x` axis (pitch).
#[inline] #[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) Rotation3::from_axis_angle(Vector3::unit_x(), theta)
} }
/// Create a rotation from an angle around the `y` axis (yaw). /// Create a rotation from an angle around the `y` axis (yaw).
#[inline] #[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) Rotation3::from_axis_angle(Vector3::unit_y(), theta)
} }
/// Create a rotation from an angle around the `z` axis (roll). /// Create a rotation from an angle around the `z` axis (roll).
#[inline] #[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) 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> { 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> { 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> { 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) } 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) } 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) } 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) } Basis3 { mat: Matrix3::from_angle_z(theta) }
} }
} }