MRPT  1.9.9
CMatrixFixedNumeric.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/math/eigen_frwds.h>
12 #include <mrpt/math/math_frwds.h> // Forward declarations
13 #include <mrpt/math/point_poses2vectors.h> // MRPT_MATRIX_CONSTRUCTORS_FROM_POSES()
14 #include <mrpt/math/types_math.h>
18 
19 namespace mrpt
20 {
21 namespace math
22 {
23 /** A numeric matrix of compile-time fixed size.
24  * Basically, this class is a wrapper on Eigen::Matrix<T,NROWS,NCOLS>, but
25  * with a RowMajor element memory layout (except for column vectors).
26  *
27  * These matrices also have iterators to access all the elements in the matrix
28  * as a sequence, starting from the element (0,0), then row by row, from left to
29  * right.
30  *
31  * \note This class exists for backward compatibility of ancient times when MRPT
32  * didn't rely on Eigen, feel free to directly use Eigen::Matrix<> types
33  * instead.
34  * \sa CMatrixTemplateNumeric (for dynamic-size matrices)
35  * \note For a complete introduction to Matrices and vectors in MRPT, see:
36  * https://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
37  * \ingroup mrpt_math_grp
38  */
39 template <typename T, size_t NROWS, size_t NCOLS>
41  : public Eigen::Matrix<
42  T, NROWS, NCOLS,
43  // Use row major storage for backward compatibility with MRPT matrices
44  // in all cases, except in column vectors:
45  Eigen::AutoAlign |
46  ((NCOLS == 1 && NROWS != 1) ? Eigen::ColMajor : Eigen::RowMajor)>
47 {
48  public:
49  using Base = Eigen::Matrix<
50  T, NROWS, NCOLS,
51  Eigen::AutoAlign |
52  ((NCOLS == 1 && NROWS != 1) ? Eigen::ColMajor : Eigen::RowMajor)>;
56  CMatrixFixedNumeric) // Implements ctor and "operator =" for any other
57  // Eigen class
58 
59  /** Default constructor, initializes all elements to zero */
60  inline CMatrixFixedNumeric() : Base() { Base::setZero(); }
61  /** Constructor from an array in row major */
62  inline CMatrixFixedNumeric(const T* vals) : Base(vals) {}
63  /** Constructor which leaves the matrix uninitialized.
64  * Example of usage: CMatrixFixedNumeric<double,3,2>
65  * M(mrpt::math::UNINITIALIZED_MATRIX);
66  */
68  template <size_t N, typename ReturnType>
69  inline ReturnType getVicinity(size_t c, size_t r) const
70  {
71  return detail::getVicinity<
73  N>::get(c, r, *this);
74  }
75 
76  inline void loadFromArray(const T* vals)
77  {
78  Base b(vals);
79  *this = b;
80  }
81 
82  /** == comparison of two matrices; it differs from default Eigen operator in
83  * that returns false if matrices are of different sizes instead of raising
84  * an assert. */
85  template <typename Derived>
86  inline bool operator==(const Eigen::MatrixBase<Derived>& m2) const
87  {
88  return Base::cols() == m2.cols() && Base::rows() == m2.rows() &&
89  Base::cwiseEqual(m2).all();
90  }
91  /** != comparison of two matrices; it differs from default Eigen operator in
92  * that returns true if matrices are of different sizes instead of raising
93  * an assert. */
94  template <typename Derived>
95  inline bool operator!=(const Eigen::MatrixBase<Derived>& m2) const
96  {
97  return !((*this) == m2);
98  }
99 
100 }; // end of class definition ------------------------------
101 
102 namespace detail
103 {
104 /**
105  * Vicinity traits class specialization for fixed size matrices.
106  */
107 template <typename T, size_t D>
109 {
110  public:
111  inline static void initialize(CMatrixFixedNumeric<T, D, D>& mat, size_t N)
112  {
113  UNUSED(mat);
114  ASSERT_(N == D);
115  }
116  inline static void insertInContainer(
117  CMatrixFixedNumeric<T, D, D>& mat, size_t r, size_t c, const T& t)
118  {
119  mat.get_unsafe(r, c) = t;
120  }
121 };
122 } // namespace detail
123 
124 } // namespace math
125 
126 namespace typemeta
127 {
128 // Extensions to mrpt::typemeta::TTypeName for matrices:
129 template <typename T, size_t N, size_t M>
131 {
132  constexpr static auto get()
133  {
134  return literal("CMatrixFixedNumeric<") + TTypeName<T>::get() +
137  }
138 };
139 } // namespace typemeta
140 
141 } // namespace mrpt
Eigen::Matrix< T, NROWS, NCOLS, Eigen::AutoAlign|((NCOLS==1 &&NROWS !=1) ? Eigen::ColMajor :Eigen::RowMajor)> Base
static void initialize(CMatrixFixedNumeric< T, D, D > &mat, size_t N)
GLdouble GLdouble t
Definition: glext.h:3695
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes)...
Definition: math_frwds.h:72
bool operator==(const Eigen::MatrixBase< Derived > &m2) const
== comparison of two matrices; it differs from default Eigen operator in that returns false if matric...
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
bool operator!=(const Eigen::MatrixBase< Derived > &m2) const
!= comparison of two matrices; it differs from default Eigen operator in that returns true if matrice...
MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixFixedNumeric) inline CMatrixFixedNumeric()
Default constructor, initializes all elements to zero.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:162
CMatrixFixedNumeric(TConstructorFlags_Matrices)
Constructor which leaves the matrix uninitialized.
A numeric matrix of compile-time fixed size.
constexpr auto literal(const char(&lit)[N_PLUS_1]) -> string_literal< N_PLUS_1 - 1 >
Definition: static_string.h:46
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
const GLubyte * c
Definition: glext.h:6406
static void insertInContainer(CMatrixFixedNumeric< T, D, D > &mat, size_t r, size_t c, const T &t)
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Definition: math_frwds.h:94
GLubyte GLubyte b
Definition: glext.h:6372
CMatrixFixedNumeric(const T *vals)
Constructor from an array in row major.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
ReturnType getVicinity(size_t c, size_t r) const
This huge template encapsulates a function to get the vicinity of an element, with maximum genericity...
Definition: math_frwds.h:172
constexpr string representation of a number.
Definition: num_to_string.h:44
static constexpr auto get()
Definition: TTypeName.h:71



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 58f856185 Mon May 20 11:43:57 2019 +0200 at lun may 20 11:50:17 CEST 2019