Changeset 3266 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
- Timestamp:
- 01/11/09 12:56:56 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r3265 r3266 202 202 } 203 203 204 cout << "max depth for testing children" << mMaxDepthForTestingChildren << endl;204 //cout << "max depth for testing children " << mMaxDepthForTestingChildren << endl; 205 205 } 206 206 … … 229 229 230 230 mMaxDepthForTestingChildren = maxDepthForTestingChildren; 231 cout << "max depth for testing children" << mMaxDepthForTestingChildren << endl;231 //cout << "max depth for testing children " << mMaxDepthForTestingChildren << endl; 232 232 } 233 233 … … 956 956 int numVirtualLeaves = 0; 957 957 int numGeometry = 0; 958 958 cout<<"here3 computing stats"<<endl; 959 959 while (!nodeStack.empty()) 960 960 { … … 962 962 nodeStack.pop(); 963 963 964 cout << "x"; 965 if (node->IsLeaf() && !node->IsVirtualLeaf()) 966 cerr << "here34"<<endl; 967 964 968 if (node->IsVirtualLeaf()) 965 969 { … … 967 971 numGeometry += node->CountPrimitives(); 968 972 973 cout << " l " << numGeometry; 969 974 BvhLeaf *leaf = static_cast<BvhLeaf *>(node); 970 975 … … 975 980 else 976 981 { 982 cout << "i"; 983 977 984 bvhStats.mInteriorSA += node->mBox.SurfaceArea(); 978 985 bvhStats.mInteriorVol += node->mBox.GetVolume(); … … 991 998 992 999 993 void Bvh::PrintBvhStats(const BvhStats &bvhStats) const 994 { 995 cout << "\n============ BVH statistics =============" << endl; 996 cout << "interiorNodesSA = " << bvhStats.mInteriorSA / mRoot->mBox.SurfaceArea() << endl; 997 cout << "leafNodesSA = " << bvhStats.mLeafSA / mRoot->mBox.SurfaceArea() << endl; 998 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol / mRoot->mBox.GetVolume() << endl; 999 cout << "leafNodesVolume = " << bvhStats.mLeafVol / mRoot->mBox.GetVolume() << endl; 1000 void Bvh::PrintBvhStats(const BvhStats &bvhStats, BvhNode *root) const 1001 { 1002 cout << "interiorNodesSA = " << bvhStats.mInteriorSA / root->mBox.SurfaceArea() << endl; 1003 cout << "leafNodesSA = " << bvhStats.mLeafSA / root->mBox.SurfaceArea() << endl; 1004 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol / root->mBox.GetVolume() << endl; 1005 cout << "leafNodesVolume = " << bvhStats.mLeafVol / root->mBox.GetVolume() << endl; 1000 1006 1001 1007 cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; … … 1086 1092 nodeStack.push(mRoot); 1087 1093 1094 cout << "here9" << endl; 1088 1095 while (!nodeStack.empty()) 1089 1096 { 1097 cout <<"u"<< endl; 1090 1098 BvhNode *node = nodeStack.top(); 1091 1099 nodeStack.pop(); … … 1108 1116 (CountTriangles(node) <= numTriangles)) 1109 1117 { 1110 node->mIsVirtualLeaf = true; 1118 cout << " l " << CountTriangles(node) << " " << node->mIsMaxDepthForVirtualLeaf << endl; 1119 node->mIsVirtualLeaf = true; 1111 1120 } 1112 1121 else … … 1138 1147 nodeStack.push(mRoot); 1139 1148 1149 cout << "here7 computing depth" << endl; 1140 1150 while (!nodeStack.empty()) 1141 1151 { 1142 1152 BvhNode *node = nodeStack.top(); 1143 1153 nodeStack.pop(); 1144 1154 cout << "z"; 1145 1155 if (node->IsLeaf()) 1146 1156 { … … 1156 1166 if ((f->mFirst == b->mFirst) && (f->mLast == b->mLast)) 1157 1167 { 1168 cout << "f: " << f->mFirst << " " << f->mLast << endl; 1158 1169 // point reached where beyond there would be no further reduction 1159 1170 // as both subtrees contain the same objects => stop here … … 1169 1180 } 1170 1181 } 1182 cout << "here7" << endl; 1171 1183 } 1172 1184 … … 1191 1203 // compute and print stats for whole (static + dynamic) bvh 1192 1204 ComputeBvhStats(mRoot, mBvhStats); 1193 PrintBvhStats(mBvhStats); 1205 1206 BvhStats staticBvhStats; 1207 ComputeBvhStats(mRoot, staticBvhStats); 1208 cout << "\n============ Static BVH statistics =============" << endl; 1209 1210 PrintBvhStats(staticBvhStats, mStaticRoot); 1194 1211 1195 1212 // also output dynamic stats 1196 1213 if (mDynamicGeometrySize) 1197 1214 { 1198 BvhStats bvhStats;1199 ComputeBvhStats(mDynamicRoot, bvhStats);1200 1215 BvhStats dynBvhStats; 1216 ComputeBvhStats(mDynamicRoot, dynBvhStats); 1217 1201 1218 cout << "\n=========== Dynamic BVH statistics: =========" << endl; 1202 cout << "leaves = " << bvhStats.mLeaves << endl; 1203 cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl; 1204 cout << "leafNodesSA = " << bvhStats.mLeafSA << endl; 1205 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol << endl; 1206 cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl; 1207 1208 cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 1209 cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 1210 cout << "=============================================" << endl << endl; 1219 PrintBvhStats(dynBvhStats, mDynamicRoot); 1211 1220 } 1212 1221 } … … 1344 1353 1345 1354 1346 ////////////////////////1347 //-- functions for construction of the dynamic hierarchy1348 1349 int Bvh::SortTriangles(BvhLeaf *leaf,1350 int axis,1351 float position1352 )1353 {1354 int i = leaf->mFirst;1355 int j = leaf->mLast;1356 1357 while (1)1358 {1359 //while (mGeometry[i]->GetWorldCenter()[axis] < position) ++ i;1360 //while (position < mGeometry[j]->GetWorldCenter()[axis]) -- j;1361 1362 while ((i < leaf->mLast) && (mGeometry[i]->GetWorldCenter()[axis] < position)) ++ i;1363 while ((j > leaf->mFirst) && (position < mGeometry[j]->GetWorldCenter()[axis])) -- j;1364 1365 // sorting finished1366 if (i >= j) break;1367 1368 // swap entities1369 swap(mGeometry[i], mGeometry[j]);1370 1371 ++ i;1372 -- j;1373 }1374 1375 return j;1376 }1377 1378 1379 int Bvh::SortTrianglesSpatialMedian(BvhLeaf *leaf,1380 int axis1381 )1382 {1383 // spatial median1384 float m = leaf->mBox.Center()[axis];1385 return SortTriangles(leaf, axis, m);1386 }1387 1388 1389 BvhNode *Bvh::SubdivideLeaf(BvhLeaf *leaf,1390 int parentAxis1391 )1392 {1393 if (TerminationCriteriaMet(leaf))1394 {1395 if (leaf->CountPrimitives() == 0)1396 cout << "error: leaf constructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl;1397 1398 return leaf;1399 }1400 1401 //const int axis = (parentAxis + 1) % 3;1402 const int axis = leaf->mBox.MajorAxis();1403 1404 const int scale = 20;1405 1406 // position of the split in the partailly sorted array of triangles1407 // corresponding to this leaf1408 int split = -1;1409 float pos = -1.0f;1410 1411 // Spatial median subdivision1412 split = SortTrianglesSpatialMedian(leaf, axis);1413 pos = leaf->mBox.Center()[axis];1414 1415 //if ((split >= leaf->mLast) || (split < leaf->mFirst))1416 if (split == leaf->mLast)1417 {1418 // no split could be achieved => just halve number of objects1419 split = (leaf->mLast + leaf->mFirst) / 2;1420 cerr << "no reduction " << leaf->CountPrimitives() << " " << leaf->mFirst << " " << leaf->mLast << " " << split << endl;1421 }1422 1423 // create two more nodes1424 mNumNodes += 2;1425 BvhInterior *parent = new BvhInterior(leaf->GetParent());1426 parent->mFirst = leaf->mFirst;1427 parent->mLast = leaf->mLast;1428 1429 parent->mAxis = axis;1430 parent->mBox = leaf->mBox;1431 parent->mDepth = leaf->mDepth;1432 1433 BvhLeaf *front = new BvhLeaf(parent);1434 1435 parent->mBack = leaf;1436 parent->mFront = front;1437 1438 // now assign the triangles to the subnodes1439 front->mFirst = split + 1;1440 front->mLast = leaf->mLast;1441 front->mDepth = leaf->mDepth + 1;1442 1443 if (leaf->mFirst > leaf->mLast)1444 cerr << "erorr!!! " << leaf->CountPrimitives() << " " << leaf->mFirst << " " << leaf->mLast << " " << split << endl;1445 1446 leaf->mLast = split;1447 leaf->mDepth = front->mDepth;1448 leaf->mParent = parent;1449 1450 front->mBox = SceneEntity::ComputeBoundingBox(mGeometry + front->mFirst, front->CountPrimitives());1451 leaf->mBox = SceneEntity::ComputeBoundingBox(mGeometry + leaf->mFirst, leaf->CountPrimitives());1452 1453 // recursively continue subdivision1454 parent->mBack = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mBack), axis);1455 parent->mFront = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mFront), axis);1456 1457 return parent;1458 }1459 1460 1461 bool Bvh::TerminationCriteriaMet(BvhLeaf *leaf) const1462 {1463 const bool criteriaMet =1464 (leaf->mDepth > mMaxDepthForDynamicBranch) ||1465 (leaf->CountPrimitives() <= 1);1466 1467 return criteriaMet;1468 }1469 1470 1471 void Bvh::UpdateDynamicBranch(BvhNode *node)1472 {1473 if (node->IsLeaf())1474 {1475 int numEntities;1476 SceneEntity **entities = GetGeometry(node, numEntities);1477 1478 node->mBox = SceneEntity::ComputeBoundingBox(entities, numEntities);1479 //cout << "box: " << node->mBox << endl;1480 }1481 else1482 {1483 BvhNode *f = static_cast<BvhInterior *>(node)->GetFront();1484 BvhNode *b = static_cast<BvhInterior *>(node)->GetBack();1485 1486 UpdateDynamicBranch(f);1487 UpdateDynamicBranch(b);1488 1489 node->mBox = f->mBox;1490 node->mBox.Include(b->mBox);1491 }1492 }1493 1494 1495 void Bvh::CreateDynamicBranch()1496 {1497 // the bvh has two main branches1498 // a static branch (the old root), and a dynamic branch1499 // we create a 'dynamic' leaf which basically is a container1500 // for all dynamic objects underneath1501 1502 // the bounding boxes of the dynamic tree must be updated1503 // once each frame in order to be able to incorporate1504 // the movements of the objects within1505 1506 cout << "creating dynamic bvh branch" << endl;1507 1508 DEL_PTR(mDynamicRoot);1509 -- mNumNodes;1510 1511 #if 11512 BvhConstructor bvhConstructor(mGeometry, (int)mStaticGeometrySize, (int)mGeometrySize - 1);1513 1514 int numNodes;1515 mDynamicRoot = bvhConstructor.Construct(numNodes);1516 1517 mNumNodes += numNodes;1518 1519 #else1520 1521 BvhLeaf *l = new BvhLeaf(mRoot);1522 mDynamicRoot = l;1523 1524 l->mBox = SceneEntity::ComputeBoundingBox(mGeometry + mStaticGeometrySize,1525 (int)mDynamicGeometrySize);1526 1527 l->mFirst = (int)mStaticGeometrySize;1528 l->mLast = (int)mGeometrySize - 1;1529 l->mArea = l->mBox.SurfaceArea();1530 1531 if (mDynamicGeometrySize)1532 {1533 mDynamicRoot = SubdivideLeaf(l, 0);1534 }1535 #endif1536 }1537 1538 1355 1539 1356 bool Bvh::IntersectsNearPlane(BvhNode *node) const … … 1581 1398 1582 1399 1583 } 1400 1401 1402 //////////////////////// 1403 //-- dynamic hierarchy stuff 1404 1405 1406 void Bvh::UpdateDynamicBranch(BvhNode *node) 1407 { 1408 if (node->IsLeaf()) 1409 { 1410 int numEntities; 1411 SceneEntity **entities = GetGeometry(node, numEntities); 1412 1413 node->mBox = SceneEntity::ComputeBoundingBox(entities, numEntities); 1414 //cout << "box: " << node->mBox << endl; 1415 } 1416 else 1417 { 1418 BvhNode *f = static_cast<BvhInterior *>(node)->GetFront(); 1419 BvhNode *b = static_cast<BvhInterior *>(node)->GetBack(); 1420 1421 UpdateDynamicBranch(f); 1422 UpdateDynamicBranch(b); 1423 1424 node->mBox = f->mBox; 1425 node->mBox.Include(b->mBox); 1426 } 1427 } 1428 1429 1430 void Bvh::CreateDynamicBranch() 1431 { 1432 // the bvh has two main branches 1433 // a static branch (the old root), and a dynamic branch 1434 // we create a 'dynamic' leaf which basically is a container 1435 // for all dynamic objects underneath 1436 1437 // the bounding boxes of the dynamic tree must be updated 1438 // once each frame in order to be able to incorporate 1439 // the movements of the objects within 1440 1441 cout << "creating dynamic bvh branch" << endl; 1442 1443 DEL_PTR(mDynamicRoot); 1444 -- mNumNodes; 1445 1446 BvhConstructor bvhConstructor(mGeometry, (int)mStaticGeometrySize, (int)mGeometrySize - 1); 1447 1448 int numNodes; 1449 mDynamicRoot = bvhConstructor.Construct(numNodes); 1450 1451 mNumNodes += numNodes; 1452 } 1453 1454 }
Note: See TracChangeset
for help on using the changeset viewer.