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-18 03:23:47 +08:00
|
|
|
// Unfortunately, we need to specialize it in order to work. (We could add it in main.h test framework)
|
|
|
|
template <typename Scalar, class System>
|
|
|
|
bool verifyIsApprox(const Eigen::EulerAngles<Scalar, System>& a, const Eigen::EulerAngles<Scalar, System>& b)
|
|
|
|
{
|
|
|
|
return verifyIsApprox(a.angles(), b.angles());
|
|
|
|
}
|
|
|
|
|
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) \
|
2016-10-16 19:39:26 +08:00
|
|
|
do { \
|
|
|
|
VERIFY_IS_APPROX_OR_LESS_THAN(a, x); \
|
|
|
|
VERIFY_IS_APPROX_OR_LESS_THAN(x, b); \
|
|
|
|
} while(0)
|
2016-10-14 21:03:28 +08:00
|
|
|
|
2016-10-16 19:39:26 +08:00
|
|
|
const char X = EULER_X;
|
|
|
|
const char Y = EULER_Y;
|
|
|
|
const char Z = EULER_Z;
|
|
|
|
|
2016-10-18 03:23:47 +08:00
|
|
|
template<typename Scalar, class EulerSystem>
|
2016-10-16 19:39:26 +08:00
|
|
|
void verify_euler(const EulerAngles<Scalar, EulerSystem>& e)
|
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-18 01:42:08 +08:00
|
|
|
// It's very important calc the acceptable precision depending on the distance from the pole.
|
|
|
|
const Scalar longitudeRadius = std::abs(
|
|
|
|
EulerSystem::IsTaitBryan ?
|
|
|
|
std::cos(e.beta()) :
|
|
|
|
std::sin(e.beta())
|
|
|
|
);
|
2016-10-19 04:24:57 +08:00
|
|
|
Scalar precision = test_precision<Scalar>() / longitudeRadius;
|
2016-10-18 01:42:08 +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-16 19:39:26 +08:00
|
|
|
if (!EulerSystem::IsBetaOpposite)
|
|
|
|
{
|
|
|
|
betaRangeStart = 0;
|
|
|
|
betaRangeEnd = PI;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
betaRangeStart = -PI;
|
|
|
|
betaRangeEnd = 0;
|
|
|
|
}
|
2015-12-20 22:24:53 +08:00
|
|
|
}
|
|
|
|
|
2019-01-25 21:54:39 +08:00
|
|
|
const Vector3 I_ = EulerAnglesType::AlphaAxisVector();
|
|
|
|
const Vector3 J_ = EulerAnglesType::BetaAxisVector();
|
|
|
|
const Vector3 K_ = EulerAnglesType::GammaAxisVector();
|
2016-10-18 03:23:47 +08:00
|
|
|
|
|
|
|
// Is approx checks
|
|
|
|
VERIFY(e.isApprox(e));
|
|
|
|
VERIFY_IS_APPROX(e, e);
|
|
|
|
VERIFY_IS_NOT_APPROX(e, EulerAnglesType(e.alpha() + ONE, e.beta() + ONE, e.gamma() + ONE));
|
2016-10-14 05:45:51 +08:00
|
|
|
|
2016-10-16 19:39:26 +08:00
|
|
|
const Matrix3 m(e);
|
|
|
|
VERIFY_IS_APPROX(Scalar(m.determinant()), ONE);
|
2016-10-14 05:45:51 +08:00
|
|
|
|
2016-10-19 04:24:57 +08:00
|
|
|
EulerAnglesType ebis(m);
|
|
|
|
|
|
|
|
// When no roll(acting like polar representation), we have the best precision.
|
|
|
|
// One of those cases is when the Euler angles are on the pole, and because it's singular case,
|
|
|
|
// the computation returns no roll.
|
|
|
|
if (ebis.beta() == 0)
|
|
|
|
precision = test_precision<Scalar>();
|
2015-12-20 22:24:53 +08:00
|
|
|
|
|
|
|
// Check that eabis in range
|
2016-10-19 04:24:57 +08:00
|
|
|
VERIFY_APPROXED_RANGE(-PI, ebis.alpha(), PI);
|
|
|
|
VERIFY_APPROXED_RANGE(betaRangeStart, ebis.beta(), betaRangeEnd);
|
|
|
|
VERIFY_APPROXED_RANGE(-PI, ebis.gamma(), PI);
|
2016-10-14 05:45:51 +08:00
|
|
|
|
2019-01-25 21:54:39 +08:00
|
|
|
const Matrix3 mbis(AngleAxisType(ebis.alpha(), I_) * AngleAxisType(ebis.beta(), J_) * AngleAxisType(ebis.gamma(), K_));
|
2016-10-16 19:39:26 +08:00
|
|
|
VERIFY_IS_APPROX(Scalar(mbis.determinant()), ONE);
|
2016-10-19 04:24:57 +08:00
|
|
|
VERIFY_IS_APPROX(mbis, ebis.toRotationMatrix());
|
2016-10-16 19:39:26 +08:00
|
|
|
/*std::cout << "===================\n" <<
|
|
|
|
"e: " << e << std::endl <<
|
|
|
|
"eabis: " << eabis.transpose() << std::endl <<
|
|
|
|
"m: " << m << std::endl <<
|
|
|
|
"mbis: " << mbis << std::endl <<
|
|
|
|
"X: " << (m * Vector3::UnitX()).transpose() << std::endl <<
|
|
|
|
"X: " << (mbis * Vector3::UnitX()).transpose() << std::endl;*/
|
2016-10-18 01:42:08 +08:00
|
|
|
VERIFY(m.isApprox(mbis, precision));
|
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
|
|
|
|
|
2016-10-16 19:39:26 +08:00
|
|
|
// TODO: Make this test work well, and use range saturation function.
|
|
|
|
/*// 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.
|
|
|
|
if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision<Scalar>())) )
|
|
|
|
VERIFY_IS_APPROX(ea, eabis);*/
|
2015-12-20 22:24:53 +08:00
|
|
|
|
|
|
|
// Quaternions
|
2016-10-16 19:39:26 +08:00
|
|
|
const QuaternionType q(e);
|
2016-10-19 04:24:57 +08:00
|
|
|
ebis = q;
|
|
|
|
const QuaternionType qbis(ebis);
|
2016-10-18 01:42:08 +08:00
|
|
|
VERIFY(internal::isApprox<Scalar>(std::abs(q.dot(qbis)), ONE, precision));
|
2016-10-14 05:45:51 +08:00
|
|
|
//VERIFY_IS_APPROX(eabis, eabis2);// Verify that the euler angles are still the same
|
2016-10-18 03:23:47 +08:00
|
|
|
|
|
|
|
// A suggestion for simple product test when will be supported.
|
|
|
|
/*EulerAnglesType e2(PI/2, PI/2, PI/2);
|
|
|
|
Matrix3 m2(e2);
|
|
|
|
VERIFY_IS_APPROX(e*e2, m*m2);*/
|
2015-12-20 18:49:12 +08:00
|
|
|
}
|
|
|
|
|
2016-10-16 19:39:26 +08:00
|
|
|
template<signed char A, signed char B, signed char C, typename Scalar>
|
|
|
|
void verify_euler_vec(const Matrix<Scalar,3,1>& ea)
|
|
|
|
{
|
|
|
|
verify_euler(EulerAngles<Scalar, EulerSystem<A, B, C> >(ea[0], ea[1], ea[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<signed char A, signed char B, signed char C, typename Scalar>
|
|
|
|
void verify_euler_all_neg(const Matrix<Scalar,3,1>& ea)
|
|
|
|
{
|
|
|
|
verify_euler_vec<+A,+B,+C>(ea);
|
|
|
|
verify_euler_vec<+A,+B,-C>(ea);
|
|
|
|
verify_euler_vec<+A,-B,+C>(ea);
|
|
|
|
verify_euler_vec<+A,-B,-C>(ea);
|
|
|
|
|
|
|
|
verify_euler_vec<-A,+B,+C>(ea);
|
|
|
|
verify_euler_vec<-A,+B,-C>(ea);
|
|
|
|
verify_euler_vec<-A,-B,+C>(ea);
|
|
|
|
verify_euler_vec<-A,-B,-C>(ea);
|
|
|
|
}
|
|
|
|
|
2015-12-20 18:49:12 +08:00
|
|
|
template<typename Scalar> void check_all_var(const Matrix<Scalar,3,1>& ea)
|
|
|
|
{
|
2016-10-16 19:39:26 +08:00
|
|
|
verify_euler_all_neg<X,Y,Z>(ea);
|
|
|
|
verify_euler_all_neg<X,Y,X>(ea);
|
|
|
|
verify_euler_all_neg<X,Z,Y>(ea);
|
|
|
|
verify_euler_all_neg<X,Z,X>(ea);
|
|
|
|
|
|
|
|
verify_euler_all_neg<Y,Z,X>(ea);
|
|
|
|
verify_euler_all_neg<Y,Z,Y>(ea);
|
|
|
|
verify_euler_all_neg<Y,X,Z>(ea);
|
|
|
|
verify_euler_all_neg<Y,X,Y>(ea);
|
|
|
|
|
|
|
|
verify_euler_all_neg<Z,X,Y>(ea);
|
|
|
|
verify_euler_all_neg<Z,X,Z>(ea);
|
|
|
|
verify_euler_all_neg<Z,Y,X>(ea);
|
|
|
|
verify_euler_all_neg<Z,Y,Z>(ea);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Scalar> void check_singular_cases(const Scalar& singularBeta)
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar,3,1> Vector3;
|
|
|
|
const Scalar PI = Scalar(EIGEN_PI);
|
|
|
|
|
2016-10-19 04:24:57 +08:00
|
|
|
for (Scalar epsilon = NumTraits<Scalar>::epsilon(); epsilon < 1; epsilon *= Scalar(1.2))
|
2016-10-18 01:42:08 +08:00
|
|
|
{
|
|
|
|
check_all_var(Vector3(PI/4, singularBeta, PI/3));
|
|
|
|
check_all_var(Vector3(PI/4, singularBeta - epsilon, PI/3));
|
|
|
|
check_all_var(Vector3(PI/4, singularBeta - Scalar(1.5)*epsilon, PI/3));
|
|
|
|
check_all_var(Vector3(PI/4, singularBeta - 2*epsilon, PI/3));
|
|
|
|
check_all_var(Vector3(PI*Scalar(0.8), singularBeta - epsilon, Scalar(0.9)*PI));
|
|
|
|
check_all_var(Vector3(PI*Scalar(-0.9), singularBeta + epsilon, PI*Scalar(0.3)));
|
|
|
|
check_all_var(Vector3(PI*Scalar(-0.6), singularBeta + Scalar(1.5)*epsilon, PI*Scalar(0.3)));
|
|
|
|
check_all_var(Vector3(PI*Scalar(-0.5), singularBeta + 2*epsilon, PI*Scalar(0.4)));
|
|
|
|
check_all_var(Vector3(PI*Scalar(0.9), singularBeta + epsilon, Scalar(0.8)*PI));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This one for sanity, it had a problem with near pole cases in float scalar.
|
|
|
|
check_all_var(Vector3(PI*Scalar(0.8), singularBeta - Scalar(1E-6), Scalar(0.9)*PI));
|
2016-10-16 19:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Scalar> void eulerangles_manual()
|
|
|
|
{
|
|
|
|
typedef Matrix<Scalar,3,1> Vector3;
|
2018-08-25 06:02:46 +08:00
|
|
|
typedef Matrix<Scalar,Dynamic,1> VectorX;
|
2016-10-16 19:39:26 +08:00
|
|
|
const Vector3 Zero = Vector3::Zero();
|
|
|
|
const Scalar PI = Scalar(EIGEN_PI);
|
|
|
|
|
|
|
|
check_all_var(Zero);
|
|
|
|
|
|
|
|
// singular cases
|
|
|
|
check_singular_cases(PI/2);
|
|
|
|
check_singular_cases(-PI/2);
|
|
|
|
|
|
|
|
check_singular_cases(Scalar(0));
|
|
|
|
check_singular_cases(Scalar(-0));
|
|
|
|
|
|
|
|
check_singular_cases(PI);
|
|
|
|
check_singular_cases(-PI);
|
|
|
|
|
|
|
|
// non-singular cases
|
2019-05-10 20:57:05 +08:00
|
|
|
VectorX alpha = VectorX::LinSpaced(20, Scalar(-0.99) * PI, PI);
|
|
|
|
VectorX beta = VectorX::LinSpaced(20, Scalar(-0.49) * PI, Scalar(0.49) * PI);
|
|
|
|
VectorX gamma = VectorX::LinSpaced(20, Scalar(-0.99) * PI, PI);
|
2016-10-16 19:39:26 +08:00
|
|
|
for (int i = 0; i < alpha.size(); ++i) {
|
|
|
|
for (int j = 0; j < beta.size(); ++j) {
|
|
|
|
for (int k = 0; k < gamma.size(); ++k) {
|
2018-08-25 06:02:46 +08:00
|
|
|
check_all_var(Vector3(alpha(i), beta(j), gamma(k)));
|
2016-10-16 19:39:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-20 18:49:12 +08:00
|
|
|
}
|
|
|
|
|
2016-10-16 19:39:26 +08:00
|
|
|
template<typename Scalar> void eulerangles_rand()
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(EulerAngles)
|
2015-12-20 18:49:12 +08:00
|
|
|
{
|
2016-10-18 03:23:47 +08:00
|
|
|
// Simple cast test
|
|
|
|
EulerAnglesXYZd onesEd(1, 1, 1);
|
|
|
|
EulerAnglesXYZf onesEf = onesEd.cast<float>();
|
|
|
|
VERIFY_IS_APPROX(onesEd, onesEf.cast<double>());
|
2017-08-31 01:26:30 +08:00
|
|
|
|
|
|
|
// Simple Construction from Vector3 test
|
|
|
|
VERIFY_IS_APPROX(onesEd, EulerAnglesXYZd(Vector3d::Ones()));
|
2016-10-18 03:23:47 +08:00
|
|
|
|
2016-10-16 19:39:26 +08:00
|
|
|
CALL_SUBTEST_1( eulerangles_manual<float>() );
|
|
|
|
CALL_SUBTEST_2( eulerangles_manual<double>() );
|
|
|
|
|
2015-12-20 18:49:12 +08:00
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2016-10-16 19:39:26 +08:00
|
|
|
CALL_SUBTEST_3( eulerangles_rand<float>() );
|
|
|
|
CALL_SUBTEST_4( eulerangles_rand<double>() );
|
2015-12-20 18:49:12 +08:00
|
|
|
}
|
2016-10-16 19:39:26 +08:00
|
|
|
|
|
|
|
// TODO: Add tests for auto diff
|
|
|
|
// TODO: Add tests for complex numbers
|
2015-09-27 21:51:24 +08:00
|
|
|
}
|