Ignore:
Timestamp:
01/11/09 12:56:56 (15 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp

    r3265 r3266  
    202202        } 
    203203 
    204         cout << "max depth for testing children" << mMaxDepthForTestingChildren << endl; 
     204        //cout << "max depth for testing children " << mMaxDepthForTestingChildren << endl; 
    205205} 
    206206 
     
    229229 
    230230        mMaxDepthForTestingChildren = maxDepthForTestingChildren; 
    231         cout << "max depth for testing children" << mMaxDepthForTestingChildren << endl; 
     231        //cout << "max depth for testing children " << mMaxDepthForTestingChildren << endl; 
    232232} 
    233233 
     
    956956        int numVirtualLeaves = 0; 
    957957        int numGeometry = 0; 
    958  
     958cout<<"here3 computing stats"<<endl; 
    959959        while (!nodeStack.empty())  
    960960        { 
     
    962962                nodeStack.pop(); 
    963963 
     964                cout << "x"; 
     965                if (node->IsLeaf() && !node->IsVirtualLeaf()) 
     966                        cerr << "here34"<<endl; 
     967 
    964968                if (node->IsVirtualLeaf())  
    965969                { 
     
    967971                        numGeometry += node->CountPrimitives(); 
    968972 
     973                        cout << " l " << numGeometry; 
    969974                        BvhLeaf *leaf = static_cast<BvhLeaf *>(node); 
    970975 
     
    975980                else  
    976981                { 
     982                        cout << "i"; 
     983 
    977984                        bvhStats.mInteriorSA += node->mBox.SurfaceArea(); 
    978985                        bvhStats.mInteriorVol += node->mBox.GetVolume(); 
     
    991998 
    992999 
    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; 
     1000void 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; 
    10001006 
    10011007        cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 
     
    10861092        nodeStack.push(mRoot); 
    10871093 
     1094        cout << "here9" << endl; 
    10881095        while (!nodeStack.empty())  
    10891096        { 
     1097                cout <<"u"<< endl; 
    10901098                BvhNode *node = nodeStack.top(); 
    10911099                nodeStack.pop(); 
     
    11081116                                (CountTriangles(node) <= numTriangles)) 
    11091117                        { 
    1110                                  node->mIsVirtualLeaf = true; 
     1118                                cout << " l " << CountTriangles(node) << " " << node->mIsMaxDepthForVirtualLeaf << endl; 
     1119                                node->mIsVirtualLeaf = true; 
    11111120                        } 
    11121121                        else 
     
    11381147        nodeStack.push(mRoot); 
    11391148 
     1149        cout << "here7 computing depth" << endl; 
    11401150        while (!nodeStack.empty())  
    11411151        { 
    11421152                BvhNode *node = nodeStack.top(); 
    11431153                nodeStack.pop(); 
    1144  
     1154cout << "z"; 
    11451155                if (node->IsLeaf())  
    11461156                { 
     
    11561166                        if ((f->mFirst == b->mFirst) && (f->mLast == b->mLast)) 
    11571167                        { 
     1168                                cout << "f: " << f->mFirst << " " << f->mLast << endl; 
    11581169                                // point reached where beyond there would be no further reduction  
    11591170                                // as both subtrees contain the same objects => stop here 
     
    11691180                } 
    11701181        } 
     1182        cout << "here7" << endl; 
    11711183} 
    11721184 
     
    11911203        // compute and print stats for whole (static + dynamic) bvh 
    11921204        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); 
    11941211 
    11951212        // also output dynamic stats 
    11961213        if (mDynamicGeometrySize) 
    11971214        { 
    1198                 BvhStats bvhStats; 
    1199                 ComputeBvhStats(mDynamicRoot, bvhStats); 
    1200  
     1215                BvhStats dynBvhStats; 
     1216                ComputeBvhStats(mDynamicRoot, dynBvhStats); 
     1217         
    12011218                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); 
    12111220        } 
    12121221} 
     
    13441353 
    13451354 
    1346 //////////////////////// 
    1347 //-- functions for construction of the dynamic hierarchy 
    1348  
    1349 int Bvh::SortTriangles(BvhLeaf *leaf,  
    1350                                            int axis,  
    1351                                            float position 
    1352                                            ) 
    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 finished 
    1366                 if (i >= j) break; 
    1367  
    1368                 // swap entities 
    1369                 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 axis 
    1381                                                                         ) 
    1382 { 
    1383         // spatial median 
    1384         float m = leaf->mBox.Center()[axis]; 
    1385         return SortTriangles(leaf, axis, m); 
    1386 } 
    1387  
    1388  
    1389 BvhNode *Bvh::SubdivideLeaf(BvhLeaf *leaf,  
    1390                                                         int parentAxis 
    1391                                                         ) 
    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 triangles 
    1407         // corresponding to this leaf 
    1408         int split = -1; 
    1409         float pos = -1.0f; 
    1410          
    1411         // Spatial median subdivision 
    1412         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 objects 
    1419                 split = (leaf->mLast + leaf->mFirst) / 2; 
    1420                 cerr << "no reduction " << leaf->CountPrimitives() << " " << leaf->mFirst << " " << leaf->mLast << " " << split << endl; 
    1421         } 
    1422  
    1423         // create two more nodes 
    1424         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 subnodes 
    1439         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 subdivision 
    1454         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) const 
    1462 { 
    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         else 
    1482         { 
    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 branches 
    1498         // a static branch (the old root), and a dynamic branch  
    1499         // we create a 'dynamic' leaf which basically is a container 
    1500         // for all dynamic objects underneath 
    1501  
    1502         // the bounding boxes of the dynamic tree must be updated 
    1503         // once each frame in order to be able to incorporate 
    1504         // the movements of the objects within 
    1505  
    1506         cout << "creating dynamic bvh branch"  << endl; 
    1507  
    1508         DEL_PTR(mDynamicRoot); 
    1509         -- mNumNodes; 
    1510  
    1511 #if 1 
    1512         BvhConstructor bvhConstructor(mGeometry, (int)mStaticGeometrySize, (int)mGeometrySize - 1); 
    1513  
    1514         int numNodes; 
    1515         mDynamicRoot = bvhConstructor.Construct(numNodes); 
    1516  
    1517         mNumNodes += numNodes; 
    1518  
    1519 #else 
    1520  
    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 #endif 
    1536 } 
    1537  
    15381355 
    15391356bool Bvh::IntersectsNearPlane(BvhNode *node) const 
     
    15811398   
    15821399 
    1583 } 
     1400 
     1401 
     1402//////////////////////// 
     1403//-- dynamic hierarchy stuff 
     1404 
     1405 
     1406void 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 
     1430void 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} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h

    r3262 r3266  
    2929{ 
    3030        friend class Bvh; 
    31         friend class BvhLoader; 
     31        friend class BvhFactory; 
    3232        friend class BvhExporter; 
    3333        friend class BvhConstructor; 
     
    251251{ 
    252252        friend class Bvh; 
    253         friend class BvhLoader; 
     253        friend class BvhFactory; 
    254254        friend class BvhExporter; 
    255255        friend class BvhConstructor; 
     
    282282{ 
    283283        friend class Bvh; 
    284         friend class BvhLoader; 
     284        friend class BvhFactory; 
    285285 
    286286public: 
     
    314314class Bvh 
    315315{ 
    316         friend class BvhLoader; 
     316        friend class BvhFactory; 
    317317        friend class BvhConstructor; 
    318318        friend class BvhExporter; 
     
    530530        /** Output the bvh statistics. 
    531531        */ 
    532         void PrintBvhStats(const BvhStats &bvhStats) const; 
     532        void PrintBvhStats(const BvhStats &bvhStats, BvhNode *root) const; 
    533533 
    534534 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp

    r3244 r3266  
     1#include "BvhLoader.h" 
     2#include "BvhConstructor.h" 
     3#include "gzstream.h" 
     4 
    15#include <queue> 
    26#include <stack> 
     
    48#include <iostream> 
    59 
    6 #include "BvhLoader.h" 
    7 #include "gzstream.h" 
    810 
    911 
     
    3032 
    3133 
    32 BvhNode *BvhLoader::LoadNextNode(igzstream &stream, BvhInterior *parent) 
     34BvhNode *BvhFactory::LoadNextNode(igzstream &stream, BvhInterior *parent) 
    3335{ 
    3436        int nodeType; 
     
    6264 
    6365 
    64 Bvh *BvhLoader::Load(const string &filename,   
    65                                          const SceneEntityContainer &staticEntities, 
    66                                          const SceneEntityContainer &dynamicEntities, 
    67                                          int maxDepthForTestingChildren) 
     66Bvh *BvhFactory::Create(const string &filename,  
     67                                                const SceneEntityContainer &staticEntities, 
     68                                                const SceneEntityContainer &dynamicEntities, 
     69                                                int maxDepthForTestingChildren) 
     70 
    6871{ 
    6972        queue<BvhNode *> tQueue; 
     
    7275        if (!stream.is_open()) return NULL; 
    7376 
    74         cout << "\n*******************\nloading bvh ..." << endl; 
     77        cout << "\n*******************\nloading bvh from file " << filename << " ..." << endl; 
    7578 
    7679        Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren); 
     
    130133 
    131134 
     135Bvh *BvhFactory::Create(const SceneEntityContainer &staticEntities, 
     136                                                const SceneEntityContainer &dynamicEntities, 
     137                                                int maxDepthForTestingChildren) 
     138{ 
     139 
     140        cout << "\n******************* bvh *******************" << endl; 
     141 
     142        Bvh *bvh = new Bvh(staticEntities, dynamicEntities, maxDepthForTestingChildren); 
     143 
     144        BvhConstructor bvhConstructor(bvh->mGeometry,  
     145                                          0, 
     146                                                                  (int)bvh->mStaticGeometrySize - 1); 
     147 
     148        int numNodes; 
     149        bvh->mStaticRoot = bvhConstructor.Construct(numNodes); 
     150 
     151        bvh->mNumNodes = 2 + numNodes; 
     152 
     153        cout << "created " << bvh->mNumNodes - 2 << " static bvh nodes" << endl; 
     154 
     155 
     156        /////////// 
     157        //-- create dynamic part of the hierarchy 
     158 
     159        bvh->CreateDynamicBranch(); 
     160 
     161 
     162        /////////// 
     163        //-- post process nodes 
     164 
     165        bvh->PostProcess(); 
     166 
     167        cout << "bvh bb: " << bvh->mBox << endl; 
     168 
     169        return bvh; 
    132170} 
     171 
     172 
     173} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h

    r3123 r3266  
    1212{ 
    1313 
    14 /** Loader for a bounding volume hierarchy. 
     14/** Loads or creates a bounding volume hierarchy. 
    1515*/ 
    16 class BvhLoader 
     16class BvhFactory 
    1717{ 
    1818public: 
    19         /** Creates and loads bvh with static and dynamic objects. 
    20                 The static part of the hierarchy is loaded from the disc, 
    21                 the dynamic part is created and changed on the fly. 
     19        /** Creates a bvh. 
     20                the static part of the hierarchy is loaded from disc. 
     21                the dynamic part of the hierarchy is created from scratch. 
    2222        */ 
    23         Bvh *Load(const std::string &filename,  
    24                           const SceneEntityContainer &staticEntities, 
    25                           const SceneEntityContainer &dynamicEntities, 
    26                           int maxDepthForTestingChildren); 
     23        Bvh *Create(const std::string &staticBvhFilename,  
     24                        const SceneEntityContainer &staticEntities, 
     25                                const SceneEntityContainer &dynamicEntities, 
     26                                int maxDepthForTestingChildren); 
     27 
     28        /** Creates a bvh from scratch. 
     29        */ 
     30        Bvh *Create(  
     31                        const SceneEntityContainer &staticEntities, 
     32                                const SceneEntityContainer &dynamicEntities, 
     33                                int maxDepthForTestingChildren); 
    2734 
    2835protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3261 r3266  
    644644        const string bvh_filename = string(model_path + filename + ".bvh"); 
    645645 
    646         BvhLoader bvhLoader; 
    647         bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects, maxDepthForTestingChildren); 
     646        BvhFactory bvhFactory; 
     647        //bvh = bvhFactory.Create(bvh_filename, sceneEntities, dynamicObjects, maxDepthForTestingChildren); 
     648        bvh = bvhFactory.Create(sceneEntities, dynamicObjects, maxDepthForTestingChildren); 
    648649 
    649650        if (!bvh) 
Note: See TracChangeset for help on using the changeset viewer.