24#include "AstMath/MathOperator.hpp"
25#include "AstUtil/Constants.h"
42template<
typename _Scalar,
size_t N>
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];} \
59template<
typename _Scalar,
size_t N>
63 typedef _Scalar Scalar;
65 _AST_DEF_VECTOR_METHOD(Scalar);
71template<
typename _Scalar>
76 typedef _Scalar Scalar;
80 :x_(0), y_(0), z_(0){}
81 VectorN(
double x,
double y,
double z)
82 :x_(x), y_(y), z_(z){}
84 #ifdef AST_BUILD_LIB_PY
85 void __setitem__(
size_t idx, _Scalar v){data()[idx] = v;}
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]; }
97 double norm()
const{
return _ASTMATH
norm(*
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);}
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);
117template<
typename _Scalar>
125 void resize(
size_t size);
127 A_DEF_ITERABLE(_Scalar, data_, size_)
129 _Scalar* data_{
nullptr};
137template<
typename _Scalar>
140 double normProduct = v1.norm() * v2.norm();
141 if (normProduct == 0) {
145 double dot = v1.dot(v2);
146 double threshold = normProduct * 0.9999;
147 if ((dot < -threshold) || (dot > threshold)) {
148 Self v3 = v1.cross(v2);
150 return asin(v3.norm() / normProduct);
152 return kPI - asin(v3.norm() / normProduct);
155 return acos(dot / normProduct);
158template<
typename _Scalar>
159inline VectorX<_Scalar>::VectorX()
165template<
typename _Scalar>
166inline VectorX<_Scalar>::VectorX(
size_t size)
167 : data_((_Scalar*)malloc(sizeof(_Scalar)* size))
174template<
typename _Scalar>
175inline VectorX<_Scalar>::~VectorX()
181template<
typename _Scalar>
182inline void VectorX<_Scalar>::resize(
size_t size)
188 data_ = (_Scalar*)malloc(
sizeof(_Scalar) * size);
193template <
typename _Scalar>
194inline void VectorX<_Scalar>::setZero()
197 memset(data_, 0,
sizeof(_Scalar) * size_);
200typedef VectorX<double> VectorXd;
208AST_DECL_TYPE_ALIAS(Vector3d)
209AST_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