• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
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 
10 #include "main.h"
11 #include <Eigen/StdVector>
12 #include <Eigen/Geometry>
13 
14 template<typename MatrixType>
check_stdvector_matrix(const MatrixType & m)15 void check_stdvector_matrix(const MatrixType& m)
16 {
17   Index rows = m.rows();
18   Index cols = m.cols();
19   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
20   std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType::Zero(rows,cols)), w(20, y);
21   v[5] = x;
22   w[6] = v[5];
23   VERIFY_IS_APPROX(w[6], v[5]);
24   v = w;
25   for(int i = 0; i < 20; i++)
26   {
27     VERIFY_IS_APPROX(w[i], v[i]);
28   }
29 
30   v.resize(21);
31   v[20] = x;
32   VERIFY_IS_APPROX(v[20], x);
33   v.resize(22,y);
34   VERIFY_IS_APPROX(v[21], y);
35   v.push_back(x);
36   VERIFY_IS_APPROX(v[22], x);
37   VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(MatrixType));
38 
39   // do a lot of push_back such that the vector gets internally resized
40   // (with memory reallocation)
41   MatrixType* ref = &w[0];
42   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
43     v.push_back(w[i%w.size()]);
44   for(unsigned int i=23; i<v.size(); ++i)
45   {
46     VERIFY(v[i]==w[(i-23)%w.size()]);
47   }
48 }
49 
50 template<typename TransformType>
check_stdvector_transform(const TransformType &)51 void check_stdvector_transform(const TransformType&)
52 {
53   typedef typename TransformType::MatrixType MatrixType;
54   TransformType x(MatrixType::Random()), y(MatrixType::Random());
55   std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
56   v[5] = x;
57   w[6] = v[5];
58   VERIFY_IS_APPROX(w[6], v[5]);
59   v = w;
60   for(int i = 0; i < 20; i++)
61   {
62     VERIFY_IS_APPROX(w[i], v[i]);
63   }
64 
65   v.resize(21);
66   v[20] = x;
67   VERIFY_IS_APPROX(v[20], x);
68   v.resize(22,y);
69   VERIFY_IS_APPROX(v[21], y);
70   v.push_back(x);
71   VERIFY_IS_APPROX(v[22], x);
72   VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(TransformType));
73 
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; i<v.size(); ++i)
80   {
81     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
82   }
83 }
84 
85 template<typename QuaternionType>
check_stdvector_quaternion(const QuaternionType &)86 void check_stdvector_quaternion(const QuaternionType&)
87 {
88   typedef typename QuaternionType::Coefficients Coefficients;
89   QuaternionType x(Coefficients::Random()), y(Coefficients::Random()), qi=QuaternionType::Identity();
90   std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10,qi), w(20, y);
91   v[5] = x;
92   w[6] = v[5];
93   VERIFY_IS_APPROX(w[6], v[5]);
94   v = w;
95   for(int i = 0; i < 20; i++)
96   {
97     VERIFY_IS_APPROX(w[i], v[i]);
98   }
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((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(QuaternionType));
108 
109   // do a lot of push_back such that the vector gets internally resized
110   // (with memory reallocation)
111   QuaternionType* ref = &w[0];
112   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
113     v.push_back(w[i%w.size()]);
114   for(unsigned int i=23; i<v.size(); ++i)
115   {
116     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
117   }
118 }
119 
120 // the code below triggered an invalid warning with gcc >= 7
121 // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
122 // This has been reported to gcc there: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
std_vector_gcc_warning()123 void std_vector_gcc_warning()
124 {
125   typedef Eigen::Vector3f T;
126   std::vector<T, Eigen::aligned_allocator<T> > v;
127   v.push_back(T());
128 }
129 
EIGEN_DECLARE_TEST(stdvector)130 EIGEN_DECLARE_TEST(stdvector)
131 {
132   // some non vectorizable fixed sizes
133   CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
134   CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
135   CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
136 
137   // some vectorizable fixed sizes
138   CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
139   CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
140   CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
141   CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
142 
143   // some dynamic sizes
144   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
145   CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
146   CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
147   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
148 
149   // some Transform
150   CALL_SUBTEST_4(check_stdvector_transform(Projective2f()));
151   CALL_SUBTEST_4(check_stdvector_transform(Projective3f()));
152   CALL_SUBTEST_4(check_stdvector_transform(Projective3d()));
153   //CALL_SUBTEST(heck_stdvector_transform(Projective4d()));
154 
155   // some Quaternion
156   CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
157   CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
158 }
159