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