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.

290 lines
9.9 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. using std::abs;
  24. typedef typename QuatType::Scalar Scalar;
  25. typedef AngleAxis<Scalar> AA;
  26. Scalar largeEps = test_precision<Scalar>();
  27. Scalar theta_tot = AA(q1*q0.inverse()).angle();
  28. if(theta_tot>EIGEN_PI)
  29. theta_tot = Scalar(2.*EIGEN_PI)-theta_tot;
  30. for(Scalar t=0; t<=Scalar(1.001); t+=Scalar(0.1))
  31. {
  32. QuatType q = q0.slerp(t,q1);
  33. Scalar theta = AA(q*q0.inverse()).angle();
  34. VERIFY(abs(q.norm() - 1) < largeEps);
  35. if(theta_tot==0) VERIFY(theta_tot==0);
  36. else VERIFY(abs(theta - t * theta_tot) < 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. using std::abs;
  45. typedef Matrix<Scalar,3,1> Vector3;
  46. typedef Matrix<Scalar,3,3> Matrix3;
  47. typedef Matrix<Scalar,4,1> Vector4;
  48. typedef Quaternion<Scalar,Options> Quaternionx;
  49. typedef AngleAxis<Scalar> AngleAxisx;
  50. Scalar largeEps = test_precision<Scalar>();
  51. if (internal::is_same<Scalar,float>::value)
  52. largeEps = 1e-3f;
  53. Scalar eps = internal::random<Scalar>() * Scalar(1e-2);
  54. Vector3 v0 = Vector3::Random(),
  55. v1 = Vector3::Random(),
  56. v2 = Vector3::Random(),
  57. v3 = Vector3::Random();
  58. Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI)),
  59. b = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
  60. // Quaternion: Identity(), setIdentity();
  61. Quaternionx q1, q2;
  62. q2.setIdentity();
  63. VERIFY_IS_APPROX(Quaternionx(Quaternionx::Identity()).coeffs(), q2.coeffs());
  64. q1.coeffs().setRandom();
  65. VERIFY_IS_APPROX(q1.coeffs(), (q1*q2).coeffs());
  66. // concatenation
  67. q1 *= q2;
  68. q1 = AngleAxisx(a, v0.normalized());
  69. q2 = AngleAxisx(a, v1.normalized());
  70. // angular distance
  71. Scalar refangle = abs(AngleAxisx(q1.inverse()*q2).angle());
  72. if (refangle>Scalar(EIGEN_PI))
  73. refangle = Scalar(2)*Scalar(EIGEN_PI) - refangle;
  74. if((q1.coeffs()-q2.coeffs()).norm() > 10*largeEps)
  75. {
  76. VERIFY_IS_MUCH_SMALLER_THAN(abs(q1.angularDistance(q2) - refangle), Scalar(1));
  77. }
  78. // rotation matrix conversion
  79. VERIFY_IS_APPROX(q1 * v2, q1.toRotationMatrix() * v2);
  80. VERIFY_IS_APPROX(q1 * q2 * v2,
  81. q1.toRotationMatrix() * q2.toRotationMatrix() * v2);
  82. VERIFY( (q2*q1).isApprox(q1*q2, largeEps)
  83. || !(q2 * q1 * v2).isApprox(q1.toRotationMatrix() * q2.toRotationMatrix() * v2));
  84. q2 = q1.toRotationMatrix();
  85. VERIFY_IS_APPROX(q1*v1,q2*v1);
  86. Matrix3 rot1(q1);
  87. VERIFY_IS_APPROX(q1*v1,rot1*v1);
  88. Quaternionx q3(rot1.transpose()*rot1);
  89. VERIFY_IS_APPROX(q3*v1,v1);
  90. // angle-axis conversion
  91. AngleAxisx aa = AngleAxisx(q1);
  92. VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
  93. // Do not execute the test if the rotation angle is almost zero, or
  94. // the rotation axis and v1 are almost parallel.
  95. if (abs(aa.angle()) > 5*test_precision<Scalar>()
  96. && (aa.axis() - v1.normalized()).norm() < 1.99
  97. && (aa.axis() + v1.normalized()).norm() < 1.99)
  98. {
  99. VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1);
  100. }
  101. // from two vector creation
  102. VERIFY_IS_APPROX( v2.normalized(),(q2.setFromTwoVectors(v1, v2)*v1).normalized());
  103. VERIFY_IS_APPROX( v1.normalized(),(q2.setFromTwoVectors(v1, v1)*v1).normalized());
  104. VERIFY_IS_APPROX(-v1.normalized(),(q2.setFromTwoVectors(v1,-v1)*v1).normalized());
  105. if (internal::is_same<Scalar,double>::value)
  106. {
  107. v3 = (v1.array()+eps).matrix();
  108. VERIFY_IS_APPROX( v3.normalized(),(q2.setFromTwoVectors(v1, v3)*v1).normalized());
  109. VERIFY_IS_APPROX(-v3.normalized(),(q2.setFromTwoVectors(v1,-v3)*v1).normalized());
  110. }
  111. // from two vector creation static function
  112. VERIFY_IS_APPROX( v2.normalized(),(Quaternionx::FromTwoVectors(v1, v2)*v1).normalized());
  113. VERIFY_IS_APPROX( v1.normalized(),(Quaternionx::FromTwoVectors(v1, v1)*v1).normalized());
  114. VERIFY_IS_APPROX(-v1.normalized(),(Quaternionx::FromTwoVectors(v1,-v1)*v1).normalized());
  115. if (internal::is_same<Scalar,double>::value)
  116. {
  117. v3 = (v1.array()+eps).matrix();
  118. VERIFY_IS_APPROX( v3.normalized(),(Quaternionx::FromTwoVectors(v1, v3)*v1).normalized());
  119. VERIFY_IS_APPROX(-v3.normalized(),(Quaternionx::FromTwoVectors(v1,-v3)*v1).normalized());
  120. }
  121. // inverse and conjugate
  122. VERIFY_IS_APPROX(q1 * (q1.inverse() * v1), v1);
  123. VERIFY_IS_APPROX(q1 * (q1.conjugate() * v1), v1);
  124. // test casting
  125. Quaternion<float> q1f = q1.template cast<float>();
  126. VERIFY_IS_APPROX(q1f.template cast<Scalar>(),q1);
  127. Quaternion<double> q1d = q1.template cast<double>();
  128. VERIFY_IS_APPROX(q1d.template cast<Scalar>(),q1);
  129. // test bug 369 - improper alignment.
  130. Quaternionx *q = new Quaternionx;
  131. delete q;
  132. q1 = AngleAxisx(a, v0.normalized());
  133. q2 = AngleAxisx(b, v1.normalized());
  134. check_slerp(q1,q2);
  135. q1 = AngleAxisx(b, v1.normalized());
  136. q2 = AngleAxisx(b+Scalar(EIGEN_PI), v1.normalized());
  137. check_slerp(q1,q2);
  138. q1 = AngleAxisx(b, v1.normalized());
  139. q2 = AngleAxisx(-b, -v1.normalized());
  140. check_slerp(q1,q2);
  141. q1.coeffs() = Vector4::Random().normalized();
  142. q2.coeffs() = -q1.coeffs();
  143. check_slerp(q1,q2);
  144. }
  145. template<typename Scalar> void mapQuaternion(void){
  146. typedef Map<Quaternion<Scalar>, Aligned> MQuaternionA;
  147. typedef Map<const Quaternion<Scalar>, Aligned> MCQuaternionA;
  148. typedef Map<Quaternion<Scalar> > MQuaternionUA;
  149. typedef Map<const Quaternion<Scalar> > MCQuaternionUA;
  150. typedef Quaternion<Scalar> Quaternionx;
  151. typedef Matrix<Scalar,3,1> Vector3;
  152. typedef AngleAxis<Scalar> AngleAxisx;
  153. Vector3 v0 = Vector3::Random(),
  154. v1 = Vector3::Random();
  155. Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
  156. EIGEN_ALIGN_MAX Scalar array1[4];
  157. EIGEN_ALIGN_MAX Scalar array2[4];
  158. EIGEN_ALIGN_MAX Scalar array3[4+1];
  159. Scalar* array3unaligned = array3+1;
  160. MQuaternionA mq1(array1);
  161. MCQuaternionA mcq1(array1);
  162. MQuaternionA mq2(array2);
  163. MQuaternionUA mq3(array3unaligned);
  164. MCQuaternionUA mcq3(array3unaligned);
  165. // std::cerr << array1 << " " << array2 << " " << array3 << "\n";
  166. mq1 = AngleAxisx(a, v0.normalized());
  167. mq2 = mq1;
  168. mq3 = mq1;
  169. Quaternionx q1 = mq1;
  170. Quaternionx q2 = mq2;
  171. Quaternionx q3 = mq3;
  172. Quaternionx q4 = MCQuaternionUA(array3unaligned);
  173. VERIFY_IS_APPROX(q1.coeffs(), q2.coeffs());
  174. VERIFY_IS_APPROX(q1.coeffs(), q3.coeffs());
  175. VERIFY_IS_APPROX(q4.coeffs(), q3.coeffs());
  176. #ifdef EIGEN_VECTORIZE
  177. if(internal::packet_traits<Scalar>::Vectorizable)
  178. VERIFY_RAISES_ASSERT((MQuaternionA(array3unaligned)));
  179. #endif
  180. VERIFY_IS_APPROX(mq1 * (mq1.inverse() * v1), v1);
  181. VERIFY_IS_APPROX(mq1 * (mq1.conjugate() * v1), v1);
  182. VERIFY_IS_APPROX(mcq1 * (mcq1.inverse() * v1), v1);
  183. VERIFY_IS_APPROX(mcq1 * (mcq1.conjugate() * v1), v1);
  184. VERIFY_IS_APPROX(mq3 * (mq3.inverse() * v1), v1);
  185. VERIFY_IS_APPROX(mq3 * (mq3.conjugate() * v1), v1);
  186. VERIFY_IS_APPROX(mcq3 * (mcq3.inverse() * v1), v1);
  187. VERIFY_IS_APPROX(mcq3 * (mcq3.conjugate() * v1), v1);
  188. VERIFY_IS_APPROX(mq1*mq2, q1*q2);
  189. VERIFY_IS_APPROX(mq3*mq2, q3*q2);
  190. VERIFY_IS_APPROX(mcq1*mq2, q1*q2);
  191. VERIFY_IS_APPROX(mcq3*mq2, q3*q2);
  192. }
  193. template<typename Scalar> void quaternionAlignment(void){
  194. typedef Quaternion<Scalar,AutoAlign> QuaternionA;
  195. typedef Quaternion<Scalar,DontAlign> QuaternionUA;
  196. EIGEN_ALIGN_MAX Scalar array1[4];
  197. EIGEN_ALIGN_MAX Scalar array2[4];
  198. EIGEN_ALIGN_MAX Scalar array3[4+1];
  199. Scalar* arrayunaligned = array3+1;
  200. QuaternionA *q1 = ::new(reinterpret_cast<void*>(array1)) QuaternionA;
  201. QuaternionUA *q2 = ::new(reinterpret_cast<void*>(array2)) QuaternionUA;
  202. QuaternionUA *q3 = ::new(reinterpret_cast<void*>(arrayunaligned)) QuaternionUA;
  203. q1->coeffs().setRandom();
  204. *q2 = *q1;
  205. *q3 = *q1;
  206. VERIFY_IS_APPROX(q1->coeffs(), q2->coeffs());
  207. VERIFY_IS_APPROX(q1->coeffs(), q3->coeffs());
  208. #if defined(EIGEN_VECTORIZE) && EIGEN_MAX_STATIC_ALIGN_BYTES>0
  209. if(internal::packet_traits<Scalar>::Vectorizable && internal::packet_traits<Scalar>::size<=4)
  210. VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(arrayunaligned)) QuaternionA));
  211. #endif
  212. }
  213. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  214. {
  215. // there's a lot that we can't test here while still having this test compile!
  216. // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
  217. // CMake can help with that.
  218. // verify that map-to-const don't have LvalueBit
  219. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  220. VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
  221. VERIFY( !(internal::traits<Map<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
  222. VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
  223. VERIFY( !(Map<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
  224. }
  225. void test_geo_quaternion()
  226. {
  227. for(int i = 0; i < g_repeat; i++) {
  228. CALL_SUBTEST_1(( quaternion<float,AutoAlign>() ));
  229. CALL_SUBTEST_1( check_const_correctness(Quaternionf()) );
  230. CALL_SUBTEST_2(( quaternion<double,AutoAlign>() ));
  231. CALL_SUBTEST_2( check_const_correctness(Quaterniond()) );
  232. CALL_SUBTEST_3(( quaternion<float,DontAlign>() ));
  233. CALL_SUBTEST_4(( quaternion<double,DontAlign>() ));
  234. CALL_SUBTEST_5(( quaternionAlignment<float>() ));
  235. CALL_SUBTEST_6(( quaternionAlignment<double>() ));
  236. CALL_SUBTEST_1( mapQuaternion<float>() );
  237. CALL_SUBTEST_2( mapQuaternion<double>() );
  238. }
  239. }