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

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
305void
306RssPreprocessor::ComputeRenderError()
307{
308  // compute rendering error
309       
310  if (renderer && renderer->mPvsStatFrames) {
311        //      emit EvalPvsStat();
312        //      QMutex mutex;
313        //      mutex.lock();
314        //      renderer->mRenderingFinished.wait(&mutex);
315        //      mutex.unlock();
316       
317        renderer->EvalPvsStat();
318
319        mStats <<
320          "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
321          "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
322          "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
323          "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
324  }
325}
326
327void
328NormalizeRatios(vector<SamplingStrategy *> &distributions)
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 
361 
362  cout<<"New ratios: ";
363  for (i=0 ; i < distributions.size(); i++)
364        cout<<distributions[i]->mRatio<<" ";
365  cout<<endl;
366}
367
368void
369EvaluateRatios(vector<SamplingStrategy *> &distributions)
370{
371  // now evaluate the ratios for the next pass
372#define TIME_WEIGHT 0.0f
373  for (int i=0; i < distributions.size(); i++) {
374        distributions[i]->mRatio = distributions[i]->mContribution/
375          (Limits::Small +
376           (TIME_WEIGHT*distributions[i]->mTime +
377                (1 - TIME_WEIGHT)*distributions[i]->mRays)
378           );
379
380#if 1
381        distributions[i]->mRatio = sqr(distributions[i]->mRatio);
382#endif
383  }
384}
385
386
387
388bool
389RssPreprocessor::ComputeVisibility()
390{
391
392  Debug << "type: rss" << endl;
393
394  cout<<"Rss Preprocessor started\n"<<flush;
395  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
396
397  Randomize(0);
398
399  if (0) {
400        Exporter *exporter = Exporter::GetExporter("samples.wrl");
401    exporter->SetFilled();
402        Halton<4> halton;
403
404        //      for (int i=1; i < 7; i++)
405        //        cout<<i<<" prime= "<<FindPrime(i)<<endl;
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  }
428 
429 
430  cout<<"COMPUTATION THREAD="<<GetThread()->GetCurrentThreadId()<<endl<<flush;
431 
432  // use ray buffer to export ray animation
433  const bool useRayBuffer = false;
434 
435  vector<VssRayContainer> rayBuffer(50);
436 
437  long startTime = GetTime();
438  long lastTime;
439  float totalTime;
440 
441  int totalSamples = 0;
442
443  // if not already loaded, construct view cells from file
444  if (!mLoadViewCells)
445  {
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);
452  }
453
454 
455  int rssPass = 0;
456  int rssSamples = 0;
457
458  // now decode distribution string
459  char buff[1024];
460  Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions",
461                                                                                          buff);
462 
463  char *curr = buff;
464  mUseRssTree = false;
465
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;
501  }
502
503  mMixtureDistribution = new MixtureDistribution(*this);
504  mMixtureDistribution->mDistributions = mDistributions;
505  mMixtureDistribution->Init();
506  bool oldGenerator = false;
507
508  const int batch = 100000;
509 
510  if (mLoadInitialSamples) {
511        cout << "Loading samples from file ... ";
512        LoadSamples(mVssRays, mObjects);
513        cout << "finished\n" << endl;
514  } else {
515        SimpleRayContainer rays;
516       
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        }
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) {
563       
564        char filename[64];
565        sprintf(filename, "rss-rays-initial.x3d");
566        ExportRays(filename, mVssRays, mExportNumRays);
567
568        if (useRayBuffer)
569          mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
570       
571  }
572 
573  if (mStoreInitialSamples) {
574        cout << "Writing samples to file ... ";
575        ExportSamples(mVssRays);
576        cout << "finished\n" << endl;
577  }
578       
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);
599        }
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-");
617       
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) {
632        ComputeRenderError();
633  }
634 
635  rssPass++;
636
637  //  mDistributions.resize(1);
638
639  mRssTree = new RssTree;
640  mRssTree->SetPass(mPass);
641 
642  /// compute view cell contribution of rays if view cells manager already constructed
643  //  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
644
645  if (mUseRssTree) {
646        mRssTree->Construct(mObjects, mVssRays);
647       
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);
657        }
658  }
659 
660  if (mExportPvs) {
661        ExportPvs("rss-pvs-initial.x3d", mRssTree);
662  }
663 
664 
665  // keep only rss
666  //  mDistributions.resize(1);
667
668  while (1) {
669
670        static SimpleRayContainer rays;
671        static VssRayContainer vssRays;
672        static VssRayContainer tmpVssRays;
673
674        cout<<"H1"<<endl<<flush;
675
676        //rays.reserve((int)(1.1f*mRssSamplesPerPass));
677
678        cout<<"H10"<<endl<<flush;
679
680        rays.clear();
681        vssRays.clear();
682        tmpVssRays.clear();
683       
684        int castRays = 0;
685
686        cout<<"H11"<<endl<<flush;
687
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               
708#if ADD_RAYS_IN_PLACE
709                float contribution =
710                  mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
711#else
712                float contribution =
713                  mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
714#endif
715                mDistributions[i]->mContribution = contribution;
716                mDistributions[i]->mTotalContribution += contribution;
717               
718                cout<<"done."<<endl;
719          }
720         
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() );
728        }
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 
738        totalSamples += castRays;
739        rssSamples += (int)vssRays.size();
740       
741        cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
742                                                                                                                                                   mInitialSamples)<<"%\n";
743       
744       
745        long time = GetTime();
746        totalTime += TimeDiff(lastTime, time);
747        lastTime = time;
748       
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;
755       
756       
757        Debug<<"Print statistics...\n"<<flush;
758        vssRays.PrintStatistics(mStats);
759        mViewCellsManager->PrintPvsStatistics(mStats);
760        Debug<<"done.\n"<<flush;
761       
762        if (renderer && mPass > 0) {
763          Debug<<"Computing render errror..."<<endl<<flush;
764          char buf[100];
765          sprintf(buf, "snap/i-%02d-", mPass);
766         
767          renderer->SetSnapPrefix(buf);
768         
769          renderer->SetSnapErrorFrames(true);
770          ComputeRenderError();
771          Debug<<"done."<<endl<<flush;
772        }
773       
774        // epxort rays before adding them to the tree -> some of them can be deleted
775       
776        if (mExportRays) {
777          Debug<<"Exporting rays..."<<endl<<flush;
778          char filename[64];
779          sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
780         
781          if (useRayBuffer)
782                vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
783         
784          ExportRays(filename, vssRays, mExportNumRays);
785         
786          // now export all contributing rays
787          VssRayContainer contributingRays;
788          vssRays.GetContributingRays(contributingRays, mPass);
789          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
790          sprintf(filename, "rss-crays-%04d.x3d", rssPass);
791          ExportRays(filename, contributingRays, mExportNumRays);
792
793          Debug<<"done."<<endl<<flush;
794        }
795
796       
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....
800        if (mUseRssTree) {
801          Debug<<"Adding rays...\n"<<flush;
802          mRssTree->AddRays(vssRays);
803          Debug<<"done.\n"<<flush;
804          if (mUpdateSubdivision) {
805                int updatePasses = 1;
806                if (mPass % updatePasses == 0) {
807                  Debug<<"Updating rss tree...\n"<<flush;
808                  int subdivided = mRssTree->UpdateSubdivision();
809                  Debug<<"done.\n"<<flush;
810                  cout<<"subdivided leafs = "<<subdivided<<endl;
811                  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
812                }
813          }
814        }
815       
816        if (mExportPvs) {
817          char filename[64];
818          sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
819          ExportPvs(filename, mRssTree);
820        }
821       
822       
823        if (!mUseRssTree)
824          CLEAR_CONTAINER(vssRays);
825        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
826       
827        if (totalSamples >= mRssSamples + mInitialSamples)
828          break;
829       
830       
831        rssPass++;
832        mPass++;
833        mRssTree->SetPass(mPass);
834  }
835 
836  if(0) {
837        VssRayContainer selectedRays;
838        int desired = mViewCellsManager->GetVisualizationSamples();
839       
840        mVssRays.SelectRays(desired, selectedRays);
841       
842        mViewCellsManager->Visualize(mObjects, selectedRays);
843  }
844 
845  // view cells after sampling
846  mViewCellsManager->PrintStatistics(Debug);
847
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) {
862        char filename[64];
863        sprintf(filename, "rss-rays-i.x3d");
864       
865        rayBuffer.resize(mPass);
866        ExportRayAnimation(filename, rayBuffer);
867  }
868 
869 
870  // do not delete rss tree now - can be used for visualization..
871#if 0
872  Debug<<"Deleting RSS tree...\n";
873  delete mRssTree;
874  Debug<<"Done.\n";
875#endif
876
877 
878  return true;
879}
880
881}
Note: See TracBrowser for help on using the repository browser.