Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:31:56

0001 #ifndef ATOOLS_Math_Vec3_H
0002 #define ATOOLS_Math_Vec3_H
0003 
0004 #include "ATOOLS/Math/MathTools.H"
0005  
0006 namespace ATOOLS {
0007   template<typename Scalar> class Vec4;
0008   template<typename Scalar> class Vec3;
0009 
0010   template<typename Scalar>
0011   class Vec3 {
0012     Scalar m_x[3];
0013   public:
0014     inline Vec3() {
0015       m_x[0]=m_x[1]=m_x[2]=Scalar(0.0);
0016     }
0017     inline Vec3(const Vec3<Scalar>& vec) {
0018       m_x[0]=vec.m_x[0];
0019       m_x[1]=vec.m_x[1];
0020       m_x[2]=vec.m_x[2];
0021     }
0022     /*!
0023      \brief Special Constructor taking 3 single components
0024     */
0025     inline Vec3(const Scalar& x, const Scalar& y, const Scalar& z) {
0026       m_x[0]=x;
0027       m_x[1]=y;
0028       m_x[2]=z;
0029     }
0030     /*!
0031      \brief Special Constructor extracting the space part of a minkowski vector
0032     */
0033     inline Vec3(const Vec4<Scalar>& v) {
0034       m_x[0]=v[1];
0035       m_x[1]=v[2];
0036       m_x[2]=v[3];
0037     }
0038 
0039     bool Nan() const {
0040       for(short unsigned int i(0);i<3;++i)
0041         if (ATOOLS::IsNan<Scalar>(m_x[i])) return true;
0042       return false;
0043     }
0044 
0045     bool IsZero() const {
0046       for(short unsigned int i(0);i<3;++i)
0047         if (!ATOOLS::IsZero<Scalar>(m_x[i])) return false;
0048       return true;
0049     }
0050 
0051     inline const Scalar Abs() const { return sqrt(Sqr()); }
0052     inline const Scalar Sqr() const {
0053       return m_x[0]*m_x[0]+m_x[1]*m_x[1]+m_x[2]*m_x[2];
0054     }
0055 
0056     inline Scalar& operator[] (int i) { return m_x[--i]; }
0057     inline const Scalar operator[] (int i) const { return m_x[--i]; }
0058 
0059     template<typename Scalar2> inline PROMOTE(Scalar,Scalar2)
0060     operator*(const Vec3<Scalar2>& v2) const {
0061       return m_x[0]*v2[1]+m_x[1]*v2[2]+m_x[2]*v2[3];
0062     }
0063 
0064     template<typename Scalar2> inline Vec3<PROMOTE(Scalar,Scalar2)>
0065     operator+ (const Vec3<Scalar2>& v2) const {
0066       return Vec3<PROMOTE(Scalar,Scalar2)>
0067         (m_x[0]+v2[1],m_x[1]+v2[2],m_x[2]+v2[3]);
0068     }
0069 
0070     inline Vec3<Scalar>& operator+=(const Vec3<Scalar>& v) {
0071       m_x[0] += v[1];
0072       m_x[1] += v[2];
0073       m_x[2] += v[3];
0074       return *this;
0075     }
0076 
0077     inline Vec3<Scalar>& operator-=(const Vec3<Scalar>& v) {
0078       m_x[0] -= v[1];
0079       m_x[1] -= v[2];
0080       m_x[2] -= v[3];
0081       return *this;
0082     }
0083 
0084     template<typename Scalar2> inline Vec3<PROMOTE(Scalar,Scalar2)>
0085     operator- (const Vec3<Scalar2>& v2) const {
0086       return Vec3<PROMOTE(Scalar,Scalar2)>
0087         (m_x[0]-v2[1],m_x[1]-v2[2],m_x[2]-v2[3]);
0088     }
0089 
0090     template<typename Scalar2> inline Vec3<PROMOTE(Scalar,Scalar2)>
0091     operator* (const Scalar2& s) const {
0092       return Vec3<PROMOTE(Scalar,Scalar2)>
0093         (s*m_x[0],s*m_x[1],s*m_x[2]);
0094     }
0095 
0096     inline Vec3<Scalar>& operator*= (const Scalar& scal) {
0097       m_x[0] *= scal;
0098       m_x[1] *= scal;
0099       m_x[2] *= scal;
0100       return *this;
0101     }
0102 
0103     inline Vec3<Scalar> operator-() const {
0104       return Vec3<Scalar>(-m_x[0],-m_x[1],-m_x[2]);
0105     }
0106 
0107     template<typename Scalar2> inline Vec3<PROMOTE(Scalar,Scalar2)>
0108     operator/ (const Scalar2& scal) const {
0109       return Vec3<PROMOTE(Scalar,Scalar2)>(m_x[0]/scal,m_x[1]/scal,m_x[2]/scal);
0110     }
0111 
0112     // standard vectors:
0113     const static Vec3<Scalar> XVEC;
0114     const static Vec3<Scalar> YVEC;
0115     const static Vec3<Scalar> ZVEC;
0116   };
0117 
0118   template<typename Scalar,typename Scal2> inline Vec3<PROMOTE(Scalar,Scal2)>
0119   operator* (const Scal2& s, const Vec3<Scalar>& v) {
0120     return v*s;
0121   }
0122 
0123   template<typename Scalar>
0124   inline Vec3<Scalar> cross(const Vec3<Scalar>& v1, const  Vec3<Scalar>& v2)
0125   {
0126     return Vec3<Scalar>(v1[2]*v2[3]-v1[3]*v2[2],
0127                         v1[3]*v2[1]-v1[1]*v2[3],
0128                         v1[1]*v2[2]-v1[2]*v2[1]);
0129   }
0130 
0131   template<typename Scalar>
0132   std::ostream& operator<<(std::ostream& s, const Vec3<Scalar>& vec) {
0133     return s<<'('<<vec[1]<<','<<vec[2]<<','<<vec[3]<<')';
0134   }
0135 
0136     
0137 }
0138 
0139 /*!
0140  \file
0141  \brief   contains class Vec3
0142 */
0143 
0144 /*!
0145  \class Vec3<Scalar>
0146  \brief implementation of a 3 dimensional Euclidean vector and its algebraic
0147  operations
0148 
0149  This class can be used as a 3 dimensional vector with arbitrary scalar types.
0150  All necessary operations, e.g. addition, scalar product, cross product, 
0151  etc. are available.
0152  If you want to use this vector for a new scalar type, you might have to
0153  implement specific functions for this type in MathTools.H.
0154  "Fallback" types for operations with two vectors of different types, e. g.
0155  "complex and double become complex" have to be specified in MathTools.H as
0156  well, see the DECLARE_PROMOTE macro.
0157 */
0158 
0159 /*!
0160  \fn Vec3::Vec3()
0161  \brief Standard Constructor
0162 */
0163 
0164 /*!
0165  \fn Vec3::Vec3(const Scalar &x, const Scalar &y, const Scalar &z){
0166  \brief Special Constructor taking 3 single components
0167 */
0168 
0169 /*!
0170  \fn ATOOLS::Vec3<Scalar>::Vec3(const Vec3<Scalar>& v)
0171  \brief Special Constructor extracting the space part of a minkowski vector
0172 */
0173 
0174 /*!
0175  \fn inline const double Vec3::Abs() const
0176  \brief returns \f$ \sqrt{x^2 + y^2 + z^2} \f$.
0177 */
0178 
0179 /*!
0180  \fn inline const double Vec3::Sqr() const
0181  \brief returns \f$ x^2 + y^2 + z^2 \f$.
0182 */
0183 
0184 /*!
0185  \fn   inline double& Vec3::operator[] (int i)
0186  \brief returns x,y,z for i=1,2,3 resp. (May be manipulated.)
0187 */
0188 
0189 /*!
0190  \fn   inline const double Vec3::operator[] (int i) const
0191  \brief returns x,y,z for i=1,2,3 resp.
0192 */
0193 
0194 #endif