33 #ifndef GSLAM_MATRIX_H 34 #define GSLAM_MATRIX_H 45 template<
typename Scalar,
size_t Rows,
size_t Cols>
49 Scalar _data[Rows][Cols] {};
53 Matrix(
const std::array<Scalar,Rows*Cols>& c){
54 memcpy(_data, c.data(),
sizeof(_data));
57 Matrix(
const Scalar data_[Rows*Cols])
59 memcpy(_data, data_,
sizeof(_data));
62 Matrix(
const Scalar data_[Rows][Cols])
64 memcpy(_data, data_,
sizeof(_data));
69 memcpy(_data, other._data,
sizeof(_data));
89 const Scalar *data()
const 94 inline Scalar operator()(
size_t i,
size_t j)
const 99 inline Scalar &operator()(
size_t i,
size_t j)
106 if (
this != &other) {
107 memcpy(_data, other._data,
sizeof(_data));
112 void copyTo(Scalar (&dst)[Rows*Cols])
const 114 memcpy(dst, _data,
sizeof(dst));
117 void copyToRaw(Scalar *dst)
const 119 memcpy(dst, _data,
sizeof(_data));
122 void copyToColumnMajor(Scalar (&dst)[Rows*Cols])
const 126 for (
size_t i = 0; i < Rows; i++) {
127 for (
size_t j = 0; j < Cols; j++) {
128 dst[i+(j*Rows)] =
self(i, j);
140 for (
size_t i = 0; i < Rows; i++) {
141 for (
size_t k = 0; k < P; k++) {
142 for (
size_t j = 0; j < Cols; j++) {
143 res(i, k) +=
self(i, j) * other(j, k);
156 for (
size_t i = 0; i < Rows; i++) {
157 for (
size_t j = 0; j < Cols; j++) {
158 res(i, j) =
self(i, j)*other(i, j);
170 for (
size_t i = 0; i < Rows; i++) {
171 for (
size_t j = 0; j < Cols; j++) {
172 res(i, j) =
self(i, j)/other(i, j);
184 for (
size_t i = 0; i < Rows; i++) {
185 for (
size_t j = 0; j < Cols; j++) {
186 res(i, j) =
self(i, j) + other(i, j);
198 for (
size_t i = 0; i < Rows; i++) {
199 for (
size_t j = 0; j < Cols; j++) {
200 res(i, j) =
self(i, j) - other(i, j);
212 for (
size_t i = 0; i < Rows; i++) {
213 for (
size_t j = 0; j < Cols; j++) {
214 res(i, j) = -
self(i, j);
245 for (
size_t i = 0; i < Rows; i++) {
246 for (
size_t j = 0; j < Cols; j++) {
247 res(i, j) =
self(i, j) * scalar;
256 return (*
this)*(1/scalar);
264 for (
size_t i = 0; i < Rows; i++) {
265 for (
size_t j = 0; j < Cols; j++) {
266 res(i, j) =
self(i, j) + scalar;
275 return (*
this) + (-1*scalar);
278 void operator*=(Scalar scalar)
282 for (
size_t i = 0; i < Rows; i++) {
283 for (
size_t j = 0; j < Cols; j++) {
284 self(i, j) =
self(i, j) * scalar;
289 void operator/=(Scalar scalar)
292 self =
self * (1.0f / scalar);
295 inline void operator+=(Scalar scalar)
297 *
this = (*this) + scalar;
300 inline void operator-=(Scalar scalar)
302 *
this = (*this) - scalar;
310 static constexpr Scalar eps = std::numeric_limits<Scalar>::epsilon();
312 for (
size_t i = 0; i < Rows; i++) {
313 for (
size_t j = 0; j < Cols; j++) {
314 if (fabs(
self(i, j) - other(i, j)) > eps) {
326 return !(
self == other);
330 for (
size_t i = 0; i < Rows; i++) {
331 for (
size_t j = 0; j < Cols; j++) {
332 if (fabs((*
this)(i, j) - y(i, j)) > eps) {
345 for (
size_t i = 0; i < Rows; i++) {
346 for (
size_t j = 0; j < Cols; j++) {
347 res(j, i) =
self(i, j);
360 template<
size_t P,
size_t Q>
367 template<
size_t P,
size_t Q>
371 for (
size_t i = 0; i < P; i++) {
372 for (
size_t j = 0; j < Q; j++) {
373 self(i + x0, j + y0) = m(i, j);
381 for (
size_t j = 0; j < Cols; j++) {
382 self(i, j) = row(j, 0);
389 for (
size_t i = 0; i < Rows; i++) {
390 self(i, j) = col(i, 0);
396 memset(_data, 0,
sizeof(_data));
404 void setAll(Scalar val)
408 for (
size_t i = 0; i < Rows; i++) {
409 for (
size_t j = 0; j < Cols; j++) {
425 for (
size_t i = 0; i < Rows && i < Cols; i++) {
430 inline void identity()
435 inline void swapRows(
size_t a,
size_t b)
443 for (
size_t j = 0; j < Cols; j++) {
444 Scalar tmp =
self(a, j);
445 self(a, j) =
self(b, j);
450 inline void swapCols(
size_t a,
size_t b)
458 for (
size_t i = 0; i < Rows; i++) {
459 Scalar tmp =
self(i, a);
460 self(i, a) =
self(i, b);
468 for (
size_t i=0; i<Rows; i++) {
469 for (
size_t j=0; j<Cols; j++) {
470 r(i,j) = Scalar(fabs((*
this)(i,j)));
478 Scalar max_val = (*this)(0,0);
479 for (
size_t i=0; i<Rows; i++) {
480 for (
size_t j=0; j<Cols; j++) {
481 Scalar val = (*this)(i,j);
492 Scalar min_val = (*this)(0,0);
493 for (
size_t i=0; i<Rows; i++) {
494 for (
size_t j=0; j<Cols; j++) {
495 Scalar val = (*this)(i,j);
504 int rows()
const {
return Rows;}
506 int cols()
const {
return Cols;}
508 #ifdef EIGEN_MATRIX_H 509 operator Eigen::Matrix<Scalar,Rows,Cols,Eigen::ColMajor>()
511 return Eigen::Map<Eigen::Matrix<Scalar,Rows,Cols,Eigen::RowMajor> >(_data[0]);
514 Matrix(
const Eigen::Matrix<Scalar,Rows,Cols,Eigen::ColMajor>& eigen)
516 Eigen::Matrix<Scalar,Rows,Cols,Eigen::RowMajor> rowM=eigen;
517 memcpy(_data,rowM.data(),
sizeof(_data));
520 Matrix(
const Eigen::Matrix<Scalar,Rows,Cols,Eigen::RowMajor>& eigen)
522 memcpy(_data,eigen.data(),
sizeof(_data));
527 template<
typename Scalar,
size_t Rows,
size_t Cols>
528 std::ostream& operator<<(std::ostream& os,const Matrix<Scalar, Rows, Cols>& matrix)
531 for (
size_t i = 0; i < Rows; ++i) {
532 for (
size_t j = 0; j < Cols; ++j) {
535 << ((j<Cols-1)?
" ":
"\n");
541 template<
typename Scalar,
size_t Rows,
size_t Cols>
544 return other * scalar;
547 template<
typename Scalar,
size_t Rows>
555 Vector(
const MatrixM1 & other) :
560 Vector(
const Scalar data_[Rows]) :
565 inline Scalar operator()(
size_t i)
const 567 const MatrixM1 &v = *
this;
571 inline Scalar &operator()(
size_t i)
577 inline Scalar operator[](
size_t i)
const 579 const MatrixM1 &v = *
this;
583 inline Scalar &operator[](
size_t i)
589 Scalar dot(
const MatrixM1 & b)
const {
592 for (
size_t i = 0; i<Rows; i++) {
598 inline Scalar operator*(
const MatrixM1 & b)
const {
603 inline Vector operator*(Scalar b)
const {
604 return Vector(MatrixM1::operator*(b));
607 Scalar norm()
const {
609 return Scalar(sqrt(a.dot(a)));
612 Scalar norm_squared()
const {
617 inline Scalar length()
const {
621 inline void normalize() {
626 return (*
this) / norm();
629 Vector unit_or_zero(
const Scalar eps = Scalar(1e-5)) {
630 const Scalar n = norm();
637 inline Vector normalized()
const {
641 Vector pow(Scalar v)
const {
644 for (
size_t i = 0; i<Rows; i++) {
645 r(i) = Scalar(::pow(a(i), v));
651 template<
typename Scalar>
661 Vector2(
const Matrix21 & other) :
666 Vector2(
const Scalar data_[2]) :
678 explicit Vector2(
const Vector3 & other)
685 Scalar cross(
const Matrix21 & b)
const {
687 return a(0)*b(1, 0) - a(1)*b(0, 0);
690 Scalar operator^(
const Matrix21 & b)
const {
691 return (*this).cross(b);
696 template<
typename Scalar>
705 Vector3(
const Matrix31 & other) :
710 Vector3(
const Scalar data_[3]) :
715 Vector3(Scalar x, Scalar y, Scalar z) {
722 Vector3 cross(
const Matrix31 & b)
const {
725 c(0) = a(1)*b(2,0) - a(2)*b(1,0);
726 c(1) = -a(0)*b(2,0) + a(2)*b(0,0);
727 c(2) = a(0)*b(1,0) - a(1)*b(0,0);
733 return Matrix31::operator+(other);
738 return Matrix31::operator-(other);
741 inline Vector3 operator-()
const 743 return Matrix31::operator-();
746 inline Vector3 operator*(Scalar scalar)
const 748 return Matrix31::operator*(scalar);
751 inline Scalar operator*(
Vector3 b)
const 756 inline Vector3 operator^(
const Matrix31 & b)
const {
757 return (*this).cross(b);
767 inline Vector3 normalized()
const {
Vector3 unit() const
Override vector ops so Vector3 type is returned.
Definition: Matrix.h:763