Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +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. |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | f645d1f | 2009-01-20 16:50:47 +0000 | [diff] [blame] | 4 | // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 6 | // This Source Code Form is subject to the terms of the Mozilla |
| 7 | // Public License v. 2.0. If a copy of the MPL was not distributed |
| 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 9 | |
Gael Guennebaud | 3009d79 | 2009-02-07 11:16:15 +0000 | [diff] [blame] | 10 | #include "main.h" |
Benoit Jacob | eac79b6 | 2009-05-09 03:41:17 +0000 | [diff] [blame] | 11 | #include <Eigen/StdVector> |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 12 | #include <Eigen/Geometry> |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 13 | |
| 14 | template<typename MatrixType> |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 15 | void check_stdvector_matrix(const MatrixType& m) |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 16 | { |
Gael Guennebaud | 12e1ebb | 2018-07-12 17:16:40 +0200 | [diff] [blame] | 17 | Index rows = m.rows(); |
| 18 | Index cols = m.cols(); |
Benoit Jacob | 0c1ef2f | 2009-01-10 13:10:23 +0000 | [diff] [blame] | 19 | MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols); |
Gael Guennebaud | 469b8aa | 2009-05-03 15:39:37 +0000 | [diff] [blame] | 20 | std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType(rows,cols)), w(20, y); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 21 | v[5] = x; |
| 22 | w[6] = v[5]; |
| 23 | VERIFY_IS_APPROX(w[6], v[5]); |
| 24 | v = w; |
| 25 | for(int i = 0; i < 20; i++) |
| 26 | { |
| 27 | VERIFY_IS_APPROX(w[i], v[i]); |
| 28 | } |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 29 | |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 30 | v.resize(21); |
Benoit Jacob | 5f43a42 | 2009-01-21 17:10:23 +0000 | [diff] [blame] | 31 | v[20] = x; |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 32 | VERIFY_IS_APPROX(v[20], x); |
| 33 | v.resize(22,y); |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 34 | VERIFY_IS_APPROX(v[21], y); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 35 | v.push_back(x); |
| 36 | VERIFY_IS_APPROX(v[22], x); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 37 | VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(MatrixType)); |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 38 | |
| 39 | // do a lot of push_back such that the vector gets internally resized |
| 40 | // (with memory reallocation) |
| 41 | MatrixType* ref = &w[0]; |
Benoit Jacob | 336ad58 | 2009-01-12 13:41:40 +0000 | [diff] [blame] | 42 | for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 43 | v.push_back(w[i%w.size()]); |
Benoit Jacob | 336ad58 | 2009-01-12 13:41:40 +0000 | [diff] [blame] | 44 | for(unsigned int i=23; i<v.size(); ++i) |
Gael Guennebaud | 9e8f437 | 2009-01-11 21:59:04 +0000 | [diff] [blame] | 45 | { |
| 46 | VERIFY(v[i]==w[(i-23)%w.size()]); |
| 47 | } |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 50 | template<typename TransformType> |
| 51 | void check_stdvector_transform(const TransformType&) |
| 52 | { |
| 53 | typedef typename TransformType::MatrixType MatrixType; |
| 54 | TransformType x(MatrixType::Random()), y(MatrixType::Random()); |
Gael Guennebaud | 469b8aa | 2009-05-03 15:39:37 +0000 | [diff] [blame] | 55 | std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y); |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 56 | v[5] = x; |
| 57 | w[6] = v[5]; |
| 58 | VERIFY_IS_APPROX(w[6], v[5]); |
| 59 | v = w; |
| 60 | for(int i = 0; i < 20; i++) |
| 61 | { |
| 62 | VERIFY_IS_APPROX(w[i], v[i]); |
| 63 | } |
| 64 | |
| 65 | v.resize(21); |
| 66 | v[20] = x; |
| 67 | VERIFY_IS_APPROX(v[20], x); |
| 68 | v.resize(22,y); |
| 69 | VERIFY_IS_APPROX(v[21], y); |
| 70 | v.push_back(x); |
| 71 | VERIFY_IS_APPROX(v[22], x); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 72 | VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(TransformType)); |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 73 | |
| 74 | // do a lot of push_back such that the vector gets internally resized |
| 75 | // (with memory reallocation) |
| 76 | TransformType* ref = &w[0]; |
| 77 | for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) |
| 78 | v.push_back(w[i%w.size()]); |
| 79 | for(unsigned int i=23; i<v.size(); ++i) |
| 80 | { |
| 81 | VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | template<typename QuaternionType> |
| 86 | void check_stdvector_quaternion(const QuaternionType&) |
| 87 | { |
| 88 | typedef typename QuaternionType::Coefficients Coefficients; |
| 89 | QuaternionType x(Coefficients::Random()), y(Coefficients::Random()); |
Gael Guennebaud | 469b8aa | 2009-05-03 15:39:37 +0000 | [diff] [blame] | 90 | std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10), w(20, y); |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 91 | v[5] = x; |
| 92 | w[6] = v[5]; |
| 93 | VERIFY_IS_APPROX(w[6], v[5]); |
| 94 | v = w; |
| 95 | for(int i = 0; i < 20; i++) |
| 96 | { |
| 97 | VERIFY_IS_APPROX(w[i], v[i]); |
| 98 | } |
| 99 | |
| 100 | v.resize(21); |
| 101 | v[20] = x; |
| 102 | VERIFY_IS_APPROX(v[20], x); |
| 103 | v.resize(22,y); |
| 104 | VERIFY_IS_APPROX(v[21], y); |
| 105 | v.push_back(x); |
| 106 | VERIFY_IS_APPROX(v[22], x); |
Gael Guennebaud | fdcad68 | 2016-05-26 17:41:28 +0200 | [diff] [blame] | 107 | VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(QuaternionType)); |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 108 | |
| 109 | // do a lot of push_back such that the vector gets internally resized |
| 110 | // (with memory reallocation) |
| 111 | QuaternionType* ref = &w[0]; |
| 112 | for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i) |
| 113 | v.push_back(w[i%w.size()]); |
| 114 | for(unsigned int i=23; i<v.size(); ++i) |
| 115 | { |
| 116 | VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs()); |
| 117 | } |
| 118 | } |
| 119 | |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 120 | void test_stdvector() |
| 121 | { |
| 122 | // some non vectorizable fixed sizes |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 123 | CALL_SUBTEST_1(check_stdvector_matrix(Vector2f())); |
| 124 | CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f())); |
| 125 | CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d())); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 126 | |
| 127 | // some vectorizable fixed sizes |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 128 | CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f())); |
| 129 | CALL_SUBTEST_1(check_stdvector_matrix(Vector4f())); |
| 130 | CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f())); |
| 131 | CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d())); |
Benoit Jacob | 0c1ef2f | 2009-01-10 13:10:23 +0000 | [diff] [blame] | 132 | |
| 133 | // some dynamic sizes |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 134 | CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1))); |
| 135 | CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20))); |
| 136 | CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20))); |
| 137 | CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10))); |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 138 | |
| 139 | // some Transform |
Hauke Heibel | 85fdcdf | 2010-08-17 20:03:50 +0200 | [diff] [blame] | 140 | CALL_SUBTEST_4(check_stdvector_transform(Projective2f())); |
| 141 | CALL_SUBTEST_4(check_stdvector_transform(Projective3f())); |
| 142 | CALL_SUBTEST_4(check_stdvector_transform(Projective3d())); |
| 143 | //CALL_SUBTEST(heck_stdvector_transform(Projective4d())); |
Benoit Jacob | 4d44ca2 | 2009-01-12 16:06:04 +0000 | [diff] [blame] | 144 | |
| 145 | // some Quaternion |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 146 | CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf())); |
| 147 | CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond())); |
Benoit Jacob | 335d3bc | 2009-01-09 23:26:45 +0000 | [diff] [blame] | 148 | } |