Main MRPT website > C++ reference for MRPT 1.9.9
CMatrixFixedNumeric.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 CMatrixFixedNumeric_H
10 #define CMatrixFixedNumeric_H
11 
12 #include <mrpt/math/math_frwds.h> // Forward declarations
13 #include <mrpt/math/eigen_frwds.h>
14 #include <mrpt/utils/types_math.h>
16 #include <mrpt/math/point_poses2vectors.h> // MRPT_MATRIX_CONSTRUCTORS_FROM_POSES()
17 
18 namespace mrpt
19 {
20 namespace math
21 {
22 /** A numeric matrix of compile-time fixed size.
23  * Basically, this class is a wrapper on Eigen::Matrix<T,NROWS,NCOLS>, but
24  * with a RowMajor element memory layout (except for column vectors).
25  *
26  * These matrices also have iterators to access all the elements in the matrix
27  * as a sequence, starting from the element (0,0), then row by row, from left to
28  * right.
29  *
30  * \note This class exists for backward compatibility of ancient times when MRPT
31  * didn't rely on Eigen, feel free to directly use Eigen::Matrix<> types
32  * instead.
33  * \sa CMatrixTemplateNumeric (for dynamic-size matrices)
34  * \note For a complete introduction to Matrices and vectors in MRPT, see:
35  * http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
36  * \ingroup mrpt_base_grp
37  */
38 template <typename T, size_t NROWS, size_t NCOLS>
40  : public Eigen::Matrix<
41  T, NROWS, NCOLS,
42  // Use row major storage for backward compatibility with MRPT matrices
43  // in all cases, except in column vectors:
44  Eigen::AutoAlign |
45  ((NCOLS == 1 && NROWS != 1) ? Eigen::ColMajor : Eigen::RowMajor)>
46 {
47  public:
48  typedef Eigen::Matrix<T, NROWS, NCOLS,
49  Eigen::AutoAlign |
50  ((NCOLS == 1 && NROWS != 1) ? Eigen::ColMajor
51  : Eigen::RowMajor)>
54 
56  CMatrixFixedNumeric) // Implements ctor and "operator =" for any other
57  // Eigen class
59 
60  /** Default constructor, initializes all elements to zero */
61  inline CMatrixFixedNumeric() : Base() { Base::setZero(); }
62  /** Constructor from an array in row major */
63  inline CMatrixFixedNumeric(const T* vals) : Base(vals) {}
64  /** Constructor which leaves the matrix uninitialized.
65  * Example of usage: CMatrixFixedNumeric<double,3,2>
66  * M(mrpt::math::UNINITIALIZED_MATRIX);
67  */
69  template <size_t N, typename ReturnType>
70  inline ReturnType getVicinity(size_t c, size_t r) const
71  {
73  ReturnType, 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 } // End of detail namespace.
123 
124 } // End of namespace
125 
126 namespace utils
127 {
128 // Extensions to mrpt::utils::TTypeName for matrices:
129 template <typename T, size_t N, size_t M>
131 {
132  static std::string get()
133  {
134  return mrpt::format(
135  "CMatrixFixedNumeric<%s,%u,%u>", TTypeName<T>::get().c_str(),
136  (unsigned int)N, (unsigned int)M);
137  }
138 };
139 }
140 
141 } // End of namespace
142 
143 #endif
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:3689
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes)...
Definition: math_frwds.h:84
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:55
bool operator==(const Eigen::MatrixBase< Derived > &m2) const
== comparison of two matrices; it differs from default Eigen operator in that returns false if matric...
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.
CMatrixFixedNumeric(TConstructorFlags_Matrices)
Constructor which leaves the matrix uninitialized.
A numeric matrix of compile-time fixed size.
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
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:106
GLubyte GLubyte b
Definition: glext.h:6279
CMatrixFixedNumeric(const T *vals)
Constructor from an array in row major.
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
#define ASSERT_(f)
ReturnType getVicinity(size_t c, size_t r) const
CMatrixFixedNumeric< T, NROWS, NCOLS > mrpt_autotype
This huge template encapsulates a function to get the vicinity of an element, with maximum genericity...
Definition: math_frwds.h:229



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