vec3t.hpp

Go to the documentation of this file.
00001 /* Kernel Independent Fast Multipole Method
00002    Copyright (C) 2004 Lexing Ying, New York University
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2, or (at your option)
00007 any later version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00012 for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; see the file COPYING.  If not, write to the Free
00016 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017 02111-1307, USA.  */
00018 #ifndef  _VEC3T_HPP_
00019 #define  _VEC3T_HPP_
00020 
00021 #include "commoninc.hpp"
00022 
00023 using std::istream;
00024 using std::ostream;
00025 using std::min;
00026 using std::max;
00027 using std::abs;
00028 
00030 template <class F>
00032 class Vec3T {
00033 private:
00035   F _v[3];
00036 public:
00037   enum{ X=0, Y=1, Z=2 };
00038   //------------CONSTRUCTOR AND DESTRUCTOR
00040   Vec3T()              { _v[0]=F(0);    _v[1]=F(0);    _v[2]=F(0); }
00042   Vec3T(F f)           { _v[0]=f;       _v[1]=f;       _v[2]=f;}
00044   Vec3T(const F* f)    { _v[0]=f[0];    _v[1]=f[1];    _v[2]=f[2]; }
00046   Vec3T(F a,F b,F c)   { _v[0]=a;       _v[1]=b;       _v[2]=c; }
00048   Vec3T(const Vec3T& c){ _v[0]=c._v[0]; _v[1]=c._v[1]; _v[2]=c._v[2]; }
00050   ~Vec3T() {}
00051  //------------POINTER and ACCESS
00053   operator F*()             { return &_v[0]; }
00055   operator const F*() const { return &_v[0]; }
00057   F* array()                { return &_v[0]; }  //access array
00059   F& operator()(int i)             { assert(i<3); return _v[i]; }
00061   const F& operator()(int i) const { assert(i<3); return _v[i]; }
00063   F& operator[](int i)             { assert(i<3); return _v[i]; }
00065   const F& operator[](int i) const { assert(i<3); return _v[i]; }
00067   F& x()             { return _v[0];}
00069   F& y()             { return _v[1];}
00071   F& z()             { return _v[2];}
00073   const F& x() const { return _v[0];}
00075   const F& y() const { return _v[1];}
00077   const F& z() const { return _v[2];}
00078   //------------ASSIGN
00080   Vec3T& operator= ( const Vec3T& c ) { _v[0] =c._v[0]; _v[1] =c._v[1]; _v[2] =c._v[2]; return *this; }
00082   Vec3T& operator+=( const Vec3T& c ) { _v[0]+=c._v[0]; _v[1]+=c._v[1]; _v[2]+=c._v[2]; return *this; }
00084   Vec3T& operator-=( const Vec3T& c ) { _v[0]-=c._v[0]; _v[1]-=c._v[1]; _v[2]-=c._v[2]; return *this; }
00086   Vec3T& operator*=( const F& s )     { _v[0]*=s;       _v[1]*=s;       _v[2]*=s;       return *this; }
00088   Vec3T& operator/=( const F& s )     { _v[0]/=s;       _v[1]/=s;       _v[2]/=s;       return *this; }
00089   //-----------LENGTH...
00091   F l1( void )     const  { F sum=F(0); for(int i=0; i<3; i++) sum=sum+abs(_v[i]); return sum; }
00093   F linfty( void ) const  { F cur=F(0); for(int i=0; i<3; i++) cur=max(cur,abs(_v[i])); return cur; }
00095   F l2( void )     const  { F sum=F(0); for(int i=0; i<3; i++) sum=sum+_v[i]*_v[i]; return sqrt(sum); }
00097   F length( void ) const  { return l2(); }
00099   Vec3T dir( void )    const  { F a=l2(); return (*this)/a; }
00100 };
00101 //-----------BOOLEAN OPS
00103 template <class F> inline bool operator==(const Vec3T<F>& a, const Vec3T<F>& b) {
00104   bool res = true;  for(int i=0; i<3; i++)   res = res && (a(i)==b(i));  return res;
00105 }
00107 template <class F> inline bool operator!=(const Vec3T<F>& a, const Vec3T<F>& b) {
00108   return !(a==b);
00109 }
00111 template <class F> inline bool operator> (const Vec3T<F>& a, const Vec3T<F>& b) {
00112   bool res = true;  for(int i=0; i<3; i++)   res = res && (a(i)> b(i));  return res; 
00113 }
00115 template <class F> inline bool operator< (const Vec3T<F>& a, const Vec3T<F>& b) {
00116   bool res = true;  for(int i=0; i<3; i++)   res = res && (a(i)< b(i));  return res; 
00117 }
00119 template <class F> inline bool operator>=(const Vec3T<F>& a, const Vec3T<F>& b) {
00120   bool res = true;  for(int i=0; i<3; i++)      res = res && (a(i)>=b(i));  return res; 
00121 }
00123 template <class F> inline bool operator<=(const Vec3T<F>& a, const Vec3T<F>& b) {
00124   bool res = true;  for(int i=0; i<3; i++)   res = res && (a(i)<=b(i));  return res; 
00125 }
00126 
00127 //-----------NUMERICAL OPS
00129 template <class F> inline Vec3T<F> operator- (const Vec3T<F>& a) {
00130   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = -a[i]; return r;
00131 }
00133 template <class F> inline Vec3T<F> operator+ (const Vec3T<F>& a, const Vec3T<F>& b) {
00134   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = a[i]+b[i]; return r; 
00135 }
00137 template <class F> inline Vec3T<F> operator- (const Vec3T<F>& a, const Vec3T<F>& b) {
00138   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = a[i]-b[i]; return r;
00139 }
00141 template <class F> inline Vec3T<F> operator* (F scl, const Vec3T<F>& a) {
00142   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = scl*a[i];  return r;
00143 }
00145 template <class F> inline Vec3T<F> operator* (const Vec3T<F>& a, F scl) {
00146   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = scl*a[i];  return r;
00147 }
00149 template <class F> inline Vec3T<F> operator/ (const Vec3T<F>& a, F scl) {
00150   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = a[i]/scl;  return r;
00151 }
00153 template <class F> inline F operator* (const Vec3T<F>& a, const Vec3T<F>& b) {
00154   F sum=F(0); for(int i=0; i<3; i++) sum=sum+a(i)*b(i); return sum;
00155 }
00157 template <class F> inline F dot       (const Vec3T<F>& a, const Vec3T<F>& b) {
00158   return a*b;
00159 }
00161 template <class F> inline Vec3T<F> operator^ (const Vec3T<F>& a, const Vec3T<F>& b) {
00162   return Vec3T<F>(a(1)*b(2)-a(2)*b(1), a(2)*b(0)-a(0)*b(2), a(0)*b(1)-a(1)*b(0)); 
00163 }
00165 template <class F> inline Vec3T<F> cross     (const Vec3T<F>& a, const Vec3T<F>& b) { 
00166   return a^b; 
00167 }
00168 //-------------ew OPS
00170 template <class F> inline Vec3T<F> min(const Vec3T<F>& a, const Vec3T<F>& b) {
00171   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = min(a[i], b[i]); return r;
00172 }
00174 template <class F> inline Vec3T<F> max(const Vec3T<F>& a, const Vec3T<F>& b) {
00175   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = max(a[i], b[i]); return r;
00176 }
00178 template <class F> inline Vec3T<F> abs(const Vec3T<F>& a) {
00179   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = abs(a[i]); return r;
00180 }
00182 template <class F> inline Vec3T<F> ewmul(const Vec3T<F>&a, const Vec3T<F>& b) {
00183   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = a[i]*b[i]; return r;
00184 }
00186 template <class F> inline Vec3T<F> ewdiv(const Vec3T<F>&a, const Vec3T<F>& b) { 
00187   Vec3T<F> r;  for(int i=0; i<3; i++) r[i] = a[i]/b[i]; return r;
00188 }
00189 //---------------INOUT
00191 template <class F> istream& operator>>(istream& is, Vec3T<F>& a) {
00192   for(int i=0; i<3; i++) is>>a[i]; return is;
00193 }
00195 template <class F> ostream& operator<<(ostream& os, const Vec3T<F>& a) { 
00196   for(int i=0; i<3; i++) os<<a[i]<<" "; return os;
00197 }
00198 
00199 //---------------------------------------------------------
00201 
00202 typedef Vec3T<double> Point3;
00204 typedef Vec3T<int>    Index3;
00205 
00206 #endif

Generated on Sun Dec 4 18:13:13 2005 for common by  doxygen 1.4.5