geo_parametrizedline.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 LineType> void parametrizedline(const LineType& _line)
  15. {
  16. /* this test covers the following files:
  17. ParametrizedLine.h
  18. */
  19. using std::abs;
  20. const Index dim = _line.dim();
  21. typedef typename LineType::Scalar Scalar;
  22. typedef typename NumTraits<Scalar>::Real RealScalar;
  23. typedef Matrix<Scalar, LineType::AmbientDimAtCompileTime, 1> VectorType;
  24. typedef Hyperplane<Scalar,LineType::AmbientDimAtCompileTime> HyperplaneType;
  25. typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime,
  26. HyperplaneType::AmbientDimAtCompileTime> MatrixType;
  27. VectorType p0 = VectorType::Random(dim);
  28. VectorType p1 = VectorType::Random(dim);
  29. VectorType d0 = VectorType::Random(dim).normalized();
  30. LineType l0(p0, d0);
  31. Scalar s0 = internal::random<Scalar>();
  32. Scalar s1 = abs(internal::random<Scalar>());
  33. VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0), RealScalar(1) );
  34. VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0+s0*d0), RealScalar(1) );
  35. VERIFY_IS_APPROX( (l0.projection(p1)-p1).norm(), l0.distance(p1) );
  36. VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(l0.projection(p1)), RealScalar(1) );
  37. VERIFY_IS_APPROX( Scalar(l0.distance((p0+s0*d0) + d0.unitOrthogonal() * s1)), s1 );
  38. // casting
  39. const int Dim = LineType::AmbientDimAtCompileTime;
  40. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  41. ParametrizedLine<OtherScalar,Dim> hp1f = l0.template cast<OtherScalar>();
  42. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),l0);
  43. ParametrizedLine<Scalar,Dim> hp1d = l0.template cast<Scalar>();
  44. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),l0);
  45. // intersections
  46. VectorType p2 = VectorType::Random(dim);
  47. VectorType n2 = VectorType::Random(dim).normalized();
  48. HyperplaneType hp(p2,n2);
  49. Scalar t = l0.intersectionParameter(hp);
  50. VectorType pi = l0.pointAt(t);
  51. VERIFY_IS_MUCH_SMALLER_THAN(hp.signedDistance(pi), RealScalar(1));
  52. VERIFY_IS_MUCH_SMALLER_THAN(l0.distance(pi), RealScalar(1));
  53. VERIFY_IS_APPROX(l0.intersectionPoint(hp), pi);
  54. // transform
  55. if (!NumTraits<Scalar>::IsComplex)
  56. {
  57. MatrixType rot = MatrixType::Random(dim,dim).householderQr().householderQ();
  58. DiagonalMatrix<Scalar,LineType::AmbientDimAtCompileTime> scaling(VectorType::Random());
  59. Translation<Scalar,LineType::AmbientDimAtCompileTime> translation(VectorType::Random());
  60. while(scaling.diagonal().cwiseAbs().minCoeff()<RealScalar(1e-4)) scaling.diagonal() = VectorType::Random();
  61. LineType l1 = l0;
  62. VectorType p3 = l0.pointAt(Scalar(1));
  63. VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot).distance(rot * p3), Scalar(1) );
  64. l1 = l0;
  65. VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot,Isometry).distance(rot * p3), Scalar(1) );
  66. l1 = l0;
  67. VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot*scaling).distance((rot*scaling) * p3), Scalar(1) );
  68. l1 = l0;
  69. VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot*scaling*translation)
  70. .distance((rot*scaling*translation) * p3), Scalar(1) );
  71. l1 = l0;
  72. VERIFY_IS_MUCH_SMALLER_THAN( l1.transform(rot*translation,Isometry)
  73. .distance((rot*translation) * p3), Scalar(1) );
  74. }
  75. }
  76. template<typename Scalar> void parametrizedline_alignment()
  77. {
  78. typedef ParametrizedLine<Scalar,4,AutoAlign> Line4a;
  79. typedef ParametrizedLine<Scalar,4,DontAlign> Line4u;
  80. EIGEN_ALIGN_MAX Scalar array1[16];
  81. EIGEN_ALIGN_MAX Scalar array2[16];
  82. EIGEN_ALIGN_MAX Scalar array3[16+1];
  83. Scalar* array3u = array3+1;
  84. Line4a *p1 = ::new(reinterpret_cast<void*>(array1)) Line4a;
  85. Line4u *p2 = ::new(reinterpret_cast<void*>(array2)) Line4u;
  86. Line4u *p3 = ::new(reinterpret_cast<void*>(array3u)) Line4u;
  87. p1->origin().setRandom();
  88. p1->direction().setRandom();
  89. *p2 = *p1;
  90. *p3 = *p1;
  91. VERIFY_IS_APPROX(p1->origin(), p2->origin());
  92. VERIFY_IS_APPROX(p1->origin(), p3->origin());
  93. VERIFY_IS_APPROX(p1->direction(), p2->direction());
  94. VERIFY_IS_APPROX(p1->direction(), p3->direction());
  95. }
  96. EIGEN_DECLARE_TEST(geo_parametrizedline)
  97. {
  98. for(int i = 0; i < g_repeat; i++) {
  99. CALL_SUBTEST_1( parametrizedline(ParametrizedLine<float,2>()) );
  100. CALL_SUBTEST_2( parametrizedline(ParametrizedLine<float,3>()) );
  101. CALL_SUBTEST_2( parametrizedline_alignment<float>() );
  102. CALL_SUBTEST_3( parametrizedline(ParametrizedLine<double,4>()) );
  103. CALL_SUBTEST_3( parametrizedline_alignment<double>() );
  104. CALL_SUBTEST_4( parametrizedline(ParametrizedLine<std::complex<double>,5>()) );
  105. }
  106. }