Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 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 | |
| 33 | template<typename MatrixType> |
| 34 | void check_qtvector_matrix(const MatrixType& m) |
| 35 | { |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 36 | typedef typename MatrixType::Index Index; |
| 37 | |
| 38 | Index rows = m.rows(); |
| 39 | Index cols = m.cols(); |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 40 | MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols); |
| 41 | QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y); |
Gael Guennebaud | 9849931 | 2009-01-23 16:31:03 +0000 | [diff] [blame] | 42 | for(int i = 0; i < 20; i++) |
| 43 | { |
| 44 | VERIFY_IS_APPROX(w[i], y); |
| 45 | } |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 46 | 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 Jacob | 5f43a42 | 2009-01-21 17:10:23 +0000 | [diff] [blame] | 56 | v[20] = x; |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 57 | VERIFY_IS_APPROX(v[20], x); |
| 58 | v.fill(y,22); |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 59 | 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 | |
| 75 | template<typename TransformType> |
| 76 | void 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 Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 94 | 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 Jacob | 5f43a42 | 2009-01-21 17:10:23 +0000 | [diff] [blame] | 104 | for(unsigned int i=23; int(i)<v.size(); ++i) |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 105 | { |
| 106 | VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix()); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | template<typename QuaternionType> |
| 111 | void 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 Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 129 | 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 Jacob | 5f43a42 | 2009-01-21 17:10:23 +0000 | [diff] [blame] | 139 | for(unsigned int i=23; int(i)<v.size(); ++i) |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 140 | { |
| 141 | VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void 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 |
Hauke Heibel | 85fdcdf | 2010-08-17 20:03:50 +0200 | [diff] [blame^] | 165 | CALL_SUBTEST(check_qtvector_transform(Affine2f())); |
| 166 | CALL_SUBTEST(check_qtvector_transform(Affine3f())); |
| 167 | CALL_SUBTEST(check_qtvector_transform(Affine3d())); |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 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 | } |