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

Revision 1966, 22.4 KB checked in by bittner, 17 years ago (diff)

samplign preprocessor updates, merge

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