Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TreeNode.C

Go to the documentation of this file.
00001 
00007 #include "TreeNode.h"
00008 #include "binUtils.h"
00009 #include "parUtils.h"
00010 #include "seqUtils.h"
00011 
00012 #ifdef __DEBUG__
00013 #ifndef __DEBUG_TN__
00014 #define __DEBUG_TN__
00015 #endif
00016 #endif
00017 
00018 namespace ot {
00019 
00020   std::vector<TreeNode> TreeNode::getAllNeighbours() const {
00021     /*
00022        0 = Left;  1 =  Right;  2 =  Front;  3 = Back;   4 = LeftBack;  5 = RightBack;  6 = LeftFront;  7 = RightFront;  8 = Top;
00023        9 = TopRight;  10 =  TopBack;  11 =  TopRightBack;  12 =  Bottom;  13 =  BottomBack;  14 =  TopLeft;  15 =  BottomLeft; 
00024        16 =  BottomRight;  17 =  TopFront;  18 =  BottomFront;  19 =  TopLeftFront;  20 =  TopRightFront;  21 =  BottomLeftFront;
00025        22 =  BottomRightFront;  23 =  TopLeftBack;  24 = BottomLeftBack;  25 = BottomRightBack;
00026        */
00027     std::vector<TreeNode> neighList;
00028 
00029     if( m_uiDim== 3){
00030       neighList.resize(26);
00031       neighList[0] = getLeft();
00032       neighList[1] =  getRight();
00033       neighList[2] =  getFront();
00034       neighList[3] =  getBack(); 
00035       neighList[4] =  getLeftBack();
00036       neighList[5] = getRightBack();
00037       neighList[6] =  getLeftFront();
00038       neighList[7] =  getRightFront();
00039       neighList[8] =  getTop();
00040       neighList[9] = getTopRight();
00041       neighList[10] =  getTopBack();
00042       neighList[11] =  getTopRightBack();
00043       neighList[12] =  getBottom();
00044       neighList[13] =  getBottomBack();
00045       neighList[14] =  getTopLeft();
00046       neighList[15] =  getBottomLeft(); 
00047       neighList[16] =  getBottomRight();
00048       neighList[17] =  getTopFront();
00049       neighList[18] =  getBottomFront();
00050       neighList[19] =  getTopLeftFront();
00051       neighList[20] =  getTopRightFront();
00052       neighList[21] =   getBottomLeftFront();
00053       neighList[22] =   getBottomRightFront();
00054       neighList[23] =  getTopLeftBack();
00055       neighList[24] = getBottomLeftBack();
00056       neighList[25] = getBottomRightBack();
00057     }else if(m_uiDim == 2) {
00058       neighList.resize(8);
00059       neighList[0] = getLeft();
00060       neighList[1] =  getRight();
00061       neighList[2] =  getFront();
00062       neighList[3] =  getBack(); 
00063       neighList[4] =  getLeftBack();
00064       neighList[5] = getRightBack();
00065       neighList[6] =  getLeftFront();
00066       neighList[7] =  getRightFront();
00067     }else {
00068       neighList.resize(2);
00069       neighList[0] = getLeft();
00070       neighList[1] =  getRight();
00071     }
00072     return neighList;
00073   }
00074 
00075   std::vector<std::vector<TreeNode> > TreeNode::getAllB_Neighbours() const {
00076     /*
00077        0 = Left;  1 =  Right;  2 =  Front;  3 = Back;   4 = LeftBack;  5 = RightBack;  6 = LeftFront;  7 = RightFront;  8 = Top;
00078        9 = TopRight;  10 =  TopBack;  11 =  TopRightBack;  12 =  Bottom;  13 =  BottomBack;  14 =  TopLeft;  15 =  BottomLeft; 
00079        16 =  BottomRight;  17 =  TopFront;  18 =  BottomFront;  19 =  TopLeftFront;  20 =  TopRightFront;  21 =  BottomLeftFront;
00080        22 =  BottomRightFront;  23 =  TopLeftBack;  24 = BottomLeftBack;  25 = BottomRightBack;
00081        */
00082 
00083     std::vector<std::vector<TreeNode> > neighList;
00084 
00085     if(m_uiDim == 3){
00086       neighList.resize(26);
00087       neighList[0] = getB_Left();
00088       neighList[1] =  getB_Right();
00089       neighList[2] =  getB_Front();
00090       neighList[3] =  getB_Back(); 
00091       neighList[4] =  getB_LeftBack();
00092       neighList[5] = getB_RightBack();
00093       neighList[6] =  getB_LeftFront();
00094       neighList[7] =  getB_RightFront();
00095       neighList[8] =  getB_Top();
00096       neighList[9] = getB_TopRight();
00097       neighList[10] =  getB_TopBack();
00098       neighList[11] =  getB_TopRightBack();
00099       neighList[12] =  getB_Bottom();
00100       neighList[13] =  getB_BottomBack();
00101       neighList[14] =  getB_TopLeft();
00102       neighList[15] =  getB_BottomLeft(); 
00103       neighList[16] =  getB_BottomRight();
00104       neighList[17] =  getB_TopFront();
00105       neighList[18] =  getB_BottomFront();
00106       neighList[19] =  getB_TopLeftFront();
00107       neighList[20] =  getB_TopRightFront();
00108       neighList[21] =   getB_BottomLeftFront();
00109       neighList[22] =   getB_BottomRightFront();
00110       neighList[23] =  getB_TopLeftBack();
00111       neighList[24] = getB_BottomLeftBack();
00112       neighList[25] = getB_BottomRightBack();
00113     }else if(m_uiDim == 2) {
00114       neighList.resize(8);
00115       neighList[0] = getB_Left();
00116       neighList[1] =  getB_Right();
00117       neighList[2] =  getB_Front();
00118       neighList[3] =  getB_Back(); 
00119       neighList[4] =  getB_LeftBack();
00120       neighList[5] = getB_RightBack();
00121       neighList[6] =  getB_LeftFront();
00122       neighList[7] =  getB_RightFront();
00123     }else {
00124       neighList.resize(2);
00125       neighList[0] = getB_Left();
00126       neighList[1] =  getB_Right();
00127     }
00128     return neighList;
00129   }
00130 
00131 
00132   int TreeNode::addBalancingDescendants(TreeNode other, std::vector<TreeNode>& seeds, bool incCorner) const {
00133 #ifdef __DEBUG_TN__
00134     assert( (other.getLevel()) > ( getLevel() + 1));
00135     assert(areComparable(*this, other));
00136     assert(!(this->isAncestor(other)));
00137 #endif
00138     TreeNode root(m_uiDim,m_uiMaxDepth);
00139     std::vector<TreeNode> nhs = other.getAllNeighbours();
00140     /*
00141        0 = Left;  1 =  Right;  2 =  Front;  3 = Back;   4 = LeftBack;  5 = RightBack;  6 = LeftFront;  7 = RightFront;  8 = Top;
00142        9 = TopRight;  10 =  TopBack;  11 =  TopRightBack;  12 =  Bottom;  13 =  BottomBack;  14 =  TopLeft;  15 =  BottomLeft; 
00143        16 =  BottomRight;  17 =  TopFront;  18 =  BottomFront;  19 =  TopLeftFront;  20 =  TopRightFront;  21 =  BottomLeftFront;
00144        22 =  BottomRightFront;  23 =  TopLeftBack;  24 = BottomLeftBack;  25 = BottomRightBack;
00145        */
00146     std::vector<unsigned int> dirs;
00147     if(m_uiDim == 1) {
00148       //ignore incCorner
00149       dirs.resize(2);
00150       for(unsigned int i = 0; i < dirs.size(); i++) {
00151         dirs[i] = i;
00152       }
00153     }else if(m_uiDim== 2){
00154       if(incCorner){
00155         dirs.resize(8);
00156         for(unsigned int i = 0; i < dirs.size(); i++) {
00157           dirs[i] = i;
00158         }
00159       }else{
00160         dirs.resize(4);
00161         for(unsigned int i = 0; i < dirs.size(); i++) {
00162           dirs[i] = i;
00163         }
00164       }
00165     }else {
00166       if(incCorner){
00167         dirs.resize(26);
00168         for(unsigned int i = 0; i < dirs.size(); i++) {
00169           dirs[i] = i;
00170         }
00171       }else{
00172         dirs.resize(18);
00173         for(int i = 0; i < 11; i++) {
00174           dirs[i] = i;
00175         }
00176         //skip 11 = TRBk
00177         for(int i = 12; i < 19; i++) {
00178           dirs[i-1] = i;
00179         }
00180       }
00181     }
00182     unsigned int oldSz = static_cast<unsigned int>(seeds.size());
00183     seeds.resize(seeds.size()+dirs.size());
00184     for(unsigned int i = 0; i < dirs.size(); i++) {
00185 #ifdef __DEBUG_TN__
00186       assert(areComparable(*this, nhs[dirs[i]]));
00187 #endif
00188       if(this->isAncestor(nhs[dirs[i]])){
00189         seeds[oldSz + i] = nhs[dirs[i]].getParent();
00190       }else {
00191         seeds[oldSz + i]  = root;
00192       }//end if - else
00193     }//end for i
00194     return 1;
00195   }//end function
00196 
00197 
00198   bool TreeNode::isBoundaryOctant(const TreeNode &block, int type, unsigned char *flags) const {
00199     unsigned char _flags = 0;
00200 
00201     unsigned int _x = block.getX();
00202     unsigned int _y = block.getY();
00203     unsigned int _z = block.getZ();
00204     unsigned int _d = block.getLevel();
00205 
00206     /*
00207     // Block has to be an ancestor of the octant or equal to the octant.
00208     if( (!block.isAncestor(*this)) && (block != *this) ) {
00209     if (flags) {
00210      *flags = _flags;
00211      }
00212      return false;
00213      }
00214      */
00215 
00216     if ( (type & NEGATIVE) == NEGATIVE ) {
00217       // test if any of the anchor values matches those of the block ... 
00218       if (m_uiX == _x) _flags |= X_NEG_BDY;  
00219       if (m_uiY == _y) _flags |= Y_NEG_BDY;  
00220       if (m_uiZ == _z) _flags |= Z_NEG_BDY;  
00221     }
00222 
00223     if ( (type & POSITIVE) == POSITIVE ) {
00224       unsigned int len  = (unsigned int)(1u << ( m_uiMaxDepth - getLevel() ) );
00225       unsigned int blen = ((unsigned int)(1u << (m_uiMaxDepth - _d))) - len;
00226 
00227       if ( m_uiX == (_x+blen) )  _flags |= X_POS_BDY;
00228       if ( m_uiY == (_y+blen) )  _flags |= Y_POS_BDY;
00229       if ( m_uiZ == (_z+blen) )  _flags |= Z_POS_BDY;
00230     }
00231 
00232     if (flags) {
00233       *flags = _flags;
00234     }
00235     if (_flags) {
00236       return true;
00237     }
00238     return false;
00239   } //end function
00240 
00241   bool TreeNode::isBoundaryOctant(int type, unsigned char *flags) const {
00242     unsigned char _flags = 0;
00243     if ( (type & NEGATIVE) == NEGATIVE ) {
00244       // test if any of the anchor values is zero ...  (sufficient ??? )
00245       if (!m_uiX) _flags |= X_NEG_BDY;  
00246       if (!m_uiY) _flags |=  Y_NEG_BDY; 
00247       if (!m_uiZ) _flags |=   Z_NEG_BDY;
00248     }
00249 
00250     if ( (type & POSITIVE) == POSITIVE ) {
00251       unsigned int len  = (unsigned int)(1u << ( m_uiMaxDepth - getLevel() ) );
00252       unsigned int blen = ((unsigned int)(1u << m_uiMaxDepth)) - len;
00253 
00254       if ( m_uiX == blen )  _flags |= X_POS_BDY;
00255       if ( m_uiY == blen )  _flags |= Y_POS_BDY;
00256       if ( m_uiZ == blen )  _flags |= Z_POS_BDY;
00257     }
00258 
00259     if (flags)
00260       *flags = _flags;
00261     if (_flags)
00262       return true;
00263 
00264     return false;
00265   } //end function
00266 
00267 
00268   int TreeNode  ::addChildren(std::vector<ot::TreeNode  > &children) const { 
00269     unsigned int dim = m_uiDim;
00270     unsigned int maxDepth = m_uiMaxDepth;
00271     unsigned int childrenSz = children.size();
00272     children.resize(childrenSz + (1 << dim));
00273     if( (m_uiLevel & ot::TreeNode::MAX_LEVEL) == maxDepth) {
00274       for(int i = 0; i < (1 << dim); i++) {
00275         children[childrenSz + i] = *this;
00276       }
00277       return 1;
00278     }
00279     //The check that lev < maxD is taken care of in the constructor.
00280 
00281     //Order: X first, Y next and Z last
00282 
00283     unsigned int len = (unsigned int)(1u << ( maxDepth - ((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1) ) ) ;
00284 
00285     TreeNode   tmpNode0(1,m_uiX,m_uiY ,m_uiZ ,((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00286     children[childrenSz + 0] = tmpNode0;
00287 
00288     TreeNode   tmpNode1(1,(m_uiX + len),m_uiY ,m_uiZ ,((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00289     children[childrenSz + 1] = tmpNode1;
00290 
00291     if( dim >= 2 ) {    
00292       TreeNode   tmpNode2(1,m_uiX,(m_uiY + len) ,m_uiZ ,((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00293       children[childrenSz + 2] = tmpNode2;
00294 
00295       TreeNode   tmpNode3(1,(m_uiX + len),(m_uiY + len) ,m_uiZ  ,((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00296       children[childrenSz + 3] = tmpNode3;
00297     }
00298 
00299     if (dim == 3) {
00300       TreeNode   tmpNode4(1,m_uiX,m_uiY ,(m_uiZ + len),((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00301       children[childrenSz + 4] = tmpNode4;
00302 
00303       TreeNode   tmpNode5(1,(m_uiX+ len),m_uiY ,(m_uiZ + len),((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00304       children[childrenSz + 5] = tmpNode5;
00305 
00306       TreeNode   tmpNode6(1,m_uiX,(m_uiY+ len) ,(m_uiZ + len),((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00307       children[childrenSz + 6] = tmpNode6;
00308 
00309       TreeNode   tmpNode7(1,(m_uiX+ len),(m_uiY + len),(m_uiZ + len),((m_uiLevel & ot::TreeNode::MAX_LEVEL)+1),m_uiDim,m_uiMaxDepth);
00310       children[childrenSz + 7] = tmpNode7;
00311     }//end if
00312     return 1;
00313   }//end function
00314 
00315   int TreeNode  ::addBrothers(std::vector<TreeNode>& bros) const {
00316     unsigned int dim = m_uiDim;
00317     bros.resize(((1 << dim) - 1));
00318     if( (this->m_uiLevel & ot::TreeNode::MAX_LEVEL) == 0) {
00319       for(int i = 0; i< ((1 << dim) - 1); i++) {
00320         bros[i] = *this;
00321       }//end for
00322       return 1;
00323     }//end if
00324 
00325     TreeNode parent = this->getParent();
00326     std::vector<TreeNode> childrenOfParent;
00327     parent.addChildren(childrenOfParent);
00328     int k= 0;
00329     for(int i = 0;i < (1 << dim); i++) {
00330       if(childrenOfParent[i] != (*this)) {
00331         bros[k] = childrenOfParent[i];
00332         k++;
00333       }
00334     }//end for
00335     childrenOfParent.clear();
00336     return 1;
00337   }//end fn.
00338 
00339   std::vector<TreeNode> TreeNode::getSearchKeys(bool incCorners){
00340 
00341     unsigned int myK = this->getChildNumber();
00342     //Morton Order: X,Y,Z
00343     bool zdir = (myK >= 4);
00344     bool ydir = ((myK - ((zdir)?4:0)) >= 2);
00345     bool xdir = (myK%2); 
00346 
00347     unsigned int xCor = this->minX();
00348     unsigned int yCor = this->minY();
00349     unsigned int zCor = this->minZ();
00350     if(xdir){
00351       xCor = ((this->maxX())-1);
00352     }
00353     if(ydir){
00354       yCor = ((this->maxY())-1);
00355     }
00356     if(zdir){
00357       zCor = ((this->maxZ())-1);
00358     }
00359 
00360     TreeNode searchCorner(1,xCor,yCor,zCor,m_uiMaxDepth,m_uiDim,m_uiMaxDepth);  
00361     std::vector<TreeNode> tList;
00362     if(xdir) {
00363       tList.push_back(searchCorner.getRight());
00364     }else {
00365       tList.push_back(searchCorner.getLeft());
00366     }
00367     if(m_uiDim > 1) {
00368       if(ydir) {
00369         tList.push_back(searchCorner.getBack());
00370       }else {
00371         tList.push_back(searchCorner.getFront());
00372       }
00373       if(incCorners || m_uiDim > 2) {
00374         if(ydir) {
00375           if(xdir) {
00376             tList.push_back(searchCorner.getRightBack());
00377           }else {
00378             tList.push_back(searchCorner.getLeftBack());
00379           }
00380         }else {
00381           if(xdir) {
00382             tList.push_back(searchCorner.getRightFront());
00383           }else {
00384             tList.push_back(searchCorner.getLeftFront());
00385           }
00386         }
00387       }
00388     }
00389     if(m_uiDim > 2) {
00390       if(zdir) {
00391         tList.push_back(searchCorner.getTop());
00392         if(xdir) {
00393           tList.push_back(searchCorner.getTopRight());
00394         }else {
00395           tList.push_back(searchCorner.getTopLeft());
00396         }
00397         if(ydir) {
00398           tList.push_back(searchCorner.getTopBack());
00399         }else {
00400           tList.push_back(searchCorner.getTopFront());
00401         }
00402       }else {
00403         tList.push_back(searchCorner.getBottom());
00404         if(xdir) {
00405           tList.push_back(searchCorner.getBottomRight());
00406         }else {
00407           tList.push_back(searchCorner.getBottomLeft());
00408         }
00409         if(ydir) {
00410           tList.push_back(searchCorner.getBottomBack());
00411         }else {
00412           tList.push_back(searchCorner.getBottomFront());
00413         }
00414       }
00415       if(incCorners) {
00416         if(zdir) {
00417           if(xdir) {
00418             if(ydir) {
00419               tList.push_back(searchCorner.getTopRightBack());
00420             }else {
00421               tList.push_back(searchCorner.getTopRightFront());
00422             }
00423           }else {
00424             if(ydir) {
00425               tList.push_back(searchCorner.getTopLeftBack());
00426             }else {
00427               tList.push_back(searchCorner.getTopLeftFront());
00428             }
00429           }
00430         }else {
00431           if(xdir) {
00432             if(ydir) {
00433               tList.push_back(searchCorner.getBottomRightBack());
00434             }else {
00435               tList.push_back(searchCorner.getBottomRightFront());
00436             }
00437           }else {
00438             if(ydir) {
00439               tList.push_back(searchCorner.getBottomLeftBack());
00440             }else {
00441               tList.push_back(searchCorner.getBottomLeftFront());
00442             }
00443           }
00444         }
00445       }
00446     }
00447 
00448     return tList;
00449   }//end function
00450 
00451   std::vector<bool> ot::TreeNode  ::getMorton() const {
00452     unsigned int dim = getDim();
00453     unsigned int maxDepth = getMaxDepth();
00454     unsigned int Ln = 1;
00455     if(maxDepth > 0) {
00456       Ln = binOp::binLength(maxDepth);
00457     }
00458     unsigned int const N = (dim*maxDepth) + Ln;
00459     std::vector<bool> numBin(N);
00460     std::vector<bool> xBin(maxDepth);
00461     std::vector<bool> yBin(maxDepth);
00462     std::vector<bool> zBin(maxDepth);
00463     std::vector<bool> levBin(Ln);
00464 
00465     //create Binary Representations
00466     binOp::toBin(getX(), maxDepth, xBin);
00467     if(dim > 1) { binOp::toBin(getY(), maxDepth, yBin); }
00468     if(dim > 2) { binOp::toBin(getZ(), maxDepth, zBin); }
00469     binOp::toBin(getLevel(), Ln, levBin);
00470 
00471     //Interleave bits
00472     if(dim > 2) {
00473       for(unsigned int j = 0; j < maxDepth; j++) {        
00474         numBin[(j*dim)] = zBin[j];
00475         numBin[((j*dim)+1)] = yBin[j];
00476         numBin[((j*dim)+2)] = xBin[j];
00477       }//end for  
00478     }else if(dim > 1) {
00479       for(unsigned int j = 0; j < maxDepth; j++) {        
00480         numBin[(j*dim)] = yBin[j];
00481         numBin[((j*dim)+1)] = xBin[j];
00482       }//end for  
00483     }else {
00484       for(unsigned int j = 0; j < maxDepth; j++) {        
00485         numBin[j] = xBin[j];
00486       }//end for  
00487     }//end if-else
00488 
00489     //Append level
00490     for(unsigned int j = 0; j < Ln; j++) {
00491       numBin[((maxDepth*dim)+j)] = levBin[j];
00492     }
00493 
00494     return numBin ;
00495   }
00496 
00497   //Constructors...
00498   TreeNode :: TreeNode () {
00499     m_uiX = m_uiY = m_uiZ = m_uiLevel =
00500       m_uiWeight = m_uiDim = m_uiMaxDepth = 0;
00501   }
00502 
00503   TreeNode  :: TreeNode (const int dummy, const unsigned int x,const unsigned int y,
00504       const unsigned int z, const unsigned int lev, const unsigned int dim, const unsigned int maxDepth) {              
00505     m_uiDim = dim;
00506     m_uiMaxDepth = maxDepth;
00507     m_uiX = x;
00508     if(dim > 1) { m_uiY = y; } else { m_uiY = 0; }
00509     if(dim > 2) { m_uiZ = z; } else { m_uiZ = 0; }
00510 
00511     m_uiLevel = lev;
00512     m_uiWeight = 1;
00513   }//end function
00514 
00515   TreeNode  :: TreeNode (const unsigned int dim, const unsigned int maxDepth) {
00516     m_uiX = 0;
00517     m_uiY = 0;
00518     m_uiZ = 0;
00519     m_uiLevel = 0;
00520     m_uiWeight = 1;
00521     m_uiDim = dim; 
00522     m_uiMaxDepth = maxDepth;
00523 #ifdef __DEBUG_TN__
00524     if((dim != 1) && (dim != 2) && (dim != 3)) {
00525       std::cout<<"Wrong Value for dim: "<<dim<<std::endl;   
00526     }
00527 #endif
00528     assert((dim == 1)|| (dim == 2) || (dim == 3));
00529   }//end function
00530 
00531   TreeNode  :: TreeNode (const unsigned int x,const unsigned int y,
00532       const unsigned int z, const unsigned int lev, const unsigned int dim, const unsigned int maxDepth) {              
00533     m_uiDim = dim;
00534     m_uiMaxDepth = maxDepth;
00535     m_uiX = x;
00536     if(dim > 1) { m_uiY = y; } else { m_uiY = 0; }
00537     if(dim > 2) { m_uiZ = z; } else { m_uiZ = 0; }
00538 
00539     m_uiLevel = lev;
00540     m_uiWeight = 1;
00541 
00542 #ifdef __DEBUG_TN__
00543     if((dim != 1) && (dim != 2) && (dim != 3)) {
00544       std::cout<<"Wrong Value for dim: "<<dim<<std::endl;   
00545     }
00546     assert(m_uiX < ((unsigned int)(1u << maxDepth)) );
00547     assert( (m_uiX % ((unsigned int)(1u << (maxDepth-lev))) ) == 0 );
00548     assert(m_uiY < ((unsigned int)(1u << maxDepth)) );
00549     assert( (m_uiY % ((unsigned int)(1u << (maxDepth-lev))) ) == 0 );
00550     assert(m_uiZ < ((unsigned int)(1u << maxDepth)) );
00551     assert( (m_uiZ % ((unsigned int)(1u << (maxDepth-lev))) ) == 0 );
00552     assert((dim == 1)|| (dim == 2) || (dim == 3));      
00553 #endif
00554 
00555   }//end function
00556 
00557   //copy constructor    
00558   TreeNode  :: TreeNode  (const TreeNode  &other) {
00559     m_uiX = other.m_uiX;
00560     m_uiY = other.m_uiY;
00561     m_uiZ = other.m_uiZ;
00562     m_uiLevel = other.m_uiLevel;
00563     m_uiWeight = other.m_uiWeight;
00564     m_uiDim = other.m_uiDim;
00565     m_uiMaxDepth = other.m_uiMaxDepth;
00566   }//end function
00567 
00568 
00569   std::ostream & operator <<(std::ostream & os, TreeNode const & other){
00570     return (os << other.getX() <<" "<< other.getY() <<" "<<other.getZ()<<" "<<other.getLevel());
00571   }//end fn.
00572 
00573 
00574   //Assignment operator
00575   //No checks for dim or maxD. It's ok to change dim and maxD using the
00576   //assignment operator.
00577   TreeNode & TreeNode  :: operator = ( TreeNode   const & other) {
00578     if(this == (&other)) {return *this;}        
00579     m_uiX = other.m_uiX;
00580     m_uiY = other.m_uiY;
00581     m_uiZ = other.m_uiZ;
00582     m_uiLevel = other.m_uiLevel;
00583     m_uiWeight = other.m_uiWeight;
00584     m_uiDim = other.m_uiDim;
00585     m_uiMaxDepth = other.m_uiMaxDepth;
00586     return *this;
00587   }//end fn.
00588 
00589   int TreeNode::pickInternalBoundaryCells(std::vector<TreeNode > & allInternal,
00590       std::vector<TreeNode > & boundary)  const {
00591     unsigned int dim = this->getDim();
00592 
00593     //Pre-alloc
00594     boundary.resize(allInternal.size());
00595     unsigned int boundarySz = 0; 
00596 
00597     int myMinX = this->minX();
00598     int myMinY = this->minY();
00599     int myMinZ = this->minZ();
00600     int myMaxX = this->maxX();
00601     int myMaxY = this->maxY();
00602     int myMaxZ = this->maxZ();
00603 
00604     for(unsigned int i = 0; i < allInternal.size(); i++) {
00605 #ifdef __DEBUG_TN__
00606       assert(areComparable(*this, allInternal[i]));
00607       if(!(this->isAncestor(allInternal[i]) )) {
00608         std::cout<<(*this)<<RED<<" is not an Ancestor of "<<NRM<<allInternal[i]<<std::endl;
00609       }
00610       assert( this->isAncestor(allInternal[i]) );
00611 #endif
00612       int testMinX = allInternal[i].minX();
00613       int testMinY = allInternal[i].minY();
00614       int testMinZ = allInternal[i].minZ();
00615       int testMaxX = allInternal[i].maxX();
00616       int testMaxY = allInternal[i].maxY(); 
00617       int testMaxZ = allInternal[i].maxZ(); 
00618       if((myMinX == testMinX) || (myMaxX == testMaxX)) {
00619         boundary[boundarySz]=(allInternal[i]);
00620         boundarySz++;
00621       }else if(dim > 1) {
00622         if((myMinY == testMinY) || (myMaxY == testMaxY)) {
00623           boundary[boundarySz]=(allInternal[i]);
00624           boundarySz++;
00625         }else if(dim > 2) {
00626           if((myMinZ == testMinZ) || (myMaxZ == testMaxZ)) {
00627             boundary[boundarySz]=(allInternal[i]);
00628             boundarySz++;
00629           }//end if
00630         }//end if-else-if       
00631       }//end if-else-if on boundary
00632     }//end for
00633 
00634     boundary.resize(boundarySz);
00635     return 1;
00636   }//end function
00637 
00638 
00639   int TreeNode::splitInternalAndBoundaryCells(std::vector<TreeNode> & allInternal,
00640       std::vector<TreeNode> & onlyInternal, std::vector<TreeNode> & boundary) const{
00641     unsigned int dim = this->getDim();
00642 
00643     //Pre-alloc
00644     boundary.resize(allInternal.size());
00645     onlyInternal.resize(allInternal.size());
00646     unsigned int boundarySz = 0; 
00647     unsigned int onlyInternalSz = 0; 
00648 
00649     int myMinX = this->minX();
00650     int myMinY = this->minY();
00651     int myMinZ = this->minZ();
00652     int myMaxX = this->maxX();
00653     int myMaxY = this->maxY();
00654     int myMaxZ = this->maxZ();
00655 
00656     for(unsigned int i = 0; i < allInternal.size(); i++) {
00657 #ifdef __DEBUG_TN__
00658       assert(areComparable(*this, allInternal[i]));
00659       if(!(this->isAncestor(allInternal[i]) )) {
00660         std::cout<<(*this)<<RED<<" is not an Ancestor of "<<NRM<<allInternal[i]<<std::endl;
00661       }
00662       assert( this->isAncestor(allInternal[i]) );
00663 #endif
00664       int testMinX = allInternal[i].minX();
00665       int testMinY = allInternal[i].minY();
00666       int testMinZ = allInternal[i].minZ();
00667       int testMaxX = allInternal[i].maxX();
00668       int testMaxY = allInternal[i].maxY(); 
00669       int testMaxZ = allInternal[i].maxZ(); 
00670       if((myMinX == testMinX) || (myMaxX == testMaxX)) {
00671         boundary[boundarySz]=(allInternal[i]);
00672         boundarySz++;
00673       }else if(dim > 1) {
00674         if((myMinY == testMinY) || (myMaxY == testMaxY)) {
00675           boundary[boundarySz]=(allInternal[i]);
00676           boundarySz++;
00677         }else if(dim > 2) {
00678           if((myMinZ == testMinZ) || (myMaxZ == testMaxZ)) {
00679             boundary[boundarySz]=(allInternal[i]);
00680             boundarySz++;
00681           }else {
00682             onlyInternal[onlyInternalSz]=(allInternal[i]);
00683             onlyInternalSz++;
00684           }//end if
00685         }else {
00686           onlyInternal[onlyInternalSz]=(allInternal[i]);
00687           onlyInternalSz++;
00688         }//end if-else-if       
00689       }else {
00690         onlyInternal[onlyInternalSz]=(allInternal[i]);
00691         onlyInternalSz++;
00692       }//end if-else-if on boundary
00693     }//end for
00694 
00695     boundary.resize(boundarySz);
00696     onlyInternal.resize(onlyInternalSz);
00697     return 1;
00698   }//end function
00699 
00700   int TreeNode ::balanceSubtree(std::vector<TreeNode > & inp,
00701       std::vector<TreeNode  > & out, bool incCorner,
00702       bool isSortedCompleteLinear) const {
00703 
00704     PROF_BAL_SUBTREE_BEGIN
00705 
00706       unsigned int dim = getDim();
00707     unsigned int maxDepth = getMaxDepth();      
00708     unsigned int maxCornerX = this->maxX();     
00709     unsigned int maxCornerY = this->maxY();     
00710     unsigned int maxCornerZ = this->maxZ();     
00711     unsigned int minLevInp = maxDepth;
00712     unsigned int maxLevInp = 0;
00713 
00714     out.clear();
00715     if( (maxDepth == 0) || (getLevel() == maxDepth) || (inp.empty()) ) {
00716       //No decendants.
00717       PROF_BAL_SUBTREE_END
00718     }
00719 
00720     std::vector<TreeNode > *workVec = NULL;
00721     unsigned int *workVecSz = NULL;     
00722 
00723     if(maxDepth - getLevel()) {
00724       workVec = new std::vector<TreeNode >[maxDepth - getLevel()];      
00725       workVecSz = new unsigned int [maxDepth-getLevel()];       
00726     }
00727 
00728     for(unsigned int i = 0; i < (maxDepth - getLevel()); i++) {
00729       workVecSz[i] = 0;
00730     }
00731 
00732     for(unsigned int i = 0; i < inp.size(); i++) {
00733 #ifdef __DEBUG_TN__
00734       assert(areComparable(*this, inp[i]));
00735 #endif
00736       if( this->isAncestor(inp[i]) ) {
00737         unsigned int currOctLev = inp[i].getLevel();
00738         workVecSz[maxDepth - currOctLev]++;
00739         if(currOctLev < minLevInp) {
00740           minLevInp = currOctLev;
00741         }
00742         if(currOctLev > maxLevInp) {
00743           maxLevInp = currOctLev;
00744         }
00745       }//end if decendant of block
00746     }//end for
00747 
00748     if(isSortedCompleteLinear && ((maxLevInp - minLevInp) < 2) ) {
00749       //Already balanced
00750       out = inp;
00751 
00752       if(workVecSz) {
00753         delete [] workVecSz;
00754         workVecSz = NULL;
00755       }
00756 
00757       if(workVec) {
00758         delete [] workVec;
00759         workVec = NULL;
00760       }
00761 
00762       PROF_BAL_SUBTREE_END
00763     }
00764 
00765     for(unsigned int i = 0; i < (maxDepth - getLevel()); i++) {
00766       workVec[i].resize(workVecSz[i]);
00767       workVecSz[i] = 0;
00768     }
00769 
00770     for(unsigned int i = 0; i < inp.size(); i++) {
00771 #ifdef __DEBUG_TN__
00772       assert(areComparable(*this, inp[i]));
00773 #endif
00774       if( this->isAncestor(inp[i]) ) {
00775         workVec[maxDepth - (inp[i].getLevel())][workVecSz[maxDepth - (inp[i].getLevel())]] = inp[i];
00776         workVecSz[maxDepth - (inp[i].getLevel())]++;
00777       }
00778     }//end for
00779 
00780     if(workVecSz) {
00781       delete [] workVecSz;
00782       workVecSz = NULL;
00783     }
00784 
00785     TreeNode tmpRoot (getDim(), getMaxDepth());
00786     for(unsigned int i = 0; i < maxDepth - getLevel() -1; i++) {
00787       //This also sorts.
00788       seq::makeVectorUnique(workVec[i], false);
00789       //Remove Brothers from a sorted list.
00790       std::vector<TreeNode> tmpList (workVec[i].size());
00791       unsigned int tmpListSz=0;
00792       if(!workVec[i].empty()) {
00793         tmpList[tmpListSz] = workVec[i][0];
00794         tmpListSz++;
00795       }
00796       for(unsigned int j=1;j<workVec[i].size();j++) {
00797         if(workVec[i][j-1].getParent() != workVec[i][j].getParent()) {
00798           tmpList[tmpListSz] = workVec[i][j];
00799           tmpListSz++;
00800         }
00801       }//end for j
00802       workVec[i].clear();
00803       tmpList.resize(tmpListSz);
00804       workVec[i] = tmpList;
00805       tmpList.clear();
00806       unsigned int oldOutSz = out.size();
00807       unsigned int newOut = 0;
00808       out.resize(oldOutSz + (workVec[i].size()*(1 << dim)));
00809       unsigned int oldNextWsz = workVec[i+1].size();
00810       unsigned int newNextW = 0;
00811       unsigned int wLen = workVec[i].size();
00812       if(wLen > 0) {
00813         workVec[i+1].resize(oldNextWsz + (26*(workVec[i].size())));
00814       }
00815       for(unsigned int j = 0; j < wLen; j++) {
00816         out[oldOutSz + newOut]=workVec[i][j];
00817         newOut++;
00818         std::vector<TreeNode> bros;
00819         workVec[i][j].addBrothers(bros);
00820         for(int k =0;k< ((1 << dim)-1);k++) {
00821           out[oldOutSz + newOut]=bros[k];
00822           newOut++;
00823         }//end for k
00824         bros.clear();
00825 
00826         TreeNode  parent = workVec[i][j].getParent();
00827         TreeNode  tmpNode (getDim(), getMaxDepth());
00828         //Add all the neighbours of parent to workVec[i+1]
00829 
00830         tmpNode = parent.getLeft();
00831         if(tmpNode > tmpRoot) {          
00832           if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00833               (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00834               (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00835             workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00836             newNextW++;
00837           }
00838         }
00839 
00840         tmpNode = parent.getRight();
00841         if(tmpNode > tmpRoot) {          
00842           if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00843               (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00844               (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00845             workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00846             newNextW++;
00847           }
00848         }
00849 
00850         tmpNode = parent.getTop();
00851         if(tmpNode > tmpRoot) {          
00852           if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00853               (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00854               (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00855             workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00856             newNextW++;
00857           }
00858         } 
00859 
00860         tmpNode = parent.getBottom();
00861         if(tmpNode > tmpRoot) {          
00862           if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00863               (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00864               (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00865             workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00866             newNextW++;
00867           }
00868         }    
00869 
00870         tmpNode = parent.getFront();
00871         if(tmpNode > tmpRoot) {          
00872           if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00873               (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00874               (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00875             workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00876             newNextW++;
00877           }
00878         }            
00879 
00880         tmpNode = parent.getBack(); 
00881         if(tmpNode > tmpRoot) {          
00882           if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00883               (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00884               (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00885             workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00886             newNextW++;
00887           }
00888         }        
00889         if(dim == 3 || incCorner) {
00890           tmpNode = parent.getTopLeft();
00891           if(tmpNode > tmpRoot) {                
00892             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00893                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00894                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00895               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00896               newNextW++;
00897             }
00898           }     
00899 
00900           tmpNode = parent.getTopRight();
00901           if(tmpNode > tmpRoot) {                
00902             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00903                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00904                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00905               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00906               newNextW++;
00907             }
00908           }      
00909 
00910           tmpNode = parent.getBottomLeft() ; 
00911           if(tmpNode > tmpRoot) {                
00912             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00913                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00914                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00915               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00916               newNextW++;
00917             }
00918           }    
00919 
00920           tmpNode = parent.getBottomRight();
00921           if(tmpNode > tmpRoot) {                
00922             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00923                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00924                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00925               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00926               newNextW++;
00927             }
00928           } 
00929 
00930           tmpNode = parent.getLeftFront() ;
00931           if(tmpNode > tmpRoot) {                
00932             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00933                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00934                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00935               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00936               newNextW++;
00937             }
00938           }
00939 
00940           tmpNode = parent.getRightFront() ;
00941           if(tmpNode > tmpRoot) {                
00942             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00943                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00944                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00945               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00946               newNextW++;
00947             }
00948           }
00949 
00950           tmpNode = parent.getTopFront() ;
00951           if(tmpNode > tmpRoot) {                
00952             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00953                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00954                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00955               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00956               newNextW++;
00957             }
00958           }
00959 
00960           tmpNode = parent.getBottomFront() ;
00961           if(tmpNode > tmpRoot) {                
00962             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00963                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00964                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00965               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00966               newNextW++;
00967             }
00968           }
00969           tmpNode = parent.getLeftBack() ;
00970           if(tmpNode > tmpRoot) {                
00971             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00972                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00973                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00974               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00975               newNextW++;
00976             }
00977           }
00978 
00979           tmpNode = parent.getRightBack() ;
00980           if(tmpNode > tmpRoot) {                
00981             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00982                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00983                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00984               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00985               newNextW++;
00986             }
00987           }
00988 
00989           tmpNode = parent.getTopBack();
00990           if(tmpNode > tmpRoot) {                
00991             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
00992                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
00993                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
00994               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
00995               newNextW++;
00996             }
00997           }
00998 
00999           tmpNode = parent.getBottomBack();
01000           if(tmpNode > tmpRoot) {                
01001             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01002                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01003                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01004               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01005               newNextW++;
01006             }
01007           }
01008 
01009 
01010         }//end if-incCorner or dim=3
01011         if(incCorner) {
01012           tmpNode = parent.getTopLeftFront() ;
01013           if(tmpNode > tmpRoot) {                
01014             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01015                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01016                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01017               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01018               newNextW++;
01019             }
01020           }
01021 
01022           tmpNode = parent.getTopRightFront(); 
01023           if(tmpNode > tmpRoot) {                
01024             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01025                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01026                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01027               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01028               newNextW++;
01029             }
01030           }
01031 
01032           tmpNode = parent.getBottomLeftFront();
01033           if(tmpNode > tmpRoot) {                
01034             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01035                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01036                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01037               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01038               newNextW++;
01039             }
01040           }
01041 
01042           tmpNode = parent.getBottomRightFront();
01043           if(tmpNode > tmpRoot) {                
01044             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01045                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01046                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01047               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01048               newNextW++;
01049             }
01050           }
01051 
01052           tmpNode = parent.getTopLeftBack();
01053           if(tmpNode > tmpRoot) {                
01054             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01055                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01056                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01057               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01058               newNextW++;
01059             }
01060           }
01061 
01062           tmpNode = parent.getTopRightBack();
01063           if(tmpNode > tmpRoot) {                
01064             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01065                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01066                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01067               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01068               newNextW++;
01069             }
01070           }
01071 
01072           tmpNode = parent.getBottomLeftBack();
01073           if(tmpNode > tmpRoot) {                
01074             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01075                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01076                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01077               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01078               newNextW++;
01079             }
01080           }
01081 
01082           tmpNode = parent.getBottomRightBack();
01083           if(tmpNode > tmpRoot) {                
01084             if( (tmpNode.m_uiX >= this->m_uiX) && (tmpNode.m_uiY >= this->m_uiY) && 
01085                 (tmpNode.m_uiZ >= this->m_uiZ) && (tmpNode.m_uiX < maxCornerX) &&
01086                 (tmpNode.m_uiY < maxCornerY) && (tmpNode.m_uiZ < maxCornerZ) ) {
01087 
01088               workVec[i+1][oldNextWsz + newNextW]=tmpNode;
01089               newNextW++;
01090             }
01091           }
01092         }//end if-incCorner
01093       }//end for j
01094       workVec[i].clear();
01095       if(wLen > 0) {
01096         workVec[i+1].resize(oldNextWsz + newNextW);
01097       }
01098     }//end for i
01099 
01100     if(workVec) {
01101       if(!(workVec[maxDepth-1-getLevel()].empty())) {
01102         unsigned int oldOutSz = out.size();
01103         unsigned int newOut = 0;
01104         out.resize(oldOutSz + (1 << dim));
01105         out[oldOutSz + newOut]=workVec[maxDepth-1-getLevel()][0];
01106         newOut++;
01107         std::vector<TreeNode> bros;
01108         workVec[maxDepth-1-getLevel()][0].addBrothers(bros);
01109         workVec[maxDepth-1-getLevel()].clear();
01110         for(int k =0;k < ((1 << dim) -1);k++) {
01111           out[oldOutSz + newOut]=bros[k];
01112           newOut++;
01113         }//end for k
01114 
01115         bros.clear();
01116       }//end if
01117       delete [] workVec;
01118       workVec = NULL;
01119     }
01120 
01121     //Sort and make unique.
01122     seq::makeVectorUnique<TreeNode>(out,false);
01123     //Remove overlaps.
01124     std::vector<unsigned int> toRem(out.size());
01125     unsigned int remCtr =0;
01126     //compare i+1 with i
01127     for(unsigned int i=1; i<out.size(); i++) {
01128 #ifdef __DEBUG_TN__
01129       assert(areComparable(out[i-1], out[i]));
01130 #endif
01131       if(out[i-1].isAncestor(out[i])) {
01132         toRem[remCtr]=(i-1);
01133         remCtr++;
01134       }//end if
01135     }//end for i
01136     toRem.resize(remCtr);
01137 
01138     if(!toRem.empty()) {
01139       //Note toRem is sorted.
01140       std::vector<TreeNode> tmpRem(out.size()-toRem.size());
01141       unsigned int pos=0; unsigned int kk=0;
01142       for(unsigned int i=0;i<toRem.size();i++) {
01143         for(unsigned int j = pos; j<toRem[i];j++) {
01144           tmpRem[kk]=out[j];
01145           kk++;
01146         }//end for j
01147         pos = toRem[i]+1;
01148       }//end for i
01149       for(unsigned int j = pos;j < out.size(); j++) {
01150         tmpRem[kk] = out[j];
01151         kk++;
01152       }//end for j
01153       out = tmpRem;
01154       tmpRem.clear();
01155     }//end if 
01156     toRem.clear();
01157 
01158     PROF_BAL_SUBTREE_END
01159   }//end function   
01160 
01161   int TreeNode ::completeSubtree(std::vector<TreeNode > & inp,
01162       std::vector<TreeNode  > & out ) const {
01163 
01164     PROF_COMPLETE_SUBTREE_BEGIN
01165 
01166       unsigned int dim = m_uiDim;
01167     unsigned int maxDepth = m_uiMaxDepth;       
01168 
01169     out.clear();
01170     if( (maxDepth == 0) || (getLevel() == maxDepth) || (inp.empty()) ) {
01171       //No decendants.
01172       PROF_COMPLETE_SUBTREE_END
01173     }
01174 
01175     std::vector<TreeNode > *workVec = NULL;
01176     unsigned int *workVecSz = NULL;
01177 
01178     if(maxDepth - getLevel()) {
01179       workVec = new std::vector<TreeNode >[maxDepth-getLevel()];        
01180       workVecSz = new unsigned int [maxDepth-getLevel()];       
01181     }
01182 
01183     for(unsigned int i = 0; i < (maxDepth - getLevel()); i++) {
01184       workVecSz[i] = 0;
01185     }
01186 
01187     for(unsigned int i = 0; i < inp.size(); i++) {
01188 #ifdef __DEBUG_TN__
01189       assert(areComparable(*this, inp[i]));
01190 #endif
01191       if( this->isAncestor(inp[i]) ) {
01192         workVecSz[maxDepth - (inp[i].getLevel())]++;
01193       } else {
01194         std::cout<<inp[i]<<" is not a decendant of "<<(*this)<<std::endl;
01195         assert(false);
01196       }
01197     }//end for
01198 
01199     for(unsigned int i = 0; i < (maxDepth - getLevel()); i++) {
01200       workVec[i].resize(workVecSz[i]);
01201       workVecSz[i] = 0;
01202     }
01203 
01204     for(unsigned int i = 0; i < inp.size(); i++) {
01205 #ifdef __DEBUG_TN__
01206       assert(areComparable(*this, inp[i]));
01207 #endif
01208       if( this->isAncestor(inp[i]) ) {
01209         workVec[maxDepth - (inp[i].getLevel())][workVecSz[maxDepth - (inp[i].getLevel())]] = inp[i];
01210         workVecSz[maxDepth - (inp[i].getLevel())]++;
01211       }
01212     }//end for
01213 
01214     if(workVecSz) {
01215       delete [] workVecSz;
01216       workVecSz = NULL;
01217     }
01218 
01219     TreeNode tmpRoot (m_uiDim, m_uiMaxDepth);
01220     for(unsigned int i = 0; i < (maxDepth - getLevel() -1); i++) {
01221       //This also sorts.
01222       seq::makeVectorUnique(workVec[i], false);
01223       //Remove Brothers from a sorted list.
01224       std::vector<TreeNode> tmpList (workVec[i].size());
01225       unsigned int tmpListSz=0;
01226       if(!workVec[i].empty()) {
01227         tmpList[tmpListSz] = workVec[i][0];
01228         tmpListSz++;
01229       }
01230       for(unsigned int j = 1; j < workVec[i].size(); j++) {
01231         if(workVec[i][j-1].getParent() != workVec[i][j].getParent()) {
01232           tmpList[tmpListSz] = workVec[i][j];
01233           tmpListSz++;
01234         }
01235       }//end for j
01236       workVec[i].clear();
01237       tmpList.resize(tmpListSz);
01238       workVec[i] = tmpList;
01239       tmpList.clear();
01240       unsigned int oldOutSz = out.size();
01241       unsigned int newOut = 0;
01242       out.resize(oldOutSz + (workVec[i].size()*(1 << dim)));
01243       unsigned int oldNextWsz = workVec[i+1].size();
01244       unsigned int newNextW = 0;
01245       unsigned int wLen = workVec[i].size();
01246       if(wLen > 0) {
01247         workVec[i+1].resize(oldNextWsz + (7*(workVec[i].size())));
01248       }
01249       for(unsigned int j = 0; j < wLen; j++) {
01250         out[oldOutSz + newOut]=workVec[i][j];
01251         newOut++;
01252         std::vector<TreeNode> bros;
01253         workVec[i][j].addBrothers(bros);
01254         for(int k =0;k< ((1 << dim)-1);k++) {
01255           out[oldOutSz + newOut]=bros[k];
01256           newOut++;
01257         }//end for k
01258         bros.clear();
01259 
01260         TreeNode  parent = workVec[i][j].getParent();
01261         parent.addBrothers(bros);
01262         for(int k =0;k< ((1 << dim)-1);k++) {
01263           workVec[i+1][oldNextWsz + newNextW]=bros[k];
01264           newNextW++;
01265         }//end for k
01266         bros.clear();
01267         //Add all the brothers of parent to workVec[i+1]
01268       }//end for j
01269       workVec[i].clear();
01270       if(wLen > 0) {
01271         workVec[i+1].resize(oldNextWsz + newNextW);
01272       }
01273     }//end for i
01274 
01275     if(workVec) {
01276       if(!(workVec[maxDepth-1-getLevel()].empty())) {
01277         unsigned int oldOutSz = out.size();
01278         unsigned int newOut = 0;
01279         out.resize(oldOutSz + (1 << dim));
01280         out[oldOutSz + newOut]=workVec[maxDepth-1-getLevel()][0];
01281         newOut++;
01282         std::vector<TreeNode> bros;
01283         workVec[maxDepth-1-getLevel()][0].addBrothers(bros);
01284         workVec[maxDepth-1-getLevel()].clear();
01285         for(int k =0;k < ((1 << dim) -1);k++) {
01286           out[oldOutSz + newOut]=bros[k];
01287           newOut++;
01288         }//end for k
01289 
01290         bros.clear();
01291       }//end if
01292       delete [] workVec;
01293       workVec = NULL;
01294     }
01295 
01296     //sort and removde dups.
01297     seq::makeVectorUnique<TreeNode>(out, false);
01298     //Remove overlaps.
01299     std::vector<unsigned int> toRem(out.size());
01300     unsigned int remCtr =0;
01301     //compare i+1 with i
01302     for(unsigned int i = 1; i < out.size(); i++) {
01303 #ifdef __DEBUG_TN__
01304       assert(areComparable(out[i-1], out[i]));
01305 #endif
01306       if(out[i-1].isAncestor(out[i])) {
01307         toRem[remCtr]=(i-1);
01308         remCtr++;
01309       }//end if
01310     }//end for i
01311     toRem.resize(remCtr);
01312 
01313     if(!toRem.empty()) {
01314       //Note toRem is sorted.
01315       std::vector<TreeNode> tmpRem(out.size()-toRem.size());
01316       unsigned int pos = 0;
01317       unsigned int kk=0;
01318       for(unsigned int i = 0; i < toRem.size(); i++) {
01319         for(unsigned int j = pos; j < toRem[i]; j++) {
01320           tmpRem[kk]=out[j];
01321           kk++;
01322         }//end for j
01323         pos = toRem[i]+1;
01324       }//end for i
01325       for(unsigned int j = pos; j < out.size(); j++) {
01326         tmpRem[kk] = out[j];
01327         kk++;
01328       }//end for j
01329       out = tmpRem;
01330       tmpRem.clear();
01331     }//end if 
01332     toRem.clear();
01333 
01334     PROF_COMPLETE_SUBTREE_END
01335 
01336   }//end function   
01337 
01338 }//end namespace

Generated on Tue Mar 24 16:14:05 2009 for DENDRO by  doxygen 1.3.9.1