Changeset 1345


Ignore:
Timestamp:
09/12/06 19:30:31 (18 years ago)
Author:
mattausch
Message:

started with global objects sorting for bvh split heuristics

Location:
GTP/trunk/Lib/Vis/Preprocessing/src
Files:
2 edited

Legend:

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

    r1344 r1345  
    262262 
    263263 
    264 BvhInterior *BvHierarchy::SubdivideNode(const ObjectContainer &frontObjects, 
    265                                                                                 const ObjectContainer &backObjects, 
    266                                                                                 const BvhTraversalData &tData, 
     264BvhInterior *BvHierarchy::SubdivideNode(const BvhSubdivisionCandidate &sc, 
    267265                                                                                BvhTraversalData &frontData, 
    268266                                                                                BvhTraversalData &backData) 
    269267{ 
     268        const BvhTraversalData &tData = sc.mParentData; 
    270269        BvhLeaf *leaf = tData.mNode; 
    271          
    272         // two new leaves 
    273     mBvhStats.nodes += 2; 
     270    mBvhStats.nodes += 2; // we have two new leaves 
    274271 
    275272        // add the new nodes to the tree 
     
    281278        frontData.mDepth = backData.mDepth = tData.mDepth + 1; 
    282279 
    283         frontData.mBoundingBox = ComputeBoundingBox(frontObjects, &tData.mBoundingBox); 
    284         backData.mBoundingBox = ComputeBoundingBox(backObjects, &tData.mBoundingBox); 
    285          
    286         ///////////// 
     280        frontData.mBoundingBox = ComputeBoundingBox(sc.mFrontObjects, &tData.mBoundingBox); 
     281        backData.mBoundingBox = ComputeBoundingBox(sc.mBackObjects, &tData.mBoundingBox); 
     282         
     283 
     284        //////////////////////////////// 
    287285        //-- create front and back leaf 
    288286 
    289287        BvhLeaf *back =  
    290                 new BvhLeaf(backData.mBoundingBox, node, (int)backObjects.size()); 
     288                new BvhLeaf(backData.mBoundingBox, node, (int)sc.mBackObjects.size()); 
    291289        BvhLeaf *front =  
    292                 new BvhLeaf(frontData.mBoundingBox, node, (int)frontObjects.size()); 
     290                new BvhLeaf(frontData.mBoundingBox, node, (int)sc.mFrontObjects.size()); 
    293291 
    294292        BvhInterior *parent = leaf->GetParent(); 
    295  
    296293 
    297294        // replace a link from node's parent 
     
    301298                node->SetParent(parent); 
    302299        } 
    303         else // new root 
     300        else // no parent => this node is the root 
    304301        { 
    305302                mRoot = node; 
     
    311308        ++ mBvhStats.splits; 
    312309 
    313  
    314310        //////////////////////////////////// 
     311        //-- fill traversal data 
    315312 
    316313        frontData.mNode = front; 
    317314        backData.mNode = back; 
    318315 
    319         back->mObjects = backObjects; 
    320         front->mObjects = frontObjects; 
     316        back->mObjects = sc.mBackObjects; 
     317        front->mObjects = sc.mFrontObjects; 
    321318 
    322319        AssociateObjectsWithLeaf(back); 
    323320        AssociateObjectsWithLeaf(front); 
    324321    
    325         // compute probability, i.e., volume of seen view cells 
    326         frontData.mProbability = EvalViewCellsVolume(frontObjects); 
    327         backData.mProbability = EvalViewCellsVolume(backObjects); 
    328  
     322        // compute probability of this node being visible,  
     323        // i.e., volume of the view cells that can see this node 
     324        frontData.mProbability = EvalViewCellsVolume(sc.mFrontObjects); 
     325        backData.mProbability = EvalViewCellsVolume(sc.mBackObjects); 
     326 
     327    // how often was max cost ratio missed in this branch? 
     328        frontData.mMaxCostMisses = sc.mMaxCostMisses; 
     329        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 
     339        // return the new interior node 
    329340        return node; 
    330341} 
     
    339350        BvhTraversalData &tData = sc->mParentData; 
    340351 
    341         BvhNode *newNode = tData.mNode; 
     352        BvhNode *currentNode = tData.mNode; 
    342353 
    343354        if (!LocalTerminationCriteriaMet(tData) && !globalCriteriaMet) 
    344355        {        
     356                ////////////////////////////////////////// 
    345357                //-- continue subdivision 
    346358 
     
    349361                         
    350362                // create new interior node and two leaf node 
    351                 newNode = SubdivideNode(sc->mFrontObjects, 
    352                                                                 sc->mBackObjects, 
    353                                                                 tData,  
    354                                                                 tFrontData,  
    355                                                                 tBackData); 
    356          
    357                 const int maxCostMisses = sc->mMaxCostMisses; 
    358  
    359         // how often was max cost ratio missed in this branch? 
    360                 tFrontData.mMaxCostMisses = maxCostMisses; 
    361                 tBackData.mMaxCostMisses = maxCostMisses; 
    362                  
     363                currentNode = SubdivideNode( 
     364                        *sc,  
     365                        tFrontData,  
     366                        tBackData); 
     367         
    363368                // decrease the weighted average cost of the subdivisoin 
    364369                mTotalCost -= sc->GetRenderCostDecrease(); 
     
    367372                if (1) PrintSubdivisionStats(*sc); 
    368373 
     374 
     375                ///////////////////////////////////////// 
    369376                //-- push the new split candidates on the queue 
    370377                 
     
    386393                tQueue.Push(frontCandidate); 
    387394                tQueue.Push(backCandidate); 
    388  
    389                 // delete old leaf node 
    390                 //DEL_PTR(tData.mNode); 
    391         } 
    392  
    393         //-- terminate traversal 
    394  
    395     if (newNode->IsLeaf()) 
    396         { 
     395        } 
     396 
     397        ///////////////////////////////// 
     398        //-- node is a leaf => terminate traversal 
     399 
     400        if (currentNode->IsLeaf()) 
     401        { 
     402                ////////////////////////////////////// 
    397403                //-- store additional info 
    398                  
    399404                EvaluateLeafStats(tData); 
    400405         
     
    402407                if (mStoreRays) 
    403408                { 
    404                         BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(newNode); 
     409                        BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(currentNode); 
    405410                        CollectRays(leaf->mObjects, leaf->mVssRays); 
    406411                } 
    407412                 
    408                 // detach subdivision candidate: this leaf is no candidate for 
    409                 // splitting anymore 
     413                ////////////////////////////////////// 
     414                 
     415                // this leaf is no candidate for splitting anymore 
     416                // => detach subdivision candidate 
    410417                tData.mNode->SetSubdivisionCandidate(NULL);  
    411                 // detach node so it won't get deleted 
     418                // detach node so we don't delete it with the traversal data 
    412419                tData.mNode = NULL; 
    413420        } 
    414421         
    415         return newNode; 
     422        return currentNode; 
    416423} 
    417424 
     
    561568                                                                                        ObjectContainer &objectsBack) 
    562569{ 
    563         SortSubdivisionCandidates(tData, axis); 
     570        SortSubdivisionCandidates(tData.mNode->mObjects, &mSubdivisionCandidates, axis); 
    564571         
    565572        vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
     
    596603                                                   ObjectContainer &objectsBack) 
    597604{ 
    598         SortSubdivisionCandidates(tData, axis); 
     605        SortSubdivisionCandidates(tData.mNode->mObjects, &mSubdivisionCandidates, axis); 
    599606   
    600607        // go through the lists, count the number of objects left and right 
     
    846853 
    847854 
    848 void BvHierarchy::SortSubdivisionCandidates(const BvhTraversalData &tData, 
     855void BvHierarchy::SortSubdivisionCandidates(const ObjectContainer &objects, 
     856                                                                                        vector<SortableEntry> **subdivisionCandidates, 
    849857                                                                                        const int axis)                                                                                  
    850858{ 
    851         mSubdivisionCandidates->clear(); 
    852  
    853         //RayInfoContainer *rays = tData.mRays; 
    854         BvhLeaf *leaf = tData.mNode; 
     859        (*subdivisionCandidates)->clear(); 
    855860 
    856861        // compute requested size 
    857         const int requestedSize = (int)leaf->mObjects.size() * 2; 
     862        const int requestedSize = (int)objects.size() * 2; 
    858863         
    859864        // creates a sorted split candidates array 
    860         if (mSubdivisionCandidates->capacity() > 500000 && 
    861                 requestedSize < (int)(mSubdivisionCandidates->capacity() / 10) ) 
    862         { 
    863         delete mSubdivisionCandidates; 
    864                 mSubdivisionCandidates = new vector<SortableEntry>; 
    865         } 
    866  
    867         mSubdivisionCandidates->reserve(requestedSize); 
     865        if ((*subdivisionCandidates)->capacity() > 500000 && 
     866                requestedSize < (int)((*subdivisionCandidates)->capacity() / 10) ) 
     867        { 
     868        delete *subdivisionCandidates; 
     869                *subdivisionCandidates = new vector<SortableEntry>; 
     870        } 
     871 
     872        (*subdivisionCandidates)->reserve(requestedSize); 
    868873 
    869874        //-- insert object queries 
     
    872877        //-- there is no ray associated with these events!! 
    873878 
    874         ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end(); 
    875  
    876         for (oit = leaf->mObjects.begin(); oit < oit_end; ++ oit) 
     879        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     880 
     881        for (oit = objects.begin(); oit < oit_end; ++ oit) 
    877882        { 
    878883                Intersectable *obj = *oit; 
     
    882887                const float midPt = (box.Min(axis) + box.Max(axis)) * 0.5f; 
    883888 
    884                 mSubdivisionCandidates->push_back(SortableEntry(object, midPt)); 
    885         } 
    886  
    887         stable_sort(mSubdivisionCandidates->begin(), mSubdivisionCandidates->end()); 
     889                (*subdivisionCandidates)->push_back(SortableEntry(object, midPt)); 
     890        } 
     891 
     892        stable_sort((*subdivisionCandidates)->begin(), (*subdivisionCandidates)->end()); 
    888893} 
    889894 
     
    901906 
    902907        // sort so we can use a sweep from right to left 
    903         SortSubdivisionCandidates(tData, axis); 
     908        SortSubdivisionCandidates(tData.mNode->mObjects, &mSubdivisionCandidates, axis); 
    904909 
    905910        // collect and mark the view cells as belonging to front pvs 
     
    14641469        mBvhStats.nodes = 1; 
    14651470 
     1471        for (int i = 0; i < 3; ++ i) 
     1472        { 
     1473                mGlobalSubdivisionCandidates[i] = new vector<SortableEntry>(); 
     1474                SortSubdivisionCandidates(objects, &mGlobalSubdivisionCandidates[i], i); 
     1475        } 
     1476 
    14661477        // note matt: we assume that we have objects sorted by their id 
    14671478 
  • GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h

    r1323 r1345  
    283283public: 
    284284         
     285        struct SortableEntry; 
     286        typedef vector<SortableEntry> SortableEntryContainer; 
     287 
    285288        /** Additional data which is passed down the BSP tree during traversal. 
    286289        */ 
     
    294297                mProbability(0.0), 
    295298                mMaxCostMisses(0),  
    296                 mPriority(0), 
    297299                mAxis(0) 
     300                //mObjectsStart(0), 
     301                //mObjectsEnd(0) 
    298302                {} 
    299303                 
     
    307311                mProbability(v), 
    308312                mMaxCostMisses(0), 
    309                 mPriority(0), 
    310313                mAxis(0) 
     314                //mObjectsStart(0) 
     315                //mObjectsEnd(0) 
    311316                {} 
    312  
    313  
    314                 /** Returns cost of the traversal data. 
    315                 */ 
    316                 float GetCost() const 
    317                 { 
    318                         return mPriority; 
    319                 } 
    320317 
    321318                /// deletes contents and sets them to NULL 
     
    337334                /// current axis 
    338335                int mAxis; 
    339                 /// current priority 
    340                 float mPriority; 
    341  
    342  
    343                 friend bool operator<(const BvhTraversalData &a, const BvhTraversalData &b) 
    344                 { 
    345                         return a.GetCost() < b.GetCost(); 
    346                 } 
     336                /// start of objects 
     337                SortableEntryContainer::const_iterator mObjectsStart; 
     338                /// end of objects 
     339                SortableEntryContainer::const_iterator mObjectsEnd; 
    347340    }; 
    348341 
     
    544537                } 
    545538        }; 
    546   
     539 
    547540        /** Evaluate balanced object partition. 
    548541        */ 
     
    586579         
    587580        /** Subdivides leaf. 
    588                 @param leaf the leaf to be subdivided 
     581                @param sc the subdivisionCandidate holding all necessary data for subdivision            
    589582                 
    590                 @param polys the polygons to be split 
    591                 @param frontPolys returns the polygons in front of the split plane 
    592                 @param backPolys returns the polygons in the back of the split plane 
    593                  
    594                 @param rays the polygons to be filtered 
    595                 @param frontRays returns the polygons in front of the split plane 
    596                 @param backRays returns the polygons in the back of the split plane 
    597  
    598                 @returns the root of the subdivision 
     583                @param frontData returns the traversal data for the front node 
     584                @param backData returns the traversal data for the back node 
     585 
     586                @returns the new interior node = the of the subdivision 
    599587        */ 
    600588        BvhInterior *SubdivideNode( 
    601                 const ObjectContainer &frontObjects, 
    602                 const ObjectContainer &backObjects, 
    603                 const BvhTraversalData &tData, 
     589                const BvhSubdivisionCandidate &sc, 
    604590                BvhTraversalData &frontData, 
    605591                BvhTraversalData &backData); 
     
    636622                @param axis the current split axis 
    637623        */ 
    638         void SortSubdivisionCandidates( 
    639                 const BvhTraversalData &data, 
     624        static void SortSubdivisionCandidates( 
     625                const ObjectContainer &objects, 
     626                vector<SortableEntry> **subdivisionCandidates, 
    640627                const int axis); 
    641628 
     
    724711protected: 
    725712         
     713        /// pre-sorted subdivision candidtes for all three directions. 
     714        vector<SortableEntry> *mGlobalSubdivisionCandidates[3]; 
     715 
    726716        /// pointer to the hierarchy of view cells 
    727717        ViewCellsTree *mViewCellsTree; 
Note: See TracChangeset for help on using the changeset viewer.