Main MRPT website > C++ reference
MRPT logo
CMatrixFixedNumeric.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CMatrixFixedNumeric_H
29 #define CMatrixFixedNumeric_H
30 
31 #include <mrpt/math/CArray.h>
32 #include <mrpt/math/math_frwds.h> // Fordward declarations
34 
35 namespace mrpt
36 {
37  namespace math
38  {
39  using namespace mrpt::system;
40  using namespace mrpt::poses;
41 
42  /** A numeric matrix of compile-time fixed size.
43  * Basically, this class is a wrapper on Eigen::Matrix<T,NROWS,NCOLS>, but
44  * with a RowMajor element memory layout (except for column vectors).
45  *
46  * 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.
47  *
48  * \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.
49  * \sa CMatrixTemplateNumeric (for dynamic-size matrices)
50  * \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
51  * \ingroup mrpt_base_grp
52  */
53  template <typename T,size_t NROWS,size_t NCOLS>
55  public Eigen::Matrix<
56  T,
57  NROWS,
58  NCOLS,
59  // Use row major storage for backward compatibility with MRPT matrices in all cases, except in column vectors:
60  Eigen::AutoAlign |
61  ( (NCOLS==1 && NROWS!=1) ? Eigen::ColMajor : Eigen::RowMajor )
62  >
63  {
64  public:
65  typedef Eigen::Matrix<T,NROWS,NCOLS, Eigen::AutoAlign | ( (NCOLS==1 && NROWS!=1) ? Eigen::ColMajor : Eigen::RowMajor ) > Base;
67 
68  MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixFixedNumeric) // Implements ctor and "operator =" for any other Eigen class
70 
71  /** Default constructor, initializes all elements to zero */
72  inline CMatrixFixedNumeric() : Base() { Base::setZero(); }
73 
74  /** Constructor from an array in row major */
75  inline CMatrixFixedNumeric(const T * vals) : Base(vals) { }
76 
77  /** Constructor which leaves the matrix uninitialized.
78  * Example of usage: CMatrixFixedNumeric<double,3,2> M(UNINITIALIZED_MATRIX);
79  */
80  inline CMatrixFixedNumeric(TConstructorFlags_Matrices constructor_flag) : Base() { }
81 
82  template<size_t N,typename ReturnType> inline ReturnType getVicinity(size_t c,size_t r) const {
83  return detail::getVicinity<CMatrixFixedNumeric<T,NROWS,NCOLS>,T,ReturnType,N>::get(c,r,*this);
84  }
85 
86  inline void loadFromArray(const T* vals)
87  {
88  Base b(vals);
89  *this = b;
90  }
91 
92  /** == 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. */
93  template <typename Derived>
94  inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
95  {
96  return Base::cols()==m2.cols() &&
97  Base::rows()==m2.rows() &&
98  Base::cwiseEqual(m2).all();
99  }
100  /** != 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. */
101  template <typename Derived>
102  inline bool operator !=(const Eigen::MatrixBase<Derived>& m2) const { return !((*this)==m2); }
103 
104 
105  }; // end of class definition ------------------------------
106 
107  /** @name Typedefs for common sizes
108  @{ */
126 
144  /** @} */
145 
146 
147  namespace detail
148  {
149  /**
150  * Vicinity traits class specialization for fixed size matrices.
151  */
152  template<typename T,size_t D> class VicinityTraits<CMatrixFixedNumeric<T,D,D> > {
153  public:
154  inline static void initialize(CMatrixFixedNumeric<T,D,D> &mat,size_t N) {
155  ASSERT_(N==D);
156  }
157  inline static void insertInContainer(CMatrixFixedNumeric<T,D,D> &mat,size_t r,size_t c,const T &t) {
158  mat.get_unsafe(r,c)=t;
159  }
160  };
161  } //End of detail namespace.
162 
163 
164  } // End of namespace
165 
166  namespace utils
167  {
168  // Extensions to mrpt::utils::TTypeName for matrices:
169  template<typename T,size_t N,size_t M> struct TTypeName <mrpt::math::CMatrixFixedNumeric<T,N,M> > {
170  static std::string get() { return mrpt::format("CMatrixFixedNumeric<%s,%u,%u>",TTypeName<T>::get().c_str(),(unsigned int)N,(unsigned int)M); }
171  };
172  }
173 
174 } // End of namespace
175 
176 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013