src/types/matrix.h

Fri, 10 Feb 2017 23:06:24 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 10 Feb 2017 23:06:24 +0200
changeset 1122
795d1c3554b9
parent 1072
9ce9496427f2
child 1151
0eddb5bcf25b
permissions
-rw-r--r--

Added the RingAdapter class and interfacing ring() function and simplified some math in GLCompiler with it

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
9ce9496427f2 Happy new year 2017!
Teemu Piippo <teemu@hecknology.net>
parents: 1070
diff changeset
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>
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
21
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
22 /*
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
23 * A mathematical 3 x 3 matrix
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
24 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
25 class Matrix
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
26 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
27 public:
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
28 class RowView;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
29 class ConstRowView;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
30
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
31 Matrix();
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
32 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
33 Matrix (double fillval);
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
34
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
35 double* begin();
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
36 const double* begin() const;
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
37 double determinant() const;
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
38 void dump() 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;
292c64cb2a75 Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1069
diff changeset
56
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
57 private:
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
58 double m_values[9];
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
59 };
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
60
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 * 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
63 * 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
64 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
65 class Matrix::RowView
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 public:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
68 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
69 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
70 Matrix& matrix() const;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
71 int row();
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
72
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
73 private:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
74 Matrix& _matrix;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
75 const int _row;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
76 };
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 * 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
80 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
81 class Matrix::ConstRowView
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 public:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
84 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
85 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
86 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
87 int row();
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
88
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
89 private:
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
90 const Matrix& _matrix;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
91 const int _row;
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
92 };

mercurial