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.

187 lines
5.4 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/Geometry>
  11. #include <Eigen/LU>
  12. #include <Eigen/QR>
  13. #include<iostream>
  14. using namespace std;
  15. template<typename T> EIGEN_DONT_INLINE
  16. void kill_extra_precision(T& x) { eigen_assert((void*)(&x) != (void*)0); }
  17. template<typename BoxType> void alignedbox(const BoxType& _box)
  18. {
  19. /* this test covers the following files:
  20. AlignedBox.h
  21. */
  22. typedef typename BoxType::Index Index;
  23. typedef typename BoxType::Scalar Scalar;
  24. typedef typename NumTraits<Scalar>::Real RealScalar;
  25. typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
  26. const Index dim = _box.dim();
  27. VectorType p0 = VectorType::Random(dim);
  28. VectorType p1 = VectorType::Random(dim);
  29. while( p1 == p0 ){
  30. p1 = VectorType::Random(dim); }
  31. RealScalar s1 = internal::random<RealScalar>(0,1);
  32. BoxType b0(dim);
  33. BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
  34. BoxType b2;
  35. kill_extra_precision(b1);
  36. kill_extra_precision(p0);
  37. kill_extra_precision(p1);
  38. b0.extend(p0);
  39. b0.extend(p1);
  40. VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
  41. (b2 = b0).extend(b1);
  42. VERIFY(b2.contains(b0));
  43. VERIFY(b2.contains(b1));
  44. VERIFY_IS_APPROX(b2.clamp(b0), b0);
  45. // intersection
  46. BoxType box1(VectorType::Random(dim));
  47. box1.extend(VectorType::Random(dim));
  48. BoxType box2(VectorType::Random(dim));
  49. box2.extend(VectorType::Random(dim));
  50. VERIFY(box1.intersects(box2) == !box1.intersection(box2).isEmpty());
  51. // alignment -- make sure there is no memory alignment assertion
  52. BoxType *bp0 = new BoxType(dim);
  53. BoxType *bp1 = new BoxType(dim);
  54. bp0->extend(*bp1);
  55. delete bp0;
  56. delete bp1;
  57. // sampling
  58. for( int i=0; i<10; ++i )
  59. {
  60. VectorType r = b0.sample();
  61. VERIFY(b0.contains(r));
  62. }
  63. }
  64. template<typename BoxType>
  65. void alignedboxCastTests(const BoxType& _box)
  66. {
  67. // casting
  68. typedef typename BoxType::Index Index;
  69. typedef typename BoxType::Scalar Scalar;
  70. typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
  71. const Index dim = _box.dim();
  72. VectorType p0 = VectorType::Random(dim);
  73. VectorType p1 = VectorType::Random(dim);
  74. BoxType b0(dim);
  75. b0.extend(p0);
  76. b0.extend(p1);
  77. const int Dim = BoxType::AmbientDimAtCompileTime;
  78. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  79. AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
  80. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
  81. AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
  82. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
  83. }
  84. void specificTest1()
  85. {
  86. Vector2f m; m << -1.0f, -2.0f;
  87. Vector2f M; M << 1.0f, 5.0f;
  88. typedef AlignedBox2f BoxType;
  89. BoxType box( m, M );
  90. Vector2f sides = M-m;
  91. VERIFY_IS_APPROX(sides, box.sizes() );
  92. VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
  93. VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
  94. VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
  95. VERIFY_IS_APPROX( 14.0f, box.volume() );
  96. VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
  97. VERIFY_IS_APPROX( std::sqrt( 53.0f ), box.diagonal().norm() );
  98. VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
  99. VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
  100. Vector2f bottomRight; bottomRight << M[0], m[1];
  101. Vector2f topLeft; topLeft << m[0], M[1];
  102. VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
  103. VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
  104. }
  105. void specificTest2()
  106. {
  107. Vector3i m; m << -1, -2, 0;
  108. Vector3i M; M << 1, 5, 3;
  109. typedef AlignedBox3i BoxType;
  110. BoxType box( m, M );
  111. Vector3i sides = M-m;
  112. VERIFY_IS_APPROX(sides, box.sizes() );
  113. VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
  114. VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
  115. VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
  116. VERIFY_IS_APPROX( 42, box.volume() );
  117. VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
  118. VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
  119. VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
  120. Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
  121. Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
  122. VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
  123. VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
  124. }
  125. void test_geo_alignedbox()
  126. {
  127. for(int i = 0; i < g_repeat; i++)
  128. {
  129. CALL_SUBTEST_1( alignedbox(AlignedBox2f()) );
  130. CALL_SUBTEST_2( alignedboxCastTests(AlignedBox2f()) );
  131. CALL_SUBTEST_3( alignedbox(AlignedBox3f()) );
  132. CALL_SUBTEST_4( alignedboxCastTests(AlignedBox3f()) );
  133. CALL_SUBTEST_5( alignedbox(AlignedBox4d()) );
  134. CALL_SUBTEST_6( alignedboxCastTests(AlignedBox4d()) );
  135. CALL_SUBTEST_7( alignedbox(AlignedBox1d()) );
  136. CALL_SUBTEST_8( alignedboxCastTests(AlignedBox1d()) );
  137. CALL_SUBTEST_9( alignedbox(AlignedBox1i()) );
  138. CALL_SUBTEST_10( alignedbox(AlignedBox2i()) );
  139. CALL_SUBTEST_11( alignedbox(AlignedBox3i()) );
  140. CALL_SUBTEST_14( alignedbox(AlignedBox<double,Dynamic>(4)) );
  141. }
  142. CALL_SUBTEST_12( specificTest1() );
  143. CALL_SUBTEST_13( specificTest2() );
  144. }