eigen2_qtvector.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
00005 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@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 #define EIGEN_WORK_AROUND_QT_BUG_CALLING_WRONG_OPERATOR_NEW_FIXED_IN_QT_4_5
00027 
00028 #include "main.h"
00029 
00030 #include <Eigen/Geometry>
00031 #include <Eigen/QtAlignedMalloc>
00032 
00033 #include <QtCore/QVector>
00034 
00035 template<typename MatrixType>
00036 void check_qtvector_matrix(const MatrixType& m)
00037 {
00038   int rows = m.rows();
00039   int cols = m.cols();
00040   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
00041   QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
00042   for(int i = 0; i < 20; i++)
00043   {
00044     VERIFY_IS_APPROX(w[i], y);
00045   }
00046   v[5] = x;
00047   w[6] = v[5];
00048   VERIFY_IS_APPROX(w[6], v[5]);
00049   v = w;
00050   for(int i = 0; i < 20; i++)
00051   {
00052     VERIFY_IS_APPROX(w[i], v[i]);
00053   }
00054 
00055   v.resize(21);
00056   v[20] = x;
00057   VERIFY_IS_APPROX(v[20], x);
00058   v.fill(y,22);
00059   VERIFY_IS_APPROX(v[21], y);
00060   v.push_back(x);
00061   VERIFY_IS_APPROX(v[22], x);
00062   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
00063 
00064   // do a lot of push_back such that the vector gets internally resized
00065   // (with memory reallocation)
00066   MatrixType* ref = &w[0];
00067   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00068     v.push_back(w[i%w.size()]);
00069   for(int i=23; i<v.size(); ++i)
00070   {
00071     VERIFY(v[i]==w[(i-23)%w.size()]);
00072   }
00073 }
00074 
00075 template<typename TransformType>
00076 void check_qtvector_transform(const TransformType&)
00077 {
00078   typedef typename TransformType::MatrixType MatrixType;
00079   TransformType x(MatrixType::Random()), y(MatrixType::Random());
00080   QVector<TransformType> v(10), w(20, y);
00081   v[5] = x;
00082   w[6] = v[5];
00083   VERIFY_IS_APPROX(w[6], v[5]);
00084   v = w;
00085   for(int i = 0; i < 20; i++)
00086   {
00087     VERIFY_IS_APPROX(w[i], v[i]);
00088   }
00089 
00090   v.resize(21);
00091   v[20] = x;
00092   VERIFY_IS_APPROX(v[20], x);
00093   v.fill(y,22);
00094   VERIFY_IS_APPROX(v[21], y);
00095   v.push_back(x);
00096   VERIFY_IS_APPROX(v[22], x);
00097   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
00098 
00099   // do a lot of push_back such that the vector gets internally resized
00100   // (with memory reallocation)
00101   TransformType* ref = &w[0];
00102   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00103     v.push_back(w[i%w.size()]);
00104   for(unsigned int i=23; int(i)<v.size(); ++i)
00105   {
00106     VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
00107   }
00108 }
00109 
00110 template<typename QuaternionType>
00111 void check_qtvector_quaternion(const QuaternionType&)
00112 {
00113   typedef typename QuaternionType::Coefficients Coefficients;
00114   QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
00115   QVector<QuaternionType> v(10), w(20, y);
00116   v[5] = x;
00117   w[6] = v[5];
00118   VERIFY_IS_APPROX(w[6], v[5]);
00119   v = w;
00120   for(int i = 0; i < 20; i++)
00121   {
00122     VERIFY_IS_APPROX(w[i], v[i]);
00123   }
00124 
00125   v.resize(21);
00126   v[20] = x;
00127   VERIFY_IS_APPROX(v[20], x);
00128   v.fill(y,22);
00129   VERIFY_IS_APPROX(v[21], y);
00130   v.push_back(x);
00131   VERIFY_IS_APPROX(v[22], x);
00132   VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
00133 
00134   // do a lot of push_back such that the vector gets internally resized
00135   // (with memory reallocation)
00136   QuaternionType* ref = &w[0];
00137   for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
00138     v.push_back(w[i%w.size()]);
00139   for(unsigned int i=23; int(i)<v.size(); ++i)
00140   {
00141     VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
00142   }
00143 }
00144 
00145 void test_eigen2_qtvector()
00146 {
00147   // some non vectorizable fixed sizes
00148   CALL_SUBTEST_1(check_qtvector_matrix(Vector2f()));
00149   CALL_SUBTEST_1(check_qtvector_matrix(Matrix3f()));
00150   CALL_SUBTEST_1(check_qtvector_matrix(Matrix3d()));
00151 
00152   // some vectorizable fixed sizes
00153   CALL_SUBTEST_2(check_qtvector_matrix(Matrix2f()));
00154   CALL_SUBTEST_2(check_qtvector_matrix(Vector4f()));
00155   CALL_SUBTEST_2(check_qtvector_matrix(Matrix4f()));
00156   CALL_SUBTEST_2(check_qtvector_matrix(Matrix4d()));
00157 
00158   // some dynamic sizes
00159   CALL_SUBTEST_3(check_qtvector_matrix(MatrixXd(1,1)));
00160   CALL_SUBTEST_3(check_qtvector_matrix(VectorXd(20)));
00161   CALL_SUBTEST_3(check_qtvector_matrix(RowVectorXf(20)));
00162   CALL_SUBTEST_3(check_qtvector_matrix(MatrixXcf(10,10)));
00163 
00164   // some Transform
00165   CALL_SUBTEST_4(check_qtvector_transform(Transform2f()));
00166   CALL_SUBTEST_4(check_qtvector_transform(Transform3f()));
00167   CALL_SUBTEST_4(check_qtvector_transform(Transform3d()));
00168   //CALL_SUBTEST_4(check_qtvector_transform(Transform4d()));
00169 
00170   // some Quaternion
00171   CALL_SUBTEST_5(check_qtvector_quaternion(Quaternionf()));
00172   CALL_SUBTEST_5(check_qtvector_quaternion(Quaternionf()));
00173 }


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