9 #ifndef CMatrixTemplate_H 10 #define CMatrixTemplate_H 27 typedef std::array<size_t, 2>
Base;
47 inline operator size_t(
void)
const {
return 2; }
91 void realloc(
size_t row,
size_t col,
bool newElementsToZero =
false)
96 bool doZeroColumns = newElementsToZero && (col >
m_Cols);
97 size_t sizeZeroColumns =
sizeof(T) * (col -
m_Cols);
110 m_Val =
static_cast<T**
>(
115 size_t row_size = col *
sizeof(T);
124 m_Val[
r] =
static_cast<T*
>(
137 m_Val[
r] =
static_cast<T*
>(
155 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 195 const size_t cropColCount)
200 realloc(cropRowCount, cropColCount);
201 for (
size_t i = 0; i <
m_Rows; i++)
214 template <
typename V,
size_t N>
223 "Mismatch between matrix size %lu x %lu and array of " 225 static_cast<long unsigned>(
m_Rows),
226 static_cast<long unsigned>(
m_Cols),
227 static_cast<long unsigned>(N)))
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++]);
238 template <
typename V>
242 const size_t N = theVector.size();
247 "Mismatch between matrix size %lu x %lu and array of " 249 static_cast<long unsigned>(
m_Rows),
250 static_cast<long unsigned>(
m_Cols),
251 static_cast<long unsigned>(N)))
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++));
264 for (
size_t i = 0; i <
m_Rows; i++)
281 template <
typename V,
size_t N>
289 "Mismatch between matrix size %lu x %lu and array of " 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++]);
318 void setSize(
size_t row,
size_t col,
bool zeroNewElements =
false)
328 setSize(siz[0], siz[1], zeroNewElements);
335 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 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)));
352 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 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)));
371 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 377 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 380 "Index %u out of range!", static_cast<unsigned>(ith));
382 return m_Val[0][ith];
387 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 390 "Index %u out of range!", static_cast<unsigned>(ith));
392 return m_Val[ith][0];
402 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 408 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 411 "Index %u out of range!", static_cast<unsigned>(ith));
413 return m_Val[0][ith];
418 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 421 "Index %u out of range!", static_cast<unsigned>(ith));
423 return m_Val[ith][0];
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)));
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)));
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)));
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)));
502 const size_t row1,
const size_t row2,
const size_t col1,
503 const size_t col2)
const 514 const size_t row1,
const size_t row2,
const size_t col1,
517 int nrows = int(row2) - int(row1) + 1;
518 int ncols = int(col2) - int(col1) + 1;
519 if (nrows <= 0 || ncols <= 0)
527 for (
int i = 0; i < nrows; i++)
528 for (
int j = 0; j < ncols; j++)
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 537 int nrows = int(row2) - int(row1) + 1;
538 int ncols = int(col2) - int(col1) + 1;
539 if (nrows <= 0 || ncols <= 0)
541 out =
typename EIGEN_MATRIX::PlainObject();
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];
579 void extractCol(
size_t nCol, std::vector<T>& out,
int startingRow = 0)
const 582 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 590 for (i = 0; i <
n; i++) out[i] =
m_Val[i + startingRow][nCol];
600 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 608 for (i = 0; i <
n; i++) out(i, 0) =
m_Val[i + startingRow][nCol];
679 size_t n =
in.size();
682 for (
size_t i = 0; i <
n; i++)
m_Val[i][nCol] =
in[i];
691 for (
size_t i = 0; i <
m_Rows; i++)
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.
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.
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
std::ptrdiff_t difference_type
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.
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't exist already.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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.
CMatrixTemplateSize mrpt_autotype
void getAsVector(std::vector< T > &out) const
Returns a vector containing the matrix's values.
void * aligned_calloc(size_t bytes, size_t alignment)
Identical to aligned_malloc, but it zeroes the reserved memory block.
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.
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
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
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...
size_t getColCount() const
Number of columns in the matrix.
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.
const T & const_reference
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.
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.