2015-09-27 21:51:24 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2015 Tal Hadad <tal_hd@hotmail.com>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
|
|
#include <unsupported/Eigen/EulerAngles>
|
|
|
|
|
|
2015-12-20 18:49:12 +08:00
|
|
|
using namespace Eigen;
|
|
|
|
|
|
2016-10-14 21:03:28 +08:00
|
|
|
// Verify that x is in the approxed range [a, b]
|
|
|
|
|
#define VERIFY_APPROXED_RANGE(a, x, b) \
|
|
|
|
|
do { \
|
|
|
|
|
VERIFY_IS_APPROX_OR_LESS_THAN(a, x); \
|
|
|
|
|
VERIFY_IS_APPROX_OR_LESS_THAN(x, b); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
2015-12-20 18:49:12 +08:00
|
|
|
template<typename EulerSystem, typename Scalar>
|
2016-10-14 21:03:28 +08:00
|
|
|
void verify_euler(const Matrix<Scalar,3,1>& ea)
|
2015-12-20 18:49:12 +08:00
|
|
|
{
|
|
|
|
|
typedef EulerAngles<Scalar, EulerSystem> EulerAnglesType;
|
|
|
|
|
typedef Matrix<Scalar,3,3> Matrix3;
|
|
|
|
|
typedef Matrix<Scalar,3,1> Vector3;
|
2015-12-20 22:24:53 +08:00
|
|
|
typedef Quaternion<Scalar> QuaternionType;
|
|
|
|
|
typedef AngleAxis<Scalar> AngleAxisType;
|
2015-12-20 18:49:12 +08:00
|
|
|
|
2016-10-14 21:03:28 +08:00
|
|
|
const Scalar ONE = Scalar(1);
|
|
|
|
|
const Scalar HALF_PI = Scalar(EIGEN_PI / 2);
|
|
|
|
|
const Scalar PI = Scalar(EIGEN_PI);
|
2015-12-20 22:24:53 +08:00
|
|
|
|
2016-10-14 21:03:28 +08:00
|
|
|
Scalar betaRangeStart, betaRangeEnd;
|
2016-10-14 05:45:51 +08:00
|
|
|
if (EulerSystem::IsTaitBryan)
|
2015-12-20 22:24:53 +08:00
|
|
|
{
|
2016-10-14 21:03:28 +08:00
|
|
|
betaRangeStart = -HALF_PI;
|
|
|
|
|
betaRangeEnd = HALF_PI;
|
2015-12-20 22:24:53 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-10-14 21:03:28 +08:00
|
|
|
betaRangeStart = -PI;
|
|
|
|
|
betaRangeEnd = PI;
|
2015-12-20 22:24:53 +08:00
|
|
|
}
|
|
|
|
|
|
2016-06-03 03:12:57 +08:00
|
|
|
const Vector3 I = EulerAnglesType::AlphaAxisVector();
|
|
|
|
|
const Vector3 J = EulerAnglesType::BetaAxisVector();
|
|
|
|
|
const Vector3 K = EulerAnglesType::GammaAxisVector();
|
2015-12-20 18:49:12 +08:00
|
|
|
|
|
|
|
|
EulerAnglesType e(ea[0], ea[1], ea[2]);
|
2016-10-14 05:45:51 +08:00
|
|
|
|
2015-12-20 18:49:12 +08:00
|
|
|
Matrix3 m(e);
|
2016-10-14 05:45:51 +08:00
|
|
|
|
2016-10-14 21:03:28 +08:00
|
|
|
Vector3 eabis = static_cast<EulerAnglesType>(m).angles();
|
2015-12-20 22:24:53 +08:00
|
|
|
|
|
|
|
|
// Check that eabis in range
|
2016-10-14 21:03:28 +08:00
|
|
|
VERIFY_APPROXED_RANGE(-PI, eabis[0], PI);
|
|
|
|
|
VERIFY_APPROXED_RANGE(betaRangeStart, eabis[1], betaRangeEnd);
|
|
|
|
|
VERIFY_APPROXED_RANGE(-PI, eabis[2], PI);
|
2016-10-14 05:45:51 +08:00
|
|
|
|
2015-12-20 22:24:53 +08:00
|
|
|
Matrix3 mbis(AngleAxisType(eabis[0], I) * AngleAxisType(eabis[1], J) * AngleAxisType(eabis[2], K));
|
2015-12-20 18:49:12 +08:00
|
|
|
VERIFY_IS_APPROX(m, mbis);
|
2016-10-14 05:45:51 +08:00
|
|
|
|
|
|
|
|
// Test if ea and eabis are the same
|
|
|
|
|
// Need to check both singular and non-singular cases
|
|
|
|
|
// There are two singular cases.
|
|
|
|
|
// 1. When I==K and sin(ea(1)) == 0
|
|
|
|
|
// 2. When I!=K and cos(ea(1)) == 0
|
|
|
|
|
|
|
|
|
|
// Tests that are only relevant for no positive range
|
|
|
|
|
/*if (!(positiveRangeAlpha || positiveRangeGamma))
|
2015-12-20 22:24:53 +08:00
|
|
|
{
|
2016-10-14 05:45:51 +08:00
|
|
|
// If I==K, and ea[1]==0, then there no unique solution.
|
|
|
|
|
// The remark apply in the case where I!=K, and |ea[1]| is close to pi/2.
|
2015-12-20 22:24:53 +08:00
|
|
|
if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision<Scalar>())) )
|
|
|
|
|
VERIFY((ea-eabis).norm() <= test_precision<Scalar>());
|
|
|
|
|
|
|
|
|
|
// approx_or_less_than does not work for 0
|
2016-10-14 21:03:28 +08:00
|
|
|
VERIFY(0 < eabis[0] || VERIFY_IS_MUCH_SMALLER_THAN(eabis[0], Scalar(1)));
|
2016-10-14 05:45:51 +08:00
|
|
|
}*/
|
2015-12-20 22:24:53 +08:00
|
|
|
|
|
|
|
|
// Quaternions
|
|
|
|
|
QuaternionType q(e);
|
2016-10-14 21:03:28 +08:00
|
|
|
eabis = static_cast<EulerAnglesType>(q).angles();
|
2016-10-14 05:45:51 +08:00
|
|
|
QuaternionType qbis(AngleAxisType(eabis[0], I) * AngleAxisType(eabis[1], J) * AngleAxisType(eabis[2], K));
|
2016-10-14 21:03:28 +08:00
|
|
|
VERIFY_IS_APPROX(std::abs(q.dot(qbis)), ONE);
|
2016-10-14 05:45:51 +08:00
|
|
|
//VERIFY_IS_APPROX(eabis, eabis2);// Verify that the euler angles are still the same
|
2015-12-20 18:49:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Scalar> void check_all_var(const Matrix<Scalar,3,1>& ea)
|
|
|
|
|
{
|
2015-12-20 22:24:53 +08:00
|
|
|
verify_euler<EulerSystemXYZ>(ea);
|
|
|
|
|
verify_euler<EulerSystemXYX>(ea);
|
|
|
|
|
verify_euler<EulerSystemXZY>(ea);
|
|
|
|
|
verify_euler<EulerSystemXZX>(ea);
|
|
|
|
|
|
|
|
|
|
verify_euler<EulerSystemYZX>(ea);
|
|
|
|
|
verify_euler<EulerSystemYZY>(ea);
|
|
|
|
|
verify_euler<EulerSystemYXZ>(ea);
|
|
|
|
|
verify_euler<EulerSystemYXY>(ea);
|
|
|
|
|
|
|
|
|
|
verify_euler<EulerSystemZXY>(ea);
|
|
|
|
|
verify_euler<EulerSystemZXZ>(ea);
|
|
|
|
|
verify_euler<EulerSystemZYX>(ea);
|
|
|
|
|
verify_euler<EulerSystemZYZ>(ea);
|
2016-10-14 21:03:28 +08:00
|
|
|
|
|
|
|
|
// TODO: Test negative axes as well! (only test if the angles get negative when needed)
|
2015-12-20 18:49:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Scalar> void eulerangles()
|
2015-09-27 21:51:24 +08:00
|
|
|
{
|
2015-12-20 18:49:12 +08:00
|
|
|
typedef Matrix<Scalar,3,3> Matrix3;
|
|
|
|
|
typedef Matrix<Scalar,3,1> Vector3;
|
|
|
|
|
typedef Array<Scalar,3,1> Array3;
|
|
|
|
|
typedef Quaternion<Scalar> Quaternionx;
|
2015-12-20 22:24:53 +08:00
|
|
|
typedef AngleAxis<Scalar> AngleAxisType;
|
2015-12-20 18:49:12 +08:00
|
|
|
|
|
|
|
|
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
|
|
|
|
|
Quaternionx q1;
|
2015-12-20 22:24:53 +08:00
|
|
|
q1 = AngleAxisType(a, Vector3::Random().normalized());
|
2015-12-20 18:49:12 +08:00
|
|
|
Matrix3 m;
|
|
|
|
|
m = q1;
|
|
|
|
|
|
|
|
|
|
Vector3 ea = m.eulerAngles(0,1,2);
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
ea = m.eulerAngles(0,1,0);
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
|
|
|
|
|
// Check with purely random Quaternion:
|
|
|
|
|
q1.coeffs() = Quaternionx::Coefficients::Random().normalized();
|
|
|
|
|
m = q1;
|
|
|
|
|
ea = m.eulerAngles(0,1,2);
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
ea = m.eulerAngles(0,1,0);
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
|
|
|
|
|
// Check with random angles in range [0:pi]x[-pi:pi]x[-pi:pi].
|
|
|
|
|
ea = (Array3::Random() + Array3(1,0,0))*Scalar(EIGEN_PI)*Array3(0.5,1,1);
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
|
|
|
|
|
ea[2] = ea[0] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
|
|
|
|
|
check_all_var(ea);
|
2015-09-27 21:51:24 +08:00
|
|
|
|
2015-12-20 18:49:12 +08:00
|
|
|
ea[0] = ea[1] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
|
|
|
|
|
ea[1] = 0;
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
|
|
|
|
|
ea.head(2).setZero();
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
|
|
|
|
|
ea.setZero();
|
|
|
|
|
check_all_var(ea);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_EulerAngles()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
|
|
|
|
CALL_SUBTEST_1( eulerangles<float>() );
|
|
|
|
|
CALL_SUBTEST_2( eulerangles<double>() );
|
|
|
|
|
}
|
2015-09-27 21:51:24 +08:00
|
|
|
}
|