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



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