Main MRPT website > C++ reference
MRPT logo

CMatrixFixedNumeric.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 CMatrixFixedNumeric_H
00029 #define CMatrixFixedNumeric_H
00030 
00031 #include <mrpt/math/CArray.h>
00032 #include <mrpt/math/math_frwds.h>  // Fordward declarations
00033 #include <mrpt/utils/CSerializable.h>
00034 
00035 namespace mrpt
00036 {
00037         namespace math
00038         {
00039                 using namespace mrpt::system;
00040                 using namespace mrpt::poses;
00041 
00042                 /**  A numeric matrix of compile-time fixed size.
00043                  *   Basically, this class is a wrapper on Eigen::Matrix<T,NROWS,NCOLS>, but
00044                  *   with a RowMajor element memory layout (except for column vectors).
00045                  *
00046                  *  These matrices also have iterators to access all the elements in the matrix as a sequence, starting from the element (0,0), then row by row, from left to right.
00047                  *
00048                  * \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.
00049                  * \sa CMatrixTemplateNumeric (for dynamic-size matrices)
00050                  */
00051                 template <typename T,size_t NROWS,size_t NCOLS>
00052                 class CMatrixFixedNumeric  : 
00053                         public Eigen::Matrix<
00054                                 T,
00055                                 NROWS,
00056                                 NCOLS,
00057                                 // Use row major storage for backward compatibility with MRPT matrices in all cases, except in column vectors:
00058                                 Eigen::AutoAlign |
00059                                 ( (NCOLS==1 && NROWS!=1) ? Eigen::ColMajor : Eigen::RowMajor ) 
00060                                 >
00061                 {
00062                 public:
00063                         typedef Eigen::Matrix<T,NROWS,NCOLS, Eigen::AutoAlign | ( (NCOLS==1 && NROWS!=1) ? Eigen::ColMajor : Eigen::RowMajor ) > Base;
00064                         typedef CMatrixFixedNumeric<T,NROWS,NCOLS> mrpt_autotype;
00065 
00066                         MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixFixedNumeric) // Implements ctor and "operator =" for any other Eigen class
00067                         MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(CMatrixFixedNumeric)
00068 
00069                         /** Default constructor, initializes all elements to zero */
00070                         inline CMatrixFixedNumeric() : Base() { Base::setZero(); }
00071 
00072                         /** Constructor from an array in row major */
00073                         inline CMatrixFixedNumeric(const T * vals) : Base(vals) { }
00074 
00075                         /** Constructor which leaves the matrix uninitialized.
00076                           *  Example of usage: CMatrixFixedNumeric<double,3,2> M(UNINITIALIZED_MATRIX);
00077                           */
00078                         inline CMatrixFixedNumeric(TConstructorFlags_Matrices constructor_flag) : Base() { }
00079 
00080                         template<size_t N,typename ReturnType> inline ReturnType getVicinity(size_t c,size_t r) const   {
00081                                 return detail::getVicinity<CMatrixFixedNumeric<T,NROWS,NCOLS>,T,ReturnType,N>::get(c,r,*this);
00082                         }
00083 
00084                         inline void loadFromArray(const T* vals) 
00085                         {
00086                                 Base b(vals);
00087                                 *this = b;
00088                         }
00089 
00090                         /** == 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. */
00091                         template <typename Derived>
00092                         inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
00093                         {
00094                                 return Base::cols()==m2.cols() && 
00095                                            Base::rows()==m2.rows() && 
00096                                            Base::cwiseEqual(m2).all();
00097                         }
00098                         /** != 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. */
00099                         template <typename Derived>
00100                         inline bool operator !=(const Eigen::MatrixBase<Derived>& m2) const { return !((*this)==m2); }
00101 
00102 
00103                 }; // end of class definition ------------------------------
00104 
00105                 /** @name Typedefs for common sizes
00106                         @{ */
00107                 typedef CMatrixFixedNumeric<double,2,2> CMatrixDouble22;
00108                 typedef CMatrixFixedNumeric<double,2,3> CMatrixDouble23;
00109                 typedef CMatrixFixedNumeric<double,3,2> CMatrixDouble32;
00110                 typedef CMatrixFixedNumeric<double,3,3> CMatrixDouble33;
00111                 typedef CMatrixFixedNumeric<double,4,4> CMatrixDouble44;
00112                 typedef CMatrixFixedNumeric<double,6,6> CMatrixDouble66;
00113                 typedef CMatrixFixedNumeric<double,7,7> CMatrixDouble77;
00114                 typedef CMatrixFixedNumeric<double,1,3> CMatrixDouble13;
00115                 typedef CMatrixFixedNumeric<double,3,1> CMatrixDouble31;
00116                 typedef CMatrixFixedNumeric<double,1,2> CMatrixDouble12;
00117                 typedef CMatrixFixedNumeric<double,2,1> CMatrixDouble21;
00118                 typedef CMatrixFixedNumeric<double,6,1> CMatrixDouble61;
00119                 typedef CMatrixFixedNumeric<double,1,6> CMatrixDouble16;
00120                 typedef CMatrixFixedNumeric<double,7,1> CMatrixDouble71;
00121                 typedef CMatrixFixedNumeric<double,1,7> CMatrixDouble17;
00122                 typedef CMatrixFixedNumeric<double,5,1> CMatrixDouble51;
00123                 typedef CMatrixFixedNumeric<double,1,5> CMatrixDouble15;
00124 
00125                 typedef CMatrixFixedNumeric<float,2,2> CMatrixFloat22;
00126                 typedef CMatrixFixedNumeric<float,2,3> CMatrixFloat23;
00127                 typedef CMatrixFixedNumeric<float,3,2> CMatrixFloat32;
00128                 typedef CMatrixFixedNumeric<float,3,3> CMatrixFloat33;
00129                 typedef CMatrixFixedNumeric<float,4,4> CMatrixFloat44;
00130                 typedef CMatrixFixedNumeric<float,6,6> CMatrixFloat66;
00131                 typedef CMatrixFixedNumeric<float,7,7> CMatrixFloat77;
00132                 typedef CMatrixFixedNumeric<float,1,3> CMatrixFloat13;
00133                 typedef CMatrixFixedNumeric<float,3,1> CMatrixFloat31;
00134                 typedef CMatrixFixedNumeric<float,1,2> CMatrixFloat12;
00135                 typedef CMatrixFixedNumeric<float,2,1> CMatrixFloat21;
00136                 typedef CMatrixFixedNumeric<float,6,1> CMatrixFloat61;
00137                 typedef CMatrixFixedNumeric<float,1,6> CMatrixFloat16;
00138                 typedef CMatrixFixedNumeric<float,7,1> CMatrixFloat71;
00139                 typedef CMatrixFixedNumeric<float,1,7> CMatrixFloat17;
00140                 typedef CMatrixFixedNumeric<float,5,1> CMatrixFloat51;
00141                 typedef CMatrixFixedNumeric<float,1,5> CMatrixFloat15;
00142                 /**  @} */
00143 
00144 
00145                 namespace detail        
00146                 {
00147                         /**
00148                           * Vicinity traits class specialization for fixed size matrices.
00149                           */
00150                         template<typename T,size_t D> class VicinityTraits<CMatrixFixedNumeric<T,D,D> > {
00151                         public:
00152                                 inline static void initialize(CMatrixFixedNumeric<T,D,D> &mat,size_t N) {
00153                                         ASSERT_(N==D);
00154                                 }
00155                                 inline static void insertInContainer(CMatrixFixedNumeric<T,D,D> &mat,size_t r,size_t c,const T &t)      {
00156                                         mat.get_unsafe(r,c)=t;
00157                                 }
00158                         };
00159                 }       //End of detail namespace.
00160 
00161 
00162         } // End of namespace
00163 
00164         namespace utils
00165         {
00166                 // Extensions to mrpt::utils::TTypeName for matrices:
00167                 template<typename T,size_t N,size_t M> struct TTypeName <mrpt::math::CMatrixFixedNumeric<T,N,M> > {
00168                         static std::string get() { return mrpt::format("CMatrixFixedNumeric<%s,%u,%u>",TTypeName<T>::get().c_str(),(unsigned int)N,(unsigned int)M); }
00169                 };
00170         }
00171 
00172 } // End of namespace
00173 
00174 #endif



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