src/types/matrix.h

Sun, 10 Jun 2018 17:17:42 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 10 Jun 2018 17:17:42 +0300
changeset 1399
f52ea4078f5d
parent 1390
3eace926af7f
permissions
-rw-r--r--

added the disc and disc negative to the circular primitive type

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
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1316
diff changeset
3 * Copyright (C) 2013 - 2018 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>
1316
31f4293cec30 reworked includes
Teemu Piippo <teemu@hecknology.net>
parents: 1269
diff changeset
21 #include <QMetaType>
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 /*
1316
31f4293cec30 reworked includes
Teemu Piippo <teemu@hecknology.net>
parents: 1269
diff changeset
24 * A mathematical 3 × 3 matrix
1037
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;
1390
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
54 Matrix& operator*=(const Matrix& other);
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
55
1070
292c64cb2a75 Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1069
diff changeset
56 static const Matrix identity;
1390
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
57 static Matrix fromQMatrix(const QMatrix4x4& matrix);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
58 static Matrix scaleMatrix(qreal scalar);
1070
292c64cb2a75 Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1069
diff changeset
59
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
60 private:
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
61 double m_values[9];
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
62 };
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
63
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1159
diff changeset
64 Q_DECLARE_METATYPE(Matrix)
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1159
diff changeset
65
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
66 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
67 * 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
68 * 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
69 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
70 class Matrix::RowView
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
71 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
72 public:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
73 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
74 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
75 Matrix& matrix() const;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
76 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 private:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
79 Matrix& _matrix;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
80 const int _row;
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
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 * 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
85 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
86 class Matrix::ConstRowView
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
87 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
88 public:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
89 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
90 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
91 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
92 int row();
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
93
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
94 private:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
95 const Matrix& _matrix;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
96 const int _row;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
97 };

mercurial