stdvector_overload.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 
00028 #include <Eigen/StdVector>
00029 #include <Eigen/Geometry>
00030 
00031 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Vector4f)
00032 
00033 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix2f)
00034 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix4f)
00035 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Matrix4d)
00036 
00037 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Affine3f)
00038 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Affine3d)
00039 
00040 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Quaternionf)
00041 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Quaterniond)
00042 
00043 template<typename MatrixType>
00044 void check_stdvector_matrix(const MatrixType& m)
00045 {
00046   typename MatrixType::Index rows = m.rows();
00047   typename MatrixType::Index cols = m.cols();
00048   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
00049   std::vector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
00050   v[5] = x;
00051   w[6] = v[5];
00052   VERIFY_IS_APPROX(w[6], v[5]);
00053   v = w;
00054   for(int i = 0; i < 20; i++)
00055   {
00056     VERIFY_IS_APPROX(w[i], v[i]);
00057   }
00058 
00059   v.resize(21);
00060   v[20] = x;
00061   VERIFY_IS_APPROX(v[20], x);
00062   v.resize(22,y);
00063   VERIFY_IS_APPROX(v[21], y);
00064   v.push_back(x);
00065   VERIFY_IS_APPROX(v[22], x);
00066   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
00067 
00068   // do a lot of push_back such that the vector gets internally resized
00069   // (with memory reallocation)
00070   MatrixType* ref = &w[0];
00071   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00072     v.push_back(w[i%w.size()]);
00073   for(unsigned int i=23; i<v.size(); ++i)
00074   {
00075     VERIFY(v[i]==w[(i-23)%w.size()]);
00076   }
00077 }
00078 
00079 template<typename TransformType>
00080 void check_stdvector_transform(const TransformType&)
00081 {
00082   typedef typename TransformType::MatrixType MatrixType;
00083   TransformType x(MatrixType::Random()), y(MatrixType::Random());
00084   std::vector<TransformType> v(10), w(20, y);
00085   v[5] = x;
00086   w[6] = v[5];
00087   VERIFY_IS_APPROX(w[6], v[5]);
00088   v = w;
00089   for(int i = 0; i < 20; i++)
00090   {
00091     VERIFY_IS_APPROX(w[i], v[i]);
00092   }
00093 
00094   v.resize(21);
00095   v[20] = x;
00096   VERIFY_IS_APPROX(v[20], x);
00097   v.resize(22,y);
00098   VERIFY_IS_APPROX(v[21], y);
00099   v.push_back(x);
00100   VERIFY_IS_APPROX(v[22], x);
00101   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
00102 
00103   // do a lot of push_back such that the vector gets internally resized
00104   // (with memory reallocation)
00105   TransformType* ref = &w[0];
00106   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00107     v.push_back(w[i%w.size()]);
00108   for(unsigned int i=23; i<v.size(); ++i)
00109   {
00110     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
00111   }
00112 }
00113 
00114 template<typename QuaternionType>
00115 void check_stdvector_quaternion(const QuaternionType&)
00116 {
00117   typedef typename QuaternionType::Coefficients Coefficients;
00118   QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
00119   std::vector<QuaternionType> v(10), w(20, y);
00120   v[5] = x;
00121   w[6] = v[5];
00122   VERIFY_IS_APPROX(w[6], v[5]);
00123   v = w;
00124   for(int i = 0; i < 20; i++)
00125   {
00126     VERIFY_IS_APPROX(w[i], v[i]);
00127   }
00128 
00129   v.resize(21);
00130   v[20] = x;
00131   VERIFY_IS_APPROX(v[20], x);
00132   v.resize(22,y);
00133   VERIFY_IS_APPROX(v[21], y);
00134   v.push_back(x);
00135   VERIFY_IS_APPROX(v[22], x);
00136   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
00137 
00138   // do a lot of push_back such that the vector gets internally resized
00139   // (with memory reallocation)
00140   QuaternionType* ref = &w[0];
00141   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00142     v.push_back(w[i%w.size()]);
00143   for(unsigned int i=23; i<v.size(); ++i)
00144   {
00145     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
00146   }
00147 }
00148 
00149 void test_stdvector_overload()
00150 {
00151   // some non vectorizable fixed sizes
00152   CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
00153   CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
00154   CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
00155 
00156   // some vectorizable fixed sizes
00157   CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
00158   CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
00159   CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
00160   CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
00161 
00162   // some dynamic sizes
00163   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
00164   CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
00165   CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
00166   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
00167 
00168   // some Transform
00169   CALL_SUBTEST_4(check_stdvector_transform(Affine2f())); // does not need the specialization (2+1)^2 = 9
00170   CALL_SUBTEST_4(check_stdvector_transform(Affine3f()));
00171   CALL_SUBTEST_4(check_stdvector_transform(Affine3d()));
00172 
00173   // some Quaternion
00174   CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
00175   CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
00176 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:00