eigen2_newstdvector.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 #define EIGEN_USE_NEW_STDVECTOR
00026 #include "main.h"
00027 #include <Eigen/StdVector>
00028 #include <Eigen/Geometry>
00029 
00030 template<typename MatrixType>
00031 void check_stdvector_matrix(const MatrixType& m)
00032 {
00033   int rows = m.rows();
00034   int cols = m.cols();
00035   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
00036   std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType(rows,cols)), w(20, y);
00037   v[5] = x;
00038   w[6] = v[5];
00039   VERIFY_IS_APPROX(w[6], v[5]);
00040   v = w;
00041   for(int i = 0; i < 20; i++)
00042   {
00043     VERIFY_IS_APPROX(w[i], v[i]);
00044   }
00045 
00046   v.resize(21);
00047   v[20] = x;
00048   VERIFY_IS_APPROX(v[20], x);
00049   v.resize(22,y);
00050   VERIFY_IS_APPROX(v[21], y);
00051   v.push_back(x);
00052   VERIFY_IS_APPROX(v[22], x);
00053   VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(MatrixType));
00054 
00055   // do a lot of push_back such that the vector gets internally resized
00056   // (with memory reallocation)
00057   MatrixType* ref = &w[0];
00058   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00059     v.push_back(w[i%w.size()]);
00060   for(unsigned int i=23; i<v.size(); ++i)
00061   {
00062     VERIFY(v[i]==w[(i-23)%w.size()]);
00063   }
00064 }
00065 
00066 template<typename TransformType>
00067 void check_stdvector_transform(const TransformType&)
00068 {
00069   typedef typename TransformType::MatrixType MatrixType;
00070   TransformType x(MatrixType::Random()), y(MatrixType::Random());
00071   std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
00072   v[5] = x;
00073   w[6] = v[5];
00074   VERIFY_IS_APPROX(w[6], v[5]);
00075   v = w;
00076   for(int i = 0; i < 20; i++)
00077   {
00078     VERIFY_IS_APPROX(w[i], v[i]);
00079   }
00080 
00081   v.resize(21);
00082   v[20] = x;
00083   VERIFY_IS_APPROX(v[20], x);
00084   v.resize(22,y);
00085   VERIFY_IS_APPROX(v[21], y);
00086   v.push_back(x);
00087   VERIFY_IS_APPROX(v[22], x);
00088   VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(TransformType));
00089 
00090   // do a lot of push_back such that the vector gets internally resized
00091   // (with memory reallocation)
00092   TransformType* ref = &w[0];
00093   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00094     v.push_back(w[i%w.size()]);
00095   for(unsigned int i=23; i<v.size(); ++i)
00096   {
00097     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
00098   }
00099 }
00100 
00101 template<typename QuaternionType>
00102 void check_stdvector_quaternion(const QuaternionType&)
00103 {
00104   typedef typename QuaternionType::Coefficients Coefficients;
00105   QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
00106   std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10), w(20, y);
00107   v[5] = x;
00108   w[6] = v[5];
00109   VERIFY_IS_APPROX(w[6], v[5]);
00110   v = w;
00111   for(int i = 0; i < 20; i++)
00112   {
00113     VERIFY_IS_APPROX(w[i], v[i]);
00114   }
00115 
00116   v.resize(21);
00117   v[20] = x;
00118   VERIFY_IS_APPROX(v[20], x);
00119   v.resize(22,y);
00120   VERIFY_IS_APPROX(v[21], y);
00121   v.push_back(x);
00122   VERIFY_IS_APPROX(v[22], x);
00123   VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(QuaternionType));
00124 
00125   // do a lot of push_back such that the vector gets internally resized
00126   // (with memory reallocation)
00127   QuaternionType* ref = &w[0];
00128   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00129     v.push_back(w[i%w.size()]);
00130   for(unsigned int i=23; i<v.size(); ++i)
00131   {
00132     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
00133   }
00134 }
00135 
00136 void test_eigen2_newstdvector()
00137 {
00138   // some non vectorizable fixed sizes
00139   CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
00140   CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
00141   CALL_SUBTEST_1(check_stdvector_matrix(Matrix3d()));
00142 
00143   // some vectorizable fixed sizes
00144   CALL_SUBTEST_2(check_stdvector_matrix(Matrix2f()));
00145   CALL_SUBTEST_2(check_stdvector_matrix(Vector4f()));
00146   CALL_SUBTEST_2(check_stdvector_matrix(Matrix4f()));
00147   CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
00148 
00149   // some dynamic sizes
00150   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
00151   CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
00152   CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
00153   CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
00154 
00155   // some Transform
00156   CALL_SUBTEST_4(check_stdvector_transform(Transform2f()));
00157   CALL_SUBTEST_4(check_stdvector_transform(Transform3f()));
00158   CALL_SUBTEST_4(check_stdvector_transform(Transform3d()));
00159   //CALL_SUBTEST(check_stdvector_transform(Transform4d()));
00160 
00161   // some Quaternion
00162   CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
00163   CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
00164 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:38