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++)
119 for (
size_t c = 0; c <
m_Cols; c++)
120 newData[r *
m_Cols + c] = T();
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++)
132 newData[r *
m_Cols + c] = T();
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++));
258 (*
this)(r, c) = static_cast<T>(m(r, c));
266 template <
typename U>
276 template <
class Derived>
285 template <std::
size_t ROWS, std::
size_t COLS>
306 template <
typename V,
size_t N>
309 static_assert(N != 0,
"Empty array!");
313 "Mismatch between matrix size %lu x %lu and array of " 318 for (
size_t i = 0; i <
m_Rows; i++)
319 for (
size_t j = 0; j <
m_Cols; j++)
320 (*
this)(i, j) = static_cast<T>(theArray[idx++]);
330 m_data = std::move(m.m_data);
364 setSize(siz[0], siz[1], zeroNewElements);
391 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 394 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
395 static_cast<unsigned long>(
row),
396 static_cast<unsigned long>(
col),
397 static_cast<unsigned long>(
m_Rows),
398 static_cast<unsigned long>(
m_Cols)));
407 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 410 "Indexes (%lu,%lu) out of range. Matrix is %lux%lu",
411 static_cast<unsigned long>(
row),
412 static_cast<unsigned long>(
col),
413 static_cast<unsigned long>(
m_Rows),
414 static_cast<unsigned long>(
m_Cols)));
425 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 431 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 434 "Index %u out of range!", static_cast<unsigned>(ith));
440 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 443 "Index %u out of range!", static_cast<unsigned>(ith));
456 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 476 template <
typename VECTOR>
485 for (
size_t i = 0; i <
m_Cols; i++) (*
this)(
row, i) = in[i];
488 template <
typename VECTOR>
492 for (
Index c = 0; c <
cols(); c++) (*
this)(
row, c) = v[c];
495 template <
typename VECTOR>
499 for (
Index r = 0; r <
rows(); r++) (*
this)(r,
col) = v[r];
509 template <
typename VECTOR>
522 for (
size_t i = 0; i <
m_Rows; i++) (*
this)(i,
m_Cols - 1) = in[i];
527 template <
typename VECTOR>
532 for (
const auto& d :
m_data)
out.push_back(d);
537 typename EIGEN_MATRIX =
eigen_t,
543 std::is_same_v<EIGEN_MATRIX, eigen_t>,
544 "Please, do not override the default template arguments of this " 550 typename EIGEN_MATRIX =
eigen_t,
556 std::is_same_v<EIGEN_MATRIX, eigen_t>,
557 "Please, do not override the default template arguments of this " 602 #ifdef HAVE_LONG_DOUBLE 620 template <
typename T>
static constexpr int SizeAtCompileTime
A compile-time fixed-size numeric matrix container.
void resize(size_t row, size_t col)
Trivially copiable underlying data for TPoint3D 1-byte memory packed, no padding].
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)
std::string std::string format(std::string_view fmt, ARGS &&... args)
const T * data() const
Return raw pointer to row-major data buffer.
#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)
CMatrixDynamic & operator=(CMatrixDynamic &&m)
Move operator.
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.
T 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.
mrpt::vision::TStereoCalibResults out
matrix_size_t size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
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(CMatrixDynamic &&m)
Move ctor.
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.