src/types/matrix.cpp

Sat, 31 Mar 2018 18:41:24 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 31 Mar 2018 18:41:24 +0300
changeset 1366
69087b1e123b
parent 1326
69a90bd2dba2
permissions
-rw-r--r--

start rework

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: 1159
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 #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
1070
292c64cb2a75 Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1069
diff changeset
23 const Matrix Matrix::identity {1, 0, 0, 0, 1, 0, 0, 0, 1};
292c64cb2a75 Moved the identity matrix constant into Matrix's namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1069
diff changeset
24
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
25 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
26 * Default-constructor for a matrix
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
27 */
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
28 Matrix::Matrix() :
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
29 m_values{0} {}
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
30
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
31 /*
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
32 * 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
33 */
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
34 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
35 m_values {fillvalue} {}
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
36
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
37 /*
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
38 * 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
39 * 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
40 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
41 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
42 {
1069
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
43 int i = 0;
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
44
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
45 for (double value : values)
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
46 {
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
47 if (i < 9)
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
48 m_values[i++] = value;
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
49 else
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
50 break;
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
51 }
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
52 }
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
53
1366
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
54 Matrix::Matrix(const QGenericMatrix<3, 3>& other)
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
55 {
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
56 for (int i = 0; i < 3; i += 1)
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
57 for (int j = 0; j < 3; j += 1)
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
58 (*this)(i, j) = other(i, j);
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
59 }
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
60
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
61 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
62 * 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
63 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
64 QString Matrix::toString() const
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
65 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
66 QString val;
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 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
69 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
70 if (i > 0)
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
71 val += ' ';
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
72
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
73 val += QString::number (m_values[i]);
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
74 }
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 return val;
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
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
79 /*
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
80 * Fills the matrix with zeros
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
81 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
82 void Matrix::zero()
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
83 {
1069
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
84 for (double& value : m_values)
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
85 value = 0;
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
86 }
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
87
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
88 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
89 * Performs matrix multiplication.
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
90 * 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
91 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
92 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
93 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
94 Matrix result;
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
95
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
96 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
97 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
98 for (int k = 0; k < 3; ++k)
1069
220cde0fa2d9 Removed the dangerous C-array constructor from the matrix - no need for it anyway since the matrix already is an array of doubles.
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
99 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
100
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
101 return 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
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
104 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
105 * Returns the matrix's determinant
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
106 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
107 double Matrix::determinant() const
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
108 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
109 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
110 (value (1) * value (5) * value (6)) +
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
111 (value (2) * value (3) * value (7)) -
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
112 (value (2) * value (4) * value (6)) -
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
113 (value (1) * value (3) * value (8)) -
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
114 (value (0) * value (5) * value (7));
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
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
117 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
118 * 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
119 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
120 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
121 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
122 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
123 }
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 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
126 * 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
127 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
128 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
129 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
130 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
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 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
134 * Performs matrix multiplication
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
135 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
136 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
137 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
138 return multiply(other);
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 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
142 * 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
143 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
144 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
145 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
146 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
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 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
150 * 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
151 */
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
152 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
153 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
154 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
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 /*
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
158 * 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
159 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
160 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
161 {
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
162 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
163 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
164 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
165 return false;
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 return true;
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
168 }
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
169
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 * @brief Matrix::operator !=
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
172 * @param other
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
173 * @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
174 */
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
175 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
176 {
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
177 return not operator==(other);
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
178 }
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
179
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
180 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
181 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
182 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
183 }
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
184
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
185 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
186 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
187 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
188 }
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
189
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
190 double* Matrix::begin()
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 return m_values;
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
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
195 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
196 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
197 return m_values;
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
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
200 double* Matrix::end()
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 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
203 }
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
204
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
205 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
206 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
207 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
208 }
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
209
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
210 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
211 _matrix {matrix},
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
212 _row {row} {}
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 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
215 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
216 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
217 }
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
218
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
219 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
220 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
221 return _matrix;
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
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
224 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
225 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
226 return _row;
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
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
229 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
230 _matrix {matrix},
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
231 _row {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 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
234 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
235 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
236 }
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
237
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
238 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
239 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
240 return _matrix;
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
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
243 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
244 {
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
245 return _row;
1151
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
246 }
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
247
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
248 Matrix Matrix::fromRotationMatrix(const GLRotationMatrix& rotationMatrix)
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
249 {
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
250 Matrix result;
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
251
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
252 for (int i = 0; i < 3; ++i)
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
253 for (int j = 0; j < 3; ++j)
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
254 result(i, j) = rotationMatrix(i, j);
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
255
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
256 return result;
0eddb5bcf25b Made fixed cameras matrix-based. This simplifies some math.
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
257 }

mercurial