MRPT  2.0.4
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-2020, 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;
50  using difference_type = std::ptrdiff_t;
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  /** const segmentCopy(): Returns a *copy* of the given vector segment */
204  template <int LEN>
206  {
208  for (int i = 0; i < LEN; i++) v[i] = m_data[start + i];
209  return v;
210  }
211 
212  /** const segmentCopy(): Returns a *copy* of the given vector segment (non
213  * templated version, dynamic length) */
214  CVectorDynamic<Scalar> segmentCopy(int start, int LEN) const
215  {
217  v.resize(LEN);
218  for (int i = 0; i < LEN; i++) v[i] = m_data[start + i];
219  return v;
220  }
221 
222  /** Subscript operator to get/set individual elements
223  */
224  inline T& operator()(size_t row, size_t col)
225  {
226 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
227  if (row >= m_data.size() || col > 0)
229  "Indexes (%lu,%lu) out of range. Vector is %lux%lu",
230  static_cast<unsigned long>(row),
231  static_cast<unsigned long>(col),
232  static_cast<unsigned long>(m_data.size()),
233  static_cast<unsigned long>(1)));
234 #endif
235  return m_data[row];
236  }
237 
238  /** Subscript operator to get individual elements
239  */
240  inline const T& operator()(size_t row, size_t col) const
241  {
242 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
243  if (row >= m_data.size() || col > 0)
245  "Indexes (%lu,%lu) out of range. Vector is %lux%lu",
246  static_cast<unsigned long>(row),
247  static_cast<unsigned long>(col),
248  static_cast<unsigned long>(m_data.size()),
249  static_cast<unsigned long>(1)));
250 #endif
251  return m_data[row];
252  }
253 
254  /** Subscript operator to get/set an individual element from a row or column
255  * matrix.
256  * \exception std::exception If the object is not a column or row matrix.
257  */
258  inline T& operator[](size_t ith)
259  {
260 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
261  if (ith >= m_data.size())
263  "Index %u out of range!", static_cast<unsigned>(ith));
264 #endif
265  return m_data[ith];
266  }
267 
268  /// \overload
269  inline const T& operator[](size_t ith) const
270  {
271 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
272  if (ith >= m_data.size())
274  "Index %u out of range!", static_cast<unsigned>(ith));
275 #endif
276  return m_data[ith];
277  }
278 
279  /** Get as an Eigen-compatible Eigen::Map object */
280  template <
281  typename EIGEN_VECTOR = eigen_t,
282  typename EIGEN_MAP = Eigen::Map<
283  EIGEN_VECTOR, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>>>
284  EIGEN_MAP asEigen()
285  {
286  return EIGEN_MAP(&m_data[0], m_data.size());
287  }
288  /** \overload (const version) */
289  template <
290  typename EIGEN_VECTOR = Eigen::Matrix<T, -1, 1, 0, -1, 1>,
291  typename EIGEN_MAP = Eigen::Map<
292  const EIGEN_VECTOR, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>>>
293  EIGEN_MAP asEigen() const
294  {
295  return EIGEN_MAP(&m_data[0], m_data.size());
296  }
297 
298  template <typename T2>
299  CVectorDynamic<T2> cast() const;
300 };
301 
304 
313 
314 } // namespace mrpt::math
315 
316 namespace mrpt::typemeta
317 {
318 // Extensions to mrpt::typemeta::TTypeName for matrices:
319 template <typename T>
321 {
322  static auto get()
323  {
324  return literal("CVectorDynamic<") + TTypeName<T>::get() + literal(">");
325  }
326 };
327 } // 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
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
size_t size(const MATRIXLIKE &m, const int dim)
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
CVectorDynamic< Scalar > segmentCopy(int start, int LEN) const
const segmentCopy(): Returns a copy of the given vector segment (non templated version, dynamic length)
T & operator[](size_t ith)
Subscript operator to get/set an individual element from a row or column matrix.
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)
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
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.
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.
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:54
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:626
#define MRPT_END
Definition: exceptions.h:245
T & operator()(size_t row, size_t col)
Subscript operator to get/set individual elements.
Base CRTP class for all MRPT vectors and matrices.
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...
const T & operator[](size_t ith) const
static constexpr int ColsAtCompileTime
static constexpr auto get()
Definition: TTypeName.h:71
CMatrixFixed< Scalar, LEN, 1 > segmentCopy(int start=0) const
const segmentCopy(): Returns a copy of the given vector segment
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020