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 #define EIGEN_USE_NEW_STDVECTOR
11 #include "main.h"
12 #include <Eigen/StdVector>
13 #include <Eigen/Geometry>
14 
15 template<typename MatrixType>
check_stdvector_matrix(const MatrixType & m)16 void check_stdvector_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   std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType(rows,cols)), w(20, y);
22   v[5] = x;
23   w[6] = v[5];
24   VERIFY_IS_APPROX(w[6], v[5]);
25   v = w;
26   for(int i = 0; i < 20; i++)
27   {
28     VERIFY_IS_APPROX(w[i], v[i]);
29   }
30 
31   v.resize(21);
32   v[20] = x;
33   VERIFY_IS_APPROX(v[20], x);
34   v.resize(22,y);
35   VERIFY_IS_APPROX(v[21], y);
36   v.push_back(x);
37   VERIFY_IS_APPROX(v[22], x);
38   VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(MatrixType));
39 
40   // do a lot of push_back such that the vector gets internally resized
41   // (with memory reallocation)
42   MatrixType* ref = &w[0];
43   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
44     v.push_back(w[i%w.size()]);
45   for(unsigned int i=23; i<v.size(); ++i)
46   {
47     VERIFY(v[i]==w[(i-23)%w.size()]);
48   }
49 }
50 
51 template<typename TransformType>
check_stdvector_transform(const TransformType &)52 void check_stdvector_transform(const TransformType&)
53 {
54   typedef typename TransformType::MatrixType MatrixType;
55   TransformType x(MatrixType::Random()), y(MatrixType::Random());
56   std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
57   v[5] = x;
58   w[6] = v[5];
59   VERIFY_IS_APPROX(w[6], v[5]);
60   v = w;
61   for(int i = 0; i < 20; i++)
62   {
63     VERIFY_IS_APPROX(w[i], v[i]);
64   }
65 
66   v.resize(21);
67   v[20] = x;
68   VERIFY_IS_APPROX(v[20], x);
69   v.resize(22,y);
70   VERIFY_IS_APPROX(v[21], y);
71   v.push_back(x);
72   VERIFY_IS_APPROX(v[22], x);
73   VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(TransformType));
74 
75   // do a lot of push_back such that the vector gets internally resized
76   // (with memory reallocation)
77   TransformType* ref = &w[0];
78   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
79     v.push_back(w[i%w.size()]);
80   for(unsigned int i=23; i<v.size(); ++i)
81   {
82     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
83   }
84 }
85 
86 template<typename QuaternionType>
check_stdvector_quaternion(const 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,Eigen::aligned_allocator<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 
101   v.resize(21);
102   v[20] = x;
103   VERIFY_IS_APPROX(v[20], x);
104   v.resize(22,y);
105   VERIFY_IS_APPROX(v[21], y);
106   v.push_back(x);
107   VERIFY_IS_APPROX(v[22], x);
108   VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(QuaternionType));
109 
110   // do a lot of push_back such that the vector gets internally resized
111   // (with memory reallocation)
112   QuaternionType* ref = &w[0];
113   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
114     v.push_back(w[i%w.size()]);
115   for(unsigned int i=23; i<v.size(); ++i)
116   {
117     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
118   }
119 }
120 
test_eigen2_newstdvector()121 void test_eigen2_newstdvector()
122 {
123   // some non vectorizable fixed sizes
124   CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
125   CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
126   CALL_SUBTEST_1(check_stdvector_matrix(Matrix3d()));
127 
128   // some vectorizable fixed sizes
129   CALL_SUBTEST_2(check_stdvector_matrix(Matrix2f()));
130   CALL_SUBTEST_2(check_stdvector_matrix(Vector4f()));
131   CALL_SUBTEST_2(check_stdvector_matrix(Matrix4f()));
132   CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
133 
134   // some dynamic sizes
135   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
136   CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
137   CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
138   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
139 
140   // some Transform
141   CALL_SUBTEST_4(check_stdvector_transform(Transform2f()));
142   CALL_SUBTEST_4(check_stdvector_transform(Transform3f()));
143   CALL_SUBTEST_4(check_stdvector_transform(Transform3d()));
144   //CALL_SUBTEST(check_stdvector_transform(Transform4d()));
145 
146   // some Quaternion
147   CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
148   CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
149 }
150