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

Revision 1723, 22.9 KB checked in by mattausch, 18 years ago (diff)
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[3])
369{
370  int i;
371  float sumRatios;
372  sumRatios = ratios[0] + ratios[1] + ratios[2];
373  if (sumRatios == 0.0f) {
374        ratios[0] = ratios[1] = ratios[2] = 1.0f;
375        sumRatios = 3.0f;
376  }
377 
378  for (i=0 ; i < 3; 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 < 3; i++)
390        if (ratios[i] < MIN_RATIO)
391          ratios[i] = MIN_RATIO;
392
393  sumRatios = ratios[0] + ratios[1] + ratios[2];
394  for (i=0 ; i < 3; 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  if (mLoadInitialSamples) {
436        cout << "Loading samples from file ... ";
437        LoadSamples(mVssRays, mObjects);
438        cout << "finished\n" << endl;
439  } else {
440        SimpleRayContainer rays;
441
442        cout<<"Generating initial rays..."<<endl<<flush;
443
444        if (mUseImportanceSampling) {
445          GenerateRays(mInitialSamples/2, SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays);
446          //      GenerateRays(mInitialSamples/3, SamplingStrategy::OBJECT_BASED_DISTRIBUTION, rays);
447          GenerateRays(mInitialSamples/2, SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays);
448          //    GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
449        } else {
450                int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
451          if (mObjectBasedSampling)
452                rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
453          else
454                if (mDirectionalSampling)
455                  rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
456          cout<<"Generating rays..."<<endl;
457          GenerateRays(mRssSamplesPerPass, rayType, rays);
458        }
459       
460       
461        cout<<"Casting initial rays..."<<endl<<flush;
462        CastRays(rays, mVssRays, true);
463
464        ExportObjectRays(mVssRays, 1546);
465  }
466 
467  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
468 
469  rssSamples += (int)mVssRays.size();
470  totalSamples += mInitialSamples;
471  mPass++;
472 
473  if (mExportRays) {
474       
475        char filename[64];
476        sprintf(filename, "rss-rays-initial.x3d");
477        ExportRays(filename, mVssRays, mExportNumRays);
478
479        if (useRayBuffer)
480          mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
481       
482  }
483 
484  if (mStoreInitialSamples) {
485        cout << "Writing samples to file ... ";
486        ExportSamples(mVssRays);
487        cout << "finished\n" << endl;
488  }
489       
490  if (mUseViewcells) {
491
492        cout<<"Computing sample contributions..."<<endl<<flush;
493        // evaluate contributions of the intitial rays
494        mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
495        cout<<"done.\n"<<flush;
496
497        long time = GetTime();
498        totalTime = TimeDiff(startTime, time)*1e-3f;
499        lastTime = time;
500       
501        mStats <<
502          "#Pass\n" <<mPass<<endl<<
503          "#RssPass\n" <<rssPass<<endl<<
504          "#Time\n" << totalTime <<endl<<
505          "#TotalSamples\n" <<totalSamples<<endl<<
506          "#RssSamples\n" <<rssSamples<<endl;
507
508        {
509          VssRayContainer contributingRays;
510          mVssRays.GetContributingRays(contributingRays, 0);
511          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
512          if (mExportRays) {
513                char filename[64];
514                sprintf(filename, "rss-crays-%04d.x3d", 0);
515                ExportRays(filename, contributingRays, mExportNumRays);
516          }
517        }
518       
519        mVssRays.PrintStatistics(mStats);
520        mViewCellsManager->PrintPvsStatistics(mStats);
521         
522        // viewcells->UpdatePVS(newVssRays);
523        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
524        // cull viewcells with PVS > median (0.5f)
525        //mViewCellsManager->SetValidityPercentage(0, 0.5f);
526        mViewCellsManager->SetValidityPercentage(0, 1.0f);
527        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
528       
529        ComputeRenderError();
530  }
531 
532  rssPass++;
533 
534  mRssTree = new RssTree;
535  mRssTree->SetPass(mPass);
536 
537  /// compute view cell contribution of rays if view cells manager already constructed
538  //  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
539
540  if (mUseImportanceSampling) {
541
542#if USE_RSS_TREE       
543        mRssTree->Construct(mObjects, mVssRays);
544#endif
545       
546        mRssTree->stat.Print(mStats);
547        cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
548       
549        if (mExportRssTree) {
550          ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
551          ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
552          ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
553          ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
554          ExportRssTreeLeaves(mRssTree, 10);
555        }
556       
557        if (mExportPvs) {
558          ExportPvs("rss-pvs-initial.x3d", mRssTree);
559        }
560  }
561 
562 
563
564  while (1) {
565
566        static SimpleRayContainer rays;
567        static VssRayContainer vssRays;
568        static VssRayContainer tmpVssRays;
569
570        rays.reserve((int)(1.1f*mRssSamplesPerPass));
571
572        rays.clear();
573        vssRays.clear();
574        tmpVssRays.clear();
575
576        int castRays = 0;
577        if (mUseImportanceSampling) {
578
579#if USE_RSS_TREE       
580          static float ratios[] = {0.9f,0.05f,0.05f};
581#else
582          static float ratios[] = {0.0f,0.05f,0.05f};
583#endif
584          NormalizeRatios(ratios);
585                 
586          cout<<"New ratios: "<<ratios[0]<<" "<<ratios[1]<<" "<<ratios[2]<<endl;
587
588          //float ratios[] = {1.0f,0.0f,0.0f};
589         
590          int nrays[3];
591          float contributions[3];
592          float times[3];
593         
594          long t1;
595
596          t1 = GetTime();
597         
598          GenerateRays(int(mRssSamplesPerPass*ratios[0]),
599                                   SamplingStrategy::RSS_BASED_DISTRIBUTION,
600                                   rays);
601         
602          rays.NormalizePdf((float)rays.size());
603         
604          CastRays(rays, tmpVssRays, true);
605          castRays += (int)rays.size();
606#if ADD_RAYS_IN_PLACE
607          contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
608#else
609          contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
610#endif
611          times[0] = TimeDiff(t1, GetTime());
612          nrays[0] = (int)rays.size();
613         
614          mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
615         
616          vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
617          rays.clear();
618          tmpVssRays.clear();
619          if (ratios[1]!=0.0f) {
620                t1 = GetTime();
621                GenerateRays(int(mRssSamplesPerPass*ratios[1]), SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays);
622                CastRays(rays, tmpVssRays, true);
623                castRays += (int)rays.size();
624#if ADD_RAYS_IN_PLACE
625                contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
626#else
627                contributions[1] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
628#endif
629          times[1] = TimeDiff(t1, GetTime());
630          nrays[1] = (int)rays.size();
631
632          mStats<<"#SpatialRelContrib"<<endl<<contributions[1]/nrays[1]<<endl;
633               
634                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
635               
636                rays.clear();
637                tmpVssRays.clear();
638          }
639         
640         
641          if (ratios[2]!=0.0f) {
642                t1 = GetTime();
643                GenerateRays(int(mRssSamplesPerPass*ratios[2]), SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays);
644                CastRays(rays, tmpVssRays, true);
645                castRays += (int)rays.size();
646#if ADD_RAYS_IN_PLACE
647                contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
648#else
649                contributions[2] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
650#endif
651                times[2] = TimeDiff(t1, GetTime());
652                nrays[2] = (int)rays.size();
653               
654                mStats<<"#DirectionalRelContrib"<<endl<<contributions[2]/nrays[2]<<endl;
655               
656                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
657               
658                rays.clear();
659                tmpVssRays.clear();
660          }
661         
662         
663          // now evaluate the ratios for the next pass
664#define TIME_WEIGHT 0.5f
665
666#if 1
667          ratios[0] = sqr(contributions[0]/(TIME_WEIGHT*times[0] + (1 - TIME_WEIGHT)*nrays[0]));
668          ratios[1] = sqr(contributions[1]/(TIME_WEIGHT*times[1] + (1 - TIME_WEIGHT)*nrays[1]));
669          ratios[2] = sqr(contributions[2]/(TIME_WEIGHT*times[2] + (1 - TIME_WEIGHT)*nrays[2]));
670#else
671          ratios[0] = contributions[0]/(TIME_WEIGHT*times[0] + (1 - TIME_WEIGHT)*nrays[0]);
672          ratios[1] = contributions[1]/(TIME_WEIGHT*times[1] + (1 - TIME_WEIGHT)*nrays[1]);
673          ratios[2] = contributions[2]/(TIME_WEIGHT*times[2] + (1 - TIME_WEIGHT)*nrays[2]);
674#endif   
675
676#if !USE_RSS_TREE
677          ratios[0] = 0;
678#endif
679         
680          // add contributions of all rays at once...
681#if !ADD_RAYS_IN_PLACE
682          mViewCellsManager->AddSampleContributions(vssRays);
683#endif   
684        }
685        else {
686                int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
687          if (mObjectBasedSampling)
688                  rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
689          else
690                if (mDirectionalSampling)
691                        rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
692
693          cout<<"Generating rays..."<<endl;
694
695          GenerateRays(mRssSamplesPerPass, rayType, rays);
696          cout<<"done."<<endl;
697
698          cout<<"Casting rays..."<<endl;          CastRays(rays, vssRays, true);
699          cout<<"done."<<endl;
700          castRays += (int)rays.size();
701          if (mUseViewcells) {
702                /// compute view cell contribution of rays
703                cout<<"Computing sample contributions..."<<endl;
704                mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
705                cout<<"done."<<endl;
706          }
707               
708         
709        }
710        totalSamples += castRays;
711        rssSamples += (int)vssRays.size();
712
713        cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
714                                                                                                                                                   mInitialSamples)<<"%\n";
715       
716
717        long time = GetTime();
718        totalTime += TimeDiff(lastTime, time);
719        lastTime = time;
720       
721        mStats <<
722          "#Pass\n" <<mPass<<endl<<
723          "#RssPass\n" <<rssPass<<endl<<
724          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
725          "#TotalSamples\n" <<totalSamples<<endl<<
726          "#RssSamples\n" <<rssSamples<<endl;
727
728
729        if (mUseViewcells) {
730          Debug<<"Print statistics...\n"<<flush;
731          vssRays.PrintStatistics(mStats);
732          mViewCellsManager->PrintPvsStatistics(mStats);
733          Debug<<"done.\n"<<flush;
734        }
735
736        if (0 && mPass > 0) {
737                char buf[100];
738          if (mUseImportanceSampling)
739          {
740                sprintf(buf, "snap/i-%02d-", mPass);
741
742                renderer->SetSnapPrefix(buf);
743          }
744          else
745          {
746                sprintf(buf, "snap/r-%02d-", mPass);
747
748                renderer->SetSnapPrefix(buf);
749          }
750
751          renderer->SetSnapErrorFrames(true);
752        }
753
754        ComputeRenderError();
755       
756        // epxort rays before adding them to the tree -> some of them can be deleted
757
758        if (mExportRays) {
759          char filename[64];
760          if (mUseImportanceSampling)
761                sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
762          else
763                sprintf(filename, "rss-rays-%04d.x3d", rssPass);
764         
765         
766
767          if (useRayBuffer)
768                vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
769         
770          ExportRays(filename, vssRays, mExportNumRays);
771         
772          // now export all contributing rays
773          VssRayContainer contributingRays;
774          vssRays.GetContributingRays(contributingRays, mPass);
775          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
776          sprintf(filename, "rss-crays-%04d.x3d", rssPass);
777          ExportRays(filename, contributingRays, mExportNumRays);
778         
779        }
780
781       
782        // add rays to the tree after the viewcells have been cast to have their contributions
783        // already when adding into the tree
784        // do not add those rays which have too low or no contribution....
785
786#if USE_RSS_TREE       
787        if (mUseImportanceSampling) {
788          Debug<<"Adding rays...\n"<<flush;
789          mRssTree->AddRays(vssRays);
790          Debug<<"done.\n"<<flush;
791          if (mUpdateSubdivision) {
792                int updatePasses = 1;
793                if (mPass % updatePasses == 0) {
794                  Debug<<"Updating rss tree...\n"<<flush;
795                  int subdivided = mRssTree->UpdateSubdivision();
796                  Debug<<"done.\n"<<flush;
797                  cout<<"subdivided leafs = "<<subdivided<<endl;
798                  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
799                }
800          }
801        }
802#endif
803       
804        if (mExportPvs) {
805          char filename[64];
806          sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
807          ExportPvs(filename, mRssTree);
808        }
809       
810       
811        if (!mUseImportanceSampling)
812          CLEAR_CONTAINER(vssRays);
813        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
814
815        if (totalSamples >= mRssSamples + mInitialSamples)
816          break;
817       
818       
819        rssPass++;
820        mPass++;
821        mRssTree->SetPass(mPass);
822
823       
824  }
825 
826  if (mUseViewcells) {
827
828        if(0)
829          {
830                VssRayContainer selectedRays;
831                int desired = mViewCellsManager->GetVisualizationSamples();
832               
833                mVssRays.SelectRays(desired, selectedRays);
834               
835                mViewCellsManager->Visualize(mObjects, selectedRays);
836          }
837       
838        // view cells after sampling
839        mViewCellsManager->PrintStatistics(Debug);
840       
841        //-- render simulation after merge
842        cout << "\nevaluating bsp view cells render time after sampling ... ";
843       
844        mRenderSimulator->RenderScene();
845        SimulationStatistics ss;
846        mRenderSimulator->GetStatistics(ss);
847       
848        cout << " finished" << endl;
849        cout << ss << endl;
850        Debug << ss << endl;
851       
852  }
853
854
855  if (useRayBuffer&& mExportRays && mUseImportanceSampling) {
856        char filename[64];
857        sprintf(filename, "rss-rays-i.x3d");
858       
859        rayBuffer.resize(mPass);
860        ExportRayAnimation(filename, rayBuffer);
861  }
862         
863
864  // do not delete rss tree now - can be used for visualization..
865#if 0
866  Debug<<"Deleting RSS tree...\n";
867  delete mRssTree;
868  Debug<<"Done.\n";
869#endif
870
871  //mViewCellsManager->ExportViewCells("visibility.xml",
872  //                                                                     true);
873 
874 
875  return true;
876}
877
878}
Note: See TracBrowser for help on using the repository browser.