diff --git a/matrix/Dcm.hpp b/matrix/Dcm.hpp index 62bac589f7..0e1cf49960 100644 --- a/matrix/Dcm.hpp +++ b/matrix/Dcm.hpp @@ -46,10 +46,7 @@ public: * * Initializes to identity */ - Dcm() - { - (*this) = eye(); - } + Dcm() : SquareMatrix(eye()) {} /** * Constructor from array diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index bc9dbfc7a5..2b865f4ab3 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -18,14 +18,6 @@ #include "math.hpp" -// There is a bug in GCC 4.8, which causes the compiler to segfault due to array {} constructors. -// Do for-loop constructors just for GCC 4.8 -#ifdef __GNUC__ -#define MATRIX_GCC_4_8_WORKAROUND (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)) -#else -#define MATRIX_GCC_4_8_WORKAROUND 0 -#endif - namespace matrix { @@ -41,41 +33,38 @@ class Slice; template class Matrix { -#if MATRIX_GCC_4_8_WORKAROUND - Type _data[M][N]; -#else Type _data[M][N] {}; -#endif public: // Constructors -#if MATRIX_GCC_4_8_WORKAROUND - Matrix() - { - for (size_t i = 0; i < M; i++) { - for (size_t j = 0; j < N; j++) { - _data[i][j] = Type{}; - } - } - } -#else Matrix() = default; -#endif explicit Matrix(const Type data_[M*N]) { - memcpy(_data, data_, sizeof(_data)); + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + _data[i][j] = data_[N*i + j]; + } + } } explicit Matrix(const Type data_[M][N]) { - memcpy(_data, data_, sizeof(_data)); + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + _data[i][j] = data_[i][j]; + } + } } Matrix(const Matrix &other) { - memcpy(_data, other._data, sizeof(_data)); + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + _data[i][j] = other(i, j); + } + } } template @@ -113,14 +102,24 @@ public: Matrix & operator=(const Matrix &other) { if (this != &other) { - memcpy(_data, other._data, sizeof(_data)); + Matrix &self = *this; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + self(i, j) = other(i, j); + } + } } return (*this); } void copyTo(Type dst[M*N]) const { - memcpy(dst, _data, sizeof(Type)*M*N); + const Matrix &self = *this; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + dst[N*i + j] = self(i, j); + } + } } void copyToColumnMajor(Type dst[M*N]) const @@ -146,8 +145,7 @@ public: Matrix operator*(const Matrix &other) const { const Matrix &self = *this; - Matrix res; - res.setZero(); + Matrix res{}; for (size_t i = 0; i < M; i++) { for (size_t k = 0; k < P; k++) {