Test converting axis angles to quaternions

This commit is contained in:
Jordan Miner 2016-05-01 03:00:35 -05:00
parent 467e87f3d3
commit b56119a42f
3 changed files with 58 additions and 0 deletions

View file

@ -161,6 +161,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);
let _1subc = S::one() - c;
@ -266,6 +268,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);
let _1subc = S::one() - c;

View file

@ -72,6 +72,8 @@ pub trait Rotation3<S: BaseFloat>: Rotation<Point3<S>>
+ Into<Quaternion<S>>
+ From<Euler<Rad<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;
/// Create a rotation from an angle around the `x` axis (pitch).

View file

@ -168,3 +168,55 @@ mod rotate_from_euler {
assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec);
}
}
mod rotate_from_axis_angle {
use cgmath::*;
#[test]
fn test_x() {
let vec = vec3(0.0, 0.0, 1.0);
let rot = Quaternion::from_angle_x(deg(90.0).into());
assert_approx_eq!(vec3(0.0, -1.0, 0.0), rot * vec);
}
#[test]
fn test_y() {
let vec = vec3(0.0, 0.0, 1.0);
let rot = Quaternion::from_angle_y(deg(90.0).into());
assert_approx_eq!(vec3(1.0, 0.0, 0.0), rot * vec);
}
#[test]
fn test_z() {
let vec = vec3(1.0, 0.0, 0.0);
let rot = Quaternion::from_angle_z(deg(90.0).into());
assert_approx_eq!(vec3(0.0, 1.0, 0.0), rot * vec);
}
#[test]
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());
assert_approx_eq!(vec3(2.0f32.sqrt() / 2.0, -2.0f32.sqrt() / 2.0, 0.0), rot * vec);
}
#[test]
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());
assert_approx_eq!(vec3(0.0, -2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0), rot * vec);
}
#[test]
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());
assert_approx_eq!(vec3(-2.0f32.sqrt() / 2.0, 0.0, 2.0f32.sqrt() / 2.0), rot * vec);
}
}