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.

121 lines
5.0 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 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. template<typename Scalar,int Size> void homogeneous(void)
  12. {
  13. /* this test covers the following files:
  14. Homogeneous.h
  15. */
  16. typedef Matrix<Scalar,Size,Size> MatrixType;
  17. typedef Matrix<Scalar,Size,1, ColMajor> VectorType;
  18. typedef Matrix<Scalar,Size+1,Size> HMatrixType;
  19. typedef Matrix<Scalar,Size+1,1> HVectorType;
  20. typedef Matrix<Scalar,Size,Size+1> T1MatrixType;
  21. typedef Matrix<Scalar,Size+1,Size+1> T2MatrixType;
  22. typedef Matrix<Scalar,Size+1,Size> T3MatrixType;
  23. VectorType v0 = VectorType::Random(),
  24. ones = VectorType::Ones();
  25. HVectorType hv0 = HVectorType::Random();
  26. MatrixType m0 = MatrixType::Random();
  27. HMatrixType hm0 = HMatrixType::Random();
  28. hv0 << v0, 1;
  29. VERIFY_IS_APPROX(v0.homogeneous(), hv0);
  30. VERIFY_IS_APPROX(v0, hv0.hnormalized());
  31. VERIFY_IS_APPROX(v0.homogeneous().sum(), hv0.sum());
  32. VERIFY_IS_APPROX(v0.homogeneous().minCoeff(), hv0.minCoeff());
  33. VERIFY_IS_APPROX(v0.homogeneous().maxCoeff(), hv0.maxCoeff());
  34. hm0 << m0, ones.transpose();
  35. VERIFY_IS_APPROX(m0.colwise().homogeneous(), hm0);
  36. VERIFY_IS_APPROX(m0, hm0.colwise().hnormalized());
  37. hm0.row(Size-1).setRandom();
  38. for(int j=0; j<Size; ++j)
  39. m0.col(j) = hm0.col(j).head(Size) / hm0(Size,j);
  40. VERIFY_IS_APPROX(m0, hm0.colwise().hnormalized());
  41. T1MatrixType t1 = T1MatrixType::Random();
  42. VERIFY_IS_APPROX(t1 * (v0.homogeneous().eval()), t1 * v0.homogeneous());
  43. VERIFY_IS_APPROX(t1 * (m0.colwise().homogeneous().eval()), t1 * m0.colwise().homogeneous());
  44. T2MatrixType t2 = T2MatrixType::Random();
  45. VERIFY_IS_APPROX(t2 * (v0.homogeneous().eval()), t2 * v0.homogeneous());
  46. VERIFY_IS_APPROX(t2 * (m0.colwise().homogeneous().eval()), t2 * m0.colwise().homogeneous());
  47. VERIFY_IS_APPROX((v0.transpose().rowwise().homogeneous().eval()) * t2,
  48. v0.transpose().rowwise().homogeneous() * t2);
  49. VERIFY_IS_APPROX((m0.transpose().rowwise().homogeneous().eval()) * t2,
  50. m0.transpose().rowwise().homogeneous() * t2);
  51. T3MatrixType t3 = T3MatrixType::Random();
  52. VERIFY_IS_APPROX((v0.transpose().rowwise().homogeneous().eval()) * t3,
  53. v0.transpose().rowwise().homogeneous() * t3);
  54. VERIFY_IS_APPROX((m0.transpose().rowwise().homogeneous().eval()) * t3,
  55. m0.transpose().rowwise().homogeneous() * t3);
  56. // test product with a Transform object
  57. Transform<Scalar, Size, Affine> aff;
  58. Transform<Scalar, Size, AffineCompact> caff;
  59. Transform<Scalar, Size, Projective> proj;
  60. Matrix<Scalar, Size, Dynamic> pts;
  61. Matrix<Scalar, Size+1, Dynamic> pts1, pts2;
  62. aff.affine().setRandom();
  63. proj = caff = aff;
  64. pts.setRandom(Size,internal::random<int>(1,20));
  65. pts1 = pts.colwise().homogeneous();
  66. VERIFY_IS_APPROX(aff * pts.colwise().homogeneous(), (aff * pts1).colwise().hnormalized());
  67. VERIFY_IS_APPROX(caff * pts.colwise().homogeneous(), (caff * pts1).colwise().hnormalized());
  68. VERIFY_IS_APPROX(proj * pts.colwise().homogeneous(), (proj * pts1));
  69. VERIFY_IS_APPROX((aff * pts1).colwise().hnormalized(), aff * pts);
  70. VERIFY_IS_APPROX((caff * pts1).colwise().hnormalized(), caff * pts);
  71. pts2 = pts1;
  72. pts2.row(Size).setRandom();
  73. VERIFY_IS_APPROX((aff * pts2).colwise().hnormalized(), aff * pts2.colwise().hnormalized());
  74. VERIFY_IS_APPROX((caff * pts2).colwise().hnormalized(), caff * pts2.colwise().hnormalized());
  75. VERIFY_IS_APPROX((proj * pts2).colwise().hnormalized(), (proj * pts2.colwise().hnormalized().colwise().homogeneous()).colwise().hnormalized());
  76. // Test combination of homogeneous
  77. VERIFY_IS_APPROX( (t2 * v0.homogeneous()).hnormalized(),
  78. (t2.template topLeftCorner<Size,Size>() * v0 + t2.template topRightCorner<Size,1>())
  79. / ((t2.template bottomLeftCorner<1,Size>()*v0).value() + t2(Size,Size)) );
  80. VERIFY_IS_APPROX( (t2 * pts.colwise().homogeneous()).colwise().hnormalized(),
  81. (Matrix<Scalar, Size+1, Dynamic>(t2 * pts1).colwise().hnormalized()) );
  82. VERIFY_IS_APPROX( (t2 .lazyProduct( v0.homogeneous() )).hnormalized(), (t2 * v0.homogeneous()).hnormalized() );
  83. VERIFY_IS_APPROX( (t2 .lazyProduct ( pts.colwise().homogeneous() )).colwise().hnormalized(), (t2 * pts1).colwise().hnormalized() );
  84. VERIFY_IS_APPROX( (v0.transpose().homogeneous() .lazyProduct( t2 )).hnormalized(), (v0.transpose().homogeneous()*t2).hnormalized() );
  85. VERIFY_IS_APPROX( (pts.transpose().rowwise().homogeneous() .lazyProduct( t2 )).rowwise().hnormalized(), (pts1.transpose()*t2).rowwise().hnormalized() );
  86. }
  87. void test_geo_homogeneous()
  88. {
  89. for(int i = 0; i < g_repeat; i++) {
  90. CALL_SUBTEST_1(( homogeneous<float,1>() ));
  91. CALL_SUBTEST_2(( homogeneous<double,3>() ));
  92. CALL_SUBTEST_3(( homogeneous<double,8>() ));
  93. }
  94. }