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.

255 lines
8.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2009 Mathieu Gautier <mathieu.gautier@cea.fr>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <Eigen/Geometry>
  12. #include <Eigen/LU>
  13. #include <Eigen/SVD>
  14. template<typename T> T bounded_acos(T v)
  15. {
  16. using std::acos;
  17. using std::min;
  18. using std::max;
  19. return acos((max)(T(-1),(min)(v,T(1))));
  20. }
  21. template<typename QuatType> void check_slerp(const QuatType& q0, const QuatType& q1)
  22. {
  23. typedef typename QuatType::Scalar Scalar;
  24. typedef Matrix<Scalar,3,1> VectorType;
  25. typedef AngleAxis<Scalar> AA;
  26. Scalar largeEps = test_precision<Scalar>();
  27. Scalar theta_tot = AA(q1*q0.inverse()).angle();
  28. if(theta_tot>M_PI)
  29. theta_tot = 2.*M_PI-theta_tot;
  30. for(Scalar t=0; t<=1.001; t+=0.1)
  31. {
  32. QuatType q = q0.slerp(t,q1);
  33. Scalar theta = AA(q*q0.inverse()).angle();
  34. VERIFY(internal::abs(q.norm() - 1) < largeEps);
  35. if(theta_tot==0) VERIFY(theta_tot==0);
  36. else VERIFY(internal::abs(theta/theta_tot - t) < largeEps);
  37. }
  38. }
  39. template<typename Scalar, int Options> void quaternion(void)
  40. {
  41. /* this test covers the following files:
  42. Quaternion.h
  43. */
  44. typedef Matrix<Scalar,3,3> Matrix3;
  45. typedef Matrix<Scalar,3,1> Vector3;
  46. typedef Matrix<Scalar,4,1> Vector4;
  47. typedef Quaternion<Scalar,Options> Quaternionx;
  48. typedef AngleAxis<Scalar> AngleAxisx;
  49. Scalar largeEps = test_precision<Scalar>();
  50. if (internal::is_same<Scalar,float>::value)
  51. largeEps = 1e-3f;
  52. Scalar eps = internal::random<Scalar>() * Scalar(1e-2);
  53. Vector3 v0 = Vector3::Random(),
  54. v1 = Vector3::Random(),
  55. v2 = Vector3::Random(),
  56. v3 = Vector3::Random();
  57. Scalar a = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI)),
  58. b = internal::random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
  59. // Quaternion: Identity(), setIdentity();
  60. Quaternionx q1, q2;
  61. q2.setIdentity();
  62. VERIFY_IS_APPROX(Quaternionx(Quaternionx::Identity()).coeffs(), q2.coeffs());
  63. q1.coeffs().setRandom();
  64. VERIFY_IS_APPROX(q1.coeffs(), (q1*q2).coeffs());
  65. // concatenation
  66. q1 *= q2;
  67. q1 = AngleAxisx(a, v0.normalized());
  68. q2 = AngleAxisx(a, v1.normalized());
  69. // angular distance
  70. Scalar refangle = internal::abs(AngleAxisx(q1.inverse()*q2).angle());
  71. if (refangle>Scalar(M_PI))
  72. refangle = Scalar(2)*Scalar(M_PI) - refangle;
  73. if((q1.coeffs()-q2.coeffs()).norm() > 10*largeEps)
  74. {
  75. VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(q1.angularDistance(q2) - refangle), Scalar(1));
  76. }
  77. // rotation matrix conversion
  78. VERIFY_IS_APPROX(q1 * v2, q1.toRotationMatrix() * v2);
  79. VERIFY_IS_APPROX(q1 * q2 * v2,
  80. q1.toRotationMatrix() * q2.toRotationMatrix() * v2);
  81. VERIFY( (q2*q1).isApprox(q1*q2, largeEps)
  82. || !(q2 * q1 * v2).isApprox(q1.toRotationMatrix() * q2.toRotationMatrix() * v2));
  83. q2 = q1.toRotationMatrix();
  84. VERIFY_IS_APPROX(q1*v1,q2*v1);
  85. // angle-axis conversion
  86. AngleAxisx aa = AngleAxisx(q1);
  87. VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
  88. // Do not execute the test if the rotation angle is almost zero, or
  89. // the rotation axis and v1 are almost parallel.
  90. if (internal::abs(aa.angle()) > 5*test_precision<Scalar>()
  91. && (aa.axis() - v1.normalized()).norm() < 1.99
  92. && (aa.axis() + v1.normalized()).norm() < 1.99)
  93. {
  94. VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1);
  95. }
  96. // from two vector creation
  97. VERIFY_IS_APPROX( v2.normalized(),(q2.setFromTwoVectors(v1, v2)*v1).normalized());
  98. VERIFY_IS_APPROX( v1.normalized(),(q2.setFromTwoVectors(v1, v1)*v1).normalized());
  99. VERIFY_IS_APPROX(-v1.normalized(),(q2.setFromTwoVectors(v1,-v1)*v1).normalized());
  100. if (internal::is_same<Scalar,double>::value)
  101. {
  102. v3 = (v1.array()+eps).matrix();
  103. VERIFY_IS_APPROX( v3.normalized(),(q2.setFromTwoVectors(v1, v3)*v1).normalized());
  104. VERIFY_IS_APPROX(-v3.normalized(),(q2.setFromTwoVectors(v1,-v3)*v1).normalized());
  105. }
  106. // from two vector creation static function
  107. VERIFY_IS_APPROX( v2.normalized(),(Quaternionx::FromTwoVectors(v1, v2)*v1).normalized());
  108. VERIFY_IS_APPROX( v1.normalized(),(Quaternionx::FromTwoVectors(v1, v1)*v1).normalized());
  109. VERIFY_IS_APPROX(-v1.normalized(),(Quaternionx::FromTwoVectors(v1,-v1)*v1).normalized());
  110. if (internal::is_same<Scalar,double>::value)
  111. {
  112. v3 = (v1.array()+eps).matrix();
  113. VERIFY_IS_APPROX( v3.normalized(),(Quaternionx::FromTwoVectors(v1, v3)*v1).normalized());
  114. VERIFY_IS_APPROX(-v3.normalized(),(Quaternionx::FromTwoVectors(v1,-v3)*v1).normalized());
  115. }
  116. // inverse and conjugate
  117. VERIFY_IS_APPROX(q1 * (q1.inverse() * v1), v1);
  118. VERIFY_IS_APPROX(q1 * (q1.conjugate() * v1), v1);
  119. // test casting
  120. Quaternion<float> q1f = q1.template cast<float>();
  121. VERIFY_IS_APPROX(q1f.template cast<Scalar>(),q1);
  122. Quaternion<double> q1d = q1.template cast<double>();
  123. VERIFY_IS_APPROX(q1d.template cast<Scalar>(),q1);
  124. // test bug 369 - improper alignment.
  125. Quaternionx *q = new Quaternionx;
  126. delete q;
  127. q1 = AngleAxisx(a, v0.normalized());
  128. q2 = AngleAxisx(b, v1.normalized());
  129. check_slerp(q1,q2);
  130. q1 = AngleAxisx(b, v1.normalized());
  131. q2 = AngleAxisx(b+M_PI, v1.normalized());
  132. check_slerp(q1,q2);
  133. q1 = AngleAxisx(b, v1.normalized());
  134. q2 = AngleAxisx(-b, -v1.normalized());
  135. check_slerp(q1,q2);
  136. q1.coeffs() = Vector4::Random().normalized();
  137. q2.coeffs() = -q1.coeffs();
  138. check_slerp(q1,q2);
  139. }
  140. template<typename Scalar> void mapQuaternion(void){
  141. typedef Map<Quaternion<Scalar>, Aligned> MQuaternionA;
  142. typedef Map<Quaternion<Scalar> > MQuaternionUA;
  143. typedef Map<const Quaternion<Scalar> > MCQuaternionUA;
  144. typedef Quaternion<Scalar> Quaternionx;
  145. EIGEN_ALIGN16 Scalar array1[4];
  146. EIGEN_ALIGN16 Scalar array2[4];
  147. EIGEN_ALIGN16 Scalar array3[4+1];
  148. Scalar* array3unaligned = array3+1;
  149. // std::cerr << array1 << " " << array2 << " " << array3 << "\n";
  150. MQuaternionA(array1).coeffs().setRandom();
  151. (MQuaternionA(array2)) = MQuaternionA(array1);
  152. (MQuaternionUA(array3unaligned)) = MQuaternionA(array1);
  153. Quaternionx q1 = MQuaternionA(array1);
  154. Quaternionx q2 = MQuaternionA(array2);
  155. Quaternionx q3 = MQuaternionUA(array3unaligned);
  156. Quaternionx q4 = MCQuaternionUA(array3unaligned);
  157. VERIFY_IS_APPROX(q1.coeffs(), q2.coeffs());
  158. VERIFY_IS_APPROX(q1.coeffs(), q3.coeffs());
  159. VERIFY_IS_APPROX(q4.coeffs(), q3.coeffs());
  160. #ifdef EIGEN_VECTORIZE
  161. if(internal::packet_traits<Scalar>::Vectorizable)
  162. VERIFY_RAISES_ASSERT((MQuaternionA(array3unaligned)));
  163. #endif
  164. }
  165. template<typename Scalar> void quaternionAlignment(void){
  166. typedef Quaternion<Scalar,AutoAlign> QuaternionA;
  167. typedef Quaternion<Scalar,DontAlign> QuaternionUA;
  168. EIGEN_ALIGN16 Scalar array1[4];
  169. EIGEN_ALIGN16 Scalar array2[4];
  170. EIGEN_ALIGN16 Scalar array3[4+1];
  171. Scalar* arrayunaligned = array3+1;
  172. QuaternionA *q1 = ::new(reinterpret_cast<void*>(array1)) QuaternionA;
  173. QuaternionUA *q2 = ::new(reinterpret_cast<void*>(array2)) QuaternionUA;
  174. QuaternionUA *q3 = ::new(reinterpret_cast<void*>(arrayunaligned)) QuaternionUA;
  175. q1->coeffs().setRandom();
  176. *q2 = *q1;
  177. *q3 = *q1;
  178. VERIFY_IS_APPROX(q1->coeffs(), q2->coeffs());
  179. VERIFY_IS_APPROX(q1->coeffs(), q3->coeffs());
  180. #if defined(EIGEN_VECTORIZE) && EIGEN_ALIGN_STATICALLY
  181. if(internal::packet_traits<Scalar>::Vectorizable)
  182. VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(arrayunaligned)) QuaternionA));
  183. #endif
  184. }
  185. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  186. {
  187. // there's a lot that we can't test here while still having this test compile!
  188. // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
  189. // CMake can help with that.
  190. // verify that map-to-const don't have LvalueBit
  191. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  192. VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
  193. VERIFY( !(internal::traits<Map<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
  194. VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
  195. VERIFY( !(Map<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
  196. }
  197. void test_geo_quaternion()
  198. {
  199. for(int i = 0; i < g_repeat; i++) {
  200. CALL_SUBTEST_1(( quaternion<float,AutoAlign>() ));
  201. CALL_SUBTEST_1( check_const_correctness(Quaternionf()) );
  202. CALL_SUBTEST_2(( quaternion<double,AutoAlign>() ));
  203. CALL_SUBTEST_2( check_const_correctness(Quaterniond()) );
  204. CALL_SUBTEST_3(( quaternion<float,DontAlign>() ));
  205. CALL_SUBTEST_4(( quaternion<double,DontAlign>() ));
  206. CALL_SUBTEST_5(( quaternionAlignment<float>() ));
  207. CALL_SUBTEST_6(( quaternionAlignment<double>() ));
  208. CALL_SUBTEST_1( mapQuaternion<float>() );
  209. CALL_SUBTEST_2( mapQuaternion<double>() );
  210. }
  211. }