Ignore:
Timestamp:
01/03/06 23:33:45 (18 years ago)
Author:
bittner
Message:

Large merge - viewcells seem not functional now

File:
1 edited

Legend:

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

    r490 r492  
    1111#include "ViewCellsManager.h" 
    1212#include "RenderSimulator.h" 
     13#include "GlRenderer.h" 
    1314 
    1415static bool useViewSpaceBox = false; 
     
    3334  environment->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays); 
    3435  environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells); 
    35   environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells); 
     36  environment->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling); 
     37  environment->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling); 
    3638 
    3739  environment->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples); 
     
    3941   
    4042  mStats.open("stats.log"); 
     43 
    4144} 
    4245 
    4346RssPreprocessor::~RssPreprocessor() 
    4447{ 
    45   CLEAR_CONTAINER(mVssRays); 
     48  // mVssRays get deleted in the tree 
     49  //  CLEAR_CONTAINER(mVssRays); 
    4650} 
    4751 
     
    7882  Vector3 pointA, pointB; 
    7983  float bsize = Magnitude(box.Size()); 
     84 
    8085   
    8186  if (mKdTree->CastRay(ray)) { 
     
    8691        // compute intersection with the scene bounding box 
    8792        float tmin, tmax; 
    88         box.ComputeMinMaxT(ray, &tmin, &tmax); 
    89         if (tmax > bsize) { 
    90           //cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl; 
    91           //cerr<<"ray"<<ray<<endl; 
    92         } 
    93         pointA = ray.Extrap(tmax); 
     93        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax) 
     94          pointA = ray.Extrap(tmax); 
     95        else 
     96          return 0; 
    9497  } 
    9598 
     
    108111        objectB = NULL; 
    109112        float tmin, tmax; 
    110         box.ComputeMinMaxT(ray, &tmin, &tmax); 
    111         if (tmax > bsize) { 
    112           //cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl; 
    113           //cerr<<"ray"<<ray<<endl; 
    114         } 
    115         pointB = ray.Extrap(tmax); 
    116   } 
    117  
     113        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax) 
     114          pointB = ray.Extrap(tmax); 
     115        else 
     116          return 0; 
     117  } 
     118 
     119  //  if (objectA == NULL && objectB != NULL) { 
     120  if (1) { 
     121        // cast again to ensure that there is no objectA 
     122        SetupRay(ray, pointB, direction); 
     123        if (mKdTree->CastRay(ray)) { 
     124          objectA = ray.intersections[0].mObject; 
     125          pointA = ray.Extrap(ray.intersections[0].mT); 
     126        } 
     127  } 
     128   
     129   
    118130  VssRay *vssRay  = NULL; 
    119131 
    120   bool validSample = true; 
    121   if (detectEmptyViewSpace) { 
     132  bool validSample = (objectA != objectB); 
     133  if (0 && detectEmptyViewSpace) {   // consider all samples valid 
    122134        // check if the viewpoint lies on the line segment AB 
    123135        if (Distance(pointA, pointB) < 
     
    176188  Vector3 point; 
    177189  if (!use2dSampling) { 
    178         Vector3 normal; 
    179         int i = (int)RandomValue(0, (Real)((int)mObjects.size()-1)); 
    180         Intersectable *object = mObjects[i]; 
    181         object->GetRandomSurfacePoint(point, normal); 
     190        if (mObjectBasedSampling) { 
     191          Vector3 normal; 
     192          int i = RandomValue(0, mObjects.size()-1); 
     193          Intersectable *object = mObjects[i]; 
     194          object->GetRandomSurfacePoint(point, normal); 
     195        } else { 
     196          // select 
     197          if (mDirectionalSampling) { 
     198                point = viewpoint + UniformRandomVector(); 
     199          } else { 
     200                // select the other point uniformly distruted in the whole viewspace 
     201                AxisAlignedBox3 box; 
     202                 
     203                if (viewSpaceBox) 
     204                  box =*viewSpaceBox; 
     205                else  
     206                  box = mKdTree->GetBox(); 
     207                 
     208                point = box.GetRandomPoint(); 
     209          } 
     210        } 
    182211  } else { 
    183212        AxisAlignedBox3 box; 
     
    187216        else  
    188217          box = mKdTree->GetBox(); 
    189                  
     218         
    190219        point = box.GetRandomPoint(); 
    191220        point.y = viewpoint.y; 
    192221  } 
    193222         
     223  return point - viewpoint; 
     224} 
     225 
     226Vector3 
     227RssPreprocessor::InitialGetDirection(const Vector3 &viewpoint, 
     228                                                                         AxisAlignedBox3 *viewSpaceBox 
     229                                                                         ) 
     230{ 
     231  Vector3 point; 
     232  if (!use2dSampling) { 
     233        if (1 || mObjectBasedSampling) { 
     234          Vector3 normal; 
     235          int i = RandomValue(0, mObjects.size()-1); 
     236          Intersectable *object = mObjects[i]; 
     237          object->GetRandomSurfacePoint(point, normal); 
     238        } else { 
     239          // select 
     240          if (1 || mDirectionalSampling) { 
     241                point = viewpoint + UniformRandomVector(); 
     242          } else { 
     243                // select the other point uniformly distruted in the whole viewspace 
     244                AxisAlignedBox3 box; 
     245                 
     246                if (viewSpaceBox) 
     247                  box =*viewSpaceBox; 
     248                else  
     249                  box = mKdTree->GetBox(); 
     250                 
     251                point = box.GetRandomPoint(); 
     252          } 
     253        } 
     254  } else { 
     255        AxisAlignedBox3 box; 
     256         
     257        if (viewSpaceBox) 
     258          box =*viewSpaceBox; 
     259        else  
     260          box = mKdTree->GetBox(); 
     261         
     262        point = box.GetRandomPoint(); 
     263        point.y = viewpoint.y; 
     264  } 
     265   
    194266  return point - viewpoint; 
    195267} 
     
    203275  int num; 
    204276 
    205  
    206    
    207   float avgPvsSize; 
    208   float avgRayContribution; 
    209   float avgPvsEntropy; 
    210   float avgRayLengthEntropy; 
    211   float avgImportance; 
    212   float avgRays; 
    213   rssTree->GetTreeStatistics( 
    214                                                          avgPvsSize, 
    215                                                          avgRays, 
    216                                                          avgRayContribution, 
    217                                                          avgPvsEntropy, 
    218                                                          avgRayLengthEntropy, 
    219                                                          avgImportance); 
    220    
     277  rssTree->UpdateTreeStatistics(); 
     278 
    221279  cout<< 
    222         "#RSS_AVG_PVS_SIZE\n"<<avgPvsSize<<endl<< 
    223         "#RSS_AVG_RAYS\n"<<avgRays<<endl<< 
    224         "#RSS_AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl<< 
    225         "#RSS_AVG_PVS_ENTROPY\n"<<avgPvsEntropy<<endl<< 
    226         "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<avgRayLengthEntropy<<endl<< 
    227         "#RSS_AVG_IMPORTANCE\n"<<avgImportance<<endl; 
     280        "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<< 
     281        "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<< 
     282        "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<< 
     283        "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<< 
     284        "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<< 
     285        "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl; 
    228286   
    229287  if (0) { 
    230         float p = desiredSamples/(float)(avgRayContribution*rssTree->stat.Leaves()); 
     288        float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves()); 
    231289        num = rssTree->GenerateRays(p, rays); 
    232290  } else { 
     
    249307  cout<<"Exporting vss rays..."<<endl<<flush; 
    250308         
    251   float prob = number/(float)vssRays.size(); 
    252  
    253  
    254309  Exporter *exporter = NULL; 
    255310  exporter = Exporter::GetExporter(filename); 
     
    266321  } 
    267322         
    268   VssRayContainer rays; for (int i=0; i < vssRays.size(); i++) 
    269         if (RandomValue(0,1) < prob) 
    270           rays.push_back(vssRays[i]); 
     323  VssRayContainer rays; 
     324   
     325  vssRays.SelectRays( number, rays); 
    271326 
    272327  exporter->ExportRays(rays, RgbColor(1, 0, 0)); 
     
    388443} 
    389444 
     445 
     446void 
     447RssPreprocessor::ComputeRenderError() 
     448{ 
     449  // compute rendering error 
     450  if (renderer) { 
     451        emit EvalPvsStat(); 
     452        QMutex mutex; 
     453        mutex.lock(); 
     454        renderer->mRenderingFinished.wait(&mutex); 
     455        mutex.unlock(); 
     456        mStats << 
     457          "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<< 
     458          "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<< 
     459          "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl; 
     460  } 
     461} 
     462 
    390463bool 
    391464RssPreprocessor::ComputeVisibility() 
    392465{ 
     466   
     467  connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) ); 
     468   
    393469  cout<<"Rss Preprocessor started\n"<<flush; 
    394470  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl; 
    395   mSceneGraph->CollectObjects(&mObjects); 
     471 
     472  Randomize(0); 
     473   
    396474         
    397475  long startTime = GetTime(); 
     
    443521 
    444522  RssTree *rssTree = NULL; 
    445  
    446   if (mLoadInitialSamples) 
    447   { 
    448           cout << "Loading samples from file ... "; 
    449           LoadSamples(mVssRays, mObjects); 
    450           cout << "finished\n" << endl; 
    451   } 
    452   else 
    453   { 
     523  int rssPass = 0; 
     524  int rssSamples = 0; 
     525 
     526#if 0 
     527  if (mLoadInitialSamples) { 
     528        cout << "Loading samples from file ... "; 
     529        LoadSamples(mVssRays, mObjects); 
     530        cout << "finished\n" << endl; 
     531  } else { 
     532#endif 
    454533        while (totalSamples < mInitialSamples) { 
    455                 int passContributingSamples = 0; 
    456                 int passSampleContributions = 0; 
    457                 int passSamples = 0; 
    458                 int index = 0; 
    459                          
    460                 int sampleContributions; 
    461                          
    462                 int s = Min(mSamplesPerPass, mInitialSamples); 
    463                 for (int k=0; k < s; k++) { 
    464                                  
     534          int passContributingSamples = 0; 
     535          int passSampleContributions = 0; 
     536          int passSamples = 0; 
     537          int index = 0; 
     538           
     539          int sampleContributions; 
     540           
     541          //int s = Min(mSamplesPerPass, mInitialSamples); 
     542          int s = mInitialSamples; 
     543           
     544          for (int k=0; k < s; k++) { 
     545                 
     546                 
    465547                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 
    466548                Vector3 viewpoint;  
    467                 mViewCellsManager->GetViewPoint(viewpoint); 
    468                 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 
    469                                  
     549                //              mViewCellsManager->GetViewPoint(viewpoint); 
     550                viewpoint = GetViewpoint(mViewSpaceBox); 
     551                Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox); 
     552                 
    470553                sampleContributions = CastRay(viewpoint, direction, mVssRays); 
    471                                  
    472                                  
     554                 
     555                 
    473556                //-- CORR matt: put block inside loop 
    474557                if (sampleContributions) { 
    475                         passContributingSamples ++; 
    476                         passSampleContributions += sampleContributions; 
     558                  passContributingSamples ++; 
     559                  passSampleContributions += sampleContributions; 
    477560                } 
    478561                passSamples++; 
    479562                totalSamples++; 
    480                 } 
    481              
    482                          
    483                 float avgRayContrib = (passContributingSamples > 0) ?  
     563          } 
     564           
     565           
     566          float avgRayContrib = (passContributingSamples > 0) ?  
    484567                passSampleContributions/(float)passContributingSamples : 0; 
    485                          
    486                 cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
    487                 cout << "#TotalSamples=" << totalSamples/1000  
    488                         << "k   #SampleContributions=" << passSampleContributions << " ("  
    489                         << 100*passContributingSamples/(float)passSamples<<"%)"  
    490                         << "avgcontrib=" << avgRayContrib << endl; 
    491                          
    492                 mStats << 
     568           
     569          cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 
     570          cout << "#TotalSamples=" << totalSamples/1000  
     571                   << "k   #SampleContributions=" << passSampleContributions << " ("  
     572                   << 100*passContributingSamples/(float)passSamples<<"%)"  
     573                   << "avgcontrib=" << avgRayContrib << endl; 
     574           
     575          mPass++; 
     576        } 
     577           
     578        cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 
     579        cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush; 
     580         
     581        rssSamples += mVssRays.size(); 
     582         
     583         
     584        if (mExportRays) { 
     585          char filename[64]; 
     586          sprintf(filename, "rss-rays-initial.x3d"); 
     587          ExportRays(filename, mVssRays, mExportNumRays); 
     588        } 
     589         
     590        if (mStoreInitialSamples) 
     591          { 
     592                cout << "Writing samples to file ... "; 
     593                WriteSamples(mVssRays); 
     594                cout << "finished\n" << endl; 
     595          } 
     596         
     597        if (mUseViewcells) { 
     598          // construct view cells 
     599          mViewCellsManager->Construct(mObjects, mVssRays); 
     600          // evaluate contributions of the intitial rays 
     601          mViewCellsManager->ComputeSampleContributions(mVssRays); 
     602           
     603           
     604          mStats << 
    493605                "#Pass\n" <<mPass<<endl<< 
    494                 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 
    495                 "#TotalSamples\n" << totalSamples<< endl<< 
    496                 "#SampleContributions\n" << passSampleContributions << endl <<  
    497                 "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 
    498                 "#AvgRayContrib\n" << avgRayContrib << endl; 
    499                  
    500                 mPass++; 
    501  
    502         } 
    503    
    504         cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 
    505   } 
    506  
    507   cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush; 
    508    
    509  
    510   if (mExportRays) { 
    511         char filename[64]; 
    512         sprintf(filename, "rss-rays-initial.x3d"); 
    513         ExportRays(filename, mVssRays, mExportNumRays); 
    514   } 
    515    
    516   if (mStoreInitialSamples) 
    517   { 
    518           cout << "Writing samples to file ... "; 
    519           WriteSamples(mVssRays); 
    520           cout << "finished\n" << endl; 
    521   } 
    522  
    523   if (mUseViewcells) { 
    524         // construct view cells 
    525         mViewCellsManager->Construct(mObjects, mVssRays); 
    526          
     606                "#RssPass\n" <<rssPass<<endl<< 
     607                "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<< 
     608                "#TotalSamples\n" <<totalSamples<<endl<< 
     609                "#RssSamples\n" <<rssSamples<<endl; 
     610           
     611        ComputeRenderError(); 
     612 
     613        mVssRays.PrintStatistics(mStats); 
     614        mViewCellsManager->PrintPvsStatistics(mStats); 
     615 
    527616        VssRayContainer selectedRays; 
    528617        int desired = Max( 
    529618                                          mViewCellsManager->GetPostProcessSamples(), 
    530619                                          mViewCellsManager->GetVisualizationSamples()); 
    531         float p = desired/(float)mVssRays.size(); 
    532          
    533         for (int i=0; i < mVssRays.size(); i++) { 
    534           if (Random(1.0f) < p) 
    535                 selectedRays.push_back(mVssRays[i]); 
    536         } 
     620 
     621        mVssRays.SelectRays(desired, selectedRays); 
     622         
    537623        //-- post process view cells 
    538624        mViewCellsManager->PostProcess(mObjects, selectedRays); 
     
    547633 
    548634 
     635   
     636  rssPass++; 
     637   
    549638  rssTree = new RssTree; 
    550          
     639  rssTree->SetPass(mPass); 
     640   
    551641  if (mUseImportanceSampling) { 
    552642         
     
    555645        else 
    556646          rssTree->Construct(mVssRays, NULL); 
     647 
     648        rssTree->stat.Print(mStats); 
    557649        cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl; 
    558650         
     
    572664  // viewcells->UpdatePVS(newVssRays); 
    573665   
    574   int rssPass = 0; 
    575   int rssSamples = 0; 
     666 
    576667  while (1) { 
    577668        int num = mRssSamplesPerPass; 
    578669        SimpleRayContainer rays; 
    579670        VssRayContainer vssRays; 
    580                  
     671 
     672 
    581673        if (!mUseImportanceSampling) { 
    582674          for (int j=0; j < num; j++) { 
     
    597689 
    598690         
    599         totalSamples+=num; 
    600         rssSamples+=num; 
    601  
     691        totalSamples += rays.size(); 
     692        rssSamples += vssRays.size(); 
     693 
     694         
     695         
    602696        mStats << 
    603697          "#Pass\n" <<mPass<<endl<< 
     
    607701          "#RssSamples\n" <<rssSamples<<endl; 
    608702 
     703 
    609704        if (mUseViewcells) { 
    610705           
     
    616711        } 
    617712 
    618         // add rays to the tree after the viewcells have been cast to have their contributions 
    619         // already when adding into the tree 
    620         // do not add those rays which have too low or no contribution.... 
    621          
    622         if (mUseImportanceSampling) { 
    623           rssTree->AddRays(vssRays); 
    624            
    625           if (0) { 
    626                 cout<<"############# Rss PVS STAT ##################\n"; 
    627                 cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl; 
    628                 cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl; 
    629           } 
    630            
    631           if (mUpdateSubdivision) { 
    632                 int subdivided = rssTree->UpdateSubdivision(); 
    633                 cout<<"subdivided leafs = "<<subdivided<<endl; 
    634                 cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl; 
    635           } 
    636         } 
    637          
     713 
     714        ComputeRenderError(); 
     715         
     716        // epxort rays before adding them to the tree -> some of them can be deleted 
     717 
    638718        if (mExportRays) { 
    639719          char filename[64]; 
     
    644724           
    645725          ExportRays(filename, vssRays, mExportNumRays); 
    646         } 
     726 
     727          // now export all contributing rays 
     728          VssRayContainer contributingRays; 
     729          vssRays.GetContributingRays(contributingRays, mPass); 
     730          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<contributingRays.size()<<endl; 
     731          sprintf(filename, "rss-crays-%04d.x3d", rssPass); 
     732          ExportRays(filename, contributingRays, mExportNumRays); 
     733        } 
     734 
     735         
     736        // add rays to the tree after the viewcells have been cast to have their contributions 
     737        // already when adding into the tree 
     738        // do not add those rays which have too low or no contribution.... 
     739         
     740        if (mUseImportanceSampling) { 
     741          rssTree->AddRays(vssRays); 
     742           
     743          if (0) { 
     744                cout<<"############# Rss PVS STAT ##################\n"; 
     745                cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl; 
     746                cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl; 
     747          } 
     748           
     749           
     750          if (mUpdateSubdivision) { 
     751                int updatePasses = 1; 
     752                if (mPass % updatePasses == 0) { 
     753                  int subdivided = rssTree->UpdateSubdivision(); 
     754                  cout<<"subdivided leafs = "<<subdivided<<endl; 
     755                  cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl; 
     756                } 
     757          } 
     758        } 
     759         
    647760 
    648761         
     
    653766        } 
    654767 
     768         
     769        if (!mUseImportanceSampling)  
     770          CLEAR_CONTAINER(vssRays); 
     771 
    655772        if (totalSamples >= mRssSamples + mInitialSamples) 
    656773          break; 
     
    659776        rssPass++; 
    660777        mPass++; 
     778        rssTree->SetPass(mPass); 
    661779  } 
    662780   
Note: See TracChangeset for help on using the changeset viewer.