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

Revision 1966, 22.4 KB checked in by bittner, 17 years ago (diff)

samplign preprocessor updates, merge

Line 
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"
11#include "ViewCellsManager.h"
12#include "RenderSimulator.h"
13#include "GlRenderer.h"
14#include "SamplingStrategy.h"
15#include "PreprocessorThread.h"
16
17
18namespace GtpVisibilityPreprocessor {
19
20
21
22  const bool pruneInvalidRays = false;
23 
24 
25#define ADD_RAYS_IN_PLACE 1
26 
27RssPreprocessor::RssPreprocessor():
28  mVssRays()
29{
30  // this should increase coherence of the samples
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);
36
37  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useRssTree",
38                                                                                        mUseRssTree);
39
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);
47
48  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
49
50  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
51
52  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
53 
54  mRssTree = NULL;
55}
56
57
58int
59RssPreprocessor::GenerateRays(
60                                                          const int number,
61                                                          SamplingStrategy &strategy,
62                                                          SimpleRayContainer &rays)
63{
64  int result = 0;
65 
66  Debug<<"Generate rays...\n"<<flush;
67
68  switch (strategy.mType) {
69  case SamplingStrategy::RSS_BASED_DISTRIBUTION:
70  case SamplingStrategy::RSS_SILHOUETTE_BASED_DISTRIBUTION:
71        if (mRssTree) {
72          result = GenerateImportanceRays(mRssTree, number, rays);
73        }
74        break;
75       
76  default:
77        result = Preprocessor::GenerateRays(number, strategy, rays);
78  }
79 
80  //  rays.NormalizePdf();
81  Debug<<"done.\n"<<flush;
82
83  return result;
84}
85
86
87
88
89RssPreprocessor::~RssPreprocessor()
90{
91  // mVssRays get deleted in the tree
92  //  CLEAR_CONTAINER(mVssRays);
93}
94
95
96void
97RssPreprocessor::ExportObjectRays(VssRayContainer &rays,
98                                                                  const int objectId)
99{
100  ObjectContainer::const_iterator oi;
101
102  Intersectable *object = NULL;
103  for (oi = mObjects.begin(); oi != mObjects.end(); ++oi)
104        if (objectId == (*oi)->GetId()) {
105          object = *oi;
106          break;
107        }
108
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
134int
135RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
136                                                                                const int desiredSamples,
137                                                                                SimpleRayContainer &rays
138                                                                                )
139{
140  int num;
141
142  rssTree->UpdateTreeStatistics();
143
144  cout<<
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;
151 
152  if (0) {
153        float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
154        num = rssTree->GenerateRays(p, rays);
155  } else {
156        int leaves = rssTree->stat.Leaves()/1;
157       
158        num = rssTree->GenerateRays(desiredSamples, leaves, rays);
159  }
160       
161       
162  return num;
163}
164
165
166
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)
183        exporter->ExportScene(mSceneGraph->GetRoot());
184  exporter->SetWireframe();
185
186  if (1) {
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
202bool
203RssPreprocessor::ExportRssTree(char *filename,
204                                                           RssTree *tree,
205                                                           const Vector3 &dir
206                                                           )
207{
208  Exporter *exporter = Exporter::GetExporter(filename);
209  exporter->SetFilled();
210  exporter->ExportScene(mSceneGraph->GetRoot());
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);
297        exporter->SetWireframe();
298        exporter->ExportBox(rssTree->bbox);
299        exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
300        delete exporter;
301  }
302}
303
304
305
306void
307NormalizeRatios(vector<SamplingStrategy *> &distributions)
308{
309  int i;
310  float sumRatios = 0.0f;
311 
312  for (i=0; i < distributions.size(); i++)
313        sumRatios += distributions[i]->mRatio;
314
315 
316  if (sumRatios == 0.0f) {
317        for (i=0; i < distributions.size(); i++) {
318          distributions[i]->mRatio = 1.0f;
319          sumRatios += 1.0f;
320        }
321  }
322
323  for (i=0 ; i < distributions.size(); i++)
324        distributions[i]->mRatio/=sumRatios;
325
326#define MIN_RATIO 0.1f
327 
328  for (i = 0; i < distributions.size(); i++)
329        if (distributions[i]->mRatio < MIN_RATIO)
330          distributions[i]->mRatio = MIN_RATIO;
331
332
333  sumRatios = 0.0f;
334  for (i=0; i < distributions.size(); i++)
335        sumRatios += distributions[i]->mRatio;
336
337  for (i=0 ; i < distributions.size(); i++)
338        distributions[i]->mRatio/=sumRatios;
339 
340 
341  cout<<"New ratios: ";
342  for (i=0 ; i < distributions.size(); i++)
343        cout<<distributions[i]->mRatio<<" ";
344  cout<<endl;
345}
346
347void
348EvaluateRatios(vector<SamplingStrategy *> &distributions)
349{
350  // now evaluate the ratios for the next pass
351#define TIME_WEIGHT 0.0f
352  for (int i=0; i < distributions.size(); i++) {
353        distributions[i]->mRatio = distributions[i]->mContribution/
354          (Limits::Small +
355           (TIME_WEIGHT*distributions[i]->mTime +
356                (1 - TIME_WEIGHT)*distributions[i]->mRays)
357           );
358
359#if 1
360        distributions[i]->mRatio = sqr(distributions[i]->mRatio);
361#endif
362  }
363}
364
365
366
367bool
368RssPreprocessor::ComputeVisibility()
369{
370
371  Debug << "type: rss" << endl;
372
373  cout<<"Rss Preprocessor started\n"<<flush;
374  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
375
376  Randomize(0);
377
378  if (0) {
379        Exporter *exporter = Exporter::GetExporter("samples.wrl");
380    exporter->SetFilled();
381        Halton<4> halton;
382
383        //      for (int i=1; i < 7; i++)
384        //        cout<<i<<" prime= "<<FindPrime(i)<<endl;
385       
386        Material mA, mB;
387        mA.mDiffuseColor = RgbColor(1, 0, 0);
388        mB.mDiffuseColor = RgbColor(0, 1, 0);
389       
390        for (int i=0; i < 10000; i++) {
391          float r[4];
392          halton.GetNext(r);
393          Vector3 v1 = UniformRandomVector(r[0], r[1]);
394          Vector3 v2 = UniformRandomVector(r[2], r[3]);
395
396          const float size =0.002f;
397          AxisAlignedBox3 box(v1-Vector3(size), v1+Vector3(size));
398          exporter->SetForcedMaterial(mA);
399          exporter->ExportBox(box);
400          AxisAlignedBox3 box2(v2-Vector3(size), v2+Vector3(size));
401
402          exporter->SetForcedMaterial(mB);
403          exporter->ExportBox(box2);
404        }
405        delete exporter;
406  }
407 
408  if (!GetThread())
409          cerr << "!!no thread available!" << endl;
410
411  cout<<"COMPUTATION THREAD="<<GetThread()->GetCurrentThreadId()<<endl<<flush;
412 
413  // use ray buffer to export ray animation
414  const bool useRayBuffer = false;
415 
416  vector<VssRayContainer> rayBuffer(50);
417 
418  long startTime = GetTime();
419  long lastTime;
420  float totalTime;
421 
422  int totalSamples = 0;
423
424  // if not already loaded, construct view cells from file
425  if (!mLoadViewCells)
426  {
427          // construct view cells using it's own set of samples
428          mViewCellsManager->Construct(this);
429
430          //-- several visualizations and statistics
431          Debug << "view cells construction finished: " << endl;
432          mViewCellsManager->PrintStatistics(Debug);
433  }
434
435 
436  int rssPass = 0;
437  int rssSamples = 0;
438
439  // now decode distribution string
440  char buff[1024];
441  Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions",
442                                                                                          buff);
443 
444  char *curr = buff;
445  mUseRssTree = false;
446
447  while (1) {
448        char *e = strchr(curr,'+');
449        if (e!=NULL) {
450          *e=0;
451        }
452       
453        if (strcmp(curr, "rss")==0) {
454          mDistributions.push_back(new RssBasedDistribution(*this));
455          mUseRssTree = true;
456        } else
457          if (strcmp(curr, "object")==0) {
458                mDistributions.push_back(new ObjectBasedDistribution(*this));
459          } else
460                if (strcmp(curr, "spatial")==0) {
461                  mDistributions.push_back(new SpatialBoxBasedDistribution(*this));
462                } else
463                  if (strcmp(curr, "global")==0) {
464                        mDistributions.push_back(new GlobalLinesDistribution(*this));
465                  } else
466                        if (strcmp(curr, "direction")==0) {
467                          mDistributions.push_back(new DirectionBasedDistribution(*this));
468                        } else
469                          if (strcmp(curr, "object_direction")==0) {
470                                mDistributions.push_back(new ObjectDirectionBasedDistribution(*this));
471                          } else
472                                if (strcmp(curr, "reverse_object")==0) {
473                                  mDistributions.push_back(new ReverseObjectBasedDistribution(*this));
474                                } else
475                                  if (strcmp(curr, "reverse_viewspace_border")==0) {
476                                        mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(*this));
477                                  }
478       
479        if (e==NULL)
480          break;
481        curr = e+1;
482  }
483
484  mMixtureDistribution = new MixtureDistribution(*this);
485  mMixtureDistribution->mDistributions = mDistributions;
486  mMixtureDistribution->Init();
487  bool oldGenerator = false;
488
489  const int batch = 100000;
490 
491  if (mLoadInitialSamples) {
492        cout << "Loading samples from file ... ";
493        LoadSamples(mVssRays, mObjects);
494        cout << "finished\n" << endl;
495  } else {
496        SimpleRayContainer rays;
497       
498        if (oldGenerator) {
499         
500          cout<<"Generating initial rays..."<<endl<<flush;
501         
502          int count = 0;
503          int i;
504         
505          // Generate initial samples
506          for (i=0; i < mDistributions.size(); i++)
507                if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
508                  count++;
509         
510          for (i=0; i < mDistributions.size(); i++)
511                if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
512                  GenerateRays(mInitialSamples/count,
513                                           *mDistributions[i],
514                                           rays);
515
516          cout<<"Casting initial rays..."<<endl<<flush;
517          CastRays(rays, mVssRays, true, pruneInvalidRays);
518         
519          ExportObjectRays(mVssRays, 1546);
520
521          cout<<"Computing sample contributions..."<<endl<<flush;
522          // evaluate contributions of the intitial rays
523          mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
524          cout<<"done.\n"<<flush;
525         
526        } else {
527          for (int i=0; i < mInitialSamples; i+=batch) {
528                rays.clear();
529                mVssRays.clear();
530                mMixtureDistribution->GenerateSamples(batch, rays);
531                CastRays(rays, mVssRays, true, pruneInvalidRays);
532                mMixtureDistribution->ComputeContributions(mVssRays);
533          }
534        }
535  }
536 
537  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
538 
539  rssSamples += (int)mVssRays.size();
540  totalSamples += mInitialSamples;
541  mPass++;
542 
543  if (mExportRays) {
544       
545        char filename[64];
546        sprintf(filename, "rss-rays-initial.x3d");
547        ExportRays(filename, mVssRays, mExportNumRays);
548
549        if (useRayBuffer)
550          mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
551       
552  }
553 
554  if (mStoreInitialSamples) {
555        cout << "Writing samples to file ... ";
556        ExportSamples(mVssRays);
557        cout << "finished\n" << endl;
558  }
559       
560 
561  long time = GetTime();
562  totalTime = TimeDiff(startTime, time)*1e-3f;
563  lastTime = time;
564 
565  mStats <<
566        "#Pass\n" <<mPass<<endl<<
567        "#RssPass\n" <<rssPass<<endl<<
568        "#Time\n" << totalTime <<endl<<
569        "#TotalSamples\n" <<totalSamples<<endl<<
570        "#RssSamples\n" <<rssSamples<<endl;
571 
572  {
573        VssRayContainer contributingRays;
574        mVssRays.GetContributingRays(contributingRays, 0);
575        mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
576        if (mExportRays) {
577          char filename[64];
578          sprintf(filename, "rss-crays-%04d.x3d", 0);
579          ExportRays(filename, contributingRays, mExportNumRays);
580        }
581  }
582 
583 
584  // viewcells->UpdatePVS(newVssRays);
585  Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
586  // cull viewcells with PVS > median (0.5f)
587  //mViewCellsManager->SetValidityPercentage(0, 0.5f);
588  //    mViewCellsManager->SetValidityPercentage(0, 1.0f);
589  Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
590 
591  mVssRays.PrintStatistics(mStats);
592  mViewCellsManager->PrintPvsStatistics(mStats);
593 
594 
595  if (0) {
596        char str[64];
597        sprintf(str, "tmp/v-");
598       
599        // visualization
600        const bool exportRays = true;
601        const bool exportPvs = true;
602        ObjectContainer objects;
603        mViewCellsManager->ExportSingleViewCells(objects,
604                                                                                         1000,
605                                                                                         false,
606                                                                                         exportPvs,
607                                                                                         exportRays,
608                                                                                         10000,
609                                                                                         str);
610  }
611 
612  if (renderer) {
613        ComputeRenderError();
614  }
615 
616  rssPass++;
617
618  //  mDistributions.resize(1);
619
620  mRssTree = new RssTree;
621  mRssTree->SetPass(mPass);
622 
623  /// compute view cell contribution of rays if view cells manager already constructed
624  //  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
625
626  if (mUseRssTree) {
627        mRssTree->Construct(mObjects, mVssRays);
628       
629        mRssTree->stat.Print(mStats);
630        cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
631       
632        if (mExportRssTree) {
633          ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
634          ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
635          ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
636          ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
637          ExportRssTreeLeaves(mRssTree, 10);
638        }
639  }
640 
641  if (mExportPvs) {
642        ExportPvs("rss-pvs-initial.x3d", mRssTree);
643  }
644 
645 
646  // keep only rss
647  //  mDistributions.resize(1);
648
649  while (1) {
650
651        static SimpleRayContainer rays;
652        static VssRayContainer vssRays;
653        static VssRayContainer tmpVssRays;
654
655        cout<<"H1"<<endl<<flush;
656
657        //rays.reserve((int)(1.1f*mRssSamplesPerPass));
658
659        cout<<"H10"<<endl<<flush;
660
661        rays.clear();
662        vssRays.clear();
663        tmpVssRays.clear();
664       
665        int castRays = 0;
666
667        cout<<"H11"<<endl<<flush;
668
669        NormalizeRatios(mDistributions);
670       
671        long t1;
672        cout<<"H2"<<endl<<flush;
673        for (int i=0; i < mDistributions.size(); i++) {
674          t1 = GetTime();
675          rays.clear();
676          tmpVssRays.clear();
677          if (mDistributions[i]->mRatio != 0) {
678                GenerateRays(int(mRssSamplesPerPass*mDistributions[i]->mRatio),
679                                         *mDistributions[i],
680                                         rays);
681               
682                rays.NormalizePdf((float)rays.size());
683               
684                CastRays(rays, tmpVssRays, true, pruneInvalidRays);
685                castRays += (int)rays.size();
686               
687                cout<<"Computing sample contributions..."<<endl<<flush;
688               
689#if ADD_RAYS_IN_PLACE
690                float contribution =
691                  mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
692#else
693                float contribution =
694                  mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
695#endif
696                mDistributions[i]->mContribution = contribution;
697                mDistributions[i]->mTotalContribution += contribution;
698               
699                cout<<"done."<<endl;
700          }
701         
702          mDistributions[i]->mTime = TimeDiff(t1, GetTime());
703          //            mDistributions[i]->mRays = (int)rays.size();
704          mDistributions[i]->mRays = (int)tmpVssRays.size();
705          mDistributions[i]->mTotalRays = (int)tmpVssRays.size();
706
707          //            mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
708          vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
709        }
710       
711        EvaluateRatios(mDistributions);
712       
713        // add contributions of all rays at once...
714#if !ADD_RAYS_IN_PLACE
715        mViewCellsManager->AddSampleContributions(vssRays);
716#endif   
717       
718 
719        totalSamples += castRays;
720        rssSamples += (int)vssRays.size();
721       
722        cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
723                                                                                                                                                   mInitialSamples)<<"%\n";
724       
725       
726        long time = GetTime();
727        totalTime += TimeDiff(lastTime, time);
728        lastTime = time;
729       
730        mStats <<
731          "#Pass\n" <<mPass<<endl<<
732          "#RssPass\n" <<rssPass<<endl<<
733          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
734          "#TotalSamples\n" <<totalSamples<<endl<<
735          "#RssSamples\n" <<rssSamples<<endl;
736       
737       
738        Debug<<"Print statistics...\n"<<flush;
739        vssRays.PrintStatistics(mStats);
740        mViewCellsManager->PrintPvsStatistics(mStats);
741        Debug<<"done.\n"<<flush;
742       
743        if (renderer && mPass > 0) {
744          Debug<<"Computing render errror..."<<endl<<flush;
745          char buf[100];
746          sprintf(buf, "snap/i-%02d-", mPass);
747         
748          renderer->SetSnapPrefix(buf);
749         
750          renderer->SetSnapErrorFrames(true);
751          ComputeRenderError();
752          Debug<<"done."<<endl<<flush;
753        }
754       
755        // epxort rays before adding them to the tree -> some of them can be deleted
756       
757        if (mExportRays) {
758          Debug<<"Exporting rays..."<<endl<<flush;
759          char filename[64];
760          sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
761         
762          if (useRayBuffer)
763                vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
764         
765          ExportRays(filename, vssRays, mExportNumRays);
766         
767          // now export all contributing rays
768          VssRayContainer contributingRays;
769          vssRays.GetContributingRays(contributingRays, mPass);
770          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
771          sprintf(filename, "rss-crays-%04d.x3d", rssPass);
772          ExportRays(filename, contributingRays, mExportNumRays);
773
774          Debug<<"done."<<endl<<flush;
775        }
776
777       
778        // add rays to the tree after the viewcells have been cast to have their contributions
779        // already when adding into the tree
780        // do not add those rays which have too low or no contribution....
781        if (mUseRssTree) {
782          Debug<<"Adding rays...\n"<<flush;
783          mRssTree->AddRays(vssRays);
784          Debug<<"done.\n"<<flush;
785          if (mUpdateSubdivision) {
786                int updatePasses = 1;
787                if (mPass % updatePasses == 0) {
788                  Debug<<"Updating rss tree...\n"<<flush;
789                  int subdivided = mRssTree->UpdateSubdivision();
790                  Debug<<"done.\n"<<flush;
791                  cout<<"subdivided leafs = "<<subdivided<<endl;
792                  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
793                }
794          }
795        }
796       
797        if (mExportPvs) {
798          char filename[64];
799          sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
800          ExportPvs(filename, mRssTree);
801        }
802       
803       
804        if (!mUseRssTree)
805          CLEAR_CONTAINER(vssRays);
806        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
807       
808        if (totalSamples >= mRssSamples + mInitialSamples)
809          break;
810       
811       
812        rssPass++;
813        mPass++;
814        mRssTree->SetPass(mPass);
815  }
816 
817  if(0) {
818        VssRayContainer selectedRays;
819        int desired = mViewCellsManager->GetVisualizationSamples();
820       
821        mVssRays.SelectRays(desired, selectedRays);
822       
823        mViewCellsManager->Visualize(mObjects, selectedRays);
824  }
825 
826  // view cells after sampling
827  mViewCellsManager->PrintStatistics(Debug);
828
829  EvalViewCellHistogram();
830 
831  //-- render simulation after merge
832  cout << "\nEvaluating view cells render time after sampling ... ";
833 
834  mRenderSimulator->RenderScene();
835  SimulationStatistics ss;
836  mRenderSimulator->GetStatistics(ss);
837 
838  cout << " finished" << endl;
839  cout << ss << endl;
840  Debug << ss << endl;
841 
842  if (useRayBuffer && mExportRays) {
843        char filename[64];
844        sprintf(filename, "rss-rays-i.x3d");
845       
846        rayBuffer.resize(mPass);
847        ExportRayAnimation(filename, rayBuffer);
848  }
849 
850 
851  // do not delete rss tree now - can be used for visualization..
852#if 0
853  Debug<<"Deleting RSS tree...\n";
854  delete mRssTree;
855  Debug<<"Done.\n";
856#endif
857
858 
859  return true;
860}
861
862}
Note: See TracBrowser for help on using the repository browser.