Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

vhdABTTree.cpp

00001 00002 // Copyright : Sebastien Schertenleib 00003 // : VRLab-EPFL 00004 // 00005 // Email :Sebastien.Schertenleib@epfl.ch 00006 // 00007 // This source code is a part of the Game Programming Gems 5 Source Code 00008 // 00009 // Permission to copy, modify, reproduce or redistribute this source code is 00010 // granted provided the above copyright notice is retained in the resulting 00011 // source code. 00012 // 00013 // This file is provided 'as is' with no expressed or implied warranty. 00014 // The author accepts no liability if it causes any damage to your computer. 00015 // 00016 // Do expect bugs. 00017 // Please let me know of any bugs/mods/improvements. 00018 // and I will try to fix/incorporate them into this file. 00019 // Enjoy! 00020 // 00022 00023 00024 00025 #include "vhdABTTree.h" 00026 #include "vhdSceneConverterABTTree.h" 00027 00028 #include <iostream> 00029 #include <math.h> 00030 00031 #define BITLEAF 0 00032 #define LX 1 00033 #define LY 2 00034 #define LZ 3 00035 #define RX 4 00036 #define RY 5 00037 #define RZ 6 00038 #define BITFLAG 7 00039 00040 #define MASK_ISLEAF 0x01 00041 #define MASK_NULL 0x00 00042 #define MASK_LX 0x80 00043 #define MASK_LY 0x40 00044 #define MASK_LZ 0x20 00045 #define MASK_RX 0x10 00046 #define MASK_RY 0x08 00047 #define MASK_RZ 0x04 00048 #define MASK_LL 0x02 00049 #define MASK_RL 0x01 00050 00051 00052 00053 00054 uint32 00055 vhdABTTree::_computeSubTreeDepth() 00056 { 00057 unsigned int nResult(0); 00058 unsigned int nCur(vhdABTTree::nCacheLineBytes); 00059 nCur/=4 ; 00060 while (nCur > 2) 00061 { 00062 //we divide in two 00063 nCur/=2 ; 00064 ++nResult; 00065 } 00066 return nResult; 00067 } 00068 00069 //set up memory structure constants 00070 #define ABTTREE_CACHE_LINE_BYTES 64 00071 00072 uint32 vhdABTTree::nCacheLineBytes = ABTTREE_CACHE_LINE_BYTES; 00073 uint32 vhdABTTree::nCacheLineNodes = vhdABTTree::nCacheLineBytes / sizeof(vhdABTTreeNode); 00074 uint32 vhdABTTree::nDepth=vhdABTTree::_computeSubTreeDepth(); 00075 uint32 vhdABTTree::nSubTreeId = pow(2,vhdABTTree::nDepth-1) -1; 00076 const uint32 vhdABTTree::nArrayDeep = 180; 00077 00078 00079 00080 00081 void 00082 vhdABTTree::visit(unsigned int uiCurrentPos, unsigned int uiIndex, const vhdAxisAlignedBox &bbox) 00083 { 00084 return; 00085 } 00086 00087 00088 vhdABTTree::vhdABTTree() 00089 { 00090 00091 _nFirstFreeCacheLineIndex = 0; 00092 _nLeafIndex = 0; 00093 funcIsVisible= vhdABTTree::_isVisible; 00094 funcVisit = vhdABTTree::visit; 00095 } 00096 00097 00098 void 00099 vhdABTTree::create(uint32 nObjects) 00100 { 00101 _nFirstFreeCacheLineIndex = 0; 00102 nCacheLines = nObjects * vhdABTTree::nArrayDeep / nCacheLineNodes + 1; 00103 00104 uint32 maxFreeNodes = vhdABTTree::nCacheLines * (vhdABTTree::nCacheLineNodes + 1); 00105 _pNode= new vhdABTTreeNode[maxFreeNodes]; 00106 memset(_pNode,0,maxFreeNodes*sizeof(vhdABTTreeNode)); 00107 00108 _pLeaf = new vhdABTTreeLeaf[nObjects]; 00109 _nLeafIndex = 0; 00110 00111 vhdAxisAlignedBox boxLeft; 00112 vhdAxisAlignedBox boxRight; 00113 00114 //we are looking for getting the absolute bounding box. 00115 //id 0 contains all geometry 00116 vhdSceneConverterABTTree::getSingleton().getChildren(vhdSceneConverterABTTree::getSingleton().pDataRootNode ,0, boxLeft, boxRight); 00117 _bbox = vhdSceneConverterABTTree::getSingleton().findGlobalAABB(vhdSceneConverterABTTree::getSingleton().pDataRootNode->listAABB); 00118 00119 _makeNodeDataFlags(0,_bbox,boxLeft, boxRight); 00120 00121 //go with the build 00122 subTreeBuild(0, _bbox); 00123 } 00124 00125 void 00126 vhdABTTree::destroy() 00127 { 00128 delete[] _pLeaf; 00129 delete[] _pNode; 00130 } 00131 00132 void 00133 vhdABTTree::create(unsigned int nCacheLineSize, unsigned int nLeafSize ) 00134 { 00135 _nFirstFreeCacheLineIndex = 0; 00136 nCacheLines = nCacheLineSize; 00137 00138 _pNode= new vhdABTTreeNode[nCacheLineSize]; 00139 00140 _pLeaf = new vhdABTTreeLeaf[nLeafSize]; 00141 _nLeafIndex = nLeafSize; 00142 } 00143 00144 00145 byte 00146 vhdABTTree::_extendLeft(float fMinExtend, float fMinParent, float fMaxParent) 00147 { 00148 return (byte) (255* ( (fMinExtend - fMinParent) / ( fMaxParent - fMinParent ) ) ); 00149 } 00150 byte 00151 vhdABTTree::_extendRight(float fMaxExtend, float fMinParent, float fMaxParent) 00152 { 00153 return (byte) (255* ( ( fMaxParent - fMaxExtend ) / ( fMaxParent - fMinParent ) ) ); 00154 } 00155 00156 void 00157 vhdABTTree::_makeNodeDataFlags(uint32 nCurrentChildPosition, const vhdAxisAlignedBox &box, const vhdAxisAlignedBox &boxLeft, const vhdAxisAlignedBox & boxRight) 00158 { 00159 vhdVector3 minBox = box.getMinimum(); 00160 vhdVector3 maxBox = box.getMaximum(); 00161 00162 vhdVector3 minBoxLeft = boxLeft.getMinimum(); 00163 vhdVector3 maxBoxLeft = boxLeft.getMaximum(); 00164 vhdVector3 minBoxRight = boxRight.getMinimum(); 00165 vhdVector3 maxBoxRight = boxRight.getMaximum(); 00166 00167 #ifdef TREE_DEBUG 00168 std::cout << "vhdABTTree::_makeNodeDataFlags " << box << " " << boxLeft << " " << boxRight << std::endl; 00169 #endif 00170 00171 if (minBox.x == minBoxLeft.x) 00172 { 00173 _pNode[nCurrentChildPosition].node.data[BITFLAG] = _pNode[nCurrentChildPosition].node.data[BITFLAG] | MASK_LX; 00174 _pNode[nCurrentChildPosition].node.data[LX] = _extendLeft(minBoxRight.x, minBox.x, maxBox.x); 00175 } 00176 else 00177 { 00178 _pNode[nCurrentChildPosition].node.data[LX] = _extendLeft(minBoxLeft.x, minBox.x, maxBox.x); 00179 } 00180 if (minBox.y == minBoxLeft.y) 00181 { 00182 _pNode[nCurrentChildPosition].node.data[BITFLAG] = _pNode[nCurrentChildPosition].node.data[BITFLAG] | MASK_LY; 00183 _pNode[nCurrentChildPosition].node.data[LY] = _extendLeft(minBoxRight.y, minBox.y, maxBox.y); 00184 } 00185 else 00186 { 00187 _pNode[nCurrentChildPosition].node.data[LY] = _extendLeft(minBoxLeft.y, minBox.y, maxBox.y); 00188 } 00189 if (minBox.z == minBoxLeft.z) 00190 { 00191 _pNode[nCurrentChildPosition].node.data[BITFLAG] = _pNode[nCurrentChildPosition].node.data[BITFLAG] | MASK_LZ; 00192 _pNode[nCurrentChildPosition].node.data[LZ] = _extendLeft(minBoxRight.z, minBox.z, maxBox.z); 00193 } 00194 else 00195 { 00196 _pNode[nCurrentChildPosition].node.data[LZ] = _extendLeft(minBoxLeft.z, minBox.z, maxBox.z); 00197 } 00198 if (maxBox.x == maxBoxLeft.x) 00199 { 00200 _pNode[nCurrentChildPosition].node.data[BITFLAG] = _pNode[nCurrentChildPosition].node.data[BITFLAG] | MASK_RX; 00201 _pNode[nCurrentChildPosition].node.data[RX] = _extendRight(maxBoxRight.x, minBox.x, maxBox.x); 00202 } 00203 else 00204 { 00205 _pNode[nCurrentChildPosition].node.data[RX] = _extendRight(maxBoxLeft.x, minBox.x, maxBox.x); 00206 } 00207 if (maxBox.y == maxBoxLeft.y) 00208 { 00209 _pNode[nCurrentChildPosition].node.data[BITFLAG] = _pNode[nCurrentChildPosition].node.data[BITFLAG] | MASK_RY; 00210 _pNode[nCurrentChildPosition].node.data[RY] = _extendRight(maxBoxRight.y, minBox.y, maxBox.y); 00211 } 00212 else 00213 { 00214 _pNode[nCurrentChildPosition].node.data[RY] = _extendRight(maxBoxLeft.y, minBox.y, maxBox.y); 00215 } 00216 if (maxBox.z == maxBoxLeft.z) 00217 { 00218 _pNode[nCurrentChildPosition].node.data[BITFLAG] = _pNode[nCurrentChildPosition].node.data[BITFLAG] | MASK_RZ; 00219 _pNode[nCurrentChildPosition].node.data[RZ] = _extendRight(maxBoxRight.z, minBox.z, maxBox.z); 00220 } 00221 else 00222 { 00223 _pNode[nCurrentChildPosition].node.data[RZ] = _extendRight(maxBoxLeft.z, minBox.z, maxBox.z); 00224 } 00225 00226 _pNode[nCurrentChildPosition].node.data[BITLEAF] = MASK_NULL; 00227 } 00228 00229 00230 00231 void 00232 vhdABTTree::_makeChildren(ListElement & listElement, const vhdAxisAlignedBox &box, uint32 nCurrentPosition,uint32 nCurrentChildPosition, uint32 index, bool bLeft ) 00233 { 00234 //get the two box bbox. 00235 vhdAxisAlignedBox boxLeft,boxRight; 00236 00237 vhdSceneConverterABTTree::vhdSceneABTTreeNode *pCurNode= NULL; 00238 if (bLeft) 00239 { 00240 pCurNode=vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentPosition].pLeft; 00241 } 00242 else 00243 { 00244 pCurNode=vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentPosition].pRight; 00245 } 00246 pCurNode->pLeft=pCurNode->pRight=NULL; 00247 00248 std::cout << "Make Children : " << nCurrentChildPosition << std::endl; 00249 00250 vhdSceneConverterABTTree::getSingleton().getChildren(pCurNode ,nCurrentChildPosition,boxLeft, boxRight); 00251 00252 00253 _makeNodeDataFlags(nCurrentChildPosition, box, boxLeft,boxRight); 00254 listElement.push_back(Element(index, box)); 00255 } 00256 00257 00258 00259 void 00260 vhdABTTree::_makeLeaf(uint32 nCurrentPosition,uint32 nCurrentChildPosition ,bool bLeft) 00261 { 00262 _pNode[nCurrentChildPosition].leaf.index = _nLeafIndex << MASK_ISLEAF; 00263 _pNode[nCurrentChildPosition].leaf.index = _pNode[nCurrentChildPosition].leaf.index | MASK_ISLEAF; 00264 _pNode[nCurrentChildPosition].leaf.callback = NULL; 00265 ++_nLeafIndex; 00266 00267 00268 //we insert the current status in our map set to null so means leaf!!! 00269 vhdSceneConverterABTTree::vhdSceneABTTreeNode data; 00270 if (bLeft) 00271 { 00272 data.listAABB = vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentPosition].pLeft->listAABB; 00273 } 00274 else 00275 { 00276 data.listAABB = vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentPosition].pRight->listAABB; 00277 } 00278 data.pLeft = NULL; 00279 data.pRight = NULL; 00280 00281 std::cout << "Make Leaf" << std::endl; 00282 std::cout << "Current Child Position : " << nCurrentChildPosition << std::endl; 00283 std::cout << "Leaf Index : " << _nLeafIndex << std::endl; 00284 std::cout << "Lead ID : "; 00285 //loop 00286 vhdSceneConverterABTTree::ListAABB::iterator it = data.listAABB.begin(); 00287 std::cout << it->strPath; 00288 ++it; 00289 for ( it; it!= data.listAABB.end(); ++it) 00290 { 00291 std::cout << " , " << it->strPath; 00292 } 00293 std::cout << std::endl << std::endl; 00294 00295 vhdSceneConverterABTTree::getSingleton().mapNodeChild.insert(vhdSceneConverterABTTree::MapNodeChild::value_type(nCurrentChildPosition,data)); 00296 } 00297 00298 00299 bool 00300 vhdABTTree::_needToSplit(const vhdAxisAlignedBox &box, uint32 nCurrentPos, bool bLeft) 00301 { 00302 //we check with this companion methods 00303 vhdSceneConverterABTTree::vhdSceneABTTreeNode *pCurNode= NULL; 00304 if (bLeft) 00305 { 00306 pCurNode=vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentPos].pLeft; 00307 } 00308 else 00309 { 00310 pCurNode=vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentPos].pRight; 00311 } 00312 00313 if (pCurNode ) 00314 { 00315 //2 for each sub-node (left/right) 00316 if(pCurNode->listAABB.size()>4) 00317 { 00318 return true; 00319 } 00320 } 00321 return false; 00322 } 00323 00324 00325 00326 void 00327 vhdABTTree::_computeChildBox(const vhdAxisAlignedBox & boxParent, vhdAxisAlignedBox &boxLeft,vhdAxisAlignedBox &boxRight, uint32 nCurrentPos ) 00328 { 00329 vhdVector3 vecMinParent = boxParent.getMinimum(); 00330 vhdVector3 vecMaxParent = boxParent.getMaximum(); 00331 vhdVector3 vecMinLeft, vecMinRight, vecMaxLeft, vecMaxRight; 00332 00333 00334 if ( (_pNode[nCurrentPos].node.data[BITFLAG] & MASK_LX) == MASK_LX) 00335 { 00336 //mean that the left children take the parent value 00337 vecMinLeft.x = vecMinParent.x; 00338 vecMinRight.x = ((float)(_pNode[nCurrentPos].node.data[LX])/255.0f) * (vecMaxParent.x-vecMinParent.x) + vecMinParent.x; 00339 } 00340 else 00341 { 00342 //left is parent 00343 vecMinRight.x = vecMinParent.x; 00344 vecMinLeft.x = ((float)(_pNode[nCurrentPos].node.data[LX])/255.0f) * (vecMaxParent.x-vecMinParent.x) + vecMinParent.x; 00345 } 00346 00347 if ((_pNode[nCurrentPos].node.data[BITFLAG] & MASK_LY) == MASK_LY) 00348 { 00349 //mean that the left children take the parent value 00350 vecMinLeft.y = vecMinParent.y; 00351 vecMinRight.y = ((float)(_pNode[nCurrentPos].node.data[LY])/255.0f) * (vecMaxParent.y-vecMinParent.y) + vecMinParent.y; 00352 } 00353 else 00354 { 00355 //left is parent 00356 vecMinRight.y = vecMinParent.y; 00357 vecMinLeft.y = ((float)(_pNode[nCurrentPos].node.data[LY])/255.0f) * (vecMaxParent.y-vecMinParent.y) + vecMinParent.y; 00358 } 00359 00360 if ((_pNode[nCurrentPos].node.data[BITFLAG] & MASK_LZ) == MASK_LZ) 00361 { 00362 //mean that the left children take the parent value 00363 vecMinLeft.z = vecMinParent.z; 00364 vecMinRight.z = ((float)(_pNode[nCurrentPos].node.data[LZ])/255.0f) * (vecMaxParent.z-vecMinParent.z) + vecMinParent.z; 00365 } 00366 else 00367 { 00368 //left is parent 00369 vecMinRight.z = vecMinParent.z; 00370 vecMinLeft.z = ((float)(_pNode[nCurrentPos].node.data[LZ])/255.0f) * (vecMaxParent.z-vecMinParent.z) + vecMinParent.z; 00371 } 00372 00373 if ((_pNode[nCurrentPos].node.data[BITFLAG] & MASK_RX) == MASK_RX) 00374 { 00375 //mean that the left children take the parent value 00376 vecMaxLeft.x = vecMaxParent.x; 00377 vecMaxRight.x = vecMaxParent.x- ((float)(_pNode[nCurrentPos].node.data[RX])/255.0f) * (vecMaxParent.x-vecMinParent.x); 00378 } 00379 else 00380 { 00381 //left is parent 00382 vecMaxRight.x = vecMaxParent.x; 00383 vecMaxLeft.x = vecMaxParent.x -((float)(_pNode[nCurrentPos].node.data[RX])/255.0f) * (vecMaxParent.x-vecMinParent.x); 00384 } 00385 00386 if ((_pNode[nCurrentPos].node.data[BITFLAG] & MASK_RY) == MASK_RY) 00387 { 00388 //mean that the left children take the parent value 00389 vecMaxLeft.y = vecMaxParent.y; 00390 vecMaxRight.y = vecMaxParent.y-((float)(_pNode[nCurrentPos].node.data[RY])/255.0f) * (vecMaxParent.y-vecMinParent.y); 00391 } 00392 else 00393 { 00394 //left is parent 00395 vecMaxRight.y = vecMaxParent.y; 00396 vecMaxLeft.y = vecMaxParent.y - ((float)(_pNode[nCurrentPos].node.data[RY])/255.0f) * (vecMaxParent.y-vecMinParent.y); 00397 } 00398 00399 if ((_pNode[nCurrentPos].node.data[BITFLAG] & MASK_RZ) == MASK_RZ ) 00400 { 00401 //mean that the left children take the parent value 00402 vecMaxLeft.z = vecMaxParent.z; 00403 vecMaxRight.z = vecMaxParent.z - ((float)(_pNode[nCurrentPos].node.data[RZ])/255.0f) * (vecMaxParent.z-vecMinParent.z); 00404 } 00405 else 00406 { 00407 //left is parent 00408 vecMaxRight.z = vecMaxParent.z; 00409 vecMaxLeft.z = vecMaxParent.z - ((float)(_pNode[nCurrentPos].node.data[RZ])/255.0f) * (vecMaxParent.z-vecMinParent.z) ; 00410 } 00411 00412 //at this time we have both vhdAxisAlignedBox 00413 if (vecMinLeft.x>vecMaxLeft.x) 00414 { 00415 std::swap(vecMinLeft.x,vecMaxLeft.x); 00416 } 00417 if (vecMinLeft.y>vecMaxLeft.y) 00418 { 00419 std::swap(vecMinLeft.y,vecMaxLeft.y); 00420 } 00421 if (vecMinLeft.z>vecMaxLeft.z) 00422 { 00423 std::swap(vecMinLeft.z,vecMaxLeft.z); 00424 } 00425 if (vecMinRight.x>vecMaxRight.x) 00426 { 00427 std::swap(vecMinRight.x,vecMaxRight.x); 00428 } 00429 if (vecMinRight.y>vecMaxRight.y) 00430 { 00431 std::swap(vecMinRight.y,vecMaxRight.y); 00432 } 00433 if (vecMinRight.z>vecMaxRight.z) 00434 { 00435 std::swap(vecMinRight.z,vecMaxRight.z); 00436 } 00437 boxLeft.setExtents(vecMinLeft.x, vecMinLeft.y, vecMinLeft.z, vecMaxLeft.x, vecMaxLeft.y, vecMaxLeft.z); 00438 boxRight.setExtents(vecMinRight.x, vecMinRight.y, vecMinRight.z, vecMaxRight.x, vecMaxRight.y, vecMaxRight.z); 00439 00440 } 00441 00442 00443 00444 void 00445 vhdABTTree::subTreeBuild(uint32 nCurrentCacheLineIndex, const vhdAxisAlignedBox &box) 00446 { 00447 std::cout << "Current Cache Line Index : " << nCurrentCacheLineIndex << std::endl; 00448 std::cout << "First Free Cache Line Index : " << _nFirstFreeCacheLineIndex << std::endl; 00449 std::cout << std::endl; 00450 00451 //sub-tree must be like this, their are reference in their local index in the current cache lines 00452 // 1|2 00453 // 3|4 5|6 00454 // 7|8 9|10 11|12 13|14 00455 00456 // 1 solution 00457 // 00458 // List = [1] 00459 //ask for split 00460 //for each el in list, pop_front() , [ push_children] 00461 // 00462 // ex: 00463 // 1|2 --> 0 00464 // 3|4 5|6 --> 1 2 00465 // 5|6 7|8 9|10 ---> 2 3 4 00466 // 7|8 9|10 11|12 13|14 --> 3 4 5 6 00467 // 9|10 11|12 13|14 --> 4 5 6 00468 // 11|12 13|14 --> 5 6 00469 // 13|14 --> 6 00470 00471 00472 ListElement listElement; 00473 listElement.push_back(Element(0,box)); 00474 00475 while (listElement.size()>0) 00476 { 00477 //check if we are not at the last line of the sub-tree 00478 if ( listElement.begin()->id >= nSubTreeId) 00479 { 00480 //we reach the end line 00481 break; 00482 } 00483 uint32 nCurrentPos = nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id; 00484 00485 if ((_pNode[nCurrentPos].node.data[BITLEAF] & MASK_ISLEAF) == MASK_ISLEAF) 00486 { 00487 //this is a leaf, 00488 //skip it (ony if local root is leaf, could be done once only) 00489 listElement.pop_front(); 00490 continue; 00491 } 00492 00493 vhdAxisAlignedBox boxLeft,boxRight; 00494 _computeChildBox(listElement.begin()->bbox,boxLeft,boxRight, nCurrentPos); 00495 00496 00497 //check if left need to be split 00498 if (_needToSplit(boxLeft,nCurrentPos,true)) 00499 { 00500 _makeChildren(listElement,boxLeft,nCurrentPos,nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id*2 + 1, listElement.begin()->id*2 + 1,true); 00501 } 00502 else 00503 { 00504 _makeLeaf(nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id,nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id*2 + 1,true); 00505 } 00506 //right 00507 if (_needToSplit(boxRight,nCurrentPos,false)) 00508 { 00509 _makeChildren(listElement,boxRight,nCurrentPos,nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id*2 + 2,listElement.begin()->id*2 + 2,false); 00510 } 00511 else 00512 { 00513 _makeLeaf(nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id,nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id*2 + 2,false); 00514 } 00515 listElement.pop_front(); 00516 } 00517 00518 if (listElement.size()>0) 00519 00520 { 00521 //at this stage, we need to handle the final line of this sub-tree 00522 bool bFirst(true); 00523 uint32 i; 00524 ListElement::iterator it(listElement.begin()); 00525 for ( i = nSubTreeId ; i < vhdABTTree::nCacheLineNodes-1; ++i) 00526 { 00527 if ( it->id != i) 00528 { 00529 //this potential node is not existing, so skipt it 00530 continue; 00531 } 00532 uint32 nCurrentPos = nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + it->id; 00533 if ( (_pNode[nCurrentPos].node.data[BITLEAF] & MASK_ISLEAF) == MASK_ISLEAF) 00534 { 00535 //this is a leaf, so we don't care 00536 ++it; 00537 continue; 00538 } 00539 _nFirstFreeCacheLineIndex+=2; //for left and right 00540 #ifdef TREE_DEBUG 00541 std::cout << "_nFirstFreeCacheLineIndex internal " << _nFirstFreeCacheLineIndex << std::endl; 00542 #endif 00543 if (bFirst) 00544 { 00545 //setup the next children into the line 00546 bFirst = false; 00547 _pNode[(nCurrentCacheLineIndex+1) * vhdABTTree::nCacheLineNodes -1 ].leaf.index = _nFirstFreeCacheLineIndex-1; //point to the left 00548 } 00549 //set this node as a non-leaf for continuity, use during traversal 00550 uint32 mask = 0x1 << (i-nSubTreeId); 00551 _pNode[(nCurrentCacheLineIndex+1) * vhdABTTree::nCacheLineNodes -1 ].leaf.callback = _pNode[(nCurrentCacheLineIndex+1) * vhdABTTree::nCacheLineNodes -1 ].leaf.callback | mask; 00552 00553 ++it; 00554 } 00555 00556 uint32 nChildIndex=_pNode[(nCurrentCacheLineIndex+1) * vhdABTTree::nCacheLineNodes -1 ].leaf.index; 00557 for ( it = listElement.begin(); it != listElement.end(); ++it) 00558 { 00559 //recursive call 00560 vhdAxisAlignedBox boxLeft,boxRight; 00561 _computeChildBox(listElement.begin()->bbox,boxLeft,boxRight, nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id); 00562 //left 00563 //first el is not a leaf 00564 _pNode[nChildIndex*vhdABTTree::nCacheLineNodes].node.data[BITLEAF] = MASK_NULL; 00565 //we need to provide the next right and left here, so lets do it! 00566 vhdAxisAlignedBox boxLeftL,boxRightL; 00567 00568 vhdSceneConverterABTTree::getSingleton().getChildren(vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + it->id].pLeft ,nChildIndex*vhdABTTree::nCacheLineNodes, boxLeftL, boxRightL); 00569 _makeNodeDataFlags(nChildIndex*vhdABTTree::nCacheLineNodes,it->bbox,boxLeftL, boxRightL); 00570 subTreeBuild(nChildIndex++,boxLeft); 00571 00572 vhdSceneConverterABTTree::getSingleton().getChildren(vhdSceneConverterABTTree::getSingleton().mapNodeChild[nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + it->id].pRight ,nChildIndex*vhdABTTree::nCacheLineNodes, boxLeftL, boxRightL); 00573 _makeNodeDataFlags(nChildIndex*vhdABTTree::nCacheLineNodes,it->bbox,boxLeftL, boxRightL); 00574 subTreeBuild(nChildIndex++,boxRight); 00575 00576 } 00577 } 00578 } 00579 00580 00581 00582 00583 bool 00584 vhdABTTree::_isVisible(const vhdAxisAlignedBox & box) 00585 { 00586 //default callback -> all node are visile 00587 return true; 00588 } 00589 00590 00591 void 00592 vhdABTTree::traversal() 00593 { 00594 //we start from the root -> so _box 00595 _traversal(0,_bbox); 00596 } 00597 00598 void 00599 vhdABTTree::_traversal(uint32 nCurrentCacheLineIndex,const vhdAxisAlignedBox & box) 00600 { 00601 00602 #ifdef TREE_DEBUG 00603 std::cout << "_traversal " << nCurrentCacheLineIndex << std::endl; 00604 #endif 00605 //1) we need to try to traverse a complete sub-tree 00606 ListElement listElement; 00607 listElement.push_back(Element(0,box)); 00608 00609 while (listElement.size()>0) 00610 { 00611 //check if we are not at the last line of the sub-tree 00612 if ( listElement.begin()->id >= nSubTreeId) 00613 { 00614 //we reach the end line 00615 break; 00616 } 00617 00618 uint32 nCurrentPos = nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + listElement.begin()->id; 00619 00620 if ( (_pNode[nCurrentPos].node.data[BITLEAF] & MASK_ISLEAF) == MASK_ISLEAF) 00621 { 00622 //this is a leaf, 00623 //TO DO NOTIFY THAT WE NEED TO RENDER THIS LEAF 00624 uint32 index = _pNode[nCurrentPos].leaf.index >> MASK_ISLEAF; 00625 //we notify that this leaf is visible. 00626 #ifdef TREE_DEBUG 00627 std::cout << "_pNode[nCurrentPos] need to be render " << nCurrentPos << " and leaf is " << index << std::endl; 00628 #endif 00629 funcVisit(nCurrentPos, index,listElement.begin()->bbox); 00630 listElement.pop_front(); 00631 continue; 00632 } 00633 00634 vhdAxisAlignedBox boxLeft,boxRight; 00635 _computeChildBox(listElement.begin()->bbox,boxLeft,boxRight, nCurrentPos); 00636 00637 00638 //check if left need to be split 00639 if (funcIsVisible(boxLeft)) 00640 { 00641 //add the new leaf with the number 00642 listElement.push_back(Element(listElement.begin()->id*2 + 1,boxLeft)); 00643 } 00644 //right 00645 if (funcIsVisible(boxRight)) 00646 { 00647 //add the new leaf with the number 00648 listElement.push_back(Element(listElement.begin()->id*2 + 2,boxRight)); 00649 } 00650 listElement.pop_front(); 00651 } 00652 00653 //now we reach the end line of this sub-tree 00654 00655 //take the index to the first children 00656 uint32 nNextChildren = _pNode[(nCurrentCacheLineIndex+1) * vhdABTTree::nCacheLineNodes - 1].leaf.index; 00657 00658 uint32 i; 00659 ListElement::iterator it(listElement.begin()); 00660 for ( i = nSubTreeId ; i < vhdABTTree::nCacheLineNodes; ++i) 00661 { 00662 if (it == listElement.end()) 00663 { 00664 break; 00665 } 00666 uint32 nCurrentPos = nCurrentCacheLineIndex * vhdABTTree::nCacheLineNodes + it->id; //nNextChildren * vhdABTTree::nCacheLineNodes + it->id; 00667 if (nNextChildren==0) 00668 { 00669 //mean a leaf here 00670 //this is a leaf, so we render it 00671 //TO DO NOTIFY THAT WE NEED TO RENDER THIS LEAF 00672 uint32 index = _pNode[nCurrentPos].leaf.index >> MASK_ISLEAF; 00673 #ifdef TREE_DEBUG 00674 std::cout << "_pNode[nCurrentPos] need to be render " << nCurrentPos << " and leaf is " << index << std::endl; 00675 #endif 00676 funcVisit(nCurrentPos, index,it->bbox); 00677 //listElement.pop_front(); 00678 ++it; 00679 continue; 00680 } 00681 00682 00683 00684 00685 if ( it->id != i) 00686 { 00687 //this potential node is not existing, so skipt it 00688 continue; 00689 } 00690 if ( (_pNode[nCurrentPos].node.data[BITLEAF] & MASK_ISLEAF) == MASK_ISLEAF) 00691 { 00692 //this is a leaf, so we render it 00693 uint32 index = _pNode[nCurrentPos].leaf.index >> MASK_ISLEAF; 00694 #ifdef TREE_DEBUG 00695 std::cout << "_pNode[nCurrentPos] need to be render " << nCurrentPos << " and leaf is " << index << std::endl; 00696 #endif 00697 funcVisit(nCurrentPos, index,it->bbox); 00698 //listElement.pop_front(); 00699 ++it; 00700 continue; 00701 } 00702 //check the flags 00703 uint32 mask = 0x1 << (i-nSubTreeId); 00704 if ((_pNode[(nCurrentCacheLineIndex+1) * vhdABTTree::nCacheLineNodes -1 ].leaf.callback & mask) == mask) 00705 { 00706 //this node would have a two child 00707 nNextChildren+=2; //for left and right 00708 } 00709 00710 //we need to check for the children from here,so as we have already increment the index 00711 //recursive call. 00712 vhdAxisAlignedBox boxLeft,boxRight; 00713 _computeChildBox(it->bbox,boxLeft,boxRight, nCurrentPos); 00714 //left 00715 //check if left need to be split 00716 if (funcIsVisible(boxLeft)) 00717 { 00718 _traversal(nNextChildren-2, boxLeft); 00719 } 00720 //right 00721 if (funcIsVisible(boxRight)) 00722 { 00723 _traversal(nNextChildren-1,boxRight); 00724 } 00725 00726 00727 ++it; 00728 } 00729 00730 } 00731 00732 00733 00734 00735 00736 00737 00738 00739 00740 00741 00742 00743

Generated on Sun Oct 3 16:32:22 2004 for vhdABTTree by doxygen 1.3.8