// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008-2009 Gael Guennebaud // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" #include #include #include template void non_projective_only() { /* this test covers the following files: Cross.h Quaternion.h, Transform.cpp */ typedef Matrix Vector3; typedef Quaternion Quaternionx; typedef AngleAxis AngleAxisx; typedef Transform Transform3; typedef DiagonalMatrix AlignedScaling3; typedef Translation Translation3; Vector3 v0 = Vector3::Random(), v1 = Vector3::Random(); Transform3 t0, t1, t2; Scalar a = internal::random(-Scalar(M_PI), Scalar(M_PI)); Quaternionx q1, q2; q1 = AngleAxisx(a, v0.normalized()); t0 = Transform3::Identity(); VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity()); t0.linear() = q1.toRotationMatrix(); v0 << 50, 2, 1; t0.scale(v0); VERIFY_IS_APPROX( (t0 * Vector3(1,0,0)).template head<3>().norm(), v0.x()); t0.setIdentity(); t1.setIdentity(); v1 << 1, 2, 3; t0.linear() = q1.toRotationMatrix(); t0.pretranslate(v0); t0.scale(v1); t1.linear() = q1.conjugate().toRotationMatrix(); t1.prescale(v1.cwiseInverse()); t1.translate(-v0); VERIFY((t0 * t1).matrix().isIdentity(test_precision())); t1.fromPositionOrientationScale(v0, q1, v1); VERIFY_IS_APPROX(t1.matrix(), t0.matrix()); VERIFY_IS_APPROX(t1*v1, t0*v1); // translation * vector t0.setIdentity(); t0.translate(v0); VERIFY_IS_APPROX((t0 * v1).template head<3>(), Translation3(v0) * v1); // AlignedScaling * vector t0.setIdentity(); t0.scale(v0); VERIFY_IS_APPROX((t0 * v1).template head<3>(), AlignedScaling3(v0) * v1); } template void transformations() { /* this test covers the following files: Cross.h Quaternion.h, Transform.cpp */ using std::cos; using std::abs; typedef Matrix Matrix3; typedef Matrix Matrix4; typedef Matrix Vector2; typedef Matrix Vector3; typedef Matrix Vector4; typedef Quaternion Quaternionx; typedef AngleAxis AngleAxisx; typedef Transform Transform2; typedef Transform Transform3; typedef typename Transform3::MatrixType MatrixType; typedef DiagonalMatrix AlignedScaling3; typedef Translation Translation2; typedef Translation Translation3; Vector3 v0 = Vector3::Random(), v1 = Vector3::Random(); Matrix3 matrot1, m; Scalar a = internal::random(-Scalar(M_PI), Scalar(M_PI)); Scalar s0 = internal::random(); VERIFY_IS_APPROX(v0, AngleAxisx(a, v0.normalized()) * v0); VERIFY_IS_APPROX(-v0, AngleAxisx(Scalar(M_PI), v0.unitOrthogonal()) * v0); VERIFY_IS_APPROX(cos(a)*v0.squaredNorm(), v0.dot(AngleAxisx(a, v0.unitOrthogonal()) * v0)); m = AngleAxisx(a, v0.normalized()).toRotationMatrix().adjoint(); VERIFY_IS_APPROX(Matrix3::Identity(), m * AngleAxisx(a, v0.normalized())); VERIFY_IS_APPROX(Matrix3::Identity(), AngleAxisx(a, v0.normalized()) * m); Quaternionx q1, q2; q1 = AngleAxisx(a, v0.normalized()); q2 = AngleAxisx(a, v1.normalized()); // rotation matrix conversion matrot1 = AngleAxisx(Scalar(0.1), Vector3::UnitX()) * AngleAxisx(Scalar(0.2), Vector3::UnitY()) * AngleAxisx(Scalar(0.3), Vector3::UnitZ()); VERIFY_IS_APPROX(matrot1 * v1, AngleAxisx(Scalar(0.1), Vector3(1,0,0)).toRotationMatrix() * (AngleAxisx(Scalar(0.2), Vector3(0,1,0)).toRotationMatrix() * (AngleAxisx(Scalar(0.3), Vector3(0,0,1)).toRotationMatrix() * v1))); // angle-axis conversion AngleAxisx aa = AngleAxisx(q1); VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1); VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1); aa.fromRotationMatrix(aa.toRotationMatrix()); VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1); VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1); // AngleAxis VERIFY_IS_APPROX(AngleAxisx(a,v1.normalized()).toRotationMatrix(), Quaternionx(AngleAxisx(a,v1.normalized())).toRotationMatrix()); AngleAxisx aa1; m = q1.toRotationMatrix(); aa1 = m; VERIFY_IS_APPROX(AngleAxisx(m).toRotationMatrix(), Quaternionx(m).toRotationMatrix()); // Transform // TODO complete the tests ! a = 0; while (abs(a)(-Scalar(0.4)*Scalar(M_PI), Scalar(0.4)*Scalar(M_PI)); q1 = AngleAxisx(a, v0.normalized()); Transform3 t0, t1, t2; // first test setIdentity() and Identity() t0.setIdentity(); VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity()); t0.matrix().setZero(); t0 = Transform3::Identity(); VERIFY_IS_APPROX(t0.matrix(), Transform3::MatrixType::Identity()); t0.setIdentity(); t1.setIdentity(); v1 << 1, 2, 3; t0.linear() = q1.toRotationMatrix(); t0.pretranslate(v0); t0.scale(v1); t1.linear() = q1.conjugate().toRotationMatrix(); t1.prescale(v1.cwiseInverse()); t1.translate(-v0); VERIFY((t0 * t1).matrix().isIdentity(test_precision())); t1.fromPositionOrientationScale(v0, q1, v1); VERIFY_IS_APPROX(t1.matrix(), t0.matrix()); t0.setIdentity(); t0.scale(v0).rotate(q1.toRotationMatrix()); t1.setIdentity(); t1.scale(v0).rotate(q1); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.setIdentity(); t0.scale(v0).rotate(AngleAxisx(q1)); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); VERIFY_IS_APPROX(t0.scale(a).matrix(), t1.scale(Vector3::Constant(a)).matrix()); VERIFY_IS_APPROX(t0.prescale(a).matrix(), t1.prescale(Vector3::Constant(a)).matrix()); // More transform constructors, operator=, operator*= Matrix3 mat3 = Matrix3::Random(); Matrix4 mat4; mat4 << mat3 , Vector3::Zero() , Vector4::Zero().transpose(); Transform3 tmat3(mat3), tmat4(mat4); if(Mode!=int(AffineCompact)) tmat4.matrix()(3,3) = Scalar(1); VERIFY_IS_APPROX(tmat3.matrix(), tmat4.matrix()); Scalar a3 = internal::random(-Scalar(M_PI), Scalar(M_PI)); Vector3 v3 = Vector3::Random().normalized(); AngleAxisx aa3(a3, v3); Transform3 t3(aa3); Transform3 t4; t4 = aa3; VERIFY_IS_APPROX(t3.matrix(), t4.matrix()); t4.rotate(AngleAxisx(-a3,v3)); VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity()); t4 *= aa3; VERIFY_IS_APPROX(t3.matrix(), t4.matrix()); v3 = Vector3::Random(); Translation3 tv3(v3); Transform3 t5(tv3); t4 = tv3; VERIFY_IS_APPROX(t5.matrix(), t4.matrix()); t4.translate(-v3); VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity()); t4 *= tv3; VERIFY_IS_APPROX(t5.matrix(), t4.matrix()); AlignedScaling3 sv3(v3); Transform3 t6(sv3); t4 = sv3; VERIFY_IS_APPROX(t6.matrix(), t4.matrix()); t4.scale(v3.cwiseInverse()); VERIFY_IS_APPROX(t4.matrix(), MatrixType::Identity()); t4 *= sv3; VERIFY_IS_APPROX(t6.matrix(), t4.matrix()); // matrix * transform VERIFY_IS_APPROX((t3.matrix()*t4).matrix(), (t3*t4).matrix()); // chained Transform product VERIFY_IS_APPROX(((t3*t4)*t5).matrix(), (t3*(t4*t5)).matrix()); // check that Transform product doesn't have aliasing problems t5 = t4; t5 = t5*t5; VERIFY_IS_APPROX(t5, t4*t4); // 2D transformation Transform2 t20, t21; Vector2 v20 = Vector2::Random(); Vector2 v21 = Vector2::Random(); for (int k=0; k<2; ++k) if (abs(v21[k])(a).toRotationMatrix(); VERIFY_IS_APPROX(t20.fromPositionOrientationScale(v20,a,v21).matrix(), t21.pretranslate(v20).scale(v21).matrix()); t21.setIdentity(); t21.linear() = Rotation2D(-a).toRotationMatrix(); VERIFY( (t20.fromPositionOrientationScale(v20,a,v21) * (t21.prescale(v21.cwiseInverse()).translate(-v20))).matrix().isIdentity(test_precision()) ); // Transform - new API // 3D t0.setIdentity(); t0.rotate(q1).scale(v0).translate(v0); // mat * aligned scaling and mat * translation t1 = (Matrix3(q1) * AlignedScaling3(v0)) * Translation3(v0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t1 = (Matrix3(q1) * Eigen::Scaling(v0)) * Translation3(v0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t1 = (q1 * Eigen::Scaling(v0)) * Translation3(v0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // mat * transformation and aligned scaling * translation t1 = Matrix3(q1) * (AlignedScaling3(v0) * Translation3(v0)); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.setIdentity(); t0.scale(s0).translate(v0); t1 = Eigen::Scaling(s0) * Translation3(v0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.prescale(s0); t1 = Eigen::Scaling(s0) * t1; VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0 = t3; t0.scale(s0); t1 = t3 * Eigen::Scaling(s0,s0,s0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.prescale(s0); t1 = Eigen::Scaling(s0,s0,s0) * t1; VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0 = t3; t0.scale(s0); t1 = t3 * Eigen::Scaling(s0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.prescale(s0); t1 = Eigen::Scaling(s0) * t1; VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.setIdentity(); t0.prerotate(q1).prescale(v0).pretranslate(v0); // translation * aligned scaling and transformation * mat t1 = (Translation3(v0) * AlignedScaling3(v0)) * Transform3(q1); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // scaling * mat and translation * mat t1 = Translation3(v0) * (AlignedScaling3(v0) * Transform3(q1)); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); t0.setIdentity(); t0.scale(v0).translate(v0).rotate(q1); // translation * mat and aligned scaling * transformation t1 = AlignedScaling3(v0) * (Translation3(v0) * Transform3(q1)); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // transformation * aligned scaling t0.scale(v0); t1 *= AlignedScaling3(v0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // transformation * translation t0.translate(v0); t1 = t1 * Translation3(v0); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // translation * transformation t0.pretranslate(v0); t1 = Translation3(v0) * t1; VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // transform * quaternion t0.rotate(q1); t1 = t1 * q1; VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // translation * quaternion t0.translate(v1).rotate(q1); t1 = t1 * (Translation3(v1) * q1); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // aligned scaling * quaternion t0.scale(v1).rotate(q1); t1 = t1 * (AlignedScaling3(v1) * q1); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // quaternion * transform t0.prerotate(q1); t1 = q1 * t1; VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // quaternion * translation t0.rotate(q1).translate(v1); t1 = t1 * (q1 * Translation3(v1)); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // quaternion * aligned scaling t0.rotate(q1).scale(v1); t1 = t1 * (q1 * AlignedScaling3(v1)); VERIFY_IS_APPROX(t0.matrix(), t1.matrix()); // test transform inversion t0.setIdentity(); t0.translate(v0); t0.linear().setRandom(); Matrix4 t044 = Matrix4::Zero(); t044(3,3) = 1; t044.block(0,0,t0.matrix().rows(),4) = t0.matrix(); VERIFY_IS_APPROX(t0.inverse(Affine).matrix(), t044.inverse().block(0,0,t0.matrix().rows(),4)); t0.setIdentity(); t0.translate(v0).rotate(q1); t044 = Matrix4::Zero(); t044(3,3) = 1; t044.block(0,0,t0.matrix().rows(),4) = t0.matrix(); VERIFY_IS_APPROX(t0.inverse(Isometry).matrix(), t044.inverse().block(0,0,t0.matrix().rows(),4)); Matrix3 mat_rotation, mat_scaling; t0.setIdentity(); t0.translate(v0).rotate(q1).scale(v1); t0.computeRotationScaling(&mat_rotation, &mat_scaling); VERIFY_IS_APPROX(t0.linear(), mat_rotation * mat_scaling); VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity()); VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1)); t0.computeScalingRotation(&mat_scaling, &mat_rotation); VERIFY_IS_APPROX(t0.linear(), mat_scaling * mat_rotation); VERIFY_IS_APPROX(mat_rotation*mat_rotation.adjoint(), Matrix3::Identity()); VERIFY_IS_APPROX(mat_rotation.determinant(), Scalar(1)); // test casting Transform t1f = t1.template cast(); VERIFY_IS_APPROX(t1f.template cast(),t1); Transform t1d = t1.template cast(); VERIFY_IS_APPROX(t1d.template cast(),t1); Translation3 tr1(v0); Translation tr1f = tr1.template cast(); VERIFY_IS_APPROX(tr1f.template cast(),tr1); Translation tr1d = tr1.template cast(); VERIFY_IS_APPROX(tr1d.template cast(),tr1); AngleAxis aa1f = aa1.template cast(); VERIFY_IS_APPROX(aa1f.template cast(),aa1); AngleAxis aa1d = aa1.template cast(); VERIFY_IS_APPROX(aa1d.template cast(),aa1); Rotation2D r2d1(internal::random()); Rotation2D r2d1f = r2d1.template cast(); VERIFY_IS_APPROX(r2d1f.template cast(),r2d1); Rotation2D r2d1d = r2d1.template cast(); VERIFY_IS_APPROX(r2d1d.template cast(),r2d1); t20 = Translation2(v20) * (Rotation2D(s0) * Eigen::Scaling(s0)); t21 = Translation2(v20) * Rotation2D(s0) * Eigen::Scaling(s0); VERIFY_IS_APPROX(t20,t21); // check basic features { Rotation2D r1; // default ctor r1 = Rotation2D(s0); // copy assignment VERIFY_IS_APPROX(r1.angle(),s0); Rotation2D r2(r1); // copy ctor VERIFY_IS_APPROX(r2.angle(),s0); } } template void transform_alignment() { typedef Transform Projective3a; typedef Transform Projective3u; EIGEN_ALIGN16 Scalar array1[16]; EIGEN_ALIGN16 Scalar array2[16]; EIGEN_ALIGN16 Scalar array3[16+1]; Scalar* array3u = array3+1; Projective3a *p1 = ::new(reinterpret_cast(array1)) Projective3a; Projective3u *p2 = ::new(reinterpret_cast(array2)) Projective3u; Projective3u *p3 = ::new(reinterpret_cast(array3u)) Projective3u; p1->matrix().setRandom(); *p2 = *p1; *p3 = *p1; VERIFY_IS_APPROX(p1->matrix(), p2->matrix()); VERIFY_IS_APPROX(p1->matrix(), p3->matrix()); VERIFY_IS_APPROX( (*p1) * (*p1), (*p2)*(*p3)); #if defined(EIGEN_VECTORIZE) && EIGEN_ALIGN_STATICALLY if(internal::packet_traits::Vectorizable) VERIFY_RAISES_ASSERT((::new(reinterpret_cast(array3u)) Projective3a)); #endif } template void transform_products() { typedef Matrix Mat; typedef Transform Proj; typedef Transform Aff; typedef Transform AffC; Proj p; p.matrix().setRandom(); Aff a; a.linear().setRandom(); a.translation().setRandom(); AffC ac = a; Mat p_m(p.matrix()), a_m(a.matrix()); VERIFY_IS_APPROX((p*p).matrix(), p_m*p_m); VERIFY_IS_APPROX((a*a).matrix(), a_m*a_m); VERIFY_IS_APPROX((p*a).matrix(), p_m*a_m); VERIFY_IS_APPROX((a*p).matrix(), a_m*p_m); VERIFY_IS_APPROX((ac*a).matrix(), a_m*a_m); VERIFY_IS_APPROX((a*ac).matrix(), a_m*a_m); VERIFY_IS_APPROX((p*ac).matrix(), p_m*a_m); VERIFY_IS_APPROX((ac*p).matrix(), a_m*p_m); } void test_geo_transformations() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1(( transformations() )); CALL_SUBTEST_1(( non_projective_only() )); CALL_SUBTEST_2(( transformations() )); CALL_SUBTEST_2(( non_projective_only() )); CALL_SUBTEST_2(( transform_alignment() )); CALL_SUBTEST_3(( transformations() )); CALL_SUBTEST_3(( transformations() )); CALL_SUBTEST_3(( transform_alignment() )); CALL_SUBTEST_4(( transformations() )); CALL_SUBTEST_4(( non_projective_only() )); CALL_SUBTEST_5(( transformations() )); CALL_SUBTEST_5(( non_projective_only() )); CALL_SUBTEST_6(( transformations() )); CALL_SUBTEST_6(( transformations() )); CALL_SUBTEST_7(( transform_products() )); CALL_SUBTEST_7(( transform_products() )); } }