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.

161 lines
4.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2010 Hauke Heibel <hauke.heibel@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/StdVector>
  12. #include <Eigen/Geometry>
  13. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Vector4f)
  14. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix2f)
  15. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix4f)
  16. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix4d)
  17. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Affine3f)
  18. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Affine3d)
  19. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Quaternionf)
  20. EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Quaterniond)
  21. template<typename MatrixType>
  22. void check_stdvector_matrix(const MatrixType& m)
  23. {
  24. typename MatrixType::Index rows = m.rows();
  25. typename MatrixType::Index cols = m.cols();
  26. MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
  27. std::vector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
  28. v[5] = x;
  29. w[6] = v[5];
  30. VERIFY_IS_APPROX(w[6], v[5]);
  31. v = w;
  32. for(int i = 0; i < 20; i++)
  33. {
  34. VERIFY_IS_APPROX(w[i], v[i]);
  35. }
  36. v.resize(21);
  37. v[20] = x;
  38. VERIFY_IS_APPROX(v[20], x);
  39. v.resize(22,y);
  40. VERIFY_IS_APPROX(v[21], y);
  41. v.push_back(x);
  42. VERIFY_IS_APPROX(v[22], x);
  43. VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
  44. // do a lot of push_back such that the vector gets internally resized
  45. // (with memory reallocation)
  46. MatrixType* ref = &w[0];
  47. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  48. v.push_back(w[i%w.size()]);
  49. for(unsigned int i=23; i<v.size(); ++i)
  50. {
  51. VERIFY(v[i]==w[(i-23)%w.size()]);
  52. }
  53. }
  54. template<typename TransformType>
  55. void check_stdvector_transform(const TransformType&)
  56. {
  57. typedef typename TransformType::MatrixType MatrixType;
  58. TransformType x(MatrixType::Random()), y(MatrixType::Random());
  59. std::vector<TransformType> v(10), w(20, y);
  60. v[5] = x;
  61. w[6] = v[5];
  62. VERIFY_IS_APPROX(w[6], v[5]);
  63. v = w;
  64. for(int i = 0; i < 20; i++)
  65. {
  66. VERIFY_IS_APPROX(w[i], v[i]);
  67. }
  68. v.resize(21);
  69. v[20] = x;
  70. VERIFY_IS_APPROX(v[20], x);
  71. v.resize(22,y);
  72. VERIFY_IS_APPROX(v[21], y);
  73. v.push_back(x);
  74. VERIFY_IS_APPROX(v[22], x);
  75. VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
  76. // do a lot of push_back such that the vector gets internally resized
  77. // (with memory reallocation)
  78. TransformType* ref = &w[0];
  79. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  80. v.push_back(w[i%w.size()]);
  81. for(unsigned int i=23; i<v.size(); ++i)
  82. {
  83. VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
  84. }
  85. }
  86. template<typename QuaternionType>
  87. void check_stdvector_quaternion(const QuaternionType&)
  88. {
  89. typedef typename QuaternionType::Coefficients Coefficients;
  90. QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
  91. std::vector<QuaternionType> v(10), w(20, y);
  92. v[5] = x;
  93. w[6] = v[5];
  94. VERIFY_IS_APPROX(w[6], v[5]);
  95. v = w;
  96. for(int i = 0; i < 20; i++)
  97. {
  98. VERIFY_IS_APPROX(w[i], v[i]);
  99. }
  100. v.resize(21);
  101. v[20] = x;
  102. VERIFY_IS_APPROX(v[20], x);
  103. v.resize(22,y);
  104. VERIFY_IS_APPROX(v[21], y);
  105. v.push_back(x);
  106. VERIFY_IS_APPROX(v[22], x);
  107. VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
  108. // do a lot of push_back such that the vector gets internally resized
  109. // (with memory reallocation)
  110. QuaternionType* ref = &w[0];
  111. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  112. v.push_back(w[i%w.size()]);
  113. for(unsigned int i=23; i<v.size(); ++i)
  114. {
  115. VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
  116. }
  117. }
  118. void test_stdvector_overload()
  119. {
  120. // some non vectorizable fixed sizes
  121. CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
  122. CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
  123. CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
  124. // some vectorizable fixed sizes
  125. CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
  126. CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
  127. CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
  128. CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
  129. // some dynamic sizes
  130. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
  131. CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
  132. CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
  133. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
  134. // some Transform
  135. CALL_SUBTEST_4(check_stdvector_transform(Affine2f())); // does not need the specialization (2+1)^2 = 9
  136. CALL_SUBTEST_4(check_stdvector_transform(Affine3f()));
  137. CALL_SUBTEST_4(check_stdvector_transform(Affine3d()));
  138. // some Quaternion
  139. CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
  140. CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
  141. }