MRPT  1.9.9
CVectorDynamic.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 
12 #include <mrpt/core/exceptions.h> // ASSERT_()
14 #include <mrpt/math/math_frwds.h>
18 #include <cstring> // memset()
19 #include <type_traits>
20 
21 namespace mrpt::math
22 {
23 /** Template for column vectors of dynamic size, compatible with Eigen.
24  *
25  * \note For a complete introduction to Matrices and vectors in MRPT, see:
26  *http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
27  * \sa CVectorDynamic, CMatrixFixed, CVectorFixed
28  * \ingroup mrpt_math_grp
29  */
30 template <class T>
31 class CVectorDynamic : public MatrixVectorBase<T, CVectorDynamic<T>>
32 {
33  protected:
34  static constexpr size_t small_size = 16;
36  T, small_size, MRPT_MAX_STATIC_ALIGN_BYTES>;
37 
39 
40  public:
41  /** @name Matrix type definitions
42  * @{ */
43  /** The type of the matrix elements */
44  using value_type = T;
45  using Scalar = T;
46  using Index = int;
47  using reference = T&;
48  using const_reference = const T&;
49  using size_type = int;
51  constexpr static int RowsAtCompileTime = -1;
52  constexpr static int ColsAtCompileTime = 1;
53  constexpr static int is_mrpt_type = 1;
54  using eigen_t = Eigen::Matrix<T, -1, 1, 0, -1, 1>;
55  /** @} */
56 
57  /** @name Iterators interface
58  * @{ */
59  using iterator = typename vec_t::iterator;
61  iterator begin() { return m_data.begin(); }
62  iterator end() { return m_data.end(); }
63  const_iterator begin() const { return m_data.begin(); }
64  const_iterator end() const { return m_data.end(); }
65  const_iterator cbegin() const { return m_data.begin(); }
66  const_iterator cend() const { return m_data.end(); }
67  /** @} */
68 
69  /** Internal use only: It reallocs the memory for the 2D matrix, maintaining
70  * the previous contents if posible.
71  */
72  void realloc(const size_t new_len, bool newElementsToZero = false)
73  {
74  const auto old_len = m_data.size();
75  if (new_len == old_len) return;
76  m_data.resize(new_len);
77  if (newElementsToZero && new_len > old_len)
78  {
79  if constexpr (std::is_trivial_v<T>)
80  ::memset(&m_data[old_len], 0, sizeof(T) * (new_len - old_len));
81  else
82  for (size_t k = old_len; k < new_len; k++) m_data[k] = T();
83  }
84  }
85 
86  public:
88 
89  CVectorDynamic() = default;
90 
91  /** Initializes to a vector of "N" zeros */
92  CVectorDynamic(size_t N, bool initZero = true) { realloc(N, initZero); }
93 
94  /** Copy (casting from if needed) from another matrix */
95  template <typename U>
96  explicit CVectorDynamic(const CVectorDynamic<U>& m)
97  {
98  (*this) = m;
99  }
100 
101  /** Ctor from a fixed-size vector */
102  template <std::size_t ROWS>
104  {
105  (*this) = v;
106  }
107 
108  /** Constructor from a given size and a C array. The array length must match
109  *cols x row.
110  * \code
111  * const double numbers[] = {
112  * 1,2,3,
113  * 4,5,6 };
114  * CMatrixDouble M(3,2, numbers);
115  * \endcode
116  */
117  template <
118  typename ARRAY, typename = std::enable_if_t<std::is_array_v<ARRAY>>>
119  CVectorDynamic(const ARRAY& data)
120  {
121  std::size_t N = std::size(data);
122  ASSERTMSG_(N != 0, "CVectorDynamic ctor: Empty array!");
123  realloc(N);
124  for (size_t i = 0; i < N; i++) m_data[i] = static_cast<T>(data[i]);
125  }
126 
127  /** Convert from Eigen matrix */
128  template <class Derived>
130  {
131  *this = m;
132  }
133 
134  /** Number of rows in the vector */
135  size_type rows() const { return m_data.size(); }
136 
137  /** Number of columns in the matrix (always 1) */
138  size_type cols() const { return 1; }
139 
140  /** Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x)) */
141  size_type size() const { return m_data.size(); }
142 
143  bool empty() const { return m_data.empty(); }
144 
145  /** Changes the size of matrix, maintaining the previous contents. */
146  void setSize(size_t row, size_t col, bool zeroNewElements = false)
147  {
148  ASSERT_(col == 1);
149  realloc(row, zeroNewElements);
150  }
151  void resize(std::size_t N, bool zeroNewElements = false)
152  {
153  setSize(N, 1, zeroNewElements);
154  }
155 
156  template <class MAT>
157  void fromVectorLike(const MAT& m)
158  {
159  MRPT_START
160  ASSERT_EQUAL_(m.cols(), 1U);
161  setSize(m.rows(), 1);
162  for (Index r = 0; r < rows(); r++) (*this)[r] = m(r, 0);
163  MRPT_END
164  }
165 
166  /** Assignment operator from another matrix (possibly of a different type)
167  */
168  template <typename U>
170  {
171  MRPT_START
172  fromVectorLike(m);
173  return *this;
174  MRPT_END
175  }
176 
177  /** Assignment from an Eigen matrix */
178  template <class Derived>
180  {
181  MRPT_START
182  fromVectorLike(m);
183  return *this;
184  MRPT_END
185  }
186 
187  /** Assignment from a fixed-size vector */
188  template <std::size_t ROWS>
190  {
191  MRPT_START
192  fromVectorLike(v);
193  return *this;
194  MRPT_END
195  }
196 
197  void push_back(const T& val)
198  {
199  m_data.resize(m_data.size() + 1);
200  m_data.back() = val;
201  }
202 
203  /** Subscript operator to get/set individual elements
204  */
205  inline T& operator()(size_t row, size_t col)
206  {
207 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
208  if (row >= m_data.size() || col > 0)
210  "Indexes (%lu,%lu) out of range. Vector is %lux%lu",
211  static_cast<unsigned long>(row),
212  static_cast<unsigned long>(col),
213  static_cast<unsigned long>(m_data.size()),
214  static_cast<unsigned long>(1)));
215 #endif
216  return m_data[row];
217  }
218 
219  /** Subscript operator to get individual elements
220  */
221  inline const T& operator()(size_t row, size_t col) const
222  {
223 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
224  if (row >= m_data.size() || col > 0)
226  "Indexes (%lu,%lu) out of range. Vector is %lux%lu",
227  static_cast<unsigned long>(row),
228  static_cast<unsigned long>(col),
229  static_cast<unsigned long>(m_data.size()),
230  static_cast<unsigned long>(1)));
231 #endif
232  return m_data[row];
233  }
234 
235  /** Subscript operator to get/set an individual element from a row or column
236  * matrix.
237  * \exception std::exception If the object is not a column or row matrix.
238  */
239  inline T& operator[](size_t ith)
240  {
241 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
242  if (ith >= m_data.size())
244  "Index %u out of range!", static_cast<unsigned>(ith));
245 #endif
246  return m_data[ith];
247  }
248 
249  /// \overload
250  inline const T& operator[](size_t ith) const
251  {
252 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
253  if (ith >= m_data.size())
255  "Index %u out of range!", static_cast<unsigned>(ith));
256 #endif
257  return m_data[ith];
258  }
259 
260  /** Get as an Eigen-compatible Eigen::Map object */
261  template <
262  typename EIGEN_VECTOR = eigen_t,
263  typename EIGEN_MAP = Eigen::Map<
264  EIGEN_VECTOR, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>>>
265  EIGEN_MAP asEigen()
266  {
267  return EIGEN_MAP(&m_data[0], m_data.size());
268  }
269  /** \overload (const version) */
270  template <
271  typename EIGEN_VECTOR = Eigen::Matrix<T, -1, 1, 0, -1, 1>,
272  typename EIGEN_MAP = Eigen::Map<
273  const EIGEN_VECTOR, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>>>
274  EIGEN_MAP asEigen() const
275  {
276  return EIGEN_MAP(&m_data[0], m_data.size());
277  }
278 
279  template <typename T2>
280  CVectorDynamic<T2> cast() const;
281 };
282 
285 
294 
295 } // namespace mrpt::math
296 
297 namespace mrpt::typemeta
298 {
299 // Extensions to mrpt::typemeta::TTypeName for matrices:
300 template <typename T>
302 {
303  static auto get()
304  {
305  return literal("CVectorDynamic<") + TTypeName<T>::get() + literal(">");
306  }
307 };
308 } // namespace mrpt::typemeta
typename vec_t::iterator iterator
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
#define MRPT_START
Definition: exceptions.h:241
CVectorDynamic & operator=(const CMatrixDynamic< U > &m)
Assignment operator from another matrix (possibly of a different type)
Template for column vectors of dynamic size, compatible with Eigen.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
CVectorDynamic< T2 > cast() const
size_type cols() const
Number of columns in the matrix (always 1)
Eigen::Matrix< T, -1, 1, 0, -1, 1 > eigen_t
CVectorDynamic & operator=(const Eigen::MatrixBase< Derived > &m)
Assignment from an Eigen matrix.
size_type size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
static constexpr int is_mrpt_type
T & operator[](size_t ith)
Subscript operator to get/set an individual element from a row or column matrix.
GLdouble s
Definition: glext.h:3682
CVectorDynamic(const ARRAY &data)
Constructor from a given size and a C array.
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
void push_back(const T &val)
const_iterator end() const
CVectorDynamic(const CMatrixFixed< T, ROWS, 1 > &v)
Ctor from a fixed-size vector.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
EIGEN_MAP asEigen() const
CVectorDynamic & operator=(const CMatrixFixed< T, ROWS, 1 > &v)
Assignment from a fixed-size vector.
This base provides a set of functions for maths stuff.
const T & operator()(size_t row, size_t col) const
Subscript operator to get individual elements.
constexpr auto literal(const char(&lit)[N_PLUS_1]) -> string_literal< N_PLUS_1 - 1 >
Definition: static_string.h:46
const_iterator begin() const
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
static constexpr int RowsAtCompileTime
const_iterator cbegin() const
CVectorDynamic(const Eigen::MatrixBase< Derived > &m)
Convert from Eigen matrix.
int val
Definition: mrpt_jpeglib.h:957
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
typename vec_t::const_iterator const_iterator
void realloc(const size_t new_len, bool newElementsToZero=false)
Internal use only: It reallocs the memory for the 2D matrix, maintaining the previous contents if pos...
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
size_type rows() const
Number of rows in the vector.
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
_W64 int ptrdiff_t
Definition: glew.h:136
void swap(CVectorDynamic< T > &o)
const_iterator cend() const
CVectorDynamic(const CVectorDynamic< U > &m)
Copy (casting from if needed) from another matrix.
KFTYPE value_type
The type of the matrix elements.
const GLdouble * v
Definition: glext.h:3684
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void fromVectorLike(const MAT &m)
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:630
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
GLenum GLenum GLvoid * row
Definition: glext.h:3580
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
T & operator()(size_t row, size_t col)
Subscript operator to get/set individual elements.
Base CRTP class for all MRPT vectors and matrices.
GLsizeiptr size
Definition: glext.h:3934
EIGEN_MAP asEigen()
Get as an Eigen-compatible Eigen::Map object.
void resize(std::size_t N, bool zeroNewElements=false)
static constexpr size_t small_size
CVectorDynamic(size_t N, bool initZero=true)
Initializes to a vector of "N" zeros.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
This template class provides the basic functionality for a general 2D any-size, resizable container o...
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
const T & operator[](size_t ith) const
static constexpr int ColsAtCompileTime
static constexpr auto get()
Definition: TTypeName.h:71



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019