Sun, 26 Jan 2020 01:06:27 +0200
fix default angle
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | #pragma once |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
20 | #include <QGenericMatrix> |
17 | 21 | #include <type_traits> |
22 | ||
23 | template<int Rows, int Columns, typename T = double> | |
24 | struct MatrixIterator; | |
25 | ||
26 | template<int Rows, int Columns> | |
27 | struct MatrixIndex | |
28 | { | |
29 | int row; | |
30 | int column; | |
31 | }; | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | template<int Rows, int Columns, typename T = double> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | struct Matrix |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | { |
17 | 36 | using Iterator = MatrixIterator<Rows, Columns, T>; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | T values[Rows][Columns]; |
17 | 38 | Iterator begin() |
39 | { | |
40 | return {*this, {0, 0}}; | |
41 | } | |
42 | Iterator end() | |
43 | { | |
44 | return {*this, {Rows, 0}}; | |
45 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | T& operator()(int row, int column) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | return this->values[row][column]; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | T operator()(int row, int column) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | return this->values[row][column]; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | } |
17 | 54 | T& operator[](const MatrixIndex<Rows, Columns>& index) |
55 | { | |
56 | return (*this)(index.row, index.column); | |
57 | } | |
58 | T operator[](const MatrixIndex<Rows, Columns>& index) const | |
59 | { | |
60 | return (*this)(index.row, index.column); | |
61 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | }; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
64 | template<int Rows, int Columns, typename T> |
17 | 65 | struct MatrixIterator |
66 | { | |
67 | struct Value | |
68 | { | |
69 | const MatrixIndex<Rows, Columns> index; | |
70 | decltype(std::declval<Matrix<Rows, Columns, T>>()(0, 0)) value; | |
71 | }; | |
72 | Matrix<Rows, Columns, T>& matrix; | |
73 | MatrixIndex<Rows, Columns> index; | |
74 | }; | |
75 | ||
76 | template<int Rows, int Columns> | |
77 | auto& operator++(MatrixIndex<Rows, Columns>& index) | |
78 | { | |
79 | index.column += 1; | |
80 | if (index.column >= Columns) | |
81 | { | |
82 | index.row += 1; | |
83 | index.column -= Columns; | |
84 | } | |
85 | return index; | |
86 | } | |
87 | ||
88 | template<int Rows, int Columns> | |
89 | bool operator==( | |
90 | const MatrixIndex<Rows, Columns>& one, | |
91 | const MatrixIndex<Rows, Columns>& other) | |
92 | { | |
93 | return one.row == other.row and one.column == other.column; | |
94 | } | |
95 | template<int Rows, int Columns, typename T> | |
96 | auto& operator++(MatrixIterator<Rows, Columns, T>& iterator) | |
97 | { | |
98 | ++iterator.index; | |
99 | return iterator; | |
100 | } | |
101 | ||
102 | template<int Rows, int Columns, typename T> | |
103 | bool operator==( | |
104 | const MatrixIterator<Rows, Columns, T>& one, | |
105 | const MatrixIterator<Rows, Columns, T>& other) | |
106 | { | |
107 | return &one.matrix == &other.matrix and one.index == other.index; | |
108 | } | |
109 | ||
110 | template<int Rows, int Columns, typename T> | |
111 | bool operator!=( | |
112 | const MatrixIterator<Rows, Columns, T>& one, | |
113 | const MatrixIterator<Rows, Columns, T>& other) | |
114 | { | |
115 | return not (one == other); | |
116 | } | |
117 | ||
118 | template<int Rows, int Columns, typename T> | |
119 | auto operator*(MatrixIterator<Rows, Columns, T>& iterator) | |
120 | -> typename MatrixIterator<Rows, Columns, T>::Value | |
121 | { | |
122 | return {iterator.index, iterator.matrix[iterator.index]}; | |
123 | } | |
124 | ||
125 | template<int Rows, int Columns, typename T> | |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
126 | 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
|
127 | { |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
128 | return {matrix.values}; |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
129 | } |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
130 | |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
131 | template<int Rows, int Columns, typename T> |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
132 | 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
|
133 | { |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
134 | Matrix<Rows, Columns, T> result; |
17 | 135 | for (auto& cell : result) |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
136 | { |
17 | 137 | 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
|
138 | } |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
139 | return result; |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
140 | } |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
141 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
142 | using Matrix3x3 = Matrix<3, 3>; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
143 | Q_DECLARE_METATYPE(Matrix3x3); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
144 | using Matrix4x4 = Matrix<4, 4>; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
145 | Q_DECLARE_METATYPE(Matrix4x4); |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
146 | |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
147 | template<int Rows, int Columns, typename T> |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
148 | 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
|
149 | { |
17 | 150 | for (auto& cell : matrix) |
151 | { | |
152 | stream << cell.value; | |
153 | } | |
154 | return stream; | |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
155 | } |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
156 | |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
157 | template<int Rows, int Columns, typename T> |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
158 | 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
|
159 | { |
17 | 160 | for (auto& cell : matrix) |
161 | { | |
162 | stream >> cell.value; | |
163 | } | |
11
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
164 | return stream; |
771168ee2c76
added matrix conversions and datastream operators
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
165 | } |
17 | 166 | |
21 | 167 | Matrix4x4 combine(const Matrix3x3& topLeft, const struct Point3D& translation); |
168 | ||
17 | 169 | static constexpr Matrix3x3 identity3x3 {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}}; |
170 | static constexpr Matrix4x4 identity4x4 {{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}}; | |
26 | 171 | |
172 | namespace math | |
173 | { | |
174 | qreal det(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f, qreal g, qreal h, qreal i); | |
175 | qreal det(const Matrix<2, 2>& matrix); | |
176 | qreal det(const Matrix3x3& matrix); | |
177 | qreal det(const Matrix4x4& matrix); | |
178 | } |