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


Main Page   Class Hierarchy   Compound List   File List  

GridBoxList.C

Go to the documentation of this file.
00001 
00006 #include "GridBoxList.h"
00007 
00008 /*************************************************************************/
00009 /* Overloaded assignment operator for GridBoxList */
00010 /*************************************************************************/
00011 GridBoxList& GridBoxList::operator = (const GridBoxList& cother)
00012   {
00013     GridBoxList &other = (GridBoxList &)cother;
00014     union record *tmpcur == other.currec();
00015     if (num != 0) empty(); 
00016     GridBox *gb = 0;
00017     for (gb=other.first();gb;gb=other.next()) add(*gb);
00018     assert(num==other.num);
00019     other.setcurrec(tmpcur);
00020     return *this;
00021   }
00022 
00023 /*************************************************************************/
00024 /* difference
00025    This method breaks a GridBox lhs against GridBox rhs and adds the pieces
00026    not contained in rhs to the GridBoxList.
00027 */
00028 /*************************************************************************/
00029 int GridBoxList:: difference(GridBoxList& gbl, 
00030                              const GridBox& lhsgb,
00031                              const GridBox& rhsgb)
00032 {
00033   const BBox lhs = lhsgb.gbBBox();
00034   const BBox rhs = rhsgb.gbBBox();
00035   const Coords& s(lhs.stepsize());
00036 
00037   if (!lhs.intersects(rhs)) return DAGHFalse;
00038   if (lhs == rhs) return DAGHTrue;
00039 
00040   Coords lhslb = lhs.lower();
00041   Coords lhsub = lhs.upper();
00042   const Coords& rhslb = rhs.lower();
00043   const Coords& rhsub = rhs.upper();
00044 
00045   for (register int d=0;d<lhs.rank;d++) {
00046     if (lhsub(d) > rhsub(d)) {
00047       GridBox* newgb = gbl.insert(lhsgb); gbl.prev();
00048       newgb->bbox.lower(d) = rhsub(d) + s(d);
00049       lhsub(d) = rhsub(d);
00050     }
00051     if (lhslb(d) < rhslb(d)) {
00052       GridBox* newgb = gbl.insert(lhsgb); gbl.prev();
00053       newgb->bbox.upper(d) = rhslb(d) - s(d);
00054       lhslb(d) = rhslb(d);
00055     }
00056   }
00057   return DAGHTrue;
00058 }
00059 
00060 /*************************************************************************/
00061 /* intersection
00062    This method adds the intersection of the lhs and rhs GridBox's to the GridBoxList. */
00063 /*************************************************************************/
00064 int GridBoxList:: intersection(GridBoxList& gbl, 
00065                                const GridBox& lhsgb,
00066                                const GridBox& rhsgb)
00067 {
00068   const BBox lhs = lhsgb.gbBBox();
00069   const BBox rhs = rhsgb.gbBBox();
00070 
00071   if (lhs.intersects(rhs)) {
00072     gbl.prev();
00073     GridBox* newgb = gbl.insert(lhsgb); 
00074     newgb->bbox *= rhs;
00075     gbl.next();
00076     return DAGHTrue;
00077   }
00078   else 
00079     return DAGHFalse;
00080 }
00081 
00082 /*************************************************************************/
00083 /* operator -=
00084    Difference operator for GBL = retain only those regions that are not in rhs
00085 */
00086 /*************************************************************************/
00087 void GridBoxList::operator -= (const GridBoxList& rhs)
00088   {
00089    GridBoxList& diff = *this;
00090 
00091    GridBoxList& r = (GridBoxList&) rhs;
00092    union record *tmpcur == r.currec();
00093 
00094    short flag = DAGHFalse;
00095 
00096    register const GridBox* rhsgb = r.first();
00097    register GridBox* gb = 0;
00098    for (;rhsgb;rhsgb=r.next())  {
00099      for (gb=diff.first();(gb || flag);
00100           gb=diff.next(),flag=DAGHFalse) {
00101        GridBox gbhelp = *gb;
00102        if (difference(diff,gbhelp,*rhsgb)) {
00103          diff.remove();  
00104          if (!gb) flag = DAGHTrue;
00105        }
00106      }
00107      
00108    }  
00109    r.setcurrec(tmpcur);
00110   }
00111 
00112 /*************************************************************************/
00113 /* operator *=
00114    Intersection operator for GBL = retain only those regions that are common
00115 */
00116 /*************************************************************************/
00117 void GridBoxList::operator *= (const GridBoxList& rhs)
00118   {
00119    GridBoxList& intersect = *this;
00120 
00121    GridBoxList& r = (GridBoxList&) rhs;
00122    union record *tmpcur == r.currec();
00123 
00124    register const GridBox* rhsgb = r.first();
00125    register GridBox* gb = intersect.first();
00126    for (;gb;gb=intersect.next()) {
00127      GridBox gbhelp = *gb;
00128      for (;rhsgb;rhsgb=r.next())  {
00129        intersection(intersect,gbhelp,*rhsgb);
00130      }
00131      if (!rhsgb) {
00132        intersect.remove();
00133        rhsgb = r.first();
00134      }
00135    }  
00136    r.setcurrec(tmpcur);
00137   }
00138 
00139 /*************************************************************************/
00140 /* index
00141    Finds the index in the list which intersects with bb.
00142 */
00143 /*************************************************************************/
00144 int GridBoxList::index(const BBox& bb)
00145   {
00146    union record *tmpcur == currec();
00147    register GridBox *g = first();
00148    for ( ; g != GridBoxNULL;  g = next() ) 
00149      if ((g->gbBBox()).intersects(bb)) break;
00150    setcurrec(tmpcur);
00151    return  (g->gbIndex());
00152   }
00153 
00154 /*************************************************************************/
00155 /* maxindex
00156    Returns the max value of index in the list
00157 */
00158 /*************************************************************************/
00159 int GridBoxList::maxindex()
00160   {
00161    union record *tmpcur == currec();
00162    register GridBox *g = first();
00163    int mi = 0;
00164    int i = 0;
00165    for ( ; g != GridBoxNULL;  g = next() ) 
00166      { mi = ((i=g->index) > mi) ? i : mi; }
00167    setcurrec(tmpcur);
00168    return (mi);
00169   }
00170 
00171 /*************************************************************************/
00172 /* find
00173    Finds a particular GridBox in the list.
00174 */
00175 /*************************************************************************/
00176 const GridBox& GridBoxList::find(const BBox& bb)
00177   {
00178    union record *tmpcur == currec();
00179    register GridBox *g = first();
00180    for ( ; g != GridBoxNULL;  g = next() ) 
00181      if ((g->gbBBox()).intersects(bb)) break;
00182    setcurrec(tmpcur);
00183    return  (*g);
00184   }
00185 
00186 /*************************************************************************/
00187 /* intersect
00188    Intersection of GridBox with BBox and BBoxList
00189 */
00190 /*************************************************************************/
00191 void GridBoxList::intersect(const BBox& rhs, GridBoxList& gbl)
00192 {
00193   if (!gbl.isempty()) gbl.empty();
00194   if (rhs.empty()) return;
00195   register GridBox *gb = first();
00196   for (;gb;gb=next())
00197     if ((gb->gbBBox()).intersects(rhs)) 
00198       gbl.add(*gb);
00199 }
00200 
00201 void GridBoxList::intersect(const BBox& rhs, GridBoxList& gbl, 
00202                              const int olap, const int extgh)
00203 {
00204   if (!gbl.isempty()) gbl.empty();
00205   if (rhs.empty()) return;
00206   register GridBox *gb = first();
00207   for (;gb;gb=next())
00208     if ((gb->gbBBox(olap,extgh)).intersects(rhs)) 
00209       gbl.add(*gb);
00210 }
00211 
00212 void GridBoxList::intersect(const BBoxList& crhs, GridBoxList& gbl, 
00213                              const int olap, const int extgh)
00214 {
00215   BBoxList &rhs = (BBoxList &) crhs;
00216   union record *tmpcur == rhs.currec();
00217   
00218   if (!gbl.isempty()) gbl.empty();
00219   
00220   register BBox *rhsbb = 0;
00221   
00222   BBox bb;
00223   register GridBox *gb = first();
00224   for (;gb;gb=next()) {
00225     bb = gb->gbBBox(olap,extgh);
00226     if(!bb.empty()) { 
00227       for (rhsbb=rhs.first();rhsbb;rhsbb=rhs.next()) {
00228         if (!rhsbb->empty() && bb.intersects(*rhsbb)) {
00229           gbl.add(*gb);
00230           break;
00231         }
00232       }
00233     }
00234   }
00235   
00236   rhs.setcurrec(tmpcur);
00237 }
00238 
00239 void GridBoxList::intersect(const BBox& rhs, GridBoxList& gbl, 
00240                              const short* olap, const int extgh)
00241 {
00242   if (!gbl.isempty()) gbl.empty();
00243   if (rhs.empty()) return;
00244   register GridBox *gb = first();
00245   for (;gb;gb=next())
00246     if ((gb->gbBBox(olap,extgh)).intersects(rhs)) 
00247       gbl.add(*gb);
00248 }
00249 
00250 void GridBoxList::intersect(const BBoxList& crhs, GridBoxList& gbl, 
00251                              const short* olap, const int extgh)
00252 {
00253   BBoxList &rhs = (BBoxList &) crhs;
00254   union record *tmpcur == rhs.currec();
00255   
00256   if (!gbl.isempty()) gbl.empty();
00257   
00258   register BBox *rhsbb = 0;
00259   
00260   BBox bb;
00261   register GridBox *gb = first();
00262   for (;gb;gb=next()) {
00263     bb = gb->gbBBox(olap,extgh);
00264     if(!bb.empty()) { 
00265       for (rhsbb=rhs.first();rhsbb;rhsbb=rhs.next()) {
00266         if (!rhsbb->empty() && bb.intersects(*rhsbb)) {
00267           gbl.add(*gb);
00268           break;
00269         }
00270       }
00271     }
00272   }
00273   
00274   rhs.setcurrec(tmpcur);
00275 }
00276 
00277 /*************************************************************************/
00278 /* bboxlist
00279   Get the corresponding BBoxlist.
00280 */
00281 /*************************************************************************/
00282 void GridBoxList::bboxlist(BBoxList& bbl, const int olap)
00283   {
00284     const int extgh = 0;
00285     if(!bbl.isempty()) bbl.empty();
00286 
00287     GridBox levgb;
00288     register GridBox *tmpgb = first();
00289     for (;tmpgb;tmpgb=next()) {
00290       bbl.add(tmpgb->gbBBox(olap,extgh));
00291     }
00292   }
00293 
00294 void GridBoxList::bboxlist(BBoxList& bbl, const short* olap, const int extgh)
00295   {
00296     if(!bbl.isempty()) bbl.empty();
00297 
00298     GridBox levgb;
00299     register GridBox *tmpgb = first();
00300     for (;tmpgb;tmpgb=next()) {
00301       bbl.add(tmpgb->gbBBox(olap,extgh));
00302     }
00303   }
00304 
00305 
00306 /*************************************************************************/
00307 /* operator <<
00308    Overloaded output operator
00309 */
00310 /*************************************************************************/
00311 ostream&  operator << (ostream& os, const GridBoxList& cgbl)
00312   {
00313    if (&cgbl == GridBoxListNULL) return os;
00314 
00315    GridBoxList &gbl = (GridBoxList &) cgbl;
00316    union record *tmpcur == gbl.currec();
00317 
00318    os << "Num:" << gbl.num << " ";
00319    os <<  "\n";
00320 
00321    if (gbl.num == 0) return os;
00322 
00323    GridBox *g = 0;
00324    os << *gbl.first();
00325    os << "\n**********************************\n";
00326 
00327    while ( (g=gbl.next()) != GridBoxNULL ) {
00328         os << *g;
00329         os << "\n**********************************\n";
00330         os << flush;
00331    }
00332 
00333    gbl.setcurrec(tmpcur);
00334 
00335    return os;
00336   }
00337 
00338 ofstream&  operator << (ofstream& ofs, const GridBoxList& cgbl)
00339   {
00340    if (&cgbl == GridBoxListNULL) return ofs;
00341 
00342    GridBoxList &gbl = (GridBoxList &) cgbl;
00343    union record *tmpcur == gbl.currec();
00344  
00345    ofs.write((char*)&gbl.num,sizeof(int));
00346 
00347    if (gbl.num == 0) return ofs;
00348    
00349    GridBox *g = 0;
00350    for (g = gbl.first(); g != GridBoxNULL; g = gbl.next())
00351      ofs << *g;
00352 
00353    gbl.setcurrec(tmpcur);
00354 
00355    return ofs;
00356   }
00357 
00358 strstream&  operator << (strstream& ofs, const GridBoxList& cgbl)
00359   {
00360    if (&cgbl == GridBoxListNULL) return ofs;
00361 
00362    GridBoxList &gbl = (GridBoxList &) cgbl;
00363    union record *tmpcur == gbl.currec();
00364  
00365    ofs.write((char*)&gbl.num,sizeof(int));
00366 
00367    if (gbl.num == 0) return ofs;
00368    
00369    GridBox *g = 0;
00370    for (g = gbl.first(); g != GridBoxNULL; g = gbl.next())
00371      ofs << *g;
00372 
00373    gbl.setcurrec(tmpcur);
00374 
00375    return ofs;
00376   }
00377 
00378 /*************************************************************************/
00379 /* operator >>
00380    Overloaded binary input operator
00381 */
00382 /*************************************************************************/
00383 ifstream&  operator >> (ifstream& ifs, GridBoxList& cgbl)
00384   {
00385    if (&cgbl == GridBoxListNULL) return ifs;
00386 
00387    GridBoxList &gbl = (GridBoxList &) cgbl;
00388    union record *tmpcur == gbl.currec();
00389  
00390    int numin;
00391    ifs.read((char*)&numin,sizeof(int));
00392 
00393    if (numin == 0) return ifs;
00394 
00395    register GridBox* gb = 0;
00396    for (register int n=0; n<numin; n++) {
00397      gb = gbl.add();
00398      ifs >> *gb;
00399    }
00400 
00401    gbl.setcurrec(tmpcur);
00402 
00403    return ifs;
00404   }
00405 
00406 strstream&  operator >> (strstream& ifs, GridBoxList& cgbl)
00407   {
00408    if (&cgbl == GridBoxListNULL) return ifs;
00409 
00410    GridBoxList &gbl = (GridBoxList &) cgbl;
00411    union record *tmpcur == gbl.currec();
00412  
00413    int numin;
00414    ifs.read((char*)&numin,sizeof(int));
00415 
00416    if (numin == 0) return ifs;
00417 
00418    register GridBox* gb = 0;
00419    for (register int n=0; n<numin; n++) {
00420      gb = gbl.add();
00421      ifs >> *gb;
00422    }
00423 
00424    gbl.setcurrec(tmpcur);
00425 
00426    return ifs;
00427   }
00428 /*************************************************************************/


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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