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

Revision 2002, 21.6 KB checked in by bittner, 17 years ago (diff)

renderer code updates for pixel error measurements

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