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 
11 #include "main.h"
12 
13 #include <Eigen/StdDeque>
14 #include <Eigen/Geometry>
15 
16 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Vector4f)
17 
EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix2f)18 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix2f)
19 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix4f)
20 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix4d)
21 
22 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Affine3f)
23 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Affine3d)
24 
25 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Quaternionf)
26 EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Quaterniond)
27 
28 template<typename MatrixType>
29 void check_stddeque_matrix(const MatrixType& m)
30 {
31   typename MatrixType::Index rows = m.rows();
32   typename MatrixType::Index cols = m.cols();
33   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
34   std::deque<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
35   v[5] = x;
36   w[6] = v[5];
37   VERIFY_IS_APPROX(w[6], v[5]);
38   v = w;
39   for(int i = 0; i < 20; i++)
40   {
41     VERIFY_IS_APPROX(w[i], v[i]);
42   }
43 
44   v.resize(21);
45   v[20] = x;
46   VERIFY_IS_APPROX(v[20], x);
47   v.resize(22,y);
48   VERIFY_IS_APPROX(v[21], y);
49   v.push_back(x);
50   VERIFY_IS_APPROX(v[22], x);
51 
52   // do a lot of push_back such that the deque gets internally resized
53   // (with memory reallocation)
54   MatrixType* ref = &w[0];
55   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
56     v.push_back(w[i%w.size()]);
57   for(unsigned int i=23; i<v.size(); ++i)
58   {
59     VERIFY(v[i]==w[(i-23)%w.size()]);
60   }
61 }
62 
63 template<typename TransformType>
check_stddeque_transform(const TransformType &)64 void check_stddeque_transform(const TransformType&)
65 {
66   typedef typename TransformType::MatrixType MatrixType;
67   TransformType x(MatrixType::Random()), y(MatrixType::Random());
68   std::deque<TransformType> v(10), w(20, y);
69   v[5] = x;
70   w[6] = v[5];
71   VERIFY_IS_APPROX(w[6], v[5]);
72   v = w;
73   for(int i = 0; i < 20; i++)
74   {
75     VERIFY_IS_APPROX(w[i], v[i]);
76   }
77 
78   v.resize(21);
79   v[20] = x;
80   VERIFY_IS_APPROX(v[20], x);
81   v.resize(22,y);
82   VERIFY_IS_APPROX(v[21], y);
83   v.push_back(x);
84   VERIFY_IS_APPROX(v[22], x);
85 
86   // do a lot of push_back such that the deque gets internally resized
87   // (with memory reallocation)
88   TransformType* ref = &w[0];
89   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
90     v.push_back(w[i%w.size()]);
91   for(unsigned int i=23; i<v.size(); ++i)
92   {
93     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
94   }
95 }
96 
97 template<typename QuaternionType>
check_stddeque_quaternion(const QuaternionType &)98 void check_stddeque_quaternion(const QuaternionType&)
99 {
100   typedef typename QuaternionType::Coefficients Coefficients;
101   QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
102   std::deque<QuaternionType> v(10), w(20, y);
103   v[5] = x;
104   w[6] = v[5];
105   VERIFY_IS_APPROX(w[6], v[5]);
106   v = w;
107   for(int i = 0; i < 20; i++)
108   {
109     VERIFY_IS_APPROX(w[i], v[i]);
110   }
111 
112   v.resize(21);
113   v[20] = x;
114   VERIFY_IS_APPROX(v[20], x);
115   v.resize(22,y);
116   VERIFY_IS_APPROX(v[21], y);
117   v.push_back(x);
118   VERIFY_IS_APPROX(v[22], x);
119 
120   // do a lot of push_back such that the deque gets internally resized
121   // (with memory reallocation)
122   QuaternionType* ref = &w[0];
123   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
124     v.push_back(w[i%w.size()]);
125   for(unsigned int i=23; i<v.size(); ++i)
126   {
127     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
128   }
129 }
130 
test_stddeque_overload()131 void test_stddeque_overload()
132 {
133   // some non vectorizable fixed sizes
134   CALL_SUBTEST_1(check_stddeque_matrix(Vector2f()));
135   CALL_SUBTEST_1(check_stddeque_matrix(Matrix3f()));
136   CALL_SUBTEST_2(check_stddeque_matrix(Matrix3d()));
137 
138   // some vectorizable fixed sizes
139   CALL_SUBTEST_1(check_stddeque_matrix(Matrix2f()));
140   CALL_SUBTEST_1(check_stddeque_matrix(Vector4f()));
141   CALL_SUBTEST_1(check_stddeque_matrix(Matrix4f()));
142   CALL_SUBTEST_2(check_stddeque_matrix(Matrix4d()));
143 
144   // some dynamic sizes
145   CALL_SUBTEST_3(check_stddeque_matrix(MatrixXd(1,1)));
146   CALL_SUBTEST_3(check_stddeque_matrix(VectorXd(20)));
147   CALL_SUBTEST_3(check_stddeque_matrix(RowVectorXf(20)));
148   CALL_SUBTEST_3(check_stddeque_matrix(MatrixXcf(10,10)));
149 
150   // some Transform
151   CALL_SUBTEST_4(check_stddeque_transform(Affine2f())); // does not need the specialization (2+1)^2 = 9
152   CALL_SUBTEST_4(check_stddeque_transform(Affine3f()));
153   CALL_SUBTEST_4(check_stddeque_transform(Affine3d()));
154 
155   // some Quaternion
156   CALL_SUBTEST_5(check_stddeque_quaternion(Quaternionf()));
157   CALL_SUBTEST_5(check_stddeque_quaternion(Quaterniond()));
158 }
159