vec2t.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  _VEC2T_HPP_
00019 #define  _VEC2T_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 
00029 //-----------------------------------------------------------------------------------------------------
00031 template <class F>
00033 class Vec2T {
00034 private:
00036   F _v[2];
00037 public:
00038   enum{ X=0, Y=1 };
00039   //------------CONSTRUCTOR AND DESTRUCTOR
00041   Vec2T()              { _v[0]=F(0);    _v[1]=F(0); }
00043   Vec2T(F f)           { _v[0]=f;       _v[1]=f; }
00045   Vec2T(const F* f)    { _v[0]=f[0];    _v[1]=f[1]; }
00047   Vec2T(F a,F b)       { _v[0]=a;       _v[1]=b; }
00049   Vec2T(const Vec2T& c){ _v[0]=c._v[0]; _v[1]=c._v[1]; }
00051   ~Vec2T() {}
00052   //------------POINTER and ACCESS
00054   operator F*()             { return &_v[0]; }
00056   operator const F*() const { return &_v[0]; }
00058   F* array()                { return &_v[0]; }  //access array
00060   F& operator()(int i)             { assert(i<2); return _v[i]; }
00062   const F& operator()(int i) const { assert(i<2); return _v[i]; }
00064   F& operator[](int i)             { assert(i<2); return _v[i]; }
00066   const F& operator[](int i) const { assert(i<2); return _v[i]; }
00068   F& x()             { return _v[0];}
00070   F& y()             { return _v[1];}
00072   const F& x() const { return _v[0];}
00074   const F& y() const { return _v[1];}
00075   //------------ASSIGN
00077   Vec2T& operator= ( const Vec2T& c ) { _v[0] =c._v[0]; _v[1] =c._v[1]; return *this; }
00079   Vec2T& operator+=( const Vec2T& c ) { _v[0]+=c._v[0]; _v[1]+=c._v[1]; return *this; }
00081   Vec2T& operator-=( const Vec2T& c ) { _v[0]-=c._v[0]; _v[1]-=c._v[1]; return *this; }
00083   Vec2T& operator*=( const F& s )     { _v[0]*=s;       _v[1]*=s;       return *this; }
00085   Vec2T& operator/=( const F& s )     { _v[0]/=s;       _v[1]/=s;       return *this; }
00086   //-----------LENGTH...
00088   F l1( void )     const  { F sum=F(0); for(int i=0; i<2; i++) sum=sum+abs(_v[i]); return sum; }
00090   F linfty( void ) const  { F cur=F(0); for(int i=0; i<2; i++) cur=max(cur,abs(_v[i])); return cur; }
00092   F l2( void )     const  { F sum=F(0); for(int i=0; i<2; i++) sum=sum+_v[i]*_v[i]; return sqrt(sum); }
00094   F length( void ) const  { return l2(); }
00096   Vec2T dir( void )    const  { F a=l2(); return (*this)/a; }
00097 };
00098 
00099 //-----------BOOLEAN OPS
00101 template <class F> inline bool operator==(const Vec2T<F>& a, const Vec2T<F>& b) {
00102   bool res = true;  for(int i=0; i<2; i++)   res = res && (a(i)==b(i));  return res;
00103 }
00105 template <class F> inline bool operator!=(const Vec2T<F>& a, const Vec2T<F>& b) {
00106   return !(a==b);
00107 }
00109 template <class F> inline bool operator> (const Vec2T<F>& a, const Vec2T<F>& b) {
00110   bool res = true;  for(int i=0; i<2; i++)   res = res && (a(i)> b(i));  return res; 
00111 }
00113 template <class F> inline bool operator< (const Vec2T<F>& a, const Vec2T<F>& b) {
00114   bool res = true;  for(int i=0; i<2; i++)   res = res && (a(i)< b(i));  return res; 
00115 }
00117 template <class F> inline bool operator>=(const Vec2T<F>& a, const Vec2T<F>& b) {
00118   bool res = true;  for(int i=0; i<2; i++)      res = res && (a(i)>=b(i));  return res; 
00119 }
00121 template <class F> inline bool operator<=(const Vec2T<F>& a, const Vec2T<F>& b) {
00122   bool res = true;  for(int i=0; i<2; i++)   res = res && (a(i)<=b(i));  return res; 
00123 }
00124 
00125 //-----------NUMERICAL OPS
00127 template <class F> inline Vec2T<F> operator- (const Vec2T<F>& a) {
00128   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = -a[i]; return r;
00129 }
00131 template <class F> inline Vec2T<F> operator+ (const Vec2T<F>& a, const Vec2T<F>& b) {
00132   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = a[i]+b[i]; return r; 
00133 }
00135 template <class F> inline Vec2T<F> operator- (const Vec2T<F>& a, const Vec2T<F>& b) {
00136   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = a[i]-b[i]; return r;
00137 }
00139 template <class F> inline Vec2T<F> operator* (F scl, const Vec2T<F>& a) {
00140   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = scl*a[i];  return r;
00141 }
00143 template <class F> inline Vec2T<F> operator* (const Vec2T<F>& a, F scl) {
00144   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = scl*a[i];  return r;
00145 }
00147 template <class F> inline Vec2T<F> operator/ (const Vec2T<F>& a, F scl) {
00148   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = a[i]/scl;  return r;
00149 }
00151 template <class F> inline F operator* (const Vec2T<F>& a, const Vec2T<F>& b) {
00152   F sum=F(0); for(int i=0; i<2; i++) sum=sum+a(i)*b(i); return sum;
00153 }
00155 template <class F> inline F dot       (const Vec2T<F>& a, const Vec2T<F>& b) {
00156   return a*b;
00157 }
00158 //-------------ew OPS
00160 template <class F> inline Vec2T<F> min(const Vec2T<F>& a, const Vec2T<F>& b) {
00161   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = min(a[i], b[i]); return r;
00162 }
00164 template <class F> inline Vec2T<F> max(const Vec2T<F>& a, const Vec2T<F>& b) {
00165   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = max(a[i], b[i]); return r;
00166 }
00168 template <class F> inline Vec2T<F> abs(const Vec2T<F>& a) {
00169   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = abs(a[i]); return r;
00170 }
00172 template <class F> inline Vec2T<F> ewmul(const Vec2T<F>&a, const Vec2T<F>& b) {
00173   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = a[i]*b[i]; return r;
00174 }
00176 template <class F> inline Vec2T<F> ewdiv(const Vec2T<F>&a, const Vec2T<F>& b) { 
00177   Vec2T<F> r;  for(int i=0; i<2; i++) r[i] = a[i]/b[i]; return r;
00178 }
00179 //---------------INOUT
00181 template <class F> istream& operator>>(istream& is, Vec2T<F>& a) {
00182   for(int i=0; i<2; i++) is>>a[i]; return is;
00183 }
00185 template <class F> ostream& operator<<(ostream& os, const Vec2T<F>& a) { 
00186   for(int i=0; i<2; i++) os<<a[i]<<" "; return os;
00187 }
00188 
00189 //---------------------------------------------------------
00191 
00192 typedef Vec2T<double> Point2;
00194 typedef Vec2T<int>    Index2;
00195 
00196 
00197 
00198 #endif

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