Ignore:
Timestamp:
02/01/06 19:29:59 (18 years ago)
Author:
mattausch
Message:

implemented variance
started implementing merge history

File:
1 edited

Legend:

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

    r579 r580  
    3333        mViewSpaceBox.Initialize(); 
    3434        ParseEnvironment(); 
     35 
     36        mViewCellsTree = new ViewCellsTree(this); 
    3537} 
    3638 
     
    5355 
    5456        environment->GetBoolValue("ViewCells.exportToFile", mExportViewCells); 
     57 
     58        environment->GetBoolValue("ViewCells.PostProcess.useRaysForMerge", mUseRaysForMerge); 
    5559 
    5660        mMinPvsSize = emptyViewCells ? 1 : 0; 
     
    7579{ 
    7680        DEL_PTR(mRenderer); 
    77         CLEAR_CONTAINER(mViewCells); 
     81        //CLEAR_CONTAINER(mViewCells); 
     82        DEL_PTR(mViewCellsTree); 
     83 
    7884        CLEAR_CONTAINER(mMeshContainer); 
    7985} 
     
    302308float ViewCellsManager::GetViewSpaceVolume() 
    303309{ 
    304         return mViewSpaceBox.GetVolume()*(2.0f*sqr(M_PI)); 
     310        return mViewSpaceBox.GetVolume()*(2.0f*sqr((float)M_PI)); 
    305311} 
    306312 
     
    356362 
    357363 
    358 void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost,  
     364void ViewCellsManager::EvaluateRenderStatistics(float &totalRenderCost, 
    359365                                                                                                float &expectedRenderCost,  
    360                                                                                                 float &variance) 
     366                                                                                                float &deviation, 
     367                                                                                                float &variance, 
     368                                                                                                int &totalPvs, 
     369                                                                                                float &avgRenderCost) 
    361370{ 
    362371        ViewCellContainer::const_iterator it, it_end = mViewCells.end(); 
    363372 
     373 
    364374        //-- compute expected value 
    365375 
    366376        totalRenderCost = 0; 
     377        totalPvs = 0; 
    367378 
    368379        for (it = mViewCells.begin(); it != it_end; ++ it) 
    369380        { 
    370381                ViewCell *vc = *it; 
    371  
    372382                totalRenderCost += vc->GetPvs().GetSize() * vc->GetVolume(); 
    373         } 
    374  
     383                totalPvs += (int)vc->GetPvs().GetSize(); 
     384        } 
     385 
     386        // normalize with view space box 
     387        totalRenderCost /= mViewSpaceBox.GetVolume(); 
    375388        expectedRenderCost = totalRenderCost / (float)mViewCells.size(); 
    376  
    377  
    378         //-- compute variance 
    379          
     389        avgRenderCost = totalPvs / (float)mViewCells.size(); 
     390 
     391 
     392        //-- compute standard defiation 
    380393        variance = 0; 
     394        deviation = 0; 
    381395 
    382396        for (it = mViewCells.begin(); it != it_end; ++ it) 
     
    385399 
    386400                float renderCost = vc->GetPvs().GetSize() * vc->GetVolume(); 
    387  
    388                 const float var = (expectedRenderCost - renderCost) * (expectedRenderCost - renderCost); 
    389  
    390                 variance += var; 
    391         } 
    392          
     401                float dev; 
     402 
     403                if (1) 
     404                        dev = fabs(avgRenderCost - (float)vc->GetPvs().GetSize()); 
     405                else 
     406                        dev = fabs(expectedRenderCost - renderCost); 
     407 
     408                deviation += dev; 
     409                variance += dev * dev; 
     410        } 
     411 
    393412        variance /= (float)mViewCells.size(); 
     413        deviation /= (float)mViewCells.size(); 
    394414} 
    395415 
     
    493513 
    494514 
    495 ViewCell *ViewCellsManager::MergeViewCells(ViewCell &front, ViewCell &back) const 
    496 { 
    497         // generate merged view cell 
    498         ViewCell *vc = GenerateViewCell(); 
     515ViewCellInterior *ViewCellsManager::MergeViewCells(ViewCell &left, ViewCell &right) const 
     516{ 
     517        // generate parent view cell 
     518        ViewCellInterior *vc = new ViewCellInterior();//GenerateViewCell(); 
    499519 
    500520        // merge pvs 
    501         vc->GetPvs().Merge(front.GetPvs(), back.GetPvs()); 
     521        vc->GetPvs().Merge(left.GetPvs(), right.GetPvs()); 
    502522 
    503523        //-- merge ray sets 
    504524        if (0) 
    505525        { 
    506                 stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end()); 
    507                 stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end()); 
    508  
    509                 std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(), 
    510                                    back.mPiercingRays.begin(), back.mPiercingRays.end(), 
     526                stable_sort(left.mPiercingRays.begin(), left.mPiercingRays.end()); 
     527                stable_sort(right.mPiercingRays.begin(), right.mPiercingRays.end()); 
     528 
     529                std::merge(left.mPiercingRays.begin(), left.mPiercingRays.end(), 
     530                                   right.mPiercingRays.begin(), right.mPiercingRays.end(), 
    511531                                   vc->mPiercingRays.begin()); 
    512532        } 
    513533 
     534 
     535        vc->SetupChildLink(&left); 
     536        vc->SetupChildLink(&right); 
     537 
     538 
    514539        return vc; 
    515540} 
     
    522547 
    523548 
    524 ViewCell *ViewCellsManager::GenerateViewCell(Mesh *mesh) const 
    525 { 
    526         return new ViewCell(mesh); 
     549ViewCellsTree *ViewCellsManager::GetViewCellsTree() 
     550{ 
     551        return mViewCellsTree; 
    527552} 
    528553 
     
    814839                { 
    815840                        ExportColor(exporter, *it); 
    816                         ExportVcGeometry(exporter, *it); 
     841                        ExportViewCellGeometry(exporter, *it); 
    817842                } 
    818843        } 
     
    11861211 
    11871212                Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
    1188                           << ", piercing rays=" << (int)vcRays.size() 
    1189                           << ", leaves=" << (int)vc->mLeaves.size() << endl; 
     1213                          << ", piercing rays=" << (int)vcRays.size() << endl; 
     1214                         // << ", leaves=" << (int)vc->mLeaves.size() << endl; 
    11901215 
    11911216 
    11921217                // export rays piercing this view cell 
    1193 #if 0 
    11941218                exporter->ExportRays(vcRays, RgbColor(0, 1, 0)); 
    1195 #else 
    1196                 vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end(); 
    1197                 for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit) 
    1198                         exporter->ExportRays((*lit)->mVssRays); 
    1199 #endif 
     1219 
    12001220                m.mDiffuseColor = RgbColor(1, 0, 0); 
    12011221                exporter->SetForcedMaterial(m); 
     
    12491269                { 
    12501270                        BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit); 
    1251                         ray->intersections.push_back(BspIntersection(0, vc->mLeaves[0])); 
     1271                        ray->intersections.push_back(BspIntersection(0, vc->mLeaf)); 
    12521272                } 
    12531273 
     
    12781298        case 2: // merges 
    12791299                { 
    1280                         BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc); 
    1281  
    1282                         importance = (float)bspVc->mLeaves.size() / 
     1300                        importance = (float)mViewCellsTree->GetSize(vc) / 
    12831301                                (float)mViewCellsStats.maxLeaves; 
    12841302                } 
     
    13011319} 
    13021320 
    1303 void BspViewCellsManager::ExportVcGeometry(Exporter *exporter, 
     1321void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter, 
    13041322                                                                                 ViewCell *vc) const 
    13051323{ 
     
    13321350  return mBspTree->GetViewCell(point); 
    13331351} 
     1352 
     1353 
     1354void BspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates) 
     1355{ 
     1356        // TODO 
     1357} 
     1358 
    13341359 
    13351360/**********************************************************************/ 
     
    15851610} 
    15861611 
    1587  
    1588 void KdViewCellsManager::ExportVcGeometry(Exporter *exporter, 
    1589                                                                                   ViewCell *vc) const 
    1590 { 
    1591         KdViewCell *kdVc = dynamic_cast<KdViewCell *>(vc); 
    1592         vector<KdLeaf *>::const_iterator it, it_end = kdVc->mLeaves.end(); 
    1593  
    1594         for (it = kdVc->mLeaves.begin(); it != it_end; ++ it) 
    1595                 exporter->ExportBox(mKdTree->GetBox(*it)); 
     1612ViewCell *KdViewCellsManager::GenerateViewCell(Mesh *mesh) const 
     1613{ 
     1614        return new KdViewCell(mesh); 
     1615} 
     1616 
     1617 
     1618void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter, 
     1619                                                                                                ViewCell *vc) const 
     1620{ 
     1621        ViewCellContainer leaves; 
     1622 
     1623        mViewCellsTree->CollectLeaves(vc, leaves); 
     1624        ViewCellContainer::const_iterator it, it_end = leaves.end(); 
     1625 
     1626        for (it = leaves.begin(); it != it_end; ++ it) 
     1627        { 
     1628                KdViewCell *kdVc = dynamic_cast<KdViewCell *>(*it); 
     1629         
     1630                exporter->ExportBox(mKdTree->GetBox(kdVc->mLeaf)); 
     1631        } 
    15961632} 
    15971633 
     
    16251661        // TODO 
    16261662} 
     1663 
     1664 
     1665 
     1666void KdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,  
     1667                                                                                                vector<MergeCandidate> &candidates) 
     1668{ 
     1669        // TODO 
     1670} 
     1671 
    16271672 
    16281673/**********************************************************************/ 
     
    17981843                exporter->SetWireframe(); 
    17991844 
    1800                 ExportVcGeometry(exporter, vc); 
     1845                ExportViewCellGeometry(exporter, vc); 
    18011846 
    18021847                //-- export stored rays 
     1848                 
    18031849                if (mExportRays) 
    18041850                { 
    1805                         vector<VspKdLeaf *>::const_iterator it, 
    1806                                 it_end = vc->mLeaves.end(); 
    1807  
    1808                         for (it = vc->mLeaves.begin(); it != it_end; ++ it) 
     1851                        ViewCellContainer leaves; 
     1852                        mViewCellsTree->CollectLeaves(vc, leaves); 
     1853 
     1854                        ViewCellContainer::const_iterator it, 
     1855                                it_end = leaves.end(); 
     1856 
     1857                        for (it = leaves.begin(); it != it_end; ++ it) 
    18091858                        { 
    1810                                 VspKdLeaf *leaf = *it; 
     1859                                VspKdViewCell *vspKdVc = dynamic_cast<VspKdViewCell *>(*it); 
     1860                                VspKdLeaf *leaf = vspKdVc->mLeaf; 
    18111861                                AxisAlignedBox3 box = mVspKdTree->GetBBox(leaf); 
    18121862 
     
    18341884                        } 
    18351885                } 
    1836  
     1886         
    18371887                //-- output PVS of view cell 
    18381888                m.mDiffuseColor = RgbColor(1, 0, 0); 
     
    19271977                } 
    19281978                break; 
    1929         case 2: // merges 
    1930                 { 
    1931             VspKdViewCell *vspKdVc = dynamic_cast<VspKdViewCell *>(vc); 
    1932  
    1933                         importance = (float)vspKdVc->mLeaves.size() / 
     1979        case 2: // merged leaves 
     1980                { 
     1981                int lSize = mViewCellsTree->GetSize(vc); 
     1982                        importance = (float)lSize / 
    19341983                                (float)mViewCellsStats.maxLeaves; 
    19351984                } 
     
    19542003 
    19552004 
    1956 void VspKdViewCellsManager::ExportVcGeometry(Exporter *exporter, 
     2005void VspKdViewCellsManager::ExportViewCellGeometry(Exporter *exporter, 
    19572006                                                                                   ViewCell *vc) const 
    19582007{ 
    19592008        VspKdViewCell *kdVc = dynamic_cast<VspKdViewCell *>(vc); 
    1960         vector<VspKdLeaf *>::const_iterator it, it_end = kdVc->mLeaves.end(); 
    19612009 
    19622010        Mesh m; 
    1963         for (it = kdVc->mLeaves.begin(); it != it_end; ++ it) 
    1964         { 
    1965                 mVspKdTree->GetBBox(*it).AddBoxToMesh(&m); 
     2011 
     2012        ViewCellContainer leaves; 
     2013        mViewCellsTree->CollectLeaves(vc, leaves); 
     2014 
     2015        ViewCellContainer::const_iterator it, it_end = leaves.end(); 
     2016 
     2017        for (it = leaves.begin(); it != it_end; ++ it) 
     2018        { 
     2019                VspKdLeaf *l = dynamic_cast<VspKdViewCell *>(*it)->mLeaf; 
     2020                mVspKdTree->GetBBox(l).AddBoxToMesh(&m); 
    19662021        } 
    19672022 
     
    19752030} 
    19762031 
     2032 
     2033void VspKdViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates) 
     2034{ 
     2035        // TODO 
     2036} 
    19772037 
    19782038 
     
    21312191        long startTime = GetTime(); 
    21322192 
     2193         
    21332194        // TODO: should be done BEFORE the ray casting 
    2134         merged = mVspBspTree->MergeViewCells(rays, objects); 
     2195        merged = mViewCellsTree->ConstructMergeTree(rays, objects); 
    21352196 
    21362197        //-- stats and visualizations 
     
    21942255 
    21952256        // refining the merged view cells 
    2196         const int refined = mVspBspTree->RefineViewCells(rays, objects); 
     2257        const int refined = mViewCellsTree->RefineViewCells(rays, objects); 
    21972258 
    21982259        //-- stats and visualizations 
     
    22342295        mViewCellsStats.Reset(); 
    22352296        EvaluateViewCellsStats(); 
     2297 
    22362298        // has to be recomputed 
    22372299        mTotalAreaValid = false; 
     
    22542316        //-- merge the individual view cells 
    22552317        MergeViewCells(postProcessRays, objects); 
     2318 
    22562319        //-- refines the merged view cells 
    22572320        RefineViewCells(postProcessRays, objects); 
     2321 
     2322        if (1) 
     2323        {                
     2324                float totalCost, erc, var, dev, avg; 
     2325                int totalpvs; 
     2326 
     2327                mViewCellsStats.Reset(); 
     2328 
     2329        EvaluateRenderStatistics(totalCost, erc, dev, var, totalpvs, avg); 
     2330                 
     2331                Debug << "statistics after merge "   
     2332                          << " erc: " << erc  
     2333                          << " dev: " << dev 
     2334                          << " totalpvs: " << totalpvs  
     2335                          << " avg: " << avg << endl; 
     2336        } 
     2337 
    22582338 
    22592339        //-- export shuffled view cells 
     
    22932373                                vm.mDiffuseColor.b -= 0.45f; 
    22942374 
    2295                                 vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end(); 
    2296  
    2297                                 for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit) 
     2375 
     2376                                ViewCellContainer leaves; 
     2377                                mViewCellsTree->CollectLeaves(vc, leaves); 
     2378 
     2379                                ViewCellContainer::const_iterator lit, lit_end = leaves.end(); 
     2380 
     2381                                for (lit = leaves.begin(); lit != lit_end; ++ lit) 
    22982382                                { 
    2299                                         BspLeaf *leaf = *lit; 
     2383                                        BspLeaf *leaf = dynamic_cast<BspViewCell *>(*lit)->mLeaf; 
    23002384 
    23012385                                        if (leaf->Mailed()) 
     
    23912475        if (1) // export view cells 
    23922476        { 
    2393                 cout << "exporting view cells after post process ... "; 
    23942477                Exporter *exporter = Exporter::GetExporter("final_view_cells.x3d"); 
    23952478 
    23962479                if (exporter) 
    23972480                { 
     2481                        cout << "exporting view cells after post process ... "; 
     2482 
    23982483                        if (1) 
    23992484                        { 
     
    24162501                        ExportViewCellsForViz(exporter); 
    24172502                        delete exporter; 
     2503                        cout << "finished" << endl; 
    24182504                } 
    24192505        } 
     
    24462532                                BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit); 
    24472533 
    2448                                 vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end(); 
    2449  
    2450                                 for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit) 
     2534                                ViewCellContainer leaves; 
     2535                                mViewCellsTree->CollectLeaves(vc, leaves); 
     2536 
     2537                                ViewCellContainer::const_iterator lit, lit_end = leaves.end(); 
     2538 
     2539                                for (lit = leaves.begin(); lit != lit_end; ++ lit) 
    24512540                                { 
    2452                                         BspLeaf *leaf = *lit; 
     2541                                        BspLeaf *leaf = dynamic_cast<BspViewCell *>(*lit)->mLeaf; 
    24532542 
    24542543                                        Material m;  
     
    25732662                                { 
    25742663                                        BspViewCell *bspVc = dynamic_cast<BspViewCell *>(ray->mViewCells[j]); 
    2575                                         BspLeaf *leaf = bspVc->mLeaves[0]; 
     2664                                        BspLeaf *leaf = bspVc->mLeaf; 
    25762665                                        if (vc == bspVc) 
    25772666                                                vcRays.push_back(ray); 
     
    25892678                exporter->SetForcedMaterial(m); 
    25902679 
    2591                 ExportVcGeometry(exporter, vc); 
     2680                ExportViewCellGeometry(exporter, vc); 
    25922681 
    25932682 
    25942683                Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
    2595                           << ", piercing rays=" << (int)vcRays.size() 
    2596                           << ", leaves=" << (int)vc->mLeaves.size() << endl; 
     2684                          << ", piercing rays=" << (int)vcRays.size() << endl; 
     2685                        //  << ", leaves=" << (int)vc->mLeaves.size() << endl; 
    25972686 
    25982687                 
     
    26042693                else 
    26052694                { 
    2606                         vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end(); 
    2607  
    2608                         for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit) 
    2609                                 exporter->ExportRays((*lit)->mVssRays); 
     2695 
     2696                        ViewCellContainer leaves; 
     2697                        mViewCellsTree->CollectLeaves(vc, leaves); 
     2698 
     2699                        ViewCellContainer::const_iterator lit, lit_end = leaves.end(); 
     2700 
     2701                        for (lit = leaves.begin(); lit != lit_end; ++ lit) 
     2702                        { 
     2703                                BspLeaf *l = dynamic_cast<BspViewCell *>(*lit)->mLeaf; 
     2704                                exporter->ExportRays(l->mVssRays); 
     2705                        } 
    26102706                } 
    26112707 
     
    26862782        case 2: // merges 
    26872783                { 
    2688             BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc); 
    2689  
    2690                         importance = (float)bspVc->mLeaves.size() / 
    2691                                 (float)mViewCellsStats.maxLeaves; 
     2784            int lSize = mViewCellsTree->GetSize(vc); 
     2785         
     2786                        importance = (float)lSize / (float)mViewCellsStats.maxLeaves; 
    26922787                } 
    26932788                break; 
     
    27132808 
    27142809 
    2715 void VspBspViewCellsManager::ExportVcGeometry(Exporter *exporter, 
     2810void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter, 
    27162811                                                                                          ViewCell *vc) const 
    27172812{ 
     
    27312826int VspBspViewCellsManager::GetMaxTreeDiff(ViewCell *vc) const 
    27322827{ 
    2733         BspViewCell *bspVc = dynamic_cast<BspViewCell *>(vc); 
     2828        ViewCellContainer leaves; 
     2829        mViewCellsTree->CollectLeaves(vc, leaves); 
     2830 
    27342831 
    27352832        int maxDist = 0; 
     2833         
    27362834        // compute max height difference 
    2737         for (int i = 0; i < (int)bspVc->mLeaves.size(); ++ i) 
    2738                 for (int j = 0; j < (int)bspVc->mLeaves.size(); ++ j) 
    2739         { 
    2740                 BspLeaf *leaf = bspVc->mLeaves[i]; 
     2835        for (int i = 0; i < (int)leaves.size(); ++ i) 
     2836                for (int j = 0; j < (int)leaves.size(); ++ j) 
     2837        { 
     2838                BspLeaf *leaf = dynamic_cast<BspViewCell *>(leaves[i])->mLeaf; 
    27412839 
    27422840                if (i != j) 
    27432841                { 
    2744                         BspLeaf *leaf2 = bspVc->mLeaves[j]; 
     2842                        BspLeaf *leaf2 =dynamic_cast<BspViewCell *>(leaves[j])->mLeaf; 
    27452843                        int dist = mVspBspTree->TreeDistance(leaf, leaf2); 
    27462844                        if (dist > maxDist) 
     
    27482846                } 
    27492847        } 
     2848 
    27502849        return maxDist; 
    27512850} 
     
    28712970        CreateMesh(vc); 
    28722971 
    2873         vector<BspLeaf *>::const_iterator it, it_end = vc->mLeaves.end(); 
    2874  
    28752972        float area = 0; 
    28762973        float volume = 0; 
    28772974 
    2878         for (it = vc->mLeaves.begin(); it != it_end; ++ it) 
     2975        ViewCellContainer leaves; 
     2976        mViewCellsTree->CollectLeaves(vc, leaves); 
     2977 
     2978        ViewCellContainer::const_iterator it, it_end = leaves.end(); 
     2979 
     2980        for (it = leaves.begin(); it != it_end; ++ it) 
    28792981        { 
    28802982                BspNodeGeometry geom; 
    2881                 BspLeaf *leaf = *it; 
     2983                BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf; 
    28822984                mVspBspTree->ConstructGeometry(leaf, geom); 
    28832985 
     
    28983000 
    28993001 
    2900 //////////////////////////////////77 
     3002 
     3003void VspBspViewCellsManager::CollectMergeCandidates(const VssRayContainer &rays,  
     3004                                                                                                        vector<MergeCandidate> &candidates) 
     3005{        
     3006        cout << "collecting merge candidates ... " << endl; 
     3007 
     3008        if (mUseRaysForMerge) 
     3009        { 
     3010                mVspBspTree->CollectMergeCandidates(rays, candidates); 
     3011        } 
     3012        else 
     3013        { 
     3014                vector<BspLeaf *> leaves; 
     3015                mVspBspTree->CollectLeaves(leaves); 
     3016                mVspBspTree->CollectMergeCandidates(leaves, candidates); 
     3017        } 
     3018 
     3019        cout << "fininshed collecting candidates" << endl; 
     3020} 
     3021 
     3022 
     3023 
     3024////////////////////////////////// 
    29013025ViewCellsManager *ViewCellsManagerFactory::Create(const string mName) 
    29023026{ 
     
    29043028        return NULL;// new VspBspViewCellsManager(); 
    29053029} 
     3030 
Note: See TracChangeset for help on using the changeset viewer.