24 using Base = std::array<size_t, 2>;
44 inline operator size_t(
void)
const {
return 2; }
88 void realloc(
size_t row,
size_t col,
bool newElementsToZero =
false)
93 bool doZeroColumns = newElementsToZero && (col >
m_Cols);
94 size_t sizeZeroColumns =
sizeof(T) * (col -
m_Cols);
106 m_Val =
static_cast<T**
>(
110 size_t row_size = col *
sizeof(T);
119 m_Val[
r] =
static_cast<T*
>(
133 ::memset(
m_Val[
r], 0, row_size);
150 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 188 const size_t cropColCount)
193 realloc(cropRowCount, cropColCount);
194 for (
size_t i = 0; i <
m_Rows; i++)
207 template <
typename V,
size_t N>
216 "Mismatch between matrix size %lu x %lu and array of " 218 static_cast<long unsigned>(
m_Rows),
219 static_cast<long unsigned>(
m_Cols),
220 static_cast<long unsigned>(N)))
222 for (
size_t i = 0; i <
m_Rows; i++)
223 for (
size_t j = 0; j <
m_Cols; j++)
224 m_Val[i][j] = static_cast<T>(theArray[idx++]);
231 template <
typename V>
235 const size_t N = theVector.size();
240 "Mismatch between matrix size %lu x %lu and array of " 242 static_cast<long unsigned>(
m_Rows),
243 static_cast<long unsigned>(
m_Cols),
244 static_cast<long unsigned>(N)))
246 for (
size_t i = 0; i <
m_Rows; i++)
247 for (
size_t j = 0; j <
m_Cols; j++)
248 m_Val[i][j] = static_cast<T>(*(it++));
257 for (
size_t i = 0; i <
m_Rows; i++)
274 template <
typename V,
size_t N>
282 "Mismatch between matrix size %lu x %lu and array of " 287 for (
size_t i = 0; i <
m_Rows; i++)
288 for (
size_t j = 0; j <
m_Cols; j++)
289 m_Val[i][j] = static_cast<T>(theArray[idx++]);
311 void setSize(
size_t row,
size_t col,
bool zeroNewElements =
false)
321 setSize(siz[0], siz[1], zeroNewElements);
328 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 332 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
333 static_cast<unsigned long>(
row),
334 static_cast<unsigned long>(col),
335 static_cast<unsigned long>(
m_Rows),
336 static_cast<unsigned long>(
m_Cols)));
345 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 349 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
350 static_cast<unsigned long>(
row),
351 static_cast<unsigned long>(col),
352 static_cast<unsigned long>(
m_Rows),
353 static_cast<unsigned long>(
m_Cols)));
364 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 370 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 373 "Index %u out of range!", static_cast<unsigned>(ith));
375 return m_Val[0][ith];
380 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 383 "Index %u out of range!", static_cast<unsigned>(ith));
385 return m_Val[ith][0];
395 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 401 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 404 "Index %u out of range!", static_cast<unsigned>(ith));
406 return m_Val[0][ith];
411 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 414 "Index %u out of range!", static_cast<unsigned>(ith));
416 return m_Val[ith][0];
428 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
429 static_cast<unsigned long>(
row),
430 static_cast<unsigned long>(col),
431 static_cast<unsigned long>(
m_Rows),
432 static_cast<unsigned long>(
m_Cols)));
445 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
446 static_cast<unsigned long>(
row),
447 static_cast<unsigned long>(col),
448 static_cast<unsigned long>(
m_Rows),
449 static_cast<unsigned long>(
m_Cols)));
462 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
463 static_cast<unsigned long>(
row),
464 static_cast<unsigned long>(col),
465 static_cast<unsigned long>(
m_Rows),
466 static_cast<unsigned long>(
m_Cols)));
480 "Row index %lu out of range. Matrix is %lux%lu",
481 static_cast<unsigned long>(
row),
482 static_cast<unsigned long>(
m_Rows),
483 static_cast<unsigned long>(
m_Cols)));
495 const size_t row1,
const size_t row2,
const size_t col1,
496 const size_t col2)
const 507 const size_t row1,
const size_t row2,
const size_t col1,
510 int nrows = int(row2) - int(row1) + 1;
511 int ncols = int(col2) - int(col1) + 1;
512 if (nrows <= 0 || ncols <= 0)
520 for (
int i = 0; i < nrows; i++)
521 for (
int j = 0; j < ncols; j++)
525 template <
class EIGEN_MATRIX>
527 const size_t row1,
const size_t row2,
const size_t col1,
528 const size_t col2, EIGEN_MATRIX& out)
const 530 int nrows = int(row2) - int(row1) + 1;
531 int ncols = int(col2) - int(col1) + 1;
532 if (nrows <= 0 || ncols <= 0)
534 out =
typename EIGEN_MATRIX::PlainObject();
539 out.resize(nrows, ncols);
540 for (
int i = 0; i < nrows; i++)
541 for (
int j = 0; j < ncols; j++)
542 out.coeffRef(i, j) =
m_Val[i + row1][j + col1];
572 void extractCol(
size_t nCol, std::vector<T>& out,
int startingRow = 0)
const 575 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 583 for (i = 0; i <
n; i++) out[i] =
m_Val[i + startingRow][nCol];
593 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 601 for (i = 0; i <
n; i++) out(i, 0) =
m_Val[i + startingRow][nCol];
672 size_t n =
in.size();
675 for (
size_t i = 0; i <
n; i++)
m_Val[i][nCol] =
in[i];
684 for (
size_t i = 0; i <
m_Rows; i++)
std::ptrdiff_t difference_type
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.
#define THROW_EXCEPTION(msg)
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...
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
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't exist already.
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.
This base provides a set of functions for maths stuff.
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...
size_t rows() const
Number of rows in the matrix.
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.
void getAsVector(std::vector< T > &out) const
Returns a vector containing the matrix's values.
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.
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...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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...
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)
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...
CMatrixTemplate & operator=(const CMatrixTemplate &m)
Assignment operator from another matrix.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
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
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.