Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

IntVect.H

Go to the documentation of this file.
00001 /*   _______              __
00002     / ___/ /  ___  __ _  / /  ___
00003    / /__/ _ \/ _ \/  V \/ _ \/ _ \
00004    \___/_//_/\___/_/_/_/_.__/\___/
00005 */
00006 // CHOMBO Copyright (c) 2000-2004, The Regents of the University of
00007 // California, through Lawrence Berkeley National Laboratory (subject to
00008 // receipt of any required approvals from U.S. Dept. of Energy).  All
00009 // rights reserved.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are met:
00013 //
00014 // (1) Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 // (2) Redistributions in binary form must reproduce the above copyright
00017 // notice, this list of conditions and the following disclaimer in the
00018 // documentation and/or other materials provided with the distribution.
00019 // (3) Neither the name of Lawrence Berkeley National Laboratory, U.S.
00020 // Dept. of Energy nor the names of its contributors may be used to endorse
00021 // or promote products derived from this software without specific prior
00022 // written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035 //
00036 // You are under no obligation whatsoever to provide any bug fixes,
00037 // patches, or upgrades to the features, functionality or performance of
00038 // the source code ("Enhancements") to anyone; however, if you choose to
00039 // make your Enhancements available either publicly, or directly to
00040 // Lawrence Berkeley National Laboratory, without imposing a separate
00041 // written license agreement for such Enhancements, then you hereby grant
00042 // the following license: a non-exclusive, royalty-free perpetual license
00043 // to install, use, modify, prepare derivative works, incorporate into
00044 // other computer software, distribute, and sublicense such Enhancements or
00045 // derivative works thereof, in binary and source code form.
00046 //
00047 // TRADEMARKS. Product and company names mentioned herein may be the
00048 // trademarks of their respective owners.  Any rights not expressly granted
00049 // herein are reserved.
00050 //
00051 
00052 #ifndef _INTVECT_H_
00053 #define _INTVECT_H_
00054 
00055 #ifndef WRAPPER
00056 #include <cstddef>
00057 #include <cstdlib>
00058 #include <cstring>
00059 #include <iostream>
00060 #include <cassert>
00061 
00062 class HDF5Handle;
00063 
00064 #include "Misc.H"
00065 #endif
00066 
00067 #include "SPACE.H"
00068 
00070 
00078 class IntVect
00079 {
00080 public:
00081 
00086 
00088 
00091   IntVect()
00092   {}
00093 
00095 
00098   ~IntVect()
00099   {}
00100 
00102 
00107   IntVect (D_DECL(int i, int j, int k));
00108 
00110 
00114   explicit IntVect (const int* a);
00115 
00117 
00120   IntVect (const IntVect& rhs);
00121 
00122   IntVect copy() const {return *this;}
00124 
00127   IntVect& operator= (const IntVect& rhs);
00128 
00130 
00134   inline
00135   int& operator[] (int i);
00136 
00138 
00141   inline
00142   int operator[] (int i) const;
00143 
00145 
00148   void setVal (int i,
00149                int val);
00150 
00157 
00159 
00163   const int* getVect () const;
00164 
00166 
00169   const int*  dataPtr() const;
00170 
00172 
00175   int*  dataPtr();
00176 
00183 
00185 
00189   bool operator== (const IntVect& p) const;
00190 
00192 
00196   bool operator!= (const IntVect& p) const;
00197 
00199 
00206   bool operator< (const IntVect& p) const;
00207 
00209 
00216   bool operator<= (const IntVect& p) const;
00217 
00219 
00226   bool operator> (const IntVect& p) const;
00227 
00229 
00237   bool operator>= (const IntVect& p) const;
00238 
00240 
00253   bool lexLT (const IntVect& s) const;
00254 
00256 
00269   bool lexGT (const IntVect& s) const;
00270 
00277 
00279 
00282   IntVect operator+ () const;
00283 
00285 
00288   IntVect operator- () const;
00289 
00296 
00298 
00301   IntVect& operator+= (int s);
00302 
00304 
00307   IntVect& operator+= (const IntVect& p);
00308 
00310 
00313   IntVect operator+ (const IntVect& p) const;
00314 
00316 
00320   IntVect operator+ (int s) const;
00321 
00323 
00327   friend inline IntVect operator+ (int            s,
00328                                    const IntVect& p);
00329 
00336 
00338 
00342   IntVect& operator-= (int s);
00343 
00345 
00348   IntVect& operator-= (const IntVect& p);
00349 
00351 
00355   IntVect operator- (const IntVect& p) const;
00356 
00358 
00362   IntVect operator- (int s) const;
00363 
00365 
00368   friend inline IntVect operator- (int            s,
00369                                    const IntVect& p);
00370 
00377 
00379 
00383   IntVect& operator*= (int s);
00384 
00386 
00389   IntVect& operator*= (const IntVect& p);
00390 
00392 
00395   IntVect operator* (const IntVect& p) const;
00396 
00398 
00402   IntVect operator* (int s) const;
00403 
00405 
00409   friend inline IntVect operator* (int            s,
00410                                    const IntVect& p);
00411 
00418 
00420 
00423   IntVect& operator/= (int s);
00424 
00426 
00430   IntVect& operator/= (const IntVect& p);
00431 
00433 
00436   IntVect operator/ (const IntVect& p) const;
00437 
00439 
00443   IntVect operator/ (int s) const;
00444 
00451 
00453 
00457   IntVect& min (const IntVect& p);
00458 
00460 
00464   friend inline IntVect min (const IntVect& p1,
00465                              const IntVect& p2);
00466 
00468 
00472   IntVect& max (const IntVect& p);
00473 
00475 
00479   friend inline IntVect max (const IntVect& p1,
00480                              const IntVect& p2);
00481 
00483 
00486   IntVect& scale (int s);
00487 
00489 
00493   friend inline IntVect scale (const IntVect& p,
00494                                int            s);
00495 
00497 
00502   IntVect& reflect (int ref_ix,
00503                     int idir);
00504 
00506 
00511   friend inline IntVect reflect(const IntVect& a,
00512                                 int            ref_ix,
00513                                 int            idir);
00514 
00516 
00520   IntVect& shift (int coord,
00521                   int s);
00522 
00524 
00528   IntVect& shift (const IntVect& iv);
00529 
00531 
00535   IntVect& diagShift (int s);
00536 
00538 
00542   friend inline IntVect diagShift (const IntVect& p,
00543                                    int            s);
00544 
00546 
00549   IntVect& coarsen (const IntVect& p);
00550 
00552 
00555   IntVect& coarsen (int p);
00556 
00558 
00562   friend inline IntVect coarsen (const IntVect& p,
00563                                  int            s);
00564 
00566 
00570   friend inline IntVect coarsen (const IntVect& p1,
00571                                  const IntVect& p2);
00572 
00579 
00581 
00584   void printOn (std::ostream& os) const;
00585 
00587 
00590   void p() const;
00591 
00593 
00596   void dumpOn (std::ostream& os) const;
00597 
00599 
00602   friend std::ostream& operator<< (std::ostream&       os,
00603                                    const IntVect& iv);
00604 
00606 
00609   friend std::istream& operator>> (std::istream& os,
00610                                    IntVect& iv);
00611 
00618 
00620 
00628   friend inline IntVect BASISV (int dir);
00629 
00633   static const IntVect Zero;
00634 
00638   static const IntVect Unit;
00639 
00640 #ifndef WRAPPER
00641 
00644   static const IntVect TheZeroVector ();
00645 
00649   static const IntVect TheUnitVector ();
00650 #endif
00651 
00654 protected:
00655   //
00656   // Box is a friend of ours.
00657   //
00658   friend class Box;
00659 
00660   friend class HDF5Handle;
00661   friend class VolIndex;
00662   friend class FaceIndex;
00663 
00667   int vect[SpaceDim];
00668 
00672   static const size_t IntVectSize;
00673 };
00674 
00675 IntVect UnitVector();
00676 IntVect ZeroVector();
00677 
00678 #ifndef WRAPPER
00679 //
00680 // Inlines.
00681 //
00682 
00683 // try uninitialized IntVect null construction for now.....
00684 
00685 // inline
00686 // IntVect::IntVect ()
00687 // {
00688 //     D_EXPR(vect[0] = 0, vect[1] = 0, vect[2] = 0);
00689 // }
00690 
00691 inline
00692 IntVect::IntVect (D_DECL(int i, int j, int k))
00693 {
00694   D_EXPR(vect[0] = i, vect[1] = j, vect[2] = k);
00695 }
00696 
00697 inline
00698 IntVect::IntVect (const int *a)
00699 {
00700   D_EXPR(vect[0] = a[0], vect[1] = a[1], vect[2] = a[2]);
00701 }
00702 
00703 inline
00704 IntVect::IntVect (const IntVect &iv)
00705 {
00706   //D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
00707   memcpy(vect, iv.vect, IntVectSize);
00708 }
00709 
00710 inline
00711 IntVect&
00712 IntVect::operator= (const IntVect &iv)
00713 {
00714   D_EXPR(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2]);
00715   return *this;
00716 }
00717 
00718 inline
00719 int&
00720 IntVect::operator[] (int i)
00721 {
00722   assert(i>=0 && i < SpaceDim);
00723   return vect[i];
00724 }
00725 
00726 inline
00727 int
00728 IntVect::operator[] (int i) const
00729 {
00730   assert(i>=0 && i < SpaceDim);
00731   return vect[i];
00732 }
00733 
00734 inline
00735 void
00736 IntVect::setVal (int i,
00737                  int val)
00738 {
00739   assert(i >=0 && i < SpaceDim);
00740   vect[i] = val;
00741   //  return *this;
00742 }
00743 
00744 inline
00745 const int*
00746 IntVect::dataPtr() const
00747 {
00748   return vect;
00749 }
00750 
00751 inline
00752 int*
00753 IntVect::dataPtr()
00754 {
00755   return vect;
00756 }
00757 
00758 inline
00759 const int*
00760 IntVect::getVect () const
00761 {
00762   return vect;
00763 }
00764 
00765 inline
00766 bool
00767 IntVect::operator== (const IntVect& p) const
00768 {
00769   return D_TERM(vect[0] == p[0], && vect[1] == p[1], && vect[2] == p[2]);
00770 }
00771 
00772 inline
00773 bool
00774 IntVect::operator!= (const IntVect& p) const
00775 {
00776   return D_TERM(vect[0] != p[0], || vect[1] != p[1], || vect[2] != p[2]);
00777 }
00778 
00779 inline
00780 bool
00781 IntVect::operator< (const IntVect& p) const
00782 {
00783   return D_TERM(vect[0] < p[0], && vect[1] < p[1], && vect[2] < p[2]);
00784 }
00785 
00786 inline
00787 bool
00788 IntVect::operator<= (const IntVect& p) const
00789 {
00790   return D_TERM(vect[0] <= p[0], && vect[1] <= p[1], && vect[2] <= p[2]);
00791 }
00792 
00793 inline
00794 bool
00795 IntVect::operator> (const IntVect& p) const
00796 {
00797   return D_TERM(vect[0] > p[0], && vect[1] > p[1], && vect[2] > p[2]);
00798 }
00799 
00800 inline
00801 bool
00802 IntVect::operator>= (const IntVect& p) const
00803 {
00804   return D_TERM(vect[0] >= p[0], && vect[1] >= p[1], && vect[2] >= p[2]);
00805 }
00806 
00807 inline
00808 bool
00809 IntVect::lexLT (const IntVect &s) const
00810 {
00811 #define LLT0 (vect[0] < s[0])
00812 #define LLT1 ((vect[0] == s[0]) && (vect[1] < s[1]))
00813 #define LLT2 ((vect[1] == s[1]) && (vect[2] < s[2]))
00814 #if   CH_SPACEDIM == 1
00815   return LLT0;
00816 #elif CH_SPACEDIM == 2
00817   return LLT0 || LLT1;
00818 #elif CH_SPACEDIM == 3
00819   return LLT0 || (vect[0]==s[0] && ((vect[1] < s[1] || LLT2)));
00820 #endif
00821 #undef LLT0
00822 #undef LLT1
00823 #undef LLT2
00824 }
00825 
00826 inline
00827 bool
00828 IntVect::lexGT (const IntVect& s) const
00829 {
00830 #define LGT0 (vect[0] > s[0])
00831 #define LGT1 ((vect[0] == s[0]) && (vect[1] > s[1]))
00832 #define LGT2 ((vect[1] == s[1]) && (vect[2] > s[2]))
00833 #if   CH_SPACEDIM == 1
00834   return LGT0;
00835 #elif CH_SPACEDIM == 2
00836   return LGT0 || LGT1;
00837 #elif CH_SPACEDIM == 3
00838   return LGT0 || (vect[0] == s[0] && ((vect[1] > s[1] || LGT2)));
00839 #endif
00840 #undef LGT0
00841 #undef LGT1
00842 #undef LGT2
00843 }
00844 
00845 inline
00846 IntVect
00847 IntVect::operator+ () const
00848 {
00849   return IntVect(*this);
00850 }
00851 
00852 inline
00853 IntVect
00854 IntVect::operator- () const
00855 {
00856   return IntVect(D_DECL(-vect[0], -vect[1], -vect[2] ));
00857 }
00858 
00859 inline
00860 IntVect&
00861 IntVect::operator+= (int s)
00862 {
00863   D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
00864   return *this;
00865 }
00866 
00867 inline
00868 IntVect&
00869 IntVect::operator+= (const IntVect& p)
00870 {
00871   D_EXPR(vect[0] += p[0], vect[1] += p[1], vect[2] += p[2]);
00872   return *this;
00873 }
00874 
00875 inline
00876 IntVect&
00877 IntVect::operator*= (int s)
00878 {
00879   D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
00880   return *this;
00881 }
00882 
00883 inline
00884 IntVect&
00885 IntVect::operator*= (const IntVect &p)
00886 {
00887   D_EXPR(vect[0] *= p[0], vect[1] *= p[1], vect[2] *= p[2]);
00888   return *this;
00889 }
00890 
00891 inline
00892 IntVect&
00893 IntVect::operator/= (int s)
00894 {
00895   D_EXPR(vect[0] /= s, vect[1] /= s, vect[2] /= s);
00896   return *this;
00897 }
00898 
00899 inline
00900 IntVect&
00901 IntVect::operator/= (const IntVect& p)
00902 {
00903   D_EXPR(vect[0] /= p[0], vect[1] /= p[1], vect[2] /= p[2]);
00904   return *this;
00905 }
00906 
00907 inline
00908 IntVect&
00909 IntVect::operator-= (int s)
00910 {
00911   D_EXPR(vect[0] -= s, vect[1] -= s, vect[2] -= s);
00912   return *this;
00913 }
00914 
00915 inline
00916 IntVect&
00917 IntVect::operator-= (const IntVect& p)
00918 {
00919   D_EXPR(vect[0] -= p[0], vect[1] -= p[1], vect[2] -= p[2]);
00920   return *this;
00921 }
00922 
00923 inline
00924 IntVect
00925 IntVect::operator+ (const IntVect& p) const
00926 {
00927   return IntVect(D_DECL(vect[0] + p[0], vect[1] + p[1], vect[2] + p[2]));
00928 }
00929 
00930 inline
00931 IntVect
00932 IntVect::operator+ (int s) const
00933 {
00934   return IntVect(D_DECL(vect[0] + s, vect[1] + s, vect[2] + s));
00935 }
00936 
00937 inline
00938 IntVect
00939 IntVect::operator- (const IntVect& p) const
00940 {
00941   return IntVect(D_DECL(vect[0] - p[0], vect[1] - p[1], vect[2] - p[2]));
00942 }
00943 
00944 inline
00945 IntVect
00946 IntVect::operator- (int s) const
00947 {
00948   return IntVect(D_DECL(vect[0] - s, vect[1] - s, vect[2] - s));
00949 }
00950 
00951 inline
00952 IntVect
00953 IntVect::operator* (const IntVect& p) const
00954 {
00955   return IntVect(D_DECL(vect[0] * p[0], vect[1] * p[1], vect[2] * p[2]));
00956 }
00957 
00958 inline
00959 IntVect
00960 IntVect::operator* (int s) const
00961 {
00962   return IntVect(D_DECL(vect[0] * s, vect[1] * s, vect[2] * s));
00963 }
00964 
00965 inline
00966 IntVect
00967 IntVect::operator/ (const IntVect& p) const
00968 {
00969   return IntVect(D_DECL(vect[0] / p[0], vect[1] / p[1], vect[2] / p[2]));
00970 }
00971 
00972 inline
00973 IntVect
00974 IntVect::operator/ (int s) const
00975 {
00976   return IntVect(D_DECL(vect[0] / s, vect[1] / s, vect[2] / s));
00977 }
00978 
00979 inline
00980 IntVect&
00981 IntVect::min (const IntVect& p)
00982 {
00983   D_EXPR(vect[0] = Min(vect[0], p.vect[0]),
00984          vect[1] = Min(vect[1], p.vect[1]),
00985          vect[2] = Min(vect[2], p.vect[2]));
00986   return *this;
00987 }
00988 
00989 inline
00990 IntVect&
00991 IntVect::max (const IntVect& p)
00992 {
00993   D_EXPR(vect[0] = Max(vect[0], p.vect[0]),
00994          vect[1] = Max(vect[1], p.vect[1]),
00995          vect[2] = Max(vect[2], p.vect[2]));
00996   return *this;
00997 }
00998 
00999 inline
01000 IntVect&
01001 IntVect::scale (int s)
01002 {
01003   D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
01004   return *this;
01005 }
01006 
01007 inline
01008 IntVect&
01009 IntVect::reflect (int ref_ix,
01010                   int idir)
01011 {
01012   assert(idir >= 0 && idir < SpaceDim);
01013   vect[idir] = -vect[idir] + 2*ref_ix;
01014   return *this;
01015 }
01016 
01017 inline
01018 IntVect&
01019 IntVect::shift (int coord,
01020                 int s)
01021 {
01022   assert(coord >= 0 && coord < SpaceDim);
01023   vect[coord] += s;
01024   return *this;
01025 }
01026 
01027 inline
01028 IntVect&
01029 IntVect::shift (const IntVect& iv)
01030 {
01031   *this += iv;
01032   return *this;
01033 }
01034 
01035 inline
01036 IntVect&
01037 IntVect::diagShift (int s)
01038 {
01039   D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
01040   return *this;
01041 }
01042 
01043 inline
01044 IntVect
01045 operator+ (int            s,
01046            const IntVect& p)
01047 {
01048   return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
01049 }
01050 
01051 inline
01052 IntVect
01053 operator- (int            s,
01054            const IntVect& p)
01055 {
01056   return IntVect(D_DECL(s - p[0], s - p[1], s - p[2]));
01057 }
01058 
01059 inline
01060 IntVect
01061 operator* (int            s,
01062            const IntVect& p)
01063 {
01064   return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
01065 }
01066 
01067 inline
01068 IntVect
01069 scale (const IntVect& p,
01070        int            s)
01071 {
01072   return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
01073 }
01074 
01075 inline
01076 IntVect
01077 diagShift (const IntVect &p, int s)
01078 {
01079   return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
01080 }
01081 
01082 inline
01083 IntVect
01084 min (const IntVect& p1,
01085      const IntVect& p2)
01086 {
01087   IntVect p(p1);
01088   return p.min(p2);
01089 }
01090 
01091 inline
01092 IntVect
01093 max (const IntVect& p1,
01094      const IntVect& p2)
01095 {
01096   IntVect p(p1);
01097   return p.max(p2);
01098 }
01099 
01100 inline
01101 IntVect
01102 BASISV (int dir)
01103 {
01104   assert(dir >= 0 && dir < SpaceDim);
01105   IntVect tmp = IntVect::TheZeroVector() ;
01106   tmp.vect[dir] = 1;
01107   return tmp;
01108 }
01109 
01110 inline
01111 IntVect
01112 reflect (const IntVect& a,
01113          int            ref_ix,
01114          int            idir)
01115 {
01116   assert(idir >= 0 && idir < SpaceDim);
01117   IntVect b(a);
01118   b.vect[idir] = -b.vect[idir] + 2*ref_ix;
01119   return b;
01120 }
01121 
01122 inline
01123 IntVect
01124 coarsen (const IntVect& p,
01125          int            s)
01126 {
01127   assert(s > 0);
01128   return IntVect(
01129                  D_DECL((p.vect[0]<0) ? -abs(p.vect[0]+1)/s-1 : p.vect[0]/s ,
01130                         (p.vect[1]<0) ? -abs(p.vect[1]+1)/s-1 : p.vect[1]/s ,
01131                         (p.vect[2]<0) ? -abs(p.vect[2]+1)/s-1 : p.vect[2]/s ));
01132 }
01133 
01134 inline
01135 IntVect
01136 coarsen (const IntVect& p1,
01137          const IntVect& p2)
01138 {
01139   assert(p2 > IntVect::TheZeroVector());
01140   return IntVect(
01141                  D_DECL(
01142                         (p1.vect[0]<0)?-abs(p1.vect[0]+1)/p2.vect[0]-1:p1.vect[0]/p2.vect[0],
01143                         (p1.vect[1]<0)?-abs(p1.vect[1]+1)/p2.vect[1]-1:p1.vect[1]/p2.vect[1],
01144                         (p1.vect[2]<0)?-abs(p1.vect[2]+1)/p2.vect[2]-1:p1.vect[2]/p2.vect[2])
01145                  );
01146 }
01147 
01148 inline
01149 IntVect&
01150 IntVect::coarsen (int s)
01151 {
01152   assert(s > 0);
01153   for (int i = 0; i < SpaceDim; ++i)
01154     vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
01155   return *this;
01156 }
01157 
01158 inline
01159 IntVect&
01160 IntVect::coarsen (const IntVect& p)
01161 {
01162   assert(p > IntVect::TheZeroVector());
01163   for (int i = 0; i <SpaceDim; ++i)
01164     {
01165       const int s = p.vect[i];
01166       vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
01167     }
01168   return *this;
01169 }
01170 
01171 #endif /* WRAPPER */
01172 
01173 #endif

Generated on Wed Jan 19 17:51:25 2005 for Chombo&INSwithParticles by doxygen1.2.16