Main MRPT website > C++ reference
MRPT logo
CMatrixTemplateNumeric.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 CMatrixTemplateNumeric_H
29 #define CMatrixTemplateNumeric_H
30 
33 
34 #include <mrpt/system/os.h>
35 #include <cmath>
36 #include <limits>
37 
38 namespace mrpt
39 {
40  namespace poses
41  {
42  class CPose2D;
43  class CPose3D;
44  class CPoint2D;
45  class CPoint3D;
46  }
47 
48  namespace math
49  {
50  using namespace mrpt::system;
51 
52  /** A matrix of dynamic size.
53  * Basically, this class is a wrapper on Eigen::Matrix<T,Dynamic,Dynamic>, but
54  * with a RowMajor element memory layout (except for column vectors).
55  *
56  * \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.
57  *
58  * \sa CMatrixTemplate (a non Eigen lib-based class, which can hold arbitrary objects, not only numerical types).
59  * \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
60  * \ingroup mrpt_base_grp
61  */
62  template <class T>
64  public Eigen::Matrix<
65  T,
66  Eigen::Dynamic,
67  Eigen::Dynamic,
68  // Use row major storage for backward compatibility with MRPT matrices in all cases (even in column vectors!)
69  Eigen::AutoAlign | Eigen::RowMajor
70  >
71  {
72  public:
73  typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::AutoAlign|Eigen::RowMajor> Base;
75 
77 
78  /** Default constructor, builds a 1x1 matrix */
79  inline CMatrixTemplateNumeric() : Base(1,1) { Base::setZero(); }
80 
81  /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency is a priority.
82  * Use as:
83  * \code
84  * CMatrixTemplateNumeric<double> M( UNINITIALIZED_MATRIX);
85  * \endcode
86  */
87  inline CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag) : Base( 0,0 ) { }
88 
89  /** Constructor, creates a matrix of the given size, filled with zeros. */
90  inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row,col) { Base::setZero(); }
91 
92  MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateNumeric) // Implements ctor and "operator =" for any other Eigen class
93 
94  /** Assignment operator of other types
95  */
96  template <class R>
97  inline CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
98  {
99  Base::resize( m.getRowCount(), m.getColCount() );
100 
101  for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
102  for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
103  Base::coeffRef(i,j) = static_cast<T>(m.get_unsafe(i,j));
104  return *this;
105  }
106 
107  /** Constructor from a given size and a C array. The array length must match cols x row.
108  * \code
109  * const double numbers[] = {
110  * 1,2,3,
111  * 4,5,6 };
112  * CMatrixDouble M(3,2, numbers);
113  * \endcode
114  */
115  template <typename V, size_t N>
116  inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : Base(row,col)
117  {
118  ASSERT_EQUAL_(row*col,N)
119  ASSERT_EQUAL_(sizeof(theArray[0]),sizeof(T))
120  ::memcpy(Base::data(),&theArray[0],sizeof(T)*N); // Remember, row-major order!
121  }
122 
123  /** Destructor
124  */
125  inline ~CMatrixTemplateNumeric() { }
126 
127  /** == 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. */
128  template <typename Derived>
129  inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
130  {
131  return Base::cols()==m2.cols() &&
132  Base::rows()==m2.rows() &&
133  Base::cwiseEqual(m2).all();
134  }
135  /** != 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. */
136  template <typename Derived>
137  inline bool operator !=(const Eigen::MatrixBase<Derived>& m2) const{ return !((*this)==m2); }
138 
139  }; // end of class definition
140 
141 
142  /** Declares a matrix of float numbers (non serializable).
143  * For a serializable version, use math::CMatrix
144  * \sa CMatrixDouble, CMatrix, CMatrixD
145  */
147 
148  /** Declares a matrix of double numbers (non serializable).
149  * For a serializable version, use math::CMatrixD
150  * \sa CMatrixFloat, CMatrix, CMatrixD
151  */
153 
154  /** Declares a matrix of unsigned ints (non serializable).
155  * \sa CMatrixDouble, CMatrixFloat
156  */
158 
159  /** Declares a matrix of booleans (non serializable).
160  * \sa CMatrixDouble, CMatrixFloat, CMatrixB
161  */
163 
164 #ifdef HAVE_LONG_DOUBLE
165  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
166  * \sa CMatrixDouble, CMatrixFloat
167  */
169 #else
170  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
171  * \sa CMatrixDouble, CMatrixFloat
172  */
174 #endif
175 
176 
177  namespace detail
178  {
179  /**
180  * Vicinity traits class specialization for fixed size matrices.
181  */
182  template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> > {
183  public:
184  inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N) {
185  mat.setSize(N,N);
186  mat.fill(0);
187  }
188  inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t) {
189  mat.get_unsafe(r,c)=t;
190  }
191  };
192  } //End of detail namespace.
193 
194  } // End of namespace
195 
196 
197  namespace utils
198  {
199  // Extensions to mrpt::utils::TTypeName for matrices:
200  template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
201  static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
202  };
203  }
204 
205 } // End of namespace
206 
207 
208 #endif



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