2019-03-25 18:21:41 +08:00
|
|
|
use na::{Quaternion, RealField, UnitQuaternion, Vector2, Vector3};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn angle_2() {
|
|
|
|
let a = Vector2::new(4.0, 0.0);
|
|
|
|
let b = Vector2::new(9.0, 0.0);
|
|
|
|
|
|
|
|
assert_eq!(a.angle(&b), 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn angle_3() {
|
|
|
|
let a = Vector3::new(4.0, 0.0, 0.5);
|
|
|
|
let b = Vector3::new(8.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
assert_eq!(a.angle(&b), 0.0);
|
|
|
|
}
|
|
|
|
|
2018-12-05 04:23:21 +08:00
|
|
|
#[test]
|
2018-12-29 19:12:56 +08:00
|
|
|
fn quaternion_euler_angles_issue_494() {
|
2018-12-05 04:23:21 +08:00
|
|
|
let quat = UnitQuaternion::from_quaternion(Quaternion::new(
|
|
|
|
-0.10405792,
|
|
|
|
-0.6993922f32,
|
|
|
|
-0.10406871,
|
|
|
|
0.69942284,
|
|
|
|
));
|
|
|
|
let angs = quat.euler_angles();
|
|
|
|
assert_eq!(angs.0, 2.8461843);
|
|
|
|
assert_eq!(angs.1, f32::frac_pi_2());
|
|
|
|
assert_eq!(angs.2, 0.0);
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[cfg(feature = "proptest-support")]
|
|
|
|
mod proptest_tests {
|
2022-03-25 01:34:04 +08:00
|
|
|
use na::{self, Rotation, Rotation2, Rotation3, Unit};
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2018-10-22 13:00:10 +08:00
|
|
|
use std::f64;
|
2018-01-17 23:48:47 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
use crate::proptest::*;
|
|
|
|
use proptest::{prop_assert, prop_assert_eq, proptest};
|
|
|
|
|
|
|
|
proptest! {
|
2018-01-17 23:48:47 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Euler angles.
|
|
|
|
*
|
|
|
|
*/
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn from_euler_angles(r in PROPTEST_F64, p in PROPTEST_F64, y in PROPTEST_F64) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let roll = Rotation3::from_euler_angles(r, 0.0, 0.0);
|
|
|
|
let pitch = Rotation3::from_euler_angles(0.0, p, 0.0);
|
|
|
|
let yaw = Rotation3::from_euler_angles(0.0, 0.0, y);
|
|
|
|
|
|
|
|
let rpy = Rotation3::from_euler_angles(r, p, y);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert_eq!(roll[(0, 0)], 1.0); // rotation wrt. x axis.
|
|
|
|
prop_assert_eq!(pitch[(1, 1)], 1.0); // rotation wrt. y axis.
|
|
|
|
prop_assert_eq!(yaw[(2, 2)], 1.0); // rotation wrt. z axis.
|
|
|
|
prop_assert_eq!(yaw * pitch * roll, rpy);
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2018-01-10 04:15:57 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn euler_angles(r in PROPTEST_F64, p in PROPTEST_F64, y in PROPTEST_F64) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let rpy = Rotation3::from_euler_angles(r, p, y);
|
2018-12-29 19:12:56 +08:00
|
|
|
let (roll, pitch, yaw) = rpy.euler_angles();
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(Rotation3::from_euler_angles(roll, pitch, yaw), rpy, epsilon = 1.0e-7));
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2018-01-10 04:15:57 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn euler_angles_gimble_lock(r in PROPTEST_F64, y in PROPTEST_F64) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let pos = Rotation3::from_euler_angles(r, f64::frac_pi_2(), y);
|
|
|
|
let neg = Rotation3::from_euler_angles(r, -f64::frac_pi_2(), y);
|
2018-12-29 19:12:56 +08:00
|
|
|
let (pos_r, pos_p, pos_y) = pos.euler_angles();
|
|
|
|
let (neg_r, neg_p, neg_y) = neg.euler_angles();
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(Rotation3::from_euler_angles(pos_r, pos_p, pos_y), pos, epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!(Rotation3::from_euler_angles(neg_r, neg_p, neg_y), neg, epsilon = 1.0e-7));
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-01-17 23:48:47 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Inversion is transposition.
|
|
|
|
*
|
|
|
|
*/
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_inv_3(a in rotation3()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let ta = a.transpose();
|
|
|
|
let ia = a.inverse();
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert_eq!(ta, ia);
|
|
|
|
prop_assert!(relative_eq!(&ta * &a, Rotation3::identity(), epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!(&ia * a, Rotation3::identity(), epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!( a * &ta, Rotation3::identity(), epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!( a * ia, Rotation3::identity(), epsilon = 1.0e-7));
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_inv_2(a in rotation2()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let ta = a.transpose();
|
|
|
|
let ia = a.inverse();
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert_eq!(ta, ia);
|
|
|
|
prop_assert!(relative_eq!(&ta * &a, Rotation2::identity(), epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!(&ia * a, Rotation2::identity(), epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!( a * &ta, Rotation2::identity(), epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!( a * ia, Rotation2::identity(), epsilon = 1.0e-7));
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-01-17 23:48:47 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Angle between vectors.
|
|
|
|
*
|
|
|
|
*/
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn angle_is_commutative_2(a in vector2(), b in vector2()) {
|
|
|
|
prop_assert_eq!(a.angle(&b), b.angle(&a))
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn angle_is_commutative_3(a in vector3(), b in vector3()) {
|
|
|
|
prop_assert_eq!(a.angle(&b), b.angle(&a))
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-01-17 23:48:47 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Rotation matrix between vectors.
|
|
|
|
*
|
|
|
|
*/
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_between_is_anticommutative_2(a in vector2(), b in vector2()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let rab = Rotation2::rotation_between(&a, &b);
|
|
|
|
let rba = Rotation2::rotation_between(&b, &a);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(rab * rba, Rotation2::identity()));
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_between_is_anticommutative_3(a in vector3(), b in vector3()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let rots = (Rotation3::rotation_between(&a, &b), Rotation3::rotation_between(&b, &a));
|
|
|
|
if let (Some(rab), Some(rba)) = rots {
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(rab * rba, Rotation3::identity(), epsilon = 1.0e-7));
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_between_is_identity(v2 in vector2(), v3 in vector3()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let vv2 = 3.42 * v2;
|
|
|
|
let vv3 = 4.23 * v3;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(v2.angle(&vv2), 0.0, epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!(v3.angle(&vv3), 0.0, epsilon = 1.0e-7));
|
|
|
|
prop_assert!(relative_eq!(Rotation2::rotation_between(&v2, &vv2), Rotation2::identity()));
|
|
|
|
prop_assert_eq!(Rotation3::rotation_between(&v3, &vv3).unwrap(), Rotation3::identity());
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_between_2(a in vector2(), b in vector2()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
if !relative_eq!(a.angle(&b), 0.0, epsilon = 1.0e-7) {
|
|
|
|
let r = Rotation2::rotation_between(&a, &b);
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!((r * a).angle(&b), 0.0, epsilon = 1.0e-7))
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn rotation_between_3(a in vector3(), b in vector3()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
if !relative_eq!(a.angle(&b), 0.0, epsilon = 1.0e-7) {
|
|
|
|
let r = Rotation3::rotation_between(&a, &b).unwrap();
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!((r * a).angle(&b), 0.0, epsilon = 1.0e-7))
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
2018-01-17 23:48:47 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Rotation construction.
|
|
|
|
*
|
|
|
|
*/
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn new_rotation_2(angle in PROPTEST_F64) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let r = Rotation2::new(angle);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
let angle = na::wrap(angle, -f64::pi(), f64::pi());
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(r.angle(), angle, epsilon = 1.0e-7))
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn new_rotation_3(axisangle in vector3()) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let r = Rotation3::new(axisangle);
|
|
|
|
|
|
|
|
if let Some((axis, angle)) = Unit::try_new_and_get(axisangle, 0.0) {
|
|
|
|
let angle = na::wrap(angle, -f64::pi(), f64::pi());
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!((relative_eq!(r.angle(), angle, epsilon = 1.0e-7) &&
|
2018-01-17 23:48:47 +08:00
|
|
|
relative_eq!(r.axis().unwrap(), axis, epsilon = 1.0e-7)) ||
|
|
|
|
(relative_eq!(r.angle(), -angle, epsilon = 1.0e-7) &&
|
2021-03-01 00:52:14 +08:00
|
|
|
relative_eq!(r.axis().unwrap(), -axis, epsilon = 1.0e-7)))
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
|
|
|
else {
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert_eq!(r, Rotation3::identity())
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-01-17 23:48:47 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Rotation pow.
|
|
|
|
*
|
|
|
|
*/
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn powf_rotation_2(angle in PROPTEST_F64, pow in PROPTEST_F64) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let r = Rotation2::new(angle).powf(pow);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-01-17 23:48:47 +08:00
|
|
|
let angle = na::wrap(angle, -f64::pi(), f64::pi());
|
2016-12-05 05:44:42 +08:00
|
|
|
let pangle = na::wrap(angle * pow, -f64::pi(), f64::pi());
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(r.angle(), pangle, epsilon = 1.0e-7));
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
2018-01-17 23:48:47 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn powf_rotation_3(axisangle in vector3(), pow in PROPTEST_F64) {
|
2018-01-17 23:48:47 +08:00
|
|
|
let r = Rotation3::new(axisangle).powf(pow);
|
|
|
|
|
|
|
|
if let Some((axis, angle)) = Unit::try_new_and_get(axisangle, 0.0) {
|
|
|
|
let angle = na::wrap(angle, -f64::pi(), f64::pi());
|
|
|
|
let pangle = na::wrap(angle * pow, -f64::pi(), f64::pi());
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!((relative_eq!(r.angle(), pangle, epsilon = 1.0e-7) &&
|
2018-01-17 23:48:47 +08:00
|
|
|
relative_eq!(r.axis().unwrap(), axis, epsilon = 1.0e-7)) ||
|
|
|
|
(relative_eq!(r.angle(), -pangle, epsilon = 1.0e-7) &&
|
2021-03-01 00:52:14 +08:00
|
|
|
relative_eq!(r.axis().unwrap(), -axis, epsilon = 1.0e-7)));
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
|
|
|
else {
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert_eq!(r, Rotation3::identity())
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
2022-03-25 01:34:04 +08:00
|
|
|
|
|
|
|
// macro_rules! gen_pof_rotation_test {
|
|
|
|
// ($(
|
|
|
|
// fn $powf_rot_n:ident($($v1:ident in $vec1:ident(), $v2:ident in $vec2:ident()),*);
|
|
|
|
// )*) => {$
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn $powf_rot_n(
|
|
|
|
// $($v1 in $vec1(), $v2 in $vec2(),)*
|
|
|
|
// pow in PROPTEST_F64
|
|
|
|
// ) {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// )*}
|
|
|
|
// }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn powf_rotation_4(
|
|
|
|
v1 in vector4(), v2 in vector4(),
|
|
|
|
v3 in vector4(), v4 in vector4(),
|
|
|
|
pow in PROPTEST_F64
|
|
|
|
) {
|
|
|
|
|
|
|
|
use nalgebra::*;
|
|
|
|
use num_traits::Zero;
|
|
|
|
|
|
|
|
type Rotation4<T> = Rotation<T,4>;
|
|
|
|
|
|
|
|
//make an orthonormal basis
|
|
|
|
let mut basis = [v1,v2,v3,v4];
|
|
|
|
Vector::orthonormalize(&mut basis);
|
|
|
|
let [v1,v2,v3,v4] = basis;
|
|
|
|
|
|
|
|
//"wedge" the vectors to make two 2-blades representing two rotation planes
|
|
|
|
//since we start with vector pairs, each bivector is guaranteed to be simple
|
|
|
|
let mut b1 = v1.transpose().kronecker(&v2) - v2.transpose().kronecker(&v1);
|
|
|
|
let mut b2 = v3.transpose().kronecker(&v4) - v4.transpose().kronecker(&v3);
|
|
|
|
|
|
|
|
//condition b1
|
|
|
|
if let Some((unit, norm)) = Unit::try_new_and_get(b1, 0.0) {
|
|
|
|
//every component is duplicated once, so there's an extra factor or sqrt(2) in the norm
|
|
|
|
//and wrap angle into the correct range
|
|
|
|
let mut angle = norm / 2.0f64.sqrt();
|
|
|
|
angle = na::wrap(angle, -f64::pi(), f64::pi());
|
|
|
|
b1 = unit.into_inner() * angle * 2.0f64.sqrt();
|
|
|
|
}
|
|
|
|
|
|
|
|
//condition b2
|
|
|
|
if let Some((unit, norm)) = Unit::try_new_and_get(b2, 0.0) {
|
|
|
|
let mut angle = norm / 2.0f64.sqrt();
|
|
|
|
angle = na::wrap(angle, -f64::pi(), f64::pi());
|
|
|
|
b2 = unit.into_inner() * angle * 2.0f64.sqrt();
|
|
|
|
}
|
|
|
|
|
|
|
|
let bivector = b1+b2;
|
|
|
|
|
|
|
|
println!("b:{:.3}", bivector);
|
|
|
|
|
|
|
|
let r1 = Rotation4::from_matrix_unchecked(bivector.exp());
|
|
|
|
let r2 = Rotation4::from_matrix_unchecked((bivector * pow).exp());
|
|
|
|
|
|
|
|
// println!("{}{}", r1, r2);
|
|
|
|
// println!("{}", r1.general_pow(pow));
|
|
|
|
|
|
|
|
prop_assert!(
|
|
|
|
relative_eq!(r1.general_pow(pow), r2, epsilon=1e-7)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|