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.

158 lines
4.6 KiB

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