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


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