Main MRPT website > C++ reference for MRPT 1.9.9
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>,
23  * but
24  * with a RowMajor element memory layout (except for column vectors).
25  *
26  * \note This class exists for backward compatibility of ancient times when
27  * MRPT didn't rely on Eigen, feel free to directly use Eigen::Matrix<> types
28  * instead.
29  *
30  * \sa CMatrixTemplate (a non Eigen lib-based class, which can hold arbitrary
31  * objects, not only numerical types).
32  * \note For a complete introduction to Matrices and vectors in MRPT, see:
33  * http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
34  * \ingroup mrpt_base_grp
35  */
36 template <class T>
38  : public Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic,
39  // Use row major storage for backward compatibility
40  // with MRPT matrices in all cases (even in column
41  // vectors!)
42  Eigen::AutoAlign | Eigen::RowMajor>
43 {
44  public:
45  typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic,
46  Eigen::AutoAlign | Eigen::RowMajor>
49 
51 
52  /** Default constructor, builds a 1x1 matrix */
53  inline CMatrixTemplateNumeric() : Base(1, 1) { Base::setZero(); }
54  /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage
55  * in places where efficiency is a priority.
56  * Use as:
57  * \code
58  * CMatrixTemplateNumeric<double> M( UNINITIALIZED_MATRIX);
59  * \endcode
60  */
62  /** Constructor, creates a matrix of the given size, filled with zeros. */
63  inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row, col)
64  {
65  Base::setZero();
66  }
67 
69  CMatrixTemplateNumeric) // Implements ctor and "operator =" for any
70  // other Eigen class
71 
72  /** Assignment operator of other types
73  */
74  template <class R>
75  inline CMatrixTemplateNumeric<T>& operator=(const CMatrixTemplate<R>& m)
76  {
77  Base::resize(m.getRowCount(), m.getColCount());
78 
79  for (size_t i = 0; i < CMatrixTemplate<T>::getRowCount(); i++)
80  for (size_t j = 0; j < CMatrixTemplate<T>::getColCount(); j++)
81  Base::coeffRef(i, j) = static_cast<T>(m.get_unsafe(i, j));
82  return *this;
83  }
84 
85  /** Assignment from any Eigen matrix/vector */
86  template <typename Derived>
87  inline CMatrixTemplateNumeric<T>& operator=(
88  const Eigen::MatrixBase<Derived>& m) const
89  {
90  Base::operator=(m);
91  return *this;
92  }
93 
94  /** Constructor from a given size and a C array. The array length must match
95  *cols x row.
96  * \code
97  * const double numbers[] = {
98  * 1,2,3,
99  * 4,5,6 };
100  * CMatrixDouble M(3,2, numbers);
101  * \endcode
102  */
103  template <typename V, size_t N>
104  inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N])
105  : Base(row, col)
106  {
107  ASSERT_EQUAL_(row * col, N)
108  ASSERT_EQUAL_(sizeof(theArray[0]), sizeof(T))
109  ::memcpy(
110  Base::data(), &theArray[0],
111  sizeof(T) * N); // Remember, row-major order!
112  }
113 
114  /** Destructor
115  */
116  inline ~CMatrixTemplateNumeric() {}
117  /** == comparison of two matrices; it differs from default Eigen operator in
118  * that returns false if matrices are of different sizes instead of raising
119  * an assert. */
120  template <typename Derived>
121  inline bool operator==(const Eigen::MatrixBase<Derived>& m2) const
122  {
123  return Base::cols() == m2.cols() && Base::rows() == m2.rows() &&
124  Base::cwiseEqual(m2).all();
125  }
126  /** != comparison of two matrices; it differs from default Eigen operator in
127  * that returns true if matrices are of different sizes instead of raising
128  * an assert. */
129  template <typename Derived>
130  inline bool operator!=(const Eigen::MatrixBase<Derived>& m2) const
131  {
132  return !((*this) == m2);
133  }
134 
135 }; // end of class definition
136 
137 /** Declares a matrix of float numbers (non serializable).
138  * For a serializable version, use math::CMatrix
139  * \sa CMatrixDouble, CMatrix, CMatrixD
140  */
142 
143 /** Declares a matrix of double numbers (non serializable).
144  * For a serializable version, use math::CMatrixD
145  * \sa CMatrixFloat, CMatrix, CMatrixD
146  */
148 
149 /** Declares a matrix of unsigned ints (non serializable).
150  * \sa CMatrixDouble, CMatrixFloat
151  */
153 
154 #ifdef HAVE_LONG_DOUBLE
155 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if
156  * the compiler does not support "long double".
157  * \sa CMatrixDouble, CMatrixFloat
158  */
160 #else
161 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if
162  * the compiler does not support "long double".
163  * \sa CMatrixDouble, CMatrixFloat
164  */
166 #endif
167 
168 namespace detail
169 {
170 /**
171  * Vicinity traits class specialization for fixed size matrices.
172  */
173 template <typename T>
175 {
176  public:
177  inline static void initialize(CMatrixTemplateNumeric<T>& mat, size_t N)
178  {
179  mat.setSize(N, N);
180  mat.fill(0);
181  }
182  inline static void insertInContainer(
183  CMatrixTemplateNumeric<T>& mat, size_t r, size_t c, const T& t)
184  {
185  mat.get_unsafe(r, c) = t;
186  }
187 };
188 } // End of detail namespace.
189 
190 } // End of namespace
191 
192 namespace utils
193 {
194 // Extensions to mrpt::utils::TTypeName for matrices:
195 template <typename T>
197 {
198  static std::string get()
199  {
200  return std::string("CMatrixTemplateNumeric<") +
202  }
203 };
204 }
205 
206 } // End of namespace
207 
208 #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.
bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
GLdouble GLdouble t
Definition: glext.h:3689
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes)...
Definition: math_frwds.h:84
#define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_)
Definition: types_math.h:46
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:55
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign|Eigen::RowMajor > Base
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:59
The purpose of this class is to model traits for containers, so that they can be used as return value...
Definition: math_frwds.h:210
const GLubyte * c
Definition: glext.h:6313
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Definition: math_frwds.h:106
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
GLsizei const GLchar ** string
Definition: glext.h:4101
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:3705
GLenum GLenum GLvoid * row
Definition: glext.h:3576
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:57
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:3546
static void initialize(CMatrixTemplateNumeric< T > &mat, size_t N)
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:355



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019