source: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp @ 1715

Revision 1715, 22.9 KB checked in by bittner, 18 years ago (diff)

new visibility filter support

RevLine 
[447]1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "RssPreprocessor.h"
4#include "X3dExporter.h"
5#include "Environment.h"
6#include "MutualVisibility.h"
7#include "Polygon3.h"
8#include "ViewCell.h"
9#include "VssRay.h"
10#include "RssTree.h"
[451]11#include "ViewCellsManager.h"
[452]12#include "RenderSimulator.h"
[492]13#include "GlRenderer.h"
[1520]14#include "SamplingStrategy.h"
[447]15
[1520]16
[1715]17#define USE_RSS_TREE 1
18
[863]19namespace GtpVisibilityPreprocessor {
[860]20
21
[549]22static bool useViewSpaceBox = false;
[447]23static bool use2dSampling = false;
[516]24
25
26// not supported anymore!
[463]27static bool fromBoxVisibility = false;
[447]28
[1199]29#define ADD_RAYS_IN_PLACE 1
[1112]30 
[447]31RssPreprocessor::RssPreprocessor():
32  mVssRays()
33{
34  // this should increase coherence of the samples
[1004]35  Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
36  Environment::GetSingleton()->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
37  Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
38  Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
39  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
[464]40
[1004]41  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
42  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
43  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
44  Environment::GetSingleton()->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
45  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
46  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling);
47  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling);
[464]48
[1004]49  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
50  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
51  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
[464]52 
[447]53  mStats.open("stats.log");
[563]54  mRssTree = NULL;
55}
[492]56
[563]57
58bool
59RssPreprocessor::GenerateRays(
60                                                          const int number,
61                                                          const int sampleType,
62                                                          SimpleRayContainer &rays
63                                                          )
64{
65  bool result = false;
[1715]66 
67  Debug<<"Generate rays...\n"<<flush;
[563]68
69  switch (sampleType) {
[1520]70  case SamplingStrategy::RSS_BASED_DISTRIBUTION:
71  case SamplingStrategy::RSS_SILHOUETTE_BASED_DISTRIBUTION:
[563]72        if (mRssTree) {
73          GenerateImportanceRays(mRssTree, number, rays);
74          result = true;
75        }
76        break;
[752]77       
[563]78  default:
79        result = Preprocessor::GenerateRays(number, sampleType, rays);
80  }
81
[1112]82  //  rays.NormalizePdf();
[1715]83  Debug<<"done.\n"<<flush;
84
[563]85  return result;
[447]86}
87
[563]88
89
[1715]90
[447]91RssPreprocessor::~RssPreprocessor()
92{
[492]93  // mVssRays get deleted in the tree
94  //  CLEAR_CONTAINER(mVssRays);
[447]95}
96
97
98
[576]99void
100RssPreprocessor::ExportObjectRays(VssRayContainer &rays,
101                                                                  const int objectId)
102{
103  ObjectContainer::const_iterator oi;
[447]104
[576]105  Intersectable *object = NULL;
106  for (oi = mObjects.begin(); oi != mObjects.end(); ++oi)
107        if (objectId == (*oi)->GetId()) {
108          object = *oi;
109          break;
110        }
[447]111
[576]112  if (object == NULL)
113        return;
114 
115  VssRayContainer selectedRays;
116  VssRayContainer::const_iterator it= rays.begin(), it_end = rays.end();
117
118 
119  for (; it != it_end; ++it) {
120        if ((*it)->mTerminationObject == object)
121          selectedRays.push_back(*it);
122  }
123 
124
125  Exporter *exporter = Exporter::GetExporter("object-rays.x3d");
126  //    exporter->SetWireframe();
127  //    exporter->ExportKdTree(*mKdTree);
128  exporter->SetFilled();
129  exporter->ExportIntersectable(object);
130  exporter->ExportRays(selectedRays, RgbColor(1, 0, 0));
131 
132  delete exporter;
133 
134}
135
136
[447]137int
138RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
139                                                                                const int desiredSamples,
140                                                                                SimpleRayContainer &rays
141                                                                                )
142{
143  int num;
[463]144
[492]145  rssTree->UpdateTreeStatistics();
[463]146
[447]147  cout<<
[492]148        "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<<
149        "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<<
150        "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<<
151        "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<<
152        "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<<
153        "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl;
[447]154 
155  if (0) {
[492]156        float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
[447]157        num = rssTree->GenerateRays(p, rays);
158  } else {
[459]159        int leaves = rssTree->stat.Leaves()/1;
[1112]160       
[447]161        num = rssTree->GenerateRays(desiredSamples, leaves, rays);
162  }
163       
164       
165  return num;
166}
167
168
169bool
170RssPreprocessor::ExportRays(const char *filename,
171                                                        const VssRayContainer &vssRays,
172                                                        const int number
173                                                        )
174{
175  cout<<"Exporting vss rays..."<<endl<<flush;
176       
177  Exporter *exporter = NULL;
178  exporter = Exporter::GetExporter(filename);
[1112]179  if (0) {
180        exporter->SetWireframe();
181        exporter->ExportKdTree(*mKdTree);
182  }
[447]183  exporter->SetFilled();
[556]184  // $$JB temporarily do not export the scene
[752]185  if (0)
[1328]186        exporter->ExportScene(mSceneGraph->GetRoot());
[447]187  exporter->SetWireframe();
188
[1563]189  if (1) {
[447]190        exporter->SetForcedMaterial(RgbColor(1,0,1));
[1112]191        exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
[447]192        exporter->ResetForcedMaterial();
193  }
[1112]194 
[492]195  VssRayContainer rays;
196 
[1112]197  vssRays.SelectRays(number, rays);
[447]198
199  exporter->ExportRays(rays, RgbColor(1, 0, 0));
200       
201  delete exporter;
202
203  cout<<"done."<<endl<<flush;
204
205  return true;
206}
207
[1112]208bool
209RssPreprocessor::ExportRayAnimation(const char *filename,
210                                                                        const vector<VssRayContainer> &vssRays
211                                                                        )
212{
213  cout<<"Exporting vss rays..."<<endl<<flush;
214       
215  Exporter *exporter = NULL;
216  exporter = Exporter::GetExporter(filename);
217  if (0) {
218        exporter->SetWireframe();
219        exporter->ExportKdTree(*mKdTree);
220  }
221  exporter->SetFilled();
222  // $$JB temporarily do not export the scene
223  if (1)
[1328]224        exporter->ExportScene(mSceneGraph->GetRoot());
[1112]225  exporter->SetWireframe();
[447]226
[1563]227  if (1) {
[1112]228        exporter->SetForcedMaterial(RgbColor(1,0,1));
229        exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
230        exporter->ResetForcedMaterial();
231  }
232 
233  exporter->ExportRaySets(vssRays, RgbColor(1, 0, 0));
234       
235  delete exporter;
236
237  cout<<"done."<<endl<<flush;
238
239  return true;
240}
241
242
[447]243bool
244RssPreprocessor::ExportRssTree(char *filename,
245                                                           RssTree *tree,
246                                                           const Vector3 &dir
247                                                           )
248{
249  Exporter *exporter = Exporter::GetExporter(filename);
250  exporter->SetFilled();
[1328]251  exporter->ExportScene(mSceneGraph->GetRoot());
[447]252  //  exporter->SetWireframe();
253  bool result = exporter->ExportRssTree2( *tree, dir );
254  delete exporter;
255  return result;
256}
257
258bool
259RssPreprocessor::ExportRssTreeLeaf(char *filename,
260                                                                   RssTree *tree,
261                                                                   RssTreeLeaf *leaf)
262{
263  Exporter *exporter = NULL;
264  exporter = Exporter::GetExporter(filename);
265  exporter->SetWireframe();
266  exporter->ExportKdTree(*mKdTree);
267       
268  exporter->SetForcedMaterial(RgbColor(0,0,1));
269  exporter->ExportBox(tree->GetBBox(leaf));
270  exporter->ResetForcedMaterial();
271       
272  VssRayContainer rays[4];
273  for (int i=0; i < leaf->rays.size(); i++) {
274        int k = leaf->rays[i].GetRayClass();
275        rays[k].push_back(leaf->rays[i].mRay);
276  }
277       
278  // SOURCE RAY
279  exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
280  // TERMINATION RAY
281  exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
282  // PASSING_RAY
283  exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
284  // CONTAINED_RAY
285  exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
286
287  delete exporter;
288  return true;
289}
290
291void
292RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
293{
294  vector<RssTreeLeaf *> leaves;
295  tree->CollectLeaves(leaves);
296
297  int num = 0;
298  int i;
299  float p = number / (float)leaves.size();
300  for (i=0; i < leaves.size(); i++) {
301        if (RandomValue(0,1) < p) {
302          char filename[64];
303          sprintf(filename, "rss-leaf-%04d.x3d", num);
304          ExportRssTreeLeaf(filename, tree, leaves[i]);
305          num++;
306        }
307        if (num >= number)
308          break;
309  }
310}
311
312
313float
314RssPreprocessor::GetAvgPvsSize(RssTree *tree,
315                                                           const vector<AxisAlignedBox3> &viewcells
316                                                           )
317{
318  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
319
320  int sum = 0;
321  for (it = viewcells.begin(); it != it_end; ++ it)
322        sum += tree->GetPvsSize(*it);
323       
324  return sum/(float)viewcells.size();
325}
326
327
328void
329RssPreprocessor::ExportPvs(char *filename,
330                                                   RssTree *rssTree
331                                                   )
332{
333  ObjectContainer pvs;
334  if (rssTree->CollectRootPvs(pvs)) {
335        Exporter *exporter = Exporter::GetExporter(filename);
336        exporter->SetFilled();
337        exporter->ExportGeometry(pvs);
[459]338        exporter->SetWireframe();
339        exporter->ExportBox(rssTree->bbox);
340        exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
[447]341        delete exporter;
342  }
343}
344
[492]345
346void
347RssPreprocessor::ComputeRenderError()
348{
349  // compute rendering error
[1145]350       
[752]351  if (renderer && renderer->mPvsStatFrames) {
[496]352        //      emit EvalPvsStat();
353        //      QMutex mutex;
354        //      mutex.lock();
355        //      renderer->mRenderingFinished.wait(&mutex);
356        //      mutex.unlock();
[556]357
[496]358        renderer->EvalPvsStat();
[492]359        mStats <<
360          "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
361          "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
[713]362          "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
363          "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
[492]364  }
365}
366
[1112]367void
368NormalizeRatios(float ratios[3])
369{
370  int i;
371  float sumRatios;
372  sumRatios = ratios[0] + ratios[1] + ratios[2];
373  if (sumRatios == 0.0f) {
374        ratios[0] = ratios[1] = ratios[2] = 1.0f;
375        sumRatios = 3.0f;
376  }
377 
378  for (i=0 ; i < 3; i++)
379        ratios[i]/=sumRatios;
[1715]380
381#define MIN_RATIO 0.1f
382
383#if USE_RSS_TREE
384  i = 0;
385#else
386  i = 1;
387#endif
388 
389  for (; i < 3; i++)
[1112]390        if (ratios[i] < MIN_RATIO)
391          ratios[i] = MIN_RATIO;
[563]392
[1112]393  sumRatios = ratios[0] + ratios[1] + ratios[2];
394  for (i=0 ; i < 3; i++)
395        ratios[i]/=sumRatios;
396 
397}
[563]398
[447]399bool
400RssPreprocessor::ComputeVisibility()
401{
[556]402
[811]403  Debug << "type: rss" << endl;
[579]404
[464]405  cout<<"Rss Preprocessor started\n"<<flush;
[567]406  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[492]407
408  Randomize(0);
409 
[1694]410  const bool useRayBuffer = false;
411 
[1112]412  vector<VssRayContainer> rayBuffer(50);
413 
[447]414  long startTime = GetTime();
[1292]415  long lastTime;
416  float totalTime;
[447]417 
418  int totalSamples = 0;
419
[577]420  // if not already loaded, construct view cells from file
[1112]421  if (!mLoadViewCells)
422  {
[1563]423          // construct view cells using it's own set of samples
424          mViewCellsManager->Construct(this);
425
426          //-- several visualizations and statistics
427          Debug << "view cells construction finished: " << endl;
428          mViewCellsManager->PrintStatistics(Debug);
[574]429  }
[1563]430
[1112]431 
[492]432  int rssPass = 0;
433  int rssSamples = 0;
[563]434 
[492]435  if (mLoadInitialSamples) {
436        cout << "Loading samples from file ... ";
437        LoadSamples(mVssRays, mObjects);
438        cout << "finished\n" << endl;
439  } else {
[563]440        SimpleRayContainer rays;
[537]441
[871]442        cout<<"Generating initial rays..."<<endl<<flush;
443
[1199]444        if (mUseImportanceSampling) {
[1585]445          GenerateRays(mInitialSamples/2, SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays);
446          //      GenerateRays(mInitialSamples/3, SamplingStrategy::OBJECT_BASED_DISTRIBUTION, rays);
447          GenerateRays(mInitialSamples/2, SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays);
[1199]448          //    GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
449        } else {
[1520]450                int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
[1199]451          if (mObjectBasedSampling)
[1520]452                rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
[1199]453          else
454                if (mDirectionalSampling)
[1520]455                  rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
[1199]456          cout<<"Generating rays..."<<endl;
457          GenerateRays(mRssSamplesPerPass, rayType, rays);
458        }
459       
460       
[871]461        cout<<"Casting initial rays..."<<endl<<flush;
[1520]462        CastRays(rays, mVssRays, true);
[576]463
464        ExportObjectRays(mVssRays, 1546);
[563]465  }
466 
467  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
468 
469  rssSamples += (int)mVssRays.size();
470  totalSamples += mInitialSamples;
471  mPass++;
472 
473  if (mExportRays) {
[577]474       
[563]475        char filename[64];
476        sprintf(filename, "rss-rays-initial.x3d");
477        ExportRays(filename, mVssRays, mExportNumRays);
[1112]478
[1694]479        if (useRayBuffer)
480          mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
[577]481       
[563]482  }
483 
484  if (mStoreInitialSamples) {
485        cout << "Writing samples to file ... ";
486        ExportSamples(mVssRays);
487        cout << "finished\n" << endl;
488  }
[492]489       
[563]490  if (mUseViewcells) {
491
[871]492        cout<<"Computing sample contributions..."<<endl<<flush;
[563]493        // evaluate contributions of the intitial rays
[574]494        mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
[871]495        cout<<"done.\n"<<flush;
[1292]496
497        long time = GetTime();
498        totalTime = TimeDiff(startTime, time)*1e-3;
499        lastTime = time;
[492]500       
[563]501        mStats <<
502          "#Pass\n" <<mPass<<endl<<
503          "#RssPass\n" <<rssPass<<endl<<
[1292]504          "#Time\n" << totalTime <<endl<<
[563]505          "#TotalSamples\n" <<totalSamples<<endl<<
506          "#RssSamples\n" <<rssSamples<<endl;
[569]507
508        {
509          VssRayContainer contributingRays;
[1112]510          mVssRays.GetContributingRays(contributingRays, 0);
[569]511          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
[1112]512          if (mExportRays) {
513                char filename[64];
514                sprintf(filename, "rss-crays-%04d.x3d", 0);
515                ExportRays(filename, contributingRays, mExportNumRays);
516          }
[569]517        }
[563]518       
519        mVssRays.PrintStatistics(mStats);
520        mViewCellsManager->PrintPvsStatistics(mStats);
[502]521         
[572]522        // viewcells->UpdatePVS(newVssRays);
523        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
524        // cull viewcells with PVS > median (0.5f)
[752]525        //mViewCellsManager->SetValidityPercentage(0, 0.5f);
526        mViewCellsManager->SetValidityPercentage(0, 1.0f);
[572]527        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
[563]528       
529        ComputeRenderError();
530  }
[492]531 
532  rssPass++;
533 
[563]534  mRssTree = new RssTree;
535  mRssTree->SetPass(mPass);
[492]536 
[527]537  /// compute view cell contribution of rays if view cells manager already constructed
[752]538  //  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
[527]539
[467]540  if (mUseImportanceSampling) {
[1715]541
542#if USE_RSS_TREE       
[563]543        mRssTree->Construct(mObjects, mVssRays);
[1715]544#endif
[467]545       
[563]546        mRssTree->stat.Print(mStats);
547        cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
548       
[467]549        if (mExportRssTree) {
[563]550          ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
551          ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
552          ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
553          ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
554          ExportRssTreeLeaves(mRssTree, 10);
[467]555        }
556       
557        if (mExportPvs) {
[563]558          ExportPvs("rss-pvs-initial.x3d", mRssTree);
[467]559        }
[459]560  }
[467]561 
[466]562 
[1715]563
[447]564  while (1) {
[1715]565
[1283]566        static SimpleRayContainer rays;
567        static VssRayContainer vssRays;
568        static VssRayContainer tmpVssRays;
[1715]569
570        rays.reserve((int)(1.1f*mRssSamplesPerPass));
571
[1283]572        rays.clear();
573        vssRays.clear();
574        tmpVssRays.clear();
[1715]575
[563]576        int castRays = 0;
577        if (mUseImportanceSampling) {
[567]578
[1715]579#if USE_RSS_TREE       
[1112]580          static float ratios[] = {0.9f,0.05f,0.05f};
[1715]581#else
582          static float ratios[] = {0.0f,0.05f,0.05f};
583#endif
584          NormalizeRatios(ratios);
585                 
586          cout<<"New ratios: "<<ratios[0]<<" "<<ratios[1]<<" "<<ratios[2]<<endl;
587
[1112]588          //float ratios[] = {1.0f,0.0f,0.0f};
589         
590          int nrays[3];
591          float contributions[3];
592          float times[3];
593         
[1150]594          long t1;
[567]595
[1112]596          t1 = GetTime();
597         
[1715]598          GenerateRays(int(mRssSamplesPerPass*ratios[0]),
599                                   SamplingStrategy::RSS_BASED_DISTRIBUTION,
600                                   rays);
601         
[1302]602          rays.NormalizePdf((float)rays.size());
[1112]603         
[1520]604          CastRays(rays, tmpVssRays, true);
[1297]605          castRays += (int)rays.size();
[1112]606#if ADD_RAYS_IN_PLACE
607          contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
608#else
609          contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
610#endif
611          times[0] = TimeDiff(t1, GetTime());
[1302]612          nrays[0] = (int)rays.size();
[1112]613         
614          mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
615         
[563]616          vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
617          rays.clear();
618          tmpVssRays.clear();
[567]619          if (ratios[1]!=0.0f) {
[1112]620                t1 = GetTime();
[1520]621                GenerateRays(int(mRssSamplesPerPass*ratios[1]), SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays);
622                CastRays(rays, tmpVssRays, true);
[1302]623                castRays += (int)rays.size();
[1112]624#if ADD_RAYS_IN_PLACE
625                contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
626#else
627                contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
628#endif
629          times[1] = TimeDiff(t1, GetTime());
[1302]630          nrays[1] = (int)rays.size();
[1112]631
632          mStats<<"#SpatialRelContrib"<<endl<<contributions[1]/nrays[1]<<endl;
[567]633               
634                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
635               
636                rays.clear();
637                tmpVssRays.clear();
638          }
[563]639         
640         
[567]641          if (ratios[2]!=0.0f) {
[1112]642                t1 = GetTime();
[1520]643                GenerateRays(int(mRssSamplesPerPass*ratios[2]), SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays);
644                CastRays(rays, tmpVssRays, true);
[1302]645                castRays += (int)rays.size();
[1112]646#if ADD_RAYS_IN_PLACE
647                contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
648#else
649                contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
650#endif
651                times[2] = TimeDiff(t1, GetTime());
[1302]652                nrays[2] = (int)rays.size();
[567]653               
[1112]654                mStats<<"#DirectionalRelContrib"<<endl<<contributions[2]/nrays[2]<<endl;
655               
[567]656                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
657               
658                rays.clear();
659                tmpVssRays.clear();
660          }
[563]661         
[1112]662         
663          // now evaluate the ratios for the next pass
[1613]664#define TIME_WEIGHT 0.5f
[1112]665
[1715]666#if 1
[1613]667          ratios[0] = sqr(contributions[0]/(TIME_WEIGHT*times[0] + (1 - TIME_WEIGHT)*nrays[0]));
668          ratios[1] = sqr(contributions[1]/(TIME_WEIGHT*times[1] + (1 - TIME_WEIGHT)*nrays[1]));
669          ratios[2] = sqr(contributions[2]/(TIME_WEIGHT*times[2] + (1 - TIME_WEIGHT)*nrays[2]));
[1715]670#else
671          ratios[0] = contributions[0]/(TIME_WEIGHT*times[0] + (1 - TIME_WEIGHT)*nrays[0]);
672          ratios[1] = contributions[1]/(TIME_WEIGHT*times[1] + (1 - TIME_WEIGHT)*nrays[1]);
673          ratios[2] = contributions[2]/(TIME_WEIGHT*times[2] + (1 - TIME_WEIGHT)*nrays[2]);
674#endif   
[1112]675
[1715]676#if !USE_RSS_TREE
677          ratios[0] = 0;
678#endif
[1112]679         
[563]680          // add contributions of all rays at once...
[1112]681#if !ADD_RAYS_IN_PLACE
[563]682          mViewCellsManager->AddSampleContributions(vssRays);
[1112]683#endif   
[563]684        }
685        else {
[1520]686                int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
[563]687          if (mObjectBasedSampling)
[1520]688                  rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
[563]689          else
690                if (mDirectionalSampling)
[1520]691                        rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
[752]692
693          cout<<"Generating rays..."<<endl;
694
[563]695          GenerateRays(mRssSamplesPerPass, rayType, rays);
[752]696          cout<<"done."<<endl;
697
[1715]698          cout<<"Casting rays..."<<endl;          CastRays(rays, vssRays, true);
[752]699          cout<<"done."<<endl;
[1302]700          castRays += (int)rays.size();
[563]701          if (mUseViewcells) {
702                /// compute view cell contribution of rays
[752]703                cout<<"Computing sample contributions..."<<endl;
[574]704                mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
[752]705                cout<<"done."<<endl;
[447]706          }
[563]707               
708         
[447]709        }
[563]710        totalSamples += castRays;
[503]711        rssSamples += (int)vssRays.size();
[467]712
[884]713        cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
714                                                                                                                                                   mInitialSamples)<<"%\n";
[492]715       
[1292]716
717        long time = GetTime();
718        totalTime += TimeDiff(lastTime, time);
719        lastTime = time;
[492]720       
[467]721        mStats <<
722          "#Pass\n" <<mPass<<endl<<
723          "#RssPass\n" <<rssPass<<endl<<
724          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
725          "#TotalSamples\n" <<totalSamples<<endl<<
726          "#RssSamples\n" <<rssSamples<<endl;
727
[492]728
[466]729        if (mUseViewcells) {
[1715]730          Debug<<"Print statistics...\n"<<flush;
[467]731          vssRays.PrintStatistics(mStats);
732          mViewCellsManager->PrintPvsStatistics(mStats);
[1715]733          Debug<<"done.\n"<<flush;
[466]734        }
[576]735
736        if (0 && mPass > 0) {
[1145]737                char buf[100];
[576]738          if (mUseImportanceSampling)
[1145]739          {
740                sprintf(buf, "snap/i-%02d-", mPass);
741
[1151]742                renderer->SetSnapPrefix(buf);
[1145]743          }
[576]744          else
[1145]745          {
746                sprintf(buf, "snap/r-%02d-", mPass);
747
[1151]748                renderer->SetSnapPrefix(buf);
[1145]749          }
750
[1151]751          renderer->SetSnapErrorFrames(true);
[576]752        }
753
[492]754        ComputeRenderError();
755       
756        // epxort rays before adding them to the tree -> some of them can be deleted
757
758        if (mExportRays) {
759          char filename[64];
760          if (mUseImportanceSampling)
761                sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
762          else
763                sprintf(filename, "rss-rays-%04d.x3d", rssPass);
764         
[1112]765         
766
[1694]767          if (useRayBuffer)
768                vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
[1112]769         
[492]770          ExportRays(filename, vssRays, mExportNumRays);
[1112]771         
[492]772          // now export all contributing rays
773          VssRayContainer contributingRays;
774          vssRays.GetContributingRays(contributingRays, mPass);
[508]775          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
[492]776          sprintf(filename, "rss-crays-%04d.x3d", rssPass);
777          ExportRays(filename, contributingRays, mExportNumRays);
[1112]778         
[492]779        }
780
781       
[467]782        // add rays to the tree after the viewcells have been cast to have their contributions
783        // already when adding into the tree
784        // do not add those rays which have too low or no contribution....
[1715]785
786#if USE_RSS_TREE       
[467]787        if (mUseImportanceSampling) {
[1715]788          Debug<<"Adding rays...\n"<<flush;
[563]789          mRssTree->AddRays(vssRays);
[1715]790          Debug<<"done.\n"<<flush;
[467]791          if (mUpdateSubdivision) {
[492]792                int updatePasses = 1;
793                if (mPass % updatePasses == 0) {
[1715]794                  Debug<<"Updating rss tree...\n"<<flush;
[563]795                  int subdivided = mRssTree->UpdateSubdivision();
[1715]796                  Debug<<"done.\n"<<flush;
[492]797                  cout<<"subdivided leafs = "<<subdivided<<endl;
[563]798                  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
[492]799                }
[467]800          }
[447]801        }
[1715]802#endif
[467]803       
[464]804        if (mExportPvs) {
[447]805          char filename[64];
[467]806          sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
[563]807          ExportPvs(filename, mRssTree);
[447]808        }
[492]809       
[563]810       
[1199]811        if (!mUseImportanceSampling)
[492]812          CLEAR_CONTAINER(vssRays);
[563]813        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
[492]814
[467]815        if (totalSamples >= mRssSamples + mInitialSamples)
[447]816          break;
817       
[1613]818       
[467]819        rssPass++;
820        mPass++;
[563]821        mRssTree->SetPass(mPass);
[1613]822
823       
[447]824  }
825 
[464]826  if (mUseViewcells) {
[466]827
[1199]828        if(0)
[574]829          {
[1199]830                VssRayContainer selectedRays;
831                int desired = mViewCellsManager->GetVisualizationSamples();
832               
833                mVssRays.SelectRays(desired, selectedRays);
834               
835                mViewCellsManager->Visualize(mObjects, selectedRays);
[574]836          }
[556]837       
[1199]838        // view cells after sampling
839        mViewCellsManager->PrintStatistics(Debug);
840       
841        //-- render simulation after merge
842        cout << "\nevaluating bsp view cells render time after sampling ... ";
843       
844        mRenderSimulator->RenderScene();
845        SimulationStatistics ss;
846        mRenderSimulator->GetStatistics(ss);
847       
848        cout << " finished" << endl;
849        cout << ss << endl;
850        Debug << ss << endl;
851       
[466]852  }
[1112]853
854
[1694]855  if (useRayBuffer&& mExportRays && mUseImportanceSampling) {
[1112]856        char filename[64];
857        sprintf(filename, "rss-rays-i.x3d");
858       
859        rayBuffer.resize(mPass);
860        ExportRayAnimation(filename, rayBuffer);
861  }
862         
[1581]863
864  // do not delete rss tree now - can be used for visualization..
865#if 0
[563]866  Debug<<"Deleting RSS tree...\n";
867  delete mRssTree;
868  Debug<<"Done.\n";
[1581]869#endif
[466]870
[1199]871  //mViewCellsManager->ExportViewCells("visibility.xml",
[871]872  //                                                                     true);
[447]873 
[871]874 
[447]875  return true;
876}
877
[871]878}
Note: See TracBrowser for help on using the repository browser.