Sat, 28 Jan 2017 17:14:05 +0200
Reworked the Matrix interface so that less index math is involved
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 |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
3 | * Copyright (C) 2013 - 2016 Teemu Piippo |
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 | #include "../basics.h" |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
20 | #include "../format.h" |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
21 | #include "matrix.h" |
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:
1065
diff
changeset
|
23 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
24 | * Default-constructor for a matrix |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
25 | */ |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
26 | Matrix::Matrix() : |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
27 | m_values{0} {} |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
28 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
29 | /* |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
30 | * Initializes the matrix from a C array |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
31 | * Note: the array must have (at least) 9 values! |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
32 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
33 | Matrix::Matrix (double values[]) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
34 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
35 | for (int i = 0; i < 9; ++i) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
36 | m_values[i] = values[i]; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
37 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
38 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
39 | /* |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
40 | * Constructs a matrix from a single fill value. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
41 | */ |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
42 | Matrix::Matrix (double fillvalue) : |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
43 | m_values {fillvalue} {} |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
44 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
45 | /* |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
46 | * Constructs a matrix from an initializer list. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
47 | * Note: the initializer list must have exactly 9 values. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
48 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
49 | Matrix::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
|
50 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
51 | if (countof(values) == 9) |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
52 | memcpy (&m_values[0], values.begin(), sizeof m_values); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
53 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
54 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
55 | /* |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
56 | * Prints the matrix out. |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
57 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
58 | void Matrix::dump() const |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
59 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
60 | for (int i = 0; i < 3; ++i) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
61 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
62 | for (int j = 0; j < 3; ++j) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
63 | print ("%1\t", m_values[i * 3 + j]); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
64 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
65 | print ("\n"); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
66 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
67 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
68 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
69 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
70 | * Returns a string representation of the matrix |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
71 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
72 | QString Matrix::toString() const |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
73 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
74 | QString val; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
75 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
76 | for (int i = 0; i < 9; ++i) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
77 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
78 | if (i > 0) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
79 | val += ' '; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
80 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
81 | val += QString::number (m_values[i]); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
82 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
83 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
84 | return val; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
85 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
86 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
87 | /* |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
88 | * Fills the matrix with zeros |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
89 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
90 | void Matrix::zero() |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
91 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
92 | memset (&m_values[0], 0, sizeof m_values); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
93 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
94 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
95 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
96 | * Performs matrix multiplication. |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
97 | * Note: A*B is not equivalent to B*A! |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
98 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
99 | Matrix Matrix::multiply (const Matrix& other) const |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
100 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
101 | Matrix result; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
102 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
103 | for (int i = 0; i < 3; ++i) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
104 | for (int j = 0; j < 3; ++j) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
105 | for (int k = 0; k < 3; ++k) |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
106 | result[i][j] += (*this)[i][k] * other[k][j]; |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
107 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
108 | return result; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
109 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
110 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
111 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
112 | * Returns the matrix's determinant |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
113 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
114 | double Matrix::determinant() const |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
115 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
116 | return (value (0) * value (4) * value (8)) + |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
117 | (value (1) * value (5) * value (6)) + |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
118 | (value (2) * value (3) * value (7)) - |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
119 | (value (2) * value (4) * value (6)) - |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
120 | (value (1) * value (3) * value (8)) - |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
121 | (value (0) * value (5) * value (7)); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
122 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
123 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
124 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
125 | * Returns a value in matrix value (index math must be done manually) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
126 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
127 | double& Matrix::value(int index) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
128 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
129 | return m_values[index]; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
130 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
131 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
132 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
133 | * Returns a value in matrix value (index math must be done manually) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
134 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
135 | const double& Matrix::value(int index) const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
136 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
137 | return m_values[index]; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
138 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
139 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
140 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
141 | * Performs matrix multiplication |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
142 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
143 | Matrix Matrix::operator*(const Matrix &other) const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
144 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
145 | return multiply(other); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
146 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
147 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
148 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
149 | * Returns a row of the matrix |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
150 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
151 | Matrix::RowView Matrix::operator[](int row) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
152 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
153 | return RowView {*this, row}; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
154 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
155 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
156 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
157 | * Returns a row of the matrix |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
158 | */ |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
159 | Matrix::ConstRowView Matrix::operator[](int row) const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
160 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
161 | return ConstRowView {*this, row}; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
162 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
163 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
164 | /* |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
165 | * Checks whether the two matrices are equal |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
166 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
167 | bool Matrix::operator==(const Matrix& other) const |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
168 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
169 | for (int i = 0; i < countof(m_values); ++i) |
1037
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
170 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
171 | if (not qFuzzyCompare(m_values[i], other.m_values[i])) |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
172 | return false; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
173 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
174 | return true; |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
175 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
176 | |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
177 | /** |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
178 | * @brief Matrix::operator != |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
179 | * @param other |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
180 | * @return whether the two matrices are not equal |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
181 | */ |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
182 | bool Matrix::operator!=(const Matrix& other) const |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
183 | { |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
184 | return not operator==(other); |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
185 | } |
4a9185e94d78
Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
186 | |
1068
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
187 | double& Matrix::operator()(int row, int column) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
188 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
189 | return m_values[row * 3 + column]; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
190 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
191 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
192 | const double& Matrix::operator()(int row, int column) const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
193 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
194 | return m_values[row * 3 + column]; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
195 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
196 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
197 | double* Matrix::begin() |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
198 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
199 | return m_values; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
200 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
201 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
202 | const double* Matrix::begin() const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
203 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
204 | return m_values; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
205 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
206 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
207 | double* Matrix::end() |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
208 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
209 | return m_values + countof(m_values); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
210 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
211 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
212 | const double* Matrix::end() const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
213 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
214 | return m_values + countof(m_values); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
215 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
216 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
217 | Matrix::RowView::RowView(Matrix &matrix, int row) : |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
218 | _matrix {matrix}, |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
219 | _row {row} {} |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
220 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
221 | double& Matrix::RowView::operator[](int column) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
222 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
223 | return _matrix.value(_row * 3 + column); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
224 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
225 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
226 | Matrix& Matrix::RowView::matrix() const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
227 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
228 | return _matrix; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
229 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
230 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
231 | int Matrix::RowView::row() |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
232 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
233 | return _row; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
234 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
235 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
236 | Matrix::ConstRowView::ConstRowView(const Matrix &matrix, int row) : |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
237 | _matrix {matrix}, |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
238 | _row {row} {} |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
239 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
240 | const double& Matrix::ConstRowView::operator[](int column) |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
241 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
242 | return _matrix.value(_row * 3 + column); |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
243 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
244 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
245 | const Matrix& Matrix::ConstRowView::matrix() const |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
246 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
247 | return _matrix; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
248 | } |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
249 | |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
250 | int Matrix::ConstRowView::row() |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
251 | { |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
252 | return _row; |
283de3bd8b0e
Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents:
1065
diff
changeset
|
253 | } |