/********************************************************************/ /* Copyright (c) 2017 System fugen G.K. and Yuzi Mizuno */ /* All rights reserved. */ /********************************************************************/ #ifndef _MGVector_HH_ #define _MGVector_HH_ /** @file */ /** @addtogroup BASE * @{ */ #include #include #include "mg/MGCL.h" // MGVector.h // Header for MGVector. //Forward Declaration class MGUnit_vector; class MGPosition; class MGIfstream; class MGOfstream; class MGIgesOfstream; ///Vector of a general n space dimension. class MG_DLL_DECLR MGVector { public: /// ベクトルの加算 ///Addition of two vectors. MG_DLL_DECLR friend MGVector operator+(const MGVector& vec1,const MGVector& vec2); /// ベクトルの減算 ///Subtraction of two vectors. MG_DLL_DECLR friend MGVector operator-(const MGVector& vec1,const MGVector& vec2); /// ベクトルの内積 ///Inner product of two vectors. MG_DLL_DECLR friend double operator%(const MGVector& vec1,const MGVector& vec2); ///ベクタの外積 ///vector product of two vectors. MG_DLL_DECLR friend MGVector operator*(const MGVector& vec1,const MGVector& vec2); /// スカラーの乗算を行いオブジェクトを生成 ///Scalar multiplication. MG_DLL_DECLR friend MGVector operator*(const MGVector& vec1,double scale); /// ベクトルのスカラー乗算を行いオブジェクトを生成 ///Scalar multiplication. MG_DLL_DECLR friend MGVector operator* (double, const MGVector&); /// スカラー除算を行いオブジェクトを生成 ///Scalar division. MG_DLL_DECLR friend MGVector operator/(const MGVector& vec1,double scale); ///Test if this vector is less than v2. ///Comparison depends on two vectors' length. inline friend bool operator<(const MGVector& v1,const MGVector& v2){return v1.len()(const MGVector& v1,const MGVector& v2){return v1.len()>v2.len();}; inline friend bool operator>=(const MGVector& v1,const MGVector& v2){return v1.len()>=v2.len();}; /// 与えられたベクトルの成分の値を比較し、同じであれば TRUE を返却 ///Test if two vectors are equal. MG_DLL_DECLR friend bool operator==(const MGVector& v1,const MGVector& v2); /// 与えられたベクトルの成分の値を比較し、not equalのとき TRUE を返却 ///Test if two vectors are equal. inline friend bool operator!=(const MGVector& v1,const MGVector& v2){return !(v1==v2);} ///String stream function MG_DLL_DECLR friend std::ostream& operator<< (std::ostream&, const MGVector&); /// 3つのベクトルから求められる行列の行列式の値を返却する。 ///Determinant of 3 by 3 matrix of 3 vectors. MG_DLL_DECLR friend double MGDeterminant( const MGVector& v1, const MGVector& v2, const MGVector& v3 ); ////////////// Constructor. コンストラクタ //////////// ///Void constructor. explicit MGVector(int sdim=0); ///Construct 2D vector by providing each element data. MGVector(double x, double y); ///Construct 3D vector by providing each element data. MGVector(double x, double y , double z); ///Construct 4D vector by providing each element data. MGVector(double x, double y, double z, double w); /// 初期値 v ですべてのエレメントを初期化してベクトルを生成する。 ///Vector of same value for each coordinate element. MGVector(int sdim, double v); /// double の配列でエレメントを初期化してベクトルを生成する。 ///Vector from array of double v[sdim]. ///***** This is the fundamental constructor.***** MGVector(int sdim, const double* v); ///Construct a vector from a class MGPosition. ///Vector from a position. MGVector(const MGPosition&); ///Construct a vector from a difference of two vectors. MGVector( const MGVector& dvec,/// MGVector(const std::vector& darrays); ///Construct from std::valarray ///MGVector(const std::valarray& darrays); ///Copy constructor MGVector ( const MGVector& ); //////////// Destructor ////////// ~MGVector(){if(m_sdim>3) delete[] m_element;} //////////// Operator Oveload //////////// ///Assignment MGVector& operator =(const MGVector &); ///Return i-th element of the vector. double operator() (int i) const{return ref(i);} ///Return i-th element of the vector. double operator[] (int i) const{return ref(i);} ///Access to i-th Inteval. ///This is left hand side value operator. If only regference is needed, /// operator[] should be used. double& operator()(int i); ///Update vector data by array of double. MGVector& operator=(const double*); /// 自身のベクトルに与えられたベクトルを加算して自身のベクトルとする ///Addition of two vectors. MGVector & operator+= (const MGVector&); /// 単項マイナス。自身のベクトルを反転し、オブジェクトを生成 ///Unary minus. Negate all the elements of the vector. MGVector operator- () const; /// 自身のベクトルから与えられたベクトルを減算し自身のベクトルとする ///Subtraction of two vectors. MGVector & operator -= ( const MGVector & ); /// スカラーの乗算を行い自身のベクトルとする ///Scalar multiplication. MGVector& operator*= (double scale); ///Update own vector by vector product output, changes to 3D vector. MGVector& operator*= (const MGVector& vec2); /// スカラー除算を行い自身のベクトルとする ///Scalar division. MGVector& operator/= (double scale); //////////// Member Function //////////// /// 自身のベクトルと与えられたベクトルのなす角度を Radian で返却 ///Compute angle in radian of two vectors. /// 0<= angle =0. double sangle(const MGVector& ) const; ///Get the space dimension int sdim() const { return m_sdim; }; ///Set this as a null vector. void set_null(); ///Change this to a unit vector. void set_unit(); ///Store vec2 data into *this. ///Store length is vec2.len(). ///Storing will be done rap-around. That is, if id i or j reached to ///each sdim(), the id will be changed to 0. void store_at( int i, ///