src/matrix.h

changeset 11
771168ee2c76
parent 8
44679e468ba9
child 17
a5111f4e6412
--- a/src/matrix.h	Sun Nov 03 13:07:04 2019 +0200
+++ b/src/matrix.h	Sun Nov 03 13:18:55 2019 +0200
@@ -1,5 +1,5 @@
 #pragma once
-#include <QMetaType>
+#include <QGenericMatrix>
 
 template<int Rows, int Columns, typename T = double>
 struct Matrix
@@ -15,7 +15,42 @@
 	}
 };
 
+template<int Rows, int Columns, typename T>
+QGenericMatrix<Rows, Columns, T> matrixToQGenericMatrix(const Matrix<Rows, Columns, T>& matrix)
+{
+	return {matrix.values};
+}
+
+template<int Rows, int Columns, typename T>
+Matrix<Rows, Columns, T> matrixFromQGenericMatrix(const QGenericMatrix<Rows, Columns, T&> matrix)
+{
+	Matrix<Rows, Columns, T> result;
+	for (int row = 0; row < Rows; row += 1)
+	{
+		for (int column = 0; column < Columns; column += 1)
+		{
+			result(row, column) = matrix(row, column);
+		}
+	}
+	return result;
+}
+
 using Matrix3x3 = Matrix<3, 3>;
 Q_DECLARE_METATYPE(Matrix3x3);
 using Matrix4x4 = Matrix<4, 4>;
 Q_DECLARE_METATYPE(Matrix4x4);
+
+template<int Rows, int Columns, typename T>
+QDataStream& operator<<(QDataStream& stream, const Matrix<Rows, Columns, T>& matrix)
+{
+	return stream << matrixToQGenericMatrix(matrix);
+}
+
+template<int Rows, int Columns, typename T>
+QDataStream& operator>>(QDataStream& stream, Matrix<Rows, Columns, T>& matrix)
+{
+	QGenericMatrix<Rows, Columns, T> qmatrix;
+	stream >> qmatrix;
+	matrix = matrixFromQGenericMatrix(matrix);
+	return stream;
+}

mercurial