blob: cc938885a60ae83365c3e60ec7788dacdb7b06f0 [file] [log] [blame]
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00001// 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// Eigen is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Lesser General Public
9// License as published by the Free Software Foundation; either
10// version 3 of the License, or (at your option) any later version.
11//
12// Alternatively, you can redistribute it and/or
13// modify it under the terms of the GNU General Public License as
14// published by the Free Software Foundation; either version 2 of
15// the License, or (at your option) any later version.
16//
17// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public
23// License and a copy of the GNU General Public License along with
24// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
26#define EIGEN_WORK_AROUND_QT_BUG_CALLING_WRONG_OPERATOR_NEW_FIXED_IN_QT_4_5
27
28#include "main.h"
29#include <QtCore/QVector>
30#include <Eigen/Geometry>
31#include <Eigen/QtAlignedMalloc>
32
33template<typename MatrixType>
34void check_qtvector_matrix(const MatrixType& m)
35{
36 int rows = m.rows();
37 int cols = m.cols();
38 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
39 QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
40 v[5] = x;
41 w[6] = v[5];
42 VERIFY_IS_APPROX(w[6], v[5]);
43 v = w;
44 for(int i = 0; i < 20; i++)
45 {
46 VERIFY_IS_APPROX(w[i], v[i]);
47 }
48
49 v.resize(21);
50 v[20].set(x);
51 VERIFY_IS_APPROX(v[20], x);
52 v.fill(y,22);
53 //v.resize(22);
54 VERIFY_IS_APPROX(v[21], y);
55 v.push_back(x);
56 VERIFY_IS_APPROX(v[22], x);
57 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
58
59 // do a lot of push_back such that the vector gets internally resized
60 // (with memory reallocation)
61 MatrixType* ref = &w[0];
62 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
63 v.push_back(w[i%w.size()]);
64 for(int i=23; i<v.size(); ++i)
65 {
66 VERIFY(v[i]==w[(i-23)%w.size()]);
67 }
68}
69
70template<typename TransformType>
71void check_qtvector_transform(const TransformType&)
72{
73 typedef typename TransformType::MatrixType MatrixType;
74 TransformType x(MatrixType::Random()), y(MatrixType::Random());
75 QVector<TransformType> v(10), w(20, y);
76 v[5] = x;
77 w[6] = v[5];
78 VERIFY_IS_APPROX(w[6], v[5]);
79 v = w;
80 for(int i = 0; i < 20; i++)
81 {
82 VERIFY_IS_APPROX(w[i], v[i]);
83 }
84
85 v.resize(21);
86 v[20] = x;
87 VERIFY_IS_APPROX(v[20], x);
88 v.fill(y,22);
89 //v.resize(22);
90 VERIFY_IS_APPROX(v[21], y);
91 v.push_back(x);
92 VERIFY_IS_APPROX(v[22], x);
93 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
94
95 // do a lot of push_back such that the vector gets internally resized
96 // (with memory reallocation)
97 TransformType* ref = &w[0];
98 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
99 v.push_back(w[i%w.size()]);
100 for(unsigned int i=23; i<v.size(); ++i)
101 {
102 VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
103 }
104}
105
106template<typename QuaternionType>
107void check_qtvector_quaternion(const QuaternionType&)
108{
109 typedef typename QuaternionType::Coefficients Coefficients;
110 QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
111 QVector<QuaternionType> v(10), w(20, y);
112 v[5] = x;
113 w[6] = v[5];
114 VERIFY_IS_APPROX(w[6], v[5]);
115 v = w;
116 for(int i = 0; i < 20; i++)
117 {
118 VERIFY_IS_APPROX(w[i], v[i]);
119 }
120
121 v.resize(21);
122 v[20] = x;
123 VERIFY_IS_APPROX(v[20], x);
124 v.fill(y,22);
125 //v.resize(22);
126 VERIFY_IS_APPROX(v[21], y);
127 v.push_back(x);
128 VERIFY_IS_APPROX(v[22], x);
129 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
130
131 // do a lot of push_back such that the vector gets internally resized
132 // (with memory reallocation)
133 QuaternionType* ref = &w[0];
134 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
135 v.push_back(w[i%w.size()]);
136 for(unsigned int i=23; i<v.size(); ++i)
137 {
138 VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
139 }
140}
141
142void test_qtvector()
143{
144 // some non vectorizable fixed sizes
145 CALL_SUBTEST(check_qtvector_matrix(Vector2f()));
146 CALL_SUBTEST(check_qtvector_matrix(Matrix3f()));
147 CALL_SUBTEST(check_qtvector_matrix(Matrix3d()));
148
149 // some vectorizable fixed sizes
150 CALL_SUBTEST(check_qtvector_matrix(Matrix2f()));
151 CALL_SUBTEST(check_qtvector_matrix(Vector4f()));
152 CALL_SUBTEST(check_qtvector_matrix(Matrix4f()));
153 CALL_SUBTEST(check_qtvector_matrix(Matrix4d()));
154
155 // some dynamic sizes
156 CALL_SUBTEST(check_qtvector_matrix(MatrixXd(1,1)));
157 CALL_SUBTEST(check_qtvector_matrix(VectorXd(20)));
158 CALL_SUBTEST(check_qtvector_matrix(RowVectorXf(20)));
159 CALL_SUBTEST(check_qtvector_matrix(MatrixXcf(10,10)));
160
161 // some Transform
162 CALL_SUBTEST(check_qtvector_transform(Transform2f()));
163 CALL_SUBTEST(check_qtvector_transform(Transform3f()));
164 CALL_SUBTEST(check_qtvector_transform(Transform3d()));
165 //CALL_SUBTEST(check_qtvector_transform(Transform4d()));
166
167 // some Quaternion
168 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
169 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
170}