Thu, 23 Feb 2017 20:18:39 +0200
print() is no longer a global function but is tied to HierarchyElement.
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
1 | /* |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
1072 | 3 | * Copyright (C) 2013 - 2017 Teemu Piippo |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
4 | * |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
9 | * |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
14 | * |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
17 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
18 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
19 | #pragma once |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
20 | #include <QString> |
1151
0eddb5bcf25b
Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents:
1072
diff
changeset
|
21 | #include "../glShared.h" |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
22 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
23 | /* |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
24 | * A mathematical 3 x 3 matrix |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
25 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
26 | class Matrix |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
27 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
28 | public: |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
29 | class RowView; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
30 | class ConstRowView; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
31 | |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
32 | Matrix(); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
33 | Matrix (const std::initializer_list<double>& values); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
34 | Matrix (double fillval); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
35 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
36 | double* begin(); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
37 | const double* begin() const; |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
38 | double determinant() const; |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
39 | double* end(); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
40 | const double* end() const; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
41 | Matrix multiply(const Matrix& other) const; |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
42 | QString toString() const; |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
43 | double& value(int index); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
44 | const double& value(int index) const; |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
45 | void zero(); |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
46 | |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
47 | bool operator==(const Matrix& other) const; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
48 | bool operator!=(const Matrix& other) const; |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
49 | Matrix operator*(const Matrix& other) const; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
50 | RowView operator[](int row); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
51 | ConstRowView operator[](int row) const; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
52 | double& operator()(int row, int column); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
53 | const double& operator()(int row, int column) const; |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
54 | |
1070
292c64cb2a75
Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1069
diff
changeset
|
55 | static const Matrix identity; |
1151
0eddb5bcf25b
Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents:
1072
diff
changeset
|
56 | static Matrix fromRotationMatrix(const GLRotationMatrix& rotationMatrix); |
1070
292c64cb2a75
Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1069
diff
changeset
|
57 | |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
58 | private: |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
59 | double m_values[9]; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
60 | }; |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
61 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
62 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
63 | * A structure that provides a view into a row in a matrix. |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
64 | * This is returned by operator[] so that the matrix can be accessed by A[i][j] |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
65 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
66 | class Matrix::RowView |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
67 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
68 | public: |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
69 | RowView(Matrix &matrix, int row); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
70 | double& operator[](int column); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
71 | Matrix& matrix() const; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
72 | int row(); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
73 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
74 | private: |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
75 | Matrix& _matrix; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
76 | const int _row; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
77 | }; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
78 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
79 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
80 | * Const version of the above |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
81 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
82 | class Matrix::ConstRowView |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
83 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
84 | public: |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
85 | ConstRowView(const Matrix &matrix, int row); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
86 | const double& operator[](int column); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
87 | const Matrix& matrix() const; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
88 | int row(); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
89 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
90 | private: |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
91 | const Matrix& _matrix; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
92 | const int _row; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1037
diff
changeset
|
93 | }; |