Much better tests, and a little bit more functionality.

This commit is contained in:
Tal Hadad 2015-12-20 12:49:12 +02:00
parent 5e0a178df2
commit 6752a69aa5
3 changed files with 158 additions and 9 deletions

View File

@ -37,16 +37,26 @@ namespace Eigen
typedef Matrix<Scalar,3,1> Vector3;
typedef Quaternion<Scalar> QuaternionType;
typedef AngleAxis<Scalar> AngleAxisType;
static Vector3 HeadingAxisVector() {
return internal::NegativeIf<System::IsHeadingOpposite>::run(Vector3::Unit(System::HeadingAxisAbs - 1));
}
static Vector3 PitchAxisVector() {
return internal::NegativeIf<System::IsPitchOpposite>::run(Vector3::Unit(System::PitchAxisAbs - 1));
}
static Vector3 RollAxisVector() {
return internal::NegativeIf<System::IsRollOpposite>::run(Vector3::Unit(System::RollAxisAbs - 1));
}
protected:
private:
Vector3 m_angles;
public:
EulerAngles() {}
inline EulerAngles(Scalar a0, Scalar a1, Scalar a2) : m_angles(a0, a1, a2) {}
inline EulerAngles(Vector3 angles) : m_angles(angles) {}
inline EulerAngles(const QuaternionType& q) { *this = q; }
inline EulerAngles(const AngleAxisType& aa) { *this = aa; }
template<typename Derived>
@ -116,7 +126,7 @@ namespace Eigen
EulerAngles& operator=(const QuaternionType& q){
// TODO: Implement it in a better way
// According to http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
// we can compute only the needed matrix cells and then convert to euler angles.
// we can compute only the needed matrix cells and then convert to euler angles. (see ZYX example below)
// Currently we compute all matrix cells from quaternion.
fromRotationMatrix(q.toRotationMatrix());
@ -131,6 +141,8 @@ namespace Eigen
return *this;
}
// TODO: Support isApprox function
/** Set \c *this from AngleAxis \a ea.
*/
EulerAngles& operator=(const AngleAxisType& ea)

View File

@ -31,6 +31,26 @@ namespace Eigen
enum { value = -Num };
};
template <bool Cond>
struct NegativeIf
{
template <typename T>
static T run(const T& t)
{
return -t;
}
};
template <>
struct NegativeIf<false>
{
template <typename T>
static T run(const T& t)
{
return t;
}
};
template <bool Cond>
struct NegateIf
{
@ -45,7 +65,7 @@ namespace Eigen
struct NegateIf<false>
{
template <typename T>
static void run(T& t)
static void run(T&)
{
// no op
}
@ -113,7 +133,7 @@ namespace Eigen
};
template <typename Derived>
static void eulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar, 3, 1>& res, const MatrixBase<Derived>& mat, internal::true_type isTaitBryan)
static void eulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar, 3, 1>& res, const MatrixBase<Derived>& mat, internal::true_type /*isTaitBryan*/)
{
using std::atan2;
using std::sin;
@ -136,7 +156,7 @@ namespace Eigen
}
template <typename Derived>
static void eulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar,3,1>& res, const MatrixBase<Derived>& mat, internal::false_type isTaitBryan)
static void eulerAngles_imp(Matrix<typename MatrixBase<Derived>::Scalar,3,1>& res, const MatrixBase<Derived>& mat, internal::false_type /*isTaitBryan*/)
{
using std::atan2;
using std::sin;

View File

@ -11,8 +11,125 @@
#include <unsupported/Eigen/EulerAngles>
using namespace Eigen;
template<typename EulerSystem, typename Scalar>
void verify_euler(const Matrix<Scalar,3,1>& ea)
{
typedef EulerAngles<Scalar, EulerSystem> EulerAnglesType;
typedef Matrix<Scalar,3,3> Matrix3;
typedef Matrix<Scalar,3,1> Vector3;
typedef AngleAxis<Scalar> AngleAxisx;
using std::abs;
const int i = EulerSystem::HeadingAxisAbs - 1;
const int j = EulerSystem::PitchAxisAbs - 1;
const int k = EulerSystem::RollAxisAbs - 1;
const int iFactor = EulerSystem::IsHeadingOpposite ? -1 : 1;
const int jFactor = EulerSystem::IsPitchOpposite ? -1 : 1;
const int kFactor = EulerSystem::IsRollOpposite ? -1 : 1;
const Vector3 I = EulerAnglesType::HeadingAxisVector();
const Vector3 J = EulerAnglesType::PitchAxisVector();
const Vector3 K = EulerAnglesType::RollAxisVector();
EulerAnglesType e(ea[0], ea[1], ea[2]);
Matrix3 m(e);
Vector3 eabis = EulerAnglesType(m).coeffs();
Vector3 eabis2 = m.eulerAngles(i, j, k);
eabis2[0] *= iFactor;
eabis2[1] *= jFactor;
eabis2[2] *= kFactor;
VERIFY_IS_APPROX(eabis, eabis2);// Verify that our estimation is the same as m.eulerAngles() is
Matrix3 mbis(AngleAxisx(eabis[0], I) * AngleAxisx(eabis[1], J) * AngleAxisx(eabis[2], K));
VERIFY_IS_APPROX(m, mbis);
/* 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((ea-eabis).norm() <= test_precision<Scalar>());
// approx_or_less_than does not work for 0
VERIFY(0 < eabis[0] || test_isMuchSmallerThan(eabis[0], Scalar(1)));
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[0], Scalar(EIGEN_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[1]);
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[1], Scalar(EIGEN_PI));
VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[2]);
VERIFY_IS_APPROX_OR_LESS_THAN(eabis[2], Scalar(EIGEN_PI));
}
template<typename Scalar> void check_all_var(const Matrix<Scalar,3,1>& ea)
{
verify_euler<EulerSystemXYZ, Scalar>(ea);
verify_euler<EulerSystemXYX, Scalar>(ea);
verify_euler<EulerSystemXZY, Scalar>(ea);
verify_euler<EulerSystemXZX, Scalar>(ea);
verify_euler<EulerSystemYZX, Scalar>(ea);
verify_euler<EulerSystemYZY, Scalar>(ea);
verify_euler<EulerSystemYXZ, Scalar>(ea);
verify_euler<EulerSystemYXY, Scalar>(ea);
verify_euler<EulerSystemZXY, Scalar>(ea);
verify_euler<EulerSystemZXZ, Scalar>(ea);
verify_euler<EulerSystemZYX, Scalar>(ea);
verify_euler<EulerSystemZYZ, Scalar>(ea);
}
template<typename Scalar> void eulerangles()
{
typedef Matrix<Scalar,3,3> Matrix3;
typedef Matrix<Scalar,3,1> Vector3;
typedef Array<Scalar,3,1> Array3;
typedef Quaternion<Scalar> Quaternionx;
typedef AngleAxis<Scalar> AngleAxisx;
Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
Quaternionx q1;
q1 = AngleAxisx(a, Vector3::Random().normalized());
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);
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()
{
//CALL_SUBTEST( test_return_by_value(32) );
for(int i = 0; i < g_repeat; i++) {
CALL_SUBTEST_1( eulerangles<float>() );
CALL_SUBTEST_2( eulerangles<double>() );
}
}