Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 
12 #include <mrpt/utils/core_defs.h>
13 #include <mrpt/utils/types_math.h>
14 #include <mrpt/utils/TTypeName.h>
15 #include <mrpt/math/point_poses2vectors.h> // MRPT_MATRIX_CONSTRUCTORS_FROM_POSES()
16 
17 namespace mrpt
18 {
19  namespace math
20  {
21  /** A matrix of dynamic size.
22  * Basically, this class is a wrapper on Eigen::Matrix<T,Dynamic,Dynamic>, but
23  * with a RowMajor element memory layout (except for column vectors).
24  *
25  * \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.
26  *
27  * \sa CMatrixTemplate (a non Eigen lib-based class, which can hold arbitrary objects, not only numerical types).
28  * \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
29  * \ingroup mrpt_base_grp
30  */
31  template <class T>
33  public Eigen::Matrix<
34  T,
35  Eigen::Dynamic,
36  Eigen::Dynamic,
37  // Use row major storage for backward compatibility with MRPT matrices in all cases (even in column vectors!)
38  Eigen::AutoAlign | Eigen::RowMajor
39  >
40  {
41  public:
42  typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::AutoAlign|Eigen::RowMajor> Base;
44 
46 
47  /** Default constructor, builds a 1x1 matrix */
48  inline CMatrixTemplateNumeric() : Base(1,1) { Base::setZero(); }
49 
50  /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency is a priority.
51  * Use as:
52  * \code
53  * CMatrixTemplateNumeric<double> M( UNINITIALIZED_MATRIX);
54  * \endcode
55  */
57 
58  /** Constructor, creates a matrix of the given size, filled with zeros. */
59  inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row,col) { Base::setZero(); }
60 
61  MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateNumeric) // Implements ctor and "operator =" for any other Eigen class
62 
63  /** Assignment operator of other types
64  */
65  template <class R>
66  inline CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
67  {
68  Base::resize( m.getRowCount(), m.getColCount() );
69 
70  for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
71  for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
72  Base::coeffRef(i,j) = static_cast<T>(m.get_unsafe(i,j));
73  return *this;
74  }
75 
76  /** Assignment from any Eigen matrix/vector */
77  template <typename Derived>
78  inline CMatrixTemplateNumeric<T>& operator =(const Eigen::MatrixBase<Derived>& m) const
79  {
80  Base::operator =(m);
81  return *this;
82  }
83 
84 
85  /** Constructor from a given size and a C array. The array length must match cols x row.
86  * \code
87  * const double numbers[] = {
88  * 1,2,3,
89  * 4,5,6 };
90  * CMatrixDouble M(3,2, numbers);
91  * \endcode
92  */
93  template <typename V, size_t N>
94  inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : Base(row,col)
95  {
96  ASSERT_EQUAL_(row*col,N)
97  ASSERT_EQUAL_(sizeof(theArray[0]),sizeof(T))
98  ::memcpy(Base::data(),&theArray[0],sizeof(T)*N); // Remember, row-major order!
99  }
100 
101  /** Destructor
102  */
103  inline ~CMatrixTemplateNumeric() { }
104 
105  /** == 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. */
106  template <typename Derived>
107  inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
108  {
109  return Base::cols()==m2.cols() &&
110  Base::rows()==m2.rows() &&
111  Base::cwiseEqual(m2).all();
112  }
113  /** != 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. */
114  template <typename Derived>
115  inline bool operator !=(const Eigen::MatrixBase<Derived>& m2) const{ return !((*this)==m2); }
116 
117  }; // end of class definition
118 
119 
120  /** Declares a matrix of float numbers (non serializable).
121  * For a serializable version, use math::CMatrix
122  * \sa CMatrixDouble, CMatrix, CMatrixD
123  */
125 
126  /** Declares a matrix of double numbers (non serializable).
127  * For a serializable version, use math::CMatrixD
128  * \sa CMatrixFloat, CMatrix, CMatrixD
129  */
131 
132  /** Declares a matrix of unsigned ints (non serializable).
133  * \sa CMatrixDouble, CMatrixFloat
134  */
136 
137 #ifdef HAVE_LONG_DOUBLE
138  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
139  * \sa CMatrixDouble, CMatrixFloat
140  */
142 #else
143  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
144  * \sa CMatrixDouble, CMatrixFloat
145  */
147 #endif
148 
149 
150  namespace detail
151  {
152  /**
153  * Vicinity traits class specialization for fixed size matrices.
154  */
155  template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> > {
156  public:
157  inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N) {
158  mat.setSize(N,N);
159  mat.fill(0);
160  }
161  inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t) {
162  mat.get_unsafe(r,c)=t;
163  }
164  };
165  } //End of detail namespace.
166 
167  } // End of namespace
168 
169 
170  namespace utils
171  {
172  // Extensions to mrpt::utils::TTypeName for matrices:
173  template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
174  static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
175  };
176  }
177 
178 } // End of namespace
179 
180 
181 #endif
#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.
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".
Definition: os.cpp:358
bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
GLdouble GLdouble t
Definition: glext.h:3610
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes)...
Definition: math_frwds.h:72
#define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_)
Definition: types_math.h:47
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:47
bool operator!=(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
static void insertInContainer(CMatrixTemplateNumeric< T > &mat, size_t r, size_t c, const T &t)
CMatrixTemplateNumeric< T > mrpt_autotype
EIGEN_STRONG_INLINE size_t getColCount() const
Get number of columns.
Definition: eigen_plugins.h:48
The purpose of this class is to model traits for containers, so that they can be used as return value...
Definition: math_frwds.h:141
const GLubyte * c
Definition: glext.h:5590
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Definition: math_frwds.h:88
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign|Eigen::RowMajor > Base
GLsizei const GLchar ** string
Definition: glext.h:3919
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).
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
GLenum GLenum GLvoid * row
Definition: glext.h:3533
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...
EIGEN_STRONG_INLINE size_t getRowCount() const
Get number of rows.
Definition: eigen_plugins.h:46
CMatrixTemplateNumeric(TConstructorFlags_Matrices)
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.
CMatrixTemplateNumeric()
Default constructor, builds a 1x1 matrix.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
static void initialize(CMatrixTemplateNumeric< T > &mat, size_t N)



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019