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



Page generated by Doxygen 1.9.7 for MRPT 1.4.0 SVN: at Tue Jun 13 13:45:58 UTC 2023