00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <winstd.H>
00027
00028 #include <algorithm>
00029
00030 #if defined(BL_OLD_STL)
00031 #include <stdlib.h>
00032 #else
00033 #include <cstdlib>
00034 #endif
00035
00036 #include <iostream>
00037
00038 #include <BLassert.H>
00039 #include <BoxLib.H>
00040 #include <IntVect.H>
00041 #include <IndexType.H>
00042
00043 const IntVect&
00044 IntVect::TheUnitVector ()
00045 {
00046 static const IntVect Unit(D_DECL(1,1,1));
00047 return Unit;
00048 }
00049
00050 const IntVect&
00051 IntVect::TheZeroVector ()
00052 {
00053 static const IntVect Zero(D_DECL(0,0,0));
00054 return Zero;
00055 }
00056
00057 const IntVect&
00058 IntVect::TheNodeVector ()
00059 {
00060 static const IntVect Node(D_DECL(IndexType::NODE,IndexType::NODE,IndexType::NODE));
00061 return Node;
00062 }
00063
00064 const IntVect&
00065 IntVect::TheCellVector ()
00066 {
00067 static const IntVect Cell(D_DECL(IndexType::CELL,IndexType::CELL,IndexType::CELL));
00068 return Cell;
00069 }
00070
00071 IntVect::IntVect (const int *a)
00072 {
00073 D_EXPR(vect[0] = a[0], vect[1] = a[1], vect[2] = a[2]);
00074 }
00075
00076 IntVect::IntVect (const Array<int> &a)
00077 {
00078 BL_ASSERT(a.size() == BL_SPACEDIM);
00079 D_EXPR(vect[0] = a[0], vect[1] = a[1], vect[2] = a[2]);
00080 }
00081
00082 bool
00083 IntVect::lexLT (const IntVect &s) const
00084 {
00085 #define LLT0 (vect[0] < s[0])
00086 #define LLT1 ((vect[0] == s[0]) && (vect[1] < s[1]))
00087 #define LLT2 ((vect[1] == s[1]) && (vect[2] < s[2]))
00088 #if BL_SPACEDIM == 1
00089 return LLT0;
00090 #elif BL_SPACEDIM == 2
00091 return LLT0 || LLT1;
00092 #elif BL_SPACEDIM == 3
00093 return LLT0 || (vect[0]==s[0] && ((vect[1] < s[1] || LLT2)));
00094 #endif
00095 #undef LLT0
00096 #undef LLT1
00097 #undef LLT2
00098 }
00099
00100 bool
00101 IntVect::lexGT (const IntVect& s) const
00102 {
00103 #define LGT0 (vect[0] > s[0])
00104 #define LGT1 ((vect[0] == s[0]) && (vect[1] > s[1]))
00105 #define LGT2 ((vect[1] == s[1]) && (vect[2] > s[2]))
00106 #if BL_SPACEDIM == 1
00107 return LGT0;
00108 #elif BL_SPACEDIM == 2
00109 return LGT0 || LGT1;
00110 #elif BL_SPACEDIM == 3
00111 return LGT0 || (vect[0] == s[0] && ((vect[1] > s[1] || LGT2)));
00112 #endif
00113 #undef LGT0
00114 #undef LGT1
00115 #undef LGT2
00116 }
00117
00118 IntVect&
00119 IntVect::min (const IntVect& p)
00120 {
00121 D_EXPR(vect[0] = std::min(vect[0], p.vect[0]),
00122 vect[1] = std::min(vect[1], p.vect[1]),
00123 vect[2] = std::min(vect[2], p.vect[2]));
00124 return *this;
00125 }
00126
00127 IntVect&
00128 IntVect::max (const IntVect& p)
00129 {
00130 D_EXPR(vect[0] = std::max(vect[0], p.vect[0]),
00131 vect[1] = std::max(vect[1], p.vect[1]),
00132 vect[2] = std::max(vect[2], p.vect[2]));
00133 return *this;
00134 }
00135
00136 IntVect&
00137 IntVect::scale (int s)
00138 {
00139 D_EXPR(vect[0] *= s, vect[1] *= s, vect[2] *= s);
00140 return *this;
00141 }
00142
00143 IntVect&
00144 IntVect::reflect (int ref_ix,
00145 int idir)
00146 {
00147 BL_ASSERT(idir >= 0 && idir < BL_SPACEDIM);
00148 vect[idir] = -vect[idir] + 2*ref_ix;
00149 return *this;
00150 }
00151
00152 IntVect&
00153 IntVect::shift (int coord,
00154 int s)
00155 {
00156 BL_ASSERT(coord >= 0 && coord < BL_SPACEDIM);
00157 vect[coord] += s;
00158 return *this;
00159 }
00160
00161 IntVect&
00162 IntVect::shift (const IntVect& iv)
00163 {
00164 *this += iv;
00165 return *this;
00166 }
00167
00168 IntVect&
00169 IntVect::diagShift (int s)
00170 {
00171 D_EXPR(vect[0] += s, vect[1] += s, vect[2] += s);
00172 return *this;
00173 }
00174
00175 IntVect
00176 operator+ (int s,
00177 const IntVect& p)
00178 {
00179 return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
00180 }
00181
00182 IntVect
00183 operator- (int s,
00184 const IntVect& p)
00185 {
00186 return IntVect(D_DECL(s - p[0], s - p[1], s - p[2]));
00187 }
00188
00189 IntVect
00190 operator* (int s,
00191 const IntVect& p)
00192 {
00193 return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
00194 }
00195
00196 IntVect
00197 BoxLib::scale (const IntVect& p,
00198 int s)
00199 {
00200 return IntVect(D_DECL(s * p[0], s * p[1], s * p[2]));
00201 }
00202
00203 IntVect
00204 BoxLib::diagShift (const IntVect &p, int s)
00205 {
00206 return IntVect(D_DECL(p[0] + s, p[1] + s, p[2] + s));
00207 }
00208
00209 IntVect
00210 BoxLib::min (const IntVect& p1,
00211 const IntVect& p2)
00212 {
00213 IntVect p(p1);
00214 return p.min(p2);
00215 }
00216
00217 IntVect
00218 BoxLib::max (const IntVect& p1,
00219 const IntVect& p2)
00220 {
00221 IntVect p(p1);
00222 return p.max(p2);
00223 }
00224
00225 IntVect
00226 BoxLib::BASISV (int dir)
00227 {
00228 BL_ASSERT(dir >= 0 && dir < BL_SPACEDIM);
00229 IntVect tmp;
00230 tmp[dir] = 1;
00231 return tmp;
00232 }
00233
00234 IntVect
00235 BoxLib::reflect (const IntVect& a,
00236 int ref_ix,
00237 int idir)
00238 {
00239 BL_ASSERT(idir >= 0 && idir < BL_SPACEDIM);
00240 IntVect b(a);
00241 b[idir] = -b[idir] + 2*ref_ix;
00242 return b;
00243 }
00244
00245 IntVect
00246 BoxLib::coarsen (const IntVect& p,
00247 int s)
00248 {
00249 BL_ASSERT(s > 0);
00250 return IntVect(
00251 D_DECL((p[0]<0) ? -abs(p[0]+1)/s-1 : p[0]/s ,
00252 (p[1]<0) ? -abs(p[1]+1)/s-1 : p[1]/s ,
00253 (p[2]<0) ? -abs(p[2]+1)/s-1 : p[2]/s ));
00254 }
00255
00256 IntVect
00257 BoxLib::coarsen (const IntVect& p1,
00258 const IntVect& p2)
00259 {
00260 BL_ASSERT(p2 > IntVect::TheZeroVector());
00261 return IntVect( D_DECL(
00262 (p1[0]<0)?-abs(p1[0]+1)/p2[0]-1:p1[0]/p2[0],
00263 (p1[1]<0)?-abs(p1[1]+1)/p2[1]-1:p1[1]/p2[1],
00264 (p1[2]<0)?-abs(p1[2]+1)/p2[2]-1:p1[2]/p2[2]) );
00265 }
00266
00267 IntVect&
00268 IntVect::coarsen (int s)
00269 {
00270 BL_ASSERT(s > 0);
00271 for (int i = 0; i < BL_SPACEDIM; ++i)
00272 vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
00273 return *this;
00274 }
00275
00276 IntVect&
00277 IntVect::coarsen (const IntVect& p)
00278 {
00279 BL_ASSERT(p > IntVect::TheZeroVector());
00280 for (int i = 0; i <BL_SPACEDIM; ++i)
00281 {
00282 const int s = p.vect[i];
00283 vect[i] = ((vect[i]<0) ? -abs(vect[i]+1)/s-1 : vect[i]/s);
00284 }
00285 return *this;
00286 }
00287
00288
00289
00290
00291
00292
00293 std::ostream&
00294 operator<< (std::ostream& os,
00295 const IntVect& p)
00296 {
00297 os << D_TERM( '(' << p[0] , <<
00298 ',' << p[1] , <<
00299 ',' << p[2]) << ')';
00300 if (os.fail())
00301 BoxLib::Error("operator<<(ostream&,IntVect&) failed");
00302 return os;
00303 }
00304
00305
00306
00307
00308 #define BL_IGNORE_MAX 100000
00309 #include <Utility.H>
00310
00311 std::istream&
00312 operator>> (std::istream& is,
00313 IntVect& iv)
00314 {
00315 is >> std::ws;
00316 char c;
00317 is >> c;
00318
00319 if (c == '(')
00320 {
00321 D_EXPR(is >> iv[0],
00322 is.ignore(BL_IGNORE_MAX, ',') >> iv[1],
00323 is.ignore(BL_IGNORE_MAX, ',') >> iv[2]);
00324 is.ignore(BL_IGNORE_MAX, ')');
00325 }
00326 else
00327 {
00328 BoxLib::Error("operator>>(istream&,IntVect&): expected \'(\'");
00329 }
00330
00331 if (is.fail())
00332 BoxLib::Error("operator>>(istream&,IntVect&) failed");
00333
00334 return is;
00335 }