20 #include <type_traits> 98 const auto nRowsToCopy =
m_Rows >= old_rows ? old_rows :
m_Rows;
99 const auto nColsToCopy =
m_Cols >= old_cols ? old_cols :
m_Cols;
100 for (
size_t r = 0;
r < nRowsToCopy;
r++)
102 if constexpr (std::is_trivial_v<T>)
105 sizeof(T) * nColsToCopy);
107 for (
size_t c = 0;
c < nColsToCopy;
c++)
111 if (newElementsToZero &&
m_Rows > old_rows)
113 if constexpr (std::is_trivial_v<T>)
115 &newData[old_rows *
m_Cols], 0,
116 sizeof(T) * (
m_Rows - old_rows));
118 for (
size_t r = old_rows;
r <
m_Rows;
r++)
123 if (newElementsToZero &&
m_Cols > old_cols)
125 for (
size_t r = 0;
r < old_rows;
r++)
126 if constexpr (std::is_trivial_v<T>)
128 &newData[
r *
m_Cols + old_cols], 0,
129 sizeof(T) * (
m_Cols - old_cols));
131 for (
size_t c = old_cols;
c <
m_Cols;
c++)
154 template <
typename U>
161 template <
class Derived>
168 template <
typename _Lhs,
typename _Rhs,
int Option>
174 template <
typename Op,
typename Lhs,
typename Rhs>
184 const size_t cropColCount)
188 realloc(cropRowCount, cropColCount);
189 for (
size_t i = 0; i <
m_Rows; i++)
190 for (
size_t j = 0; j <
m_Cols; j++) (*
this)(i, j) = m(i, j);
194 template <std::
size_t ROWS, std::
size_t COLS>
209 template <
typename V,
size_t N>
212 static_assert(N != 0,
"Empty array!");
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 (*
this)(i, j) = static_cast<T>(theArray[idx++]);
231 template <
typename V>
234 const size_t N = theVector.size();
238 "Mismatch between matrix size %lu x %lu and array of " 240 static_cast<long unsigned>(
m_Rows),
241 static_cast<long unsigned>(
m_Cols),
242 static_cast<long unsigned>(N)));
243 typename V::const_iterator it = theVector.begin();
244 for (
size_t i = 0; i <
m_Rows; i++)
245 for (
size_t j = 0; j <
m_Cols; j++)
246 (*
this)(i, j) = static_cast<T>(*(it++));
265 template <
typename U>
275 template <
class Derived>
284 template <std::
size_t ROWS, std::
size_t COLS>
305 template <
typename V,
size_t N>
308 static_assert(N != 0,
"Empty array!");
312 "Mismatch between matrix size %lu x %lu and array of " 317 for (
size_t i = 0; i <
m_Rows; i++)
318 for (
size_t j = 0; j <
m_Cols; j++)
319 (*
this)(i, j) = static_cast<T>(theArray[idx++]);
351 setSize(siz[0], siz[1], zeroNewElements);
363 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 366 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
367 static_cast<unsigned long>(
row),
368 static_cast<unsigned long>(
col),
369 static_cast<unsigned long>(
m_Rows),
370 static_cast<unsigned long>(
m_Cols)));
379 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 382 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
383 static_cast<unsigned long>(
row),
384 static_cast<unsigned long>(
col),
385 static_cast<unsigned long>(
m_Rows),
386 static_cast<unsigned long>(
m_Cols)));
397 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 403 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 406 "Index %u out of range!", static_cast<unsigned>(ith));
412 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 415 "Index %u out of range!", static_cast<unsigned>(ith));
428 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 448 template <
typename VECTOR>
457 for (
size_t i = 0; i <
m_Cols; i++) (*
this)(
row, i) =
in[i];
460 template <
typename VECTOR>
467 template <
typename VECTOR>
481 template <
typename VECTOR>
494 for (
size_t i = 0; i <
m_Rows; i++) (*
this)(i,
m_Cols - 1) =
in[i];
499 template <
typename VECTOR>
504 for (
const auto& d :
m_data) out.push_back(d);
509 typename EIGEN_MATRIX =
eigen_t,
515 std::is_same_v<EIGEN_MATRIX, eigen_t>,
516 "Please, do not override the default template arguments of this " 522 typename EIGEN_MATRIX =
eigen_t,
528 std::is_same_v<EIGEN_MATRIX, eigen_t>,
529 "Please, do not override the default template arguments of this " 568 #ifdef HAVE_LONG_DOUBLE 586 template <
typename T>
static constexpr int SizeAtCompileTime
A compile-time fixed-size numeric matrix container.
const KFTYPE & const_reference
void resize(size_t row, size_t col)
iteratorImpl< T, const T *, const T &> const_iterator
void setCol(const Index col, const VECTOR &v)
Template for column vectors of dynamic size, compatible with Eigen.
#define THROW_EXCEPTION(msg)
#define ASSERT_BELOW_(__A, __B)
CMatrixDynamic & operator=(const CMatrixDynamic< U > &m)
Assignment operator from another matrix (possibly of a different type)
const_iterator end() const
void swap(CMatrixDynamic< T > &o)
Swap with another matrix very efficiently (just swaps a pointer and two integer values).
static constexpr int ColsAtCompileTime
static constexpr int StorageOrder
void resize(const matrix_size_t &siz, bool zeroNewElements=false)
Resize the matrix.
void setRow(const Index row, const VECTOR &v)
CMatrixDynamic< double > cast_double() const
const_iterator begin() const
vec_t m_data
RowMajor matrix data.
CVectorDynamic< Scalar > lu_solve(const CVectorDynamic< Scalar > &b) const
Solves the linear system Ax=b, returns x, with A this asymmetric matrix.
const_iterator cend() const
#define ASSERT_(f)
Defines an assertion mechanism.
CMatrixDynamic & operator=(V(&theArray)[N])
Assignment operator for initializing from a C array (The matrix must be set to the correct size befor...
void setFromMatrixLike(const MAT &m)
static constexpr size_t small_size
This base provides a set of functions for maths stuff.
std::ptrdiff_t difference_type
Eigen::Matrix< T, RowsAtCompileTime, ColsAtCompileTime, StorageOrder, RowsAtCompileTime, ColsAtCompileTime > eigen_t
static constexpr int RowsAtCompileTime
void conservativeResize(size_t row, size_t col)
iterator begin() noexcept
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
const CMatrixDynamic & derived() const
CMatrixDynamic(const CMatrixDynamic &m, const size_t cropRowCount, const size_t cropColCount)
Copy constructor & crop from another matrix.
CMatrixDynamic & derived()
typename vec_t::const_iterator const_iterator
void appendCol(const VECTOR &in)
Appends a new column to the matrix from a vector.
CMatrixDynamic & operator=(const CMatrixDynamic< T > &m)=default
CMatrixDynamic & operator=(const CMatrixFixed< T, ROWS, COLS > &m)
Assignment from a fixed matrix.
const T & operator[](size_t ith) const
Subscript operator to get/set an individual element from a row or column matrix.
KFTYPE value_type
The type of the matrix elements.
void asVector(VECTOR &out) const
Returns a vector containing the matrix's values.
size_type rows() const
Number of rows in the matrix.
CMatrixDynamic(const CMatrixDynamic &m)
Constructors.
size_type cols() const
Number of columns in the matrix.
T & operator[](size_t ith)
Subscript operator to get/set an individual element from a row or column matrix.
CMatrixDynamic(const Eigen::MatrixBase< Derived > &m)
Convert from Eigen matrix.
CMatrixDynamic(const CMatrixDynamic< U > &m)
Copy (casting from if needed) from another matrix.
CMatrixDynamic(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...
EIGEN_MAP asEigen() const
Base CRTP class for all MRPT matrices.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
matrix_size_t size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
GLenum GLenum GLvoid * row
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
virtual ~CMatrixDynamic()=default
void resize(size_t vectorLen)
Resizes as a Nx1 vector.
CMatrixDynamic(const Eigen::Product< _Lhs, _Rhs, Option > &p)
Convert from Eigen product.
CMatrixDynamic & operator=(const Eigen::MatrixBase< Derived > &m)
Assignment from an Eigen matrix.
typename vec_t::iterator iterator
CVectorDynamic< Scalar > llt_solve(const CVectorDynamic< Scalar > &b) const
Solves the linear system Ax=b, returns x, with A this symmetric matrix.
CMatrixDynamic< float > cast_float() const
CMatrixDynamic(size_t row, size_t col, V(&theArray)[N])
Constructor from a given size and a C array.
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...
CMatrixDynamic(const CMatrixFixed< T, ROWS, COLS > &o)
Constructor from fixed-size matrix:
const T & operator()(size_t row, size_t col) const
Subscript operator to get individual elements.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
EIGEN_MAP asEigen()
Get as an Eigen-compatible Eigen::Map object.
CMatrixDynamic(const Eigen::CwiseBinaryOp< Op, Lhs, Rhs > &p)
Convert from Eigen binary op.
const_iterator cbegin() const
This template class provides the basic functionality for a general 2D any-size, resizable container o...
Auxiliary class used in CMatrixDynamic:size(), CMatrixDynamic::resize(), CMatrixFixed::size(), CMatrixFixed::resize(), to mimic the behavior of STL-containers.
static constexpr int is_mrpt_type
T & operator()(size_t row, size_t col)
Subscript operator to get/set individual elements.
iteratorImpl< T, T *, T &> iterator
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
CMatrixDynamic(size_t row=0, size_t col=0)
void appendRow(const VECTOR &in)
Appends a new row to the MxN matrix from a 1xN vector.