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://mrpt.sourceforge.net/                            |
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/math/matrix_iterators.h>
00033 #include <mrpt/utils/CSerializable.h>
00034 
00035 #include <mrpt/system/os.h>
00036 #include <cmath>
00037 #include <limits>
00038 
00039 namespace mrpt
00040 {
00041         namespace poses
00042         {
00043                 class CPose2D;
00044                 class CPose3D;
00045                 class CPoint2D;
00046                 class CPoint3D;
00047         }
00048 
00049         namespace math
00050         {
00051                 using namespace mrpt::system;
00052 
00053                 /**  A matrix of dynamic size.
00054                   *   Basically, this class is a wrapper on Eigen::Matrix<T,Dynamic,Dynamic>, but
00055                   *   with a RowMajor element memory layout (except for column vectors).
00056                   *
00057                   * \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.
00058                   *
00059                   * \sa CMatrixTemplate (a non Eigen lib-based  class, which can hold arbitrary objects, not only numerical types).
00060                   */
00061                 template <class T>
00062                 class CMatrixTemplateNumeric : 
00063                         public Eigen::Matrix<
00064                                 T,
00065                                 Eigen::Dynamic,
00066                                 Eigen::Dynamic,
00067                                 // Use row major storage for backward compatibility with MRPT matrices in all cases (even in column vectors!)
00068                                 Eigen::AutoAlign | Eigen::RowMajor
00069                                 >
00070                 {
00071                 public:
00072                         typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::AutoAlign|Eigen::RowMajor> Base;
00073                         typedef CMatrixTemplateNumeric<T> mrpt_autotype;
00074 
00075                         MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(CMatrixTemplateNumeric)
00076 
00077                         /** Default constructor, builds a 1x1 matrix */
00078                         inline CMatrixTemplateNumeric() : Base(1,1) { Base::setZero(); }
00079 
00080                         /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency is a priority.
00081                           *  Use as:
00082                           *   \code
00083                           *     CMatrixTemplateNumeric<double>  M( UNINITIALIZED_MATRIX);
00084                           *   \endcode
00085                           */
00086                         inline CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag) : Base( 0,0 ) { }
00087 
00088                         /** Constructor, creates a matrix of the given size, filled with zeros. */
00089                         inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row,col) { Base::setZero(); }
00090 
00091                         MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateNumeric) // Implements ctor and "operator =" for any other Eigen class
00092 
00093                         /** Assignment operator of other types
00094                         */
00095                         template <class R>
00096                         inline CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
00097                         {
00098                                 Base::resize( m.getRowCount(), m.getColCount() );
00099 
00100                                 for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
00101                                         for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
00102                                                 Base::coeffRef(i,j) = static_cast<T>(m.get_unsafe(i,j));
00103                                 return *this;
00104                         }
00105 
00106                         /** Constructor from a given size and a C array. The array length must match cols x row.
00107                           * \code
00108                           *  const double numbers[] = {
00109                           *    1,2,3,
00110                           *    4,5,6 };
00111                           *      CMatrixDouble   M(3,2, numbers);
00112                           * \endcode
00113                           */
00114                         template <typename V, size_t N>
00115                         inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : Base(row,col)
00116                         {
00117                                 ASSERT_EQUAL_(row*col,N)
00118                                 ASSERT_EQUAL_(sizeof(theArray[0]),sizeof(T))
00119                                 ::memcpy(Base::data(),&theArray[0],sizeof(T)*N); // Remember, row-major order!
00120                         }
00121 
00122                         /** Destructor
00123                           */
00124                         inline ~CMatrixTemplateNumeric() { }
00125 
00126                         /** == 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. */
00127                         template <typename Derived>
00128                         inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
00129                         {
00130                                 return Base::cols()==m2.cols() && 
00131                                            Base::rows()==m2.rows() && 
00132                                            Base::cwiseEqual(m2).all();
00133                         }
00134                         /** != 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. */
00135                         template <typename Derived>
00136                         inline bool operator !=(const Eigen::MatrixBase<Derived>& m2)  const{ return !((*this)==m2); }
00137 
00138                 }; // end of class definition
00139 
00140 
00141                 /** Declares a matrix of float numbers (non serializable).
00142                   *  For a serializable version, use math::CMatrix
00143                   *  \sa CMatrixDouble, CMatrix, CMatrixD
00144                   */
00145                 typedef CMatrixTemplateNumeric<float> CMatrixFloat;
00146 
00147                 /** Declares a matrix of double numbers (non serializable).
00148                   *  For a serializable version, use math::CMatrixD
00149                   *  \sa CMatrixFloat, CMatrix, CMatrixD
00150                   */
00151                 typedef CMatrixTemplateNumeric<double> CMatrixDouble;
00152 
00153                 /** Declares a matrix of unsigned ints (non serializable).
00154                   *  \sa CMatrixDouble, CMatrixFloat
00155                   */
00156                 typedef CMatrixTemplateNumeric<unsigned int> CMatrixUInt;
00157 
00158                 /** Declares a matrix of booleans (non serializable).
00159                   *  \sa CMatrixDouble, CMatrixFloat, CMatrixB
00160                   */
00161                 typedef CMatrixTemplate<bool> CMatrixBool;
00162 
00163 #ifdef HAVE_LONG_DOUBLE
00164                 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
00165                   *  \sa CMatrixDouble, CMatrixFloat
00166                   */
00167                 typedef CMatrixTemplateNumeric<long double> CMatrixLongDouble;
00168 #else
00169                 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
00170                   *  \sa CMatrixDouble, CMatrixFloat
00171                   */
00172                 typedef CMatrixTemplateNumeric<double> CMatrixLongDouble;
00173 #endif
00174 
00175 
00176                 namespace detail        
00177                 {
00178                         /**
00179                           * Vicinity traits class specialization for fixed size matrices.
00180                           */
00181                         template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> >   {
00182                         public:
00183                                 inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N)  {
00184                                         mat.setSize(N,N);
00185                                         mat.fill(0);
00186                                 }
00187                                 inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t)       {
00188                                         mat.get_unsafe(r,c)=t;
00189                                 }
00190                         };
00191                 }       //End of detail namespace.
00192 
00193         } // End of namespace
00194 
00195 
00196         namespace utils
00197         {
00198                 // Extensions to mrpt::utils::TTypeName for matrices:
00199                 template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
00200                         static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
00201                 };
00202         }
00203 
00204 } // End of namespace
00205 
00206 
00207 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011