Sat, 14 Dec 2019 22:36:06 +0200
added missing files
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 | 3 | #include <type_traits> |
4 | ||
5 | template<int Rows, int Columns, typename T = double> | |
6 | struct MatrixIterator; | |
7 | ||
8 | template<int Rows, int Columns> | |
9 | struct MatrixIndex | |
10 | { | |
11 | int row; | |
12 | int column; | |
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 | 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 | 20 | Iterator begin() |
21 | { | |
22 | return {*this, {0, 0}}; | |
23 | } | |
24 | Iterator end() | |
25 | { | |
26 | return {*this, {Rows, 0}}; | |
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 | 36 | T& operator[](const MatrixIndex<Rows, Columns>& index) |
37 | { | |
38 | return (*this)(index.row, index.column); | |
39 | } | |
40 | T operator[](const MatrixIndex<Rows, Columns>& index) const | |
41 | { | |
42 | return (*this)(index.row, index.column); | |
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 | 47 | struct MatrixIterator |
48 | { | |
49 | struct Value | |
50 | { | |
51 | const MatrixIndex<Rows, Columns> index; | |
52 | decltype(std::declval<Matrix<Rows, Columns, T>>()(0, 0)) value; | |
53 | }; | |
54 | Matrix<Rows, Columns, T>& matrix; | |
55 | MatrixIndex<Rows, Columns> index; | |
56 | }; | |
57 | ||
58 | template<int Rows, int Columns> | |
59 | auto& operator++(MatrixIndex<Rows, Columns>& index) | |
60 | { | |
61 | index.column += 1; | |
62 | if (index.column >= Columns) | |
63 | { | |
64 | index.row += 1; | |
65 | index.column -= Columns; | |
66 | } | |
67 | return index; | |
68 | } | |
69 | ||
70 | template<int Rows, int Columns> | |
71 | bool operator==( | |
72 | const MatrixIndex<Rows, Columns>& one, | |
73 | const MatrixIndex<Rows, Columns>& other) | |
74 | { | |
75 | return one.row == other.row and one.column == other.column; | |
76 | } | |
77 | template<int Rows, int Columns, typename T> | |
78 | auto& operator++(MatrixIterator<Rows, Columns, T>& iterator) | |
79 | { | |
80 | ++iterator.index; | |
81 | return iterator; | |
82 | } | |
83 | ||
84 | template<int Rows, int Columns, typename T> | |
85 | bool operator==( | |
86 | const MatrixIterator<Rows, Columns, T>& one, | |
87 | const MatrixIterator<Rows, Columns, T>& other) | |
88 | { | |
89 | return &one.matrix == &other.matrix and one.index == other.index; | |
90 | } | |
91 | ||
92 | template<int Rows, int Columns, typename T> | |
93 | bool operator!=( | |
94 | const MatrixIterator<Rows, Columns, T>& one, | |
95 | const MatrixIterator<Rows, Columns, T>& other) | |
96 | { | |
97 | return not (one == other); | |
98 | } | |
99 | ||
100 | template<int Rows, int Columns, typename T> | |
101 | auto operator*(MatrixIterator<Rows, Columns, T>& iterator) | |
102 | -> typename MatrixIterator<Rows, Columns, T>::Value | |
103 | { | |
104 | return {iterator.index, iterator.matrix[iterator.index]}; | |
105 | } | |
106 | ||
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 | 117 | for (auto& cell : result) |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
118 | { |
17 | 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 | 132 | for (auto& cell : matrix) |
133 | { | |
134 | stream << cell.value; | |
135 | } | |
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 | 142 | for (auto& cell : matrix) |
143 | { | |
144 | stream >> cell.value; | |
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 | 148 | |
149 | static constexpr Matrix3x3 identity3x3 {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}}; | |
150 | static constexpr Matrix4x4 identity4x4 {{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}}; |