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

RevLine 
[447]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"
[451]11#include "ViewCellsManager.h"
[452]12#include "RenderSimulator.h"
[492]13#include "GlRenderer.h"
[1520]14#include "SamplingStrategy.h"
[447]15
[1520]16
[1715]17#define USE_RSS_TREE 1
18
[863]19namespace GtpVisibilityPreprocessor {
[860]20
21
[549]22static bool useViewSpaceBox = false;
[447]23static bool use2dSampling = false;
[516]24
25
26// not supported anymore!
[463]27static bool fromBoxVisibility = false;
[447]28
[1199]29#define ADD_RAYS_IN_PLACE 1
[1112]30 
[447]31RssPreprocessor::RssPreprocessor():
32  mVssRays()
33{
34  // this should increase coherence of the samples
[1004]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);
[464]40
[1004]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);
[464]48
[1004]49  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
50  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
51  Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
[464]52 
[447]53  mStats.open("stats.log");
[563]54  mRssTree = NULL;
55}
[492]56
[563]57
58bool
59RssPreprocessor::GenerateRays(
60                                                          const int number,
61                                                          const int sampleType,
62                                                          SimpleRayContainer &rays
63                                                          )
64{
65  bool result = false;
[1715]66 
67  Debug<<"Generate rays...\n"<<flush;
[563]68
69  switch (sampleType) {
[1520]70  case SamplingStrategy::RSS_BASED_DISTRIBUTION:
71  case SamplingStrategy::RSS_SILHOUETTE_BASED_DISTRIBUTION:
[563]72        if (mRssTree) {
73          GenerateImportanceRays(mRssTree, number, rays);
74          result = true;
75        }
76        break;
[752]77       
[563]78  default:
79        result = Preprocessor::GenerateRays(number, sampleType, rays);
80  }
81
[1112]82  //  rays.NormalizePdf();
[1715]83  Debug<<"done.\n"<<flush;
84
[563]85  return result;
[447]86}
87
[563]88
89
[1715]90
[447]91RssPreprocessor::~RssPreprocessor()
92{
[492]93  // mVssRays get deleted in the tree
94  //  CLEAR_CONTAINER(mVssRays);
[447]95}
96
97
98
[576]99void
100RssPreprocessor::ExportObjectRays(VssRayContainer &rays,
101                                                                  const int objectId)
102{
103  ObjectContainer::const_iterator oi;
[447]104
[576]105  Intersectable *object = NULL;
106  for (oi = mObjects.begin(); oi != mObjects.end(); ++oi)
107        if (objectId == (*oi)->GetId()) {
108          object = *oi;
109          break;
110        }
[447]111
[576]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
[447]137int
138RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
139                                                                                const int desiredSamples,
140                                                                                SimpleRayContainer &rays
141                                                                                )
142{
143  int num;
[463]144
[492]145  rssTree->UpdateTreeStatistics();
[463]146
[447]147  cout<<
[492]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;
[447]154 
155  if (0) {
[492]156        float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
[447]157        num = rssTree->GenerateRays(p, rays);
158  } else {
[459]159        int leaves = rssTree->stat.Leaves()/1;
[1112]160       
[447]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);
[1112]179  if (0) {
180        exporter->SetWireframe();
181        exporter->ExportKdTree(*mKdTree);
182  }
[447]183  exporter->SetFilled();
[556]184  // $$JB temporarily do not export the scene
[752]185  if (0)
[1328]186        exporter->ExportScene(mSceneGraph->GetRoot());
[447]187  exporter->SetWireframe();
188
[1563]189  if (1) {
[447]190        exporter->SetForcedMaterial(RgbColor(1,0,1));
[1112]191        exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
[447]192        exporter->ResetForcedMaterial();
193  }
[1112]194 
[492]195  VssRayContainer rays;
196 
[1112]197  vssRays.SelectRays(number, rays);
[447]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
[1112]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)
[1328]224        exporter->ExportScene(mSceneGraph->GetRoot());
[1112]225  exporter->SetWireframe();
[447]226
[1563]227  if (1) {
[1112]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
[447]243bool
244RssPreprocessor::ExportRssTree(char *filename,
245                                                           RssTree *tree,
246                                                           const Vector3 &dir
247                                                           )
248{
249  Exporter *exporter = Exporter::GetExporter(filename);
250  exporter->SetFilled();
[1328]251  exporter->ExportScene(mSceneGraph->GetRoot());
[447]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);
[459]338        exporter->SetWireframe();
339        exporter->ExportBox(rssTree->bbox);
340        exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
[447]341        delete exporter;
342  }
343}
344
[492]345
346void
347RssPreprocessor::ComputeRenderError()
348{
349  // compute rendering error
[1145]350       
[752]351  if (renderer && renderer->mPvsStatFrames) {
[496]352        //      emit EvalPvsStat();
353        //      QMutex mutex;
354        //      mutex.lock();
355        //      renderer->mRenderingFinished.wait(&mutex);
356        //      mutex.unlock();
[556]357
[496]358        renderer->EvalPvsStat();
[492]359        mStats <<
360          "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
361          "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
[713]362          "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
363          "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
[492]364  }
365}
366
[1112]367void
[1737]368NormalizeRatios(float ratios[4])
[1112]369{
370  int i;
371  float sumRatios;
[1737]372  sumRatios = ratios[0] + ratios[1] + ratios[2] + ratios[3];
[1112]373  if (sumRatios == 0.0f) {
374        ratios[0] = ratios[1] = ratios[2] = 1.0f;
[1737]375        sumRatios = 4.0f;
[1112]376  }
377 
[1737]378  for (i=0 ; i < 4; i++)
[1112]379        ratios[i]/=sumRatios;
[1715]380
381#define MIN_RATIO 0.1f
382
383#if USE_RSS_TREE
384  i = 0;
385#else
386  i = 1;
387#endif
388 
[1737]389  for (; i < 4; i++)
[1112]390        if (ratios[i] < MIN_RATIO)
391          ratios[i] = MIN_RATIO;
[563]392
[1737]393  sumRatios = ratios[0] + ratios[1] + ratios[2] + ratios[3];
394  for (i=0 ; i < 4; i++)
[1112]395        ratios[i]/=sumRatios;
396 
397}
[563]398
[447]399bool
400RssPreprocessor::ComputeVisibility()
401{
[556]402
[811]403  Debug << "type: rss" << endl;
[579]404
[464]405  cout<<"Rss Preprocessor started\n"<<flush;
[567]406  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[492]407
408  Randomize(0);
409 
[1694]410  const bool useRayBuffer = false;
411 
[1112]412  vector<VssRayContainer> rayBuffer(50);
413 
[447]414  long startTime = GetTime();
[1292]415  long lastTime;
416  float totalTime;
[447]417 
418  int totalSamples = 0;
419
[577]420  // if not already loaded, construct view cells from file
[1112]421  if (!mLoadViewCells)
422  {
[1563]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);
[574]429  }
[1563]430
[1112]431 
[492]432  int rssPass = 0;
433  int rssSamples = 0;
[1737]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));
[563]439 
[492]440  if (mLoadInitialSamples) {
441        cout << "Loading samples from file ... ";
442        LoadSamples(mVssRays, mObjects);
443        cout << "finished\n" << endl;
444  } else {
[563]445        SimpleRayContainer rays;
[1737]446       
[871]447        cout<<"Generating initial rays..."<<endl<<flush;
[1737]448       
[1199]449        if (mUseImportanceSampling) {
[1737]450          GenerateRays(mInitialSamples/3, SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays);
[1585]451          //      GenerateRays(mInitialSamples/3, SamplingStrategy::OBJECT_BASED_DISTRIBUTION, rays);
[1737]452          GenerateRays(mInitialSamples/3, SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays);
453          GenerateRays(mInitialSamples/3, SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION, rays);
[1199]454          //    GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
[1737]455         
456
[1199]457        } else {
[1737]458          int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
[1199]459          if (mObjectBasedSampling)
[1520]460                rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
[1199]461          else
462                if (mDirectionalSampling)
[1520]463                  rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
[1199]464          cout<<"Generating rays..."<<endl;
465          GenerateRays(mRssSamplesPerPass, rayType, rays);
466        }
467       
468       
[871]469        cout<<"Casting initial rays..."<<endl<<flush;
[1520]470        CastRays(rays, mVssRays, true);
[576]471
472        ExportObjectRays(mVssRays, 1546);
[563]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) {
[577]482       
[563]483        char filename[64];
484        sprintf(filename, "rss-rays-initial.x3d");
485        ExportRays(filename, mVssRays, mExportNumRays);
[1112]486
[1694]487        if (useRayBuffer)
488          mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
[577]489       
[563]490  }
491 
492  if (mStoreInitialSamples) {
493        cout << "Writing samples to file ... ";
494        ExportSamples(mVssRays);
495        cout << "finished\n" << endl;
496  }
[492]497       
[563]498  if (mUseViewcells) {
499
[871]500        cout<<"Computing sample contributions..."<<endl<<flush;
[563]501        // evaluate contributions of the intitial rays
[574]502        mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
[871]503        cout<<"done.\n"<<flush;
[1292]504
505        long time = GetTime();
[1723]506        totalTime = TimeDiff(startTime, time)*1e-3f;
[1292]507        lastTime = time;
[492]508       
[563]509        mStats <<
510          "#Pass\n" <<mPass<<endl<<
511          "#RssPass\n" <<rssPass<<endl<<
[1292]512          "#Time\n" << totalTime <<endl<<
[563]513          "#TotalSamples\n" <<totalSamples<<endl<<
514          "#RssSamples\n" <<rssSamples<<endl;
[569]515
516        {
517          VssRayContainer contributingRays;
[1112]518          mVssRays.GetContributingRays(contributingRays, 0);
[569]519          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
[1112]520          if (mExportRays) {
521                char filename[64];
522                sprintf(filename, "rss-crays-%04d.x3d", 0);
523                ExportRays(filename, contributingRays, mExportNumRays);
524          }
[569]525        }
[563]526       
527        mVssRays.PrintStatistics(mStats);
528        mViewCellsManager->PrintPvsStatistics(mStats);
[502]529         
[572]530        // viewcells->UpdatePVS(newVssRays);
531        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
532        // cull viewcells with PVS > median (0.5f)
[752]533        //mViewCellsManager->SetValidityPercentage(0, 0.5f);
534        mViewCellsManager->SetValidityPercentage(0, 1.0f);
[572]535        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
[563]536       
537        ComputeRenderError();
538  }
[492]539 
540  rssPass++;
541 
[563]542  mRssTree = new RssTree;
543  mRssTree->SetPass(mPass);
[492]544 
[527]545  /// compute view cell contribution of rays if view cells manager already constructed
[752]546  //  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
[527]547
[467]548  if (mUseImportanceSampling) {
[1715]549
550#if USE_RSS_TREE       
[563]551        mRssTree->Construct(mObjects, mVssRays);
[1715]552#endif
[467]553       
[563]554        mRssTree->stat.Print(mStats);
555        cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
556       
[467]557        if (mExportRssTree) {
[563]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);
[467]563        }
564       
565        if (mExportPvs) {
[563]566          ExportPvs("rss-pvs-initial.x3d", mRssTree);
[467]567        }
[459]568  }
[467]569 
[466]570 
[1715]571
[447]572  while (1) {
[1715]573
[1283]574        static SimpleRayContainer rays;
575        static VssRayContainer vssRays;
576        static VssRayContainer tmpVssRays;
[1715]577
578        rays.reserve((int)(1.1f*mRssSamplesPerPass));
579
[1283]580        rays.clear();
581        vssRays.clear();
582        tmpVssRays.clear();
[1715]583
[563]584        int castRays = 0;
585        if (mUseImportanceSampling) {
[567]586
[1715]587#if USE_RSS_TREE       
[1737]588          static float ratios[] = {0.9f,0.05f,0.05f,0.05f};
[1715]589#else
[1737]590          static float ratios[] = {0.0f,0.05f,0.05f,0.05f};
[1715]591#endif
592          NormalizeRatios(ratios);
593                 
[1737]594          cout<<"New ratios: "<<ratios[0]<<" "<<ratios[1]<<" "<<ratios[2]<<" "<<ratios[3]<<endl;
[1715]595
[1112]596          //float ratios[] = {1.0f,0.0f,0.0f};
597         
[1737]598          int nrays[4];
599          float contributions[4];
600          float times[4];
[1112]601         
[1150]602          long t1;
[567]603
[1112]604          t1 = GetTime();
605         
[1715]606          GenerateRays(int(mRssSamplesPerPass*ratios[0]),
607                                   SamplingStrategy::RSS_BASED_DISTRIBUTION,
608                                   rays);
609         
[1302]610          rays.NormalizePdf((float)rays.size());
[1112]611         
[1520]612          CastRays(rays, tmpVssRays, true);
[1297]613          castRays += (int)rays.size();
[1112]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());
[1302]620          nrays[0] = (int)rays.size();
[1112]621         
622          mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
623         
[563]624          vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
625          rays.clear();
626          tmpVssRays.clear();
[567]627          if (ratios[1]!=0.0f) {
[1112]628                t1 = GetTime();
[1520]629                GenerateRays(int(mRssSamplesPerPass*ratios[1]), SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION, rays);
630                CastRays(rays, tmpVssRays, true);
[1302]631                castRays += (int)rays.size();
[1112]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());
[1302]638          nrays[1] = (int)rays.size();
[1112]639
640          mStats<<"#SpatialRelContrib"<<endl<<contributions[1]/nrays[1]<<endl;
[567]641               
642                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
643               
644                rays.clear();
645                tmpVssRays.clear();
646          }
[563]647         
648         
[567]649          if (ratios[2]!=0.0f) {
[1112]650                t1 = GetTime();
[1520]651                GenerateRays(int(mRssSamplesPerPass*ratios[2]), SamplingStrategy::DIRECTION_BASED_DISTRIBUTION, rays);
652                CastRays(rays, tmpVssRays, true);
[1302]653                castRays += (int)rays.size();
[1112]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());
[1302]660                nrays[2] = (int)rays.size();
[567]661               
[1112]662                mStats<<"#DirectionalRelContrib"<<endl<<contributions[2]/nrays[2]<<endl;
663               
[567]664                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
665               
666                rays.clear();
667                tmpVssRays.clear();
668          }
[563]669         
[1737]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
[1112]692         
693          // now evaluate the ratios for the next pass
[1613]694#define TIME_WEIGHT 0.5f
[1112]695
[1715]696#if 1
[1613]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]));
[1737]700          ratios[3] = sqr(contributions[3]/(TIME_WEIGHT*times[3] + (1 - TIME_WEIGHT)*nrays[3]));
[1715]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]);
[1737]705          ratios[3] = contributions[3]/(TIME_WEIGHT*times[3] + (1 - TIME_WEIGHT)*nrays[3]);
706         
[1715]707#endif   
[1112]708
[1715]709#if !USE_RSS_TREE
710          ratios[0] = 0;
711#endif
[1112]712         
[563]713          // add contributions of all rays at once...
[1112]714#if !ADD_RAYS_IN_PLACE
[563]715          mViewCellsManager->AddSampleContributions(vssRays);
[1112]716#endif   
[563]717        }
718        else {
[1520]719                int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
[563]720          if (mObjectBasedSampling)
[1520]721                  rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
[563]722          else
723                if (mDirectionalSampling)
[1520]724                        rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
[752]725
726          cout<<"Generating rays..."<<endl;
727
[563]728          GenerateRays(mRssSamplesPerPass, rayType, rays);
[752]729          cout<<"done."<<endl;
730
[1715]731          cout<<"Casting rays..."<<endl;          CastRays(rays, vssRays, true);
[752]732          cout<<"done."<<endl;
[1302]733          castRays += (int)rays.size();
[563]734          if (mUseViewcells) {
735                /// compute view cell contribution of rays
[752]736                cout<<"Computing sample contributions..."<<endl;
[574]737                mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
[752]738                cout<<"done."<<endl;
[447]739          }
[563]740               
741         
[447]742        }
[563]743        totalSamples += castRays;
[503]744        rssSamples += (int)vssRays.size();
[467]745
[884]746        cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
747                                                                                                                                                   mInitialSamples)<<"%\n";
[492]748       
[1292]749
750        long time = GetTime();
751        totalTime += TimeDiff(lastTime, time);
752        lastTime = time;
[492]753       
[467]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
[492]761
[466]762        if (mUseViewcells) {
[1715]763          Debug<<"Print statistics...\n"<<flush;
[467]764          vssRays.PrintStatistics(mStats);
765          mViewCellsManager->PrintPvsStatistics(mStats);
[1715]766          Debug<<"done.\n"<<flush;
[466]767        }
[576]768
769        if (0 && mPass > 0) {
[1145]770                char buf[100];
[576]771          if (mUseImportanceSampling)
[1145]772          {
773                sprintf(buf, "snap/i-%02d-", mPass);
774
[1151]775                renderer->SetSnapPrefix(buf);
[1145]776          }
[576]777          else
[1145]778          {
779                sprintf(buf, "snap/r-%02d-", mPass);
780
[1151]781                renderer->SetSnapPrefix(buf);
[1145]782          }
783
[1151]784          renderer->SetSnapErrorFrames(true);
[576]785        }
786
[492]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         
[1112]798         
799
[1694]800          if (useRayBuffer)
801                vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
[1112]802         
[492]803          ExportRays(filename, vssRays, mExportNumRays);
[1112]804         
[492]805          // now export all contributing rays
806          VssRayContainer contributingRays;
807          vssRays.GetContributingRays(contributingRays, mPass);
[508]808          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
[492]809          sprintf(filename, "rss-crays-%04d.x3d", rssPass);
810          ExportRays(filename, contributingRays, mExportNumRays);
[1112]811         
[492]812        }
813
814       
[467]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....
[1715]818
819#if USE_RSS_TREE       
[467]820        if (mUseImportanceSampling) {
[1715]821          Debug<<"Adding rays...\n"<<flush;
[563]822          mRssTree->AddRays(vssRays);
[1715]823          Debug<<"done.\n"<<flush;
[467]824          if (mUpdateSubdivision) {
[492]825                int updatePasses = 1;
826                if (mPass % updatePasses == 0) {
[1715]827                  Debug<<"Updating rss tree...\n"<<flush;
[563]828                  int subdivided = mRssTree->UpdateSubdivision();
[1715]829                  Debug<<"done.\n"<<flush;
[492]830                  cout<<"subdivided leafs = "<<subdivided<<endl;
[563]831                  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
[492]832                }
[467]833          }
[447]834        }
[1715]835#endif
[467]836       
[464]837        if (mExportPvs) {
[447]838          char filename[64];
[467]839          sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
[563]840          ExportPvs(filename, mRssTree);
[447]841        }
[492]842       
[563]843       
[1199]844        if (!mUseImportanceSampling)
[492]845          CLEAR_CONTAINER(vssRays);
[563]846        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
[492]847
[467]848        if (totalSamples >= mRssSamples + mInitialSamples)
[447]849          break;
850       
[1613]851       
[467]852        rssPass++;
853        mPass++;
[563]854        mRssTree->SetPass(mPass);
[1613]855
856       
[447]857  }
858 
[464]859  if (mUseViewcells) {
[466]860
[1199]861        if(0)
[574]862          {
[1199]863                VssRayContainer selectedRays;
864                int desired = mViewCellsManager->GetVisualizationSamples();
865               
866                mVssRays.SelectRays(desired, selectedRays);
867               
868                mViewCellsManager->Visualize(mObjects, selectedRays);
[574]869          }
[556]870       
[1199]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       
[466]885  }
[1112]886
887
[1694]888  if (useRayBuffer&& mExportRays && mUseImportanceSampling) {
[1112]889        char filename[64];
890        sprintf(filename, "rss-rays-i.x3d");
891       
892        rayBuffer.resize(mPass);
893        ExportRayAnimation(filename, rayBuffer);
894  }
895         
[1581]896
897  // do not delete rss tree now - can be used for visualization..
898#if 0
[563]899  Debug<<"Deleting RSS tree...\n";
900  delete mRssTree;
901  Debug<<"Done.\n";
[1581]902#endif
[466]903
[1199]904  //mViewCellsManager->ExportViewCells("visibility.xml",
[871]905  //                                                                     true);
[447]906 
[871]907 
[447]908  return true;
909}
910
[871]911}
Note: See TracBrowser for help on using the repository browser.