eigen2_geometry.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 #include <Eigen/Geometry>
00027 #include <Eigen/LU>
00028 #include <Eigen/SVD>
00029 
00030 template<typename Scalar> void geometry(void)
00031 {
00032   /* this test covers the following files:
00033      Cross.h Quaternion.h, Transform.cpp
00034   */
00035 
00036   typedef Matrix<Scalar,2,2> Matrix2;
00037   typedef Matrix<Scalar,3,3> Matrix3;
00038   typedef Matrix<Scalar,4,4> Matrix4;
00039   typedef Matrix<Scalar,2,1> Vector2;
00040   typedef Matrix<Scalar,3,1> Vector3;
00041   typedef Matrix<Scalar,4,1> Vector4;
00042   typedef Quaternion<Scalar> Quaternionx;
00043   typedef AngleAxis<Scalar> AngleAxisx;
00044   typedef Transform<Scalar,2> Transform2;
00045   typedef Transform<Scalar,3> Transform3;
00046   typedef Scaling<Scalar,2> Scaling2;
00047   typedef Scaling<Scalar,3> Scaling3;
00048   typedef Translation<Scalar,2> Translation2;
00049   typedef Translation<Scalar,3> Translation3;
00050 
00051   Scalar largeEps = test_precision<Scalar>();
00052   if (ei_is_same_type<Scalar,float>::ret)
00053     largeEps = 1e-2f;
00054 
00055   Vector3 v0 = Vector3::Random(),
00056     v1 = Vector3::Random(),
00057     v2 = Vector3::Random();
00058   Vector2 u0 = Vector2::Random();
00059   Matrix3 matrot1;
00060 
00061   Scalar a = ei_random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
00062 
00063   // cross product
00064   VERIFY_IS_MUCH_SMALLER_THAN(v1.cross(v2).eigen2_dot(v1), Scalar(1));
00065   Matrix3 m;
00066   m << v0.normalized(),
00067       (v0.cross(v1)).normalized(),
00068       (v0.cross(v1).cross(v0)).normalized();
00069   VERIFY(m.isUnitary());
00070 
00071   // Quaternion: Identity(), setIdentity();
00072   Quaternionx q1, q2;
00073   q2.setIdentity();
00074   VERIFY_IS_APPROX(Quaternionx(Quaternionx::Identity()).coeffs(), q2.coeffs());
00075   q1.coeffs().setRandom();
00076   VERIFY_IS_APPROX(q1.coeffs(), (q1*q2).coeffs());
00077 
00078   // unitOrthogonal
00079   VERIFY_IS_MUCH_SMALLER_THAN(u0.unitOrthogonal().eigen2_dot(u0), Scalar(1));
00080   VERIFY_IS_MUCH_SMALLER_THAN(v0.unitOrthogonal().eigen2_dot(v0), Scalar(1));
00081   VERIFY_IS_APPROX(u0.unitOrthogonal().norm(), Scalar(1));
00082   VERIFY_IS_APPROX(v0.unitOrthogonal().norm(), Scalar(1));
00083 
00084 
00085   VERIFY_IS_APPROX(v0, AngleAxisx(a, v0.normalized()) * v0);
00086   VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(M_PI), v0.unitOrthogonal()) * v0);
00087   VERIFY_IS_APPROX(ei_cos(a)*v0.squaredNorm(), v0.eigen2_dot(AngleAxisx(a, v0.unitOrthogonal()) * v0));
00088   m = AngleAxisx(a, v0.normalized()).toRotationMatrix().adjoint();
00089   VERIFY_IS_APPROX(Matrix3::Identity(), m * AngleAxisx(a, v0.normalized()));
00090   VERIFY_IS_APPROX(Matrix3::Identity(), AngleAxisx(a, v0.normalized()) * m);
00091 
00092   q1 = AngleAxisx(a, v0.normalized());
00093   q2 = AngleAxisx(a, v1.normalized());
00094 
00095   // angular distance
00096   Scalar refangle = ei_abs(AngleAxisx(q1.inverse()*q2).angle());
00097   if (refangle>Scalar(M_PI))
00098     refangle = Scalar(2)*Scalar(M_PI) - refangle;
00099   
00100   if((q1.coeffs()-q2.coeffs()).norm() > 10*largeEps)
00101   {
00102     VERIFY(ei_isApprox(q1.angularDistance(q2), refangle, largeEps));
00103   }
00104 
00105   // rotation matrix conversion
00106   VERIFY_IS_APPROX(q1 * v2, q1.toRotationMatrix() * v2);
00107   VERIFY_IS_APPROX(q1 * q2 * v2,
00108     q1.toRotationMatrix() * q2.toRotationMatrix() * v2);
00109 
00110   VERIFY( (q2*q1).isApprox(q1*q2, largeEps) || !(q2 * q1 * v2).isApprox(
00111     q1.toRotationMatrix() * q2.toRotationMatrix() * v2));
00112 
00113   q2 = q1.toRotationMatrix();
00114   VERIFY_IS_APPROX(q1*v1,q2*v1);
00115 
00116   matrot1 = AngleAxisx(Scalar(0.1), Vector3::UnitX())
00117           * AngleAxisx(Scalar(0.2), Vector3::UnitY())
00118           * AngleAxisx(Scalar(0.3), Vector3::UnitZ());
00119   VERIFY_IS_APPROX(matrot1 * v1,
00120        AngleAxisx(Scalar(0.1), Vector3(1,0,0)).toRotationMatrix()
00121     * (AngleAxisx(Scalar(0.2), Vector3(0,1,0)).toRotationMatrix()
00122     * (AngleAxisx(Scalar(0.3), Vector3(0,0,1)).toRotationMatrix() * v1)));
00123 
00124   // angle-axis conversion
00125   AngleAxisx aa = q1;
00126   VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1);
00127   VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1);
00128 
00129   // from two vector creation
00130   VERIFY_IS_APPROX(v2.normalized(),(q2.setFromTwoVectors(v1,v2)*v1).normalized());
00131   VERIFY_IS_APPROX(v2.normalized(),(q2.setFromTwoVectors(v1,v2)*v1).normalized());
00132 
00133   // inverse and conjugate
00134   VERIFY_IS_APPROX(q1 * (q1.inverse() * v1), v1);
00135   VERIFY_IS_APPROX(q1 * (q1.conjugate() * v1), v1);
00136 
00137   // AngleAxis
00138   VERIFY_IS_APPROX(AngleAxisx(a,v1.normalized()).toRotationMatrix(),
00139     Quaternionx(AngleAxisx(a,v1.normalized())).toRotationMatrix());
00140 
00141   AngleAxisx aa1;
00142   m = q1.toRotationMatrix();
00143   aa1 = m;
00144   VERIFY_IS_APPROX(AngleAxisx(m).toRotationMatrix(),
00145     Quaternionx(m).toRotationMatrix());
00146 
00147   // Transform
00148   // TODO complete the tests !
00149   a = 0;
00150   while (ei_abs(a)<Scalar(0.1))
00151     a = ei_random<Scalar>(-Scalar(0.4)*Scalar(M_PI), Scalar(0.4)*Scalar(M_PI));
00152   q1 = AngleAxisx(a, v0.normalized());
00153   Transform3 t0, t1, t2;
00154   // first test setIdentity() and Identity()
00155   t0.setIdentity();
00156   VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
00157   t0.matrix().setZero();
00158   t0 = Transform3::Identity();
00159   VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity());
00160 
00161   t0.linear() = q1.toRotationMatrix();
00162   t1.setIdentity();
00163   t1.linear() = q1.toRotationMatrix();
00164 
00165   v0 << 50, 2, 1;//= ei_random_matrix<Vector3>().cwiseProduct(Vector3(10,2,0.5));
00166   t0.scale(v0);
00167   t1.prescale(v0);
00168 
00169   VERIFY_IS_APPROX( (t0 * Vector3(1,0,0)).norm(), v0.x());
00170   //VERIFY(!ei_isApprox((t1 * Vector3(1,0,0)).norm(), v0.x()));
00171 
00172   t0.setIdentity();
00173   t1.setIdentity();
00174   v1 << 1, 2, 3;
00175   t0.linear() = q1.toRotationMatrix();
00176   t0.pretranslate(v0);
00177   t0.scale(v1);
00178   t1.linear() = q1.conjugate().toRotationMatrix();
00179   t1.prescale(v1.cwise().inverse());
00180   t1.translate(-v0);
00181 
00182   VERIFY((t0.matrix() * t1.matrix()).isIdentity(test_precision<Scalar>()));
00183 
00184   t1.fromPositionOrientationScale(v0, q1, v1);
00185   VERIFY_IS_APPROX(t1.matrix(), t0.matrix());
00186   VERIFY_IS_APPROX(t1*v1, t0*v1);
00187 
00188   t0.setIdentity(); t0.scale(v0).rotate(q1.toRotationMatrix());
00189   t1.setIdentity(); t1.scale(v0).rotate(q1);
00190   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00191 
00192   t0.setIdentity(); t0.scale(v0).rotate(AngleAxisx(q1));
00193   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00194 
00195   VERIFY_IS_APPROX(t0.scale(a).matrix(), t1.scale(Vector3::Constant(a)).matrix());
00196   VERIFY_IS_APPROX(t0.prescale(a).matrix(), t1.prescale(Vector3::Constant(a)).matrix());
00197 
00198   // More transform constructors, operator=, operator*=
00199 
00200   Matrix3 mat3 = Matrix3::Random();
00201   Matrix4 mat4;
00202   mat4 << mat3 , Vector3::Zero() , Vector4::Zero().transpose();
00203   Transform3 tmat3(mat3), tmat4(mat4);
00204   tmat4.matrix()(3,3) = Scalar(1);
00205   VERIFY_IS_APPROX(tmat3.matrix(), tmat4.matrix());
00206 
00207   Scalar a3 = ei_random<Scalar>(-Scalar(M_PI), Scalar(M_PI));
00208   Vector3 v3 = Vector3::Random().normalized();
00209   AngleAxisx aa3(a3, v3);
00210   Transform3 t3(aa3);
00211   Transform3 t4;
00212   t4 = aa3;
00213   VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
00214   t4.rotate(AngleAxisx(-a3,v3));
00215   VERIFY_IS_APPROX(t4.matrix(), Matrix4::Identity());
00216   t4 *= aa3;
00217   VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
00218 
00219   v3 = Vector3::Random();
00220   Translation3 tv3(v3);
00221   Transform3 t5(tv3);
00222   t4 = tv3;
00223   VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
00224   t4.translate(-v3);
00225   VERIFY_IS_APPROX(t4.matrix(), Matrix4::Identity());
00226   t4 *= tv3;
00227   VERIFY_IS_APPROX(t5.matrix(), t4.matrix());
00228 
00229   Scaling3 sv3(v3);
00230   Transform3 t6(sv3);
00231   t4 = sv3;
00232   VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
00233   t4.scale(v3.cwise().inverse());
00234   VERIFY_IS_APPROX(t4.matrix(), Matrix4::Identity());
00235   t4 *= sv3;
00236   VERIFY_IS_APPROX(t6.matrix(), t4.matrix());
00237 
00238   // matrix * transform
00239   VERIFY_IS_APPROX(Transform3(t3.matrix()*t4).matrix(), Transform3(t3*t4).matrix());
00240 
00241   // chained Transform product
00242   VERIFY_IS_APPROX(((t3*t4)*t5).matrix(), (t3*(t4*t5)).matrix());
00243 
00244   // check that Transform product doesn't have aliasing problems
00245   t5 = t4;
00246   t5 = t5*t5;
00247   VERIFY_IS_APPROX(t5, t4*t4);
00248 
00249   // 2D transformation
00250   Transform2 t20, t21;
00251   Vector2 v20 = Vector2::Random();
00252   Vector2 v21 = Vector2::Random();
00253   for (int k=0; k<2; ++k)
00254     if (ei_abs(v21[k])<Scalar(1e-3)) v21[k] = Scalar(1e-3);
00255   t21.setIdentity();
00256   t21.linear() = Rotation2D<Scalar>(a).toRotationMatrix();
00257   VERIFY_IS_APPROX(t20.fromPositionOrientationScale(v20,a,v21).matrix(),
00258     t21.pretranslate(v20).scale(v21).matrix());
00259 
00260   t21.setIdentity();
00261   t21.linear() = Rotation2D<Scalar>(-a).toRotationMatrix();
00262   VERIFY( (t20.fromPositionOrientationScale(v20,a,v21)
00263         * (t21.prescale(v21.cwise().inverse()).translate(-v20))).matrix().isIdentity(test_precision<Scalar>()) );
00264 
00265   // Transform - new API
00266   // 3D
00267   t0.setIdentity();
00268   t0.rotate(q1).scale(v0).translate(v0);
00269   // mat * scaling and mat * translation
00270   t1 = (Matrix3(q1) * Scaling3(v0)) * Translation3(v0);
00271   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00272   // mat * transformation and scaling * translation
00273   t1 = Matrix3(q1) * (Scaling3(v0) * Translation3(v0));
00274   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00275 
00276   t0.setIdentity();
00277   t0.prerotate(q1).prescale(v0).pretranslate(v0);
00278   // translation * scaling and transformation * mat
00279   t1 = (Translation3(v0) * Scaling3(v0)) * Matrix3(q1);
00280   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00281   // scaling * mat and translation * mat
00282   t1 = Translation3(v0) * (Scaling3(v0) * Matrix3(q1));
00283   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00284 
00285   t0.setIdentity();
00286   t0.scale(v0).translate(v0).rotate(q1);
00287   // translation * mat and scaling * transformation
00288   t1 = Scaling3(v0) * (Translation3(v0) * Matrix3(q1));
00289   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00290   // transformation * scaling
00291   t0.scale(v0);
00292   t1 = t1 * Scaling3(v0);
00293   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00294   // transformation * translation
00295   t0.translate(v0);
00296   t1 = t1 * Translation3(v0);
00297   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00298   // translation * transformation
00299   t0.pretranslate(v0);
00300   t1 = Translation3(v0) * t1;
00301   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00302 
00303   // transform * quaternion
00304   t0.rotate(q1);
00305   t1 = t1 * q1;
00306   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00307 
00308   // translation * quaternion
00309   t0.translate(v1).rotate(q1);
00310   t1 = t1 * (Translation3(v1) * q1);
00311   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00312 
00313   // scaling * quaternion
00314   t0.scale(v1).rotate(q1);
00315   t1 = t1 * (Scaling3(v1) * q1);
00316   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00317 
00318   // quaternion * transform
00319   t0.prerotate(q1);
00320   t1 = q1 * t1;
00321   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00322 
00323   // quaternion * translation
00324   t0.rotate(q1).translate(v1);
00325   t1 = t1 * (q1 * Translation3(v1));
00326   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00327 
00328   // quaternion * scaling
00329   t0.rotate(q1).scale(v1);
00330   t1 = t1 * (q1 * Scaling3(v1));
00331   VERIFY_IS_APPROX(t0.matrix(), t1.matrix());
00332 
00333   // translation * vector
00334   t0.setIdentity();
00335   t0.translate(v0);
00336   VERIFY_IS_APPROX(t0 * v1, Translation3(v0) * v1);
00337 
00338   // scaling * vector
00339   t0.setIdentity();
00340   t0.scale(v0);
00341   VERIFY_IS_APPROX(t0 * v1, Scaling3(v0) * v1);
00342 
00343   // test transform inversion
00344   t0.setIdentity();
00345   t0.translate(v0);
00346   t0.linear().setRandom();
00347   VERIFY_IS_APPROX(t0.inverse(Affine), t0.matrix().inverse());
00348   t0.setIdentity();
00349   t0.translate(v0).rotate(q1);
00350   VERIFY_IS_APPROX(t0.inverse(Isometry), t0.matrix().inverse());
00351 
00352   // test extract rotation and scaling
00353   t0.setIdentity();
00354   t0.translate(v0).rotate(q1).scale(v1);
00355   VERIFY_IS_APPROX(t0.rotation() * v1, Matrix3(q1) * v1);
00356 
00357   Matrix3 mat_rotation, mat_scaling;
00358   t0.setIdentity();
00359   t0.translate(v0).rotate(q1).scale(v1);
00360   t0.computeRotationScaling(&mat_rotation, &mat_scaling);
00361   VERIFY_IS_APPROX(t0.linear(), mat_rotation * mat_scaling);
00362   VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
00363   VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
00364   t0.computeScalingRotation(&mat_scaling, &mat_rotation);
00365   VERIFY_IS_APPROX(t0.linear(), mat_scaling * mat_rotation);
00366   VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity());
00367   VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1));
00368 
00369   // test casting
00370   Transform<float,3> t1f = t1.template cast<float>();
00371   VERIFY_IS_APPROX(t1f.template cast<Scalar>(),t1);
00372   Transform<double,3> t1d = t1.template cast<double>();
00373   VERIFY_IS_APPROX(t1d.template cast<Scalar>(),t1);
00374 
00375   Translation3 tr1(v0);
00376   Translation<float,3> tr1f = tr1.template cast<float>();
00377   VERIFY_IS_APPROX(tr1f.template cast<Scalar>(),tr1);
00378   Translation<double,3> tr1d = tr1.template cast<double>();
00379   VERIFY_IS_APPROX(tr1d.template cast<Scalar>(),tr1);
00380 
00381   Scaling3 sc1(v0);
00382   Scaling<float,3> sc1f = sc1.template cast<float>();
00383   VERIFY_IS_APPROX(sc1f.template cast<Scalar>(),sc1);
00384   Scaling<double,3> sc1d = sc1.template cast<double>();
00385   VERIFY_IS_APPROX(sc1d.template cast<Scalar>(),sc1);
00386 
00387   Quaternion<float> q1f = q1.template cast<float>();
00388   VERIFY_IS_APPROX(q1f.template cast<Scalar>(),q1);
00389   Quaternion<double> q1d = q1.template cast<double>();
00390   VERIFY_IS_APPROX(q1d.template cast<Scalar>(),q1);
00391 
00392   AngleAxis<float> aa1f = aa1.template cast<float>();
00393   VERIFY_IS_APPROX(aa1f.template cast<Scalar>(),aa1);
00394   AngleAxis<double> aa1d = aa1.template cast<double>();
00395   VERIFY_IS_APPROX(aa1d.template cast<Scalar>(),aa1);
00396 
00397   Rotation2D<Scalar> r2d1(ei_random<Scalar>());
00398   Rotation2D<float> r2d1f = r2d1.template cast<float>();
00399   VERIFY_IS_APPROX(r2d1f.template cast<Scalar>(),r2d1);
00400   Rotation2D<double> r2d1d = r2d1.template cast<double>();
00401   VERIFY_IS_APPROX(r2d1d.template cast<Scalar>(),r2d1);
00402 
00403   m = q1;
00404 //   m.col(1) = Vector3(0,ei_random<Scalar>(),ei_random<Scalar>()).normalized();
00405 //   m.col(0) = Vector3(-1,0,0).normalized();
00406 //   m.col(2) = m.col(0).cross(m.col(1));
00407   #define VERIFY_EULER(I,J,K, X,Y,Z) { \
00408     Vector3 ea = m.eulerAngles(I,J,K); \
00409     Matrix3 m1 = Matrix3(AngleAxisx(ea[0], Vector3::Unit##X()) * AngleAxisx(ea[1], Vector3::Unit##Y()) * AngleAxisx(ea[2], Vector3::Unit##Z())); \
00410     VERIFY_IS_APPROX(m,  Matrix3(AngleAxisx(ea[0], Vector3::Unit##X()) * AngleAxisx(ea[1], Vector3::Unit##Y()) * AngleAxisx(ea[2], Vector3::Unit##Z()))); \
00411   }
00412   VERIFY_EULER(0,1,2, X,Y,Z);
00413   VERIFY_EULER(0,1,0, X,Y,X);
00414   VERIFY_EULER(0,2,1, X,Z,Y);
00415   VERIFY_EULER(0,2,0, X,Z,X);
00416 
00417   VERIFY_EULER(1,2,0, Y,Z,X);
00418   VERIFY_EULER(1,2,1, Y,Z,Y);
00419   VERIFY_EULER(1,0,2, Y,X,Z);
00420   VERIFY_EULER(1,0,1, Y,X,Y);
00421 
00422   VERIFY_EULER(2,0,1, Z,X,Y);
00423   VERIFY_EULER(2,0,2, Z,X,Z);
00424   VERIFY_EULER(2,1,0, Z,Y,X);
00425   VERIFY_EULER(2,1,2, Z,Y,Z);
00426 
00427   // colwise/rowwise cross product
00428   mat3.setRandom();
00429   Vector3 vec3 = Vector3::Random();
00430   Matrix3 mcross;
00431   int i = ei_random<int>(0,2);
00432   mcross = mat3.colwise().cross(vec3);
00433   VERIFY_IS_APPROX(mcross.col(i), mat3.col(i).cross(vec3));
00434   mcross = mat3.rowwise().cross(vec3);
00435   VERIFY_IS_APPROX(mcross.row(i), mat3.row(i).cross(vec3));
00436 
00437 
00438 }
00439 
00440 void test_eigen2_geometry()
00441 {
00442   for(int i = 0; i < g_repeat; i++) {
00443     CALL_SUBTEST_1( geometry<float>() );
00444     CALL_SUBTEST_2( geometry<double>() );
00445   }
00446 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:03