Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. Eigen itself is part of the KDE project. |
| 3 | // |
| 4 | // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> |
| 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Lesser General Public |
| 8 | // License as published by the Free Software Foundation; either |
| 9 | // version 3 of the License, or (at your option) any later version. |
| 10 | // |
| 11 | // Alternatively, you can redistribute it and/or |
| 12 | // modify it under the terms of the GNU General Public License as |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 14 | // the License, or (at your option) any later version. |
| 15 | // |
| 16 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 19 | // GNU General Public License for more details. |
| 20 | // |
| 21 | // You should have received a copy of the GNU Lesser General Public |
| 22 | // License and a copy of the GNU General Public License along with |
| 23 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include "main.h" |
| 26 | #include <Eigen/StdVector> |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 27 | #include <Eigen/Geometry> |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 28 | |
| 29 | template<typename MatrixType> |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 30 | void check_stdvector_matrix(const MatrixType& m) |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 31 | { |
Benoit Jacob | 0c1ef2f | 2009-01-10 13:10:23 +0000 | [diff] [blame] | 32 | int rows = m.rows(); |
| 33 | int cols = m.cols(); |
| 34 | MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols); |
| 35 | std::vector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 36 | v[5] = x; |
| 37 | w[6] = v[5]; |
| 38 | VERIFY_IS_APPROX(w[6], v[5]); |
| 39 | v = w; |
| 40 | for(int i = 0; i < 20; i++) |
| 41 | { |
| 42 | VERIFY_IS_APPROX(w[i], v[i]); |
| 43 | } |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 44 | |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 45 | v.resize(21); |
Benoit Jacob | 0c1ef2f | 2009-01-10 13:10:23 +0000 | [diff] [blame] | 46 | v[20].set(x); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 47 | VERIFY_IS_APPROX(v[20], x); |
| 48 | v.resize(22,y); |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 49 | VERIFY_IS_APPROX(v[21], y); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 50 | v.push_back(x); |
| 51 | VERIFY_IS_APPROX(v[22], x); |
Benoit Jacob | 0c1ef2f | 2009-01-10 13:10:23 +0000 | [diff] [blame] | 52 | VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType)); |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 53 | |
| 54 | // do a lot of push_back such that the vector gets internally resized |
| 55 | // (with memory reallocation) |
| 56 | MatrixType* ref = &w[0]; |
Benoit Jacob | 336ad58 | 2009-01-12 13:41:40 +0000 | [diff] [blame] | 57 | for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 58 | v.push_back(w[i%w.size()]); |
Benoit Jacob | 336ad58 | 2009-01-12 13:41:40 +0000 | [diff] [blame] | 59 | for(unsigned int i=23; i<v.size(); ++i) |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 60 | { |
| 61 | VERIFY(v[i]==w[(i-23)%w.size()]); |
| 62 | } |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 65 | template<typename TransformType> |
| 66 | void check_stdvector_transform(const TransformType&) |
| 67 | { |
| 68 | typedef typename TransformType::MatrixType MatrixType; |
| 69 | TransformType x(MatrixType::Random()), y(MatrixType::Random()); |
| 70 | std::vector<TransformType> v(10), w(20, y); |
| 71 | v[5] = x; |
| 72 | w[6] = v[5]; |
| 73 | VERIFY_IS_APPROX(w[6], v[5]); |
| 74 | v = w; |
| 75 | for(int i = 0; i < 20; i++) |
| 76 | { |
| 77 | VERIFY_IS_APPROX(w[i], v[i]); |
| 78 | } |
| 79 | |
| 80 | v.resize(21); |
| 81 | v[20] = x; |
| 82 | VERIFY_IS_APPROX(v[20], x); |
| 83 | v.resize(22,y); |
| 84 | VERIFY_IS_APPROX(v[21], y); |
| 85 | v.push_back(x); |
| 86 | VERIFY_IS_APPROX(v[22], x); |
| 87 | VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType)); |
| 88 | |
| 89 | // do a lot of push_back such that the vector gets internally resized |
| 90 | // (with memory reallocation) |
| 91 | TransformType* ref = &w[0]; |
| 92 | for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) |
| 93 | v.push_back(w[i%w.size()]); |
| 94 | for(unsigned int i=23; i<v.size(); ++i) |
| 95 | { |
| 96 | VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix()); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | template<typename QuaternionType> |
| 101 | void check_stdvector_quaternion(const QuaternionType&) |
| 102 | { |
| 103 | typedef typename QuaternionType::Coefficients Coefficients; |
| 104 | QuaternionType x(Coefficients::Random()), y(Coefficients::Random()); |
| 105 | std::vector<QuaternionType> v(10), w(20, y); |
| 106 | v[5] = x; |
| 107 | w[6] = v[5]; |
| 108 | VERIFY_IS_APPROX(w[6], v[5]); |
| 109 | v = w; |
| 110 | for(int i = 0; i < 20; i++) |
| 111 | { |
| 112 | VERIFY_IS_APPROX(w[i], v[i]); |
| 113 | } |
| 114 | |
| 115 | v.resize(21); |
| 116 | v[20] = x; |
| 117 | VERIFY_IS_APPROX(v[20], x); |
| 118 | v.resize(22,y); |
| 119 | VERIFY_IS_APPROX(v[21], y); |
| 120 | v.push_back(x); |
| 121 | VERIFY_IS_APPROX(v[22], x); |
| 122 | VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType)); |
| 123 | |
| 124 | // do a lot of push_back such that the vector gets internally resized |
| 125 | // (with memory reallocation) |
| 126 | QuaternionType* ref = &w[0]; |
| 127 | for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) |
| 128 | v.push_back(w[i%w.size()]); |
| 129 | for(unsigned int i=23; i<v.size(); ++i) |
| 130 | { |
| 131 | VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs()); |
| 132 | } |
| 133 | } |
| 134 | |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 135 | void test_stdvector() |
| 136 | { |
| 137 | // some non vectorizable fixed sizes |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 138 | CALL_SUBTEST(check_stdvector_matrix(Vector2f())); |
| 139 | CALL_SUBTEST(check_stdvector_matrix(Matrix3f())); |
| 140 | CALL_SUBTEST(check_stdvector_matrix(Matrix3d())); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 141 | |
| 142 | // some vectorizable fixed sizes |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 143 | CALL_SUBTEST(check_stdvector_matrix(Matrix2f())); |
| 144 | CALL_SUBTEST(check_stdvector_matrix(Vector4f())); |
| 145 | CALL_SUBTEST(check_stdvector_matrix(Matrix4f())); |
| 146 | CALL_SUBTEST(check_stdvector_matrix(Matrix4d())); |
Benoit Jacob | 0c1ef2f | 2009-01-10 13:10:23 +0000 | [diff] [blame] | 147 | |
| 148 | // some dynamic sizes |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 149 | CALL_SUBTEST(check_stdvector_matrix(MatrixXd(1,1))); |
| 150 | CALL_SUBTEST(check_stdvector_matrix(VectorXd(20))); |
| 151 | CALL_SUBTEST(check_stdvector_matrix(RowVectorXf(20))); |
| 152 | CALL_SUBTEST(check_stdvector_matrix(MatrixXcf(10,10))); |
| 153 | |
| 154 | // some Transform |
| 155 | CALL_SUBTEST(check_stdvector_transform(Transform2f())); |
| 156 | CALL_SUBTEST(check_stdvector_transform(Transform3f())); |
| 157 | CALL_SUBTEST(check_stdvector_transform(Transform3d())); |
| 158 | //CALL_SUBTEST(check_stdvector_transform(Transform4d())); |
| 159 | |
| 160 | // some Quaternion |
| 161 | CALL_SUBTEST(check_stdvector_quaternion(Quaternionf())); |
| 162 | CALL_SUBTEST(check_stdvector_quaternion(Quaternionf())); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 163 | } |