Go to the documentation of this file.
9 #ifndef CMatrixTemplate_H
10 #define CMatrixTemplate_H
56 void realloc(
size_t row,
size_t col,
bool newElementsToZero =
false)
61 bool doZeroColumns = newElementsToZero && (col>
m_Cols);
62 size_t sizeZeroColumns =
sizeof(T)*(col-
m_Cols);
74 size_t row_size = col *
sizeof(T);
107 #if defined(_DEBUG)||(MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
113 for (
size_t r=0;r<
m_Rows;r++)
114 for (
size_t c=0;c<
m_Cols;c++)
143 realloc( cropRowCount, cropColCount );
144 for (
size_t i=0; i <
m_Rows; i++)
145 for (
size_t j=0; j <
m_Cols; j++)
157 template <
typename V,
size_t N>
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++]);
171 template <
typename V>
174 const size_t N = theVector.size();
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++) );
190 for (
size_t i=0; i <
m_Rows; i++)
191 for (
size_t j=0; j <
m_Cols; j++)
206 template <
typename V,
size_t N>
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++]);
241 void setSize(
size_t row,
size_t col,
bool zeroNewElements=
false)
243 realloc(row,col,zeroNewElements);
249 setSize(siz[0],siz[1],zeroNewElements);
256 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
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)) );
260 return m_Val[row][col];
267 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
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)) );
271 return m_Val[row][col];
279 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
285 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
289 return m_Val[0][ith];
294 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
298 return m_Val[ith][0];
307 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
313 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
317 return m_Val[0][ith];
322 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
326 return m_Val[ith][0];
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)) );
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)) );
349 return m_Val[row][col];
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)) );
360 return m_Val[row][col];
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)) );
393 int nrows=int(row2)-int(row1)+1;
394 int ncols=int(col2)-int(col1)+1;
395 if (nrows<=0||ncols<=0) {
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];
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
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();
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];
442 void extractCol(
size_t nCol, std::vector<T> &out,
int startingRow = 0)
const
445 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
454 out[i] =
m_Val[i+startingRow][nCol];
463 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
472 out(i,0) =
m_Val[i+startingRow][nCol];
508 m_Val[row][i] = in[i];
536 size_t n = in.size();
539 for (
size_t i=0;i<n;i++)
540 m_Val[i][nCol] = in[i];
void fillAll(const T &val)
#define THROW_EXCEPTION(msg)
CMatrixTemplate(size_t row, size_t col, V(&theArray)[N])
Constructor from a given size and a C array.
CMatrixTemplate & operator=(const CMatrixTemplate &m)
Assignment operator from another matrix.
void BASE_IMPEXP * aligned_realloc(void *old_ptr, size_t bytes, size_t alignment)
Frees a memory block reserved by aligned_malloc.
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...
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
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...
std::ptrdiff_t difference_type
Declares a matrix of booleans (non serializable).
size_t getColCount() const
Number of columns in the matrix.
const T & get_unsafe(size_t row, size_t col) const
Fast but unsafe method to read a value from the matrix.
Auxiliary class used in CMatrixTemplate:size(), CMatrixTemplate::resize(), CMatrixFixedNumeric::size(...
void insertCol(size_t nCol, const std::vector< T > &in)
Inserts a column from a vector, replacing the current contents of that column.
This template class provides the basic functionality for a general 2D any-size, resizable container o...
CMatrixTemplate(const CMatrixTemplate &m)
Constructors.
CMatrixTemplate(size_t row=1, size_t col=1)
void * aligned_calloc(size_t bytes, size_t alignment)
Identical to aligned_malloc, but it zeroes the reserved memory block.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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 typedef Scalar * const_iterator
void getAsVector(std::vector< T > &out) const
Returns a vector containing the matrix's values.
T & get_unsafe(size_t row, size_t col)
Fast but unsafe method to get a reference from 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.
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.
T & operator()(size_t row, size_t col)
Subscript operator to get/set individual elements.
#define MRPT_COMPILE_TIME_ASSERT(f)
void BASE_IMPEXP aligned_free(void *p)
Frees a memory block reserved by aligned_malloc.
void extractColumns(size_t firstCol, size_t lastCol, CMatrixTemplate< T > &out) const
Gets a series of contiguous columns.
const typedef T & const_reference
void appendRow(const std::vector< T > &in)
Appends a new row to the MxN matrix from a 1xN vector.
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...
T value_type
The type of the matrix elements.
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 appendCol(const std::vector< T > &in)
Appends a new column to the matrix from a vector.
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...
CMatrixTemplate(const CMatrixTemplate &m, const size_t cropRowCount, const size_t cropColCount)
Copy constructor & crop from another matrix.
void extractRows(size_t firstRow, size_t lastRow, CMatrixTemplate< T > &out) const
Gets a series of contiguous rows.
size_t getRowCount() const
Number of rows in the matrix.
void extractCol(size_t nCol, CMatrixTemplate< T > &out, int startingRow=0) const
Gets a given column to a vector (without modifying the 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.
virtual ~CMatrixTemplate()
Destructor.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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.
CMatrixTemplateSize size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
void swap(CMatrixTemplate< T > &o)
Swap with another matrix very efficiently (just swaps a pointer and two integer values).
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)
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...
Page generated by Doxygen 1.8.16 for MRPT 1.4.0 SVN: at Mon Oct 14 23:08:25 UTC 2019 | | |