🛰️航天仿真算法库 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#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
169 constexpr size_t size() const{return Row*Col;}
170 constexpr size_t row() const{return Row;}
171 constexpr size_t col() const{return Col;}
172 constexpr Scalar* data() const{return (Scalar*)this;}
173 Scalar operator[](size_t idx) const{return data()[idx];}
174 Scalar operator()(size_t idx) const {return data()[idx]; }
175 Scalar& operator()(size_t idx) {return data()[idx]; }
176 Scalar operator()(size_t row, size_t col) const {return data_[row][col]; }
177 Scalar& operator()(size_t row, size_t col) {return data_[row][col]; }
178
179public:
180 Scalar data_[Row][Col];
181};
182
183
184template<typename _Scalar>
185class MatrixMN<_Scalar, 3, 3>
186{
187public:
188 enum {Row = 3, Col = 3};
190 typedef _Scalar Scalar;
191 static Self Identity(){return Self{1,0,0,0,1,0,0,0,1};}
192 static Self Zero(){return Self{0,0,0,0,0,0,0,0,0};}
193
194 constexpr size_t size() const{return Row*Col;}
195 constexpr size_t row() const{return Row;}
196 constexpr size_t col() const{return Col;}
197 constexpr Scalar* data() const{return (Scalar*)this;}
198 Scalar operator[](size_t idx) const{return data()[idx];}
199 Scalar operator()(size_t idx) const {return data()[idx]; }
200 Scalar& operator()(size_t idx) {return data()[idx]; }
201 Scalar operator()(size_t row, size_t col) const {return data_[row][col]; }
202 Scalar& operator()(size_t row, size_t col) {return data_[row][col]; }
203
204 Self transpose() const;
205 void transposeInPlace();
206 void setIdentity(){*this = Identity();}
207 Self& operator*=(const Self& other);
208
209public:
210 Scalar data_[Row][Col];
211};
212
213
215
216template <typename _Scalar>
218{
219 return Self{
220 data_[0][0], data_[1][0], data_[2][0],
221 data_[0][1], data_[1][1], data_[2][1],
222 data_[0][2], data_[1][2], data_[2][2],
223 };
224}
225
226template <typename _Scalar>
227A_ALWAYS_INLINE void MatrixMN<_Scalar, 3, 3>::transposeInPlace()
228{
229 std::swap(data_[0][1], data_[1][0]);
230 std::swap(data_[0][2], data_[2][0]);
231 std::swap(data_[1][2], data_[2][1]);
232}
233
234template <typename _Scalar>
235A_ALWAYS_INLINE typename MatrixMN<_Scalar, 3, 3>::Self &MatrixMN<_Scalar, 3, 3>::operator*=(const Self &other)
236{
237 using namespace _AST math;
238 *this = operator*(*this, other);
239 return *this;
240}
241
244AST_NAMESPACE_END
245
246AST_DECL_TYPE_ALIAS(Matrix3d)
247
248
249
定义 Matrix.hpp:186
定义 Matrix.hpp:164
定义 Matrix.hpp:38