2014-10-15 03:37:44 +08:00
|
|
|
extern crate "nalgebra" as na;
|
2015-02-17 20:32:54 +08:00
|
|
|
extern crate rand;
|
2014-10-15 03:37:44 +08:00
|
|
|
|
|
|
|
use na::{Pnt3, Vec3, Rot3, UnitQuat, Rotation};
|
2015-02-17 20:32:54 +08:00
|
|
|
use rand::random;
|
2014-10-15 03:37:44 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quat_as_mat() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2014-10-15 03:37:44 +08:00
|
|
|
let axis_angle: Vec3<f64> = random();
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&UnitQuat::new(axis_angle).to_rot(), &Rot3::new(axis_angle)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quat_mul_vec_or_pnt_as_mat() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2014-10-15 03:37:44 +08:00
|
|
|
let axis_angle: Vec3<f64> = random();
|
|
|
|
let vec: Vec3<f64> = random();
|
|
|
|
let pnt: Pnt3<f64> = random();
|
|
|
|
|
|
|
|
let mat = Rot3::new(axis_angle);
|
|
|
|
let quat = UnitQuat::new(axis_angle);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&(mat * vec), &(quat * vec)));
|
|
|
|
assert!(na::approx_eq(&(mat * pnt), &(quat * pnt)));
|
|
|
|
assert!(na::approx_eq(&(vec * mat), &(vec * quat)));
|
|
|
|
assert!(na::approx_eq(&(pnt * mat), &(pnt * quat)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quat_div_quat() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2014-10-15 03:37:44 +08:00
|
|
|
let axis_angle1: Vec3<f64> = random();
|
|
|
|
let axis_angle2: Vec3<f64> = random();
|
|
|
|
|
|
|
|
let r1 = Rot3::new(axis_angle1);
|
|
|
|
let r2 = na::inv(&Rot3::new(axis_angle2)).unwrap();
|
|
|
|
|
|
|
|
let q1 = UnitQuat::new(axis_angle1);
|
|
|
|
let q2 = UnitQuat::new(axis_angle2);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&(q1 / q2).to_rot(), &(r1 * r2)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quat_to_axis_angle() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2014-10-15 03:37:44 +08:00
|
|
|
let axis_angle: Vec3<f64> = random();
|
|
|
|
|
|
|
|
let q = UnitQuat::new(axis_angle);
|
|
|
|
|
2015-01-10 05:52:44 +08:00
|
|
|
println!("{:?} {:?}", q.rotation(), axis_angle);
|
2014-10-15 03:37:44 +08:00
|
|
|
assert!(na::approx_eq(&q.rotation(), &axis_angle))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quat_euler_angles() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2014-10-15 03:37:44 +08:00
|
|
|
let angles: Vec3<f64> = random();
|
|
|
|
|
|
|
|
let q = UnitQuat::new_with_euler_angles(angles.x, angles.y, angles.z);
|
|
|
|
let m = Rot3::new_with_euler_angles(angles.x, angles.y, angles.z);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&q.to_rot(), &m))
|
|
|
|
}
|
|
|
|
}
|