geo_hyperplane.cpp 7.4 KB

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