You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2012 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/Geometry>
  11. #include <Eigen/LU>
  12. #include <Eigen/SVD>
  13. template<typename Scalar>
  14. void verify_euler(const Matrix<Scalar,3,1>& ea, int i, int j, int k)
  15. {
  16. typedef Matrix<Scalar,3,3> Matrix3;
  17. typedef Matrix<Scalar,3,1> Vector3;
  18. typedef AngleAxis<Scalar> AngleAxisx;
  19. using std::abs;
  20. Matrix3 m(AngleAxisx(ea[0], Vector3::Unit(i)) * AngleAxisx(ea[1], Vector3::Unit(j)) * AngleAxisx(ea[2], Vector3::Unit(k)));
  21. Vector3 eabis = m.eulerAngles(i, j, k);
  22. Matrix3 mbis(AngleAxisx(eabis[0], Vector3::Unit(i)) * AngleAxisx(eabis[1], Vector3::Unit(j)) * AngleAxisx(eabis[2], Vector3::Unit(k)));
  23. VERIFY_IS_APPROX(m, mbis);
  24. /* If I==K, and ea[1]==0, then there no unique solution. */
  25. /* The remark apply in the case where I!=K, and |ea[1]| is close to pi/2. */
  26. if( (i!=k || ea[1]!=0) && (i==k || !internal::isApprox(abs(ea[1]),Scalar(EIGEN_PI/2),test_precision<Scalar>())) )
  27. VERIFY((ea-eabis).norm() <= test_precision<Scalar>());
  28. // approx_or_less_than does not work for 0
  29. VERIFY(0 < eabis[0] || test_isMuchSmallerThan(eabis[0], Scalar(1)));
  30. VERIFY_IS_APPROX_OR_LESS_THAN(eabis[0], Scalar(EIGEN_PI));
  31. VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[1]);
  32. VERIFY_IS_APPROX_OR_LESS_THAN(eabis[1], Scalar(EIGEN_PI));
  33. VERIFY_IS_APPROX_OR_LESS_THAN(-Scalar(EIGEN_PI), eabis[2]);
  34. VERIFY_IS_APPROX_OR_LESS_THAN(eabis[2], Scalar(EIGEN_PI));
  35. }
  36. template<typename Scalar> void check_all_var(const Matrix<Scalar,3,1>& ea)
  37. {
  38. verify_euler(ea, 0,1,2);
  39. verify_euler(ea, 0,1,0);
  40. verify_euler(ea, 0,2,1);
  41. verify_euler(ea, 0,2,0);
  42. verify_euler(ea, 1,2,0);
  43. verify_euler(ea, 1,2,1);
  44. verify_euler(ea, 1,0,2);
  45. verify_euler(ea, 1,0,1);
  46. verify_euler(ea, 2,0,1);
  47. verify_euler(ea, 2,0,2);
  48. verify_euler(ea, 2,1,0);
  49. verify_euler(ea, 2,1,2);
  50. }
  51. template<typename Scalar> void eulerangles()
  52. {
  53. typedef Matrix<Scalar,3,3> Matrix3;
  54. typedef Matrix<Scalar,3,1> Vector3;
  55. typedef Array<Scalar,3,1> Array3;
  56. typedef Quaternion<Scalar> Quaternionx;
  57. typedef AngleAxis<Scalar> AngleAxisx;
  58. Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
  59. Quaternionx q1;
  60. q1 = AngleAxisx(a, Vector3::Random().normalized());
  61. Matrix3 m;
  62. m = q1;
  63. Vector3 ea = m.eulerAngles(0,1,2);
  64. check_all_var(ea);
  65. ea = m.eulerAngles(0,1,0);
  66. check_all_var(ea);
  67. // Check with purely random Quaternion:
  68. q1.coeffs() = Quaternionx::Coefficients::Random().normalized();
  69. m = q1;
  70. ea = m.eulerAngles(0,1,2);
  71. check_all_var(ea);
  72. ea = m.eulerAngles(0,1,0);
  73. check_all_var(ea);
  74. // Check with random angles in range [0:pi]x[-pi:pi]x[-pi:pi].
  75. ea = (Array3::Random() + Array3(1,0,0))*Scalar(EIGEN_PI)*Array3(0.5,1,1);
  76. check_all_var(ea);
  77. ea[2] = ea[0] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
  78. check_all_var(ea);
  79. ea[0] = ea[1] = internal::random<Scalar>(0,Scalar(EIGEN_PI));
  80. check_all_var(ea);
  81. ea[1] = 0;
  82. check_all_var(ea);
  83. ea.head(2).setZero();
  84. check_all_var(ea);
  85. ea.setZero();
  86. check_all_var(ea);
  87. }
  88. void test_geo_eulerangles()
  89. {
  90. for(int i = 0; i < g_repeat; i++) {
  91. CALL_SUBTEST_1( eulerangles<float>() );
  92. CALL_SUBTEST_2( eulerangles<double>() );
  93. }
  94. }