src/types/matrix.h

changeset 1403
7a2d84112983
parent 1402
8bbf2af8c3f5
child 1404
16eb4257e662
equal deleted inserted replaced
1402:8bbf2af8c3f5 1403:7a2d84112983
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2018 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
19 #pragma once
20 #include <QString>
21 #include <QMetaType>
22
23 /*
24 * A mathematical 3 × 3 matrix
25 */
26 class Matrix
27 {
28 public:
29 class RowView;
30 class ConstRowView;
31
32 Matrix();
33 Matrix (const std::initializer_list<double>& values);
34 Matrix (double fillval);
35
36 double* begin();
37 const double* begin() const;
38 double determinant() const;
39 double* end();
40 const double* end() const;
41 Matrix multiply(const Matrix& other) const;
42 QString toString() const;
43 double& value(int index);
44 const double& value(int index) const;
45 void zero();
46
47 bool operator==(const Matrix& other) const;
48 bool operator!=(const Matrix& other) const;
49 Matrix operator*(const Matrix& other) const;
50 RowView operator[](int row);
51 ConstRowView operator[](int row) const;
52 double& operator()(int row, int column);
53 const double& operator()(int row, int column) const;
54 Matrix& operator*=(const Matrix& other);
55
56 static const Matrix identity;
57 static Matrix fromQMatrix(const QMatrix4x4& matrix);
58 static Matrix scaleMatrix(qreal scalar);
59
60 private:
61 double m_values[9];
62 };
63
64 Q_DECLARE_METATYPE(Matrix)
65
66 /*
67 * A structure that provides a view into a row in a matrix.
68 * This is returned by operator[] so that the matrix can be accessed by A[i][j]
69 */
70 class Matrix::RowView
71 {
72 public:
73 RowView(Matrix &matrix, int row);
74 double& operator[](int column);
75 Matrix& matrix() const;
76 int row();
77
78 private:
79 Matrix& _matrix;
80 const int _row;
81 };
82
83 /*
84 * Const version of the above
85 */
86 class Matrix::ConstRowView
87 {
88 public:
89 ConstRowView(const Matrix &matrix, int row);
90 const double& operator[](int column);
91 const Matrix& matrix() const;
92 int row();
93
94 private:
95 const Matrix& _matrix;
96 const int _row;
97 };

mercurial