AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

Vector3.h

Go to the documentation of this file.
00001 #ifndef AMROC_VECTOR3_H
00002 #define AMROC_VECTOR3_H
00003 
00011 #include "CompConf.h"
00012 #include "generic.h"
00013 #include <iostream.h>
00014 #include <assert.h>
00015  
00016 #ifndef VectorName
00017 #define Vector(dim)      name2(Vector,dim)
00018 #define VectorName
00019 #endif
00020 
00028 template <class DataType>
00029 class Vector(3) {
00030   typedef Vector(3)<DataType> TVector;
00031 
00032 public:
00033   typedef DataType InternalDataType;
00034 
00035   Vector(3)() {}
00036   Vector(3)(const TVector& x) { 
00037     memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00038   }
00039   Vector(3)(const DataType& v) { 
00040     data_[0] = v; 
00041     data_[1] = v; 
00042     data_[2] = v; 
00043   }
00044 
00045   ~Vector(3)() {}
00046   
00047   DataType * data() { return data_; }
00048   const DataType * data() const { return data_; }
00049   int length() const { return 3; }
00050   static int Length() { return 3; }
00051 
00052   inline DataType operator()(int i) const {
00053 #ifdef DEBUG_PRINT
00054     assert((i >= 0) && (i < 3)); 
00055 #endif
00056     return data_[i];
00057   }
00058   inline DataType& operator()(int i) { 
00059 #ifdef DEBUG_PRINT
00060     assert((i >= 0) && (i < 3)); 
00061 #endif
00062     return data_[i];
00063   }
00064 
00065   inline DataType operator[](int i) const {
00066 #ifdef DEBUG_PRINT
00067     assert((i >= 0) && (i < 3)); 
00068 #endif
00069     return data_[i];
00070   }
00071   inline DataType& operator[](int i) { 
00072 #ifdef DEBUG_PRINT
00073     assert((i >= 0) && (i < 3)); 
00074 #endif
00075     return data_[i];
00076   }
00077 
00078   inline TVector& operator=(const TVector& x) { 
00079     memcpy((void *) data_, (void *) x.data_, sizeof(TVector));
00080     return *this; 
00081   }
00082   inline TVector& operator=(const DataType& v) { 
00083     data_[0] = v; 
00084     data_[1] = v; 
00085     data_[2] = v; 
00086     return *this; 
00087   }
00088 
00089 #define Vector_Vector_Operator(ope,op)                          \
00090    inline TVector& operator ope (const TVector &x)              \
00091      {                                                          \
00092       data_[0] ope x.data_[0];                                  \
00093       data_[1] ope x.data_[1];                                  \
00094       data_[2] ope x.data_[2];                                  \
00095       return(*this);                                            \
00096      }                                                          \
00097    inline TVector operator op (const TVector &x) const          \
00098      {                                                          \
00099       TVector ret(*this);                                       \
00100       ret.data_[0] ope x.data_[0];                              \
00101       ret.data_[1] ope x.data_[1];                              \
00102       ret.data_[2] ope x.data_[2];                              \
00103       return(ret);                                              \
00104      }
00105    Vector_Vector_Operator(+=,+)
00106    Vector_Vector_Operator(-=,-)
00107    Vector_Vector_Operator(*=,*)
00108    Vector_Vector_Operator(/=,/)
00109 #undef Vector_Vector_Operator
00110 
00111 #define Vector_Scalar_Operator(ope,op)                          \
00112    inline TVector& operator ope (const DataType &v)             \
00113      {                                                          \
00114       data_[0] ope v;                                           \
00115       data_[1] ope v;                                           \
00116       data_[2] ope v;                                           \
00117       return(*this);                                            \
00118      }                                                          \
00119    inline TVector operator op (const DataType &v) const         \
00120      {                                                          \
00121       TVector ret(*this);                                       \
00122       ret.data_[0] ope v;                                       \
00123       ret.data_[1] ope v;                                       \
00124       ret.data_[2] ope v;                                       \
00125      return(ret);                                               \
00126      }
00127    Vector_Scalar_Operator(+=,+)
00128    Vector_Scalar_Operator(-=,-)
00129    Vector_Scalar_Operator(*=,*)
00130    Vector_Scalar_Operator(/=,/)
00131 #undef Vector_Scalar_Operator
00132 
00133    /* Define the negation operator */
00134    inline TVector operator - () const
00135      { 
00136        TVector ret;
00137        ret.data_[0] = -data_[0];
00138        ret.data_[1] = -data_[1];
00139        ret.data_[2] = -data_[2];
00140        return(ret);
00141      }
00142 
00143 #define Vector_Vector_RelOperator(op)                           \
00144    inline int operator op (const TVector &x) const              \
00145      { return(                                                  \
00146                (data_[0] op x.data_[0])                         \
00147             && (data_[1] op x.data_[1])                         \
00148             && (data_[2] op x.data_[2])                         \
00149              ) ; }                     
00150 
00151    Vector_Vector_RelOperator(==)
00152    Vector_Vector_RelOperator(!=)
00153 #undef Vector_Vector_RelOperator
00154 
00155 #define Vector_Vector_RelOperator(op)                           \
00156    inline int operator op (const TVector &x) const              \
00157      { return(                                                  \
00158                (data_[0] op x.data_[0])                         \
00159             || (data_[1] op x.data_[1])                         \
00160             || (data_[2] op x.data_[2])                         \
00161              ) ; }                     
00162 
00163    Vector_Vector_RelOperator(>)
00164    Vector_Vector_RelOperator(<)
00165    Vector_Vector_RelOperator(>=)
00166    Vector_Vector_RelOperator(<=)
00167 #undef Vector_Vector_RelOperator
00168 
00169 #define Vector_Scalar_RelOperator(op)                           \
00170    inline int operator op (const DataType &v) const             \
00171      { return(                                                  \
00172                (data_[0] op v)                                  \
00173             && (data_[1] op v)                                  \
00174             && (data_[2] op v)                                  \
00175              ); }                     
00176 
00177    Vector_Scalar_RelOperator(==)
00178    Vector_Scalar_RelOperator(!=)
00179 #undef Vector_Scalar_RelOperator
00180 
00181 #define Vector_Scalar_RelOperator(op)                           \
00182    inline int operator op (const DataType &v) const             \
00183      { return(                                                  \
00184                (data_[0] op v)                                  \
00185             || (data_[1] op v)                                  \
00186             || (data_[2] op v)                                  \
00187               ); }                     
00188 
00189    Vector_Scalar_RelOperator(>)
00190    Vector_Scalar_RelOperator(<)
00191    Vector_Scalar_RelOperator(>=)
00192    Vector_Scalar_RelOperator(<=)
00193 #undef Vector_Scalar_RelOperator
00194 
00195    /* Define elementwise minimum and maximum functions for Vectors */
00196    inline void min(const TVector &x) { 
00197      data_[0] = (data_[0] < x.data_[0]) ? data_[0] : x.data_[0]; 
00198      data_[1] = (data_[1] < x.data_[1]) ? data_[1] : x.data_[1]; 
00199      data_[2] = (data_[2] < x.data_[2]) ? data_[2] : x.data_[2]; 
00200    }
00201    inline void max(const TVector &x) {
00202      data_[0] = (data_[0] > x.data_[0]) ? data_[0] : x.data_[0]; 
00203      data_[1] = (data_[1] > x.data_[1]) ? data_[1] : x.data_[1]; 
00204      data_[2] = (data_[2] > x.data_[2]) ? data_[2] : x.data_[2]; 
00205    }
00206 
00207    /* Define minimum and maximum of components */
00208    inline DataType mincomp() const { 
00209      DataType min;
00210      min = data_[0];
00211      if (data_[1] < min) min = data_[1]; 
00212      if (data_[2] < min) min = data_[2]; 
00213      return min;
00214    }
00215    inline DataType maxcomp() const { 
00216      DataType max;
00217      max = data_[0];
00218      if (data_[1] > max) max = data_[1]; 
00219      if (data_[2] > max) max = data_[2]; 
00220      return max;
00221    }
00222 
00223    inline double abs() const {
00224      DataType s = data_[0]*data_[0];
00225      s += data_[1]*data_[1]; 
00226      s += data_[2]*data_[2]; 
00227      return (sqrt(double(s)));
00228    }
00229 
00230    inline TVector getmin(const TVector &x) const
00231      { TVector ret(*this); ret.min(x); return(ret); }
00232    inline TVector getmax(const TVector &x) const
00233      { TVector ret(*this); ret.max(x); return(ret); }
00234 
00235 public:
00236    DataType data_[3];
00237 };
00238 
00239 
00240 template <class DataType>
00241 inline ostream&  operator << (ostream& os, const Vector(3)<DataType> &v) {
00242   os << "(" 
00243      << v.data_[0] 
00244      << "," << v.data_[1] 
00245      << "," << v.data_[2] 
00246      << ")";
00247   return os;
00248 }
00249 
00250 template <class DataType>
00251 inline Vector(3)<DataType> Min(const Vector(3)<DataType> &x, 
00252                                    const Vector(3)<DataType> &y) 
00253   { Vector(3)<DataType> ret(x); ret.min(y); return(ret); }
00254 
00255 template <class DataType>
00256 inline Vector(3)<DataType> Max(const Vector(3)<DataType> &x, 
00257                                    const Vector(3)<DataType> &y) 
00258   { Vector(3)<DataType> ret(x); ret.max(y); return(ret); }
00259 
00260 template <class DataType>
00261 inline double abs(const Vector(3)<DataType> &x) 
00262   { return (x.abs()); }
00263 
00264 template <class DataType>
00265 inline DataType mincomp(const Vector(3)<DataType> &x) 
00266   { return(x.mincomp()); }
00267 
00268 template <class DataType>
00269 inline DataType maxcomp(const Vector(3)<DataType> &x) 
00270   { return(x.maxcomp()); }
00271 
00272 
00273 #define Global_Vector_Scalar_Operator(op)                                  \
00274 template <class DataType>                                                  \
00275 inline Vector(3)<DataType> operator op (const DataType &v,                 \
00276    const Vector(3)<DataType> &x)                                           \
00277    { return(x op v); }
00278 
00279 Global_Vector_Scalar_Operator(+)
00280 Global_Vector_Scalar_Operator(-)
00281 Global_Vector_Scalar_Operator(*)
00282 Global_Vector_Scalar_Operator(/)
00283 #undef Global_Vector_Scalar_Operator
00284 
00285 
00286 #define Global_Vector_Function(fct)                                       \
00287 template <class DataType>                                                 \
00288 inline Vector(3)<DataType> fct(const Vector(3)<DataType> &x)              \
00289    {                                                                      \
00290       Vector(3)<DataType> ret;                                            \
00291       ret[0] = fct(x[0]);                                                 \
00292       ret[1] = fct(x[1]);                                                 \
00293       ret[2] = fct(x[2]);                                                 \
00294       return(ret);                                                        \
00295      }
00296 
00297 Global_Vector_Function(fabs)
00298 Global_Vector_Function(sqrt)
00299 #undef Global_Vector_Function
00300 
00301 
00302 #define Vector_Vector_Functions(NameTo,NameFrom,ope)                      \
00303   template <class DataType, class VectorType>                             \
00304   inline void NameTo (Vector(3)<DataType> &x, const VectorType &y)        \
00305     {                                                                     \
00306       x(0) ope y(0);                                                      \
00307       x(1) ope y(1);                                                      \
00308       x(2) ope y(2);                                                      \
00309     }                                                                     \
00310   template <class DataType, class VectorType>                             \
00311   inline void NameFrom (VectorType &x, const Vector(3)<DataType> &y)      \
00312     {                                                                     \
00313       x(0) ope y(0);                                                      \
00314       x(1) ope y(1);                                                      \
00315       x(2) ope y(2);                                                      \
00316     }
00317 
00318    Vector_Vector_Functions(equals_to,equals_from,=)
00319    Vector_Vector_Functions(plus_to,plus_from,+=)
00320    Vector_Vector_Functions(minus_to,minus_from,-=)
00321    Vector_Vector_Functions(multiply_to,multiply_from,*=)
00322    Vector_Vector_Functions(divide_to,divide_from,/=)
00323 #undef Vector_Vector_Functions
00324 
00325 
00326 #endif


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04