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

vhdSceneConverterABTTree.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 #include "vhdSceneConverterABTTree.h" 00025 #include "vhdABTTree.h" 00026 #include <float.h> 00027 #include <iostream> 00028 00029 //for singleton 00030 template <typename vhdSceneConverterABTTree> vhdSceneConverterABTTree* vhdSingleton <vhdSceneConverterABTTree>::ms_Singleton = 0; 00031 00032 const unsigned int vhdSceneConverterABTTree::ABTTREE_AXISX = 0; 00033 const unsigned int vhdSceneConverterABTTree::ABTTREE_AXISY = 1; 00034 const unsigned int vhdSceneConverterABTTree::ABTTREE_AXISZ = 2; 00035 const unsigned int vhdSceneConverterABTTree::ABTTREE_AXISINVALID = 3; 00036 00037 00038 00039 vhdSceneConverterABTTree::vhdSceneConverterABTTree() 00040 { 00041 00042 pDataRootNode = NULL; 00043 } 00044 00045 00046 vhdSceneConverterABTTree::~vhdSceneConverterABTTree() 00047 { 00048 } 00049 00050 00051 00052 00053 vhdAxisAlignedBox 00054 vhdSceneConverterABTTree::findGlobalAABB(const ListAABB & listAABB) 00055 { 00056 00057 ListAABB::const_iterator it; 00058 vhdVector3 vecMin, vecMax, vecGlobalMin(FLT_MAX,FLT_MAX,FLT_MAX), vecGlobalMax(-FLT_MAX,-FLT_MAX,-FLT_MAX); 00059 for (it=listAABB.begin(); it!=listAABB.end(); ++it) 00060 { 00061 vecMin = it->bbox.getMinimum(); 00062 vecMax = it->bbox.getMaximum(); 00063 if (vecMin.x<vecGlobalMin.x) vecGlobalMin.x=vecMin.x; 00064 if (vecMin.y<vecGlobalMin.y) vecGlobalMin.y=vecMin.y; 00065 if (vecMin.z<vecGlobalMin.z) vecGlobalMin.z=vecMin.z; 00066 if (vecMax.x>vecGlobalMax.x) vecGlobalMax.x=vecMax.x; 00067 if (vecMax.y>vecGlobalMax.y) vecGlobalMax.y=vecMax.y; 00068 if (vecMax.z>vecGlobalMax.z) vecGlobalMax.z=vecMax.z; 00069 } 00070 return vhdAxisAlignedBox(vecGlobalMin.x,vecGlobalMin.y,vecGlobalMin.z, vecGlobalMax.x,vecGlobalMax.y,vecGlobalMax.z); 00071 } 00072 00073 00074 #define TRIAL 20 00075 00076 float 00077 vhdSceneConverterABTTree::_findBestSplitLocation (vhdSceneABTTreeNode *pParentNode ,int axis, float& fSplitLoc,unsigned int uiNodeIndex) 00078 { 00079 //get the AABB 00080 vhdAxisAlignedBox aabb = findGlobalAABB(pParentNode->listAABB); 00081 unsigned int uiListSize = (unsigned int) pParentNode->listAABB.size(); 00082 00083 int i; 00084 float a; 00085 float fBestQual = -2.0; 00086 float fInvNumObjects = 1.0 / float (uiListSize); 00087 for (i = 0 ; i < TRIAL ; i++) 00088 { 00089 // Calculate a possible split location. 00090 a = aabb.getMinimum()[axis] + float (i+1)*(aabb.getMaximum()[axis]-aabb.getMinimum()[axis])/float (TRIAL+1.0); 00091 00092 00093 // Now count the number of poly that are on the left and the number of objects completely on the right 00094 // Normally the split function is much complex taking care of object complexity as well (rendering cost,....) 00095 00096 unsigned int uiLeft = 0; 00097 unsigned int uiRight = 0; 00098 00099 //we go through each instance to find the proper index, so here we go 00100 ListAABB::iterator it; 00101 vhdVector3 vecMin, vecMax; 00102 for (it=pParentNode->listAABB.begin(); it!=pParentNode->listAABB.end(); ++it) 00103 { 00104 vecMin = it->bbox.getMinimum(); 00105 vecMax = it->bbox.getMaximum(); 00106 00107 if ((vecMin[axis] + (vecMax[axis]-vecMin[axis]) / 2.0f) < a) 00108 { 00109 ++uiLeft; 00110 } 00111 else 00112 { 00113 ++uiRight; 00114 } 00115 } 00116 00117 // If we have no object on the left or right then this is a bad 00118 // split which we should never take. 00119 float fQual; 00120 if (uiLeft == 0 || uiRight == 0) 00121 { 00122 fQual = -1.0; 00123 } 00124 else 00125 { 00126 fQual = 1.0 - (float (abs ((int)(uiLeft-uiRight))) * fInvNumObjects); 00127 } 00128 if (fQual > fBestQual) 00129 { 00130 fBestQual = fQual; 00131 fSplitLoc = a; 00132 } 00133 } 00134 00135 return fBestQual; 00136 } 00137 00138 00139 00140 00141 void 00142 vhdSceneConverterABTTree::getChildren(vhdSceneABTTreeNode *pParentNode ,unsigned int nCurrentChildPosition,vhdAxisAlignedBox & boxLeft, vhdAxisAlignedBox & boxRight) 00143 { 00144 // Here we have 2 or more objects. 00145 // We use FindBestSplitLocation() to see how we can best split this 00146 // node. 00147 float split_loc_x, split_loc_y, split_loc_z; 00148 float qual_x = _findBestSplitLocation (pParentNode,vhdSceneConverterABTTree::ABTTREE_AXISX, split_loc_x,nCurrentChildPosition); 00149 float qual_y = _findBestSplitLocation (pParentNode,vhdSceneConverterABTTree::ABTTREE_AXISY, split_loc_y,nCurrentChildPosition); 00150 float qual_z = _findBestSplitLocation (pParentNode,vhdSceneConverterABTTree::ABTTREE_AXISZ, split_loc_z,nCurrentChildPosition); 00151 00152 float fSplitLocation; 00153 int iSplitAxis; 00154 if (qual_x >= 0 && qual_x >= qual_y && qual_x >= qual_z) 00155 { 00156 iSplitAxis = vhdSceneConverterABTTree::ABTTREE_AXISX; 00157 fSplitLocation = split_loc_x; 00158 } 00159 else if (qual_y >= 0 && qual_y >= qual_x && qual_y >= qual_z) 00160 { 00161 iSplitAxis = vhdSceneConverterABTTree::ABTTREE_AXISY; 00162 fSplitLocation = split_loc_y; 00163 } 00164 else 00165 { 00166 iSplitAxis = vhdSceneConverterABTTree::ABTTREE_AXISZ; 00167 fSplitLocation = split_loc_z; 00168 } 00169 00170 //we have our splitter so we do the real collection then 00171 00172 //we go through each instance to find the proper index, so here we go 00173 ListAABB::iterator it; 00174 vhdVector3 vecMin, vecMax; 00175 00176 00177 pParentNode->pLeft = new vhdSceneABTTreeNode(); 00178 pParentNode->pRight = new vhdSceneABTTreeNode(); 00179 00180 for (it=pParentNode->listAABB.begin(); it!=pParentNode->listAABB.end(); ++it) 00181 { 00182 vecMin = it->bbox.getMinimum(); 00183 vecMax = it->bbox.getMaximum(); 00184 00185 if ((vecMin[iSplitAxis] + (vecMax[iSplitAxis]-vecMin[iSplitAxis]) / 2.0f) < fSplitLocation) 00186 { 00187 pParentNode->pLeft->listAABB.push_back(*it); 00188 #ifdef TREE_DEBUG 00189 std::cout << "name left " << it->strPath << std::endl; 00190 #endif 00191 } 00192 else 00193 { 00194 pParentNode->pRight->listAABB.push_back(*it); 00195 #ifdef TREE_DEBUG 00196 std::cout << "name right " << it->strPath << std::endl; 00197 #endif 00198 } 00199 } 00200 00201 //avoid that sub-child become empty 00202 while (pParentNode->pLeft->listAABB.size()<2) 00203 { 00204 //we transfer one 00205 pParentNode->pLeft->listAABB.push_back(*pParentNode->pRight->listAABB.begin()); 00206 pParentNode->pRight->listAABB.pop_front(); 00207 } 00208 while (pParentNode->pRight->listAABB.size()<2) 00209 { 00210 //we transfer one 00211 pParentNode->pRight->listAABB.push_back(*pParentNode->pLeft->listAABB.begin()); 00212 pParentNode->pLeft->listAABB.pop_front(); 00213 } 00214 00215 //we insert the current status in our map 00216 vhdSceneConverterABTTree::getSingleton().mapNodeChild.insert(vhdSceneConverterABTTree::MapNodeChild::value_type(nCurrentChildPosition,*pParentNode)); 00217 00218 //compute bbox 00219 boxLeft = findGlobalAABB(pParentNode->pLeft->listAABB); 00220 boxRight = findGlobalAABB(pParentNode->pRight->listAABB); 00221 } 00222 00223 00224 00225 00226

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