Moved angle types to tuple structs

This commit is contained in:
Andrew Dudney 2016-07-31 20:40:31 -07:00
parent 5a1656a461
commit 3d3b9c96ca
10 changed files with 137 additions and 149 deletions

View file

@ -35,7 +35,7 @@ use num::BaseFloat;
#[derive(Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Rad<S> { pub s: S }
pub struct Rad<S>(pub S);
/// An angle, in degrees.
///
@ -44,40 +44,28 @@ pub struct Rad<S> { pub s: S }
#[derive(Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct Deg<S> { pub s: S }
/// Create a new angle, in radians
#[inline] pub fn rad<S: BaseFloat>(s: S) -> Rad<S> { Rad { s: s } }
/// Create a new angle, in degrees
#[inline] pub fn deg<S: BaseFloat>(s: S) -> Deg<S> { Deg { s: s } }
pub struct Deg<S>(pub S);
impl<S> From<Rad<S>> for Deg<S> where S: BaseFloat {
#[inline]
fn from(r: Rad<S>) -> Deg<S> {
Deg::new(r.s * cast(180.0 / f64::consts::PI).unwrap())
fn from(rad: Rad<S>) -> Deg<S> {
Deg(rad.0 * cast(180.0 / f64::consts::PI).unwrap())
}
}
impl<S> From<Deg<S>> for Rad<S> where S: BaseFloat {
#[inline]
fn from(d: Deg<S>) -> Rad<S> {
Rad::new(d.s * cast(f64::consts::PI / 180.0).unwrap())
fn from(deg: Deg<S>) -> Rad<S> {
Rad(deg.0 * cast(f64::consts::PI / 180.0).unwrap())
}
}
macro_rules! impl_angle {
($Angle:ident, $fmt:expr, $full_turn:expr, $hi:expr) => {
impl<S: BaseFloat> $Angle<S> {
#[inline]
pub fn new(value: S) -> $Angle<S> {
$Angle { s: value }
}
}
impl<S: BaseFloat> Zero for $Angle<S> {
#[inline]
fn zero() -> $Angle<S> {
$Angle::new(S::zero())
$Angle(S::zero())
}
#[inline]
@ -89,66 +77,66 @@ macro_rules! impl_angle {
impl<S: BaseFloat> Angle for $Angle<S> {
type Unitless = S;
#[inline] fn full_turn() -> $Angle<S> { $Angle::new(cast($full_turn).unwrap()) }
#[inline] fn full_turn() -> $Angle<S> { $Angle(cast($full_turn).unwrap()) }
#[inline] fn sin(self) -> S { Rad::from(self).s.sin() }
#[inline] fn cos(self) -> S { Rad::from(self).s.cos() }
#[inline] fn tan(self) -> S { Rad::from(self).s.tan() }
#[inline] fn sin_cos(self) -> (S, S) { Rad::from(self).s.sin_cos() }
#[inline] fn sin(self) -> S { Rad::from(self).0.sin() }
#[inline] fn cos(self) -> S { Rad::from(self).0.cos() }
#[inline] fn tan(self) -> S { Rad::from(self).0.tan() }
#[inline] fn sin_cos(self) -> (S, S) { Rad::from(self).0.sin_cos() }
#[inline] fn asin(a: S) -> $Angle<S> { Rad::new(a.asin()).into() }
#[inline] fn acos(a: S) -> $Angle<S> { Rad::new(a.acos()).into() }
#[inline] fn atan(a: S) -> $Angle<S> { Rad::new(a.atan()).into() }
#[inline] fn atan2(a: S, b: S) -> $Angle<S> { Rad::new(a.atan2(b)).into() }
#[inline] fn asin(a: S) -> $Angle<S> { Rad(a.asin()).into() }
#[inline] fn acos(a: S) -> $Angle<S> { Rad(a.acos()).into() }
#[inline] fn atan(a: S) -> $Angle<S> { Rad(a.atan()).into() }
#[inline] fn atan2(a: S, b: S) -> $Angle<S> { Rad(a.atan2(b)).into() }
}
impl<S: BaseFloat> Neg for $Angle<S> {
type Output = $Angle<S>;
#[inline]
fn neg(self) -> $Angle<S> { $Angle::new(-self.s) }
fn neg(self) -> $Angle<S> { $Angle(-self.0) }
}
impl<'a, S: BaseFloat> Neg for &'a $Angle<S> {
type Output = $Angle<S>;
#[inline]
fn neg(self) -> $Angle<S> { $Angle::new(-self.s) }
fn neg(self) -> $Angle<S> { $Angle(-self.0) }
}
impl_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> {
fn add(lhs, rhs) -> $Angle<S> { $Angle::new(lhs.s + rhs.s) }
fn add(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 + rhs.0) }
});
impl_operator!(<S: BaseFloat> Sub<$Angle<S> > for $Angle<S> {
fn sub(lhs, rhs) -> $Angle<S> { $Angle::new(lhs.s - rhs.s) }
fn sub(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 - rhs.0) }
});
impl_operator!(<S: BaseFloat> Div<$Angle<S> > for $Angle<S> {
fn div(lhs, rhs) -> S { lhs.s / rhs.s }
fn div(lhs, rhs) -> S { lhs.0 / rhs.0 }
});
impl_operator!(<S: BaseFloat> Rem<$Angle<S> > for $Angle<S> {
fn rem(lhs, rhs) -> $Angle<S> { $Angle::new(lhs.s % rhs.s) }
fn rem(lhs, rhs) -> $Angle<S> { $Angle(lhs.0 % rhs.0) }
});
impl_assignment_operator!(<S: BaseFloat> AddAssign<$Angle<S> > for $Angle<S> {
fn add_assign(&mut self, other) { self.s += other.s; }
fn add_assign(&mut self, other) { self.0 += other.0; }
});
impl_assignment_operator!(<S: BaseFloat> SubAssign<$Angle<S> > for $Angle<S> {
fn sub_assign(&mut self, other) { self.s -= other.s; }
fn sub_assign(&mut self, other) { self.0 -= other.0; }
});
impl_assignment_operator!(<S: BaseFloat> RemAssign<$Angle<S> > for $Angle<S> {
fn rem_assign(&mut self, other) { self.s %= other.s; }
fn rem_assign(&mut self, other) { self.0 %= other.0; }
});
impl_operator!(<S: BaseFloat> Mul<S> for $Angle<S> {
fn mul(lhs, scalar) -> $Angle<S> { $Angle::new(lhs.s * scalar) }
fn mul(lhs, scalar) -> $Angle<S> { $Angle(lhs.0 * scalar) }
});
impl_operator!(<S: BaseFloat> Div<S> for $Angle<S> {
fn div(lhs, scalar) -> $Angle<S> { $Angle::new(lhs.s / scalar) }
fn div(lhs, scalar) -> $Angle<S> { $Angle(lhs.0 / scalar) }
});
impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for $Angle<S> {
fn mul_assign(&mut self, scalar) { self.s *= scalar; }
fn mul_assign(&mut self, scalar) { self.0 *= scalar; }
});
impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for $Angle<S> {
fn div_assign(&mut self, scalar) { self.s /= scalar; }
fn div_assign(&mut self, scalar) { self.0 /= scalar; }
});
impl<S: BaseFloat> ApproxEq for $Angle<S> {
@ -156,20 +144,20 @@ macro_rules! impl_angle {
#[inline]
fn approx_eq_eps(&self, other: &$Angle<S>, epsilon: &S) -> bool {
self.s.approx_eq_eps(&other.s, epsilon)
self.0.approx_eq_eps(&other.0, epsilon)
}
}
impl<S: BaseFloat + SampleRange> Rand for $Angle<S> {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> $Angle<S> {
$Angle::new(rng.gen_range(cast(-$hi).unwrap(), cast($hi).unwrap()))
$Angle(rng.gen_range(cast(-$hi).unwrap(), cast($hi).unwrap()))
}
}
impl<S: fmt::Debug> fmt::Debug for $Angle<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, $fmt, self.s)
write!(f, $fmt, self.0)
}
}
}

View file

@ -63,9 +63,9 @@ use num::BaseFloat;
/// use cgmath::{Deg, Euler, Quaternion};
///
/// let rotation = Quaternion::from(Euler {
/// x: Deg::new(90.0),
/// y: Deg::new(45.0),
/// z: Deg::new(15.0),
/// x: Deg(90.0),
/// y: Deg(45.0),
/// z: Deg(15.0),
/// });
/// ```
///

View file

@ -72,7 +72,7 @@ pub use matrix::{Matrix2, Matrix3, Matrix4};
pub use quaternion::Quaternion;
pub use vector::{Vector1, Vector2, Vector3, Vector4, dot, vec1, vec2, vec3, vec4};
pub use angle::{Deg, Rad, deg, rad};
pub use angle::{Deg, Rad};
pub use euler::Euler;
pub use point::{Point1, Point2, Point3};
pub use rotation::*;

View file

@ -110,7 +110,7 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
/// matrix:
///
/// ```no_run
/// use cgmath::rad;
/// use cgmath::Rad;
/// use cgmath::Vector2;
/// use cgmath::{Matrix, Matrix2};
/// use cgmath::{Rotation, Rotation2, Basis2};
@ -120,7 +120,7 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
/// // For simplicity, we will rotate the unit x vector to the unit y vector --
/// // so the angle is 90 degrees, or π/2.
/// let unit_x: Vector2<f64> = Vector2::unit_x();
/// let rot: Basis2<f64> = Rotation2::from_angle(rad(0.5f64 * f64::consts::PI));
/// let rot: Basis2<f64> = Rotation2::from_angle(Rad(0.5f64 * f64::consts::PI));
///
/// // Rotate the vector using the two-dimensional rotation matrix:
/// let unit_y = rot.rotate_vector(unit_x);
@ -135,7 +135,7 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
/// assert_eq!(unit_y2, unit_y);
///
/// // Note that we can also concatenate rotations:
/// let rot_half: Basis2<f64> = Rotation2::from_angle(rad(0.25f64 * f64::consts::PI));
/// let rot_half: Basis2<f64> = Rotation2::from_angle(Rad(0.25f64 * f64::consts::PI));
/// let unit_y3 = (rot_half * rot_half).rotate_vector(unit_x);
/// assert!(unit_y3.approx_eq(&unit_y2));
/// ```

View file

@ -608,7 +608,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let ratio: f32 = Rad::sin(angle);
/// ```
fn sin(self) -> Self::Unitless;
@ -619,7 +619,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let ratio: f32 = Rad::cos(angle);
/// ```
fn cos(self) -> Self::Unitless;
@ -630,7 +630,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let ratio: f32 = Rad::tan(angle);
/// ```
fn tan(self) -> Self::Unitless;
@ -645,7 +645,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let (s, c) = Rad::sin_cos(angle);
/// ```
fn sin_cos(self) -> (Self::Unitless, Self::Unitless);
@ -658,7 +658,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let ratio: f32 = Rad::csc(angle);
/// ```
#[inline]
@ -674,7 +674,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let ratio: f32 = Rad::cot(angle);
/// ```
#[inline]
@ -690,7 +690,7 @@ pub trait Angle where
/// use cgmath::prelude::*;
/// use cgmath::Rad;
///
/// let angle = Rad::new(35.0);
/// let angle = Rad(35.0);
/// let ratio: f32 = Rad::sec(angle);
/// ```
#[inline]

View file

@ -15,24 +15,24 @@
extern crate cgmath;
use cgmath::{Rad, Deg, rad, deg};
use cgmath::{Rad, Deg};
use cgmath::ApproxEq;
#[test]
fn conv() {
let angle: Rad<_> = deg(-5.0f64).into();
let angle: Rad<_> = Deg(-5.0f64).into();
let angle: Deg<_> = angle.into();
assert!(angle.approx_eq(&deg(-5.0f64)));
assert!(angle.approx_eq(&Deg(-5.0f64)));
let angle: Rad<_> = deg(30.0f64).into();
let angle: Rad<_> = Deg(30.0f64).into();
let angle: Deg<_> = angle.into();
assert!(angle.approx_eq(&deg(30.0f64)));
assert!(angle.approx_eq(&Deg(30.0f64)));
let angle: Deg<_> = rad(-5.0f64).into();
let angle: Deg<_> = Rad(-5.0f64).into();
let angle: Rad<_> = angle.into();
assert!(angle.approx_eq(&rad(-5.0f64)));
assert!(angle.approx_eq(&Rad(-5.0f64)));
let angle: Deg<_> = rad(30.0f64).into();
let angle: Deg<_> = Rad(30.0f64).into();
let angle: Rad<_> = angle.into();
assert!(angle.approx_eq(&rad(30.0f64)));
assert!(angle.approx_eq(&Rad(30.0f64)));
}

View file

@ -152,7 +152,7 @@ pub mod matrix2 {
#[test]
fn test_from_angle() {
// Rotate the vector (1, 0) by π/2 radians to the vector (0, 1)
let rot1 = Matrix2::from_angle(rad(0.5f64 * f64::consts::PI));
let rot1 = Matrix2::from_angle(Rad(0.5f64 * f64::consts::PI));
assert_approx_eq!(rot1 * Vector2::unit_x(), &Vector2::unit_y());
// Rotate the vector (-1, 0) by -π/2 radians to the vector (0, 1)
@ -160,7 +160,7 @@ pub mod matrix2 {
assert_approx_eq!(rot2 * -Vector2::unit_x(), &Vector2::unit_y());
// Rotate the vector (1, 1) by π radians to the vector (-1, -1)
let rot3: Matrix2<f64> = Matrix2::from_angle(rad(f64::consts::PI));
let rot3: Matrix2<f64> = Matrix2::from_angle(Rad(f64::consts::PI));
assert_approx_eq!(rot3 * Vector2::new(1.0, 1.0), &Vector2::new(-1.0, -1.0));
}
}
@ -318,13 +318,13 @@ pub mod matrix3 {
fn check_from_axis_angle_x(pitch: Rad<f32>) {
let found = Matrix3::from_angle_x(pitch);
let expected = Matrix3::from(Euler { x: pitch, y: rad(0.0), z: rad(0.0) });
let expected = Matrix3::from(Euler { x: pitch, y: Rad(0.0), z: Rad(0.0) });
assert_approx_eq_eps!(found, expected, 0.001);
}
#[test] fn test_zero() { check_from_axis_angle_x(rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_x(rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_x(rad(-1.0)); }
#[test] fn test_zero() { check_from_axis_angle_x(Rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_x(Rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_x(Rad(-1.0)); }
}
mod from_axis_y {
@ -332,13 +332,13 @@ pub mod matrix3 {
fn check_from_axis_angle_y(yaw: Rad<f32>) {
let found = Matrix3::from_angle_y(yaw);
let expected = Matrix3::from(Euler { x: rad(0.0), y: yaw, z: rad(0.0) });
let expected = Matrix3::from(Euler { x: Rad(0.0), y: yaw, z: Rad(0.0) });
assert_approx_eq_eps!(found, expected, 0.001);
}
#[test] fn test_zero() { check_from_axis_angle_y(rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_y(rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_y(rad(-1.0)); }
#[test] fn test_zero() { check_from_axis_angle_y(Rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_y(Rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_y(Rad(-1.0)); }
}
mod from_axis_z {
@ -346,13 +346,13 @@ pub mod matrix3 {
fn check_from_axis_angle_z(roll: Rad<f32>) {
let found = Matrix3::from_angle_z(roll);
let expected = Matrix3::from(Euler { x: rad(0.0), y: rad(0.0), z: roll });
let expected = Matrix3::from(Euler { x: Rad(0.0), y: Rad(0.0), z: roll });
assert_approx_eq_eps!(found, expected, 0.001);
}
#[test] fn test_zero() { check_from_axis_angle_z(rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_z(rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_z(rad(-1.0)); }
#[test] fn test_zero() { check_from_axis_angle_z(Rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_z(Rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_z(Rad(-1.0)); }
}
mod from_axis_angle {
@ -361,13 +361,13 @@ pub mod matrix3 {
fn check_from_axis_angle_x(pitch: Rad<f32>) {
let found = Matrix3::from_axis_angle(Vector3::unit_x(), pitch);
let expected = Matrix3::from(Euler { x: pitch, y: rad(0.0), z: rad(0.0) });
let expected = Matrix3::from(Euler { x: pitch, y: Rad(0.0), z: Rad(0.0) });
assert_approx_eq_eps!(found, expected, 0.001);
}
#[test] fn test_zero() { check_from_axis_angle_x(rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_x(rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_x(rad(-1.0)); }
#[test] fn test_zero() { check_from_axis_angle_x(Rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_x(Rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_x(Rad(-1.0)); }
}
mod axis_y {
@ -375,13 +375,13 @@ pub mod matrix3 {
fn check_from_axis_angle_y(yaw: Rad<f32>) {
let found = Matrix3::from_axis_angle(Vector3::unit_y(), yaw);
let expected = Matrix3::from(Euler { x: rad(0.0), y: yaw, z: rad(0.0) });
let expected = Matrix3::from(Euler { x: Rad(0.0), y: yaw, z: Rad(0.0) });
assert_approx_eq_eps!(found, expected, 0.001);
}
#[test] fn test_zero() { check_from_axis_angle_y(rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_y(rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_y(rad(-1.0)); }
#[test] fn test_zero() { check_from_axis_angle_y(Rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_y(Rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_y(Rad(-1.0)); }
}
mod axis_z {
@ -389,13 +389,13 @@ pub mod matrix3 {
fn check_from_axis_angle_z(roll: Rad<f32>) {
let found = Matrix3::from_axis_angle(Vector3::unit_z(), roll);
let expected = Matrix3::from(Euler { x: rad(0.0), y: rad(0.0), z: roll });
let expected = Matrix3::from(Euler { x: Rad(0.0), y: Rad(0.0), z: roll });
assert_approx_eq_eps!(found, expected, 0.001);
}
#[test] fn test_zero() { check_from_axis_angle_z(rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_z(rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_z(rad(-1.0)); }
#[test] fn test_zero() { check_from_axis_angle_z(Rad(0.0)); }
#[test] fn test_pos_1() { check_from_axis_angle_z(Rad(1.0)); }
#[test] fn test_neg_1() { check_from_axis_angle_z(Rad(-1.0)); }
}
}
@ -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), deg(0.0), deg(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), deg(0.0), deg(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(deg(0.0), deg(90.0), deg(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(deg(0.0), deg(-90.0), deg(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(deg(0.0), deg(0.0), deg(90.0)));
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(deg(0.0), deg(0.0), deg(-90.0)));
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), deg(90.0), deg(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(deg(0.0), deg(90.0), deg(90.0)));
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));
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));
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));
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));
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));
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));
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

@ -43,12 +43,12 @@ mod operators {
#[test]
fn test_mul() {
impl_test_mul!(2.0f32, Quaternion::from(Euler { x: rad(1f32), y: rad(1f32), z: rad(1f32) }));
impl_test_mul!(2.0f32, Quaternion::from(Euler { x: Rad(1f32), y: Rad(1f32), z: Rad(1f32) }));
}
#[test]
fn test_div() {
impl_test_div!(2.0f32, Quaternion::from(Euler { x: rad(1f32), y: rad(1f32), z: rad(1f32) }));
impl_test_div!(2.0f32, Quaternion::from(Euler { x: Rad(1f32), y: Rad(1f32), z: Rad(1f32) }));
}
}
@ -63,17 +63,17 @@ mod to_from_euler {
const HPI: f32 = f32::consts::FRAC_PI_2;
#[test] fn test_zero() { check_euler(Euler { x: rad( 0f32), y: rad( 0f32), z: rad( 0f32) }); }
#[test] fn test_yaw_pos_1() { check_euler(Euler { x: rad( 0f32), y: rad( 1f32), z: rad( 0f32) }); }
#[test] fn test_yaw_neg_1() { check_euler(Euler { x: rad( 0f32), y: rad(-1f32), z: rad( 0f32) }); }
#[test] fn test_pitch_pos_1() { check_euler(Euler { x: rad( 1f32), y: rad( 0f32), z: rad( 0f32) }); }
#[test] fn test_pitch_neg_1() { check_euler(Euler { x: rad(-1f32), y: rad( 0f32), z: rad( 0f32) }); }
#[test] fn test_roll_pos_1() { check_euler(Euler { x: rad( 0f32), y: rad( 0f32), z: rad( 1f32) }); }
#[test] fn test_roll_neg_1() { check_euler(Euler { x: rad( 0f32), y: rad( 0f32), z: rad(-1f32) }); }
#[test] fn test_pitch_yaw_roll_pos_1() { check_euler(Euler { x: rad( 1f32), y: rad( 1f32), z: rad( 1f32) }); }
#[test] fn test_pitch_yaw_roll_neg_1() { check_euler(Euler { x: rad(-1f32), y: rad(-1f32), z: rad(-1f32) }); }
#[test] fn test_pitch_yaw_roll_pos_hp() { check_euler(Euler { x: rad( 0f32), y: rad( HPI), z: rad( 1f32) }); }
#[test] fn test_pitch_yaw_roll_neg_hp() { check_euler(Euler { x: rad( 0f32), y: rad( -HPI), z: rad( 1f32) }); }
#[test] fn test_zero() { check_euler(Euler { x: Rad( 0f32), y: Rad( 0f32), z: Rad( 0f32) }); }
#[test] fn test_yaw_pos_1() { check_euler(Euler { x: Rad( 0f32), y: Rad( 1f32), z: Rad( 0f32) }); }
#[test] fn test_yaw_neg_1() { check_euler(Euler { x: Rad( 0f32), y: Rad(-1f32), z: Rad( 0f32) }); }
#[test] fn test_pitch_pos_1() { check_euler(Euler { x: Rad( 1f32), y: Rad( 0f32), z: Rad( 0f32) }); }
#[test] fn test_pitch_neg_1() { check_euler(Euler { x: Rad(-1f32), y: Rad( 0f32), z: Rad( 0f32) }); }
#[test] fn test_roll_pos_1() { check_euler(Euler { x: Rad( 0f32), y: Rad( 0f32), z: Rad( 1f32) }); }
#[test] fn test_roll_neg_1() { check_euler(Euler { x: Rad( 0f32), y: Rad( 0f32), z: Rad(-1f32) }); }
#[test] fn test_pitch_yaw_roll_pos_1() { check_euler(Euler { x: Rad( 1f32), y: Rad( 1f32), z: Rad( 1f32) }); }
#[test] fn test_pitch_yaw_roll_neg_1() { check_euler(Euler { x: Rad(-1f32), y: Rad(-1f32), z: Rad(-1f32) }); }
#[test] fn test_pitch_yaw_roll_pos_hp() { check_euler(Euler { x: Rad( 0f32), y: Rad( HPI), z: Rad( 1f32) }); }
#[test] fn test_pitch_yaw_roll_neg_hp() { check_euler(Euler { x: Rad( 0f32), y: Rad( -HPI), z: Rad( 1f32) }); }
}
mod from {
@ -90,25 +90,25 @@ mod from {
// triggers: trace >= S::zero()
#[test]
fn test_positive_trace() {
check_with_euler(rad(0.0f32), rad(0.0), rad(0.0f32));
check_with_euler(Rad(0.0f32), Rad(0.0), Rad(0.0f32));
}
// triggers: (mat[0][0] > mat[1][1]) && (mat[0][0] > mat[2][2])
#[test]
fn test_xx_maximum() {
check_with_euler(rad(2.0f32), rad(1.0), rad(-1.2f32));
check_with_euler(Rad(2.0f32), Rad(1.0), Rad(-1.2f32));
}
// triggers: mat[1][1] > mat[2][2]
#[test]
fn test_yy_maximum() {
check_with_euler(rad(2.0f32), rad(1.0), rad(3.0f32));
check_with_euler(Rad(2.0f32), Rad(1.0), Rad(3.0f32));
}
// base case
#[test]
fn test_zz_maximum() {
check_with_euler(rad(1.0f32), rad(1.0), rad(3.0f32));
check_with_euler(Rad(1.0f32), Rad(1.0), Rad(3.0f32));
}
}
}
@ -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), deg(0.0), deg(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), deg(0.0), deg(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(deg(0.0), deg(90.0), deg(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(deg(0.0), deg(-90.0), deg(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(deg(0.0), deg(0.0), deg(90.0)));
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(deg(0.0), deg(0.0), deg(-90.0)));
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), deg(90.0), deg(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(deg(0.0), deg(90.0), deg(90.0)));
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));
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));
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));
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));
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));
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));
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))
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))
Rotation3::from_axis_angle(axis, Deg(30.0))
}
}

View file

@ -229,17 +229,17 @@ mod test_magnitude {
#[test]
fn test_angle() {
assert!(Vector2::new(1.0f64, 0.0f64).angle(Vector2::new(0.0f64, 1.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_2) ));
assert!(Vector2::new(10.0f64, 0.0f64).angle(Vector2::new(0.0f64, 5.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_2) ));
assert!(Vector2::new(-1.0f64, 0.0f64).angle(Vector2::new(0.0f64, 1.0f64)).approx_eq( &-rad(f64::consts::FRAC_PI_2) ));
assert!(Vector2::new(1.0f64, 0.0f64).angle(Vector2::new(0.0f64, 1.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_2) ));
assert!(Vector2::new(10.0f64, 0.0f64).angle(Vector2::new(0.0f64, 5.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_2) ));
assert!(Vector2::new(-1.0f64, 0.0f64).angle(Vector2::new(0.0f64, 1.0f64)).approx_eq( &-Rad(f64::consts::FRAC_PI_2) ));
assert!(Vector3::new(1.0f64, 0.0f64, 1.0f64).angle(Vector3::new(1.0f64, 1.0f64, 0.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_3) ));
assert!(Vector3::new(10.0f64, 0.0f64, 10.0f64).angle(Vector3::new(5.0f64, 5.0f64, 0.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_3) ));
assert!(Vector3::new(-1.0f64, 0.0f64, -1.0f64).angle(Vector3::new(1.0f64, -1.0f64, 0.0f64)).approx_eq( &rad(2.0f64 * f64::consts::FRAC_PI_3) ));
assert!(Vector3::new(1.0f64, 0.0f64, 1.0f64).angle(Vector3::new(1.0f64, 1.0f64, 0.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_3) ));
assert!(Vector3::new(10.0f64, 0.0f64, 10.0f64).angle(Vector3::new(5.0f64, 5.0f64, 0.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_3) ));
assert!(Vector3::new(-1.0f64, 0.0f64, -1.0f64).angle(Vector3::new(1.0f64, -1.0f64, 0.0f64)).approx_eq( &Rad(2.0f64 * f64::consts::FRAC_PI_3) ));
assert!(Vector4::new(1.0f64, 0.0f64, 1.0f64, 0.0f64).angle(Vector4::new(0.0f64, 1.0f64, 0.0f64, 1.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_2) ));
assert!(Vector4::new(10.0f64, 0.0f64, 10.0f64, 0.0f64).angle(Vector4::new(0.0f64, 5.0f64, 0.0f64, 5.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_2) ));
assert!(Vector4::new(-1.0f64, 0.0f64, -1.0f64, 0.0f64).angle(Vector4::new(0.0f64, 1.0f64, 0.0f64, 1.0f64)).approx_eq( &rad(f64::consts::FRAC_PI_2) ));
assert!(Vector4::new(1.0f64, 0.0f64, 1.0f64, 0.0f64).angle(Vector4::new(0.0f64, 1.0f64, 0.0f64, 1.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_2) ));
assert!(Vector4::new(10.0f64, 0.0f64, 10.0f64, 0.0f64).angle(Vector4::new(0.0f64, 5.0f64, 0.0f64, 5.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_2) ));
assert!(Vector4::new(-1.0f64, 0.0f64, -1.0f64, 0.0f64).angle(Vector4::new(0.0f64, 1.0f64, 0.0f64, 1.0f64)).approx_eq( &Rad(f64::consts::FRAC_PI_2) ));
}
#[test]