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