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 typename NumTraits<Scalar>::Real 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. VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0 );
  39. VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
  40. VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
  41. // transform
  42. if (!NumTraits<Scalar>::IsComplex)
  43. {
  44. MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
  45. DiagonalMatrix<Scalar,HyperplaneType::AmbientDimAtCompileTime> scaling(VectorType::Random());
  46. Translation<Scalar,HyperplaneType::AmbientDimAtCompileTime> translation(VectorType::Random());
  47. pl2 = pl1;
  48. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot).absDistance(rot * p1), Scalar(1) );
  49. pl2 = pl1;
  50. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot,Isometry).absDistance(rot * p1), Scalar(1) );
  51. pl2 = pl1;
  52. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling).absDistance((rot*scaling) * p1), Scalar(1) );
  53. pl2 = pl1;
  54. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*scaling*translation)
  55. .absDistance((rot*scaling*translation) * p1), Scalar(1) );
  56. pl2 = pl1;
  57. VERIFY_IS_MUCH_SMALLER_THAN( pl2.transform(rot*translation,Isometry)
  58. .absDistance((rot*translation) * p1), Scalar(1) );
  59. }
  60. // casting
  61. const int Dim = HyperplaneType::AmbientDimAtCompileTime;
  62. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  63. Hyperplane<OtherScalar,Dim,Options> hp1f = pl1.template cast<OtherScalar>();
  64. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),pl1);
  65. Hyperplane<Scalar,Dim,Options> hp1d = pl1.template cast<Scalar>();
  66. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),pl1);
  67. }
  68. template<typename Scalar> void lines()
  69. {
  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 (internal::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. }