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