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.

104 lines
3.9 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 LineType> void parametrizedline(const LineType& _line)
  15. {
  16. /* this test covers the following files:
  17. ParametrizedLine.h
  18. */
  19. using std::abs;
  20. typedef typename LineType::Index Index;
  21. const Index dim = _line.dim();
  22. typedef typename LineType::Scalar Scalar;
  23. typedef typename NumTraits<Scalar>::Real RealScalar;
  24. typedef Matrix<Scalar, LineType::AmbientDimAtCompileTime, 1> VectorType;
  25. typedef Hyperplane<Scalar,LineType::AmbientDimAtCompileTime> HyperplaneType;
  26. VectorType p0 = VectorType::Random(dim);
  27. VectorType p1 = VectorType::Random(dim);
  28. VectorType d0 = VectorType::Random(dim).normalized();
  29. LineType l0(p0, d0);
  30. Scalar s0 = internal::random<Scalar>();
  31. Scalar s1 = abs(internal::random<Scalar>());
  32. VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0), RealScalar(1) );
  33. VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(p0+s0*d0), RealScalar(1) );
  34. VERIFY_IS_APPROX( (l0.projection(p1)-p1).norm(), l0.distance(p1) );
  35. VERIFY_IS_MUCH_SMALLER_THAN( l0.distance(l0.projection(p1)), RealScalar(1) );
  36. VERIFY_IS_APPROX( Scalar(l0.distance((p0+s0*d0) + d0.unitOrthogonal() * s1)), s1 );
  37. // casting
  38. const int Dim = LineType::AmbientDimAtCompileTime;
  39. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  40. ParametrizedLine<OtherScalar,Dim> hp1f = l0.template cast<OtherScalar>();
  41. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),l0);
  42. ParametrizedLine<Scalar,Dim> hp1d = l0.template cast<Scalar>();
  43. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),l0);
  44. // intersections
  45. VectorType p2 = VectorType::Random(dim);
  46. VectorType n2 = VectorType::Random(dim).normalized();
  47. HyperplaneType hp(p2,n2);
  48. Scalar t = l0.intersectionParameter(hp);
  49. VectorType pi = l0.pointAt(t);
  50. VERIFY_IS_MUCH_SMALLER_THAN(hp.signedDistance(pi), RealScalar(1));
  51. VERIFY_IS_MUCH_SMALLER_THAN(l0.distance(pi), RealScalar(1));
  52. VERIFY_IS_APPROX(l0.intersectionPoint(hp), pi);
  53. }
  54. template<typename Scalar> void parametrizedline_alignment()
  55. {
  56. typedef ParametrizedLine<Scalar,4,AutoAlign> Line4a;
  57. typedef ParametrizedLine<Scalar,4,DontAlign> Line4u;
  58. EIGEN_ALIGN_MAX Scalar array1[16];
  59. EIGEN_ALIGN_MAX Scalar array2[16];
  60. EIGEN_ALIGN_MAX Scalar array3[16+1];
  61. Scalar* array3u = array3+1;
  62. Line4a *p1 = ::new(reinterpret_cast<void*>(array1)) Line4a;
  63. Line4u *p2 = ::new(reinterpret_cast<void*>(array2)) Line4u;
  64. Line4u *p3 = ::new(reinterpret_cast<void*>(array3u)) Line4u;
  65. p1->origin().setRandom();
  66. p1->direction().setRandom();
  67. *p2 = *p1;
  68. *p3 = *p1;
  69. VERIFY_IS_APPROX(p1->origin(), p2->origin());
  70. VERIFY_IS_APPROX(p1->origin(), p3->origin());
  71. VERIFY_IS_APPROX(p1->direction(), p2->direction());
  72. VERIFY_IS_APPROX(p1->direction(), p3->direction());
  73. #if defined(EIGEN_VECTORIZE) && EIGEN_MAX_STATIC_ALIGN_BYTES>0
  74. if(internal::packet_traits<Scalar>::Vectorizable && internal::packet_traits<Scalar>::size<=4)
  75. VERIFY_RAISES_ASSERT((::new(reinterpret_cast<void*>(array3u)) Line4a));
  76. #endif
  77. }
  78. void test_geo_parametrizedline()
  79. {
  80. for(int i = 0; i < g_repeat; i++) {
  81. CALL_SUBTEST_1( parametrizedline(ParametrizedLine<float,2>()) );
  82. CALL_SUBTEST_2( parametrizedline(ParametrizedLine<float,3>()) );
  83. CALL_SUBTEST_2( parametrizedline_alignment<float>() );
  84. CALL_SUBTEST_3( parametrizedline(ParametrizedLine<double,4>()) );
  85. CALL_SUBTEST_3( parametrizedline_alignment<double>() );
  86. CALL_SUBTEST_4( parametrizedline(ParametrizedLine<std::complex<double>,5>()) );
  87. }
  88. }