🛰️航天仿真算法库 SpaceAST 0.4.0
载入中...
搜索中...
未找到
Vector.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstMath/MathOperator.hpp"
25#include "AstUtil/Constants.hpp"
26#include "AstUtil/Math.hpp"
27#include <stdlib.h> // for malloc
28#include <string> // for std::string
29#include <string.h> // for memset
30
31AST_NAMESPACE_BEGIN
32
44template<typename _Scalar, size_t N>
45class VectorN;
46
47
48#define _AST_DEF_VECTOR_METHOD(Scalar) \
49 A_DEF_POD_ITERABLE(Scalar) \
50 Scalar operator()(size_t idx) const{return data()[idx];} \
51 Scalar& operator()(size_t idx) {return data()[idx];} \
52 Scalar& x() {return data()[0];} \
53 Scalar& y() {return data()[1];} \
54 Scalar& z() {return data()[2];} \
55 Scalar x() const {return data()[0];} \
56 Scalar y() const {return data()[1];} \
57 Scalar z() const {return data()[2];} \
58
59
60
61template<typename _Scalar, size_t N>
63{
64public:
65 typedef _Scalar Scalar;
66 enum {Dimension = N};
67 _AST_DEF_VECTOR_METHOD(Scalar);
68public:
69 Scalar m_data[N]; // 不要直接访问m_data,设为public仅为了实现聚合初始化
70};
71
72
73template<typename _Scalar>
74class VectorN<_Scalar, 3>
75{
76public:
78 typedef _Scalar Scalar;
79 enum {Dimension = 3};
80 #if defined(SWIG)
81 VectorN()
82 :x_(0), y_(0), z_(0){}
83 VectorN(double x, double y, double z)
84 :x_(x), y_(y), z_(z){}
85 #endif
86 #ifdef AST_BUILD_LIB_PY
87 void __setitem__(size_t idx, _Scalar v){data()[idx] = v;}
88 #endif
89
90 static Self Zero(){return Self{0,0,0}; }
91 static Self UnitX() {return Self{1,0,0}; }
92 static Self UnitY() {return Self{0,1,0}; }
93 static Self UnitZ() {return Self{0,0,1}; }
94 static double Angle(const Self& v1, const Self & v2);
95 _Scalar at(size_t idx) const{return data()[idx]; }
96 _Scalar& at(size_t idx) {return data()[idx]; }
97 double normalize(){return _ASTMATH normalize(*this);}
98 Self normalized() const{return _ASTMATH normalized(*this);}
99 double norm() const{return _ASTMATH norm(*this);}
100 double squaredNorm() const{return _ASTMATH squaredNorm(*this);}
101 Self cross(const Self& other) const{return _ASTMATH cross(*this, other);}
102 double angle(const Self& other) const{return Angle(*this, other);}
103 double dot(const Self& other) const{return _ASTMATH dot(*this, other);}
104 void setZero(){x_ = y_ = z_ = 0;}
105 Self& operator*=(Scalar s){return *this = _ASTMATH operator*(*this, s);}
106 Self operator*(Scalar s) const{return _ASTMATH operator*(*this, s);}
107 Self operator/(Scalar s) const{return _ASTMATH operator/(*this, s);}
108 Self operator-() const{return Self{-x_, -y_, -z_};}
109 Self operator-(const Self& other) const{return _ASTMATH operator-(*this, other);}
110 Self operator+(const Self& other) const{return _ASTMATH operator+(*this, other);}
111 Self& operator+=(const Self& other){return *this = _ASTMATH operator+(*this, other);}
112 Self& transpose(){return *this;}
113 const Self& transpose() const{return *this;}
114 std::string toString() const;
115 _AST_DEF_VECTOR_METHOD(Scalar);
116public:
117 _Scalar x_, y_, z_; // 不要直接访问数据,设为public仅为了实现聚合初始化
118};
119
120
121AST_MATH_API std::string toString(const Vector3d& v);
122
123template<typename _Scalar>
125{
126public:
127 VectorX();
128 VectorX(size_t size);
129
130 VectorX(const VectorX&) = delete;
131 VectorX& operator=(const VectorX&) = delete;
132
133 ~VectorX();
134
135 void resize(size_t size);
136 void setZero();
137 A_DEF_ITERABLE(_Scalar, data_, size_)
138private:
139 _Scalar* data_{nullptr};
140 size_t size_{0};
141};
142
143
145
146
147template<typename _Scalar>
148inline double VectorN<_Scalar, 3>::Angle(const Self& v1, const Self & v2)
149{
150 return aAngle(v1, v2);
151}
152
153template<typename _Scalar>
154inline VectorX<_Scalar>::VectorX()
155 : data_{nullptr}
156 , size_{0}
157{
158}
159
160template<typename _Scalar>
161inline VectorX<_Scalar>::VectorX(size_t size)
162 : data_((_Scalar*)malloc(sizeof(_Scalar)* size))
163 , size_{size}
164{
165 setZero();
166}
167
168
169template<typename _Scalar>
170inline VectorX<_Scalar>::~VectorX()
171{
172 if(data_)
173 free(data_);
174}
175
176template<typename _Scalar>
177inline void VectorX<_Scalar>::resize(size_t size)
178{
179 if (size > size_)
180 {
181 if(data_)
182 free(data_);
183 data_ = (_Scalar*)malloc(sizeof(_Scalar) * size);
184 }
185 size_ = size;
186}
187
188template <typename _Scalar>
189inline void VectorX<_Scalar>::setZero()
190{
191 if(data_)
192 memset(data_, 0, sizeof(_Scalar) * size_);
193}
194
195typedef VectorX<double> VectorXd;
196
197
200AST_NAMESPACE_END
201
202
203AST_DECL_TYPE_ALIAS(Vector3d)
204AST_DECL_TYPE_ALIAS(VectorXd)
205
角度抽象基类
定义 Angle.hpp:39
量纲
定义 Dimension.hpp:362
定义 Vector.hpp:75
定义 Vector.hpp:63
定义 Vector.hpp:125
MatrixMN< _Scalar, I, K > operator*(const MatrixMN< _Scalar, I, J > &left, const MatrixMN< _Scalar, J, K > &right)
矩阵乘法
定义 MathOperator.hpp:580
double norm(const double *vec, size_t N)
norm
定义 MathOperator.hpp:290
double dot(const Container1 &vec1, const Container2 &vec2)
dot
定义 MathOperator.hpp:162
auto normalized(const Vector &vec) -> typename std::enable_if<!std::is_pointer< Vector >::value, Vector >::type
normalized
定义 MathOperator.hpp:361
double normalize(double *vec, size_t N)
normalize
定义 MathOperator.hpp:315
double squaredNorm(const double *vec, size_t N)
squaredNorm
定义 MathOperator.hpp:254
Unit s
秒
定义 Unit.cpp:465
Unit N
牛顿
定义 Unit.cpp:477
constexpr EDimension operator/(EDimension dim1, EDimension dim2) noexcept
量纲除法运算符
定义 Dimension.hpp:232