17 */ |
17 */ |
18 |
18 |
19 #pragma once |
19 #pragma once |
20 #include <QString> |
20 #include <QString> |
21 |
21 |
22 /** |
22 /* |
23 * @brief The Matrix class |
|
24 * A mathematical 3 x 3 matrix |
23 * A mathematical 3 x 3 matrix |
25 */ |
24 */ |
26 class Matrix |
25 class Matrix |
27 { |
26 { |
28 public: |
27 public: |
|
28 class RowView; |
|
29 class ConstRowView; |
|
30 |
29 Matrix(); |
31 Matrix(); |
30 Matrix (const std::initializer_list<double>& values); |
32 Matrix (const std::initializer_list<double>& values); |
31 Matrix (double fillval); |
33 Matrix (double fillval); |
32 Matrix (double values[]); |
34 Matrix (double values[]); |
33 |
35 |
|
36 double* begin(); |
|
37 const double* begin() const; |
34 double determinant() const; |
38 double determinant() const; |
35 Matrix multiply (const Matrix& other) const; |
|
36 void dump() const; |
39 void dump() const; |
|
40 double* end(); |
|
41 const double* end() const; |
|
42 Matrix multiply(const Matrix& other) const; |
37 QString toString() const; |
43 QString toString() const; |
|
44 double& value(int index); |
|
45 const double& value(int index) const; |
38 void zero(); |
46 void zero(); |
|
47 |
39 bool operator==(const Matrix& other) const; |
48 bool operator==(const Matrix& other) const; |
40 bool operator!=(const Matrix& other) const; |
49 bool operator!=(const Matrix& other) const; |
41 |
50 Matrix operator*(const Matrix& other) const; |
42 /// @returns a mutable reference to a value by @c idx. |
51 RowView operator[](int row); |
43 inline double& value (int idx) { return m_values[idx]; } |
52 ConstRowView operator[](int row) const; |
44 |
53 double& operator()(int row, int column); |
45 /// @returns a const reference to a value by @c idx. |
54 const double& operator()(int row, int column) const; |
46 inline const double& value (int idx) const { return m_values[idx]; } |
|
47 |
|
48 /// @returns this matrix multiplied by @c other. |
|
49 inline Matrix operator* (const Matrix& other) const { return multiply (other); } |
|
50 |
|
51 /// @returns a mutable reference to a value by @c idx. |
|
52 inline double& operator[] (int idx) { return value(idx); } |
|
53 |
|
54 /// @returns a const reference to a value by @c idx. |
|
55 inline const double& operator[] (int idx) const { return value (idx); } |
|
56 |
55 |
57 private: |
56 private: |
58 double m_values[9]; |
57 double m_values[9]; |
59 }; |
58 }; |
|
59 |
|
60 /* |
|
61 * A structure that provides a view into a row in a matrix. |
|
62 * This is returned by operator[] so that the matrix can be accessed by A[i][j] |
|
63 */ |
|
64 class Matrix::RowView |
|
65 { |
|
66 public: |
|
67 RowView(Matrix &matrix, int row); |
|
68 double& operator[](int column); |
|
69 Matrix& matrix() const; |
|
70 int row(); |
|
71 |
|
72 private: |
|
73 Matrix& _matrix; |
|
74 const int _row; |
|
75 }; |
|
76 |
|
77 /* |
|
78 * Const version of the above |
|
79 */ |
|
80 class Matrix::ConstRowView |
|
81 { |
|
82 public: |
|
83 ConstRowView(const Matrix &matrix, int row); |
|
84 const double& operator[](int column); |
|
85 const Matrix& matrix() const; |
|
86 int row(); |
|
87 |
|
88 private: |
|
89 const Matrix& _matrix; |
|
90 const int _row; |
|
91 }; |