Main MRPT website > C++ reference
MRPT logo
CMatrixTemplateNumeric.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CMatrixTemplateNumeric_H
10 #define CMatrixTemplateNumeric_H
11 
14 
15 #include <mrpt/system/os.h>
16 #include <cmath>
17 #include <limits>
18 
19 namespace mrpt
20 {
21  namespace poses
22  {
23  class CPose2D;
24  class CPose3D;
25  class CPoint2D;
26  class CPoint3D;
27  }
28 
29  namespace math
30  {
31  using namespace mrpt::system;
32 
33  /** A matrix of dynamic size.
34  * Basically, this class is a wrapper on Eigen::Matrix<T,Dynamic,Dynamic>, but
35  * with a RowMajor element memory layout (except for column vectors).
36  *
37  * \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.
38  *
39  * \sa CMatrixTemplate (a non Eigen lib-based class, which can hold arbitrary objects, not only numerical types).
40  * \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
41  * \ingroup mrpt_base_grp
42  */
43  template <class T>
45  public Eigen::Matrix<
46  T,
47  Eigen::Dynamic,
48  Eigen::Dynamic,
49  // Use row major storage for backward compatibility with MRPT matrices in all cases (even in column vectors!)
50  Eigen::AutoAlign | Eigen::RowMajor
51  >
52  {
53  public:
54  typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::AutoAlign|Eigen::RowMajor> Base;
56 
58 
59  /** Default constructor, builds a 1x1 matrix */
60  inline CMatrixTemplateNumeric() : Base(1,1) { Base::setZero(); }
61 
62  /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency is a priority.
63  * Use as:
64  * \code
65  * CMatrixTemplateNumeric<double> M( UNINITIALIZED_MATRIX);
66  * \endcode
67  */
68  inline CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag) : Base( 0,0 ) { }
69 
70  /** Constructor, creates a matrix of the given size, filled with zeros. */
71  inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row,col) { Base::setZero(); }
72 
73  MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateNumeric) // Implements ctor and "operator =" for any other Eigen class
74 
75  /** Assignment operator of other types
76  */
77  template <class R>
78  inline CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
79  {
80  Base::resize( m.getRowCount(), m.getColCount() );
81 
82  for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
83  for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
84  Base::coeffRef(i,j) = static_cast<T>(m.get_unsafe(i,j));
85  return *this;
86  }
87 
88  /** Constructor from a given size and a C array. The array length must match cols x row.
89  * \code
90  * const double numbers[] = {
91  * 1,2,3,
92  * 4,5,6 };
93  * CMatrixDouble M(3,2, numbers);
94  * \endcode
95  */
96  template <typename V, size_t N>
97  inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : Base(row,col)
98  {
99  ASSERT_EQUAL_(row*col,N)
100  ASSERT_EQUAL_(sizeof(theArray[0]),sizeof(T))
101  ::memcpy(Base::data(),&theArray[0],sizeof(T)*N); // Remember, row-major order!
102  }
103 
104  /** Destructor
105  */
106  inline ~CMatrixTemplateNumeric() { }
107 
108  /** == 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. */
109  template <typename Derived>
110  inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
111  {
112  return Base::cols()==m2.cols() &&
113  Base::rows()==m2.rows() &&
114  Base::cwiseEqual(m2).all();
115  }
116  /** != 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. */
117  template <typename Derived>
118  inline bool operator !=(const Eigen::MatrixBase<Derived>& m2) const{ return !((*this)==m2); }
119 
120  }; // end of class definition
121 
122 
123  /** Declares a matrix of float numbers (non serializable).
124  * For a serializable version, use math::CMatrix
125  * \sa CMatrixDouble, CMatrix, CMatrixD
126  */
128 
129  /** Declares a matrix of double numbers (non serializable).
130  * For a serializable version, use math::CMatrixD
131  * \sa CMatrixFloat, CMatrix, CMatrixD
132  */
134 
135  /** Declares a matrix of unsigned ints (non serializable).
136  * \sa CMatrixDouble, CMatrixFloat
137  */
139 
140  /** Declares a matrix of booleans (non serializable).
141  * \sa CMatrixDouble, CMatrixFloat, CMatrixB
142  */
144 
145 #ifdef HAVE_LONG_DOUBLE
146  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
147  * \sa CMatrixDouble, CMatrixFloat
148  */
150 #else
151  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
152  * \sa CMatrixDouble, CMatrixFloat
153  */
155 #endif
156 
157 
158  namespace detail
159  {
160  /**
161  * Vicinity traits class specialization for fixed size matrices.
162  */
163  template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> > {
164  public:
165  inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N) {
166  mat.setSize(N,N);
167  mat.fill(0);
168  }
169  inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t) {
170  mat.get_unsafe(r,c)=t;
171  }
172  };
173  } //End of detail namespace.
174 
175  } // End of namespace
176 
177 
178  namespace utils
179  {
180  // Extensions to mrpt::utils::TTypeName for matrices:
181  template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
182  static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
183  };
184  }
185 
186 } // End of namespace
187 
188 
189 #endif
bool operator!=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:280
#define ASSERT_EQUAL_(__A, __B)
const T & get_unsafe(size_t row, size_t col) const
Fast but unsafe method to read a value from the matrix.
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes)...
Definition: math_frwds.h:81
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:32
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:46
static void insertInContainer(CMatrixTemplateNumeric< T > &mat, size_t r, size_t c, const T &t)
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
#define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_)
CMatrixTemplateNumeric< T > mrpt_autotype
EIGEN_STRONG_INLINE size_t getColCount() const
Get number of columns.
Definition: eigen_plugins.h:48
CMatrixTemplate< bool > CMatrixBool
Declares a matrix of booleans (non serializable).
The purpose of this class is to model traits for containers, so that they can be used as return value...
Definition: math_frwds.h:153
bool operator==(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:272
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign|Eigen::RowMajor > Base
A class used to store a 2D point.
Definition: CPoint2D.h:35
A class used to store a 3D point.
Definition: CPoint3D.h:33
This template class provides the basic functionality for a general 2D any-size, resizable container o...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CMatrixTemplateNumeric< unsigned int > CMatrixUInt
Declares a matrix of unsigned ints (non serializable).
A class used to store a 2D pose.
Definition: CPose2D.h:35
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:71
size_t getColCount() const
Number of columns in the matrix.
size_t getRowCount() const
Number of rows in the matrix.
CMatrixTemplateNumeric< double > CMatrixLongDouble
Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not supp...
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
EIGEN_STRONG_INLINE size_t getRowCount() const
Get number of rows.
Definition: eigen_plugins.h:46
CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag)
Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency i...
CMatrixTemplateNumeric(size_t row, size_t col)
Constructor, creates a matrix of the given size, filled with zeros.
static void initialize(CMatrixTemplateNumeric< T > &mat, size_t N)
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).



Page generated by Doxygen 1.8.14 for MRPT 1.1.0 SVN: at lun oct 28 00:54:49 CET 2019 Hosted on:
SourceForge.net Logo