Ignore:
Timestamp:
02/02/06 19:16:39 (18 years ago)
Author:
mattausch
Message:

fixed bug in mergueue to find root of merge and sort out doube view cells

File:
1 edited

Legend:

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

    r581 r582  
    308308{ 
    309309        environment->GetBoolValue("ViewCells.Visualization.exportMergedViewCells", mExportMergedViewCells); 
     310        environment->GetFloatValue("ViewCells.maxStaticMemory", mMaxMemory); 
    310311 
    311312        //-- merge options 
     
    314315        environment->GetFloatValue("ViewCells.PostProcess.maxCostRatio", mMergeMaxCostRatio); 
    315316         
     317 
     318        Debug << "========= view cell tree options ================\n"; 
     319        Debug << "minimum view cells: " << mMergeMinViewCells << endl; 
     320        Debug << "max cost ratio: " << mMergeMaxCostRatio << endl; 
     321        Debug << "max memory: " << mMaxMemory << endl; 
     322 
    316323        MergeCandidate::sRenderCostWeight = mRenderCostWeight; 
    317324 
    318325        mStats.open("mergeStats.log"); 
     326} 
     327 
     328 
     329// return memory usage in MB 
     330float ViewCellsTree::GetMemUsage() const 
     331{ 
     332        return 0; 
     333                /*(sizeof(ViewCellsTree) + 
     334                 mBspStats.Leaves() * sizeof(BspLeaf) + 
     335                 mBspStats.Interior() * sizeof(BspInterior) + 
     336                 mBspStats.accumRays * sizeof(RayInfo)) / (1024.0f * 1024.0f);*/ 
    319337} 
    320338 
     
    387405                                                                          const ObjectContainer &objects) 
    388406{ 
    389         // number of view cells equals the number of leaves  
    390         // (without the invalid ones ) 
    391         //mNumViewCells = mBspStats.Leaves();//- mBspStats.invalidLeaves; 
    392         mNumViewCells = (int)mViewCellsManager->GetViewCells().size(); 
     407        mNumActiveViewCells = (int)mViewCellsManager->GetViewCells().size(); 
    393408 
    394409        float variance = 0; 
     
    432447        // view cell manager. They will change with 
    433448        // subsequent merges 
    434         ViewCellContainer &viewCells = mViewCellsManager->GetViewCells(); 
     449        ViewCellContainer &activeViewCells = mViewCellsManager->GetViewCells(); 
    435450 
    436451 
     
    455470        float realExpectedCost = mExpectedCost; 
    456471        float realAvgRenderCost = mAvgRenderCost; 
    457         int realNumViewCells = mNumViewCells; 
     472        int realNumActiveViewCells = mNumActiveViewCells; 
    458473         
    459474        // maximal ratio of old expected render cost to expected render 
     
    461476        float avgCostMaxDeviation; 
    462477        int maxMergesPerPass; 
    463         int numActiveViewCells = 0; 
     478        int numMergedViewCells = 0; 
    464479 
    465480        environment->GetIntValue("ViewCells.PostProcess.maxMergesPerPass", maxMergesPerPass); 
     
    472487        //-- use priority queue to merge leaf pairs 
    473488 
    474         while (!mMergeQueue.empty() && (realNumViewCells > mMergeMinViewCells)) 
     489        while (!mMergeQueue.empty())// && (realNumActiveViewCells > mMergeMinViewCells)) 
    475490        { 
    476491                //-- reset merge queue if the ratio of current expected cost / real expected cost 
     
    495510                        mExpectedCost = realExpectedCost; 
    496511                        mAvgRenderCost = realAvgRenderCost; 
    497                         mNumViewCells = realNumViewCells; 
     512                        mNumActiveViewCells = realNumActiveViewCells; 
    498513                         
    499                         const int numActiveViewCells = UpdateMergedViewCells(viewCells); 
    500  
     514                        const int numMergedViewCells = UpdateActiveViewCells(activeViewCells); 
     515                 
     516                        // recompute priorities => reset render cost 
    501517                    ResetMergeQueue(); 
    502518 
     
    509525                        if (mExportMergedViewCells) 
    510526                        { 
    511                                 ExportMergedViewCells(viewCells, objects, numActiveViewCells); 
     527                                ExportMergedViewCells(activeViewCells, objects, numMergedViewCells); 
    512528                        } 
    513529                } 
     
    524540                mMergeQueue.pop(); 
    525541         
    526                 // both view cells equal! 
     542                // both view cells equal 
     543                // NOTE: do I really still need this? probably cannot happen!! 
    527544                if (mc.mLeftViewCell == mc.mRightViewCell) 
    528545                        continue; 
     
    532549                        ViewCell::NewMail(); 
    533550                                                 
    534                         -- realNumViewCells; 
     551                        -- realNumActiveViewCells; 
    535552                        ++ mergeStats.merged; 
    536553                        ++ mergedPerPass; 
     
    547564                        mDeviation += mc.GetDeviationIncr(); 
    548565                                                 
    549                         realExpectedCost = totalCost / (float)realNumViewCells; 
     566                        realExpectedCost = totalCost / (float)realNumActiveViewCells; 
    550567                         
    551568                        const float currentMergeCost = mc.GetMergeCost(); 
     
    557574 
    558575                        totalPvs += pvsDiff; 
     576 
    559577                        // set timestamp 
    560578                        mergedVc->SetTimeStamp(mergeStats.merged); 
    561579 
    562                         realAvgRenderCost = (float)totalPvs / (float)realNumViewCells; 
     580                        realAvgRenderCost = (float)totalPvs / (float)realNumActiveViewCells; 
    563581#if VC_HISTORY 
    564582                        if (mc.mLeftViewCell->IsSibling(mc.mRightViewCell)) 
     
    566584#endif 
    567585                        if (((mergeStats.merged % statsOut) == 0) ||  
    568                                 (realNumViewCells == mMergeMinViewCells)) 
     586                                (realNumActiveViewCells == mMergeMinViewCells)) 
    569587                        { 
    570588                                cout << "merged " << mergeStats.merged << " view cells" << endl; 
     
    573591                                        << "#Pass\n" << pass << endl 
    574592                                        << "#Merged\n" << mergeStats.merged << endl  
    575                                         << "#Viewcells\n" << realNumViewCells << endl  
     593                                        << "#Viewcells\n" << realNumActiveViewCells << endl  
    576594                                        << "#CurrentCost\n" << currentMergeCost << endl 
    577595                                        << "#RelativeCost\n" << currentMergeCost / mOverallCost << endl 
     
    584602                                        << "#AvgRenderCost\n" << mAvgRenderCost << endl 
    585603                                        << "#expectedCostRatio\n" << mExpectedCost / realExpectedCost << endl 
    586                                         << "#Deviation\n" << mDeviation / (float)realNumViewCells << endl 
     604                                        << "#Deviation\n" << mDeviation / (float)realNumActiveViewCells << endl 
    587605                                        << "#TotalDeviation\n" << mDeviation<< endl; 
    588606                        } 
     
    592610                        // merge candidate not valid, because one of the leaves was already 
    593611                        // merged with another one => validate and reinsert into queue 
    594                         SetMergeCandidateValid(mc); 
    595                         mMergeQueue.push(mc); 
     612                        if (ValidateMergeCandidate(mc)) 
     613                        { 
     614                                EvalMergeCost(mc); 
     615                                mMergeQueue.push(mc); 
     616                        } 
    596617                } 
    597618        } 
     
    600621        mExpectedCost = realExpectedCost; 
    601622        mAvgRenderCost = realAvgRenderCost; 
    602         mNumViewCells = realNumViewCells; 
    603  
    604         UpdateMergedViewCells(viewCells); 
     623        mNumActiveViewCells = realNumActiveViewCells; 
     624 
     625        UpdateActiveViewCells(activeViewCells); 
    605626        ResetMergeQueue(); 
    606  
    607627 
    608628        // create a root node if the merge was not done till root level, 
    609629        // else take the single node as new root 
    610         if ((int)viewCells.size() > 1) 
    611         { 
    612                 Debug << "here888" << endl; 
    613                 ViewCellInterior *root = new ViewCellInterior(); 
     630        if ((int)activeViewCells.size() > 1) 
     631        { 
     632                Debug << "creating root of view cell hierarchy for " << (int)activeViewCells.size() << " view cells" << endl; 
     633                for (int i = 0;  i < activeViewCells.size(); ++ i){ 
     634                        Debug << "here233 " << activeViewCells[i]->GetParent() << endl; 
     635                        Debug << "here233 " << activeViewCells[i] << endl; 
     636                } 
     637                ViewCellInterior *root = mViewCellsManager->MergeViewCells(activeViewCells); 
    614638 
    615639                root->SetTimeStamp(mergeStats.merged + 1); 
    616  
    617                 ViewCellContainer::const_iterator it, it_end = viewCells.end(); 
    618                 for (it = viewCells.begin(); it != it_end; ++ it) 
    619                         root->SetupChildLink(*it); 
    620  
    621640                mRoot = root; 
    622641        } 
    623         else if ((int)viewCells.size() == 1) 
    624         { 
    625                 Debug << "here555" << endl; 
    626                 mRoot = viewCells[0]; 
     642        else if ((int)activeViewCells.size() == 1) 
     643        { 
     644                Debug << "setting root of the merge history" << endl; 
     645                mRoot = activeViewCells[0]; 
    627646        } 
    628647 
     
    668687                 
    669688                // recalculate cost 
    670                 SetMergeCandidateValid(mc); 
    671                 buf.push_back(mc);                               
     689                if (ValidateMergeCandidate(mc)) 
     690                { 
     691                        EvalMergeCost(mc); 
     692                        buf.push_back(mc);                               
     693                } 
    672694        } 
    673695 
     
    684706 
    685707 
    686 int ViewCellsTree::UpdateMergedViewCells(ViewCellContainer &viewCells) 
    687 { 
    688         int numActiveViewCells = 0; 
    689  
     708int ViewCellsTree::UpdateActiveViewCells(ViewCellContainer &viewCells) 
     709{ 
     710        int numMergedViewCells = 0; 
     711 
     712        Debug << "updating active vc: " << viewCells.size() << endl; 
    690713        // find all already merged view cells and remove them from view cells 
     714                 
     715        // sort out all view cells which are not active anymore, i.e., they 
     716        // were already part of a merge 
    691717        int i = 0; 
    692718 
     719        ViewCell::NewMail(); 
     720 
    693721        while (1) 
    694722        { 
    695                 while (!viewCells.empty() && (!viewCells.back()->GetParent())) 
     723                // remove all merged view cells from end of the vector 
     724                while (!viewCells.empty() && (viewCells.back()->GetParent())) 
    696725                { 
    697726                        viewCells.pop_back(); 
     
    703732 
    704733                // already merged view cell, put it to end of vector 
    705                 if (!viewCells[i]->IsRoot()) 
     734                if (viewCells[i]->GetParent()) 
    706735                        swap(viewCells[i], viewCells.back()); 
    707736                 
    708                 ++ i; 
    709         } 
     737                viewCells[i ++]->Mail(); 
     738        } 
     739 
    710740 
    711741        // add new view cells to container only if they don't have been  
    712742        // merged in the mean time 
    713         while (!mActiveViewCells.empty()) 
    714         { 
    715                 if (!mActiveViewCells.back()->GetParent()) 
    716                 { 
    717                         viewCells.push_back(mActiveViewCells.back()); 
    718                         ++ numActiveViewCells; 
    719                 } 
    720  
    721                 mActiveViewCells.pop_back(); 
    722         } 
     743        ViewCellContainer::const_iterator ait, ait_end = mMergedViewCells.end(); 
     744        for (ait = mMergedViewCells.begin(); ait != ait_end; ++ ait) 
     745        { 
     746                ViewCell *vc = mMergedViewCells.back(); 
     747                if (!vc->GetParent() && !vc->Mailed()) 
     748                { 
     749                        vc->Mail(); 
     750                        viewCells.push_back(vc); 
     751                        ++ numMergedViewCells; 
     752                } 
     753        } 
     754 
     755        mMergedViewCells.clear(); 
    723756 
    724757        // update standard deviation 
     
    737770 
    738771        mDeviation /= (float)viewCells.size(); 
    739  
    740         // clear the view cells which were merged 
    741         mInactiveViewCells.clear(); 
    742         // remove the new view cells 
    743         mActiveViewCells.clear(); 
    744  
    745         return numActiveViewCells; 
     772         
     773        return numMergedViewCells; 
    746774} 
    747775 
     
    749777void ViewCellsTree::ExportMergedViewCells(ViewCellContainer &viewCells,  
    750778                                                                                  const ObjectContainer &objects, 
    751                                                                                   const int numActiveViewCells) 
     779                                                                                  const int numMergedViewCells) 
    752780{ 
    753781         
     
    771799                        // assign special material to new view cells 
    772800                        // new view cells are on the back of container 
    773                         if (i ++ >= (viewCells.size() - numActiveViewCells)) 
     801                        if (i ++ >= (viewCells.size() - numMergedViewCells)) 
    774802                        { 
    775803                                //m = RandomMaterial(); 
     
    798826ViewCellInterior *ViewCellsTree::MergeViewCells(ViewCell *l, ViewCell *r, int &pvsDiff) //const 
    799827{ 
    800         ViewCellInterior *vc = mViewCellsManager->MergeViewCells(*l, *r); 
     828        ViewCellInterior *vc = mViewCellsManager->MergeViewCells(l, r); 
    801829 
    802830        // if merge was unsuccessful 
     
    817845 
    818846 
    819         //-- clean up old view cells 
    820         if (0 && !mExportMergedViewCells) 
    821         { 
    822                 DEL_PTR(l); 
    823                 DEL_PTR(r); 
    824         } 
    825         else  
    826         { 
    827                 mInactiveViewCells.push_back(l); 
    828                 mInactiveViewCells.push_back(r); 
    829                  
    830                 mActiveViewCells.push_back(vc); 
    831         } 
     847        // new view cells are stored in this vector 
     848        mMergedViewCells.push_back(vc); 
    832849 
    833850        pvsDiff = vc->GetPvs().GetSize() - pvs1 - pvs2; 
     
    10181035         
    10191036        return mRenderCostWeight * (renderCost1 + renderCost2) + 
    1020                   (1.0f - mRenderCostWeight) * (dev1 + dev2) / (float)mNumViewCells; 
     1037                  (1.0f - mRenderCostWeight) * (dev1 + dev2) / (float)mNumActiveViewCells; 
    10211038} 
    10221039 
     
    11121129        { 
    11131130                const float penalty = EvalPvsPenalty(vc->GetPvs().GetSize(), lower, upper); 
    1114                 return (mAvgRenderCost - penalty) * (mAvgRenderCost - penalty) / (float)mNumViewCells; 
     1131                return (mAvgRenderCost - penalty) * (mAvgRenderCost - penalty) / (float)mNumActiveViewCells; 
    11151132        } 
    11161133 
     
    11281145        { 
    11291146                const float penalty = EvalPvsPenalty(vc->GetPvs().GetSize(), lower, upper); 
    1130                 return fabs(mAvgRenderCost - penalty) / (float)mNumViewCells; 
     1147                return fabs(mAvgRenderCost - penalty) / (float)mNumActiveViewCells; 
    11311148        } 
    11321149 
     
    11531170 
    11541171 
    1155 void ViewCellsTree::SetMergeCandidateValid(MergeCandidate &mc) const 
     1172bool ViewCellsTree::ValidateMergeCandidate(MergeCandidate &mc) const 
    11561173{ 
    11571174        while (mc.mLeftViewCell->mParent) 
     
    11651182        } 
    11661183 
    1167         EvalMergeCost(mc); 
     1184        return mc.mLeftViewCell != mc.mRightViewCell; 
    11681185} 
    11691186 
     
    12101227 
    12111228        if (1) 
    1212                 newDev = fabs(mAvgRenderCost - newPenalty) / (float)mNumViewCells; 
     1229                newDev = fabs(mAvgRenderCost - newPenalty) / (float)mNumActiveViewCells; 
    12131230        else 
    1214                 newDev = fabs(mExpectedCost - newCost) / (float)mNumViewCells; 
     1231                newDev = fabs(mExpectedCost - newCost) / (float)mNumActiveViewCells; 
    12151232         
    12161233        oldDev = GetDeviation(vc1) + GetDeviation(vc2); 
     
    12511268        TraversalQueue tqueue; 
    12521269        tqueue.push(mRoot); 
    1253         Debug << "here34 " << numViewCells << endl; 
     1270         
    12541271        while (!tqueue.empty()) 
    12551272        { 
    12561273                ViewCell *vc = tqueue.top(); 
    1257                 tqueue.pop(); 
    1258                 Debug << "here7 " << vc->GetTimeStamp() << endl; 
    1259                 if (vc->IsLeaf()) 
    1260                 { 
    1261                         Debug << "here6" << endl; 
     1274                 
     1275                // save the view cells if it is a leaf or if enough view cells have already been traversed 
     1276                // because of the priority queue, this will be the optimal set of v 
     1277                if (vc->IsLeaf() || ((viewCells.size() + tqueue.size()) >= numViewCells)) 
     1278                { 
    12621279                        viewCells.push_back(vc); 
    12631280                } 
    1264                 else if (viewCells.size() + tqueue.size() < numViewCells) 
    1265                 {       Debug << "here4" << endl; 
     1281                else  
     1282                {        
    12661283                        ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc); 
    12671284 
     
    12731290                        } 
    12741291                } 
     1292 
     1293                tqueue.pop(); 
    12751294        } 
    12761295} 
     
    13901409bool MergeCandidate::IsValid() const 
    13911410{ 
    1392         return !(mLeftViewCell->mParent && mRightViewCell->mParent); 
     1411        return !(mLeftViewCell->mParent || mRightViewCell->mParent); 
    13931412} 
    13941413 
Note: See TracChangeset for help on using the changeset viewer.