Ignore:
Timestamp:
11/16/06 15:41:44 (18 years ago)
Author:
bittner
Message:

filter updates, kd + bvh PVS coexistence

File:
1 edited

Legend:

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

    r1760 r1761  
    3232 
    3333// $$JB HACK 
    34 #define USE_KD_PVS 0 
    35 #define KD_PVS_AREA (5*1e-4f) 
     34#define USE_KD_PVS 1 
     35#define KD_PVS_AREA (1e-4f) 
    3636 
    3737 
     
    8787{ 
    8888        // visualization stuff 
    89         Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportRays", mExportRays); 
     89        Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportRays", mExportRays); 
    9090        Environment::GetSingleton()->GetBoolValue("ViewCells.Visualization.exportGeometry", mExportGeometry); 
    9191        Environment::GetSingleton()->GetFloatValue("ViewCells.maxPvsRatio", mMaxPvsRatio); 
     
    317317ViewCellsManager::GetIntersectable(const VssRay &ray, const bool isTermination) const 
    318318{ 
     319#if USE_KD_PVS 
     320  float area = GetPreprocessor()->mKdTree->GetBox().SurfaceArea()*KD_PVS_AREA; 
     321  KdNode *node = GetPreprocessor()->mKdTree->GetNode(isTermination ? 
     322                                                                                                         ray.mTermination : ray.mOrigin, 
     323                                                                                                         area); 
     324  return  
     325        GetPreprocessor()->mKdTree-> 
     326        GetOrCreateKdIntersectable(node); 
     327#else 
    319328  return isTermination ? ray.mTerminationObject : ray.mOriginObject; 
     329#endif 
    320330} 
    321331 
     
    794804                stream << "<BoundingBoxes>" << endl; 
    795805#if USE_KD_PVS  
    796         KdIntersectableMap::const_iterator kit, kit_end = GetPreprocessor()->mKdTree->mKdIntersectables.end(); 
     806                vector<KdIntersectable *>::iterator kit, kit_end = GetPreprocessor()->mKdTree->mKdIntersectables.end(); 
    797807 
    798808                int id = 0; 
    799809                for (kit = GetPreprocessor()->mKdTree->mKdIntersectables.begin(); kit != kit_end; ++ kit, ++ id) 
    800810                { 
    801                         Intersectable *obj = (*kit).second; 
    802                         const AxisAlignedBox3 box = obj->GetBox(); 
    803                  
    804                         obj->SetId(id); 
    805  
    806                         stream << "<BoundingBox" << " id=\"" << id << "\"" 
    807                                    << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\"" 
    808                                    << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl; 
     811                  Intersectable *obj = *kit; 
     812                  const AxisAlignedBox3 box = obj->GetBox(); 
     813                   
     814                  obj->SetId(id); 
     815                   
     816                  stream << "<BoundingBox" << " id=\"" << id << "\"" 
     817                                 << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\"" 
     818                                 << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl; 
    809819                } 
    810820#else 
    811         ObjectContainer::const_iterator oit, oit_end = objects.end(); 
    812  
     821                ObjectContainer::const_iterator oit, oit_end = objects.end(); 
     822                 
    813823                for (oit = objects.begin(); oit != oit_end; ++ oit) 
    814824                { 
     
    955965                                                                          float &contribution) const 
    956966{ 
    957         if (!obj) return false; 
    958  
     967        if (!obj) 
     968          return false; 
     969         
    959970        // potentially visible objects 
    960971        return vc->AddPvsSample(obj, pdf, contribution); 
     
    14011412                                                                                ) 
    14021413{ 
    1403         sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs); 
    1404  
    1405         int start = (int)(mViewCells.size() * minValid); 
    1406         int end = (int)(mViewCells.size() * maxValid); 
    1407  
    1408         for (int i = 0; i < (int)mViewCells.size(); ++ i) 
    1409         { 
    1410                 mViewCells[i]->SetValid(i >= start && i <= end); 
     1414  ObjectPvs dummyPvs; 
     1415  // update pvs sizes 
     1416  for (int i = 0; i < (int)mViewCells.size(); ++ i) { 
     1417        UpdatePvsForEvaluation(mViewCells[i], dummyPvs); 
     1418  } 
     1419   
     1420  sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs); 
     1421   
     1422  int start = (int)(mViewCells.size() * minValid); 
     1423  int end = (int)(mViewCells.size() * maxValid); 
     1424   
     1425  for (int i = 0; i < (int)mViewCells.size(); ++ i) 
     1426        { 
     1427          //      cout<<"pvs:"<<mViewCells[i]->GetCachedPvsCost()<<endl; 
     1428          mViewCells[i]->SetValid(i >= start && i <= end); 
    14111429        } 
    14121430} 
     
    18211839  stat.avgFilteredPvs = 0.0f; 
    18221840  stat.avgFilterContribution = 0.0f; 
     1841  stat.avgFilterRadius = 0; 
     1842  stat.avgFilterRatio = 0; 
    18231843   
    18241844  for (; it != mViewCells.end(); ++ it)  
    18251845        { 
    18261846          ViewCell *viewcell = *it; 
    1827            
    1828           const float pvsCost = mViewCellsTree->GetPvsCost(viewcell); 
    1829            
    1830           if (pvsCost < stat.minPvs) 
    1831                 stat.minPvs = pvsCost; 
    1832           if (pvsCost > stat.maxPvs) 
    1833                 stat.maxPvs = pvsCost; 
    1834            
    1835           stat.avgPvs += pvsCost; 
    1836  
    1837           const bool evaluateFilter = true; 
    1838            
    1839           if (evaluateFilter) { 
    1840                 ObjectPvs filteredPvs = viewcell->GetPvs(); 
    1841                 ApplyFilter2(viewcell, false, 1.0f, filteredPvs); 
    1842                 float filteredCost = filteredPvs.EvalPvsCost(); 
    1843                 stat.avgFilteredPvs += filteredCost; 
    1844                 stat.avgFilterContribution += filteredCost - pvsCost; 
    1845           } else { 
    1846                 stat.avgFilteredPvs += pvsCost; 
    1847                 stat.avgFilterContribution += 0; 
     1847          if (viewcell->GetValid()) { 
     1848                const float pvsCost = mViewCellsTree->GetPvsCost(viewcell); 
     1849                 
     1850                if (pvsCost < stat.minPvs) 
     1851                  stat.minPvs = pvsCost; 
     1852                if (pvsCost > stat.maxPvs) 
     1853                  stat.maxPvs = pvsCost; 
     1854                 
     1855                stat.avgPvs += pvsCost; 
     1856                 
     1857                const bool evaluateFilter = true; 
     1858                 
     1859                if (evaluateFilter) { 
     1860                  ObjectPvs filteredPvs = viewcell->GetPvs(); 
     1861                  PvsFilterStatistics fstat = ApplyFilter2(viewcell, false, 1.0f, filteredPvs); 
     1862                   
     1863                  float filteredCost = filteredPvs.EvalPvsCost(); 
     1864                  stat.avgFilteredPvs += filteredCost; 
     1865                  stat.avgFilterContribution += filteredCost - pvsCost; 
     1866 
     1867                  stat.avgFilterRadius += fstat.mAvgFilterRadius; 
     1868                  stat.avgFilterRatio += fstat.mLocalFilterCount / (float) fstat.mGlobalFilterCount; 
     1869                   
     1870                   
     1871                } else { 
     1872                  stat.avgFilteredPvs += pvsCost; 
     1873                  stat.avgFilterContribution += 0; 
     1874                } 
     1875                 
     1876                ++ stat.viewcells; 
    18481877          } 
    1849            
    1850           ++ stat.viewcells; 
    18511878        } 
    18521879   
     
    18551882        stat.avgFilteredPvs/=stat.viewcells; 
    18561883        stat.avgFilterContribution/=stat.viewcells; 
     1884        stat.avgFilterRadius/=stat.viewcells; 
     1885        stat.avgFilterRatio/=stat.viewcells; 
    18571886  } 
    18581887} 
     
    18671896  s<<"#AVG_FILTERED_PVS\n"<<pvsStat.avgFilteredPvs<<endl; 
    18681897  s<<"#AVG_FILTER_CONTRIBUTION\n"<<pvsStat.avgFilterContribution<<endl; 
     1898  s<<"#AVG_FILTER_RADIUS\n"<<pvsStat.avgFilterRadius<<endl; 
     1899  s<<"#AVG_FILTER_RATIO\n"<<pvsStat.avgFilterRatio<<endl; 
    18691900  s<<"#MAX_PVS\n"<<pvsStat.maxPvs<<endl; 
    18701901  s<<"#MIN_PVS\n"<<pvsStat.minPvs<<endl; 
     
    19852016        return; 
    19862017   
    1987 #if USE_KD_PVS 
    1988   float area = GetPreprocessor()->mKdTree->GetBox().SurfaceArea()*KD_PVS_AREA; 
    1989   KdNode *node = GetPreprocessor()->mKdTree->GetNode(ray.mTermination, area); 
    1990   Intersectable *obj = 
    1991         GetPreprocessor()->mKdTree-> 
    1992         GetOrCreateKdIntersectable(node); 
    1993 #else 
    1994   Intersectable *obj = ray.mTerminationObject; 
    1995 #endif 
     2018  Intersectable *obj = GetIntersectable(ray, true); 
    19962019   
    19972020  for (it = viewcells->begin(); it != viewcells->end(); ++it) { 
     
    20372060                ray.mViewCells = viewcells; 
    20382061        } 
    2039  
    2040 #if USE_KD_PVS 
    2041         float area = GetPreprocessor()->mKdTree->GetBox().SurfaceArea()*KD_PVS_AREA; 
    2042         KdNode *node = GetPreprocessor()->mKdTree->GetNode(ray.mTermination, area); 
    2043         Intersectable *obj = 
    2044           GetPreprocessor()->mKdTree-> 
    2045           GetOrCreateKdIntersectable(node); 
    2046 #else 
    2047         Intersectable *obj = ray.mTerminationObject; 
    2048 #endif 
     2062         
     2063        Intersectable *obj = GetIntersectable(ray, true); 
    20492064         
    20502065         
     
    25902605 
    25912606 
    2592 void 
     2607PvsFilterStatistics 
    25932608ViewCellsManager::ApplyFilter2(ViewCell *viewCell, 
    25942609                                                           const bool useViewSpaceFilter, 
     
    25972612                                                           ) 
    25982613{ 
     2614  PvsFilterStatistics stats; 
     2615 
    25992616  AxisAlignedBox3 vbox = GetViewCellBox(viewCell); 
    26002617  Vector3 center = vbox.Center(); 
    2601    
    26022618  // COpy the PVS 
    26032619  ObjectPvs basePvs = viewCell->GetPvs(); 
     
    26062622  ObjectPvsIterator pit = basePvs.GetIterator(); 
    26072623 
     2624#if !USE_KD_PVS 
    26082625  // first mark all object from this pvs 
    26092626  while (pit.HasMoreEntries()) {                 
     
    26132630        object->Mail(); 
    26142631  } 
    2615  
     2632#endif 
    26162633 
    26172634  int pvsSize = 0; 
    26182635  int nPvsSize = 0; 
    2619   float samples = (float)pvs.GetSamples(); 
    2620  
    2621   //  cout<<"#s"<<samples<<endl; 
    2622   //  cout<<"pvs size = "<<pvs.GetSize() <<endl; 
     2636  float samples = (float)basePvs.GetSamples(); 
     2637   
     2638  cout<<"f #s="<<samples<<"pvs size = "<<basePvs.GetSize(); 
    26232639  //  cout<<"Filter size = "<<filterSize<<endl; 
    26242640  //  cout<<"vbox = "<<vbox<<endl; 
     
    26352651#define MIN_LOCAL_SAMPLES 5 
    26362652 
    2637   float globalC = 2.0f*filterSize/sqrt(samples); 
    26382653  float viewCellRadius = 0.5f*Magnitude(vbox.Diagonal()); 
    26392654   
     
    26592674        // update samples and globalC 
    26602675        samples = (float)pvs.GetSamples(); 
    2661         globalC = 2.0f*filterSize/sqrt(samples); 
    26622676        //      cout<<"neighboring viewcells = "<<i-1<<endl; 
    26632677        //      cout<<"Samples' = "<<samples<<endl; 
    26642678  } 
     2679 
     2680  // Minimal number of samples so that filtering takes place 
     2681#define MIN_SAMPLES  100 
     2682  if (samples > MIN_SAMPLES) { 
     2683        float globalC = 2.0f*filterSize/sqrt(samples); 
     2684         
     2685        pit = basePvs.GetIterator(); 
     2686         
     2687        ObjectContainer objects; 
     2688         
     2689        while (pit.HasMoreEntries()) 
     2690          {              
     2691                ObjectPvsEntry entry = pit.Next(); 
     2692                 
     2693                Intersectable *object = entry.mObject; 
     2694                // compute filter size based on the distance and the numebr of samples 
     2695                AxisAlignedBox3 box = object->GetBox(); 
     2696                 
     2697                float distance = Distance(center, box.Center()); 
     2698                float globalRadius = distance*globalC; 
     2699                 
     2700                int objectSamples = (int)entry.mData.mSumPdf; 
     2701                float localRadius = MAX_FLOAT; 
     2702                if (objectSamples > MIN_LOCAL_SAMPLES) 
     2703                  localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/ 
     2704                        sqrt((float)objectSamples); 
     2705                 
     2706                //      cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl; 
     2707                 
     2708                // now compute the filter size 
     2709                float radius; 
     2710 
     2711                if (localRadius < globalRadius) { 
     2712                  radius = localRadius; 
     2713                  stats.mLocalFilterCount++; 
     2714                } else { 
     2715                  radius = globalRadius; 
     2716                  stats.mGlobalFilterCount++; 
     2717                } 
     2718 
     2719                stats.mAvgFilterRadius += radius; 
     2720                 
     2721                // cout<<"box = "<<box<<endl; 
     2722                //      cout<<"distance = "<<distance<<endl; 
     2723                //      cout<<"radiues = "<<radius<<endl; 
     2724                 
     2725                box.Enlarge(Vector3(radius)); 
     2726                 
     2727                objects.clear(); 
     2728                // $$ warning collect objects takes only unmailed ones! 
     2729                CollectObjects(box, objects); 
     2730                //      cout<<"collected objects="<<objects.size()<<endl; 
     2731                ObjectContainer::const_iterator noi = objects.begin(); 
     2732                for (; noi != objects.end(); ++ noi) { 
     2733                  Intersectable *o = *noi; 
     2734                  // $$ JB warning: pdfs are not correct at this point!    
     2735                  pvs.AddSampleDirty(o, Limits::Small); 
     2736                } 
     2737          } 
     2738        stats.mAvgFilterRadius /= (stats.mLocalFilterCount + stats.mGlobalFilterCount); 
     2739  } 
    26652740   
    2666   pit = basePvs.GetIterator(); 
    2667  
    2668   ObjectContainer objects; 
    2669  
    2670   while (pit.HasMoreEntries()) 
    2671   {              
    2672         ObjectPvsEntry entry = pit.Next(); 
    2673  
    2674         Intersectable *object = entry.mObject; 
    2675         // compute filter size based on the distance and the numebr of samples 
    2676         AxisAlignedBox3 box = object->GetBox(); 
    2677          
    2678         float distance = Distance(center, box.Center()); 
    2679         float globalRadius = distance*globalC; 
    2680          
    2681         int objectSamples = (int)entry.mData.mSumPdf; 
    2682         float localRadius = MAX_FLOAT; 
    2683         if (objectSamples > MIN_LOCAL_SAMPLES) 
    2684           localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/ 
    2685                 sqrt((float)objectSamples); 
    2686          
    2687         //      cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl; 
    2688          
    2689         // now compute the filter size 
    2690         float radius = Min(localRadius, globalRadius); 
    2691          
    2692         // cout<<"box = "<<box<<endl; 
    2693         //      cout<<"distance = "<<distance<<endl; 
    2694         //      cout<<"radiues = "<<radius<<endl; 
    2695          
    2696         box.Enlarge(Vector3(radius)); 
    2697          
    2698         objects.clear(); 
    2699         // $$ warning collect objects takes only unmailed ones! 
    2700         CollectObjects(box, objects); 
    2701         //      cout<<"collected objects="<<objects.size()<<endl; 
    2702         ObjectContainer::const_iterator noi = objects.begin(); 
    2703         for (; noi != objects.end(); ++ noi) { 
    2704           Intersectable *o = *noi; 
    2705           // $$ JB warning: pdfs are not correct at this point!    
    2706           pvs.AddSampleDirty(o, Limits::Small); 
    2707         } 
    2708   } 
    2709  
    2710   //  cout<<"nPvs size = "<<pvs.GetSize()<<endl; 
    2711  
     2741  cout<<" nPvs size = "<<pvs.GetSize()<<endl; 
     2742 
     2743#if !USE_KD_PVS 
    27122744  // copy the base pvs to the new pvs 
    27132745  pit = basePvs.GetIterator(); 
     
    27162748        pvs.AddSampleDirty(entry.mObject, entry.mData.mSumPdf); 
    27172749  } 
     2750#endif 
    27182751   
    27192752  Intersectable::NewMail(); 
     2753  return stats; 
    27202754} 
    27212755   
     
    50965130VspOspViewCellsManager::GetIntersectable(const VssRay &ray, const bool isTermination) const 
    50975131{ 
     5132#if USE_KD_PVS 
     5133  return ViewCellsManager::GetIntersectable(ray, isTermination); 
     5134#else 
    50985135  return mHierarchyManager->GetIntersectable(ray, isTermination); 
     5136#endif 
    50995137} 
    51005138 
     
    58535891        ViewCellContainer::const_iterator it = viewcells.begin(); 
    58545892 
     5893        Intersectable *terminationObj = GetIntersectable(ray, true); 
     5894        Intersectable *originObj = GetIntersectable(ray, false); 
     5895           
    58555896        for (; it != viewcells.end(); ++ it)  
    58565897        { 
     
    58615902                        float contribution; 
    58625903 
    5863                         if (ray.mTerminationObject)  
     5904                        if (terminationObj)  
    58645905                        { 
    58655906                                // todo: maybe not correct for kd node pvs 
    5866                                 Intersectable *obj = mHierarchyManager->GetIntersectable(ray, true); 
    5867  
    5868                                 if (viewcell->GetPvs().GetSampleContribution(obj, 
    5869                                         ray.mPdf,  
    5870                                         contribution)) 
     5907                           
     5908                          if (viewcell->GetPvs().GetSampleContribution(terminationObj, 
     5909                                                                                                                   ray.mPdf,  
     5910                                                                                                                   contribution)) 
    58715911                                { 
    5872                                         ++ ray.mPvsContribution; 
     5912                                  ++ ray.mPvsContribution; 
    58735913                                } 
    5874  
    5875                                 ray.mRelativePvsContribution += contribution; 
     5914                           
     5915                          ray.mRelativePvsContribution += contribution; 
    58765916                        } 
    5877  
     5917                         
    58785918                        //////////////// 
    58795919                        //-- for directional sampling it is important to count only contributions  
     
    58815921                        //-- the other contributions of this sample will be counted for the opposite ray! 
    58825922#if SAMPLE_ORIGIN_OBJECTS 
    5883                         if (ray.mOriginObject &&  
    5884                                 viewcell->GetPvs().GetSampleContribution(ray.mOriginObject, 
    5885                                 ray.mPdf, 
    5886                                 contribution)) 
    5887                         { 
     5923                        if (originObj &&  
     5924                                viewcell->GetPvs().GetSampleContribution(originObj, 
     5925                                                                                                                 ray.mPdf, 
     5926                                                                                                                 contribution)) 
     5927                          { 
    58885928                                ++ ray.mPvsContribution; 
    58895929                                ray.mRelativePvsContribution += contribution; 
    5890                         } 
     5930                          } 
    58915931#endif 
    58925932                } 
     
    59065946                        break; 
    59075947 
     5948                //$$JB hack 
     5949#if !USE_KD_PVS 
    59085950                AddSampleToPvs( 
    5909                         ray.mTerminationObject,  
    5910                         ray.mTermination,  
    5911                         viewCell,  
    5912                         ray.mPdf,  
    5913                         ray.mRelativePvsContribution); 
    5914  
     5951                                           terminationObj,  
     5952                                           ray.mTermination,  
     5953                                           viewCell,  
     5954                                           ray.mPdf,  
     5955                                           ray.mRelativePvsContribution); 
     5956#else 
     5957                viewCell->GetPvs().AddSample(terminationObj, ray.mPdf); 
     5958#endif 
     5959                 
    59155960#if SAMPLE_ORIGIN_OBJECTS 
    5916  
    59175961                AddSampleToPvs( 
    5918                         ray.mOriginObject,  
    5919                         ray.mOrigin,  
    5920                         viewCell,  
    5921                         ray.mPdf,  
    5922                         ray.mRelativePvsContribution); 
     5962                                           originObj,  
     5963                                           ray.mOrigin,  
     5964                                           viewCell,  
     5965                                           ray.mPdf,  
     5966                                           ray.mRelativePvsContribution); 
    59235967#endif                   
    59245968        } 
    5925  
     5969         
    59265970         
    59275971 
     
    59375981                                                                                        float &contribution) const 
    59385982{ 
    5939         // The hierarchy manager decides about the type of sample cast 
    5940         return mHierarchyManager->AddSampleToPvs(obj, hitPoint, vc, pdf, contribution); 
     5983  // The hierarchy manager decides about the type of sample cast 
     5984  return mHierarchyManager->AddSampleToPvs(obj, hitPoint, vc, pdf, contribution); 
    59415985} 
    59425986 
Note: See TracChangeset for help on using the changeset viewer.