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

Revision 1737, 24.3 KB checked in by bittner, 18 years ago (diff)

visibility filter updates

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