blob: 8d5ce2a2efc47fee18b13d6dd3b5343566c22072 [file] [log] [blame]
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00003//
Gael Guennebaud28e64b02010-06-24 23:21:58 +02004// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00005// 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{
Hauke Heibelf1679c72010-06-20 17:37:56 +020036 typedef typename MatrixType::Index Index;
37
38 Index rows = m.rows();
39 Index cols = m.cols();
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000040 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
41 QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
Gael Guennebaud98499312009-01-23 16:31:03 +000042 for(int i = 0; i < 20; i++)
43 {
44 VERIFY_IS_APPROX(w[i], y);
45 }
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000046 v[5] = x;
47 w[6] = v[5];
48 VERIFY_IS_APPROX(w[6], v[5]);
49 v = w;
50 for(int i = 0; i < 20; i++)
51 {
52 VERIFY_IS_APPROX(w[i], v[i]);
53 }
54
55 v.resize(21);
Benoit Jacob5f43a422009-01-21 17:10:23 +000056 v[20] = x;
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000057 VERIFY_IS_APPROX(v[20], x);
58 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000059 VERIFY_IS_APPROX(v[21], y);
60 v.push_back(x);
61 VERIFY_IS_APPROX(v[22], x);
62 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
63
64 // do a lot of push_back such that the vector gets internally resized
65 // (with memory reallocation)
66 MatrixType* ref = &w[0];
67 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
68 v.push_back(w[i%w.size()]);
69 for(int i=23; i<v.size(); ++i)
70 {
71 VERIFY(v[i]==w[(i-23)%w.size()]);
72 }
73}
74
75template<typename TransformType>
76void check_qtvector_transform(const TransformType&)
77{
78 typedef typename TransformType::MatrixType MatrixType;
79 TransformType x(MatrixType::Random()), y(MatrixType::Random());
80 QVector<TransformType> v(10), w(20, y);
81 v[5] = x;
82 w[6] = v[5];
83 VERIFY_IS_APPROX(w[6], v[5]);
84 v = w;
85 for(int i = 0; i < 20; i++)
86 {
87 VERIFY_IS_APPROX(w[i], v[i]);
88 }
89
90 v.resize(21);
91 v[20] = x;
92 VERIFY_IS_APPROX(v[20], x);
93 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000094 VERIFY_IS_APPROX(v[21], y);
95 v.push_back(x);
96 VERIFY_IS_APPROX(v[22], x);
97 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
98
99 // do a lot of push_back such that the vector gets internally resized
100 // (with memory reallocation)
101 TransformType* ref = &w[0];
102 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
103 v.push_back(w[i%w.size()]);
Benoit Jacob5f43a422009-01-21 17:10:23 +0000104 for(unsigned int i=23; int(i)<v.size(); ++i)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000105 {
106 VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
107 }
108}
109
110template<typename QuaternionType>
111void check_qtvector_quaternion(const QuaternionType&)
112{
113 typedef typename QuaternionType::Coefficients Coefficients;
114 QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
115 QVector<QuaternionType> v(10), w(20, y);
116 v[5] = x;
117 w[6] = v[5];
118 VERIFY_IS_APPROX(w[6], v[5]);
119 v = w;
120 for(int i = 0; i < 20; i++)
121 {
122 VERIFY_IS_APPROX(w[i], v[i]);
123 }
124
125 v.resize(21);
126 v[20] = x;
127 VERIFY_IS_APPROX(v[20], x);
128 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000129 VERIFY_IS_APPROX(v[21], y);
130 v.push_back(x);
131 VERIFY_IS_APPROX(v[22], x);
132 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
133
134 // do a lot of push_back such that the vector gets internally resized
135 // (with memory reallocation)
136 QuaternionType* ref = &w[0];
137 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
138 v.push_back(w[i%w.size()]);
Benoit Jacob5f43a422009-01-21 17:10:23 +0000139 for(unsigned int i=23; int(i)<v.size(); ++i)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000140 {
141 VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
142 }
143}
144
145void test_qtvector()
146{
147 // some non vectorizable fixed sizes
148 CALL_SUBTEST(check_qtvector_matrix(Vector2f()));
149 CALL_SUBTEST(check_qtvector_matrix(Matrix3f()));
150 CALL_SUBTEST(check_qtvector_matrix(Matrix3d()));
151
152 // some vectorizable fixed sizes
153 CALL_SUBTEST(check_qtvector_matrix(Matrix2f()));
154 CALL_SUBTEST(check_qtvector_matrix(Vector4f()));
155 CALL_SUBTEST(check_qtvector_matrix(Matrix4f()));
156 CALL_SUBTEST(check_qtvector_matrix(Matrix4d()));
157
158 // some dynamic sizes
159 CALL_SUBTEST(check_qtvector_matrix(MatrixXd(1,1)));
160 CALL_SUBTEST(check_qtvector_matrix(VectorXd(20)));
161 CALL_SUBTEST(check_qtvector_matrix(RowVectorXf(20)));
162 CALL_SUBTEST(check_qtvector_matrix(MatrixXcf(10,10)));
163
164 // some Transform
165 CALL_SUBTEST(check_qtvector_transform(Transform2f()));
166 CALL_SUBTEST(check_qtvector_transform(Transform3f()));
167 CALL_SUBTEST(check_qtvector_transform(Transform3d()));
168 //CALL_SUBTEST(check_qtvector_transform(Transform4d()));
169
170 // some Quaternion
171 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
172 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
173}