added matrix conversions and datastream operators

Sun, 03 Nov 2019 13:18:55 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 03 Nov 2019 13:18:55 +0200
changeset 11
771168ee2c76
parent 10
e249d97c7fe6
child 12
fe67489523b5

added matrix conversions and datastream operators

src/matrix.h file | annotate | diff | comparison | revisions
--- 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