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.

195 lines
7.2 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. using std::abs;
  20. typedef typename HyperplaneType::Index Index;
  21. const Index dim = _plane.dim();
  22. enum { Options = HyperplaneType::Options };
  23. typedef typename HyperplaneType::Scalar Scalar;
  24. typedef typename HyperplaneType::RealScalar RealScalar;
  25. typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
  26. typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
  27. HyperplaneType::AmbientDimAtCompileTime> MatrixType;
  28. VectorType p0 = VectorType::Random(dim);
  29. VectorType p1 = VectorType::Random(dim);
  30. VectorType n0 = VectorType::Random(dim).normalized();
  31. VectorType n1 = VectorType::Random(dim).normalized();
  32. HyperplaneType pl0(n0, p0);
  33. HyperplaneType pl1(n1, p1);
  34. HyperplaneType pl2 = pl1;
  35. Scalar s0 = internal::random<Scalar>();
  36. Scalar s1 = internal::random<Scalar>();
  37. VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
  38. VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
  39. if(numext::abs2(s0)>RealScalar(1e-6))
  40. VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0);
  41. else
  42. VERIFY_IS_MUCH_SMALLER_THAN( abs(pl1.signedDistance(p1 + n1 * s0) - s0), Scalar(1) );
  43. VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
  44. VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
  45. // transform
  46. if (!NumTraits<Scalar>::IsComplex)
  47. {
  48. MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
  49. DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
  50. Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
  51. while(scaling.diagonal().cwiseAbs().minCoeff()<RealScalar(1e-4)) scaling.diagonal() = VectorType::Random();
  52. pl2 = pl1;
  53. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
  54. pl2 = pl1;
  55. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
  56. pl2 = pl1;
  57. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
  58. pl2 = pl1;
  59. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
  60. .absDistance((rot*scaling*translation) * p1), Scalar(1) );
  61. pl2 = pl1;
  62. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
  63. .absDistance((rot*translation) * p1), Scalar(1) );
  64. }
  65. // casting
  66. const int Dim = HyperplaneType::AmbientDimAtCompileTime;
  67. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  68. Hyperplane<OtherScalar,Dim,Options> hp1f = pl1.template cast<OtherScalar>();
  69. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
  70. Hyperplane<Scalar,Dim,Options> hp1d = pl1.template cast<Scalar>();
  71. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
  72. }
  73. template<typename Scalar> void lines()
  74. {
  75. using std::abs;
  76. typedef Hyperplane<Scalar, 2> HLine;
  77. typedef ParametrizedLine<Scalar, 2> PLine;
  78. typedef Matrix<Scalar,2,1> Vector;
  79. typedef Matrix<Scalar,3,1> CoeffsType;
  80. for(int i = 0; i < 10; i++)
  81. {
  82. Vector center = Vector::Random();
  83. Vector u = Vector::Random();
  84. Vector v = Vector::Random();
  85. Scalar a = internal::random<Scalar>();
  86. while (abs(a-1) < 1e-4) a = internal::random<Scalar>();
  87. while (u.norm() < 1e-4) u = Vector::Random();
  88. while (v.norm() < 1e-4) v = Vector::Random();
  89. HLine line_u = HLine::Through(center + u, center + a*u);
  90. HLine line_v = HLine::Through(center + v, center + a*v);
  91. // the line equations should be normalized so that a^2+b^2=1
  92. VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
  93. VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
  94. Vector result = line_u.intersection(line_v);
  95. // the lines should intersect at the point we called "center"
  96. if(abs(a-1) > 1e-2 && abs(v.normalized().dot(u.normalized()))<0.9)
  97. VERIFY_IS_APPROX(result, center);
  98. // check conversions between two types of lines
  99. PLine pl(line_u); // gcc 3.3 will commit suicide if we don't name this variable
  100. HLine line_u2(pl);
  101. CoeffsType converted_coeffs = line_u2.coeffs();
  102. if(line_u2.normal().dot(line_u.normal())<0.)
  103. converted_coeffs = -line_u2.coeffs();
  104. VERIFY(line_u.coeffs().isApprox(converted_coeffs));
  105. }
  106. }
  107. template<typename Scalar> void planes()
  108. {
  109. using std::abs;
  110. typedef Hyperplane<Scalar, 3> Plane;
  111. typedef Matrix<Scalar,3,1> Vector;
  112. for(int i = 0; i < 10; i++)
  113. {
  114. Vector v0 = Vector::Random();
  115. Vector v1(v0), v2(v0);
  116. if(internal::random<double>(0,1)>0.25)
  117. v1 += Vector::Random();
  118. if(internal::random<double>(0,1)>0.25)
  119. v2 += v1 * std::pow(internal::random<Scalar>(0,1),internal::random<int>(1,16));
  120. if(internal::random<double>(0,1)>0.25)
  121. v2 += Vector::Random() * std::pow(internal::random<Scalar>(0,1),internal::random<int>(1,16));
  122. Plane p0 = Plane::Through(v0, v1, v2);
  123. VERIFY_IS_APPROX(p0.normal().norm(), Scalar(1));
  124. VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v0), Scalar(1));
  125. VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v1), Scalar(1));
  126. VERIFY_IS_MUCH_SMALLER_THAN(p0.absDistance(v2), Scalar(1));
  127. }
  128. }
  129. template<typename Scalar> void hyperplane_alignment()
  130. {
  131. typedef Hyperplane<Scalar,3,AutoAlign> Plane3a;
  132. typedef Hyperplane<Scalar,3,DontAlign> Plane3u;
  133. EIGEN_ALIGN_MAX Scalar array1[4];
  134. EIGEN_ALIGN_MAX Scalar array2[4];
  135. EIGEN_ALIGN_MAX Scalar array3[4+1];
  136. Scalar* array3u = array3+1;
  137. Plane3a *p1 = ::new(reinterpret_cast<void*>(array1)) Plane3a;
  138. Plane3u *p2 = ::new(reinterpret_cast<void*>(array2)) Plane3u;
  139. Plane3u *p3 = ::new(reinterpret_cast<void*>(array3u)) Plane3u;
  140. p1->coeffs().setRandom();
  141. *p2 = *p1;
  142. *p3 = *p1;
  143. VERIFY_IS_APPROX(p1->coeffs(), p2->coeffs());
  144. VERIFY_IS_APPROX(p1->coeffs(), p3->coeffs());
  145. #if defined(EIGEN_VECTORIZE) && EIGEN_MAX_STATIC_ALIGN_BYTES > 0
  146. if(internal::packet_traits<Scalar>::Vectorizable && internal::packet_traits<Scalar>::size<=4)
  147. VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Plane3a));
  148. #endif
  149. }
  150. void test_geo_hyperplane()
  151. {
  152. for(int i = 0; i < g_repeat; i++) {
  153. CALL_SUBTEST_1( hyperplane(Hyperplane<float,2>()) );
  154. CALL_SUBTEST_2( hyperplane(Hyperplane<float,3>()) );
  155. CALL_SUBTEST_2( hyperplane(Hyperplane<float,3,DontAlign>()) );
  156. CALL_SUBTEST_2( hyperplane_alignment<float>() );
  157. CALL_SUBTEST_3( hyperplane(Hyperplane<double,4>()) );
  158. CALL_SUBTEST_4( hyperplane(Hyperplane<std::complex<double>,5>()) );
  159. CALL_SUBTEST_1( lines<float>() );
  160. CALL_SUBTEST_3( lines<double>() );
  161. CALL_SUBTEST_2( planes<float>() );
  162. CALL_SUBTEST_5( planes<double>() );
  163. }
  164. }