Main MRPT website > C++ reference
MRPT logo
CMatrixTemplateNumeric.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CMatrixTemplateNumeric_H
00029 #define CMatrixTemplateNumeric_H
00030 
00031 #include <mrpt/math/CMatrixTemplate.h>
00032 #include <mrpt/utils/CSerializable.h>
00033 
00034 #include <mrpt/system/os.h>
00035 #include <cmath>
00036 #include <limits>
00037 
00038 namespace mrpt
00039 {
00040         namespace poses
00041         {
00042                 class CPose2D;
00043                 class CPose3D;
00044                 class CPoint2D;
00045                 class CPoint3D;
00046         }
00047 
00048         namespace math
00049         {
00050                 using namespace mrpt::system;
00051 
00052                 /**  A matrix of dynamic size.
00053                   *   Basically, this class is a wrapper on Eigen::Matrix<T,Dynamic,Dynamic>, but
00054                   *   with a RowMajor element memory layout (except for column vectors).
00055                   *
00056                   * \note This class exists for backward compatibility of ancient times when MRPT didn't rely on Eigen, feel free to directly use Eigen::Matrix<> types instead.
00057                   *
00058                   * \sa CMatrixTemplate (a non Eigen lib-based  class, which can hold arbitrary objects, not only numerical types).
00059                  * \note For a complete introduction to Matrices and vectors in MRPT, see: http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
00060                  * \ingroup mrpt_base_grp
00061                   */
00062                 template <class T>
00063                 class CMatrixTemplateNumeric : 
00064                         public Eigen::Matrix<
00065                                 T,
00066                                 Eigen::Dynamic,
00067                                 Eigen::Dynamic,
00068                                 // Use row major storage for backward compatibility with MRPT matrices in all cases (even in column vectors!)
00069                                 Eigen::AutoAlign | Eigen::RowMajor
00070                                 >
00071                 {
00072                 public:
00073                         typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::AutoAlign|Eigen::RowMajor> Base;
00074                         typedef CMatrixTemplateNumeric<T> mrpt_autotype;
00075 
00076                         MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(CMatrixTemplateNumeric)
00077 
00078                         /** Default constructor, builds a 1x1 matrix */
00079                         inline CMatrixTemplateNumeric() : Base(1,1) { Base::setZero(); }
00080 
00081                         /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency is a priority.
00082                           *  Use as:
00083                           *   \code
00084                           *     CMatrixTemplateNumeric<double>  M( UNINITIALIZED_MATRIX);
00085                           *   \endcode
00086                           */
00087                         inline CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag) : Base( 0,0 ) { }
00088 
00089                         /** Constructor, creates a matrix of the given size, filled with zeros. */
00090                         inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row,col) { Base::setZero(); }
00091 
00092                         MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateNumeric) // Implements ctor and "operator =" for any other Eigen class
00093 
00094                         /** Assignment operator of other types
00095                         */
00096                         template <class R>
00097                         inline CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
00098                         {
00099                                 Base::resize( m.getRowCount(), m.getColCount() );
00100 
00101                                 for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
00102                                         for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
00103                                                 Base::coeffRef(i,j) = static_cast<T>(m.get_unsafe(i,j));
00104                                 return *this;
00105                         }
00106 
00107                         /** Constructor from a given size and a C array. The array length must match cols x row.
00108                           * \code
00109                           *  const double numbers[] = {
00110                           *    1,2,3,
00111                           *    4,5,6 };
00112                           *      CMatrixDouble   M(3,2, numbers);
00113                           * \endcode
00114                           */
00115                         template <typename V, size_t N>
00116                         inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : Base(row,col)
00117                         {
00118                                 ASSERT_EQUAL_(row*col,N)
00119                                 ASSERT_EQUAL_(sizeof(theArray[0]),sizeof(T))
00120                                 ::memcpy(Base::data(),&theArray[0],sizeof(T)*N); // Remember, row-major order!
00121                         }
00122 
00123                         /** Destructor
00124                           */
00125                         inline ~CMatrixTemplateNumeric() { }
00126 
00127                         /** == comparison of two matrices; it differs from default Eigen operator in that returns false if matrices are of different sizes instead of raising an assert. */
00128                         template <typename Derived>
00129                         inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
00130                         {
00131                                 return Base::cols()==m2.cols() && 
00132                                            Base::rows()==m2.rows() && 
00133                                            Base::cwiseEqual(m2).all();
00134                         }
00135                         /** != comparison of two matrices; it differs from default Eigen operator in that returns true if matrices are of different sizes instead of raising an assert. */
00136                         template <typename Derived>
00137                         inline bool operator !=(const Eigen::MatrixBase<Derived>& m2)  const{ return !((*this)==m2); }
00138 
00139                 }; // end of class definition
00140 
00141 
00142                 /** Declares a matrix of float numbers (non serializable).
00143                   *  For a serializable version, use math::CMatrix
00144                   *  \sa CMatrixDouble, CMatrix, CMatrixD
00145                   */
00146                 typedef CMatrixTemplateNumeric<float> CMatrixFloat;
00147 
00148                 /** Declares a matrix of double numbers (non serializable).
00149                   *  For a serializable version, use math::CMatrixD
00150                   *  \sa CMatrixFloat, CMatrix, CMatrixD
00151                   */
00152                 typedef CMatrixTemplateNumeric<double> CMatrixDouble;
00153 
00154                 /** Declares a matrix of unsigned ints (non serializable).
00155                   *  \sa CMatrixDouble, CMatrixFloat
00156                   */
00157                 typedef CMatrixTemplateNumeric<unsigned int> CMatrixUInt;
00158 
00159                 /** Declares a matrix of booleans (non serializable).
00160                   *  \sa CMatrixDouble, CMatrixFloat, CMatrixB
00161                   */
00162                 typedef CMatrixTemplate<bool> CMatrixBool;
00163 
00164 #ifdef HAVE_LONG_DOUBLE
00165                 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
00166                   *  \sa CMatrixDouble, CMatrixFloat
00167                   */
00168                 typedef CMatrixTemplateNumeric<long double> CMatrixLongDouble;
00169 #else
00170                 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
00171                   *  \sa CMatrixDouble, CMatrixFloat
00172                   */
00173                 typedef CMatrixTemplateNumeric<double> CMatrixLongDouble;
00174 #endif
00175 
00176 
00177                 namespace detail        
00178                 {
00179                         /**
00180                           * Vicinity traits class specialization for fixed size matrices.
00181                           */
00182                         template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> >   {
00183                         public:
00184                                 inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N)  {
00185                                         mat.setSize(N,N);
00186                                         mat.fill(0);
00187                                 }
00188                                 inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t)       {
00189                                         mat.get_unsafe(r,c)=t;
00190                                 }
00191                         };
00192                 }       //End of detail namespace.
00193 
00194         } // End of namespace
00195 
00196 
00197         namespace utils
00198         {
00199                 // Extensions to mrpt::utils::TTypeName for matrices:
00200                 template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
00201                         static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
00202                 };
00203         }
00204 
00205 } // End of namespace
00206 
00207 
00208 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011