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

Revision 1824, 22.3 KB checked in by bittner, 18 years ago (diff)

global lines support

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