MRPT  1.9.9
CMatrixTemplate.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 
12 #include <mrpt/math/math_frwds.h> // forward declarations
13 #include <algorithm> // swap()
14 #include <array>
15 #include <cstring> // memset()
16 
17 namespace mrpt::math
18 {
19 /** Auxiliary class used in CMatrixTemplate:size(), CMatrixTemplate::resize(),
20  * CMatrixFixedNumeric::size(), CMatrixFixedNumeric::resize(), to mimic the
21  * behavior of STL-containers */
22 struct CMatrixTemplateSize : public std::array<size_t, 2>
23 {
24  using Base = std::array<size_t, 2>;
26 
27  inline CMatrixTemplateSize() : std::array<size_t, 2>() {}
28  inline CMatrixTemplateSize(const size_t* d)
29  {
30  (*this)[0] = d[0];
31  (*this)[1] = d[1];
32  }
33 
34  inline bool operator==(const CMatrixTemplateSize& o) const
35  {
36  return Base::operator[](0) == o[0] && Base::operator[](1) == o[1];
37  }
38  inline bool operator!=(const CMatrixTemplateSize& o) const
39  {
40  return !(*this == o);
41  }
42  /** This operator allows the size(N,M) to be compared with a plain size_t
43  * N*M */
44  inline operator size_t(void) const { return 2; }
45 };
46 
47 /** This template class provides the basic functionality for a general 2D
48  *any-size, resizable container of numerical or non-numerical elements.
49  * NOTES:
50  * - This class is not serializable since it is a template. For using
51  *serialization, see mrpt::math::CMatrixNumeric
52  * - First row or column index is "0".
53  * - This class includes range checks with ASSERT_() if compiling with
54  *"_DEBUG" or "MRPT_ALWAYS_CHECKS_DEBUG_MATRICES=1".
55  * - Please DO NOT use as template class type any other class. It can be
56  *safely used the following types:
57  * - Elemental types (int,char,float,doble,...)
58  * - Data struct (Not classes!)
59  * - Any kind of pointers (user is responsible for allocating and
60  *freeing
61  *the memory addressed by pointers).
62  *
63  * \note Memory blocks for each row are 16-bytes aligned (since MRPT 0.7.0).
64  * \note For a complete introduction to Matrices and vectors in MRPT, see:
65  *http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
66  * \sa CMatrixTemplateNumeric
67  * \ingroup mrpt_math_grp
68  */
69 template <class T>
71 {
72  public:
73  // type definitions
74  /** The type of the matrix elements */
75  using value_type = T;
76  using reference = T&;
77  using const_reference = const T&;
78  using size_type = std::size_t;
80 
81  protected:
82  T** m_Val;
83  size_t m_Rows, m_Cols;
84 
85  /** Internal use only: It reallocs the memory for the 2D matrix, maintaining
86  * the previous contents if posible.
87  */
88  void realloc(size_t row, size_t col, bool newElementsToZero = false)
89  {
90  if (row != m_Rows || col != m_Cols || m_Val == nullptr)
91  {
92  size_t r;
93  bool doZeroColumns = newElementsToZero && (col > m_Cols);
94  size_t sizeZeroColumns = sizeof(T) * (col - m_Cols);
95 
96  // If we are reducing rows, free that memory:
97  for (r = row; r < m_Rows; r++) mrpt::aligned_free(m_Val[r]);
98 
99  // Realloc the vector of pointers:
100  if (!row)
101  {
103  m_Val = nullptr;
104  }
105  else
106  m_Val = static_cast<T**>(
107  mrpt::aligned_realloc(m_Val, sizeof(T*) * row, 16));
108 
109  // How many new rows/cols?
110  size_t row_size = col * sizeof(T);
111 
112  // Alloc new ROW pointers & resize previously existing rows, as
113  // required:
114  for (r = 0; r < row; r++)
115  {
116  if (r < m_Rows)
117  {
118  // This was an existing row: Resize the memory:
119  m_Val[r] = static_cast<T*>(
120  mrpt::aligned_realloc(m_Val[r], row_size, 16));
121 
122  if (doZeroColumns)
123  {
124  // Fill with zeros:
125  ::memset(&m_Val[r][m_Cols], 0, sizeZeroColumns);
126  }
127  }
128  else
129  {
130  // This is a new row, alloc the memory for the first time:
131  m_Val[r] =
132  static_cast<T*>(mrpt::aligned_malloc(row_size, 16));
133  ::memset(m_Val[r], 0, row_size);
134  }
135  }
136  // Done!
137  m_Rows = row;
138  m_Cols = col;
139  }
140  }
141 
142  public:
143  /**
144  * Checks whether the rows [r-N,r+N] and the columns [c-N,c+N] are present
145  * in the matrix.
146  */
147  template <size_t N>
148  inline void ASSERT_ENOUGHROOM(size_t r, size_t c) const
149  {
150 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
151  ASSERT_((r >= N) && (r + N < rows()) && (c >= N) && (c + N < cols()));
152 #endif
153  }
154  /*! Fill all the elements with a given value (Note: named "fillAll" since
155  * "fill" will be used by child classes) */
156  void fillAll(const T& val)
157  {
158  for (size_t r = 0; r < m_Rows; r++)
159  for (size_t c = 0; c < m_Cols; c++) m_Val[r][c] = val;
160  }
161 
162  /** Swap with another matrix very efficiently (just swaps a pointer and two
163  * integer values). */
164  inline void swap(CMatrixTemplate<T>& o)
165  {
166  std::swap(m_Val, o.m_Val);
167  std::swap(m_Rows, o.m_Rows);
168  std::swap(m_Cols, o.m_Cols);
169  }
170 
171  /** Constructors */
173  : m_Val(nullptr), m_Rows(0), m_Cols(0)
174  {
175  (*this) = m;
176  }
177 
178  CMatrixTemplate(size_t row = 1, size_t col = 1)
179  : m_Val(nullptr), m_Rows(0), m_Cols(0)
180  {
181  realloc(row, col);
182  }
183 
184  /** Copy constructor & crop from another matrix
185  */
187  const CMatrixTemplate& m, const size_t cropRowCount,
188  const size_t cropColCount)
189  : m_Val(nullptr), m_Rows(0), m_Cols(0)
190  {
191  ASSERT_(m.m_Rows >= cropRowCount);
192  ASSERT_(m.m_Cols >= cropColCount);
193  realloc(cropRowCount, cropColCount);
194  for (size_t i = 0; i < m_Rows; i++)
195  for (size_t j = 0; j < m_Cols; j++) m_Val[i][j] = m.m_Val[i][j];
196  }
197 
198  /** Constructor from a given size and a C array. The array length must match
199  *cols x row.
200  * \code
201  * const double numbers[] = {
202  * 1,2,3,
203  * 4,5,6 };
204  * CMatrixDouble M(3,2, numbers);
205  * \endcode
206  */
207  template <typename V, size_t N>
208  CMatrixTemplate(size_t row, size_t col, V (&theArray)[N])
209  : m_Val(nullptr), m_Rows(0), m_Cols(0)
210  {
211  MRPT_COMPILE_TIME_ASSERT(N != 0);
212  realloc(row, col);
213  if (m_Rows * m_Cols != N)
215  format(
216  "Mismatch between matrix size %lu x %lu and array of "
217  "length %lu",
218  static_cast<long unsigned>(m_Rows),
219  static_cast<long unsigned>(m_Cols),
220  static_cast<long unsigned>(N)))
221  size_t idx = 0;
222  for (size_t i = 0; i < m_Rows; i++)
223  for (size_t j = 0; j < m_Cols; j++)
224  m_Val[i][j] = static_cast<T>(theArray[idx++]);
225  }
226 
227  /** Constructor from a given size and a STL container (std::vector,
228  * std::list,...) with the initial values. The vector length must match cols
229  * x row.
230  */
231  template <typename V>
232  CMatrixTemplate(size_t row, size_t col, const V& theVector)
233  : m_Val(nullptr), m_Rows(0), m_Cols(0)
234  {
235  const size_t N = theVector.size();
236  realloc(row, col);
237  if (m_Rows * m_Cols != N)
239  format(
240  "Mismatch between matrix size %lu x %lu and array of "
241  "length %lu",
242  static_cast<long unsigned>(m_Rows),
243  static_cast<long unsigned>(m_Cols),
244  static_cast<long unsigned>(N)))
245  typename V::const_iterator it = theVector.begin();
246  for (size_t i = 0; i < m_Rows; i++)
247  for (size_t j = 0; j < m_Cols; j++)
248  m_Val[i][j] = static_cast<T>(*(it++));
249  }
250 
251  /** Destructor */
252  virtual ~CMatrixTemplate() { realloc(0, 0); }
253  /** Assignment operator from another matrix */
255  {
256  realloc(m.m_Rows, m.m_Cols);
257  for (size_t i = 0; i < m_Rows; i++)
258  for (size_t j = 0; j < m_Cols; j++) m_Val[i][j] = m.m_Val[i][j];
259  return *this;
260  }
261 
262  /** Assignment operator for initializing from a C array (The matrix must be
263  *set to the correct size before invoking this asignament)
264  * \code
265  * CMatrixDouble M(3,2);
266  * const double numbers[] = {
267  * 1,2,3,
268  * 4,5,6 };
269  * M = numbers;
270  * \endcode
271  * Refer also to the constructor with initialization data
272  *CMatrixTemplate::CMatrixTemplate
273  */
274  template <typename V, size_t N>
275  CMatrixTemplate& operator=(V (&theArray)[N])
276  {
277  MRPT_COMPILE_TIME_ASSERT(N != 0);
278  if (m_Rows * m_Cols != N)
279  {
281  format(
282  "Mismatch between matrix size %lu x %lu and array of "
283  "length %lu",
284  m_Rows, m_Cols, N))
285  }
286  size_t idx = 0;
287  for (size_t i = 0; i < m_Rows; i++)
288  for (size_t j = 0; j < m_Cols; j++)
289  m_Val[i][j] = static_cast<T>(theArray[idx++]);
290  return *this;
291  }
292 
293  /** Number of rows in the matrix
294  * \sa rows(), getColCount, nr, nc
295  */
296  inline size_t rows() const { return m_Rows; }
297  /** Number of columns in the matrix
298  * \sa rows(), getColCount, nr, nc
299  */
300  inline size_t cols() const { return m_Cols; }
301  /** Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x)) */
302  inline CMatrixTemplateSize size() const
303  {
304  CMatrixTemplateSize dims;
305  dims[0] = m_Rows;
306  dims[1] = m_Cols;
307  return dims;
308  }
309 
310  /** Changes the size of matrix, maintaining the previous contents. */
311  void setSize(size_t row, size_t col, bool zeroNewElements = false)
312  {
313  realloc(row, col, zeroNewElements);
314  }
315 
316  /** This method just checks has no effects in this class, but raises an
317  * exception if the expected size does not match */
318  inline void resize(
319  const CMatrixTemplateSize& siz, bool zeroNewElements = false)
320  {
321  setSize(siz[0], siz[1], zeroNewElements);
322  }
323 
324  /** Subscript operator to get/set individual elements
325  */
326  inline T& operator()(size_t row, size_t col)
327  {
328 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
329  if (row >= m_Rows || col >= m_Cols)
331  format(
332  "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
333  static_cast<unsigned long>(row),
334  static_cast<unsigned long>(col),
335  static_cast<unsigned long>(m_Rows),
336  static_cast<unsigned long>(m_Cols)));
337 #endif
338  return m_Val[row][col];
339  }
340 
341  /** Subscript operator to get individual elements
342  */
343  inline const T& operator()(size_t row, size_t col) const
344  {
345 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
346  if (row >= m_Rows || col >= m_Cols)
348  format(
349  "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
350  static_cast<unsigned long>(row),
351  static_cast<unsigned long>(col),
352  static_cast<unsigned long>(m_Rows),
353  static_cast<unsigned long>(m_Cols)));
354 #endif
355  return m_Val[row][col];
356  }
357 
358  /** Subscript operator to get/set an individual element from a row or column
359  * matrix.
360  * \exception std::exception If the object is not a column or row matrix.
361  */
362  inline T& operator()(size_t ith)
363  {
364 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
365  ASSERT_(m_Rows == 1 || m_Cols == 1);
366 #endif
367  if (m_Rows == 1)
368  {
369 // A row matrix:
370 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
371  if (ith >= m_Cols)
373  "Index %u out of range!", static_cast<unsigned>(ith));
374 #endif
375  return m_Val[0][ith];
376  }
377  else
378  {
379 // A columns matrix:
380 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
381  if (ith >= m_Rows)
383  "Index %u out of range!", static_cast<unsigned>(ith));
384 #endif
385  return m_Val[ith][0];
386  }
387  }
388 
389  /** Subscript operator to get/set an individual element from a row or column
390  * matrix.
391  * \exception std::exception If the object is not a column or row matrix.
392  */
393  inline T operator()(size_t ith) const
394  {
395 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
396  ASSERT_(m_Rows == 1 || m_Cols == 1);
397 #endif
398  if (m_Rows == 1)
399  {
400 // A row matrix:
401 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
402  if (ith >= m_Cols)
404  "Index %u out of range!", static_cast<unsigned>(ith));
405 #endif
406  return m_Val[0][ith];
407  }
408  else
409  {
410 // A columns matrix:
411 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
412  if (ith >= m_Rows)
414  "Index %u out of range!", static_cast<unsigned>(ith));
415 #endif
416  return m_Val[ith][0];
417  }
418  }
419 
420  /** Fast but unsafe method to write a value in the matrix
421  */
422  inline void set_unsafe(size_t row, size_t col, const T& v)
423  {
424 #ifdef _DEBUG
425  if (row >= m_Rows || col >= m_Cols)
427  format(
428  "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
429  static_cast<unsigned long>(row),
430  static_cast<unsigned long>(col),
431  static_cast<unsigned long>(m_Rows),
432  static_cast<unsigned long>(m_Cols)));
433 #endif
434  m_Val[row][col] = v;
435  }
436 
437  /** Fast but unsafe method to read a value from the matrix
438  */
439  inline const T& get_unsafe(size_t row, size_t col) const
440  {
441 #ifdef _DEBUG
442  if (row >= m_Rows || col >= m_Cols)
444  format(
445  "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
446  static_cast<unsigned long>(row),
447  static_cast<unsigned long>(col),
448  static_cast<unsigned long>(m_Rows),
449  static_cast<unsigned long>(m_Cols)));
450 #endif
451  return m_Val[row][col];
452  }
453 
454  /** Fast but unsafe method to get a reference from the matrix
455  */
456  inline T& get_unsafe(size_t row, size_t col)
457  {
458 #ifdef _DEBUG
459  if (row >= m_Rows || col >= m_Cols)
461  format(
462  "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
463  static_cast<unsigned long>(row),
464  static_cast<unsigned long>(col),
465  static_cast<unsigned long>(m_Rows),
466  static_cast<unsigned long>(m_Cols)));
467 #endif
468  return m_Val[row][col];
469  }
470 
471  /** Fast but unsafe method to obtain a pointer to a given row of the matrix
472  * (Use only in time critical applications)
473  */
474  inline T* get_unsafe_row(size_t row)
475  {
476 #ifdef _DEBUG
477  if (row >= m_Rows)
479  format(
480  "Row index %lu out of range. Matrix is %lux%lu",
481  static_cast<unsigned long>(row),
482  static_cast<unsigned long>(m_Rows),
483  static_cast<unsigned long>(m_Cols)));
484 #endif
485  return m_Val[row];
486  }
487 
488  /** Fast but unsafe method to obtain a pointer to a given row of the matrix
489  * (Use only in critical applications)
490  */
491  inline const T* get_unsafe_row(size_t row) const { return m_Val[row]; }
492  /** Subscript operator to get a submatrix
493  */
495  const size_t row1, const size_t row2, const size_t col1,
496  const size_t col2) const
497  {
498  CMatrixTemplate<T> val(0, 0);
499  extractSubmatrix(row1, row2, col1, col2, val);
500  return val;
501  }
502 
503  /** Get a submatrix, given its bounds
504  * \sa extractSubmatrixSymmetricalBlocks
505  */
507  const size_t row1, const size_t row2, const size_t col1,
508  const size_t col2, CMatrixTemplate<T>& out) const
509  {
510  int nrows = int(row2) - int(row1) + 1;
511  int ncols = int(col2) - int(col1) + 1;
512  if (nrows <= 0 || ncols <= 0)
513  {
514  out.realloc(0, 0);
515  return;
516  }
517  if (row2 >= m_Rows || col2 >= m_Cols)
518  THROW_EXCEPTION("Indices out of range!");
519  out.realloc(nrows, ncols);
520  for (int i = 0; i < nrows; i++)
521  for (int j = 0; j < ncols; j++)
522  out.m_Val[i][j] = m_Val[i + row1][j + col1];
523  }
524  /// @overload
525  template <class EIGEN_MATRIX>
527  const size_t row1, const size_t row2, const size_t col1,
528  const size_t col2, EIGEN_MATRIX& out) const
529  {
530  int nrows = int(row2) - int(row1) + 1;
531  int ncols = int(col2) - int(col1) + 1;
532  if (nrows <= 0 || ncols <= 0)
533  {
534  out = typename EIGEN_MATRIX::PlainObject();
535  return;
536  }
537  if (row2 >= m_Rows || col2 >= m_Cols)
538  THROW_EXCEPTION("Indices out of range!");
539  out.resize(nrows, ncols);
540  for (int i = 0; i < nrows; i++)
541  for (int j = 0; j < ncols; j++)
542  out.coeffRef(i, j) = m_Val[i + row1][j + col1];
543  }
544 
545  /** Gets a series of contiguous rows.
546  * \exception std::logic_error On index out of bounds
547  * \sa extractRow
548  * \sa extractColumns
549  */
550  inline void extractRows(
551  size_t firstRow, size_t lastRow, CMatrixTemplate<T>& out) const
552  {
553  out.setSize(lastRow - firstRow + 1, m_Cols);
554  detail::extractMatrix(*this, firstRow, 0, out);
555  }
556 
557  /** Gets a series of contiguous columns.
558  * \exception std::logic_error On index out of bounds
559  * \sa extractColumn
560  * \sa extractRows
561  */
562  inline void extractColumns(
563  size_t firstCol, size_t lastCol, CMatrixTemplate<T>& out) const
564  {
565  out.setSize(m_Rows, lastCol - firstCol + 1);
566  detail::extractMatrix(*this, 0, firstCol, out);
567  }
568 
569  /** Returns a given column to a vector (without modifying the matrix)
570  * \exception std::exception On index out of bounds
571  */
572  void extractCol(size_t nCol, std::vector<T>& out, int startingRow = 0) const
573  {
574  size_t i, n;
575 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
576  if (nCol >= m_Cols)
577  THROW_EXCEPTION("extractCol: Column index out of bounds");
578 #endif
579 
580  n = m_Rows - startingRow;
581  out.resize(n);
582 
583  for (i = 0; i < n; i++) out[i] = m_Val[i + startingRow][nCol];
584  }
585 
586  /** Gets a given column to a vector (without modifying the matrix)
587  * \exception std::exception On index out of bounds
588  */
590  size_t nCol, CMatrixTemplate<T>& out, int startingRow = 0) const
591  {
592  size_t i, n;
593 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
594  if (nCol >= m_Cols)
595  THROW_EXCEPTION("extractCol: Column index out of bounds");
596 #endif
597 
598  n = m_Rows - startingRow;
599  out.setSize(n, 1);
600 
601  for (i = 0; i < n; i++) out(i, 0) = m_Val[i + startingRow][nCol];
602  }
603 
604  /** Appends a new row to the MxN matrix from a 1xN vector.
605  * The lenght of the vector must match the width of the matrix, unless
606  * it's empty: in that case the matrix is resized to 1xN.
607  * \code
608  * CMatrixDouble M(0,0);
609  * CVectorDouble v(7),w(7);
610  * // ...
611  * M.appendRow(v);
612  * M.appendRow(w);
613  * \endcode
614  * \exception std::exception On incorrect vector length.
615  * \sa extractRow
616  * \sa appendCol
617  */
618  void appendRow(const std::vector<T>& in)
619  {
620  size_t i, n, row;
621 
622  n = m_Cols;
623  row = m_Rows;
624 
625  if (m_Cols == 0 || m_Rows == 0)
626  {
627  ASSERT_(!in.empty());
628  n = m_Cols = in.size();
629  }
630  else
631  {
632  ASSERT_(in.size() == m_Cols);
633  }
634 
635  realloc(row + 1, n);
636 
637  for (i = 0; i < n; i++) m_Val[row][i] = in[i];
638  }
639 
640  /** Appends a new column to the matrix from a vector.
641  * The length of the vector must match the number of rows of the matrix,
642  * unless it is (0,0).
643  * \exception std::exception On size mismatch.
644  * \sa extractCol
645  * \sa appendRow
646  */
647  void appendCol(const std::vector<T>& in)
648  {
649  size_t r = m_Rows, c = m_Cols;
650  if (m_Cols == 0 || m_Rows == 0)
651  {
652  ASSERT_(!in.empty());
653  r = in.size();
654  c = 0;
655  }
656  else
657  ASSERT_(in.size() == m_Rows);
658  realloc(r, c + 1);
659  for (size_t i = 0; i < m_Rows; i++) m_Val[i][m_Cols - 1] = in[i];
660  }
661 
662  /** Inserts a column from a vector, replacing the current contents of that
663  * column.
664  * \exception std::exception On index out of bounds
665  * \sa extractCol
666  */
667  void insertCol(size_t nCol, const std::vector<T>& in)
668  {
669  if (nCol >= m_Cols)
670  THROW_EXCEPTION("insertCol: Row index out of bounds");
671 
672  size_t n = in.size();
673  ASSERT_(m_Rows >= in.size());
674 
675  for (size_t i = 0; i < n; i++) m_Val[i][nCol] = in[i];
676  }
677 
678  /** Returns a vector containing the matrix's values.
679  */
680  void getAsVector(std::vector<T>& out) const
681  {
682  out.clear();
683  out.reserve(m_Rows * m_Cols);
684  for (size_t i = 0; i < m_Rows; i++)
685  out.insert(out.end(), &(m_Val[i][0]), &(m_Val[i][m_Cols]));
686  }
687 
688 }; // end of class CMatrixTemplate
689 
690 /** Declares a matrix of booleans (non serializable).
691  * \sa CMatrixDouble, CMatrixFloat, CMatrixB
692  */
693 // using CMatrixBool = CMatrixTemplate<bool>;
694 class CMatrixBool : public CMatrixTemplate<bool>
695 {
696  public:
697  /** Constructor */
698  CMatrixBool(size_t row = 1, size_t col = 1);
699  /** Copy constructor */
701  /** Assignment operator for float matrixes */
703 };
704 
705 }
706 
const T & get_unsafe(size_t row, size_t col) const
Fast but unsafe method to read a value from the matrix.
T & operator()(size_t row, size_t col)
Subscript operator to get/set individual elements.
void aligned_free(void *ptr)
void extractCol(size_t nCol, std::vector< T > &out, int startingRow=0) const
Returns a given column to a vector (without modifying the matrix)
T & get_unsafe(size_t row, size_t col)
Fast but unsafe method to get a reference from the matrix.
virtual ~CMatrixTemplate()
Destructor.
Declares a matrix of booleans (non serializable).
void insertCol(size_t nCol, const std::vector< T > &in)
Inserts a column from a vector, replacing the current contents of that column.
CMatrixBool & operator=(const CMatrixTemplate< bool > &m)
Assignment operator for float matrixes.
Definition: CMatrixB.cpp:72
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
CMatrixTemplate(const CMatrixTemplate &m, const size_t cropRowCount, const size_t cropColCount)
Copy constructor & crop from another matrix.
std::array< size_t, 2 > Base
T * get_unsafe_row(size_t row)
Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in time critical ap...
GLenum GLsizei n
Definition: glext.h:5074
Auxiliary class used in CMatrixTemplate:size(), CMatrixTemplate::resize(), CMatrixFixedNumeric::size(...
void appendRow(const std::vector< T > &in)
Appends a new row to the MxN matrix from a 1xN vector.
bool operator!=(const CMatrixTemplateSize &o) const
STL namespace.
void * aligned_malloc(size_t size, size_t alignment)
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:197
CMatrixTemplate(size_t row, size_t col, V(&theArray)[N])
Constructor from a given size and a C array.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
This base provides a set of functions for maths stuff.
void extractMatrix(const MATORG &M, const size_t first_row, const size_t first_col, MATDEST &outMat)
Extract a submatrix - The output matrix must be set to the required size before call.
Definition: ops_matrices.h:215
const GLubyte * c
Definition: glext.h:6313
CMatrixTemplate(size_t row=1, size_t col=1)
void resize(const CMatrixTemplateSize &siz, bool zeroNewElements=false)
This method just checks has no effects in this class, but raises an exception if the expected size do...
size_t rows() const
Number of rows in the matrix.
int val
Definition: mrpt_jpeglib.h:955
void * aligned_realloc(void *ptr, size_t size, size_t alignment)
const T & operator()(size_t row, size_t col) const
Subscript operator to get individual elements.
size_t cols() const
Number of columns in the matrix.
void set_unsafe(size_t row, size_t col, const T &v)
Fast but unsafe method to write a value in the matrix.
CMatrixBool(size_t row=1, size_t col=1)
Constructor.
Definition: CMatrixB.cpp:64
void getAsVector(std::vector< T > &out) const
Returns a vector containing the matrix&#39;s values.
_W64 int ptrdiff_t
Definition: glew.h:137
void extractRows(size_t firstRow, size_t lastRow, CMatrixTemplate< T > &out) const
Gets a series of contiguous rows.
CMatrixTemplateSize size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
const T * get_unsafe_row(size_t row) const
Fast but unsafe method to obtain a pointer to a given row of the matrix (Use only in critical applica...
This template class provides the basic functionality for a general 2D any-size, resizable container o...
void extractCol(size_t nCol, CMatrixTemplate< T > &out, int startingRow=0) const
Gets a given column to a vector (without modifying the matrix)
void extractColumns(size_t firstCol, size_t lastCol, CMatrixTemplate< T > &out) const
Gets a series of contiguous columns.
const GLdouble * v
Definition: glext.h:3678
void extractSubmatrix(const size_t row1, const size_t row2, const size_t col1, const size_t col2, EIGEN_MATRIX &out) const
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
void appendCol(const std::vector< T > &in)
Appends a new column to the matrix from a vector.
CMatrixTemplate(size_t row, size_t col, const V &theVector)
Constructor from a given size and a STL container (std::vector, std::list,...) with the initial value...
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:3576
void swap(CMatrixTemplate< T > &o)
Swap with another matrix very efficiently (just swaps a pointer and two integer values).
CMatrixTemplate & operator=(V(&theArray)[N])
Assignment operator for initializing from a C array (The matrix must be set to the correct size befor...
GLuint in
Definition: glext.h:7274
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
CMatrixTemplate(const CMatrixTemplate &m)
Constructors.
#define MRPT_COMPILE_TIME_ASSERT(expression)
Definition: exceptions.h:127
bool operator==(const CMatrixTemplateSize &o) const
CMatrixTemplate< T > operator()(const size_t row1, const size_t row2, const size_t col1, const size_t col2) const
Subscript operator to get a submatrix.
void realloc(size_t row, size_t col, bool newElementsToZero=false)
Internal use only: It reallocs the memory for the 2D matrix, maintaining the previous contents if pos...
Lightweight 3D point.
CMatrixTemplate & operator=(const CMatrixTemplate &m)
Assignment operator from another matrix.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
T operator()(size_t ith) const
Subscript operator to get/set an individual element from a row or column matrix.
void ASSERT_ENOUGHROOM(size_t r, size_t c) const
Checks whether the rows [r-N,r+N] and the columns [c-N,c+N] are present in the matrix.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
T & operator()(size_t ith)
Subscript operator to get/set an individual element from a row or column matrix.
CMatrixTemplateSize(const size_t *d)
void fillAll(const T &val)
void extractSubmatrix(const size_t row1, const size_t row2, const size_t col1, const size_t col2, CMatrixTemplate< T > &out) const
Get a submatrix, given its bounds.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020