Ignore:
Timestamp:
06/18/06 03:47:06 (18 years ago)
Author:
mattausch
Message:

worked on view-object space partition
fixed some loading bugs
fixeds some exporting bugs using line segments
enabling other methods for view space sampling in ViewCellsManager? OBJECT_DIRECTION_BASED_DISTRIBUTION)
added class interface for a sampling strategy

File:
1 edited

Legend:

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

    r1016 r1020  
    2121 
    2222#define USE_FIXEDPOINT_T 0 
    23  
    24  
     23#define COUNT_ORIGIN_OBJECTS 1 
     24 
     25         
    2526//-- static members 
    26  
    2727 
    2828int VspBspTree::sFrontId = 0; 
    2929int VspBspTree::sBackId = 0; 
    3030int VspBspTree::sFrontAndBackId = 0; 
     31 
     32 
    3133 
    3234typedef pair<BspNode *, BspNodeGeometry *> bspNodePair; 
     
    113115 
    114116        Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.renderCostWeight", mRenderCostWeight); 
     117        Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight); 
    115118        Environment::GetSingleton()->GetBoolValue("VspBspTree.usePolygonSplitIfAvailable", mUsePolygonSplitIfAvailable); 
    116119 
     
    161164        Debug << "maxband: " << mMaxBand << endl; 
    162165        Debug << "use driving axis for max cost: " << mUseDrivingAxisIfMaxCostViolated << endl; 
     166        Debug << "render cost decrease weight: " << mRenderCostDecreaseWeight << endl; 
    163167 
    164168        Debug << "Split plane strategy: "; 
     
    189193 
    190194 
    191         mSplitCandidates = new vector<SortableEntry>; 
     195        mLocalSplitCandidates = new vector<SortableEntry>; 
    192196 
    193197        Debug << endl; 
     
    236240{ 
    237241        DEL_PTR(mRoot); 
    238         DEL_PTR(mSplitCandidates); 
     242        DEL_PTR(mLocalSplitCandidates); 
    239243} 
    240244 
     
    497501        const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume(); 
    498502 
     503        /// first traversal data 
    499504        VspBspTraversalData tData(mRoot, 
    500505                                                          new PolygonContainer(polys), 
     
    520525        mTotalPvsSize = tData.mPvs; 
    521526         
    522         // first stats 
    523         mSubdivisionStats  
    524                         << "#ViewCells\n1" <<  endl 
    525                         << "#RenderCostDecrease\n0" << endl  
    526                         << "#TotalRenderCost\n" << mTotalCost << endl 
    527                         << "#AvgRenderCost\n" << mTotalPvsSize << endl; 
    528  
    529         Debug << "total cost: " << mTotalCost << endl; 
    530  
    531  
     527        // first subdivison statistics 
     528        AddSubdivisionStats(1, 0, 0, mTotalCost, (float)mTotalPvsSize); 
     529     
    532530        mBspStats.Start(); 
    533531        cout << "Constructing vsp bsp tree ... \n"; 
     
    586584 
    587585        Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl; 
    588         cout << "finished\n"; 
     586        cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << "secs" << endl; 
    589587 
    590588        mBspStats.Stop(); 
     
    624622        mTotalPvsSize = tData.mPvs; 
    625623         
    626         mSubdivisionStats  
    627                         << "#ViewCells\n1\n" <<  endl 
    628                         << "#RenderCostDecrease\n0\n" << endl  
    629                         << "#SplitCandidateCost\n0\n" << endl 
    630                         << "#TotalRenderCost\n" << mTotalCost << endl 
    631                         << "#AvgRenderCost\n" << mTotalPvsSize << endl; 
    632  
    633         Debug << "total cost: " << mTotalCost << endl; 
    634          
    635          
    636         mBspStats.Start(); 
     624        // first subdivison statistics 
     625        AddSubdivisionStats(1, 0, 0, mTotalCost, (float)mTotalPvsSize); 
     626    
     627    mBspStats.Start(); 
    637628        cout << "Constructing vsp bsp tree ... \n"; 
    638629 
     
    654645 
    655646                // cost ratio of cost decrease / totalCost 
    656                 float costRatio = splitCandidate.GetCost() / mTotalCost; 
     647                float costRatio = splitCandidate.GetPriority() / mTotalCost; 
    657648 
    658649                //Debug << "cost ratio: " << costRatio << endl; 
     
    716707 
    717708 
     709void VspBspTree::AddSubdivisionStats(const int viewCells, 
     710                                                                         const float renderCostDecr, 
     711                                                                         const float splitCandidateCost, 
     712                                                                         const float totalRenderCost, 
     713                                                                         const float avgRenderCost) 
     714{ 
     715        mSubdivisionStats  
     716                        << "#ViewCells\n" << viewCells << endl 
     717                        << "#RenderCostDecrease\n" << renderCostDecr << endl  
     718                        << "#SplitCandidateCost\n" << splitCandidateCost << endl 
     719                        << "#TotalRenderCost\n" << totalRenderCost << endl 
     720                        << "#AvgRenderCost\n" << avgRenderCost << endl; 
     721} 
     722 
     723 
    718724bool VspBspTree::GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const 
    719725{ 
     
    726732 
    727733 
    728  
    729734BspNode *VspBspTree::Subdivide(VspBspTraversalQueue &tQueue, 
    730735                                                           VspBspTraversalData &tData) 
     
    785790                        if (1) 
    786791                        { 
    787                                 float cFront = (float)tFrontData.mPvs * tFrontData.mProbability; 
    788                                 float cBack = (float)tBackData.mPvs * tBackData.mProbability; 
    789                                 float cData = (float)tData.mPvs * tData.mProbability;; 
    790  
    791                 float costDecr = (cFront + cBack - cData) / mBox.GetVolume(); 
     792                                const float cFront = (float)tFrontData.mPvs * tFrontData.mProbability; 
     793                                const float cBack = (float)tBackData.mPvs * tBackData.mProbability; 
     794                                const float cData = (float)tData.mPvs * tData.mProbability;; 
     795 
     796                const float costDecr = (cFront + cBack - cData) / mBox.GetVolume(); 
    792797 
    793798                                mTotalCost += costDecr; 
    794799                                mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs; 
    795800 
    796                                 mSubdivisionStats  
    797                                                 << "#ViewCells\n" << mBspStats.Leaves() << endl 
    798                                                 << "#RenderCostDecrease\n" << -costDecr << endl  
    799                                                 << "#TotalRenderCost\n" << mTotalCost << endl 
    800                                                 << "#AvgRenderCost\n" << (float)mTotalPvsSize / (float)mBspStats.Leaves() << endl; 
     801                                AddSubdivisionStats(mBspStats.Leaves(), 
     802                                                                    -costDecr, 
     803                                                                        0, 
     804                                                                        mTotalCost, 
     805                                                                        (float)mTotalPvsSize / (float)mBspStats.Leaves()); 
    801806                        } 
    802807 
     
    814819        { 
    815820                BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode); 
     821                 
    816822                BspViewCell *viewCell = new BspViewCell(); 
    817                  
    818823                leaf->SetViewCell(viewCell); 
    819824         
     
    860865                leaf->mProbability = tData.mProbability; 
    861866 
     867                // finally evaluate stats until this leaf 
    862868                EvaluateLeafStats(tData);                
    863869        } 
     
    906912                tBackData.mMaxCostMisses = maxCostMisses; 
    907913                         
    908                  
     914                // statistics 
    909915                if (1) 
    910916                { 
     
    920926                        mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs; 
    921927 
    922                         mSubdivisionStats  
    923                                         << "#ViewCells\n" << mBspStats.Leaves() << endl 
    924                                         << "#RenderCostDecrease\n" << -costDecr << endl 
    925                                         << "#SplitCandidateCost\n" << splitCandidate.GetCost() << endl 
    926                                         << "#TotalRenderCost\n" << mTotalCost << endl 
    927                                         << "#AvgRenderCost\n" << (float)mTotalPvsSize / (float)mBspStats.Leaves() << endl; 
     928                        AddSubdivisionStats(mBspStats.Leaves(),  
     929                                                                -costDecr,   
     930                                                                splitCandidate.GetPriority(),  
     931                                                                mTotalCost,  
     932                                                                (float)mTotalPvsSize / (float)mBspStats.Leaves()); 
    928933                } 
    929934 
     
    948953        { 
    949954                BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode); 
     955 
    950956                BspViewCell *viewCell = new BspViewCell(); 
    951  
    952957        leaf->SetViewCell(viewCell); 
    953958                 
     
    974979                } 
    975980 
    976                 // should I check here? 
     981                 
    977982                viewCell->mLeaf = leaf; 
    978983 
     
    984989        leaf->mProbability = tData.mProbability; 
    985990 
     991                // finally evaluate stats until this leaf 
    986992                EvaluateLeafStats(tData);                
    987993        } 
     
    10291035         
    10301036        // compute global decrease in render cost 
    1031         splitData.mRenderCost = EvalRenderCostDecrease(splitData.mSplitPlane, tData); 
     1037        splitData.mPriority = EvalRenderCostDecrease(splitData.mSplitPlane, tData); 
    10321038        splitData.mParentData = tData; 
    10331039        splitData.mMaxCostMisses = success ? tData.mMaxCostMisses : tData.mMaxCostMisses + 1; 
     
    11921198           
    11931199                // only count termination objects? 
    1194                 if (1 && ray->mOriginObject)  
     1200                if (COUNT_ORIGIN_OBJECTS && ray->mOriginObject)  
    11951201                { 
    11961202                        if (vc->AddPvsSample(ray->mOriginObject, ray->mPdf, contribution)) 
    11971203                                madeContrib = true; 
     1204 
    11981205                        sc += contribution; 
    11991206                } 
     
    12141221                                                                         float maxBand) 
    12151222{ 
    1216         mSplitCandidates->clear(); 
     1223        mLocalSplitCandidates->clear(); 
    12171224 
    12181225        int requestedSize = 2 * (int)(rays.size()); 
    12191226        // creates a sorted split candidates array 
    1220         if (mSplitCandidates->capacity() > 500000 && 
    1221                 requestedSize < (int)(mSplitCandidates->capacity() / 10) ) 
    1222         { 
    1223         delete mSplitCandidates; 
    1224                 mSplitCandidates = new vector<SortableEntry>; 
    1225         } 
    1226  
    1227         mSplitCandidates->reserve(requestedSize); 
     1227        if (mLocalSplitCandidates->capacity() > 500000 && 
     1228                requestedSize < (int)(mLocalSplitCandidates->capacity() / 10) ) 
     1229        { 
     1230        delete mLocalSplitCandidates; 
     1231                mLocalSplitCandidates = new vector<SortableEntry>; 
     1232        } 
     1233 
     1234        mLocalSplitCandidates->reserve(requestedSize); 
    12281235 
    12291236        float pos; 
     
    12451252                if (0) ClipValue(pos, minBand, maxBand); 
    12461253                 
    1247                 mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,  
     1254                mLocalSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,  
    12481255                                                                        pos, (*ri).mRay)); 
    12491256 
     
    12521259                if (0) ClipValue(pos, minBand, maxBand); 
    12531260 
    1254                 mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,  
     1261                mLocalSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,  
    12551262                                                                        pos, (*ri).mRay)); 
    12561263        } 
    12571264 
    1258         stable_sort(mSplitCandidates->begin(), mSplitCandidates->end()); 
     1265        stable_sort(mLocalSplitCandidates->begin(), mLocalSplitCandidates->end()); 
    12591266} 
    12601267 
     
    13071314                Intersectable *tObject = (*ri).mRay->mTerminationObject; 
    13081315 
    1309                 if (oObject) 
     1316                if (COUNT_ORIGIN_OBJECTS && oObject) 
    13101317                { 
    13111318                        if (!oObject->Mailed()) 
     
    13191326                        } 
    13201327                } 
     1328 
    13211329                if (tObject) 
    13221330                { 
     
    13351343        Intersectable::NewMail(); 
    13361344 
    1337         vector<SortableEntry>::const_iterator ci, ci_end = mSplitCandidates->end(); 
    1338  
    1339         for (ci = mSplitCandidates->begin(); ci != ci_end; ++ ci) 
     1345        vector<SortableEntry>::const_iterator ci, ci_end = mLocalSplitCandidates->end(); 
     1346 
     1347        for (ci = mLocalSplitCandidates->begin(); ci != ci_end; ++ ci) 
    13401348        { 
    13411349                VssRay *ray; 
     
    13501358                        case SortableEntry::ERayMin: 
    13511359                                { 
    1352                                         if (oObject && !oObject->Mailed()) 
     1360                                        if (COUNT_ORIGIN_OBJECTS && oObject && !oObject->Mailed()) 
    13531361                                        { 
    13541362                                                oObject->Mail(); 
    13551363                                                ++ pvsl; 
    13561364                                        } 
     1365 
    13571366                                        if (tObject && !tObject->Mailed()) 
    13581367                                        { 
     
    13601369                                                ++ pvsl; 
    13611370                                        } 
     1371 
    13621372                                        break; 
    13631373                                } 
    13641374                        case SortableEntry::ERayMax: 
    13651375                                { 
    1366                                         if (oObject) 
     1376                                        if (COUNT_ORIGIN_OBJECTS && oObject) 
    13671377                                        { 
    13681378                                                if (-- oObject->mCounter == 0) 
     
    13791389                                } 
    13801390                } 
    1381  
    1382                  
    1383                  
    1384                 // Note: sufficient to compare size of bounding boxes of front and back side? 
     1391                 
     1392                 
     1393                // Note: we compare size of bounding boxes of front and back side because 
     1394                // of efficiency reasons (otherwise a new geometry would have to be computed 
     1395                // in each step and incremential evaluation would be difficult. 
     1396                // but then errors happen if the geometry is not an axis aligned box  
     1397                // (i.e., if a geometry aligned split was taken before) 
     1398                // question: is it sufficient to make this approximation? 
    13851399                if (((*ci).value >= minBand) && ((*ci).value <= maxBand)) 
    13861400                { 
     
    18731887                // find front and back pvs for origing and termination object 
    18741888                AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs); 
    1875                 AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
     1889 
     1890                if (COUNT_ORIGIN_OBJECTS)  
     1891                        AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
    18761892        } 
    18771893 
     
    19101926 
    19111927        // -- pvs rendering heuristics 
     1928 
     1929        // upper and lower bounds 
    19121930        const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize(); 
    19131931        const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize(); 
    19141932 
    1915         // only render cost heuristics or combined with standard deviation 
    1916         const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit); 
    1917     const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit); 
    1918         const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit); 
     1933        const float penaltyOld = EvalPvsPenalty((int)totalPvs, lowerPvsLimit, upperPvsLimit); 
     1934    const float penaltyFront = EvalPvsPenalty((int)pvsFront, lowerPvsLimit, upperPvsLimit); 
     1935        const float penaltyBack = EvalPvsPenalty((int)pvsBack, lowerPvsLimit, upperPvsLimit); 
    19191936                         
    19201937        const float oldRenderCost = pOverall * penaltyOld; 
     
    19241941        const float renderCostDecrease = (oldRenderCost - newRenderCost) / mBox.GetVolume(); 
    19251942         
    1926  
    1927 #if 1 
     1943        const float weight = mRenderCostDecreaseWeight; 
    19281944        // take render cost of node into account to avoid being stuck in a local minimum 
    19291945        const float normalizedOldRenderCost = oldRenderCost / mBox.GetVolume(); 
    19301946         
    1931         //Debug << "rendercostdecr: " << 0.99f * renderCostDecrease << " old render cost: " << 0.01f * normalizedOldRenderCost << endl; 
    1932         return 0.99f * renderCostDecrease + 0.01f * normalizedOldRenderCost; 
    1933 #else 
    1934         return renderCostDecrease; 
    1935 #endif 
     1947        //Debug << "rendercostdecr: " << weight * renderCostDecrease << " old render cost: " << (1.0f - weight) * normalizedOldRenderCost << endl; 
     1948        return weight * renderCostDecrease + (1.0f - weight) * normalizedOldRenderCost; 
    19361949} 
    19371950 
     
    19441957                                                                         float &pBack) const 
    19451958{ 
    1946         float cost = 0; 
    1947  
    19481959        float totalPvs = 0; 
    19491960        float pvsFront = 0; 
     
    19571968        pBack = 0; 
    19581969 
    1959  
    1960         int limit; 
    1961         bool useRand; 
    1962  
     1970        int numTests; // the number of tests 
     1971 
     1972        // if random samples shold be taken instead of testing all the rays 
     1973        bool useRand;  
     1974 
     1975        if ((int)data.mRays->size() > mMaxTests) 
     1976        { 
     1977                useRand = true; 
     1978                numTests = mMaxTests; 
     1979        } 
     1980        else 
     1981        { 
     1982                useRand = false; 
     1983                numTests = (int)data.mRays->size(); 
     1984        } 
     1985         
    19631986        // create unique ids for pvs heuristics 
    19641987        GenerateUniqueIdsForPvs(); 
    19651988 
    1966         // choose test rays randomly if too much 
    1967         if ((int)data.mRays->size() > mMaxTests) 
    1968         { 
    1969                 useRand = true; 
    1970                 limit = mMaxTests; 
    1971         } 
    1972         else 
    1973         { 
    1974                 useRand = false; 
    1975                 limit = (int)data.mRays->size(); 
    1976         } 
    1977          
    1978         for (int i = 0; i < limit; ++ i) 
     1989        for (int i = 0; i < numTests; ++ i) 
    19791990        { 
    19801991                const int testIdx = useRand ?  
     
    19881999                // find front and back pvs for origing and termination object 
    19892000                AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs); 
    1990                 AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
     2001 
     2002                if (COUNT_ORIGIN_OBJECTS)  
     2003                        AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs); 
    19912004        } 
    19922005 
     
    20572070        } 
    20582071 
    2059         cost += mPvsFactor * newCost / (oldCost + Limits::Small); 
     2072        const float cost = mPvsFactor * newCost / (oldCost + Limits::Small); 
    20602073                 
    20612074 
     
    21652178        for(rit = data.mRays->begin(); rit != rit_end; ++ rit) 
    21662179        { 
    2167                 //if (!(*rit).mRay->IsActive()) continue; 
    2168  
    21692180                // determine the side of this ray with respect to the plane 
    21702181                float t; 
     
    21722183         
    21732184                AddObjToPvs((*rit).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal); 
    2174                 AddObjToPvs((*rit).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal); 
    2175         } 
     2185 
     2186                if (COUNT_ORIGIN_OBJECTS)  
     2187                        AddObjToPvs((*rit).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal); 
     2188        } 
     2189 
    21762190 
    21772191        //-- pvs heuristics 
    2178         float pOverall; 
    2179  
    2180         //-- compute heurstics 
    2181         //-- we take simplified computation for mid split 
    2182                  
    2183         pOverall = data.mProbability; 
    2184  
     2192 
     2193        float pOverall = data.mProbability; 
     2194 
     2195        // note: we use a simplified computation assuming that we always do a 
     2196        // spatial mid split     
     2197         
    21852198        if (!mUseAreaForPvs) 
    2186         {   // volume 
     2199        {    
     2200                // volume 
    21872201                pBack = pFront = pOverall * 0.5f; 
    2188                  
    21892202#if 0 
    21902203                // box length substitute for probability 
     
    32203233                VssRay *ray = (*rit).mRay; 
    32213234 
    3222                 if (ray->mOriginObject) 
     3235                if (COUNT_ORIGIN_OBJECTS && ray->mOriginObject) 
    32233236                { 
    32243237                        if (!ray->mOriginObject->Mailed()) 
     
    32283241                        } 
    32293242                } 
     3243 
    32303244                if (ray->mTerminationObject) 
    32313245                { 
     
    33433357                        } 
    33443358                        else // one of the ray end points is on the plane 
    3345                         {       // NOTE: what to do if ray is coincident with plane? 
     3359                        {        
     3360                                // NOTE: what to do if ray is coincident with plane? 
    33463361                                if (extSide < 0) 
    33473362                                        node = in->GetBack(); 
     
    33653380                        ViewCell *viewCell; 
    33663381                         
     3382                        // question: always contribute to leaf or to currently active view cell? 
    33673383                        if (0) 
    33683384                                viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell()); 
Note: See TracChangeset for help on using the changeset viewer.