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.

171 lines
5.0 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 BoxType> void alignedbox(const BoxType& _box)
  16. {
  17. /* this test covers the following files:
  18. AlignedBox.h
  19. */
  20. typedef typename BoxType::Index Index;
  21. typedef typename BoxType::Scalar Scalar;
  22. typedef typename NumTraits<Scalar>::Real RealScalar;
  23. typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
  24. const Index dim = _box.dim();
  25. VectorType p0 = VectorType::Random(dim);
  26. VectorType p1 = VectorType::Random(dim);
  27. while( p1 == p0 ){
  28. p1 = VectorType::Random(dim); }
  29. RealScalar s1 = internal::random<RealScalar>(0,1);
  30. BoxType b0(dim);
  31. BoxType b1(VectorType::Random(dim),VectorType::Random(dim));
  32. BoxType b2;
  33. b0.extend(p0);
  34. b0.extend(p1);
  35. VERIFY(b0.contains(p0*s1+(Scalar(1)-s1)*p1));
  36. (b2 = b0).extend(b1);
  37. VERIFY(b2.contains(b0));
  38. VERIFY(b2.contains(b1));
  39. VERIFY_IS_APPROX(b2.clamp(b0), b0);
  40. // alignment -- make sure there is no memory alignment assertion
  41. BoxType *bp0 = new BoxType(dim);
  42. BoxType *bp1 = new BoxType(dim);
  43. bp0->extend(*bp1);
  44. delete bp0;
  45. delete bp1;
  46. // sampling
  47. for( int i=0; i<10; ++i )
  48. {
  49. VectorType r = b0.sample();
  50. VERIFY(b0.contains(r));
  51. }
  52. }
  53. template<typename BoxType>
  54. void alignedboxCastTests(const BoxType& _box)
  55. {
  56. // casting
  57. typedef typename BoxType::Index Index;
  58. typedef typename BoxType::Scalar Scalar;
  59. typedef typename NumTraits<Scalar>::Real RealScalar;
  60. typedef Matrix<Scalar, BoxType::AmbientDimAtCompileTime, 1> VectorType;
  61. const Index dim = _box.dim();
  62. VectorType p0 = VectorType::Random(dim);
  63. VectorType p1 = VectorType::Random(dim);
  64. BoxType b0(dim);
  65. b0.extend(p0);
  66. b0.extend(p1);
  67. const int Dim = BoxType::AmbientDimAtCompileTime;
  68. typedef typename GetDifferentType<Scalar>::type OtherScalar;
  69. AlignedBox<OtherScalar,Dim> hp1f = b0.template cast<OtherScalar>();
  70. VERIFY_IS_APPROX(hp1f.template cast<Scalar>(),b0);
  71. AlignedBox<Scalar,Dim> hp1d = b0.template cast<Scalar>();
  72. VERIFY_IS_APPROX(hp1d.template cast<Scalar>(),b0);
  73. }
  74. void specificTest1()
  75. {
  76. Vector2f m; m << -1.0f, -2.0f;
  77. Vector2f M; M << 1.0f, 5.0f;
  78. typedef AlignedBox2f BoxType;
  79. BoxType box( m, M );
  80. Vector2f sides = M-m;
  81. VERIFY_IS_APPROX(sides, box.sizes() );
  82. VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
  83. VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
  84. VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
  85. VERIFY_IS_APPROX( 14.0f, box.volume() );
  86. VERIFY_IS_APPROX( 53.0f, box.diagonal().squaredNorm() );
  87. VERIFY_IS_APPROX( internal::sqrt( 53.0f ), box.diagonal().norm() );
  88. VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeft ) );
  89. VERIFY_IS_APPROX( M, box.corner( BoxType::TopRight ) );
  90. Vector2f bottomRight; bottomRight << M[0], m[1];
  91. Vector2f topLeft; topLeft << m[0], M[1];
  92. VERIFY_IS_APPROX( bottomRight, box.corner( BoxType::BottomRight ) );
  93. VERIFY_IS_APPROX( topLeft, box.corner( BoxType::TopLeft ) );
  94. }
  95. void specificTest2()
  96. {
  97. Vector3i m; m << -1, -2, 0;
  98. Vector3i M; M << 1, 5, 3;
  99. typedef AlignedBox3i BoxType;
  100. BoxType box( m, M );
  101. Vector3i sides = M-m;
  102. VERIFY_IS_APPROX(sides, box.sizes() );
  103. VERIFY_IS_APPROX(sides[1], box.sizes()[1] );
  104. VERIFY_IS_APPROX(sides[1], box.sizes().maxCoeff() );
  105. VERIFY_IS_APPROX(sides[0], box.sizes().minCoeff() );
  106. VERIFY_IS_APPROX( 42, box.volume() );
  107. VERIFY_IS_APPROX( 62, box.diagonal().squaredNorm() );
  108. VERIFY_IS_APPROX( m, box.corner( BoxType::BottomLeftFloor ) );
  109. VERIFY_IS_APPROX( M, box.corner( BoxType::TopRightCeil ) );
  110. Vector3i bottomRightFloor; bottomRightFloor << M[0], m[1], m[2];
  111. Vector3i topLeftFloor; topLeftFloor << m[0], M[1], m[2];
  112. VERIFY_IS_APPROX( bottomRightFloor, box.corner( BoxType::BottomRightFloor ) );
  113. VERIFY_IS_APPROX( topLeftFloor, box.corner( BoxType::TopLeftFloor ) );
  114. }
  115. void test_geo_alignedbox()
  116. {
  117. for(int i = 0; i < g_repeat; i++)
  118. {
  119. CALL_SUBTEST_1( alignedbox(AlignedBox2f()) );
  120. CALL_SUBTEST_2( alignedboxCastTests(AlignedBox2f()) );
  121. CALL_SUBTEST_3( alignedbox(AlignedBox3f()) );
  122. CALL_SUBTEST_4( alignedboxCastTests(AlignedBox3f()) );
  123. CALL_SUBTEST_5( alignedbox(AlignedBox4d()) );
  124. CALL_SUBTEST_6( alignedboxCastTests(AlignedBox4d()) );
  125. CALL_SUBTEST_7( alignedbox(AlignedBox1d()) );
  126. CALL_SUBTEST_8( alignedboxCastTests(AlignedBox1d()) );
  127. CALL_SUBTEST_9( alignedbox(AlignedBox1i()) );
  128. CALL_SUBTEST_10( alignedbox(AlignedBox2i()) );
  129. CALL_SUBTEST_11( alignedbox(AlignedBox3i()) );
  130. }
  131. CALL_SUBTEST_12( specificTest1() );
  132. CALL_SUBTEST_13( specificTest2() );
  133. }