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


Main Page   Class Hierarchy   Compound List   File List  

BBoxList.C

Go to the documentation of this file.
00001 
00006 #include "BBoxList.h"
00007 
00008 /*************************************************************************/
00009 /* overloaded assignment */
00010 /*************************************************************************/
00011 BBoxList&  BBoxList::operator = (const BBoxList& cother)
00012   {
00013     BBoxList &other = (BBoxList &) cother;
00014     union record *tmpcur == other.currec();
00015 
00016     empty();
00017     BBox *bb = 0;
00018     for (bb=other.first();bb;bb=other.next()) {
00019       add(*bb);
00020     }
00021 
00022     assert(num==other.num);
00023     other.setcurrec(tmpcur);
00024     return *this;
00025   }
00026 
00027 /*************************************************************************/
00028 /* difference */
00029 /*************************************************************************/
00030 int BBoxList::difference(BBoxList& bbl, 
00031                          const BBox& lhs,
00032                          const BBox& rhs)
00033 {
00034   const Coords& s(lhs.stepsize());
00035 
00036   if (!lhs.intersects(rhs)) return DAGHFalse;
00037   if (lhs == rhs) return DAGHTrue;
00038 
00039   Coords lhslb = lhs.lower();
00040   Coords lhsub = lhs.upper();
00041   const Coords& rhslb = rhs.lower();
00042   const Coords& rhsub = rhs.upper();
00043 
00044   for (register int d=0;d<lhs.rank;d++) {
00045     if (lhsub(d) > rhsub(d)) {
00046       BBox* newbb = bbl.insert(BBox(lhslb,lhsub,s)); bbl.prev();
00047       newbb->lower(d) = rhsub(d) + s(d);
00048       lhsub(d) = rhsub(d);
00049     }
00050     if (lhslb(d) < rhslb(d)) {
00051       BBox* newbb = bbl.insert(BBox(lhslb,lhsub,s)); bbl.prev();
00052       newbb->upper(d) = rhslb(d) - s(d);
00053       lhslb(d) = rhslb(d);
00054     }
00055   }
00056   return DAGHTrue;
00057 }
00058 
00059 /*************************************************************************/
00060 /* intersection */
00061 /*************************************************************************/
00062 int BBoxList::intersection(BBoxList& bbl, 
00063                            const BBox& lhs,
00064                            const BBox& rhs)
00065 {
00066   if (lhs.intersects(rhs)) {
00067     bbl.prev();
00068     BBox* newbb = bbl.insert(lhs); 
00069     *newbb *= rhs;
00070     bbl.next();
00071     return DAGHTrue;
00072   }
00073   else 
00074     return DAGHFalse;
00075 }
00076 
00077 /*************************************************************************/
00078 /* operator -= */
00079 /*************************************************************************/
00080 void BBoxList::operator -= (const BBoxList& rhs)
00081 {
00082   BBoxList& diff = *this;
00083 
00084   BBoxList& r = (BBoxList&) rhs;
00085   union record *tmpcur == r.currec();
00086   
00087   short flag = DAGHFalse;
00088   
00089   register const BBox* rhsbb = r.first();
00090   register BBox* bb = 0;
00091   for (;rhsbb;rhsbb=r.next())  {
00092     for (bb=diff.first();(bb || flag);
00093          bb=diff.next(),flag=DAGHFalse) {
00094       BBox bbhelp = *bb;
00095       if (difference(diff,bbhelp,*rhsbb)) {
00096         diff.remove();   
00097         if (!bb) flag = DAGHTrue;
00098       }
00099     }     
00100   }  
00101   r.setcurrec(tmpcur);
00102 }
00103 
00104 void BBoxList::operator -= (const BBox& rhs)
00105 {
00106   BBoxList& diff = *this;
00107 
00108   short flag = DAGHFalse;
00109   
00110   register BBox* bb = 0;
00111   for (bb=diff.first();(bb || flag);
00112        bb=diff.next(),flag=DAGHFalse) {
00113     BBox bbhelp = *bb;
00114     if (difference(diff,bbhelp,rhs)) {
00115       diff.remove();     
00116       if (!bb) flag = DAGHTrue;
00117     }
00118   }     
00119 }
00120 
00121 /*************************************************************************/
00122 /* operator *= */
00123 /*************************************************************************/
00124 void BBoxList::operator *= (const BBoxList &rhs) 
00125 {
00126   BBoxList& intersect = *this;
00127 
00128   BBoxList& r = (BBoxList&) rhs;
00129   union record *tmpcur == r.currec();
00130 
00131   register const BBox* rhsbb = r.first();
00132   register BBox* bb = intersect.first();
00133   for (;bb;bb=intersect.next()) {
00134     BBox bbhelp = *bb;
00135     for (;rhsbb;rhsbb=r.next())  
00136       intersection(intersect,bbhelp,*rhsbb);
00137     if (!rhsbb) {
00138       intersect.remove();
00139       rhsbb = r.first();
00140     }
00141   }  
00142   r.setcurrec(tmpcur);   
00143 }
00144 
00145 void BBoxList::operator *= (const BBox &rhs) 
00146 {
00147   BBoxList& intersect = *this;
00148 
00149   register BBox* bb = intersect.first();
00150   for (;bb;bb=intersect.next()) {
00151     BBox bbhelp = *bb;
00152     intersection(intersect,bbhelp,rhs);
00153   }  
00154 }
00155 
00156 /*************************************************************************/
00157 /* nest in another list */
00158 /*************************************************************************/
00159 void BBoxList::nest (const BBoxList &crhs)
00160   {
00161    BBoxList &rhs = (BBoxList &) crhs;
00162    union record *tmpcur == rhs.currec();
00163 
00164    register BBox *rhsbb = rhs.first();
00165    register BBox *bb = first();
00166    if (num == 0) {
00167      for (;rhsbb;rhsbb=rhs.next()) 
00168         add(*rhsbb);
00169    }
00170    else {
00171      for (;rhsbb;rhsbb=rhs.next())  {
00172        for (;bb;bb=next()) {
00173          if (*rhsbb == *bb) break;
00174          else {
00175            BBox intersection = *rhsbb * *bb;
00176            if (!intersection.empty()) {
00177              *bb += *rhsbb;
00178            }
00179          }
00180        }
00181        if (!bb) bb = first();
00182      }
00183    }
00184 
00185    rhs.setcurrec(tmpcur);
00186   }
00187 
00188 /*************************************************************************/
00189 /* merge the  boxes */
00190 /*************************************************************************/
00191 void BBoxList::mergeboxes(const short* olap)
00192   {
00193    if (num == 0) return;
00194 
00195    const int rank = (first())->rank;
00196 
00197    BBoxList mergebbl = *this;
00198    BBoxList bbl;
00199 
00200    register int onum = 0;
00201    register int rstrt = 0;
00202    register int d = 0;
00203    while (onum != mergebbl.num) {
00204      onum = mergebbl.num;
00205 
00206    //for (register int d=rank-1; d>=0 ; d--) {
00207    for (register int dd=0; dd<rank ; dd++) {
00208      d = (rstrt + dd)%rank;
00209      bbl = mergebbl;
00210      mergebbl.empty();
00211      for (BBox *bb=bbl.first();bb;bb=bbl.next()) {
00212        if (!bb->empty()) {
00213          BBox mbb = *bb; 
00214          for (BBox *withbb=mergebbl.first();withbb;withbb=mergebbl.next()) {
00215            if (mbb.mergable(*withbb, d, olap[d]) || (*withbb)<=mbb) {
00216              mbb += *withbb;
00217              mergebbl.remove();
00218            }
00219          }
00220          mergebbl.add(mbb);
00221        }
00222        bbl.remove();
00223      }
00224      assert(bbl.isempty());
00225    } }
00226 
00227    empty();
00228    *this = mergebbl;
00229    rstrt++;
00230   }
00231 
00232 /*************************************************************************/
00233 /* reduce into a single bounding box */
00234 /*************************************************************************/
00235 BBox BBoxList::reduce()
00236   {
00237    if (num == 0) return BBox::_empty_bbox;
00238 
00239    union record *tmpcur == currec();
00240    
00241    BBox rbbox;
00242    register BBox *bb = first();
00243    rbbox.rank = bb->rank;
00244    for (;bb;bb=next()) rbbox += *bb;
00245 
00246    setcurrec(tmpcur);
00247    return rbbox;
00248   }
00249 
00250 /*************************************************************************/
00251 /* combine two lists */
00252 /*************************************************************************/
00253 void BBoxList::combine(const BBoxList &crhs)
00254   {
00255    BBoxList &rhs = (BBoxList &) crhs;
00256    union record *tmpcur == rhs.currec();
00257 
00258    register BBox *rhsbb = rhs.first();
00259    for (;rhsbb;rhsbb=rhs.next()) add(*rhsbb);
00260 
00261    rhs.setcurrec(tmpcur);
00262   }
00263 
00264 /*************************************************************************/
00265 /* and print it out... */
00266 /*************************************************************************/
00267 ostream&  operator << (ostream& os, const BBoxList& cbbl)
00268   {
00269    if (&cbbl == BBoxListNULL) return os;
00270 
00271    BBoxList &bbl = (BBoxList &) cbbl;
00272    union record *tmpcur == bbl.currec();
00273 
00274    os << "Num:" << bbl.num << " ";
00275    os <<  "\n";
00276 
00277    if (bbl.num == 0) return os;
00278 
00279    BBox *b = 0;
00280    os << *bbl.first();
00281    os << "\n**********************************\n";
00282 
00283    while ( (b=bbl.next()) != BBoxNULL ) {
00284         os << *b;
00285         os << "\n**********************************\n";
00286         os << flush;
00287    }
00288 
00289    bbl.setcurrec(tmpcur);
00290    return os;
00291   }
00292 
00293 /*
00294 *************************************************************************
00295 *                                                                       *
00296 * And overload BINARY << for the BBL class....                          *
00297 *                                                                       *
00298 *************************************************************************
00299 */
00300 
00301 ofstream&  operator << (ofstream& ofs, const BBoxList& cbbl)
00302   {
00303    if (&cbbl == BBoxListNULL) return ofs;
00304 
00305    BBoxList &bbl = (BBoxList &) cbbl;
00306    union record *tmpcur == bbl.currec();
00307  
00308    ofs.write((char*)&bbl.num,sizeof(int));
00309 
00310    if (bbl.num == 0) return ofs;
00311    
00312    BBox *b = 0;
00313    for (b = bbl.first(); b != BBoxNULL; b = bbl.next())
00314      ofs << *b;
00315 
00316    bbl.setcurrec(tmpcur);
00317 
00318    return ofs;
00319   }
00320 
00321 ifstream&  operator >> (ifstream& ifs, BBoxList& cbbl)
00322   {
00323    if (&cbbl == BBoxListNULL) return ifs;
00324 
00325    BBoxList &bbl = (BBoxList &) cbbl;
00326    union record *tmpcur == bbl.currec();
00327  
00328    int numin;
00329    ifs.read((char*)&numin,sizeof(int));
00330 
00331    if (numin == 0) return ifs;
00332 
00333    BBox bb;
00334    for (register int n=0; n<numin; n++) {
00335      ifs >> bb;
00336      bbl.add(bb);
00337    }
00338 
00339    bbl.setcurrec(tmpcur);
00340 
00341    return ifs;
00342   }
00343 
00344 strstream&  operator << (strstream& ofs, const BBoxList& cbbl)
00345   {
00346    if (&cbbl == BBoxListNULL) return ofs;
00347 
00348    BBoxList &bbl = (BBoxList &) cbbl;
00349    union record *tmpcur == bbl.currec();
00350  
00351    ofs.write((char*)&bbl.num,sizeof(int));
00352 
00353    if (bbl.num == 0) return ofs;
00354    
00355    BBox *b = 0;
00356    for (b = bbl.first(); b != BBoxNULL; b = bbl.next())
00357      ofs << *b;
00358 
00359    bbl.setcurrec(tmpcur);
00360 
00361    return ofs;
00362   }
00363 
00364 strstream&  operator >> (strstream& ifs, BBoxList& cbbl)
00365   {
00366    if (&cbbl == BBoxListNULL) return ifs;
00367 
00368    BBoxList &bbl = (BBoxList &) cbbl;
00369    union record *tmpcur == bbl.currec();
00370  
00371    int numin;
00372    ifs.read((char*)&numin,sizeof(int));
00373 
00374    if (numin == 0) return ifs;
00375 
00376    BBox bb;
00377    for (register int n=0; n<numin; n++) {
00378      ifs >> bb;
00379      bbl.add(bb);
00380    }
00381 
00382    bbl.setcurrec(tmpcur);
00383 
00384    return ifs;
00385   }
00386 


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



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