moab
|
00001 #ifndef MB_CART_VECT_HPP 00002 #define MB_CART_VECT_HPP 00003 00004 #include <cmath> 00005 #include <iosfwd> 00006 00007 namespace moab { 00008 00009 00013 class CartVect 00014 { 00015 private: 00016 double d[3]; 00017 00018 public: 00019 00020 inline CartVect() 00021 { } 00023 explicit inline CartVect( double v ) 00024 { d[0] = d[1] = d[2] = v; } 00025 inline CartVect( double i, double j, double k ) 00026 { d[0] = i; d[1] = j; d[2] = k; } 00028 explicit inline CartVect( const double a[3] ) 00029 { d[0] = a[0]; d[1] = a[1]; d[2] = a[2]; } 00030 inline CartVect& operator=( const double v[3] ) 00031 { d[0]= v[0]; d[1] = v[1]; d[2] = v[2]; return *this; } 00032 00033 inline double& operator[]( unsigned i ) 00034 { return d[i]; } 00035 inline double operator[]( unsigned i ) const 00036 { return d[i]; } 00037 00038 inline CartVect& operator+=( const CartVect& v ) 00039 { d[0] += v.d[0]; d[1] += v.d[1]; d[2] += v.d[2]; return *this; } 00040 inline CartVect& operator-=( const CartVect& v ) 00041 { d[0] -= v.d[0]; d[1] -= v.d[1]; d[2] -= v.d[2]; return *this; } 00043 inline CartVect& operator*=( const CartVect& v ); 00044 00045 inline CartVect& operator+=( double s ) 00046 { d[0] += s; d[1] += s; d[2] += s; return *this; } 00047 inline CartVect& operator-=( double s ) 00048 { d[0] -= s; d[1] -= s; d[2] -= s; return *this; } 00049 inline CartVect& operator*=( double s ) 00050 { d[0] *= s; d[1] *= s; d[2] *= s; return *this; } 00051 inline CartVect& operator/=( double s ) 00052 { d[0] /= s; d[1] /= s; d[2] /= s; return *this; } 00053 inline bool operator==(const CartVect& v ) const 00054 { return d[0] == v[0] && d[1] == v[1] && d[2] == v[2]; } 00055 00056 inline double length() const; 00057 00058 inline double length_squared() const; 00059 00060 inline void normalize(); 00061 00062 inline void flip(); 00063 00065 inline void scale( const CartVect& v ) 00066 { d[0] *= v.d[0]; d[1] *= v.d[1]; d[2] *= v.d[2]; } 00067 00068 // get pointer to array of three doubles 00069 inline double* array() 00070 { return d; } 00071 inline const double* array() const 00072 { return d; } 00073 00075 inline void get( double v[3] ) const 00076 { v[0] = d[0]; v[1] = d[1]; v[2] = d[2]; } 00077 }; 00078 00079 inline CartVect operator+( const CartVect& u, const CartVect& v ) 00080 { return CartVect( u[0] + v[0], u[1] + v[1], u[2] + v[2] ); } 00081 00082 inline CartVect operator-( const CartVect& u, const CartVect& v ) 00083 { return CartVect( u[0] - v[0], u[1] - v[1], u[2] - v[2] ); } 00084 00086 inline CartVect operator*( const CartVect& u, const CartVect& v ) 00087 { 00088 return CartVect( u[1] * v[2] - u[2] * v[1], 00089 u[2] * v[0] - u[0] * v[2], 00090 u[0] * v[1] - u[1] * v[0] ); 00091 } 00092 00094 inline double operator%( const CartVect& u, const CartVect& v ) 00095 { return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]; } 00096 00097 inline CartVect& CartVect::operator*=( const CartVect& v ) 00098 { return *this = *this * v; } 00099 00100 inline double CartVect::length() const 00101 { return std::sqrt( *this % *this ); } 00102 00103 inline double CartVect::length_squared() const 00104 { return d[0]*d[0] + d[1]*d[1] + d[2]*d[2]; } 00105 00106 inline void CartVect::normalize() 00107 { *this /= length(); } 00108 00109 inline void CartVect::flip() 00110 { d[0] = -d[0]; d[1] = -d[1]; d[2] = -d[2]; } 00111 00113 inline double angle( const CartVect& u, const CartVect& v ) 00114 { 00115 double tmp=(u % v) / std::sqrt((u % u) * (v % v)); 00116 if (tmp>1.) tmp=1.; 00117 if (tmp<-1.) tmp = -1.; 00118 return std::acos( tmp ); 00119 } 00120 00121 inline CartVect operator-( const CartVect& v ) 00122 { return CartVect( -v[0], -v[1], -v[2] ); } 00123 inline CartVect operator+( const CartVect& v, double s ) 00124 { return CartVect( v[0] + s, v[1] + s, v[2] + s ); } 00125 inline CartVect operator-( const CartVect& v, double s ) 00126 { return CartVect( v[0] - s, v[1] - s, v[2] - s ); } 00127 inline CartVect operator*( const CartVect& v, double s ) 00128 { return CartVect( v[0] * s, v[1] * s, v[2] * s ); } 00129 inline CartVect operator/( const CartVect& v, double s ) 00130 { return CartVect( v[0] / s, v[1] / s, v[2] / s ); } 00131 inline CartVect operator+( double s, const CartVect& v ) 00132 { return CartVect( v[0] + s, v[1] + s, v[2] + s ); } 00133 inline CartVect operator-( double s, const CartVect& v ) 00134 { return CartVect( v[0] - s, v[1] - s, v[2] - s ); } 00135 inline CartVect operator*( double s, const CartVect& v ) 00136 { return CartVect( v[0] * s, v[1] * s, v[2] * s ); } 00137 00139 inline CartVect unit( const CartVect& v ) 00140 { 00141 const double len = v.length(); 00142 return CartVect( v[0] / len, v[1] / len, v[2] / len ); 00143 } 00144 00145 00146 std::ostream& operator<<( std::ostream& s, const CartVect& v ); 00147 00148 } // namespace moab 00149 00150 #endif