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

Revision 1785, 22.2 KB checked in by bittner, 18 years ago (diff)

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