Ignore:
Timestamp:
09/13/06 17:15:26 (18 years ago)
Author:
mattausch
Message:

implemented the global version of the bounding volume construction

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp

    r1345 r1357  
    2424#define USE_FIXEDPOINT_T 0 
    2525 
    26 static float debugVol = 0; 
    27  
    28 int BvhNode::sMailId = 10000;//2147483647; 
     26int BvhNode::sMailId = 10000; //2147483647; 
    2927int BvhNode::sReservedMailboxes = 1; 
    3028 
     
    3230 
    3331 
     32/// sorting operator 
    3433inline static bool ilt(Intersectable *obj1, Intersectable *obj2) 
    3534{ 
     
    171170{ 
    172171        ReadEnvironment(); 
    173         mSubdivisionCandidates = new vector<SortableEntry>; 
     172        mSubdivisionCandidates = new SortableEntryContainer; 
    174173} 
    175174 
     
    228227                "BvHierarchy.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight); 
    229228         
    230  
     229        Environment::GetSingleton()->GetBoolValue("BvHierarchy.useGlobalSort", mUseGlobalSorting); 
     230 
     231 
     232        /////////////////// 
    231233        //-- debug output 
    232  
    233234        Debug << "******* Bvh hierarchy options ******** " << endl; 
    234235    Debug << "max depth: " << mTermMaxDepth << endl; 
     
    245246        Debug << "use cost heuristics: " << mUseCostHeuristics << endl; 
    246247        Debug << "subdivision stats log: " << subdivisionStatsLog << endl; 
    247          
    248248        Debug << "split borders: " << mSplitBorder << endl; 
    249249        Debug << "render cost decrease weight: " << mRenderCostDecreaseWeight << endl; 
     250        Debug << "use global sort: " << mUseGlobalSorting << endl; 
    250251        Debug << endl; 
    251252} 
    252253 
    253254 
    254 void AssociateObjectsWithLeaf(BvhLeaf *leaf) 
     255static void AssociateObjectsWithLeaf(BvhLeaf *leaf) 
    255256{ 
    256257        ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end(); 
     
    274275         
    275276 
    276         //-- the front and back traversal data is filled with the new values 
     277        /////////////////////////////////////////////////////////////////// 
     278        //-- fill  front and back traversal data with the new values 
    277279 
    278280        frontData.mDepth = backData.mDepth = tData.mDepth + 1; 
     
    328330        frontData.mMaxCostMisses = sc.mMaxCostMisses; 
    329331        backData.mMaxCostMisses = sc.mMaxCostMisses; 
    330                  
    331         // assign positions of the iterators 
    332 #if 0 
    333         frontData.mObjectsStart = sc.mFrontObjectsStart; 
    334         frontData.mObjectsEnd = sc.mFrontObjectsEnd; 
    335          
    336         backData.mObjectsStart = sc.mBackObjectsStart; 
    337         backData.mObjectsEnd = sc.mBackObjectsEnd; 
    338 #endif 
     332         
     333        // assign the objects in sorted order 
     334        if (mUseGlobalSorting) 
     335        { 
     336                AssignSortedObjects(sc, frontData, backData); 
     337        } 
     338         
    339339        // return the new interior node 
    340340        return node; 
     
    388388                tBackData.mNode->SetSubdivisionCandidate(backCandidate); 
    389389 
    390                 Debug << "leaf: " << tFrontData.mNode << " setting f candidate: " << tFrontData.mNode->GetSubdivisionCandidate() << " type: " << tFrontData.mNode->GetSubdivisionCandidate()->Type() << endl; 
    391                 Debug << "leaf: " << tBackData.mNode << " setting b candidate: " << tBackData.mNode->GetSubdivisionCandidate() << " type: " << tBackData.mNode->GetSubdivisionCandidate()->Type() << endl; 
     390                Debug << "leaf: " << tFrontData.mNode << " setting f candidate: "  
     391                          << tFrontData.mNode->GetSubdivisionCandidate() << " type: "  
     392                          << tFrontData.mNode->GetSubdivisionCandidate()->Type() << endl; 
     393 
     394                Debug << "leaf: " << tBackData.mNode << " setting b candidate: "  
     395                          << tBackData.mNode->GetSubdivisionCandidate() << " type: "  
     396                          << tBackData.mNode->GetSubdivisionCandidate()->Type() << endl; 
    392397                 
    393398                tQueue.Push(frontCandidate); 
     
    443448        const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
    444449        const float oldProp = EvalViewCellsVolume(leaf->mObjects); 
    445         //const float oldProp2 = splitCandidate.mParentData.mProbability; Debug << "here8 " << (oldProp - oldProp2) / viewSpaceVol << "  " << oldProp  / viewSpaceVol << " " << oldProp2  / viewSpaceVol << endl; 
    446450 
    447451        const float oldRenderCost = oldProp * (float)leaf->mObjects.size() / viewSpaceVol; 
     
    568572                                                                                        ObjectContainer &objectsBack) 
    569573{ 
    570         SortSubdivisionCandidates(tData.mNode->mObjects, &mSubdivisionCandidates, axis); 
    571          
    572         vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
     574        PrepareLocalSubdivisionCandidates(tData, axis); 
     575         
     576        SortableEntryContainer::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
    573577 
    574578        int i = 0; 
     
    603607                                                   ObjectContainer &objectsBack) 
    604608{ 
    605         SortSubdivisionCandidates(tData.mNode->mObjects, &mSubdivisionCandidates, axis); 
     609        PrepareLocalSubdivisionCandidates(tData, axis); 
    606610   
    607611        // go through the lists, count the number of objects left and right 
     
    624628        float maxBorder = minBox; 
    625629 
    626         vector<SortableEntry>::const_iterator currentPos = 
     630        SortableEntryContainer::const_iterator currentPos = 
    627631                mSubdivisionCandidates->begin(); 
    628632 
    629         vector<SortableEntry>::reverse_iterator rcit =  
     633        SortableEntryContainer::reverse_iterator rcit =  
    630634                mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend(); 
    631635                 
     
    645649        vector<float>::const_iterator bit = bordersRight.begin(); 
    646650 
    647         vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
     651        SortableEntryContainer::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
    648652        for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ bit)  
    649653        { 
     
    750754                                                                                   ObjectContainer &objectsBack) 
    751755{ 
     756        //////////////////////////////////////////////////////////////// 
     757        // go through the lists, count the number of objects left and right 
     758        // and evaluate the cost funcion 
     759 
    752760        // prepare the heuristics by setting mailboxes and counters. 
    753761        const float totalVol = PrepareHeuristics(tData, axis); 
    754  
    755         // go through the lists, count the number of objects left and right 
    756         // and evaluate the cost funcion 
    757  
     762         
    758763        // local helper variables 
    759764        float volLeft = 0; 
    760765        float volRight = totalVol; 
    761  
    762766        int nObjectsLeft = 0; 
    763  
    764767        const int nTotalObjects = (int)tData.mNode->mObjects.size(); 
    765768        const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
    766769 
    767         vector<SortableEntry>::const_iterator currentPos = 
    768                 mSubdivisionCandidates->begin(); 
     770        SortableEntryContainer::const_iterator backObjectsStart = mSubdivisionCandidates->begin(); 
    769771 
    770772        ///////////////////////////////// 
    771         // the parameters for the current optimum 
     773        //-- the parameters for the current optimum 
    772774 
    773775        float volBack = volLeft; 
     
    782784        const bool printStats = 
    783785                PrepareOutput(axis, mBvhStats.Leaves(), sumStats, vollStats, volrStats); 
    784          
    785786#endif 
    786787 
    787         ///////////////////////////// 
    788         // the sweep heuristics 
    789          
     788        /////////////////////////////////////////////////// 
     789        //-- the sweep heuristics 
    790790        //-- traverse through events and find best split plane 
    791791 
    792         vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
     792        SortableEntryContainer::const_iterator cit, cit_end = cit_end = mSubdivisionCandidates->end(); 
    793793 
    794794        for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit) 
     
    802802 
    803803                ++ nObjectsLeft; 
    804                  
    805804                const int nObjectsRight = nTotalObjects - nObjectsLeft; 
    806805 
     
    811810#ifdef _DEBUG 
    812811                if (printStats) 
     812                { 
    813813                        PrintHeuristics(nObjectsRight, sum, volLeft, volRight, viewSpaceVol, 
    814814                                                        sumStats, vollStats, volrStats); 
     815                } 
    815816#endif 
    816817 
     
    823824 
    824825                        // objects belongs to left side now 
    825                         for (; currentPos != (cit + 1); ++ currentPos); 
     826                        for (; backObjectsStart != (cit + 1); ++ backObjectsStart); 
    826827                } 
    827828        } 
    828829 
     830        //////////////////////////////////////////// 
    829831        //-- assign object to front and back volume 
    830832 
    831833        // belongs to back bv 
    832         for (cit = mSubdivisionCandidates->begin(); cit != currentPos; ++ cit) 
     834        for (cit = mSubdivisionCandidates->begin(); cit != backObjectsStart; ++ cit) 
     835        { 
    833836                objectsBack.push_back((*cit).mObject); 
    834          
     837        } 
    835838        // belongs to front bv 
    836         for (cit = currentPos; cit != cit_end; ++ cit) 
     839        for (cit = backObjectsStart; cit != cit_end; ++ cit) 
     840        { 
    837841                objectsFront.push_back((*cit).mObject); 
    838  
     842        } 
     843 
     844        // render cost of the old parent 
    839845        const float oldRenderCost = (float)nTotalObjects * totalVol + Limits::Small; 
    840846        // the relative cost ratio 
     
    853859 
    854860 
    855 void BvHierarchy::SortSubdivisionCandidates(const ObjectContainer &objects, 
    856                                                                                         vector<SortableEntry> **subdivisionCandidates, 
    857                                                                                         const int axis)                                                                                  
     861void BvHierarchy::PrepareLocalSubdivisionCandidates(const BvhTraversalData &tData, 
     862                                                                                                        const int axis)                                                                                  
     863{ 
     864        //-- insert object queries 
     865        ObjectContainer *objects; 
     866 
     867        if (!mUseGlobalSorting) 
     868                objects = &tData.mNode->mObjects; 
     869        else 
     870                objects = tData.mSortedObjects[axis]; 
     871         
     872        CreateLocalSubdivisionCandidates(*objects, &mSubdivisionCandidates, mUseGlobalSorting, axis); 
     873} 
     874 
     875 
     876void BvHierarchy::CreateLocalSubdivisionCandidates(const ObjectContainer &objects,  
     877                                                                                                  SortableEntryContainer **subdivisionCandidates,  
     878                                                                                                  const bool sort, 
     879                                                                                                  const int axis) 
    858880{ 
    859881        (*subdivisionCandidates)->clear(); 
    860882 
    861         // compute requested size 
     883        // compute requested size and look if subdivision candidate has to be recomputed 
    862884        const int requestedSize = (int)objects.size() * 2; 
    863885         
     
    866888                requestedSize < (int)((*subdivisionCandidates)->capacity() / 10) ) 
    867889        { 
    868         delete *subdivisionCandidates; 
    869                 *subdivisionCandidates = new vector<SortableEntry>; 
     890        delete (*subdivisionCandidates); 
     891                (*subdivisionCandidates) = new SortableEntryContainer; 
    870892        } 
    871893 
    872894        (*subdivisionCandidates)->reserve(requestedSize); 
    873895 
    874         //-- insert object queries 
    875         //-- These queries can induce a change in pvs size. 
    876         //-- Note that they cannot induce a change in view cell volume, as 
    877         //-- there is no ray associated with these events!! 
    878  
    879896        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
    880897 
    881898        for (oit = objects.begin(); oit < oit_end; ++ oit) 
    882899        { 
    883                 Intersectable *obj = *oit; 
    884                  
    885900                Intersectable *object = *oit; 
    886901                const AxisAlignedBox3 &box = object->GetBox(); 
     
    890905        } 
    891906 
    892         stable_sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end()); 
     907        if (sort) 
     908        { 
     909                stable_sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end()); 
     910        } 
    893911} 
    894912 
     
    905923        float vol = 0; 
    906924 
    907         // sort so we can use a sweep from right to left 
    908         SortSubdivisionCandidates(tData.mNode->mObjects, &mSubdivisionCandidates, axis); 
    909  
     925    // sort so we can use a sweep from right to left 
     926        PrepareLocalSubdivisionCandidates(tData, axis); 
     927         
    910928        // collect and mark the view cells as belonging to front pvs 
    911929        ViewCellContainer viewCells; 
     
    939957        CollectViewCells(obj, viewCells, useMailboxing); 
    940958 
    941         /// classify view cells and compute volume contri accordingly 
    942         /// possible view cell classifications: 
    943         /// view cell mailed => view cell can be seen from left child node 
    944         /// view cell counter > 0 view cell can be seen from right child node 
    945         /// combined: view cell volume belongs to both nodes 
     959        // classify view cells and compute volume contri accordingly 
     960        // possible view cell classifications: 
     961        // view cell mailed => view cell can be seen from left child node 
     962        // view cell counter > 0 view cell can be seen from right child node 
     963        // combined: view cell volume belongs to both nodes 
    946964        ViewCellContainer::const_iterator vit, vit_end = viewCells.end(); 
    947965         
     
    956974                { 
    957975                        viewCell->Mail(); 
    958  
    959976                        // we now see view cell from both nodes  
    960977                        // => add volume to left node 
     
    964981                // last reference into the right node 
    965982                if (-- viewCell->mCounter == 0) 
    966                 { 
     983                {        
    967984                        // view cell was previously seen from both nodes  => 
    968985                        // remove volume from right node 
     
    10051022        } 
    10061023         
    1007         // -- evaluate split cost for all three axis 
     1024        //////////////////////////////////////////// 
     1025        //-- evaluate split cost for all three axis 
    10081026         
    10091027        for (int axis = 0; axis < 3; ++ axis) 
     
    10421060        } 
    10431061 
     1062        ///////////////////////////////////// 
    10441063        //-- assign values 
    10451064 
     
    11291148        //-- pvs rendering heuristics 
    11301149        const float newRenderCost = nObjectsFront * pFront + nObjectsBack * pBack; 
    1131         /* 
     1150 
     1151#ifdef _DEBUG 
    11321152        const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
    11331153        Debug << "\nbvh render cost\n"  
    11341154                  << "back p: " << pBack / viewSpaceVol << " front p " << pFront / viewSpaceVol << endl 
    11351155                  << "new rc: " << newRenderCost / viewSpaceVol << endl;*/ 
     1156#endif 
    11361157 
    11371158        return newRenderCost; 
     
    14481469} 
    14491470 
     1471 
    14501472void BvHierarchy::CreateRoot(const ObjectContainer &objects) 
    14511473{ 
     
    14541476        BvhLeaf *bvhleaf = new BvhLeaf(box, NULL, (int)objects.size()); 
    14551477        bvhleaf->mObjects = objects; 
    1456  
    14571478        mRoot = bvhleaf; 
    14581479 
     
    14651486                                                                                                           const ObjectContainer &objects) 
    14661487{ 
     1488        /////////////////////////////////////////////////////////////// 
     1489        // note matt: we assume that we have objects sorted by their id 
     1490 
    14671491        mBvhStats.Reset(); 
    14681492        mBvhStats.Start(); 
    14691493        mBvhStats.nodes = 1; 
    1470  
    1471         for (int i = 0; i < 3; ++ i) 
    1472         { 
    1473                 mGlobalSubdivisionCandidates[i] = new vector<SortableEntry>(); 
    1474                 SortSubdivisionCandidates(objects, &mGlobalSubdivisionCandidates[i], i); 
    1475         } 
    1476  
    1477         // note matt: we assume that we have objects sorted by their id 
    14781494 
    14791495        // store pointer to this tree 
     
    15021518        BvhTraversalData oData(bvhleaf, 0, mBoundingBox, prop); 
    15031519 
     1520        // create sorted object lists for the first data 
     1521        if (mUseGlobalSorting) 
     1522        { 
     1523                CreateInitialSortedObjectList(oData); 
     1524        } 
     1525         
     1526 
     1527        //////////////////////////////////////////////////// 
    15041528        //-- add first candidate for object space partition      
     1529 
    15051530        BvhSubdivisionCandidate *oSubdivisionCandidate =  
    15061531                new BvhSubdivisionCandidate(oData); 
     
    15301555 
    15311556 
     1557void BvHierarchy::CreateInitialSortedObjectList(BvhTraversalData &tData) 
     1558{ 
     1559        SortableEntryContainer *sortedObjects = new SortableEntryContainer(); 
     1560 
     1561        // we sort the objects as a preprocess so they don't have 
     1562        // to be sorted for each split 
     1563        for (int i = 0; i < 3; ++ i) 
     1564        { 
     1565                CreateLocalSubdivisionCandidates(tData.mNode->mObjects, &sortedObjects, true, i); 
     1566                         
     1567                tData.mSortedObjects[i] = new ObjectContainer(); 
     1568                tData.mSortedObjects[i]->reserve((int)sortedObjects->size()); 
     1569 
     1570                SortableEntryContainer::const_iterator oit, oit_end = sortedObjects->end(); 
     1571                for (oit = sortedObjects->begin(); oit != oit_end; ++ oit) 
     1572                { 
     1573                        tData.mSortedObjects[i]->push_back((*oit).mObject); 
     1574                } 
     1575        } 
     1576 
     1577        delete sortedObjects; 
     1578} 
     1579 
     1580 
     1581void BvHierarchy::AssignSortedObjects(const BvhSubdivisionCandidate &sc, 
     1582                                                                          BvhTraversalData &frontData, 
     1583                                                                          BvhTraversalData &backData) 
     1584{ 
     1585        Intersectable::NewMail(); 
     1586 
     1587        // we sorted the objects as a preprocess so they don't have 
     1588        // to be sorted for each split 
     1589        ObjectContainer::const_iterator fit, fit_end = sc.mFrontObjects.end(); 
     1590 
     1591        for (fit = sc.mFrontObjects.begin(); fit != fit_end; ++ fit) 
     1592        { 
     1593                (*fit)->Mail(); 
     1594        } 
     1595 
     1596        for (int i = 0; i < 3; ++ i) 
     1597        { 
     1598                frontData.mSortedObjects[i]->reserve((int)sc.mFrontObjects.size()); 
     1599                backData.mSortedObjects[i]->reserve((int)sc.mFrontObjects.size()); 
     1600 
     1601                ObjectContainer::const_iterator oit, oit_end = sc.mParentData.mNode->mObjects.end(); 
     1602 
     1603                for (oit = sc.mParentData.mNode->mObjects.begin(); oit != oit_end; ++ oit) 
     1604                { 
     1605                        if ((*oit)->Mailed()) 
     1606                        { 
     1607                                frontData.mSortedObjects[i]->push_back(*oit); 
     1608                        } 
     1609                        else 
     1610                        { 
     1611                                backData.mSortedObjects[i]->push_back(*oit); 
     1612                        } 
     1613                } 
     1614        } 
     1615} 
     1616 
     1617 
    15321618void BvhStatistics::Print(ostream &app) const 
    15331619{ 
Note: See TracChangeset for help on using the changeset viewer.