diff -r aeb5f203b3eb -r a5111f4e6412 src/matrix.h --- a/src/matrix.h Fri Nov 08 19:05:07 2019 +0200 +++ b/src/matrix.h Fri Dec 13 15:55:56 2019 +0200 @@ -1,10 +1,30 @@ #pragma once #include +#include + +template +struct MatrixIterator; + +template +struct MatrixIndex +{ + int row; + int column; +}; template struct Matrix { + using Iterator = MatrixIterator; T values[Rows][Columns]; + Iterator begin() + { + return {*this, {0, 0}}; + } + Iterator end() + { + return {*this, {Rows, 0}}; + } T& operator()(int row, int column) { return this->values[row][column]; @@ -13,9 +33,78 @@ { return this->values[row][column]; } + T& operator[](const MatrixIndex& index) + { + return (*this)(index.row, index.column); + } + T operator[](const MatrixIndex& index) const + { + return (*this)(index.row, index.column); + } }; template +struct MatrixIterator +{ + struct Value + { + const MatrixIndex index; + decltype(std::declval>()(0, 0)) value; + }; + Matrix& matrix; + MatrixIndex index; +}; + +template +auto& operator++(MatrixIndex& index) +{ + index.column += 1; + if (index.column >= Columns) + { + index.row += 1; + index.column -= Columns; + } + return index; +} + +template +bool operator==( + const MatrixIndex& one, + const MatrixIndex& other) +{ + return one.row == other.row and one.column == other.column; +} +template +auto& operator++(MatrixIterator& iterator) +{ + ++iterator.index; + return iterator; +} + +template +bool operator==( + const MatrixIterator& one, + const MatrixIterator& other) +{ + return &one.matrix == &other.matrix and one.index == other.index; +} + +template +bool operator!=( + const MatrixIterator& one, + const MatrixIterator& other) +{ + return not (one == other); +} + +template +auto operator*(MatrixIterator& iterator) + -> typename MatrixIterator::Value +{ + return {iterator.index, iterator.matrix[iterator.index]}; +} + +template QGenericMatrix matrixToQGenericMatrix(const Matrix& matrix) { return {matrix.values}; @@ -25,12 +114,9 @@ Matrix matrixFromQGenericMatrix(const QGenericMatrix matrix) { Matrix result; - for (int row = 0; row < Rows; row += 1) + for (auto& cell : result) { - for (int column = 0; column < Columns; column += 1) - { - result(row, column) = matrix(row, column); - } + matrix(cell.index.row, cell.index.column) = result[cell.index]; } return result; } @@ -43,14 +129,22 @@ template QDataStream& operator<<(QDataStream& stream, const Matrix& matrix) { - return stream << matrixToQGenericMatrix(matrix); + for (auto& cell : matrix) + { + stream << cell.value; + } + return stream; } template QDataStream& operator>>(QDataStream& stream, Matrix& matrix) { - QGenericMatrix qmatrix; - stream >> qmatrix; - matrix = matrixFromQGenericMatrix(matrix); + for (auto& cell : matrix) + { + stream >> cell.value; + } return stream; } + +static constexpr Matrix3x3 identity3x3 {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}}; +static constexpr Matrix4x4 identity4x4 {{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}};