🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Matrix.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstMath/MathOperator.hpp"
25#include "AstMath/Vector.hpp"
26
27AST_NAMESPACE_BEGIN
28
35template<typename _Scalar>
36class Matrix
37{
38public:
39 Matrix();
40 Matrix(const Matrix& other);
41 Matrix(Matrix&& other);
42 ~Matrix();
43 Matrix& operator=(const Matrix& other);
44 Matrix& operator=(Matrix&& other);
45 void resize(size_t row, size_t col);
46 void setZero();
47 size_t size() const{return m_row * m_col;}
48 size_t row() const{return m_row;}
49 size_t col() const{return m_col;}
50 _Scalar* data() const{return m_data;}
51 _Scalar& operator()(size_t row, size_t col){return m_data[row * m_col + col];}
52 _Scalar operator()(size_t row, size_t col) const{return m_data[row * m_col + col];}
53protected:
54 size_t m_row;
55 size_t m_col;
56 _Scalar* m_data;
57};
58
60
61template <typename _Scalar>
63 : m_row{0}
64 , m_col{0}
65 , m_data{nullptr}
66{}
67
68template <typename _Scalar>
69inline Matrix<_Scalar>::~Matrix()
70{
71 if(m_data)
72 free(m_data);
73}
74
75template <typename _Scalar>
76inline Matrix<_Scalar>::Matrix(const Matrix& other)
77 : m_row{other.m_row}
78 , m_col{other.m_col}
79 , m_data{nullptr}
80{
81 if (other.m_data) {
82 this->m_data = (_Scalar*)malloc(other.size() * sizeof(_Scalar));
83 memcpy(m_data, other.m_data, other.size() * sizeof(_Scalar));
84 }
85}
86
87template <typename _Scalar>
88inline Matrix<_Scalar>& Matrix<_Scalar>::operator=(const Matrix& other)
89{
90 if(this != &other){
91 resize(other.row(), other.col());
92 if (other.m_data) {
93 memcpy(m_data, other.m_data, other.size() * sizeof(_Scalar));
94 }
95 }
96 return *this;
97}
98
99template <typename _Scalar>
100inline Matrix<_Scalar>::Matrix(Matrix&& other)
101 : m_row{other.m_row}
102 , m_col{other.m_col}
103 , m_data{other.m_data}
104{
105 other.m_row = 0;
106 other.m_col = 0;
107 other.m_data = nullptr;
108}
109
110template <typename _Scalar>
111inline Matrix<_Scalar>& Matrix<_Scalar>::operator=(Matrix&& other)
112{
113 if(this != &other){
114 std::swap(m_row, other.m_row);
115 std::swap(m_col, other.m_col);
116 std::swap(m_data, other.m_data);
117 }
118 return *this;
119}
120
121template <typename _Scalar>
122inline void Matrix<_Scalar>::resize(size_t row, size_t col)
123{
124 if(m_data)
125 free(m_data);
126 m_row = row;
127 m_col = col;
128 m_data = (_Scalar*)malloc(row * col * sizeof(_Scalar));
129}
130
131
132template <typename _Scalar>
133inline void Matrix<_Scalar>::setZero()
134{
135 if(m_data)
136 memset(m_data, 0, size() * sizeof(_Scalar));
137}
138
139
140template<typename _Scalar, size_t Row, size_t Col>
142{
143public:
145 typedef _Scalar Scalar;
146
147 constexpr size_t size() const{return Row*Col;}
148 constexpr size_t row() const{return Row;}
149 constexpr size_t col() const{return Col;}
150 constexpr Scalar* data() const{return (Scalar*)this;}
151 Scalar operator[](size_t idx) const{return data()[idx];}
152 Scalar operator()(size_t idx) const {return data()[idx]; }
153 Scalar& operator()(size_t idx) {return data()[idx]; }
154 Scalar operator()(size_t row, size_t col) const {return data_[row][col]; }
155 Scalar& operator()(size_t row, size_t col) {return data_[row][col]; }
156
157public:
158 Scalar data_[Row][Col];
159};
160
161
162template<typename _Scalar>
163class MatrixMN<_Scalar, 3, 3>
164{
165public:
166 enum {Row = 3, Col = 3};
168 typedef _Scalar Scalar;
169 static Self Identity(){return Self{1,0,0,0,1,0,0,0,1};}
170 static Self Zero(){return Self{0,0,0,0,0,0,0,0,0};}
171
172 constexpr size_t size() const{return Row*Col;}
173 constexpr size_t row() const{return Row;}
174 constexpr size_t col() const{return Col;}
175 constexpr Scalar* data() const{return (Scalar*)this;}
176 Scalar operator[](size_t idx) const{return data()[idx];}
177 Scalar operator()(size_t idx) const {return data()[idx]; }
178 Scalar& operator()(size_t idx) {return data()[idx]; }
179 Scalar operator()(size_t row, size_t col) const {return data_[row][col]; }
180 Scalar& operator()(size_t row, size_t col) {return data_[row][col]; }
181
182 Self transpose() const;
183 void transposeInPlace();
184 void setIdentity(){*this = Identity();}
185 Self& operator*=(const Self& other);
186
187public:
188 Scalar data_[Row][Col];
189};
190
191
193
194template <typename _Scalar>
196{
197 return Self{
198 data_[0][0], data_[1][0], data_[2][0],
199 data_[0][1], data_[1][1], data_[2][1],
200 data_[0][2], data_[1][2], data_[2][2],
201 };
202}
203
204template <typename _Scalar>
205A_ALWAYS_INLINE void MatrixMN<_Scalar, 3, 3>::transposeInPlace()
206{
207 std::swap(data_[0][1], data_[1][0]);
208 std::swap(data_[0][2], data_[2][0]);
209 std::swap(data_[1][2], data_[2][1]);
210}
211
212template <typename _Scalar>
213A_ALWAYS_INLINE typename MatrixMN<_Scalar, 3, 3>::Self &MatrixMN<_Scalar, 3, 3>::operator*=(const Self &other)
214{
215 using namespace _AST math;
216 *this = operator*(*this, other);
217 return *this;
218}
219
222AST_NAMESPACE_END
223
224AST_DECL_TYPE_ALIAS(Matrix3d)
225
226
227
定义 Matrix.hpp:164
定义 Matrix.hpp:142
定义 Matrix.hpp:37