Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
15#include <mrpt/math/point_poses2vectors.h> // MRPT_MATRIX_CONSTRUCTORS_FROM_POSES()
16
17namespace 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>
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 */
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
This template class provides the basic functionality for a general 2D any-size, resizable container o...
const T & get_unsafe(size_t row, size_t col) const
Fast but unsafe method to read a value from the matrix.
size_t getColCount() const
Number of columns in the matrix.
size_t getRowCount() const
Number of rows in the matrix.
CMatrixTemplateNumeric< T > mrpt_autotype
bool operator==(const Eigen::MatrixBase< Derived > &m2) const
== comparison of two matrices; it differs from default Eigen operator in that returns false if matric...
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign|Eigen::RowMajor > Base
bool operator!=(const Eigen::MatrixBase< Derived > &m2) const
!= comparison of two matrices; it differs from default Eigen operator in that returns true if matrice...
CMatrixTemplateNumeric(TConstructorFlags_Matrices)
Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency i...
CMatrixTemplateNumeric< T > & operator=(const CMatrixTemplate< R > &m)
Assignment operator of other types.
CMatrixTemplateNumeric(size_t row, size_t col, V(&theArray)[N])
Constructor from a given size and a C array.
CMatrixTemplateNumeric(size_t row, size_t col)
Constructor, creates a matrix of the given size, filled with zeros.
static void insertInContainer(CMatrixTemplateNumeric< T > &mat, size_t r, size_t c, const T &t)
static void initialize(CMatrixTemplateNumeric< T > &mat, size_t N)
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
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Definition math_frwds.h:88
#define ASSERT_EQUAL_(__A, __B)
CMatrixTemplateNumeric< unsigned int > CMatrixUInt
Declares a matrix of unsigned ints (non serializable).
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes),...
Definition math_frwds.h:74
CMatrixTemplateNumeric< double > CMatrixLongDouble
Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not supp...
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A template to obtain the type of its argument as a string at compile time.
Definition TTypeName.h:48
#define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_)
Definition types_math.h:47



Page generated by Doxygen 1.9.7 for MRPT 1.4.0 SVN: at Tue Jun 13 14:10:35 UTC 2023