vector3.h Source File

LibRPA: vector3.h Source File
LibRPA
vector3.h
1 //==========================================================
2 //
3 //==========================================================
4 #ifndef VECTOR3_H
5 #define VECTOR3_H
6 
7 // #ifdef _MCD_CHECK
8 //#include "../src_parallel/mcd.h"
9 // #endif
10 
11 #include <cmath>
12 #include <iostream>
13 #include <iomanip>
14 using namespace std;
15 
16 template <class T>
17 class Vector3
18 {
19 public:
20  T x;
21  T y;
22  T z;
23 
24  Vector3(const T &x1 = 0,const T &y1 = 0,const T &z1 = 0) :x(x1),y(y1),z(z1){};
25  Vector3(const Vector3<T> &v) :x(v.x),y(v.y),z(v.z){}; // Peize Lin add 2018-07-16
26  void set(const T &x1, const T &y1,const T &z1) { x = x1; y = y1; z = z1; }
27 
28  Vector3<T>& operator =(const Vector3<T> &u) { x=u.x; y=u.y; z=u.z; return *this; }
29  Vector3<T>& operator+=(const Vector3<T> &u) { x+=u.x; y+=u.y; z+=u.z; return *this; }
30  Vector3<T>& operator-=(const Vector3<T> &u) { x-=u.x; y-=u.y; z-=u.z; return *this; }
31  Vector3<T>& operator*=(const Vector3<T> &u);
32  Vector3<T>& operator*=(const T &s) { x*=s; y*=s; z*=s; return *this; }
33  Vector3<T>& operator/=(const Vector3<T> &u);
34  Vector3<T>& operator/=(const T &s) { x/=s; y/=s; z/=s; return *this; }
35  Vector3<T> operator -() const { return Vector3<T>(-x,-y,-z); } // Peize Lin add 2017-01-10
36 
37  T norm2(void) const { return x*x + y*y + z*z; }
38  T norm(void) const { return sqrt(norm2()); }
39  Vector3<T>& normalize(void){ const T m=norm(); x/=m; y/=m; z/=m; return *this; } // Peize Lin update return 2019-09-08
40  Vector3<T>& reverse(void){ x=-x; y=-y; z=-z; return *this; } // Peize Lin update return 2019-09-08
41 
42  void print(void)const ; // mohan add 2009-11-29
43 };
44 
45 template <class T> Vector3<T> operator+( const Vector3<T> &u, const Vector3<T> &v ) { return Vector3<T>( u.x+v.x, u.y+v.y, u.z+v.z ); }
46 template <class T> Vector3<T> operator-( const Vector3<T> &u, const Vector3<T> &v ) { return Vector3<T>( u.x-v.x, u.y-v.y, u.z-v.z ); }
47 //u.v=(ux*vx)+(uy*vy)+(uz*vz)
48 template <class T> T operator*( const Vector3<T> &u, const Vector3<T> &v ) { return ( u.x*v.x + u.y*v.y + u.z*v.z ); }
49 template <class T1, class T2, class TRet = T1> TRet operator*( const Vector3<T1> &u, const Vector3<T2> &v ) { return ( u.x*v.x + u.y*v.y + u.z*v.z ); }
50 template <class T> Vector3<T> operator*( const T &s, const Vector3<T> &u ) { return Vector3<T>( u.x*s, u.y*s, u.z*s ); }
51 template <class T> Vector3<T> operator*( const Vector3<T> &u, const T &s ) { return Vector3<T>( u.x*s, u.y*s, u.z*s ); } // mohan add 2009-5-10
52 template <class T> Vector3<T> operator/( const Vector3<T> &u, const T &s ) { return Vector3<T>( u.x/s, u.y/s, u.z/s ); }
53 //u.v=(ux*vx)+(uy*vy)+(uz*vz)
54 template <class T> T dot ( const Vector3<T> &u, const Vector3<T> &v ) { return ( u.x*v.x + u.y*v.y + u.z*v.z ); }
55 // | i j k |
56 // | ux uy uz |
57 // | vx vy vz |
58 // u.v=(uy*vz-uz*vy)i+(-ux*vz+uz*vx)j+(ux*vy-uy*vx)k
59 template <class T> Vector3<T> operator^(const Vector3<T> &u,const Vector3<T> &v)
60 {
61  return Vector3<T> ( u.y * v.z - u.z * v.y,
62  -u.x * v.z + u.z * v.x,
63  u.x * v.y - u.y * v.x);
64 }
65 // | i j k |
66 // | ux uy uz |
67 // | vx vy vz |
68 // u.v=(uy*vz-uz*vy)i+(-ux*vz+uz*vx)j+(ux*vy-uy*vzx)k
69 template <class T> Vector3<T> cross(const Vector3<T> &u,const Vector3<T> &v)
70 {
71  return Vector3<T> ( u.y * v.z - u.z * v.y,
72  -u.x * v.z + u.z * v.x,
73  u.x * v.y - u.y * v.x);
74 }
75 //s = u.(v x w)
76 //template <class T> T TripleScalarProduct(Vector3<T> u, Vector3<T> v, Vector3<T> w)
77 //{
78 // return T((u.x * (v.y * w.z - v.z * w.y)) +
79 // (u.y * (-v.x * w.z + v.z * w.x)) +
80 // (u.z * (v.x * w.y - v.y * w.x)));
81 //}
82 
83 //whether m1 != m2
84 template <class T> bool operator !=(const Vector3<T> &u, const Vector3<T> &v){ return !(u == v); }
85 //whether u == v
86 template <class T> bool operator ==(const Vector3<T> &u, const Vector3<T> &v)
87 {
88  if(u.x == v.x && u.y == v.y && u.z == v.z)
89  return true;
90  return false;
91 }
92 
93 template <class T> void Vector3<T>::print(void)const
94 {
95  cout.precision(5) ;
96  cout << "(" << setw(10) << x << "," << setw(10) << y << ","
97  << setw(10) << z << ")" << endl ;
98  return ;
99 }
100 template<class T> static std::ostream & operator << ( std::ostream &os, const Vector3<T> &u )
101 {
102  os << "(" << setw(10) << u.x << "," << setw(10) << u.y << "," << setw(10) << u.z << ")";
103  return os;
104 }
105 
106 
107 
108 #endif
Definition: vector3.h:18