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

fixed sah for objeect partition
loader for single triangles also for x3d

File:
1 edited

Legend:

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

    r1370 r1379  
    290290                                                                                BvhTraversalData &backData) 
    291291{ 
    292         mBvhStats.nodes += 2; // we have two new leaves 
    293  
    294292        const BvhTraversalData &tData = sc.mParentData; 
    295293        BvhLeaf *leaf = tData.mNode; 
    296294        AxisAlignedBox3 parentBox = leaf->GetBoundingBox(); 
     295 
     296        // update stats 
     297        mBvhStats.nodes += 2; // we have two new leaves 
     298 
     299        if (tData.mDepth > mBvhStats.maxDepth) 
     300        { 
     301                mBvhStats.maxDepth = tData.mDepth; 
     302        } 
    297303 
    298304        // add the new nodes to the tree 
     
    471477                splitCandidate.mParentData.mMaxCostMisses; 
    472478 
    473         const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
    474479        const float oldProp = EvalViewCellsVolume(leaf->mObjects); 
    475  
    476         const float oldRenderCost = oldProp * (float)leaf->mObjects.size() / viewSpaceVol; 
    477  
     480        const float oldRenderCost = EvalRenderCost(leaf->mObjects); 
     481                 
    478482        // compute global decrease in render cost 
    479         float newRenderCost = EvalRenderCost(splitCandidate.mParentData, 
    480                                                                              splitCandidate.mFrontObjects, 
    481                                                                                  splitCandidate.mBackObjects); 
    482  
    483         newRenderCost /=  viewSpaceVol; 
     483        const float newRenderCost =  
     484                EvalRenderCost(splitCandidate.mFrontObjects) + 
     485                EvalRenderCost(splitCandidate.mBackObjects); 
     486 
    484487        const float renderCostDecr = oldRenderCost - newRenderCost; 
    485488 
    486         //Debug << "\nbvh render cost decr: " << renderCostDecr << endl; 
     489#ifdef _DEBUG 
     490        Debug << "old render cost: " << oldRenderCost << endl; 
     491        Debug << "new render cost: " << newRenderCost << endl; 
     492        Debug << "render cost decrease: " << renderCostDecr << endl; 
     493#endif 
    487494        splitCandidate.SetRenderCostDecrease(renderCostDecr); 
    488495 
    489 #if 0 
    490         const float priority = (float)-splitCandidate.mParentData.mDepth; 
    491 #else    
     496#if 1 
    492497        // take render cost of node into account  
    493498        // otherwise danger of being stuck in a local minimum!! 
    494499        const float factor = mRenderCostDecreaseWeight; 
    495500        const float priority = factor * renderCostDecr + (1.0f - factor) * oldRenderCost; 
     501#else 
     502        const float priority = (float)-splitCandidate.mParentData.mDepth; 
    496503#endif 
    497504 
     
    540547        // depth related stuff 
    541548 
    542         if (data.mDepth > mBvhStats.maxDepth) 
    543         { 
    544                 mBvhStats.maxDepth = data.mDepth; 
    545         } 
    546  
    547549        if (data.mDepth < mBvhStats.minDepth) 
    548550        { 
     
    640642        } 
    641643 
    642         const float oldProp = EvalViewCellsVolume(tData.mNode->mObjects); 
    643         //const float oldProp = tData.mProbability; 
    644  
    645         const float oldRenderCost = oldProb * (float)tData.mNode->mObjects.size(); 
     644        const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects); 
    646645        const float newRenderCost =  
    647                 EvalRenderCost(tData, objectsFront, objectsBack); 
     646                EvalRenderCost(objectsFront) * EvalRenderCost(objectsBack); 
    648647 
    649648        const float ratio = newRenderCost / oldRenderCost; 
     
    672671                // object mailed => belongs to back objects 
    673672                if (i < border)  
     673                { 
    674674                        objectsBack.push_back(obj); 
     675                } 
    675676                else 
     677                { 
    676678                        objectsFront.push_back(obj); 
    677         } 
    678  
    679         const float oldProp = EvalViewCellsVolume(tData.mNode->mObjects); 
    680         //const float oldProp = tData.mProbability; 
    681  
    682         const float oldRenderCost = oldProp * (float)tData.mNode->mObjects.size(); 
    683         const float newRenderCost = EvalRenderCost(tData, objectsFront, objectsBack); 
     679                } 
     680        } 
     681 
     682        const float oldRenderCost = EvalRenderCost(tData.mNode->mObjects); 
     683        const float newRenderCost = EvalRenderCost(objectsFront) + EvalRenderCost(objectsBack); 
    684684 
    685685        const float ratio = newRenderCost / oldRenderCost; 
     
    694694                                                   ObjectContainer &objectsBack) 
    695695{ 
    696         PrepareLocalSubdivisionCandidates(tData, axis); 
    697    
    698696        // go through the lists, count the number of objects left and right 
    699697        // and evaluate the following cost funcion: 
    700698        // C = ct_div_ci  + (ol + or)/queries 
     699        PrepareLocalSubdivisionCandidates(tData, axis); 
     700 
    701701        int objectsLeft = 0, objectsRight = (int)tData.mNode->mObjects.size(); 
    702702   
     
    709709        float minSum = 1e20f; 
    710710   
     711        float minBorder = maxBox; 
     712        float maxBorder = minBox; 
     713        float areaLeft = 0, areaRight = 0; 
     714 
     715        SortableEntryContainer::const_iterator currentPos = 
     716                mSubdivisionCandidates->begin(); 
     717 
     718         
     719        // we keep track of both borders of the bounding boxes => 
     720        // store the events in descending order 
    711721        vector<float> bordersRight; 
    712722        bordersRight.resize(mSubdivisionCandidates->size()); 
    713723 
    714         float minBorder = maxBox; 
    715         float maxBorder = minBox; 
    716  
    717         SortableEntryContainer::const_iterator currentPos = 
    718                 mSubdivisionCandidates->begin(); 
    719  
    720724        SortableEntryContainer::reverse_iterator rcit =  
    721725                mSubdivisionCandidates->rbegin(), rcit_end = mSubdivisionCandidates->rend(); 
    722                  
     726         
    723727        vector<float>::reverse_iterator rbit = bordersRight.rbegin(); 
    724728         
     
    728732                const AxisAlignedBox3 box = obj->GetBox(); 
    729733 
    730                 if (box.Min() < minBorder) 
     734                if (box.Min(axis) < minBorder) 
     735                { 
    731736                        minBorder = box.Min(axis); 
    732                  
     737                } 
     738 
    733739                (*rbit) = minBorder; 
    734740        } 
    735741 
    736742        vector<float>::const_iterator bit = bordersRight.begin(); 
    737         cout << "here42" << endl; 
    738743        SortableEntryContainer::const_iterator cit, cit_end = mSubdivisionCandidates->end(); 
     744 
    739745        for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ bit)  
    740746        { 
     
    749755                const AxisAlignedBox3 obox = obj->GetBox(); 
    750756 
     757                // the borders of the bounding boxes have changed 
    751758                if (obox.Max(axis) > maxBorder) 
     759                { 
    752760                        maxBorder = obox.Max(axis); 
    753  
     761                } 
     762 
     763                minBorder = (*bit); 
     764         
    754765        lbox.SetMax(axis, maxBorder); 
    755                 rbox.SetMin(axis, *bit); 
    756          
    757                 const float sum = objectsLeft * lbox.SurfaceArea() + objectsRight * rbox.SurfaceArea(); 
     766                rbox.SetMin(axis, minBorder); 
     767         
     768                const float al = lbox.SurfaceArea(); 
     769                const float ar = rbox.SurfaceArea(); 
     770 
     771                const float sum = objectsLeft * al + objectsRight * ar; 
    758772       
    759                 cout << "pos=" << (*cit).mPos << "\t q=(" << objectsLeft << "," << objectsRight <<")\t r=("  
     773                /*cout << "pos=" << (*cit).mPos << "\t q=(" << objectsLeft << "," << objectsRight <<")\t r=("  
    760774                         << lbox.SurfaceArea() << "," << rbox.SurfaceArea() << ")" << endl; 
     775                cout << "minborder: " << minBorder << " maxborder: " << maxBorder << endl; 
    761776            cout << "cost= " << sum << endl; 
    762        
     777*/ 
    763778                if (sum < minSum)  
    764779                { 
    765                         minSum = sum;    
     780                        minSum = sum; 
     781                        areaLeft = al; 
     782                        areaRight = ar; 
    766783                        // objects belong to left side now 
    767784                        for (; currentPos != (cit + 1); ++ currentPos); 
    768785                } 
    769786        } 
     787 
    770788 
    771789        //////////////////////////////////////////// 
     
    784802        float ratio = newCost / oldCost; 
    785803   
    786 #if 0 
    787   cout<<"===================="<<endl; 
    788   cout<<"costRatio="<<ratio<<" pos="<<position<<" t="<<(position - minBox)/(maxBox - minBox) 
    789       <<"\t o=("<<objectsBack<<","<<objectsFront<<")"<<endl; 
     804#ifdef _DEBUG 
     805        cout << "\n\nobjects=(" << objectsBack.size() << "," << objectsFront.size() << " of " << tData.mNode->mObjects.size() << ")\t area=("  
     806                 << areaLeft << "," << areaRight << ")" << endl; 
     807        cout << "cost= " << minSum << endl; 
    790808#endif 
    791809  return ratio; 
     
    855873        int nObjectsLeft = 0; 
    856874        const int nTotalObjects = (int)tData.mNode->mObjects.size(); 
    857         const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
     875        const float viewSpaceVol = mHierarchyManager->GetViewSpaceBox().GetVolume(); 
    858876 
    859877        SortableEntryContainer::const_iterator backObjectsStart = mSubdivisionCandidates->begin(); 
     
    11281146                                        //-- use surface area heuristic because view cells not constructed yet                           
    11291147                                        nCostRatio[axis] = 
    1130                                                 EvalLocalCostHeuristics( 
    1131                                                                                                 tData, 
    1132                                                                                                 axis, 
    1133                                                                                                 nFrontObjects[axis], 
    1134                                                                                                 nBackObjects[axis]); 
     1148                                                EvalSah( 
     1149                                                                tData, 
     1150                                                                axis, 
     1151                                                                nFrontObjects[axis], 
     1152                                                                nBackObjects[axis]); 
    11351153                                } 
    11361154                        } 
     
    11381156                        { 
    11391157                                //-- split objects using some simple criteria 
    1140  
    11411158                                nCostRatio[axis] = 
    11421159                                        EvalLocalObjectPartition( 
     
    12451262 
    12461263 
    1247 float BvHierarchy::EvalRenderCost(const BvhTraversalData &tData, 
    1248                                                                   const ObjectContainer &objectsFront, 
    1249                                                                   const ObjectContainer &objectsBack) const 
    1250 { 
    1251         // probability that view point lies in a view cell which sees this node 
    1252         const float pFront = EvalViewCellsVolume(objectsFront); 
    1253         const float pBack = EvalViewCellsVolume(objectsBack); 
    1254                  
    1255         //-- pvs rendering heuristics 
    1256         const float newRenderCost = (int)objectsFront.size() * pFront + (int)objectsBack.size() * pBack; 
    1257  
    1258 #ifdef _DEBUG 
    1259         const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
    1260         Debug << "\nbvh render cost\n"  
    1261                   << "back p: " << pBack / viewSpaceVol << " front p " << pFront / viewSpaceVol << endl 
    1262                   << "new rc: " << newRenderCost / viewSpaceVol << endl; 
    1263 #endif 
    1264         return newRenderCost; 
     1264float BvHierarchy::EvalRenderCost(const ObjectContainer &objects) const 
     1265{ 
     1266        if (mHierarchyManager->GetViewSpaceSubdivisionType() == HierarchyManager::NO_VIEWSPACE_SUBDIV) 
     1267        { 
     1268                if (objects.empty()) 
     1269                        return 0.0f; 
     1270 
     1271                ///////////////////////////// 
     1272                //-- surface area heuristics 
     1273 
     1274                const AxisAlignedBox3 box = ComputeBoundingBox(objects); 
     1275                const float area = box.SurfaceArea(); 
     1276 
     1277                return (float)objects.size() * area; 
     1278        } 
     1279        else 
     1280        {       ///////////////////////////// 
     1281                //-- render cost heuristics 
     1282 
     1283                const float viewSpaceVol = mHierarchyManager->GetViewSpaceBox().GetVolume(); 
     1284                // probability that view point lies in a view cell which sees this node 
     1285                const float p = EvalViewCellsVolume(objects) / viewSpaceVol;     
     1286 
     1287                return (float)objects.size() * p; 
     1288        } 
    12651289} 
    12661290 
    12671291 
    12681292AxisAlignedBox3 BvHierarchy::ComputeBoundingBox(const ObjectContainer &objects, 
    1269                                                                                                 const AxisAlignedBox3 *parentBox) 
     1293                                                                                                const AxisAlignedBox3 *parentBox) const 
    12701294{ 
    12711295        if (parentBox && objects.empty()) 
     
    13251349                                                                   const bool setCounter) const 
    13261350{ 
     1351        // no view cells yet 
     1352        if (mHierarchyManager->GetViewSpaceSubdivisionType() ==  
     1353                HierarchyManager::NO_VIEWSPACE_SUBDIV) 
     1354                return; 
     1355 
    13271356        ViewCell::NewMail(); 
    13281357        ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     
    13481377                ViewCellContainer tmpViewCells; 
    13491378         
    1350                 mVspTree->GetViewCells(*ray, tmpViewCells); 
     1379                mHierarchyManager->mVspTree->GetViewCells(*ray, tmpViewCells); 
    13511380 
    13521381                ViewCellContainer::const_iterator vit, vit_end = tmpViewCells.end(); 
     
    16451674        bvhleaf->SetSubdivisionCandidate(oSubdivisionCandidate); 
    16461675 
    1647         const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume(); 
     1676        const float viewSpaceVol = mHierarchyManager->GetViewSpaceBox().GetVolume(); 
    16481677        mTotalCost = (float)objects.size() * prop / viewSpaceVol; 
    16491678 
Note: See TracChangeset for help on using the changeset viewer.