src/matrix.h

Sun, 19 Jan 2020 02:54:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 02:54:48 +0200
changeset 22
6da867fa5429
parent 21
0133e565e072
child 24
1a0faaaceb84
permissions
-rw-r--r--

commit work on GL rendering

8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #pragma once
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
2 #include <QGenericMatrix>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
3 #include <type_traits>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
4
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
5 template<int Rows, int Columns, typename T = double>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
6 struct MatrixIterator;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
7
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
8 template<int Rows, int Columns>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
9 struct MatrixIndex
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
10 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
11 int row;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
12 int column;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
13 };
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 template<int Rows, int Columns, typename T = double>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 struct Matrix
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 {
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
18 using Iterator = MatrixIterator<Rows, Columns, T>;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 T values[Rows][Columns];
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
20 Iterator begin()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
21 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
22 return {*this, {0, 0}};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
23 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
24 Iterator end()
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
25 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
26 return {*this, {Rows, 0}};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
27 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 T& operator()(int row, int column)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 return this->values[row][column];
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 T operator()(int row, int column) const
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 return this->values[row][column];
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
36 T& operator[](const MatrixIndex<Rows, Columns>& index)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
37 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
38 return (*this)(index.row, index.column);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
39 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
40 T operator[](const MatrixIndex<Rows, Columns>& index) const
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
41 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
42 return (*this)(index.row, index.column);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
43 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 };
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
46 template<int Rows, int Columns, typename T>
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
47 struct MatrixIterator
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
48 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
49 struct Value
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
50 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
51 const MatrixIndex<Rows, Columns> index;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
52 decltype(std::declval<Matrix<Rows, Columns, T>>()(0, 0)) value;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
53 };
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
54 Matrix<Rows, Columns, T>& matrix;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
55 MatrixIndex<Rows, Columns> index;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
56 };
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
57
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
58 template<int Rows, int Columns>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
59 auto& operator++(MatrixIndex<Rows, Columns>& index)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
60 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
61 index.column += 1;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
62 if (index.column >= Columns)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
63 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
64 index.row += 1;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
65 index.column -= Columns;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
66 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
67 return index;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
68 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
69
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
70 template<int Rows, int Columns>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
71 bool operator==(
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
72 const MatrixIndex<Rows, Columns>& one,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
73 const MatrixIndex<Rows, Columns>& other)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
74 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
75 return one.row == other.row and one.column == other.column;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
76 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
77 template<int Rows, int Columns, typename T>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
78 auto& operator++(MatrixIterator<Rows, Columns, T>& iterator)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
79 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
80 ++iterator.index;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
81 return iterator;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
82 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
83
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
84 template<int Rows, int Columns, typename T>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
85 bool operator==(
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
86 const MatrixIterator<Rows, Columns, T>& one,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
87 const MatrixIterator<Rows, Columns, T>& other)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
88 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
89 return &one.matrix == &other.matrix and one.index == other.index;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
90 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
91
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
92 template<int Rows, int Columns, typename T>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
93 bool operator!=(
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
94 const MatrixIterator<Rows, Columns, T>& one,
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
95 const MatrixIterator<Rows, Columns, T>& other)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
96 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
97 return not (one == other);
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
98 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
99
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
100 template<int Rows, int Columns, typename T>
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
101 auto operator*(MatrixIterator<Rows, Columns, T>& iterator)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
102 -> typename MatrixIterator<Rows, Columns, T>::Value
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
103 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
104 return {iterator.index, iterator.matrix[iterator.index]};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
105 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
106
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
107 template<int Rows, int Columns, typename T>
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
108 QGenericMatrix<Rows, Columns, T> matrixToQGenericMatrix(const Matrix<Rows, Columns, T>& matrix)
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
109 {
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
110 return {matrix.values};
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
111 }
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
112
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
113 template<int Rows, int Columns, typename T>
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
114 Matrix<Rows, Columns, T> matrixFromQGenericMatrix(const QGenericMatrix<Rows, Columns, T&> matrix)
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
115 {
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
116 Matrix<Rows, Columns, T> result;
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
117 for (auto& cell : result)
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
118 {
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
119 matrix(cell.index.row, cell.index.column) = result[cell.index];
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
120 }
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
121 return result;
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
122 }
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
123
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 using Matrix3x3 = Matrix<3, 3>;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 Q_DECLARE_METATYPE(Matrix3x3);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126 using Matrix4x4 = Matrix<4, 4>;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 Q_DECLARE_METATYPE(Matrix4x4);
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
128
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
129 template<int Rows, int Columns, typename T>
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
130 QDataStream& operator<<(QDataStream& stream, const Matrix<Rows, Columns, T>& matrix)
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
131 {
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
132 for (auto& cell : matrix)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
133 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
134 stream << cell.value;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
135 }
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
136 return stream;
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
137 }
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
138
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
139 template<int Rows, int Columns, typename T>
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
140 QDataStream& operator>>(QDataStream& stream, Matrix<Rows, Columns, T>& matrix)
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
141 {
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
142 for (auto& cell : matrix)
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
143 {
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
144 stream >> cell.value;
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
145 }
11
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
146 return stream;
771168ee2c76 added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
147 }
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
148
21
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
149 Matrix4x4 combine(const Matrix3x3& topLeft, const struct Point3D& translation);
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
150
17
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
151 static constexpr Matrix3x3 identity3x3 {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}};
a5111f4e6412 added teapot rendering
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
152 static constexpr Matrix4x4 identity4x4 {{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}};

mercurial