🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Vector.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstMath/MathOperator.hpp"
25#include <stdlib.h> // for malloc
26#include <string> // for std::string
27
28AST_NAMESPACE_BEGIN
29
41template<typename _Scalar, size_t N>
42class VectorN;
43
44
45#define _AST_DEF_VECTOR_METHOD(Scalar) \
46 A_DEF_POD_ITERABLE(Scalar) \
47 Scalar operator()(size_t idx) const{return data()[idx];} \
48 Scalar& operator()(size_t idx) {return data()[idx];} \
49 Scalar& x() {return data()[0];} \
50 Scalar& y() {return data()[1];} \
51 Scalar& z() {return data()[2];} \
52 Scalar x() const {return data()[0];} \
53 Scalar y() const {return data()[1];} \
54 Scalar z() const {return data()[2];} \
55
56
57
58template<typename _Scalar, size_t N>
60{
61public:
62 typedef _Scalar Scalar;
63 enum {Dimension = N};
64 _AST_DEF_VECTOR_METHOD(Scalar);
65public:
66 Scalar m_data[N]; // 不要直接访问m_data,设为public仅为了实现聚合初始化
67};
68
69
70template<typename _Scalar>
71class VectorN<_Scalar, 3>
72{
73public:
75 typedef _Scalar Scalar;
76 enum {Dimension = 3};
77
78 static Self Zero(){return Self{0,0,0}; }
79 static Self UnitX() {return Self{1,0,0}; }
80 static Self UnitY() {return Self{0,1,0}; }
81 static Self UnitZ() {return Self{0,0,1}; }
82 _Scalar at(size_t idx) const{return data()[idx]; }
83 _Scalar& at(size_t idx) {return data()[idx]; }
84 double normalize(){return _ASTMATH normalize(*this);}
85 Self normalized() const{return _ASTMATH normalized(*this);}
86 double norm() const{return _ASTMATH norm(*this);}
87 double squaredNorm() const{return _ASTMATH squaredNorm(*this);}
88 Self cross(const Self& other) const{return _ASTMATH cross(*this, other);}
89 double dot(const Self& other) const{return _ASTMATH dot(*this, other);}
90 void setZero(){x_ = y_ = z_ = 0;}
91 Self& operator*=(Scalar s){return *this = _ASTMATH operator*(*this, s);}
92 Self operator*(Scalar s) const{return _ASTMATH operator*(*this, s);}
93 Self operator-() const{return Self{-x_, -y_, -z_};}
94 Self operator-(const Self& other) const{return _ASTMATH operator-(*this, other);}
95 Self operator+(const Self& other) const{return _ASTMATH operator+(*this, other);}
96 Self& operator+=(const Self& other){return *this = _ASTMATH operator+(*this, other);}
97 std::string toString() const;
98 _AST_DEF_VECTOR_METHOD(Scalar);
99public:
100 _Scalar x_, y_, z_; // 不要直接访问数据,设为public仅为了实现聚合初始化
101};
102
103
104template<typename _Scalar>
106{
107public:
108 VectorX();
109 VectorX(size_t size);
110 ~VectorX();
111
112 void resize(size_t size);
113 void setZero();
114 A_DEF_ITERABLE(_Scalar, data_, size_)
115public:
116 _Scalar* data_;
117 size_t size_;
118};
119
120
122
123
124template<typename _Scalar>
126 : size_{0}
127 , data_{nullptr}
128{
129}
130
131template<typename _Scalar>
132inline VectorX<_Scalar>::VectorX(size_t size)
133 : size_{ size }
134 , data_((_Scalar*)malloc(sizeof(_Scalar)* size))
135{
136
137}
138
139
140template<typename _Scalar>
141inline VectorX<_Scalar>::~VectorX()
142{
143 if(data_)
144 free(data_);
145}
146
147template<typename _Scalar>
148inline void VectorX<_Scalar>::resize(size_t size)
149{
150 if (size > size_)
151 {
152 if(data_)
153 free(data_);
154 data_ = (_Scalar*)malloc(sizeof(_Scalar) * size);
155 }
156 size_ = size;
157}
158
159template <typename _Scalar>
160inline void VectorX<_Scalar>::setZero()
161{
162 if(data_)
163 memset(data_, 0, sizeof(_Scalar) * size_);
164}
165
166typedef VectorX<double> VectorXd;
167
168
171AST_NAMESPACE_END
172
173
174AST_DECL_TYPE_ALIAS(Vector3d)
175AST_DECL_TYPE_ALIAS(VectorXd)
176
Unit s
定义 Unit.cpp:425
Unit N
牛顿
定义 Unit.cpp:437
量纲
定义 Dimension.hpp:354
定义 Vector.hpp:72
定义 Vector.hpp:60
定义 Vector.hpp:106
VectorX()
inlines
定义 Vector.hpp:125
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