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.

431 lines
14 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.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> void geometry(void)
  14. {
  15. /* this test covers the following files:
  16. Cross.h Quaternion.h, Transform.cpp
  17. */
  18. typedef Matrix<Scalar,2,2> Matrix2;
  19. typedef Matrix<Scalar,3,3> Matrix3;
  20. typedef Matrix<Scalar,4,4> Matrix4;
  21. typedef Matrix<Scalar,2,1> Vector2;
  22. typedef Matrix<Scalar,3,1> Vector3;
  23. typedef Matrix<Scalar,4,1> Vector4;
  24. typedef Quaternion<Scalar> Quaternionx;
  25. typedef AngleAxis<Scalar> AngleAxisx;
  26. typedef Transform<Scalar,2> Transform2;
  27. typedef Transform<Scalar,3> Transform3;
  28. typedef Scaling<Scalar,2> Scaling2;
  29. typedef Scaling<Scalar,3> Scaling3;
  30. typedef Translation<Scalar,2> Translation2;
  31. typedef Translation<Scalar,3> Translation3;
  32. Scalar largeEps = test_precision<Scalar>();
  33. if (ei_is_same_type<Scalar,float>::ret)
  34. largeEps = 1e-2f;
  35. Vector3 v0 = Vector3::Random(),
  36. v1 = Vector3::Random(),
  37. v2 = Vector3::Random();
  38. Vector2 u0 = Vector2::Random();
  39. Matrix3 matrot1;
  40. Scalar a = ei_random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
  41. // cross product
  42. VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).eigen2_dot(v1), Scalar(1));
  43. Matrix3 m;
  44. m << v0.normalized(),
  45. (v0.cross(v1)).normalized(),
  46. (v0.cross(v1).cross(v0)).normalized();
  47. VERIFY(m.isUnitary());
  48. // Quaternion: Identity(), setIdentity();
  49. Quaternionx q1, q2;
  50. q2.setIdentity();
  51. VERIFY_IS_APPROX(Quaternionx(Quaternionx::Identity()).coeffs(), q2.coeffs());
  52. q1.coeffs().setRandom();
  53. VERIFY_IS_APPROX(q1.coeffs(), (q1*q2).coeffs());
  54. // unitOrthogonal
  55. VERIFY_IS_MUCH_SMALLER_THAN(u0.unitOrthogonal().eigen2_dot(u0), Scalar(1));
  56. VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().eigen2_dot(v0), Scalar(1));
  57. VERIFY_IS_APPROX(u0.unitOrthogonal().norm(), Scalar(1));
  58. VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), Scalar(1));
  59. VERIFY_IS_APPROX(v0, AngleAxisx(a, v0.normalized()) * v0);
  60. VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(M_PI), v0.unitOrthogonal()) * v0);
  61. VERIFY_IS_APPROX(ei_cos(a)*v0.squaredNorm(), v0.eigen2_dot(AngleAxisx(a, v0.unitOrthogonal()) * v0));
  62. m = AngleAxisx(a, v0.normalized()).toRotationMatrix().adjoint();
  63. VERIFY_IS_APPROX(Matrix3::Identity(), m * AngleAxisx(a, v0.normalized()));
  64. VERIFY_IS_APPROX(Matrix3::Identity(), AngleAxisx(a, v0.normalized()) * m);
  65. q1 = AngleAxisx(a, v0.normalized());
  66. q2 = AngleAxisx(a, v1.normalized());
  67. // angular distance
  68. Scalar refangle = ei_abs(AngleAxisx(q1.inverse()*q2).angle());
  69. if (refangle>Scalar(M_PI))
  70. refangle = Scalar(2)*Scalar(M_PI) - refangle;
  71. if((q1.coeffs()-q2.coeffs()).norm() > 10*largeEps)
  72. {
  73. VERIFY(ei_isApprox(q1.angularDistance(q2), refangle, largeEps));
  74. }
  75. // rotation matrix conversion
  76. VERIFY_IS_APPROX(q1 * v2, q1.toRotationMatrix() * v2);
  77. VERIFY_IS_APPROX(q1 * q2 * v2,
  78. q1.toRotationMatrix() * q2.toRotationMatrix() * v2);
  79. VERIFY( (q2*q1).isApprox(q1*q2, largeEps) || !(q2 * q1 * v2).isApprox(
  80. q1.toRotationMatrix() * q2.toRotationMatrix() * v2));
  81. q2 = q1.toRotationMatrix();
  82. VERIFY_IS_APPROX(q1*v1,q2*v1);
  83. matrot1 = AngleAxisx(Scalar(0.1), Vector3::UnitX())
  84. * AngleAxisx(Scalar(0.2), Vector3::UnitY())
  85. * AngleAxisx(Scalar(0.3), Vector3::UnitZ());
  86. VERIFY_IS_APPROX(matrot1 * v1,
  87. AngleAxisx(Scalar(0.1), Vector3(1,0,0)).toRotationMatrix()
  88. * (AngleAxisx(Scalar(0.2), Vector3(0,1,0)).toRotationMatrix()
  89. * (AngleAxisx(Scalar(0.3), Vector3(0,0,1)).toRotationMatrix() * v1)));
  90. // angle-axis conversion
  91. AngleAxisx aa = q1;
  92. VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
  93. VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1);
  94. // from two vector creation
  95. VERIFY_IS_APPROX(v2.normalized(),(q2.setFromTwoVectors(v1,v2)*v1).normalized());
  96. VERIFY_IS_APPROX(v2.normalized(),(q2.setFromTwoVectors(v1,v2)*v1).normalized());
  97. // inverse and conjugate
  98. VERIFY_IS_APPROX(q1 * (q1.inverse() * v1), v1);
  99. VERIFY_IS_APPROX(q1 * (q1.conjugate() * v1), v1);
  100. // AngleAxis
  101. VERIFY_IS_APPROX(AngleAxisx(a,v1.normalized()).toRotationMatrix(),
  102. Quaternionx(AngleAxisx(a,v1.normalized())).toRotationMatrix());
  103. AngleAxisx aa1;
  104. m = q1.toRotationMatrix();
  105. aa1 = m;
  106. VERIFY_IS_APPROX(AngleAxisx(m).toRotationMatrix(),
  107. Quaternionx(m).toRotationMatrix());
  108. // Transform
  109. // TODO complete the tests !
  110. a = 0;
  111. while (ei_abs(a)<Scalar(0.1))
  112. a = ei_random<Scalar>(-Scalar(0.4)*Scalar(M_PI), Scalar(0.4)*Scalar(M_PI));
  113. q1 = AngleAxisx(a, v0.normalized());
  114. Transform3 t0, t1, t2;
  115. // first test setIdentity() and Identity()
  116. t0.setIdentity();
  117. VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
  118. t0.matrix().setZero();
  119. t0 = Transform3::Identity();
  120. VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
  121. t0.linear() = q1.toRotationMatrix();
  122. t1.setIdentity();
  123. t1.linear() = q1.toRotationMatrix();
  124. v0 << 50, 2, 1;//= ei_random_matrix<Vector3>().cwiseProduct(Vector3(10,2,0.5));
  125. t0.scale(v0);
  126. t1.prescale(v0);
  127. VERIFY_IS_APPROX( (t0 * Vector3(1,0,0)).norm(), v0.x());
  128. //VERIFY(!ei_isApprox((t1 * Vector3(1,0,0)).norm(), v0.x()));
  129. t0.setIdentity();
  130. t1.setIdentity();
  131. v1 << 1, 2, 3;
  132. t0.linear() = q1.toRotationMatrix();
  133. t0.pretranslate(v0);
  134. t0.scale(v1);
  135. t1.linear() = q1.conjugate().toRotationMatrix();
  136. t1.prescale(v1.cwise().inverse());
  137. t1.translate(-v0);
  138. VERIFY((t0.matrix() * t1.matrix()).isIdentity(test_precision<Scalar>()));
  139. t1.fromPositionOrientationScale(v0, q1, v1);
  140. VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
  141. VERIFY_IS_APPROX(t1*v1, t0*v1);
  142. t0.setIdentity(); t0.scale(v0).rotate(q1.toRotationMatrix());
  143. t1.setIdentity(); t1.scale(v0).rotate(q1);
  144. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  145. t0.setIdentity(); t0.scale(v0).rotate(AngleAxisx(q1));
  146. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  147. VERIFY_IS_APPROX(t0.scale(a).matrix(), t1.scale(Vector3::Constant(a)).matrix());
  148. VERIFY_IS_APPROX(t0.prescale(a).matrix(), t1.prescale(Vector3::Constant(a)).matrix());
  149. // More transform constructors, operator=, operator*=
  150. Matrix3 mat3 = Matrix3::Random();
  151. Matrix4 mat4;
  152. mat4 << mat3 , Vector3::Zero() , Vector4::Zero().transpose();
  153. Transform3 tmat3(mat3), tmat4(mat4);
  154. tmat4.matrix()(3,3) = Scalar(1);
  155. VERIFY_IS_APPROX(tmat3.matrix(), tmat4.matrix());
  156. Scalar a3 = ei_random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
  157. Vector3 v3 = Vector3::Random().normalized();
  158. AngleAxisx aa3(a3, v3);
  159. Transform3 t3(aa3);
  160. Transform3 t4;
  161. t4 = aa3;
  162. VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
  163. t4.rotate(AngleAxisx(-a3,v3));
  164. VERIFY_IS_APPROX(t4.matrix(), Matrix4::Identity());
  165. t4 *= aa3;
  166. VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
  167. v3 = Vector3::Random();
  168. Translation3 tv3(v3);
  169. Transform3 t5(tv3);
  170. t4 = tv3;
  171. VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
  172. t4.translate(-v3);
  173. VERIFY_IS_APPROX(t4.matrix(), Matrix4::Identity());
  174. t4 *= tv3;
  175. VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
  176. Scaling3 sv3(v3);
  177. Transform3 t6(sv3);
  178. t4 = sv3;
  179. VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
  180. t4.scale(v3.cwise().inverse());
  181. VERIFY_IS_APPROX(t4.matrix(), Matrix4::Identity());
  182. t4 *= sv3;
  183. VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
  184. // matrix * transform
  185. VERIFY_IS_APPROX(Transform3(t3.matrix()*t4).matrix(), Transform3(t3*t4).matrix());
  186. // chained Transform product
  187. VERIFY_IS_APPROX(((t3*t4)*t5).matrix(), (t3*(t4*t5)).matrix());
  188. // check that Transform product doesn't have aliasing problems
  189. t5 = t4;
  190. t5 = t5*t5;
  191. VERIFY_IS_APPROX(t5, t4*t4);
  192. // 2D transformation
  193. Transform2 t20, t21;
  194. Vector2 v20 = Vector2::Random();
  195. Vector2 v21 = Vector2::Random();
  196. for (int k=0; k<2; ++k)
  197. if (ei_abs(v21[k])<Scalar(1e-3)) v21[k] = Scalar(1e-3);
  198. t21.setIdentity();
  199. t21.linear() = Rotation2D<Scalar>(a).toRotationMatrix();
  200. VERIFY_IS_APPROX(t20.fromPositionOrientationScale(v20,a,v21).matrix(),
  201. t21.pretranslate(v20).scale(v21).matrix());
  202. t21.setIdentity();
  203. t21.linear() = Rotation2D<Scalar>(-a).toRotationMatrix();
  204. VERIFY( (t20.fromPositionOrientationScale(v20,a,v21)
  205. * (t21.prescale(v21.cwise().inverse()).translate(-v20))).matrix().isIdentity(test_precision<Scalar>()) );
  206. // Transform - new API
  207. // 3D
  208. t0.setIdentity();
  209. t0.rotate(q1).scale(v0).translate(v0);
  210. // mat * scaling and mat * translation
  211. t1 = (Matrix3(q1) * Scaling3(v0)) * Translation3(v0);
  212. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  213. // mat * transformation and scaling * translation
  214. t1 = Matrix3(q1) * (Scaling3(v0) * Translation3(v0));
  215. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  216. t0.setIdentity();
  217. t0.prerotate(q1).prescale(v0).pretranslate(v0);
  218. // translation * scaling and transformation * mat
  219. t1 = (Translation3(v0) * Scaling3(v0)) * Matrix3(q1);
  220. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  221. // scaling * mat and translation * mat
  222. t1 = Translation3(v0) * (Scaling3(v0) * Matrix3(q1));
  223. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  224. t0.setIdentity();
  225. t0.scale(v0).translate(v0).rotate(q1);
  226. // translation * mat and scaling * transformation
  227. t1 = Scaling3(v0) * (Translation3(v0) * Matrix3(q1));
  228. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  229. // transformation * scaling
  230. t0.scale(v0);
  231. t1 = t1 * Scaling3(v0);
  232. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  233. // transformation * translation
  234. t0.translate(v0);
  235. t1 = t1 * Translation3(v0);
  236. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  237. // translation * transformation
  238. t0.pretranslate(v0);
  239. t1 = Translation3(v0) * t1;
  240. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  241. // transform * quaternion
  242. t0.rotate(q1);
  243. t1 = t1 * q1;
  244. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  245. // translation * quaternion
  246. t0.translate(v1).rotate(q1);
  247. t1 = t1 * (Translation3(v1) * q1);
  248. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  249. // scaling * quaternion
  250. t0.scale(v1).rotate(q1);
  251. t1 = t1 * (Scaling3(v1) * q1);
  252. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  253. // quaternion * transform
  254. t0.prerotate(q1);
  255. t1 = q1 * t1;
  256. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  257. // quaternion * translation
  258. t0.rotate(q1).translate(v1);
  259. t1 = t1 * (q1 * Translation3(v1));
  260. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  261. // quaternion * scaling
  262. t0.rotate(q1).scale(v1);
  263. t1 = t1 * (q1 * Scaling3(v1));
  264. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  265. // translation * vector
  266. t0.setIdentity();
  267. t0.translate(v0);
  268. VERIFY_IS_APPROX(t0 * v1, Translation3(v0) * v1);
  269. // scaling * vector
  270. t0.setIdentity();
  271. t0.scale(v0);
  272. VERIFY_IS_APPROX(t0 * v1, Scaling3(v0) * v1);
  273. // test transform inversion
  274. t0.setIdentity();
  275. t0.translate(v0);
  276. t0.linear().setRandom();
  277. VERIFY_IS_APPROX(t0.inverse(Affine), t0.matrix().inverse());
  278. t0.setIdentity();
  279. t0.translate(v0).rotate(q1);
  280. VERIFY_IS_APPROX(t0.inverse(Isometry), t0.matrix().inverse());
  281. // test extract rotation and scaling
  282. t0.setIdentity();
  283. t0.translate(v0).rotate(q1).scale(v1);
  284. VERIFY_IS_APPROX(t0.rotation() * v1, Matrix3(q1) * v1);
  285. Matrix3 mat_rotation, mat_scaling;
  286. t0.setIdentity();
  287. t0.translate(v0).rotate(q1).scale(v1);
  288. t0.computeRotationScaling(&mat_rotation, &mat_scaling);
  289. VERIFY_IS_APPROX(t0.linear(), mat_rotation * mat_scaling);
  290. VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
  291. VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
  292. t0.computeScalingRotation(&mat_scaling, &mat_rotation);
  293. VERIFY_IS_APPROX(t0.linear(), mat_scaling * mat_rotation);
  294. VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
  295. VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
  296. // test casting
  297. Transform<float,3> t1f = t1.template cast<float>();
  298. VERIFY_IS_APPROX(t1f.template cast<Scalar>(),t1);
  299. Transform<double,3> t1d = t1.template cast<double>();
  300. VERIFY_IS_APPROX(t1d.template cast<Scalar>(),t1);
  301. Translation3 tr1(v0);
  302. Translation<float,3> tr1f = tr1.template cast<float>();
  303. VERIFY_IS_APPROX(tr1f.template cast<Scalar>(),tr1);
  304. Translation<double,3> tr1d = tr1.template cast<double>();
  305. VERIFY_IS_APPROX(tr1d.template cast<Scalar>(),tr1);
  306. Scaling3 sc1(v0);
  307. Scaling<float,3> sc1f = sc1.template cast<float>();
  308. VERIFY_IS_APPROX(sc1f.template cast<Scalar>(),sc1);
  309. Scaling<double,3> sc1d = sc1.template cast<double>();
  310. VERIFY_IS_APPROX(sc1d.template cast<Scalar>(),sc1);
  311. Quaternion<float> q1f = q1.template cast<float>();
  312. VERIFY_IS_APPROX(q1f.template cast<Scalar>(),q1);
  313. Quaternion<double> q1d = q1.template cast<double>();
  314. VERIFY_IS_APPROX(q1d.template cast<Scalar>(),q1);
  315. AngleAxis<float> aa1f = aa1.template cast<float>();
  316. VERIFY_IS_APPROX(aa1f.template cast<Scalar>(),aa1);
  317. AngleAxis<double> aa1d = aa1.template cast<double>();
  318. VERIFY_IS_APPROX(aa1d.template cast<Scalar>(),aa1);
  319. Rotation2D<Scalar> r2d1(ei_random<Scalar>());
  320. Rotation2D<float> r2d1f = r2d1.template cast<float>();
  321. VERIFY_IS_APPROX(r2d1f.template cast<Scalar>(),r2d1);
  322. Rotation2D<double> r2d1d = r2d1.template cast<double>();
  323. VERIFY_IS_APPROX(r2d1d.template cast<Scalar>(),r2d1);
  324. m = q1;
  325. // m.col(1) = Vector3(0,ei_random<Scalar>(),ei_random<Scalar>()).normalized();
  326. // m.col(0) = Vector3(-1,0,0).normalized();
  327. // m.col(2) = m.col(0).cross(m.col(1));
  328. #define VERIFY_EULER(I,J,K, X,Y,Z) { \
  329. Vector3 ea = m.eulerAngles(I,J,K); \
  330. Matrix3 m1 = Matrix3(AngleAxisx(ea[0], Vector3::Unit##X()) * AngleAxisx(ea[1], Vector3::Unit##Y()) * AngleAxisx(ea[2], Vector3::Unit##Z())); \
  331. VERIFY_IS_APPROX(m, Matrix3(AngleAxisx(ea[0], Vector3::Unit##X()) * AngleAxisx(ea[1], Vector3::Unit##Y()) * AngleAxisx(ea[2], Vector3::Unit##Z()))); \
  332. }
  333. VERIFY_EULER(0,1,2, X,Y,Z);
  334. VERIFY_EULER(0,1,0, X,Y,X);
  335. VERIFY_EULER(0,2,1, X,Z,Y);
  336. VERIFY_EULER(0,2,0, X,Z,X);
  337. VERIFY_EULER(1,2,0, Y,Z,X);
  338. VERIFY_EULER(1,2,1, Y,Z,Y);
  339. VERIFY_EULER(1,0,2, Y,X,Z);
  340. VERIFY_EULER(1,0,1, Y,X,Y);
  341. VERIFY_EULER(2,0,1, Z,X,Y);
  342. VERIFY_EULER(2,0,2, Z,X,Z);
  343. VERIFY_EULER(2,1,0, Z,Y,X);
  344. VERIFY_EULER(2,1,2, Z,Y,Z);
  345. // colwise/rowwise cross product
  346. mat3.setRandom();
  347. Vector3 vec3 = Vector3::Random();
  348. Matrix3 mcross;
  349. int i = ei_random<int>(0,2);
  350. mcross = mat3.colwise().cross(vec3);
  351. VERIFY_IS_APPROX(mcross.col(i), mat3.col(i).cross(vec3));
  352. mcross = mat3.rowwise().cross(vec3);
  353. VERIFY_IS_APPROX(mcross.row(i), mat3.row(i).cross(vec3));
  354. }
  355. void test_eigen2_geometry()
  356. {
  357. for(int i = 0; i < g_repeat; i++) {
  358. CALL_SUBTEST_1( geometry<float>() );
  359. CALL_SUBTEST_2( geometry<double>() );
  360. }
  361. }