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

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

experiments with different contribution computations

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