Add tests for `::angle_between` and `::rotation_between`.

This commit is contained in:
Sébastien Crozet 2015-06-01 22:37:54 +02:00
parent ea86890647
commit b859b32804
3 changed files with 128 additions and 3 deletions

View File

@ -2,8 +2,8 @@ extern crate nalgebra as na;
extern crate rand;
use rand::random;
use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot3, Persp3, PerspMat3, Ortho3, OrthoMat3,
DMat, DVec, Row, Col, BaseFloat};
use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, PerspMat3, Ortho3,
OrthoMat3, DMat, DVec, Row, Col, BaseFloat};
macro_rules! test_inv_mat_impl(
($t: ty) => (
@ -153,6 +153,48 @@ fn test_inv_rotation3() {
}
}
#[test]
fn test_rot3_rotation_between() {
let r1: Rot3<f64> = random();
let r2: Rot3<f64> = random();
let delta = na::rotation_between(&r1, &r2);
assert!(na::approx_eq(&(delta * r1), &r2))
}
#[test]
fn test_rot3_angle_between() {
let r1: Rot3<f64> = random();
let r2: Rot3<f64> = random();
let delta = na::rotation_between(&r1, &r2);
let delta_angle = na::angle_between(&r1, &r2);
assert!(na::approx_eq(&na::norm(&na::rotation(&delta)), &delta_angle))
}
#[test]
fn test_rot2_rotation_between() {
let r1: Rot2<f64> = random();
let r2: Rot2<f64> = random();
let delta = na::rotation_between(&r1, &r2);
assert!(na::approx_eq(&(delta * r1), &r2))
}
#[test]
fn test_rot2_angle_between() {
let r1: Rot2<f64> = random();
let r2: Rot2<f64> = random();
let delta = na::rotation_between(&r1, &r2);
let delta_angle = na::angle_between(&r1, &r2);
assert!(na::approx_eq(&na::norm(&na::rotation(&delta)), &delta_angle))
}
#[test]
fn test_mean_dmat() {
let mat = DMat::from_row_vec(

View File

@ -69,3 +69,24 @@ fn test_quat_euler_angles() {
assert!(na::approx_eq(&q.to_rot(), &m))
}
}
#[test]
fn test_quat_rotation_between() {
let q1: UnitQuat<f64> = random();
let q2: UnitQuat<f64> = random();
let delta = na::rotation_between(&q1, &q2);
assert!(na::approx_eq(&(delta * q1), &q2))
}
#[test]
fn test_quat_angle_between() {
let q1: UnitQuat<f64> = random();
let q2: UnitQuat<f64> = random();
let delta = na::rotation_between(&q1, &q2);
let delta_angle = na::angle_between(&q1, &q2);
assert!(na::approx_eq(&na::norm(&na::rotation(&delta)), &delta_angle))
}

View File

@ -2,7 +2,7 @@ extern crate nalgebra as na;
extern crate rand;
use rand::random;
use na::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6, Mat3, Iterable, IterableMut};
use na::{Vec0, Vec1, Vec2, Vec3, Vec4, Vec5, Vec6, Mat3, Rot2, Rot3, Iterable, IterableMut};
macro_rules! test_iterator_impl(
($t: ty, $n: ty) => (
@ -317,3 +317,65 @@ fn test_outer_vec3() {
8.0, 10.0, 12.0,
12.0, 15.0, 18.0));
}
#[test]
fn test_vec3_rotation_between() {
for _ in (0usize .. 10000) {
let v1: Vec3<f64> = random();
let mut v2: Vec3<f64> = random();
v2 = na::normalize(&v2) * na::norm(&v1);
let rot = na::rotation_between(&v1, &v2);
assert!(na::approx_eq(&(rot * v1), &v2))
}
}
#[test]
fn test_vec3_angle_between() {
for _ in (0usize .. 10000) {
let vec: Vec3<f64> = random();
let other: Vec3<f64> = random();
// Ensure the axis we are using is orthogonal to `vec`.
let axis_ang = na::cross(&vec, &other);
let ang = na::norm(&axis_ang);
let rot = Rot3::new(axis_ang);
let delta = na::angle_between(&vec, &(rot * vec));
assert!(na::approx_eq(&ang, &delta))
}
}
#[test]
fn test_vec2_rotation_between() {
for _ in (0usize .. 10000) {
let v1: Vec2<f64> = random();
let mut v2: Vec2<f64> = random();
v2 = na::normalize(&v2) * na::norm(&v1);
let rot = na::rotation_between(&v1, &v2);
assert!(na::approx_eq(&(rot * v1), &v2))
}
}
#[test]
fn test_vec2_angle_between() {
for _ in (0usize .. 10000) {
let axis_ang: Vec1<f64> = random();
let ang = na::norm(&axis_ang);
let rot: Rot2<f64> = Rot2::new(axis_ang);
let vec: Vec2<f64> = random();
let delta = na::angle_between(&vec, &(rot * vec));
assert!(na::approx_eq(&ang, &delta))
}
}