🛰️航天仿真算法库 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
29AST_NAMESPACE_BEGIN
30
42template<typename _Scalar, size_t N>
43class VectorN;
44
45
46#define _AST_DEF_VECTOR_METHOD(Scalar) \
47 A_DEF_POD_ITERABLE(Scalar) \
48 Scalar operator()(size_t idx) const{return data()[idx];} \
49 Scalar& operator()(size_t idx) {return data()[idx];} \
50 Scalar& x() {return data()[0];} \
51 Scalar& y() {return data()[1];} \
52 Scalar& z() {return data()[2];} \
53 Scalar x() const {return data()[0];} \
54 Scalar y() const {return data()[1];} \
55 Scalar z() const {return data()[2];} \
56
57
58
59template<typename _Scalar, size_t N>
61{
62public:
63 typedef _Scalar Scalar;
64 enum {Dimension = N};
65 _AST_DEF_VECTOR_METHOD(Scalar);
66public:
67 Scalar m_data[N]; // 不要直接访问m_data,设为public仅为了实现聚合初始化
68};
69
70
71template<typename _Scalar>
72class VectorN<_Scalar, 3>
73{
74public:
76 typedef _Scalar Scalar;
77 enum {Dimension = 3};
78 #if defined(SWIG)
79 VectorN()
80 :x_(0), y_(0), z_(0){}
81 VectorN(double x, double y, double z)
82 :x_(x), y_(y), z_(z){}
83 #endif
84 #ifdef AST_BUILD_LIB_PY
85 void __setitem__(size_t idx, _Scalar v){data()[idx] = v;}
86 #endif
87
88 static Self Zero(){return Self{0,0,0}; }
89 static Self UnitX() {return Self{1,0,0}; }
90 static Self UnitY() {return Self{0,1,0}; }
91 static Self UnitZ() {return Self{0,0,1}; }
92 static double Angle(const Self& v1, const Self & v2);
93 _Scalar at(size_t idx) const{return data()[idx]; }
94 _Scalar& at(size_t idx) {return data()[idx]; }
95 double normalize(){return _ASTMATH normalize(*this);}
96 Self normalized() const{return _ASTMATH normalized(*this);}
97 double norm() const{return _ASTMATH norm(*this);}
98 double squaredNorm() const{return _ASTMATH squaredNorm(*this);}
99 Self cross(const Self& other) const{return _ASTMATH cross(*this, other);}
100 double angle(const Self& other) const{return Angle(*this, other);}
101 double dot(const Self& other) const{return _ASTMATH dot(*this, other);}
102 void setZero(){x_ = y_ = z_ = 0;}
103 Self& operator*=(Scalar s){return *this = _ASTMATH operator*(*this, s);}
104 Self operator*(Scalar s) const{return _ASTMATH operator*(*this, s);}
105 Self operator/(Scalar s) const{return _ASTMATH operator/(*this, s);}
106 Self operator-() const{return Self{-x_, -y_, -z_};}
107 Self operator-(const Self& other) const{return _ASTMATH operator-(*this, other);}
108 Self operator+(const Self& other) const{return _ASTMATH operator+(*this, other);}
109 Self& operator+=(const Self& other){return *this = _ASTMATH operator+(*this, other);}
110 std::string toString() const;
111 _AST_DEF_VECTOR_METHOD(Scalar);
112public:
113 _Scalar x_, y_, z_; // 不要直接访问数据,设为public仅为了实现聚合初始化
114};
115
116
117template<typename _Scalar>
119{
120public:
121 VectorX();
122 VectorX(size_t size);
123 ~VectorX();
124
125 void resize(size_t size);
126 void setZero();
127 A_DEF_ITERABLE(_Scalar, data_, size_)
128private:
129 _Scalar* data_{nullptr};
130 size_t size_{0};
131};
132
133
135
136
137template<typename _Scalar>
138inline double VectorN<_Scalar, 3>::Angle(const Self& v1, const Self & v2)
139{
140 double normProduct = v1.norm() * v2.norm();
141 if (normProduct == 0) {
142 return 0;
143 }
144
145 double dot = v1.dot(v2);
146 double threshold = normProduct * 0.9999;
147 if ((dot < -threshold) || (dot > threshold)) {
148 Self v3 = v1.cross(v2);
149 if (dot >= 0) {
150 return asin(v3.norm() / normProduct);
151 }
152 return kPI - asin(v3.norm() / normProduct);
153 }
154
155 return acos(dot / normProduct);
156}
157
158template<typename _Scalar>
159inline VectorX<_Scalar>::VectorX()
160 : data_{nullptr}
161 , size_{0}
162{
163}
164
165template<typename _Scalar>
166inline VectorX<_Scalar>::VectorX(size_t size)
167 : data_((_Scalar*)malloc(sizeof(_Scalar)* size))
168 , size_{size}
169{
170 setZero();
171}
172
173
174template<typename _Scalar>
175inline VectorX<_Scalar>::~VectorX()
176{
177 if(data_)
178 free(data_);
179}
180
181template<typename _Scalar>
182inline void VectorX<_Scalar>::resize(size_t size)
183{
184 if (size > size_)
185 {
186 if(data_)
187 free(data_);
188 data_ = (_Scalar*)malloc(sizeof(_Scalar) * size);
189 }
190 size_ = size;
191}
192
193template <typename _Scalar>
194inline void VectorX<_Scalar>::setZero()
195{
196 if(data_)
197 memset(data_, 0, sizeof(_Scalar) * size_);
198}
199
200typedef VectorX<double> VectorXd;
201
202
205AST_NAMESPACE_END
206
207
208AST_DECL_TYPE_ALIAS(Vector3d)
209AST_DECL_TYPE_ALIAS(VectorXd)
210
Unit s
定义 Unit.cpp:435
Unit N
牛顿
定义 Unit.cpp:447
量纲
定义 Dimension.hpp:355
定义 Vector.hpp:73
定义 Vector.hpp:61
定义 Vector.hpp:119
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
constexpr EDimension operator/(EDimension dim1, EDimension dim2) noexcept
量纲除法运算符
定义 Dimension.hpp:232