xref: /aosp_15_r20/external/eigen/test/qtvector.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2008 Benoit Jacob <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li #define EIGEN_WORK_AROUND_QT_BUG_CALLING_WRONG_OPERATOR_NEW_FIXED_IN_QT_4_5
12*bf2c3715SXin Li 
13*bf2c3715SXin Li #include "main.h"
14*bf2c3715SXin Li #include <QtCore/QVector>
15*bf2c3715SXin Li #include <Eigen/Geometry>
16*bf2c3715SXin Li #include <Eigen/QtAlignedMalloc>
17*bf2c3715SXin Li 
18*bf2c3715SXin Li template<typename MatrixType>
check_qtvector_matrix(const MatrixType & m)19*bf2c3715SXin Li void check_qtvector_matrix(const MatrixType& m)
20*bf2c3715SXin Li {
21*bf2c3715SXin Li   Index rows = m.rows();
22*bf2c3715SXin Li   Index cols = m.cols();
23*bf2c3715SXin Li   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
24*bf2c3715SXin Li   QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
25*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
26*bf2c3715SXin Li   {
27*bf2c3715SXin Li     VERIFY_IS_APPROX(w[i], y);
28*bf2c3715SXin Li   }
29*bf2c3715SXin Li   v[5] = x;
30*bf2c3715SXin Li   w[6] = v[5];
31*bf2c3715SXin Li   VERIFY_IS_APPROX(w[6], v[5]);
32*bf2c3715SXin Li   v = w;
33*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
34*bf2c3715SXin Li   {
35*bf2c3715SXin Li     VERIFY_IS_APPROX(w[i], v[i]);
36*bf2c3715SXin Li   }
37*bf2c3715SXin Li 
38*bf2c3715SXin Li   v.resize(21);
39*bf2c3715SXin Li   v[20] = x;
40*bf2c3715SXin Li   VERIFY_IS_APPROX(v[20], x);
41*bf2c3715SXin Li   v.fill(y,22);
42*bf2c3715SXin Li   VERIFY_IS_APPROX(v[21], y);
43*bf2c3715SXin Li   v.push_back(x);
44*bf2c3715SXin Li   VERIFY_IS_APPROX(v[22], x);
45*bf2c3715SXin Li   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
46*bf2c3715SXin Li 
47*bf2c3715SXin Li   // do a lot of push_back such that the vector gets internally resized
48*bf2c3715SXin Li   // (with memory reallocation)
49*bf2c3715SXin Li   MatrixType* ref = &w[0];
50*bf2c3715SXin Li   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
51*bf2c3715SXin Li     v.push_back(w[i%w.size()]);
52*bf2c3715SXin Li   for(int i=23; i<v.size(); ++i)
53*bf2c3715SXin Li   {
54*bf2c3715SXin Li     VERIFY(v[i]==w[(i-23)%w.size()]);
55*bf2c3715SXin Li   }
56*bf2c3715SXin Li }
57*bf2c3715SXin Li 
58*bf2c3715SXin Li template<typename TransformType>
check_qtvector_transform(const TransformType &)59*bf2c3715SXin Li void check_qtvector_transform(const TransformType&)
60*bf2c3715SXin Li {
61*bf2c3715SXin Li   typedef typename TransformType::MatrixType MatrixType;
62*bf2c3715SXin Li   TransformType x(MatrixType::Random()), y(MatrixType::Random());
63*bf2c3715SXin Li   QVector<TransformType> v(10), w(20, y);
64*bf2c3715SXin Li   v[5] = x;
65*bf2c3715SXin Li   w[6] = v[5];
66*bf2c3715SXin Li   VERIFY_IS_APPROX(w[6], v[5]);
67*bf2c3715SXin Li   v = w;
68*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
69*bf2c3715SXin Li   {
70*bf2c3715SXin Li     VERIFY_IS_APPROX(w[i], v[i]);
71*bf2c3715SXin Li   }
72*bf2c3715SXin Li 
73*bf2c3715SXin Li   v.resize(21);
74*bf2c3715SXin Li   v[20] = x;
75*bf2c3715SXin Li   VERIFY_IS_APPROX(v[20], x);
76*bf2c3715SXin Li   v.fill(y,22);
77*bf2c3715SXin Li   VERIFY_IS_APPROX(v[21], y);
78*bf2c3715SXin Li   v.push_back(x);
79*bf2c3715SXin Li   VERIFY_IS_APPROX(v[22], x);
80*bf2c3715SXin Li   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
81*bf2c3715SXin Li 
82*bf2c3715SXin Li   // do a lot of push_back such that the vector gets internally resized
83*bf2c3715SXin Li   // (with memory reallocation)
84*bf2c3715SXin Li   TransformType* ref = &w[0];
85*bf2c3715SXin Li   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
86*bf2c3715SXin Li     v.push_back(w[i%w.size()]);
87*bf2c3715SXin Li   for(unsigned int i=23; int(i)<v.size(); ++i)
88*bf2c3715SXin Li   {
89*bf2c3715SXin Li     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
90*bf2c3715SXin Li   }
91*bf2c3715SXin Li }
92*bf2c3715SXin Li 
93*bf2c3715SXin Li template<typename QuaternionType>
check_qtvector_quaternion(const QuaternionType &)94*bf2c3715SXin Li void check_qtvector_quaternion(const QuaternionType&)
95*bf2c3715SXin Li {
96*bf2c3715SXin Li   typedef typename QuaternionType::Coefficients Coefficients;
97*bf2c3715SXin Li   QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
98*bf2c3715SXin Li   QVector<QuaternionType> v(10), w(20, y);
99*bf2c3715SXin Li   v[5] = x;
100*bf2c3715SXin Li   w[6] = v[5];
101*bf2c3715SXin Li   VERIFY_IS_APPROX(w[6], v[5]);
102*bf2c3715SXin Li   v = w;
103*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
104*bf2c3715SXin Li   {
105*bf2c3715SXin Li     VERIFY_IS_APPROX(w[i], v[i]);
106*bf2c3715SXin Li   }
107*bf2c3715SXin Li 
108*bf2c3715SXin Li   v.resize(21);
109*bf2c3715SXin Li   v[20] = x;
110*bf2c3715SXin Li   VERIFY_IS_APPROX(v[20], x);
111*bf2c3715SXin Li   v.fill(y,22);
112*bf2c3715SXin Li   VERIFY_IS_APPROX(v[21], y);
113*bf2c3715SXin Li   v.push_back(x);
114*bf2c3715SXin Li   VERIFY_IS_APPROX(v[22], x);
115*bf2c3715SXin Li   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
116*bf2c3715SXin Li 
117*bf2c3715SXin Li   // do a lot of push_back such that the vector gets internally resized
118*bf2c3715SXin Li   // (with memory reallocation)
119*bf2c3715SXin Li   QuaternionType* ref = &w[0];
120*bf2c3715SXin Li   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
121*bf2c3715SXin Li     v.push_back(w[i%w.size()]);
122*bf2c3715SXin Li   for(unsigned int i=23; int(i)<v.size(); ++i)
123*bf2c3715SXin Li   {
124*bf2c3715SXin Li     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
125*bf2c3715SXin Li   }
126*bf2c3715SXin Li }
127*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(qtvector)128*bf2c3715SXin Li EIGEN_DECLARE_TEST(qtvector)
129*bf2c3715SXin Li {
130*bf2c3715SXin Li   // some non vectorizable fixed sizes
131*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Vector2f()));
132*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Matrix3f()));
133*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Matrix3d()));
134*bf2c3715SXin Li 
135*bf2c3715SXin Li   // some vectorizable fixed sizes
136*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Matrix2f()));
137*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Vector4f()));
138*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Matrix4f()));
139*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(Matrix4d()));
140*bf2c3715SXin Li 
141*bf2c3715SXin Li   // some dynamic sizes
142*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(MatrixXd(1,1)));
143*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(VectorXd(20)));
144*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(RowVectorXf(20)));
145*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_matrix(MatrixXcf(10,10)));
146*bf2c3715SXin Li 
147*bf2c3715SXin Li   // some Transform
148*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_transform(Affine2f()));
149*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_transform(Affine3f()));
150*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_transform(Affine3d()));
151*bf2c3715SXin Li   //CALL_SUBTEST(check_qtvector_transform(Transform4d()));
152*bf2c3715SXin Li 
153*bf2c3715SXin Li   // some Quaternion
154*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
155*bf2c3715SXin Li   CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
156*bf2c3715SXin Li }
157