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