24#include "AstMath/MathOperator.hpp"
25#include "AstUtil/Constants.h"
43template<
typename _Scalar,
size_t N>
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];} \
60template<
typename _Scalar,
size_t N>
64 typedef _Scalar Scalar;
66 _AST_DEF_VECTOR_METHOD(Scalar);
72template<
typename _Scalar>
77 typedef _Scalar Scalar;
81 :x_(0), y_(0), z_(0){}
82 VectorN(
double x,
double y,
double z)
83 :x_(x), y_(y), z_(z){}
85 #ifdef AST_BUILD_LIB_PY
86 void __setitem__(
size_t idx, _Scalar v){data()[idx] = v;}
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}; }
94 _Scalar at(
size_t idx)
const{
return data()[idx]; }
95 _Scalar& at(
size_t idx) {
return data()[idx]; }
98 double norm()
const{
return _ASTMATH
norm(*
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);}
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);
118template<
typename _Scalar>
130 void resize(
size_t size);
132 A_DEF_ITERABLE(_Scalar, data_, size_)
134 _Scalar* data_{
nullptr};
142template<
typename _Scalar>
145 double normProduct = v1.norm() * v2.norm();
146 if (normProduct == 0) {
150 double dot = v1.dot(v2);
151 double threshold = normProduct * 0.9999;
152 if ((dot < -threshold) || (dot > threshold)) {
153 Self v3 = v1.cross(v2);
155 return asin(v3.norm() / normProduct);
157 return kPI - asin(v3.norm() / normProduct);
160 return acos(dot / normProduct);
163template<
typename _Scalar>
164inline VectorX<_Scalar>::VectorX()
170template<
typename _Scalar>
171inline VectorX<_Scalar>::VectorX(
size_t size)
172 : data_((_Scalar*)malloc(sizeof(_Scalar)* size))
179template<
typename _Scalar>
180inline VectorX<_Scalar>::~VectorX()
186template<
typename _Scalar>
187inline void VectorX<_Scalar>::resize(
size_t size)
193 data_ = (_Scalar*)malloc(
sizeof(_Scalar) * size);
198template <
typename _Scalar>
199inline void VectorX<_Scalar>::setZero()
202 memset(data_, 0,
sizeof(_Scalar) * size_);
205typedef VectorX<double> VectorXd;
213AST_DECL_TYPE_ALIAS(Vector3d)
214AST_DECL_TYPE_ALIAS(VectorXd)
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