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