Changeset 3070 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
- Timestamp:
- 10/26/08 18:29:27 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r3069 r3070 151 151 152 152 153 154 inline AxisAlignedBox3 ComputeBoundingBox(SceneEntity **entities, int numEntities) 155 { 156 AxisAlignedBox3 box = entities[0]->GetWorldBoundingBox(); 157 158 for (int i = 1; i < numEntities; ++ i) 159 { 160 box.Include(entities[i]->GetWorldBoundingBox()); 161 } 162 163 return box; 164 } 165 166 153 167 Bvh::Bvh() 154 168 { … … 419 433 if (!mDynamicEntities.empty()) 420 434 { 421 UpdateDynamicBranch(); 435 if (!mDynamicRoot) 436 CreateDynamicBranch(); 437 438 UpdateDynamicBranch(mDynamicRoot); 422 439 } 423 440 } … … 448 465 { 449 466 SceneEntity *ent = entities[i]; 450 float dist = ent->Get TransformedBoundingBox().GetMaxDistance(sNearPlane);467 float dist = ent->GetWorldBoundingBox().GetMaxDistance(sNearPlane); 451 468 452 469 if (dist > maxDist) maxDist = dist; … … 800 817 801 818 802 void Bvh::ComputeBvhStats() 803 { 819 void Bvh::ComputeBvhStats(BvhNode *root, BvhStats &bvhStats) 820 { 821 bvhStats.Reset(); 804 822 std::stack<BvhNode *> nodeStack; 805 nodeStack.push( mRoot);823 nodeStack.push(root); 806 824 807 825 int numVirtualLeaves = 0; 826 int numGeometry = 0; 808 827 809 828 while (!nodeStack.empty()) … … 815 834 { 816 835 ++ numVirtualLeaves; 836 numGeometry += node->CountPrimitives(); 817 837 818 838 BvhLeaf *leaf = static_cast<BvhLeaf *>(node); 819 839 820 mBvhStats.mTriangles += CountTriangles(leaf);821 mBvhStats.mLeafSA += leaf->mBox.SurfaceArea();822 mBvhStats.mLeafVol += leaf->mBox.GetVolume();840 bvhStats.mTriangles += CountTriangles(leaf); 841 bvhStats.mLeafSA += leaf->mBox.SurfaceArea(); 842 bvhStats.mLeafVol += leaf->mBox.GetVolume(); 823 843 } 824 844 else 825 845 { 826 mBvhStats.mInteriorSA += node->mBox.SurfaceArea();827 mBvhStats.mInteriorVol += node->mBox.GetVolume();846 bvhStats.mInteriorSA += node->mBox.SurfaceArea(); 847 bvhStats.mInteriorVol += node->mBox.GetVolume(); 828 848 829 849 BvhInterior *interior = static_cast<BvhInterior *>(node); … … 834 854 } 835 855 836 mBvhStats.mGeometryRatio = mGeometrySize / (float)numVirtualLeaves; 837 mBvhStats.mTriangleRatio = mBvhStats.mTriangles / (float)numVirtualLeaves; 838 } 839 840 841 void Bvh::PrintBvhStats() const 842 { 843 cout << "\n******** bvh stats: ***********" << endl; 844 cout << "interiorNodesSA = " << mBvhStats.mInteriorSA / mRoot->mBox.SurfaceArea() << endl; 845 cout << "leafNodesSA = " << mBvhStats.mLeafSA / mRoot->mBox.SurfaceArea() << endl; 846 cout << "interiorNodesVolume = " << mBvhStats.mInteriorVol / mRoot->mBox.GetVolume() << endl; 847 cout << "leafNodesVolume = " << mBvhStats.mLeafVol / mRoot->mBox.GetVolume() << endl; 848 849 cout << "geometry per leaf: " << mBvhStats.mGeometryRatio << endl; 850 cout << "triangles per leaf: " << mBvhStats.mTriangleRatio << endl; 851 cout << "**************" << endl << endl; 856 bvhStats.mGeometryRatio = (float)numGeometry / numVirtualLeaves; 857 bvhStats.mTriangleRatio = (float)bvhStats.mTriangles / numVirtualLeaves; 858 bvhStats.mLeaves = numVirtualLeaves; 859 } 860 861 862 void Bvh::PrintBvhStats(const BvhStats &bvhStats) const 863 { 864 cout << "\n============ BVH statistics =============" << endl; 865 cout << "interiorNodesSA = " << bvhStats.mInteriorSA / mRoot->mBox.SurfaceArea() << endl; 866 cout << "leafNodesSA = " << bvhStats.mLeafSA / mRoot->mBox.SurfaceArea() << endl; 867 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol / mRoot->mBox.GetVolume() << endl; 868 cout << "leafNodesVolume = " << bvhStats.mLeafVol / mRoot->mBox.GetVolume() << endl; 869 870 cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 871 cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 872 cout << "===========================================" << endl << endl; 852 873 } 853 874 … … 1155 1176 int Bvh::SortTriangles(BvhLeaf *leaf, 1156 1177 int axis, 1157 float position ,1158 SceneEntityContainer &entities)1178 float position 1179 ) 1159 1180 { 1160 1181 int i = leaf->mFirst; 1161 1182 int j = leaf->mLast; 1162 1183 1163 while (1) 1164 { 1165 while (entities[i]->GetCenter()[axis] < position) 1184 while (1) 1185 { 1186 while (mGeometry[i]->GetWorldCenter()[axis] < position) 1187 { 1188 //cout<<" i " << i << " " << mGeometry[i]->GetWorldCenter()[axis]; 1166 1189 ++ i; 1167 1168 while (position < entities[j]->GetCenter()[axis]) 1190 } 1191 1192 while (position < mGeometry[j]->GetWorldCenter()[axis]) 1193 { 1194 //cout<< axis << " j " << j << " " << mGeometry[j]->GetWorldCenter()[axis] << " " << position<< " "; 1169 1195 -- j; 1196 } 1170 1197 1171 1198 // sorting finished … … 1173 1200 1174 1201 // swap entities 1175 swap( entities[i], entities[j]);1202 swap(mGeometry[i], mGeometry[j]); 1176 1203 1177 1204 ++ i; … … 1184 1211 1185 1212 int Bvh::SortTrianglesSpatialMedian(BvhLeaf *leaf, 1186 int axis ,1187 SceneEntityContainer &entities)1213 int axis 1214 ) 1188 1215 { 1189 1216 // spatial median 1190 1217 float m = leaf->mBox.Center()[axis]; 1191 return SortTriangles(leaf, axis, m, entities); 1218 //cout << "here3 " << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl; 1219 return SortTriangles(leaf, axis, m); 1192 1220 } 1193 1221 1194 1222 1195 1223 BvhNode *Bvh::SubdivideLeaf(BvhLeaf *leaf, 1196 int parentAxis ,1197 SceneEntityContainer &entities)1224 int parentAxis 1225 ) 1198 1226 { 1199 1227 if (TerminationCriteriaMet(leaf)) 1200 1228 { 1201 1229 leaf->mIsVirtualLeaf = true; 1230 leaf->mIsMaxDepthForVirtualLeaf = true; 1202 1231 return leaf; 1203 1232 } 1204 1233 1205 //int axis = leaf->mBox.MajorAxis(); 1206 int axis = (parentAxis + 1) % 3; 1234 //const int axis = (parentAxis + 1) % 3; 1235 const int axis = leaf->mBox.MajorAxis(); 1236 1237 1238 const int scale = 20; 1207 1239 1208 1240 // position of the split in the partailly sorted array of triangles 1209 1241 // corresponding to this leaf 1210 1242 int split = -1; 1211 const int scale = 20;1212 1213 1243 float pos = -1.0f; 1214 1244 1215 1245 // Spatial median subdivision 1216 split = SortTrianglesSpatialMedian(leaf, axis , entities);1246 split = SortTrianglesSpatialMedian(leaf, axis); 1217 1247 pos = leaf->mBox.Center()[axis]; 1218 1248 … … 1247 1277 1248 1278 // reset box 1249 leaf->mBox.Initialize(); 1279 //leaf->mBox.Initialize(); 1280 1281 front->mBox = ComputeBoundingBox(mGeometry + front->mFirst, front->CountPrimitives()); 1282 leaf->mBox = ComputeBoundingBox(mGeometry + leaf->mFirst, leaf->CountPrimitives()); 1250 1283 1251 1284 // recursively continue subdivision 1252 parent->mBack = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mBack), axis , entities);1253 parent->mFront = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mFront), axis , entities);1285 parent->mBack = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mBack), axis); 1286 parent->mFront = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mFront), axis); 1254 1287 1255 1288 return parent; … … 1267 1300 1268 1301 1269 void Bvh::UpdateDynamicBranch() 1302 void Bvh::UpdateDynamicBranch(BvhNode *node) 1303 { 1304 if (node->IsLeaf()) 1305 { 1306 int numEntities; 1307 SceneEntity **entities = GetGeometry(node, numEntities); 1308 1309 node->mBox = ComputeBoundingBox(entities, numEntities); 1310 //cout << "box: " << node->mBox << endl; 1311 } 1312 else 1313 { 1314 BvhNode *f = static_cast<BvhInterior *>(node)->GetFront(); 1315 BvhNode *b = static_cast<BvhInterior *>(node)->GetBack(); 1316 1317 UpdateDynamicBranch(f); 1318 UpdateDynamicBranch(b); 1319 1320 node->mBox = f->mBox; 1321 node->mBox.Include(b->mBox); 1322 } 1323 } 1324 1325 1326 void Bvh::CreateDynamicBranch() 1270 1327 { 1271 1328 // the bvh has two main branches … … 1278 1335 // the movements of the objects within 1279 1336 1280 // delete old branch1281 cout << "deleting old branch" << endl;1282 1283 1337 DEL_PTR(mDynamicRoot); 1284 1338 1285 mDynamicRoot = new BvhLeaf(mRoot); 1286 mDynamicRoot->mBox = mRoot->mBox; 1287 1288 mDynamicRoot->mFirst = 0; 1289 mDynamicRoot->mLast = (int)mDynamicEntities.size() - 1; 1290 mDynamicRoot->mArea = mDynamicRoot->mBox.SurfaceArea(); 1291 1292 cout << "updating dynamic branch" << endl; 1293 1294 mDynamicRoot = SubdivideLeaf(static_cast<BvhLeaf *>(mDynamicRoot), 0, mDynamicEntities); 1295 1296 cout << "finished updating dynamic branch" << endl; 1297 } 1298 1299 1300 void Bvh::AddDynamicObject(SceneEntity *ent) 1301 { 1302 mDynamicEntities.push_back(ent); 1339 BvhLeaf *l = new BvhLeaf(mRoot); 1340 1341 l->mBox.Initialize(); 1342 1343 SceneEntityContainer::const_iterator sit, sit_end = mDynamicEntities.end(); 1344 1345 for (sit = mDynamicEntities.begin(); sit != sit_end; ++ sit) 1346 { 1347 l->mBox.Include((*sit)->GetWorldBoundingBox()); 1348 } 1349 1350 l->mFirst = (int)(mGeometrySize - mDynamicEntities.size()); 1351 l->mLast = (int)mGeometrySize - 1; 1352 l->mArea = l->mBox.SurfaceArea(); 1353 1354 cout << "updating dynamic branch " << l->mFirst << " " << l->mLast << endl; 1355 1356 mDynamicRoot = SubdivideLeaf(l, 0); 1357 1358 BvhStats bvhStats; 1359 ComputeBvhStats(mDynamicRoot, bvhStats); 1360 1361 cout << "\n=========== Dynamic BVH statistics: =========" << endl; 1362 cout << "leaves = " << bvhStats.mLeaves << endl; 1363 cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl; 1364 cout << "leafNodesSA = " << bvhStats.mLeafSA << endl; 1365 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol << endl; 1366 cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl; 1367 1368 cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 1369 cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 1370 cout << "=============================================" << endl << endl; 1371 } 1372 1373 1374 void Bvh::AddDynamicObjects(const SceneEntityContainer &entities) 1375 { 1376 // copy old entities 1377 SceneEntity **newGeom = new SceneEntity*[mGeometrySize + (int)entities.size()]; 1378 1379 memcpy(newGeom, mGeometry, mGeometrySize * sizeof(SceneEntity *)); 1380 1381 delete [] mGeometry; 1382 mGeometry = newGeom; 1383 1384 // now add new entities 1385 SceneEntityContainer::const_iterator it, it_end = entities.end(); 1386 1387 size_t i = mGeometrySize; 1388 for (it = entities.begin(); it != it_end; ++ it, ++ i) 1389 { 1390 mGeometry[i] = (*it); 1391 mDynamicEntities.push_back(*it); 1392 } 1393 1394 1395 mGeometrySize += entities.size(); 1303 1396 } 1304 1397
Note: See TracChangeset
for help on using the changeset viewer.