blob: 4ec79b1e609ee7f0d8bffa7d3f9834ef73cf1e36 [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//
Benoit Jacob69124cf2012-07-13 14:42:47 -04007// 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/.
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000010
11#define EIGEN_WORK_AROUND_QT_BUG_CALLING_WRONG_OPERATOR_NEW_FIXED_IN_QT_4_5
12
13#include "main.h"
14#include <QtCore/QVector>
15#include <Eigen/Geometry>
16#include <Eigen/QtAlignedMalloc>
17
18template<typename MatrixType>
19void check_qtvector_matrix(const MatrixType& m)
20{
Hauke Heibelf1679c72010-06-20 17:37:56 +020021 Index rows = m.rows();
22 Index cols = m.cols();
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000023 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
24 QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
Gael Guennebaud98499312009-01-23 16:31:03 +000025 for(int i = 0; i < 20; i++)
26 {
27 VERIFY_IS_APPROX(w[i], y);
28 }
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000029 v[5] = x;
30 w[6] = v[5];
31 VERIFY_IS_APPROX(w[6], v[5]);
32 v = w;
33 for(int i = 0; i < 20; i++)
34 {
35 VERIFY_IS_APPROX(w[i], v[i]);
36 }
37
38 v.resize(21);
Benoit Jacob5f43a422009-01-21 17:10:23 +000039 v[20] = x;
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000040 VERIFY_IS_APPROX(v[20], x);
41 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000042 VERIFY_IS_APPROX(v[21], y);
43 v.push_back(x);
44 VERIFY_IS_APPROX(v[22], x);
45 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
46
47 // do a lot of push_back such that the vector gets internally resized
48 // (with memory reallocation)
49 MatrixType* ref = &w[0];
50 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
51 v.push_back(w[i%w.size()]);
52 for(int i=23; i<v.size(); ++i)
53 {
54 VERIFY(v[i]==w[(i-23)%w.size()]);
55 }
56}
57
58template<typename TransformType>
59void check_qtvector_transform(const TransformType&)
60{
61 typedef typename TransformType::MatrixType MatrixType;
62 TransformType x(MatrixType::Random()), y(MatrixType::Random());
63 QVector<TransformType> v(10), w(20, y);
64 v[5] = x;
65 w[6] = v[5];
66 VERIFY_IS_APPROX(w[6], v[5]);
67 v = w;
68 for(int i = 0; i < 20; i++)
69 {
70 VERIFY_IS_APPROX(w[i], v[i]);
71 }
72
73 v.resize(21);
74 v[20] = x;
75 VERIFY_IS_APPROX(v[20], x);
76 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000077 VERIFY_IS_APPROX(v[21], y);
78 v.push_back(x);
79 VERIFY_IS_APPROX(v[22], x);
80 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
81
82 // do a lot of push_back such that the vector gets internally resized
83 // (with memory reallocation)
84 TransformType* ref = &w[0];
85 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
86 v.push_back(w[i%w.size()]);
Benoit Jacob5f43a422009-01-21 17:10:23 +000087 for(unsigned int i=23; int(i)<v.size(); ++i)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000088 {
89 VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
90 }
91}
92
93template<typename QuaternionType>
94void check_qtvector_quaternion(const QuaternionType&)
95{
96 typedef typename QuaternionType::Coefficients Coefficients;
97 QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
98 QVector<QuaternionType> v(10), w(20, y);
99 v[5] = x;
100 w[6] = v[5];
101 VERIFY_IS_APPROX(w[6], v[5]);
102 v = w;
103 for(int i = 0; i < 20; i++)
104 {
105 VERIFY_IS_APPROX(w[i], v[i]);
106 }
107
108 v.resize(21);
109 v[20] = x;
110 VERIFY_IS_APPROX(v[20], x);
111 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000112 VERIFY_IS_APPROX(v[21], y);
113 v.push_back(x);
114 VERIFY_IS_APPROX(v[22], x);
115 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
116
117 // do a lot of push_back such that the vector gets internally resized
118 // (with memory reallocation)
119 QuaternionType* ref = &w[0];
120 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
121 v.push_back(w[i%w.size()]);
Benoit Jacob5f43a422009-01-21 17:10:23 +0000122 for(unsigned int i=23; int(i)<v.size(); ++i)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000123 {
124 VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
125 }
126}
127
Gael Guennebaud82f0ce22018-07-17 14:46:15 +0200128EIGEN_DECLARE_TEST(qtvector)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000129{
130 // some non vectorizable fixed sizes
131 CALL_SUBTEST(check_qtvector_matrix(Vector2f()));
132 CALL_SUBTEST(check_qtvector_matrix(Matrix3f()));
133 CALL_SUBTEST(check_qtvector_matrix(Matrix3d()));
134
135 // some vectorizable fixed sizes
136 CALL_SUBTEST(check_qtvector_matrix(Matrix2f()));
137 CALL_SUBTEST(check_qtvector_matrix(Vector4f()));
138 CALL_SUBTEST(check_qtvector_matrix(Matrix4f()));
139 CALL_SUBTEST(check_qtvector_matrix(Matrix4d()));
140
141 // some dynamic sizes
142 CALL_SUBTEST(check_qtvector_matrix(MatrixXd(1,1)));
143 CALL_SUBTEST(check_qtvector_matrix(VectorXd(20)));
144 CALL_SUBTEST(check_qtvector_matrix(RowVectorXf(20)));
145 CALL_SUBTEST(check_qtvector_matrix(MatrixXcf(10,10)));
146
147 // some Transform
Hauke Heibel85fdcdf2010-08-17 20:03:50 +0200148 CALL_SUBTEST(check_qtvector_transform(Affine2f()));
149 CALL_SUBTEST(check_qtvector_transform(Affine3f()));
150 CALL_SUBTEST(check_qtvector_transform(Affine3d()));
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000151 //CALL_SUBTEST(check_qtvector_transform(Transform4d()));
152
153 // some Quaternion
154 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
155 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
156}