1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
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/.
13 #include <Eigen/StdList>
14 #include <Eigen/Geometry>
16 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Vector4f
)
18 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Matrix2f
)
19 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Matrix4f
)
20 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Matrix4d
)
22 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Affine3f
)
23 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Affine3d
)
25 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Quaternionf
)
26 EIGEN_DEFINE_STL_LIST_SPECIALIZATION(Quaterniond
)
28 template <class Container
, class Position
>
29 typename
Container::iterator
get(Container
& c
, Position position
)
31 typename
Container::iterator it
= c
.begin();
32 std::advance(it
, position
);
36 template <class Container
, class Position
, class Value
>
37 void set(Container
& c
, Position position
, const Value
& value
)
39 typename
Container::iterator it
= c
.begin();
40 std::advance(it
, position
);
44 template<typename MatrixType
>
45 void check_stdlist_matrix(const MatrixType
& m
)
47 typename
MatrixType::Index rows
= m
.rows();
48 typename
MatrixType::Index cols
= m
.cols();
49 MatrixType x
= MatrixType::Random(rows
,cols
), y
= MatrixType::Random(rows
,cols
);
50 std::list
<MatrixType
> v(10, MatrixType(rows
,cols
)), w(20, y
);
51 typename
std::list
<MatrixType
>::iterator itv
= get(v
, 5);
52 typename
std::list
<MatrixType
>::iterator itw
= get(w
, 6);
55 VERIFY_IS_APPROX(*itw
, *itv
);
59 for(int i
= 0; i
< 20; i
++)
61 VERIFY_IS_APPROX(*itw
, *itv
);
68 VERIFY_IS_APPROX(*get(v
, 20), x
);
70 VERIFY_IS_APPROX(*get(v
, 21), y
);
72 VERIFY_IS_APPROX(*get(v
, 22), x
);
74 // do a lot of push_back such that the list gets internally resized
75 // (with memory reallocation)
76 MatrixType
* ref
= &(*get(w
, 0));
77 for(int i
=0; i
<30 || ((ref
==&(*get(w
, 0))) && i
<300); ++i
)
78 v
.push_back(*get(w
, i
%w
.size()));
79 for(unsigned int i
=23; i
<v
.size(); ++i
)
81 VERIFY((*get(v
, i
))==(*get(w
, (i
-23)%w
.size())));
85 template<typename TransformType
>
86 void check_stdlist_transform(const TransformType
&)
88 typedef typename
TransformType::MatrixType MatrixType
;
89 TransformType
x(MatrixType::Random()), y(MatrixType::Random());
90 std::list
<TransformType
> v(10), w(20, y
);
91 typename
std::list
<TransformType
>::iterator itv
= get(v
, 5);
92 typename
std::list
<TransformType
>::iterator itw
= get(w
, 6);
95 VERIFY_IS_APPROX(*itw
, *itv
);
99 for(int i
= 0; i
< 20; i
++)
101 VERIFY_IS_APPROX(*itw
, *itv
);
108 VERIFY_IS_APPROX(*get(v
, 20), x
);
110 VERIFY_IS_APPROX(*get(v
, 21), y
);
112 VERIFY_IS_APPROX(*get(v
, 22), x
);
114 // do a lot of push_back such that the list gets internally resized
115 // (with memory reallocation)
116 TransformType
* ref
= &(*get(w
, 0));
117 for(int i
=0; i
<30 || ((ref
==&(*get(w
, 0))) && i
<300); ++i
)
118 v
.push_back(*get(w
, i
%w
.size()));
119 for(unsigned int i
=23; i
<v
.size(); ++i
)
121 VERIFY(get(v
, i
)->matrix()==get(w
, (i
-23)%w
.size())->matrix());
125 template<typename QuaternionType
>
126 void check_stdlist_quaternion(const QuaternionType
&)
128 typedef typename
QuaternionType::Coefficients Coefficients
;
129 QuaternionType
x(Coefficients::Random()), y(Coefficients::Random());
130 std::list
<QuaternionType
> v(10), w(20, y
);
131 typename
std::list
<QuaternionType
>::iterator itv
= get(v
, 5);
132 typename
std::list
<QuaternionType
>::iterator itw
= get(w
, 6);
135 VERIFY_IS_APPROX(*itw
, *itv
);
139 for(int i
= 0; i
< 20; i
++)
141 VERIFY_IS_APPROX(*itw
, *itv
);
148 VERIFY_IS_APPROX(*get(v
, 20), x
);
150 VERIFY_IS_APPROX(*get(v
, 21), y
);
152 VERIFY_IS_APPROX(*get(v
, 22), x
);
154 // do a lot of push_back such that the list gets internally resized
155 // (with memory reallocation)
156 QuaternionType
* ref
= &(*get(w
, 0));
157 for(int i
=0; i
<30 || ((ref
==&(*get(w
, 0))) && i
<300); ++i
)
158 v
.push_back(*get(w
, i
%w
.size()));
159 for(unsigned int i
=23; i
<v
.size(); ++i
)
161 VERIFY(get(v
, i
)->coeffs()==get(w
, (i
-23)%w
.size())->coeffs());
165 void test_stdlist_overload()
167 // some non vectorizable fixed sizes
168 CALL_SUBTEST_1(check_stdlist_matrix(Vector2f()));
169 CALL_SUBTEST_1(check_stdlist_matrix(Matrix3f()));
170 CALL_SUBTEST_2(check_stdlist_matrix(Matrix3d()));
172 // some vectorizable fixed sizes
173 CALL_SUBTEST_1(check_stdlist_matrix(Matrix2f()));
174 CALL_SUBTEST_1(check_stdlist_matrix(Vector4f()));
175 CALL_SUBTEST_1(check_stdlist_matrix(Matrix4f()));
176 CALL_SUBTEST_2(check_stdlist_matrix(Matrix4d()));
178 // some dynamic sizes
179 CALL_SUBTEST_3(check_stdlist_matrix(MatrixXd(1,1)));
180 CALL_SUBTEST_3(check_stdlist_matrix(VectorXd(20)));
181 CALL_SUBTEST_3(check_stdlist_matrix(RowVectorXf(20)));
182 CALL_SUBTEST_3(check_stdlist_matrix(MatrixXcf(10,10)));
185 CALL_SUBTEST_4(check_stdlist_transform(Affine2f())); // does not need the specialization (2+1)^2 = 9
186 CALL_SUBTEST_4(check_stdlist_transform(Affine3f()));
187 CALL_SUBTEST_4(check_stdlist_transform(Affine3d()));
190 CALL_SUBTEST_5(check_stdlist_quaternion(Quaternionf()));
191 CALL_SUBTEST_5(check_stdlist_quaternion(Quaterniond()));