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