src/matrix.h

changeset 11
771168ee2c76
parent 8
44679e468ba9
child 17
a5111f4e6412
equal deleted inserted replaced
10:e249d97c7fe6 11:771168ee2c76
1 #pragma once 1 #pragma once
2 #include <QMetaType> 2 #include <QGenericMatrix>
3 3
4 template<int Rows, int Columns, typename T = double> 4 template<int Rows, int Columns, typename T = double>
5 struct Matrix 5 struct Matrix
6 { 6 {
7 T values[Rows][Columns]; 7 T values[Rows][Columns];
13 { 13 {
14 return this->values[row][column]; 14 return this->values[row][column];
15 } 15 }
16 }; 16 };
17 17
18 template<int Rows, int Columns, typename T>
19 QGenericMatrix<Rows, Columns, T> matrixToQGenericMatrix(const Matrix<Rows, Columns, T>& matrix)
20 {
21 return {matrix.values};
22 }
23
24 template<int Rows, int Columns, typename T>
25 Matrix<Rows, Columns, T> matrixFromQGenericMatrix(const QGenericMatrix<Rows, Columns, T&> matrix)
26 {
27 Matrix<Rows, Columns, T> result;
28 for (int row = 0; row < Rows; row += 1)
29 {
30 for (int column = 0; column < Columns; column += 1)
31 {
32 result(row, column) = matrix(row, column);
33 }
34 }
35 return result;
36 }
37
18 using Matrix3x3 = Matrix<3, 3>; 38 using Matrix3x3 = Matrix<3, 3>;
19 Q_DECLARE_METATYPE(Matrix3x3); 39 Q_DECLARE_METATYPE(Matrix3x3);
20 using Matrix4x4 = Matrix<4, 4>; 40 using Matrix4x4 = Matrix<4, 4>;
21 Q_DECLARE_METATYPE(Matrix4x4); 41 Q_DECLARE_METATYPE(Matrix4x4);
42
43 template<int Rows, int Columns, typename T>
44 QDataStream& operator<<(QDataStream& stream, const Matrix<Rows, Columns, T>& matrix)
45 {
46 return stream << matrixToQGenericMatrix(matrix);
47 }
48
49 template<int Rows, int Columns, typename T>
50 QDataStream& operator>>(QDataStream& stream, Matrix<Rows, Columns, T>& matrix)
51 {
52 QGenericMatrix<Rows, Columns, T> qmatrix;
53 stream >> qmatrix;
54 matrix = matrixFromQGenericMatrix(matrix);
55 return stream;
56 }

mercurial