🛰️航天仿真算法库 SpaceAST 0.4.0
载入中...
搜索中...
未找到
Matrix.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstMath/MathOperator.hpp"
25#include "AstMath/Vector.hpp"
26#include <string.h>
27
28AST_NAMESPACE_BEGIN
29
36template<typename _Scalar>
37class Matrix
38{
39public:
40 Matrix();
41 Matrix(size_t row, size_t col);
42 Matrix(const Matrix& other);
43 Matrix(Matrix&& other);
44 ~Matrix();
45 Matrix& operator=(const Matrix& other);
46 Matrix& operator=(Matrix&& other);
47 void resize(size_t row, size_t col);
48 void setZero();
49 size_t size() const{return row_ * col_;}
50 size_t row() const{return row_;}
51 size_t col() const{return col_;}
52 _Scalar* data() const{return data_;}
53 _Scalar& operator()(size_t row, size_t col){return data_[row * col_ + col];}
54 _Scalar operator()(size_t row, size_t col) const{return data_[row * col_ + col];}
55protected:
56 size_t row_;
57 size_t col_;
58 _Scalar* data_;
59};
60
62
63template <typename _Scalar>
65 : row_{0}
66 , col_{0}
67 , data_{nullptr}
68{}
69
70template <typename _Scalar>
71inline Matrix<_Scalar>::Matrix(size_t row, size_t col)
72 : row_{row}
73 , col_{col}
74 , data_{nullptr}
75{
76 data_ = (_Scalar*)malloc(row * col * sizeof(_Scalar));
77 setZero();
78}
79
80template <typename _Scalar>
81inline Matrix<_Scalar>::~Matrix()
82{
83 if(data_)
84 free(data_);
85}
86
87template <typename _Scalar>
88inline Matrix<_Scalar>::Matrix(const Matrix& other)
89 : row_{other.row()}
90 , col_{other.col()}
91 , data_{nullptr}
92{
93 if (other.data_) {
94 this->data_ = (_Scalar*)malloc(other.size() * sizeof(_Scalar));
95 if(this->data_)
96 {
97 memcpy(data_, other.data_, other.size() * sizeof(_Scalar));
98 }
99 else
100 {
101 // aError("failed to copy Matrix");
102 data_ = nullptr;
103 row_ = 0;
104 col_ = 0;
105 }
106 }
107}
108
109template <typename _Scalar>
110inline Matrix<_Scalar>& Matrix<_Scalar>::operator=(const Matrix& other)
111{
112 if(this != &other){
113 resize(other.row(), other.col());
114 if (other.data_) {
115 memcpy(data_, other.data_, other.size() * sizeof(_Scalar));
116 }
117 }
118 return *this;
119}
120
121template <typename _Scalar>
122inline Matrix<_Scalar>::Matrix(Matrix&& other)
123 : row_{other.row()}
124 , col_{other.col()}
125 , data_{other.data()}
126{
127 other.row_ = 0;
128 other.col_ = 0;
129 other.data_ = nullptr;
130}
131
132template <typename _Scalar>
133inline Matrix<_Scalar>& Matrix<_Scalar>::operator=(Matrix&& other)
134{
135 if(this != &other){
136 std::swap(row_, other.row_);
137 std::swap(col_, other.col_);
138 std::swap(data_, other.data_);
139 }
140 return *this;
141}
142
143template <typename _Scalar>
144inline void Matrix<_Scalar>::resize(size_t row, size_t col)
145{
146 if(data_)
147 free(data_);
148 row_ = row;
149 col_ = col;
150 data_ = (_Scalar*)malloc(row * col * sizeof(_Scalar));
151}
152
153
154template <typename _Scalar>
155inline void Matrix<_Scalar>::setZero()
156{
157 if(data_)
158 memset(data_, 0, size() * sizeof(_Scalar));
159}
160
161
162template<typename _Scalar, size_t Row, size_t Col>
164{
165public:
167 typedef _Scalar Scalar;
168 static Self Zero(){return Self{};}
169
170 constexpr size_t size() const{return Row*Col;}
171 constexpr size_t row() const{return Row;}
172 constexpr size_t col() const{return Col;}
173 constexpr Scalar* data() const{return (Scalar*)this;}
174 Scalar operator[](size_t idx) const{return data()[idx];}
175 Scalar& operator[](size_t idx) {return data()[idx];}
176 Scalar operator()(size_t idx) const {return data()[idx]; }
177 Scalar& operator()(size_t idx) {return data()[idx]; }
178 Scalar operator()(size_t row, size_t col) const {return data_[row][col]; }
179 Scalar& operator()(size_t row, size_t col) {return data_[row][col]; }
180
181public:
182 Scalar data_[Row][Col];
183};
184
185template<typename MatrixType>
186class TransposeMatrix;
187
188template<typename _Scalar>
189class MatrixMN<_Scalar, 3, 3>
190{
191public:
192 enum {Row = 3, Col = 3};
194 typedef _Scalar Scalar;
195 static Self Identity(){return Self{1,0,0,0,1,0,0,0,1};}
196 static Self Zero(){return Self{0,0,0,0,0,0,0,0,0};}
197
198 constexpr size_t size() const{return Row*Col;}
199 constexpr size_t row() const{return Row;}
200 constexpr size_t col() const{return Col;}
201 constexpr Scalar* data() const{return (Scalar*)this;}
202 Scalar operator[](size_t idx) const{return data()[idx];}
203 Scalar& operator[](size_t idx) {return data()[idx];}
204 Scalar operator()(size_t idx) const {return data()[idx]; }
205 Scalar& operator()(size_t idx) {return data()[idx]; }
206 Scalar operator()(size_t row, size_t col) const {return data_[row][col]; }
207 Scalar& operator()(size_t row, size_t col) {return data_[row][col]; }
208
209 friend class TransposeMatrix<Self>;
210 TransposeMatrix<Self> transpose() const;
211 void transposeInPlace();
212 void setIdentity(){*this = Identity();}
213 Self& operator*=(const Self& other);
214
215public:
216 AST_MATH_API
217 std::string toString() const;
218protected:
219 Self transposeImpl() const;
220public:
221 Scalar data_[Row][Col];
222};
223
224#if !defined(AST_USE_EIGEN_MATRIX)
227#endif
228
229AST_MATH_API std::string toString(const Matrix3d& m);
230
231template <typename _Scalar>
233{
234 return Self{
235 data_[0][0], data_[1][0], data_[2][0],
236 data_[0][1], data_[1][1], data_[2][1],
237 data_[0][2], data_[1][2], data_[2][2],
238 };
239}
240
241template <typename _Scalar>
242A_ALWAYS_INLINE TransposeMatrix<MatrixMN<_Scalar, 3, 3>> MatrixMN<_Scalar, 3, 3>::transpose() const
243{
244 return TransposeMatrix<Self>(*this);
245}
246
247template <typename _Scalar>
248A_ALWAYS_INLINE void MatrixMN<_Scalar, 3, 3>::transposeInPlace()
249{
250 std::swap(data_[0][1], data_[1][0]);
251 std::swap(data_[0][2], data_[2][0]);
252 std::swap(data_[1][2], data_[2][1]);
253}
254
255template <typename _Scalar>
256A_ALWAYS_INLINE typename MatrixMN<_Scalar, 3, 3>::Self &MatrixMN<_Scalar, 3, 3>::operator*=(const Self &other)
257{
258 using namespace _AST math;
259 *this = operator*(*this, other);
260 return *this;
261}
262
263
267template<typename MatrixType>
269{
270public:
271 TransposeMatrix(const MatrixType& operand)
272 : operand_(operand)
273 {}
274 operator MatrixType() const
275 {
276 return operand_.transposeImpl();
277 }
278public:
279 MatrixType eval() const
280 {
281 return operand_.transposeImpl();
282 }
284 const MatrixType& operand() const { return operand_; }
285private:
286 const MatrixType& operand_;
287};
288
291AST_NAMESPACE_END
292
293AST_DECL_TYPE_ALIAS(Matrix3d)
294AST_DECL_TYPE_ALIAS(Matrix6d)
295
296
定义 Matrix.hpp:190
定义 Matrix.hpp:164
定义 Matrix.hpp:38
转置矩阵视图
定义 Matrix.hpp:269
const MatrixType & operand() const
获取操作数(即原始矩阵)
定义 Matrix.hpp:284
MatrixMN< _Scalar, I, K > operator*(const MatrixMN< _Scalar, I, J > &left, const MatrixMN< _Scalar, J, K > &right)
矩阵乘法
定义 MathOperator.hpp:580
Unit m
米
定义 Unit.cpp:456