qtvector.cpp (4621B)
1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> 5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> 6 // 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/. 10 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 18 template<typename MatrixType> 19 void check_qtvector_matrix(const MatrixType& m) 20 { 21 Index rows = m.rows(); 22 Index cols = m.cols(); 23 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols); 24 QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y); 25 for(int i = 0; i < 20; i++) 26 { 27 VERIFY_IS_APPROX(w[i], y); 28 } 29 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); 39 v[20] = x; 40 VERIFY_IS_APPROX(v[20], x); 41 v.fill(y,22); 42 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 58 template<typename TransformType> 59 void 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); 77 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()]); 87 for(unsigned int i=23; int(i)<v.size(); ++i) 88 { 89 VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix()); 90 } 91 } 92 93 template<typename QuaternionType> 94 void 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); 112 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()]); 122 for(unsigned int i=23; int(i)<v.size(); ++i) 123 { 124 VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs()); 125 } 126 } 127 128 EIGEN_DECLARE_TEST(qtvector) 129 { 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 148 CALL_SUBTEST(check_qtvector_transform(Affine2f())); 149 CALL_SUBTEST(check_qtvector_transform(Affine3f())); 150 CALL_SUBTEST(check_qtvector_transform(Affine3d())); 151 //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 }