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.

560 lines
20 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. //
  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 T>
  14. Matrix<T,2,1> angleToVec(T a)
  15. {
  16. return Matrix<T,2,1>(std::cos(a), std::sin(a));
  17. }
  18. template<typename Scalar, int Mode, int Options> void non_projective_only()
  19. {
  20. /* this test covers the following files:
  21. Cross.h Quaternion.h, Transform.cpp
  22. */
  23. typedef Matrix<Scalar,3,1> Vector3;
  24. typedef Quaternion<Scalar> Quaternionx;
  25. typedef AngleAxis<Scalar> AngleAxisx;
  26. typedef Transform<Scalar,3,Mode,Options> Transform3;
  27. typedef DiagonalMatrix<Scalar,3> AlignedScaling3;
  28. typedef Translation<Scalar,3> Translation3;
  29. Vector3 v0 = Vector3::Random(),
  30. v1 = Vector3::Random();
  31. Transform3 t0, t1, t2;
  32. Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
  33. Quaternionx q1, q2;
  34. q1 = AngleAxisx(a, v0.normalized());
  35. t0 = Transform3::Identity();
  36. VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
  37. t0.linear() = q1.toRotationMatrix();
  38. v0 << 50, 2, 1;
  39. t0.scale(v0);
  40. VERIFY_IS_APPROX( (t0 * Vector3(1,0,0)).template head<3>().norm(), v0.x());
  41. t0.setIdentity();
  42. t1.setIdentity();
  43. v1 << 1, 2, 3;
  44. t0.linear() = q1.toRotationMatrix();
  45. t0.pretranslate(v0);
  46. t0.scale(v1);
  47. t1.linear() = q1.conjugate().toRotationMatrix();
  48. t1.prescale(v1.cwiseInverse());
  49. t1.translate(-v0);
  50. VERIFY((t0 * t1).matrix().isIdentity(test_precision<Scalar>()));
  51. t1.fromPositionOrientationScale(v0, q1, v1);
  52. VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
  53. VERIFY_IS_APPROX(t1*v1, t0*v1);
  54. // translation * vector
  55. t0.setIdentity();
  56. t0.translate(v0);
  57. VERIFY_IS_APPROX((t0 * v1).template head<3>(), Translation3(v0) * v1);
  58. // AlignedScaling * vector
  59. t0.setIdentity();
  60. t0.scale(v0);
  61. VERIFY_IS_APPROX((t0 * v1).template head<3>(), AlignedScaling3(v0) * v1);
  62. }
  63. template<typename Scalar, int Mode, int Options> void transformations()
  64. {
  65. /* this test covers the following files:
  66. Cross.h Quaternion.h, Transform.cpp
  67. */
  68. using std::cos;
  69. using std::abs;
  70. typedef Matrix<Scalar,3,3> Matrix3;
  71. typedef Matrix<Scalar,4,4> Matrix4;
  72. typedef Matrix<Scalar,2,1> Vector2;
  73. typedef Matrix<Scalar,3,1> Vector3;
  74. typedef Matrix<Scalar,4,1> Vector4;
  75. typedef Quaternion<Scalar> Quaternionx;
  76. typedef AngleAxis<Scalar> AngleAxisx;
  77. typedef Transform<Scalar,2,Mode,Options> Transform2;
  78. typedef Transform<Scalar,3,Mode,Options> Transform3;
  79. typedef typename Transform3::MatrixType MatrixType;
  80. typedef DiagonalMatrix<Scalar,3> AlignedScaling3;
  81. typedef Translation<Scalar,2> Translation2;
  82. typedef Translation<Scalar,3> Translation3;
  83. Vector3 v0 = Vector3::Random(),
  84. v1 = Vector3::Random();
  85. Matrix3 matrot1, m;
  86. Scalar a = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
  87. Scalar s0 = internal::random<Scalar>(), s1 = internal::random<Scalar>();
  88. while(v0.norm() < test_precision<Scalar>()) v0 = Vector3::Random();
  89. while(v1.norm() < test_precision<Scalar>()) v1 = Vector3::Random();
  90. VERIFY_IS_APPROX(v0, AngleAxisx(a, v0.normalized()) * v0);
  91. VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(EIGEN_PI), v0.unitOrthogonal()) * v0);
  92. if(abs(cos(a)) > test_precision<Scalar>())
  93. {
  94. VERIFY_IS_APPROX(cos(a)*v0.squaredNorm(), v0.dot(AngleAxisx(a, v0.unitOrthogonal()) * v0));
  95. }
  96. m = AngleAxisx(a, v0.normalized()).toRotationMatrix().adjoint();
  97. VERIFY_IS_APPROX(Matrix3::Identity(), m * AngleAxisx(a, v0.normalized()));
  98. VERIFY_IS_APPROX(Matrix3::Identity(), AngleAxisx(a, v0.normalized()) * m);
  99. Quaternionx q1, q2;
  100. q1 = AngleAxisx(a, v0.normalized());
  101. q2 = AngleAxisx(a, v1.normalized());
  102. // rotation matrix conversion
  103. matrot1 = AngleAxisx(Scalar(0.1), Vector3::UnitX())
  104. * AngleAxisx(Scalar(0.2), Vector3::UnitY())
  105. * AngleAxisx(Scalar(0.3), Vector3::UnitZ());
  106. VERIFY_IS_APPROX(matrot1 * v1,
  107. AngleAxisx(Scalar(0.1), Vector3(1,0,0)).toRotationMatrix()
  108. * (AngleAxisx(Scalar(0.2), Vector3(0,1,0)).toRotationMatrix()
  109. * (AngleAxisx(Scalar(0.3), Vector3(0,0,1)).toRotationMatrix() * v1)));
  110. // angle-axis conversion
  111. AngleAxisx aa = AngleAxisx(q1);
  112. VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
  113. // The following test is stable only if 2*angle != angle and v1 is not colinear with axis
  114. if( (abs(aa.angle()) > test_precision<Scalar>()) && (abs(aa.axis().dot(v1.normalized()))<(Scalar(1)-Scalar(4)*test_precision<Scalar>())) )
  115. {
  116. VERIFY( !(q1 * v1).isApprox(Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1) );
  117. }
  118. aa.fromRotationMatrix(aa.toRotationMatrix());
  119. VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
  120. // The following test is stable only if 2*angle != angle and v1 is not colinear with axis
  121. if( (abs(aa.angle()) > test_precision<Scalar>()) && (abs(aa.axis().dot(v1.normalized()))<(Scalar(1)-Scalar(4)*test_precision<Scalar>())) )
  122. {
  123. VERIFY( !(q1 * v1).isApprox(Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1) );
  124. }
  125. // AngleAxis
  126. VERIFY_IS_APPROX(AngleAxisx(a,v1.normalized()).toRotationMatrix(),
  127. Quaternionx(AngleAxisx(a,v1.normalized())).toRotationMatrix());
  128. AngleAxisx aa1;
  129. m = q1.toRotationMatrix();
  130. aa1 = m;
  131. VERIFY_IS_APPROX(AngleAxisx(m).toRotationMatrix(),
  132. Quaternionx(m).toRotationMatrix());
  133. // Transform
  134. // TODO complete the tests !
  135. a = 0;
  136. while (abs(a)<Scalar(0.1))
  137. a = internal::random<Scalar>(-Scalar(0.4)*Scalar(EIGEN_PI), Scalar(0.4)*Scalar(EIGEN_PI));
  138. q1 = AngleAxisx(a, v0.normalized());
  139. Transform3 t0, t1, t2;
  140. // first test setIdentity() and Identity()
  141. t0.setIdentity();
  142. VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
  143. t0.matrix().setZero();
  144. t0 = Transform3::Identity();
  145. VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
  146. t0.setIdentity();
  147. t1.setIdentity();
  148. v1 << 1, 2, 3;
  149. t0.linear() = q1.toRotationMatrix();
  150. t0.pretranslate(v0);
  151. t0.scale(v1);
  152. t1.linear() = q1.conjugate().toRotationMatrix();
  153. t1.prescale(v1.cwiseInverse());
  154. t1.translate(-v0);
  155. VERIFY((t0 * t1).matrix().isIdentity(test_precision<Scalar>()));
  156. t1.fromPositionOrientationScale(v0, q1, v1);
  157. VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
  158. t0.setIdentity(); t0.scale(v0).rotate(q1.toRotationMatrix());
  159. t1.setIdentity(); t1.scale(v0).rotate(q1);
  160. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  161. t0.setIdentity(); t0.scale(v0).rotate(AngleAxisx(q1));
  162. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  163. VERIFY_IS_APPROX(t0.scale(a).matrix(), t1.scale(Vector3::Constant(a)).matrix());
  164. VERIFY_IS_APPROX(t0.prescale(a).matrix(), t1.prescale(Vector3::Constant(a)).matrix());
  165. // More transform constructors, operator=, operator*=
  166. Matrix3 mat3 = Matrix3::Random();
  167. Matrix4 mat4;
  168. mat4 << mat3 , Vector3::Zero() , Vector4::Zero().transpose();
  169. Transform3 tmat3(mat3), tmat4(mat4);
  170. if(Mode!=int(AffineCompact))
  171. tmat4.matrix()(3,3) = Scalar(1);
  172. VERIFY_IS_APPROX(tmat3.matrix(), tmat4.matrix());
  173. Scalar a3 = internal::random<Scalar>(-Scalar(EIGEN_PI), Scalar(EIGEN_PI));
  174. Vector3 v3 = Vector3::Random().normalized();
  175. AngleAxisx aa3(a3, v3);
  176. Transform3 t3(aa3);
  177. Transform3 t4;
  178. t4 = aa3;
  179. VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
  180. t4.rotate(AngleAxisx(-a3,v3));
  181. VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity());
  182. t4 *= aa3;
  183. VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
  184. do {
  185. v3 = Vector3::Random();
  186. } while (v3.cwiseAbs().minCoeff()<NumTraits<Scalar>::epsilon());
  187. Translation3 tv3(v3);
  188. Transform3 t5(tv3);
  189. t4 = tv3;
  190. VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
  191. t4.translate(-v3);
  192. VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity());
  193. t4 *= tv3;
  194. VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
  195. AlignedScaling3 sv3(v3);
  196. Transform3 t6(sv3);
  197. t4 = sv3;
  198. VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
  199. t4.scale(v3.cwiseInverse());
  200. VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity());
  201. t4 *= sv3;
  202. VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
  203. // matrix * transform
  204. VERIFY_IS_APPROX((t3.matrix()*t4).matrix(), (t3*t4).matrix());
  205. // chained Transform product
  206. VERIFY_IS_APPROX(((t3*t4)*t5).matrix(), (t3*(t4*t5)).matrix());
  207. // check that Transform product doesn't have aliasing problems
  208. t5 = t4;
  209. t5 = t5*t5;
  210. VERIFY_IS_APPROX(t5, t4*t4);
  211. // 2D transformation
  212. Transform2 t20, t21;
  213. Vector2 v20 = Vector2::Random();
  214. Vector2 v21 = Vector2::Random();
  215. for (int k=0; k<2; ++k)
  216. if (abs(v21[k])<Scalar(1e-3)) v21[k] = Scalar(1e-3);
  217. t21.setIdentity();
  218. t21.linear() = Rotation2D<Scalar>(a).toRotationMatrix();
  219. VERIFY_IS_APPROX(t20.fromPositionOrientationScale(v20,a,v21).matrix(),
  220. t21.pretranslate(v20).scale(v21).matrix());
  221. t21.setIdentity();
  222. t21.linear() = Rotation2D<Scalar>(-a).toRotationMatrix();
  223. VERIFY( (t20.fromPositionOrientationScale(v20,a,v21)
  224. * (t21.prescale(v21.cwiseInverse()).translate(-v20))).matrix().isIdentity(test_precision<Scalar>()) );
  225. // Transform - new API
  226. // 3D
  227. t0.setIdentity();
  228. t0.rotate(q1).scale(v0).translate(v0);
  229. // mat * aligned scaling and mat * translation
  230. t1 = (Matrix3(q1) * AlignedScaling3(v0)) * Translation3(v0);
  231. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  232. t1 = (Matrix3(q1) * StormEigen::Scaling(v0)) * Translation3(v0);
  233. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  234. t1 = (q1 * StormEigen::Scaling(v0)) * Translation3(v0);
  235. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  236. // mat * transformation and aligned scaling * translation
  237. t1 = Matrix3(q1) * (AlignedScaling3(v0) * Translation3(v0));
  238. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  239. t0.setIdentity();
  240. t0.scale(s0).translate(v0);
  241. t1 = StormEigen::Scaling(s0) * Translation3(v0);
  242. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  243. t0.prescale(s0);
  244. t1 = StormEigen::Scaling(s0) * t1;
  245. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  246. t0 = t3;
  247. t0.scale(s0);
  248. t1 = t3 * StormEigen::Scaling(s0,s0,s0);
  249. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  250. t0.prescale(s0);
  251. t1 = StormEigen::Scaling(s0,s0,s0) * t1;
  252. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  253. t0 = t3;
  254. t0.scale(s0);
  255. t1 = t3 * StormEigen::Scaling(s0);
  256. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  257. t0.prescale(s0);
  258. t1 = StormEigen::Scaling(s0) * t1;
  259. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  260. t0.setIdentity();
  261. t0.prerotate(q1).prescale(v0).pretranslate(v0);
  262. // translation * aligned scaling and transformation * mat
  263. t1 = (Translation3(v0) * AlignedScaling3(v0)) * Transform3(q1);
  264. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  265. // scaling * mat and translation * mat
  266. t1 = Translation3(v0) * (AlignedScaling3(v0) * Transform3(q1));
  267. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  268. t0.setIdentity();
  269. t0.scale(v0).translate(v0).rotate(q1);
  270. // translation * mat and aligned scaling * transformation
  271. t1 = AlignedScaling3(v0) * (Translation3(v0) * Transform3(q1));
  272. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  273. // transformation * aligned scaling
  274. t0.scale(v0);
  275. t1 *= AlignedScaling3(v0);
  276. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  277. // transformation * translation
  278. t0.translate(v0);
  279. t1 = t1 * Translation3(v0);
  280. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  281. // translation * transformation
  282. t0.pretranslate(v0);
  283. t1 = Translation3(v0) * t1;
  284. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  285. // transform * quaternion
  286. t0.rotate(q1);
  287. t1 = t1 * q1;
  288. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  289. // translation * quaternion
  290. t0.translate(v1).rotate(q1);
  291. t1 = t1 * (Translation3(v1) * q1);
  292. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  293. // aligned scaling * quaternion
  294. t0.scale(v1).rotate(q1);
  295. t1 = t1 * (AlignedScaling3(v1) * q1);
  296. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  297. // quaternion * transform
  298. t0.prerotate(q1);
  299. t1 = q1 * t1;
  300. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  301. // quaternion * translation
  302. t0.rotate(q1).translate(v1);
  303. t1 = t1 * (q1 * Translation3(v1));
  304. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  305. // quaternion * aligned scaling
  306. t0.rotate(q1).scale(v1);
  307. t1 = t1 * (q1 * AlignedScaling3(v1));
  308. VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
  309. // test transform inversion
  310. t0.setIdentity();
  311. t0.translate(v0);
  312. do {
  313. t0.linear().setRandom();
  314. } while(t0.linear().jacobiSvd().singularValues()(2)<test_precision<Scalar>());
  315. Matrix4 t044 = Matrix4::Zero();
  316. t044(3,3) = 1;
  317. t044.block(0,0,t0.matrix().rows(),4) = t0.matrix();
  318. VERIFY_IS_APPROX(t0.inverse(Affine).matrix(), t044.inverse().block(0,0,t0.matrix().rows(),4));
  319. t0.setIdentity();
  320. t0.translate(v0).rotate(q1);
  321. t044 = Matrix4::Zero();
  322. t044(3,3) = 1;
  323. t044.block(0,0,t0.matrix().rows(),4) = t0.matrix();
  324. VERIFY_IS_APPROX(t0.inverse(Isometry).matrix(), t044.inverse().block(0,0,t0.matrix().rows(),4));
  325. Matrix3 mat_rotation, mat_scaling;
  326. t0.setIdentity();
  327. t0.translate(v0).rotate(q1).scale(v1);
  328. t0.computeRotationScaling(&mat_rotation, &mat_scaling);
  329. VERIFY_IS_APPROX(t0.linear(), mat_rotation * mat_scaling);
  330. VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
  331. VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
  332. t0.computeScalingRotation(&mat_scaling, &mat_rotation);
  333. VERIFY_IS_APPROX(t0.linear(), mat_scaling * mat_rotation);
  334. VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
  335. VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
  336. // test casting
  337. Transform<float,3,Mode> t1f = t1.template cast<float>();
  338. VERIFY_IS_APPROX(t1f.template cast<Scalar>(),t1);
  339. Transform<double,3,Mode> t1d = t1.template cast<double>();
  340. VERIFY_IS_APPROX(t1d.template cast<Scalar>(),t1);
  341. Translation3 tr1(v0);
  342. Translation<float,3> tr1f = tr1.template cast<float>();
  343. VERIFY_IS_APPROX(tr1f.template cast<Scalar>(),tr1);
  344. Translation<double,3> tr1d = tr1.template cast<double>();
  345. VERIFY_IS_APPROX(tr1d.template cast<Scalar>(),tr1);
  346. AngleAxis<float> aa1f = aa1.template cast<float>();
  347. VERIFY_IS_APPROX(aa1f.template cast<Scalar>(),aa1);
  348. AngleAxis<double> aa1d = aa1.template cast<double>();
  349. VERIFY_IS_APPROX(aa1d.template cast<Scalar>(),aa1);
  350. Rotation2D<Scalar> r2d1(internal::random<Scalar>());
  351. Rotation2D<float> r2d1f = r2d1.template cast<float>();
  352. VERIFY_IS_APPROX(r2d1f.template cast<Scalar>(),r2d1);
  353. Rotation2D<double> r2d1d = r2d1.template cast<double>();
  354. VERIFY_IS_APPROX(r2d1d.template cast<Scalar>(),r2d1);
  355. for(int k=0; k<100; ++k)
  356. {
  357. Scalar angle = internal::random<Scalar>(-100,100);
  358. Rotation2D<Scalar> rot2(angle);
  359. VERIFY( rot2.smallestPositiveAngle() >= 0 );
  360. VERIFY( rot2.smallestPositiveAngle() <= Scalar(2)*Scalar(EIGEN_PI) );
  361. VERIFY_IS_APPROX( angleToVec(rot2.smallestPositiveAngle()), angleToVec(rot2.angle()) );
  362. VERIFY( rot2.smallestAngle() >= -Scalar(EIGEN_PI) );
  363. VERIFY( rot2.smallestAngle() <= Scalar(EIGEN_PI) );
  364. VERIFY_IS_APPROX( angleToVec(rot2.smallestAngle()), angleToVec(rot2.angle()) );
  365. Matrix<Scalar,2,2> rot2_as_mat(rot2);
  366. Rotation2D<Scalar> rot3(rot2_as_mat);
  367. VERIFY_IS_APPROX( angleToVec(rot2.smallestAngle()), angleToVec(rot3.angle()) );
  368. }
  369. s0 = internal::random<Scalar>(-100,100);
  370. s1 = internal::random<Scalar>(-100,100);
  371. Rotation2D<Scalar> R0(s0), R1(s1);
  372. t20 = Translation2(v20) * (R0 * StormEigen::Scaling(s0));
  373. t21 = Translation2(v20) * R0 * StormEigen::Scaling(s0);
  374. VERIFY_IS_APPROX(t20,t21);
  375. t20 = Translation2(v20) * (R0 * R0.inverse() * StormEigen::Scaling(s0));
  376. t21 = Translation2(v20) * StormEigen::Scaling(s0);
  377. VERIFY_IS_APPROX(t20,t21);
  378. VERIFY_IS_APPROX(s0, (R0.slerp(0, R1)).angle());
  379. VERIFY_IS_APPROX( angleToVec(R1.smallestPositiveAngle()), angleToVec((R0.slerp(1, R1)).smallestPositiveAngle()) );
  380. VERIFY_IS_APPROX(R0.smallestPositiveAngle(), (R0.slerp(0.5, R0)).smallestPositiveAngle());
  381. if(std::cos(s0)>0)
  382. VERIFY_IS_MUCH_SMALLER_THAN((R0.slerp(0.5, R0.inverse())).smallestAngle(), Scalar(1));
  383. else
  384. VERIFY_IS_APPROX(Scalar(EIGEN_PI), (R0.slerp(0.5, R0.inverse())).smallestPositiveAngle());
  385. // Check path length
  386. Scalar l = 0;
  387. int path_steps = 100;
  388. for(int k=0; k<path_steps; ++k)
  389. {
  390. Scalar a1 = R0.slerp(Scalar(k)/Scalar(path_steps), R1).angle();
  391. Scalar a2 = R0.slerp(Scalar(k+1)/Scalar(path_steps), R1).angle();
  392. l += std::abs(a2-a1);
  393. }
  394. VERIFY(l<=EIGEN_PI*(Scalar(1)+NumTraits<Scalar>::epsilon()*Scalar(path_steps/2)));
  395. // check basic features
  396. {
  397. Rotation2D<Scalar> r1; // default ctor
  398. r1 = Rotation2D<Scalar>(s0); // copy assignment
  399. VERIFY_IS_APPROX(r1.angle(),s0);
  400. Rotation2D<Scalar> r2(r1); // copy ctor
  401. VERIFY_IS_APPROX(r2.angle(),s0);
  402. }
  403. }
  404. template<typename Scalar> void transform_alignment()
  405. {
  406. typedef Transform<Scalar,3,Projective,AutoAlign> Projective3a;
  407. typedef Transform<Scalar,3,Projective,DontAlign> Projective3u;
  408. EIGEN_ALIGN_MAX Scalar array1[16];
  409. EIGEN_ALIGN_MAX Scalar array2[16];
  410. EIGEN_ALIGN_MAX Scalar array3[16+1];
  411. Scalar* array3u = array3+1;
  412. Projective3a *p1 = ::new(reinterpret_cast<void*>(array1)) Projective3a;
  413. Projective3u *p2 = ::new(reinterpret_cast<void*>(array2)) Projective3u;
  414. Projective3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Projective3u;
  415. p1->matrix().setRandom();
  416. *p2 = *p1;
  417. *p3 = *p1;
  418. VERIFY_IS_APPROX(p1->matrix(), p2->matrix());
  419. VERIFY_IS_APPROX(p1->matrix(), p3->matrix());
  420. VERIFY_IS_APPROX( (*p1) * (*p1), (*p2)*(*p3));
  421. #if defined(EIGEN_VECTORIZE) && EIGEN_MAX_STATIC_ALIGN_BYTES>0
  422. if(internal::packet_traits<Scalar>::Vectorizable)
  423. VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Projective3a));
  424. #endif
  425. }
  426. template<typename Scalar, int Dim, int Options> void transform_products()
  427. {
  428. typedef Matrix<Scalar,Dim+1,Dim+1> Mat;
  429. typedef Transform<Scalar,Dim,Projective,Options> Proj;
  430. typedef Transform<Scalar,Dim,Affine,Options> Aff;
  431. typedef Transform<Scalar,Dim,AffineCompact,Options> AffC;
  432. Proj p; p.matrix().setRandom();
  433. Aff a; a.linear().setRandom(); a.translation().setRandom();
  434. AffC ac = a;
  435. Mat p_m(p.matrix()), a_m(a.matrix());
  436. VERIFY_IS_APPROX((p*p).matrix(), p_m*p_m);
  437. VERIFY_IS_APPROX((a*a).matrix(), a_m*a_m);
  438. VERIFY_IS_APPROX((p*a).matrix(), p_m*a_m);
  439. VERIFY_IS_APPROX((a*p).matrix(), a_m*p_m);
  440. VERIFY_IS_APPROX((ac*a).matrix(), a_m*a_m);
  441. VERIFY_IS_APPROX((a*ac).matrix(), a_m*a_m);
  442. VERIFY_IS_APPROX((p*ac).matrix(), p_m*a_m);
  443. VERIFY_IS_APPROX((ac*p).matrix(), a_m*p_m);
  444. }
  445. void test_geo_transformations()
  446. {
  447. for(int i = 0; i < g_repeat; i++) {
  448. CALL_SUBTEST_1(( transformations<double,Affine,AutoAlign>() ));
  449. CALL_SUBTEST_1(( non_projective_only<double,Affine,AutoAlign>() ));
  450. CALL_SUBTEST_2(( transformations<float,AffineCompact,AutoAlign>() ));
  451. CALL_SUBTEST_2(( non_projective_only<float,AffineCompact,AutoAlign>() ));
  452. CALL_SUBTEST_2(( transform_alignment<float>() ));
  453. CALL_SUBTEST_3(( transformations<double,Projective,AutoAlign>() ));
  454. CALL_SUBTEST_3(( transformations<double,Projective,DontAlign>() ));
  455. CALL_SUBTEST_3(( transform_alignment<double>() ));
  456. CALL_SUBTEST_4(( transformations<float,Affine,RowMajor|AutoAlign>() ));
  457. CALL_SUBTEST_4(( non_projective_only<float,Affine,RowMajor>() ));
  458. CALL_SUBTEST_5(( transformations<double,AffineCompact,RowMajor|AutoAlign>() ));
  459. CALL_SUBTEST_5(( non_projective_only<double,AffineCompact,RowMajor>() ));
  460. CALL_SUBTEST_6(( transformations<double,Projective,RowMajor|AutoAlign>() ));
  461. CALL_SUBTEST_6(( transformations<double,Projective,RowMajor|DontAlign>() ));
  462. CALL_SUBTEST_7(( transform_products<double,3,RowMajor|AutoAlign>() ));
  463. CALL_SUBTEST_7(( transform_products<float,2,AutoAlign>() ));
  464. }
  465. }