Ignore:
Timestamp:
01/27/06 16:27:22 (18 years ago)
Author:
mattausch
Message:

started to include variance into the measures

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp

    r578 r579  
    2929const float VspBspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0}; 
    3030 
    31 int BspMergeCandidate::sMaxPvsSize = 0; 
     31ViewCellsManager *BspMergeCandidate::sViewCellsManager = NULL; 
     32//int BspMergeCandidate::sMaxPvsSize = 0; 
    3233//int BspMergeCandidate::sMinPvsSize = 0; 
    3334 
     
    3738 
    3839float BspMergeCandidate::sOverallCost = 0; 
     40float BspMergeCandidate::sExpectedCost = 0; 
     41float BspMergeCandidate::sVariance = 0; 
     42float BspMergeCandidate::sRenderCostWeight = 0.5f; 
    3943bool BspMergeCandidate::sUseArea = false; 
    40  
    41  
    42 int BspMergeCandidate::sUpperPvsLimit = 120; 
    43 int BspMergeCandidate::sLowerPvsLimit = 5; 
    44  
    45  
    46  
    47 /********************************************************************/ 
    48 /*                  class VspBspTree implementation                 */ 
    49 /********************************************************************/ 
     44int BspMergeCandidate::sNumViewCells = 0; 
     45 
     46//int upperPvsLimit = 120; 
     47//int lowerPvsLimit = 5; 
     48 
     49 
     50 
     51// pvs penalty can be different from pvs size 
     52inline float EvalPvsPenalty(const int pvs,  
     53                                                        const int lower, 
     54                                                        const int upper) 
     55{ 
     56        // clamp to minmax values 
     57        if (pvs < lower) 
     58                return (float)lower; 
     59        if (pvs > upper) 
     60                return (float)upper; 
     61 
     62        return (float)pvs; 
     63} 
     64 
     65 
     66// penalty for pvs durint merge 
     67inline float EvalPvsPenaltyForMerge(const int pvs,  
     68                                                                        const int lower, 
     69                                                                        const int upper) 
     70{ 
     71        // clamp to minmax values 
     72        if (pvs < lower) 
     73                return (float)lower; 
     74        if (pvs > upper) 
     75                return (float)upper; 
     76 
     77        return (float)pvs; 
     78} 
     79 
     80 
     81/**********************************************************************/ 
     82/*                  class VspBspTree implementation                   */ 
     83/**********************************************************************/ 
    5084 
    5185 
     
    192226        DEL_PTR(mSplitCandidates); 
    193227} 
     228 
    194229 
    195230int VspBspTree::AddMeshToPolygons(Mesh *mesh, 
     
    10701105#endif 
    10711106 
     1107         
    10721108        const float newCost = pvsBack * pBack + pvsFront * pFront; 
    10731109        const float oldCost = (float)pvsSize * pOverall + Limits::Small; 
     
    13881424                if (1) 
    13891425                { 
    1390                         const float oldCost = pOverall * (float)totalPvs + Limits::Small; 
    1391                         cost += mPvsFactor * (pvsFront * pFront + pvsBack * pBack) / oldCost; 
     1426                        const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize(); 
     1427                        const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize(); 
     1428                         
     1429                        const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit); 
     1430             
     1431                        const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit); 
     1432                        const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit); 
     1433                         
     1434                        const float oldCost = pOverall * (float)penaltyOld + Limits::Small; 
     1435                        cost += mPvsFactor * (penaltyFront * pFront + penaltyBack * pBack) / oldCost; 
     1436 
    13921437                } 
    13931438                else 
     
    26732718                leaf->mPvs = new ObjectPvs(leaf->GetViewCell()->GetPvs()); 
    26742719 
    2675                 BspMergeCandidate::sOverallCost +=  
    2676                         leaf->mProbability * leaf->mPvs->GetSize(); 
    2677  
     2720                //const float rc = leaf->mProbability * (float)leaf->mPvs->GetSize(); 
     2721                //BspMergeCandidate::sOverallCost += rc; 
     2722                 
    26782723                // the same leaves must not be part of two merge candidates 
    26792724                leaf->Mail(); 
     
    27412786                                new ObjectPvs(leaf->GetViewCell()->GetPvs()); 
    27422787 
    2743                         BspMergeCandidate::sOverallCost +=  
    2744                                 leaf->mProbability * leaf->mPvs->GetSize(); 
     2788                        //BspMergeCandidate::sOverallCost +=  
     2789                        //      leaf->mProbability * leaf->mPvs->GetSize(); 
    27452790                         
    27462791                        ++ numLeaves; 
     
    27672812                                        new ObjectPvs(leaf->GetViewCell()->GetPvs()); 
    27682813                                 
    2769                                 BspMergeCandidate::sOverallCost +=  
    2770                                         leaf->mProbability * leaf->mPvs->GetSize(); 
     2814                                //BspMergeCandidate::sOverallCost +=  
     2815                                //      leaf->mProbability * leaf->mPvs->GetSize(); 
    27712816 
    27722817                                ++ numLeaves; 
     
    28722917 
    28732918 
     2919#if 1 
    28742920int VspBspTree::MergeViewCells(const VssRayContainer &rays, const ObjectContainer &objects) 
    28752921{ 
    2876         BspMergeCandidate::sMaxPvsSize = mViewCellsManager->GetMaxPvsSize(); 
    2877         //BspMergeCandidate::sMinPvsSize = mViewCellsManager->GetMinPvsSize(); 
     2922        BspMergeCandidate::sViewCellsManager = mViewCellsManager; 
    28782923        BspMergeCandidate::sUseArea = mUseAreaForPvs; 
    28792924 
     2925 
     2926        //-- compute statistics values of initial view cells 
     2927        mViewCellsManager->EvaluateRenderStatistics(BspMergeCandidate::sOverallCost,  
     2928                                                                                                BspMergeCandidate::sExpectedCost, 
     2929                                                                                                BspMergeCandidate::sVariance); 
     2930 
     2931 
     2932        //BspMergeCandidate::sExpectedCost =  
     2933        //      BspMergeCandidate::sOverallCost / BspMergeCandidate::sNumViewCells; 
     2934 
     2935 
     2936        ViewCellsManager::PvsStatistics pvsStats; 
     2937        mViewCellsManager->GetPvsStatistics(pvsStats); 
     2938 
     2939        static float expectedValue = pvsStats.avgPvs; 
     2940         
    28802941        // the current view cells are kept in this container 
    28812942        ViewCellContainer viewCells; 
     
    28852946                CollectViewCells(mRoot, true, viewCells, true); 
    28862947        } 
     2948 
     2949 
    28872950        ViewCell::NewMail(); 
    28882951 
     
    28902953        mergeStats.Start(); 
    28912954         
    2892         //BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mBspStats.maxPvs; 
    28932955        long startTime = GetTime(); 
    28942956 
     
    29122974        startTime = GetTime(); 
    29132975 
     2976        // frequency stats are updated 
     2977        const int statsOut = 100; 
     2978 
     2979        // number of view cells withouth the invalid ones 
     2980        BspMergeCandidate::sNumViewCells = mBspStats.Leaves() - mBspStats.invalidLeaves; 
     2981 
     2982        // passes are needed for statistics, because we don't want to record 
     2983        // every merge 
     2984        const int maxMergesPerPass = 100; 
     2985        int pass = 0; 
     2986 
     2987        // maximal ratio of old expected render cost to expected render 
     2988        // when the the render queue has to be reset. 
     2989        const float ercMaxRatio = 0.7f; 
     2990         
     2991        cout << "actual merge starts now ... " << endl; 
     2992 
     2993         
     2994         
     2995        //-- use priority queue to merge leaf pairs 
     2996 
     2997 
     2998        while (!mMergeQueue.empty() &&  
     2999                   (BspMergeCandidate::sNumViewCells > mMergeMinViewCells) && 
     3000                   (mMergeQueue.top().GetMergeCost() < mMergeMaxCostRatio * BspMergeCandidate::sOverallCost)) 
     3001        { 
     3002 
     3003                int mergedPerPass = 0; 
     3004                const float oldExpectedCost = BspMergeCandidate::sExpectedCost; 
     3005                 
     3006//#ifdef _DEBUG 
     3007                Debug << "abs mergecost: " << mMergeQueue.top().GetMergeCost() << " rel mergecost: " 
     3008                          << mMergeQueue.top().GetMergeCost() / BspMergeCandidate::sOverallCost  
     3009                          << " max ratio: " << mMergeMaxCostRatio << endl 
     3010                          << " expected value: " << oldExpectedCost << endl; 
     3011//#endif 
     3012 
     3013                while (!mMergeQueue.empty() &&  
     3014                           (BspMergeCandidate::sNumViewCells > mMergeMinViewCells) && 
     3015                           (ercMaxRatio > oldExpectedCost / BspMergeCandidate::sExpectedCost) && 
     3016                           (mMergeQueue.top().GetMergeCost() < mMergeMaxCostRatio * BspMergeCandidate::sOverallCost) && 
     3017                           (maxMergesPerPass < mergedPerPass)); 
     3018                { 
     3019                                Debug << "erc max ratio" << ercMaxRatio << endl; 
     3020 
     3021                                BspMergeCandidate mc = mMergeQueue.top(); 
     3022                                mMergeQueue.pop(); 
     3023 
     3024                                // both view cells equal! 
     3025                                if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell()) 
     3026                                        continue; 
     3027 
     3028                                if (mc.Valid()) 
     3029                                { 
     3030                                        ViewCell::NewMail(); 
     3031                                        const float currentMergeCost = mc.GetMergeCost(); 
     3032 
     3033                                        MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2()); 
     3034                                         
     3035                                        -- BspMergeCandidate::sNumViewCells; 
     3036                                        ++ mergeStats.merged; 
     3037                                        ++ mergedPerPass; 
     3038 
     3039                                        // increase absolute merge cost 
     3040                                        BspMergeCandidate::sOverallCost += mc.GetRenderCost(); 
     3041                                        BspMergeCandidate::sVariance = mc.GetVarianceIncr(); 
     3042 
     3043                                        BspMergeCandidate::sExpectedCost =  
     3044                                                BspMergeCandidate::sOverallCost / (float)BspMergeCandidate::sNumViewCells; 
     3045 
     3046                                        // stats 
     3047                                        if (mc.GetLeaf1()->IsSibling(mc.GetLeaf2())) 
     3048                                                ++ mergeStats.siblings; 
     3049 
     3050                                        if (0) 
     3051                                        { 
     3052                                                const int dist =  
     3053                                                        TreeDistance(mc.GetLeaf1(), mc.GetLeaf2()); 
     3054                                                if (dist > mergeStats.maxTreeDist) 
     3055                                                        mergeStats.maxTreeDist = dist; 
     3056                                                mergeStats.accTreeDist += dist; 
     3057                                        } 
     3058 
     3059                                        if ((mergeStats.merged % statsOut) == 0) 
     3060                                        { 
     3061                                                cout << "merged " << mergeStats.merged << " view cells" << endl; 
     3062 
     3063                                                mStats  
     3064                                                        << "#Pass\n" << pass << endl 
     3065                                                        << "#Merged\n" << mergeStats.merged << endl  
     3066                                                        << "#Viewcells\n" << BspMergeCandidate::sNumViewCells << endl  
     3067                                                        << "#OverallCost\n" << BspMergeCandidate::sOverallCost << endl 
     3068                                                        << "#CurrentCost\n" << currentMergeCost << endl 
     3069                                                        << "#RelativeCost\n" << currentMergeCost / BspMergeCandidate::sOverallCost << endl 
     3070                                                        << "#CurrentPvs\n" << mc.GetLeaf1()->GetViewCell()->GetPvs().GetSize() << endl 
     3071                                                        << "#MergedSiblings\n" << mergeStats.siblings << endl 
     3072                                                        << "#AvgTreeDist\n" << mergeStats.AvgTreeDist() << endl 
     3073                                                        << "#ExpectedCost\n" << BspMergeCandidate::sExpectedCost << endl 
     3074                                                        << "#RatioExpectedCost\n" << oldExpectedCost / BspMergeCandidate::sExpectedCost << endl 
     3075                                                        << "#variance\n" << BspMergeCandidate::sVariance << endl; 
     3076 
     3077                                                if (mExportMergedViewCells) 
     3078                                                        ExportMergedViewCells(viewCells, objects, BspMergeCandidate::sNumViewCells); 
     3079                                        } 
     3080                                } 
     3081                                else 
     3082                                {  
     3083 
     3084                                        // merge candidate not valid, because one of the leaves was already 
     3085                                        // merged with another one => validate and reinsert into queue 
     3086                                        mc.SetValid(); 
     3087                                        mMergeQueue.push(mc); 
     3088                                } 
     3089                } 
     3090         
     3091 
     3092                ++ pass; 
     3093        } 
     3094 
     3095        mergeStats.overallCost = BspMergeCandidate::sOverallCost; 
     3096 
     3097        mergeStats.mergeTime = TimeDiff(startTime, GetTime()); 
     3098        mergeStats.Stop(); 
     3099 
     3100        Debug << mergeStats << endl << endl; 
     3101         
     3102        // delete the view cells which were already merged 
     3103        CLEAR_CONTAINER(mOldViewCells); 
     3104         
     3105 
     3106        //TODO: should return sample contributions? 
     3107        return mergeStats.merged; 
     3108} 
     3109 
     3110#else 
     3111 
     3112int VspBspTree::MergeViewCells(const VssRayContainer &rays, const ObjectContainer &objects) 
     3113{ 
     3114        BspMergeCandidate::sViewCellsManager = mViewCellsManager; 
     3115        BspMergeCandidate::sUseArea = mUseAreaForPvs; 
     3116 
     3117        ViewCellsManager::PvsStatistics pvsStats; 
     3118        mViewCellsManager->GetPvsStatistics(pvsStats); 
     3119 
     3120        static float expectedValue = pvsStats.avgPvs; 
     3121        // the current view cells are kept in this container 
     3122        ViewCellContainer viewCells; 
     3123        if (mExportMergedViewCells) 
     3124        { 
     3125                ViewCell::NewMail(); 
     3126                CollectViewCells(mRoot, true, viewCells, true); 
     3127        } 
     3128        ViewCell::NewMail(); 
     3129 
     3130        MergeStatistics mergeStats; 
     3131        mergeStats.Start(); 
     3132         
     3133        //BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mBspStats.maxPvs; 
     3134        long startTime = GetTime(); 
     3135 
     3136        cout << "collecting merge candidates ... " << endl; 
     3137         
     3138        if (mUseRaysForMerge) 
     3139        { 
     3140                mergeStats.nodes = CollectMergeCandidates(rays); 
     3141        } 
     3142        else 
     3143        { 
     3144                vector<BspLeaf *> leaves; 
     3145                CollectLeaves(leaves); 
     3146                mergeStats.nodes = CollectMergeCandidates(leaves); 
     3147        } 
     3148         
     3149        cout << "fininshed collecting candidates" << endl; 
     3150 
     3151        mergeStats.collectTime = TimeDiff(startTime, GetTime()); 
     3152        mergeStats.candidates = (int)mMergeQueue.size(); 
     3153        startTime = GetTime(); 
     3154 
     3155         
    29143156        // number of view cells withouth the invalid ones 
    29153157        int nViewCells = mBspStats.Leaves() - mBspStats.invalidLeaves; 
    2916          
    2917          
    2918         // pass is needed for statistics. the last n passes are 
    2919         // recorded 
    2920         const int maxPasses = 1000; 
    2921         const int nextPass = 50; 
    2922  
    2923         int pass = max(nViewCells - mMergeMinViewCells - maxPasses, 0); 
    2924          
     3158        BspMergeCandidate::sExpectedCost = BspMergeCandidate::sOverallCost / (float)nViewCells; 
     3159 
     3160        // passes are needed for statistics, because we don't want to record 
     3161        // every merge 
     3162        const int mergesPerPass = 100; 
     3163         
     3164        int nextPass = 0; 
     3165    int pass = 0; 
     3166         
     3167         
     3168        Debug << "stats: " << nextStats << " " << statsIncr << endl; 
    29253169        cout << "actual merge starts now ... " << endl; 
    29263170 
     
    29393183                mMergeQueue.pop(); 
    29403184 
     3185 
    29413186                // both view cells equal! 
    29423187                if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell()) 
     
    29663211                                        ++ mergeStats.siblings; 
    29673212 
     3213                                if (0) 
    29683214                                const int dist =  
    29693215                                        TreeDistance(mc.GetLeaf1(), mc.GetLeaf2()); 
     
    29723218                                mergeStats.accTreeDist += dist; 
    29733219 
    2974                                 if ((mergeStats.merged == pass) || (nViewCells == mMergeMinViewCells)) 
     3220                                if ((mergeStats.merged == nextPass) || (nViewCells == mMergeMinViewCells)) 
    29753221                                { 
    2976                                         pass += nextPass; 
     3222                                        nextPass += mergesPerPass; 
     3223 
    29773224                                        mStats  
    29783225                                                << "#Pass\n" << pass ++ << endl 
     
    29873234 
    29883235                                                if (mExportMergedViewCells) 
    2989                                                         ExportMergedViewCells(viewCells, objects, nViewCells);    
     3236                                                        ExportMergedViewCells(viewCells, objects, nViewCells); 
    29903237                                } 
    29913238                        } 
     
    30143261        return mergeStats.merged; 
    30153262} 
     3263#endif 
    30163264 
    30173265 
     
    34673715 
    34683716 
    3469 /************************************************************************/ 
    3470 /*                BspMergeCandidate implementation                      */ 
    3471 /************************************************************************/ 
     3717/**************************************************************************/ 
     3718/*                  BspMergeCandidate implementation                      */ 
     3719/**************************************************************************/ 
    34723720 
    34733721 
    34743722BspMergeCandidate::BspMergeCandidate(BspLeaf *l1, BspLeaf *l2): 
    3475 mMergeCost(0), 
     3723mRenderCost(0), 
     3724mVarianceIncr(0), 
    34763725mLeaf1(l1), 
    34773726mLeaf2(l2), 
     
    34833732 
    34843733 
    3485 float BspMergeCandidate::GetCost(ViewCell *vc) const 
     3734float BspMergeCandidate::GetRenderCost(ViewCell *vc) const 
    34863735{ 
    34873736        if (sUseArea) 
     
    34953744{ 
    34963745        BspViewCell *vc = mLeaf1->GetViewCell(); 
    3497         return GetCost(vc); 
     3746 
     3747        return GetRenderCost(vc); 
    34983748} 
    34993749 
     
    35023752{ 
    35033753        BspViewCell *vc = mLeaf2->GetViewCell(); 
    3504         return GetCost(vc); 
     3754 
     3755        return GetRenderCost(vc); 
    35053756} 
    35063757 
     
    35333784 
    35343785 
    3535 inline float BspMergeCandidate::EvalPvsPenalty(int pvs) const 
    3536 { 
    3537         // clamp to minmax values 
    3538         if (pvs > sUpperPvsLimit) 
    3539                 return (float)sUpperPvsLimit; 
    3540         if (pvs < sLowerPvsLimit) 
    3541                 return (float)sLowerPvsLimit; 
    3542  
    3543         return (float)pvs; 
    3544 } 
    3545  
    35463786 
    35473787void BspMergeCandidate::EvalMergeCost() 
     
    35513791        BspViewCell *vc2 = mLeaf2->GetViewCell(); 
    35523792 
    3553         //const int diff1 = vc1->GetPvs().Diff(vc2->GetPvs()); 
    3554         //const int newPvs = diff1 + vc1->GetPvs().GetSize(); 
    35553793        const int newPvs = ComputeMergedPvsSize(vc1->GetPvs(), vc2->GetPvs()); 
    3556         const float f = EvalPvsPenalty(newPvs); 
     3794        const float newPenalty =  
     3795                EvalPvsPenalty(newPvs,  
     3796                                           sViewCellsManager->GetMinPvsSize(), 
     3797                                           sViewCellsManager->GetMaxPvsSize()); 
    35573798 
    35583799        //-- compute ratio of old cost 
     
    35663807 
    35673808 
    3568         if (newPvs > sMaxPvsSize) // strong penalty if pvs size too large 
    3569         { 
    3570                 mMergeCost = 1e15; 
     3809        if (newPvs > sViewCellsManager->GetMaxPvsSize()) // strong penalty if pvs size too large 
     3810        { 
     3811                mRenderCost = 1e15; 
    35713812        } 
    35723813        else 
    35733814        { 
    3574                 mMergeCost = newCost - oldCost; 
    3575         } 
     3815                mRenderCost = newCost - oldCost; 
     3816        } 
     3817 
     3818        // merge cost also takes variance into account 
     3819 
     3820        const float oldVar1 = GetLeaf1Variance(); 
     3821        const float oldVar2 = GetLeaf2Variance(); 
     3822 
     3823        const float newVar = (sExpectedCost - mRenderCost) * (sExpectedCost - mRenderCost); 
     3824 
     3825        mVarianceIncr = (newVar - oldVar1 - oldVar2) / sNumViewCells; 
     3826        //mMergeCost = mRenderCost + fabs(newPenalty - BspMergeCandidate::sExpectedCost) ; 
    35763827} 
    35773828 
     
    35893840 
    35903841 
    3591 BspLeaf *BspMergeCandidate::GetLeaf1() 
     3842BspLeaf *BspMergeCandidate::GetLeaf1() const 
    35923843{ 
    35933844        return mLeaf1; 
     
    35953846 
    35963847 
    3597 BspLeaf *BspMergeCandidate::GetLeaf2() 
     3848BspLeaf *BspMergeCandidate::GetLeaf2() const 
    35983849{ 
    35993850        return mLeaf2; 
     
    36113862float BspMergeCandidate::GetMergeCost() const 
    36123863{ 
    3613         return mMergeCost; 
     3864        return mRenderCost * sRenderCostWeight + mVarianceIncr * (1.0f - sRenderCostWeight); 
     3865} 
     3866 
     3867 
     3868float BspMergeCandidate::GetRenderCost() const 
     3869{ 
     3870        return mRenderCost; 
     3871} 
     3872 
     3873 
     3874float BspMergeCandidate::GetVarianceIncr() const 
     3875{ 
     3876        return mVarianceIncr; 
     3877} 
     3878 
     3879float BspMergeCandidate::GetLeaf1Variance() const 
     3880{ 
     3881        const float leafCost = GetLeaf1Cost(); 
     3882         
     3883        return (sExpectedCost - leafCost) * (sExpectedCost - leafCost); 
     3884} 
     3885 
     3886 
     3887float BspMergeCandidate::GetLeaf2Variance() const 
     3888{ 
     3889        const float leafCost = GetLeaf2Cost(); 
     3890         
     3891        return (sExpectedCost - leafCost) * (sExpectedCost - leafCost); 
    36143892} 
    36153893 
Note: See TracChangeset for help on using the changeset viewer.