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

Revision 1884, 23.7 KB checked in by bittner, 18 years ago (diff)

temporary version, rss preprocessor not functional

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