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.

157 lines
5.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <Eigen/Geometry>
  12. #include <Eigen/LU>
  13. #include <Eigen/QR>
  14. template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
  15. {
  16. /* this test covers the following files:
  17. Hyperplane.h
  18. */
  19. typedef typename HyperplaneType::Index Index;
  20. const Index dim = _plane.dim();
  21. enum { Options = HyperplaneType::Options };
  22. typedef typename HyperplaneType::Scalar Scalar;
  23. typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
  24. typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
  25. HyperplaneType::AmbientDimAtCompileTime> MatrixType;
  26. VectorType p0 = VectorType::Random(dim);
  27. VectorType p1 = VectorType::Random(dim);
  28. VectorType n0 = VectorType::Random(dim).normalized();
  29. VectorType n1 = VectorType::Random(dim).normalized();
  30. HyperplaneType pl0(n0, p0);
  31. HyperplaneType pl1(n1, p1);
  32. HyperplaneType pl2 = pl1;
  33. Scalar s0 = internal::random<Scalar>();
  34. Scalar s1 = internal::random<Scalar>();
  35. VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
  36. VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
  37. VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0 );
  38. VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
  39. VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
  40. // transform
  41. if (!NumTraits<Scalar>::IsComplex)
  42. {
  43. MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
  44. DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
  45. Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
  46. pl2 = pl1;
  47. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
  48. pl2 = pl1;
  49. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
  50. pl2 = pl1;
  51. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
  52. pl2 = pl1;
  53. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
  54. .absDistance((rot*scaling*translation) * p1), Scalar(1) );
  55. pl2 = pl1;
  56. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
  57. .absDistance((rot*translation) * p1), Scalar(1) );
  58. }
  59. // casting
  60. const int Dim = HyperplaneType::AmbientDimAtCompileTime;
  61. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  62. Hyperplane<OtherScalar,Dim,Options> hp1f = pl1.template cast<OtherScalar>();
  63. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
  64. Hyperplane<Scalar,Dim,Options> hp1d = pl1.template cast<Scalar>();
  65. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
  66. }
  67. template<typename Scalar> void lines()
  68. {
  69. using std::abs;
  70. typedef Hyperplane<Scalar, 2> HLine;
  71. typedef ParametrizedLine<Scalar, 2> PLine;
  72. typedef Matrix<Scalar,2,1> Vector;
  73. typedef Matrix<Scalar,3,1> CoeffsType;
  74. for(int i = 0; i < 10; i++)
  75. {
  76. Vector center = Vector::Random();
  77. Vector u = Vector::Random();
  78. Vector v = Vector::Random();
  79. Scalar a = internal::random<Scalar>();
  80. while (abs(a-1) < 1e-4) a = internal::random<Scalar>();
  81. while (u.norm() < 1e-4) u = Vector::Random();
  82. while (v.norm() < 1e-4) v = Vector::Random();
  83. HLine line_u = HLine::Through(center + u, center + a*u);
  84. HLine line_v = HLine::Through(center + v, center + a*v);
  85. // the line equations should be normalized so that a^2+b^2=1
  86. VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
  87. VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
  88. Vector result = line_u.intersection(line_v);
  89. // the lines should intersect at the point we called "center"
  90. VERIFY_IS_APPROX(result, center);
  91. // check conversions between two types of lines
  92. PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable
  93. CoeffsType converted_coeffs = HLine(pl).coeffs();
  94. converted_coeffs *= (line_u.coeffs()[0])/(converted_coeffs[0]);
  95. VERIFY(line_u.coeffs().isApprox(converted_coeffs));
  96. }
  97. }
  98. template<typename Scalar> void hyperplane_alignment()
  99. {
  100. typedef Hyperplane<Scalar,3,AutoAlign> Plane3a;
  101. typedef Hyperplane<Scalar,3,DontAlign> Plane3u;
  102. EIGEN_ALIGN16 Scalar array1[4];
  103. EIGEN_ALIGN16 Scalar array2[4];
  104. EIGEN_ALIGN16 Scalar array3[4+1];
  105. Scalar* array3u = array3+1;
  106. Plane3a *p1 = ::new(reinterpret_cast<void*>(array1)) Plane3a;
  107. Plane3u *p2 = ::new(reinterpret_cast<void*>(array2)) Plane3u;
  108. Plane3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Plane3u;
  109. p1->coeffs().setRandom();
  110. *p2 = *p1;
  111. *p3 = *p1;
  112. VERIFY_IS_APPROX(p1->coeffs(), p2->coeffs());
  113. VERIFY_IS_APPROX(p1->coeffs(), p3->coeffs());
  114. #if defined(EIGEN_VECTORIZE) && EIGEN_ALIGN_STATICALLY
  115. if(internal::packet_traits<Scalar>::Vectorizable)
  116. VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Plane3a));
  117. #endif
  118. }
  119. void test_geo_hyperplane()
  120. {
  121. for(int i = 0; i < g_repeat; i++) {
  122. CALL_SUBTEST_1( hyperplane(Hyperplane<float,2>()) );
  123. CALL_SUBTEST_2( hyperplane(Hyperplane<float,3>()) );
  124. CALL_SUBTEST_2( hyperplane(Hyperplane<float,3,DontAlign>()) );
  125. CALL_SUBTEST_2( hyperplane_alignment<float>() );
  126. CALL_SUBTEST_3( hyperplane(Hyperplane<double,4>()) );
  127. CALL_SUBTEST_4( hyperplane(Hyperplane<std::complex<double>,5>()) );
  128. CALL_SUBTEST_1( lines<float>() );
  129. CALL_SUBTEST_3( lines<double>() );
  130. }
  131. }