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-2018, 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 #pragma once
10 
11 #include <mrpt/math/types_math.h>
12 #include <Eigen/Dense>
14 #include <mrpt/math/point_poses2vectors.h> // MRPT_MATRIX_CONSTRUCTORS_FROM_POSES()
15 #include <mrpt/core/exceptions.h>
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_math_grp
35  */
36 template <class T>
37 class CMatrixTemplateNumeric : public Eigen::Matrix<
38  T, Eigen::Dynamic, Eigen::Dynamic,
39  // Use row major storage for backward
40  // compatibility with MRPT matrices in all
41  // cases (even in column vectors!)
42  Eigen::AutoAlign | Eigen::RowMajor>
43 {
44  public:
45  using Base = Eigen::Matrix<
46  T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign | Eigen::RowMajor>;
48 
49  /** Default constructor, builds a 1x1 matrix */
50  inline CMatrixTemplateNumeric() : Base(1, 1) { Base::setZero(); }
51  /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage
52  * in places where efficiency is a priority.
53  * Use as:
54  * \code
55  * CMatrixTemplateNumeric<double> M( UNINITIALIZED_MATRIX);
56  * \endcode
57  */
59  /** Constructor, creates a matrix of the given size, filled with zeros. */
60  inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row, col)
61  {
62  Base::setZero();
63  }
66  CMatrixTemplateNumeric) // Implements ctor and "operator =" for any
67  // other Eigen class
68 
69  /** Assignment operator of other types
70  */
71  template <class R>
72  inline CMatrixTemplateNumeric<T>& operator=(const CMatrixTemplate<R>& m)
73  {
74  Base::resize(m.rows(), m.cols());
75 
76  for (size_t i = 0; i < CMatrixTemplate<T>::rows(); i++)
77  for (size_t j = 0; j < CMatrixTemplate<T>::cols(); j++)
78  Base::coeffRef(i, j) = static_cast<T>(m.get_unsafe(i, j));
79  return *this;
80  }
81 
82  /** Assignment from any Eigen matrix/vector */
83  template <typename Derived>
84  inline CMatrixTemplateNumeric<T>& operator=(
85  const Eigen::MatrixBase<Derived>& m) const
86  {
87  Base::operator=(m);
88  return *this;
89  }
90 
91  /** Constructor from a given size and a C array. The array length must match
92  *cols x row.
93  * \code
94  * const double numbers[] = {
95  * 1,2,3,
96  * 4,5,6 };
97  * CMatrixDouble M(3,2, numbers);
98  * \endcode
99  */
100  template <typename V, size_t N>
101  inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N])
102  : Base(row, col)
103  {
104  ASSERT_EQUAL_(row * col, N);
105  ASSERT_EQUAL_(sizeof(theArray[0]), sizeof(T));
106  ::memcpy(
107  Base::data(), &theArray[0],
108  sizeof(T) * N); // Remember, row-major order!
109  }
110 
111  /** Destructor
112  */
113  inline ~CMatrixTemplateNumeric() {}
114  /** == comparison of two matrices; it differs from default Eigen operator in
115  * that returns false if matrices are of different sizes instead of raising
116  * an assert. */
117  template <typename Derived>
118  inline bool operator==(const Eigen::MatrixBase<Derived>& m2) const
119  {
120  return Base::cols() == m2.cols() && Base::rows() == m2.rows() &&
121  Base::cwiseEqual(m2).all();
122  }
123  /** != comparison of two matrices; it differs from default Eigen operator in
124  * that returns true if matrices are of different sizes instead of raising
125  * an assert. */
126  template <typename Derived>
127  inline bool operator!=(const Eigen::MatrixBase<Derived>& m2) const
128  {
129  return !((*this) == m2);
130  }
131 
132 }; // end of class definition
133 
134 /** Declares a matrix of float numbers (non serializable).
135  * For a serializable version, use math::CMatrix
136  * \sa CMatrixDouble, CMatrix, CMatrixD
137  */
139 
140 /** Declares a matrix of double numbers (non serializable).
141  * For a serializable version, use math::CMatrixD
142  * \sa CMatrixFloat, CMatrix, CMatrixD
143  */
145 
146 /** Declares a matrix of unsigned ints (non serializable).
147  * \sa CMatrixDouble, CMatrixFloat
148  */
150 
151 #ifdef HAVE_LONG_DOUBLE
152 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if
153  * the compiler does not support "long double".
154  * \sa CMatrixDouble, CMatrixFloat
155  */
157 #else
158 /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if
159  * the compiler does not support "long double".
160  * \sa CMatrixDouble, CMatrixFloat
161  */
163 #endif
164 
165 namespace detail
166 {
167 /**
168  * Vicinity traits class specialization for fixed size matrices.
169  */
170 template <typename T>
172 {
173  public:
174  inline static void initialize(CMatrixTemplateNumeric<T>& mat, size_t N)
175  {
176  mat.setSize(N, N);
177  mat.fill(0);
178  }
179  inline static void insertInContainer(
180  CMatrixTemplateNumeric<T>& mat, size_t r, size_t c, const T& t)
181  {
182  mat.get_unsafe(r, c) = t;
183  }
184 };
185 } // namespace detail
186 } // namespace math
187 
188 namespace typemeta
189 {
190 // Extensions to mrpt::typemeta::TTypeName for matrices:
191 template <typename T>
193 {
194  static auto get()
195  {
196  return literal("CMatrixTemplateNumeric<") + TTypeName<T>::get() +
197  literal(">");
198  }
199 };
200 } // namespace typemeta
201 
202 } // namespace mrpt
mrpt::math::operator!=
constexpr bool operator!=(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
Definition: lightweight_geom_data.h:887
exceptions.h
t
GLdouble GLdouble t
Definition: glext.h:3689
ASSERT_EQUAL_
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:153
point_poses2vectors.h
c
const GLubyte * c
Definition: glext.h:6313
mrpt::math::CMatrixTemplate::get_unsafe
const T & get_unsafe(size_t row, size_t col) const
Fast but unsafe method to read a value from the matrix.
Definition: CMatrixTemplate.h:441
mrpt::typemeta::TTypeName< mrpt::math::CMatrixTemplateNumeric< T > >::get
static auto get()
Definition: CMatrixTemplateNumeric.h:194
mrpt::math::CMatrixTemplate
This template class provides the basic functionality for a general 2D any-size, resizable container o...
Definition: CMatrixTemplate.h:72
mrpt::typemeta::TTypeName
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:65
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::math::CMatrixTemplate::rows
size_t rows() const
Number of rows in the matrix.
Definition: CMatrixTemplate.h:298
mrpt::math::CMatrixTemplateNumeric
A matrix of dynamic size.
Definition: CMatrixTemplateNumeric.h:37
r
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
mrpt::math::CMatrixTemplateNumeric::CMatrixTemplateNumeric
CMatrixTemplateNumeric(TConstructorFlags_Matrices)
Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency i...
Definition: CMatrixTemplateNumeric.h:58
TTypeName.h
data
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3547
mrpt::math::detail::VicinityTraits
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
mrpt::math::TConstructorFlags_Matrices
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes),...
Definition: math_frwds.h:73
mrpt::math::detail::VicinityTraits< CMatrixTemplateNumeric< T > >::insertInContainer
static void insertInContainer(CMatrixTemplateNumeric< T > &mat, size_t r, size_t c, const T &t)
Definition: CMatrixTemplateNumeric.h:179
mrpt::math::detail::VicinityTraits< CMatrixTemplateNumeric< T > >::initialize
static void initialize(CMatrixTemplateNumeric< T > &mat, size_t N)
Definition: CMatrixTemplateNumeric.h:174
mrpt::math::CMatrixTemplateNumeric::CMatrixTemplateNumeric
CMatrixTemplateNumeric(size_t row, size_t col)
Constructor, creates a matrix of the given size, filled with zeros.
Definition: CMatrixTemplateNumeric.h:60
mrpt::typemeta::TTypeName::get
constexpr static auto get()
Definition: TTypeName.h:67
mrpt::math::CMatrixTemplateNumeric::CMatrixTemplateNumeric
CMatrixTemplateNumeric()
Default constructor, builds a 1x1 matrix.
Definition: CMatrixTemplateNumeric.h:50
MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL
#define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_)
Definition: types_math.h:46
row
GLenum GLenum GLvoid * row
Definition: glext.h:3576
mrpt::math::operator==
constexpr bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
Definition: lightweight_geom_data.h:880
mrpt::math::CMatrixTemplate::cols
size_t cols() const
Number of columns in the matrix.
Definition: CMatrixTemplate.h:302
MRPT_MATRIX_CONSTRUCTORS_FROM_POSES
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Definition: math_frwds.h:95
Eigen::MatrixBase
Definition: eigen_frwds.h:20
mrpt::math::CMatrixTemplateNumeric::Base
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign|Eigen::RowMajor > Base
Definition: CMatrixTemplateNumeric.h:46
mrpt::typemeta::literal
constexpr auto literal(const char(&lit)[N_PLUS_1]) -> string_literal< N_PLUS_1 - 1 >
Definition: static_string.h:43
types_math.h
mrpt::system::os::memcpy
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:356



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST