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

Revision 1613, 21.9 KB checked in by bittner, 18 years ago (diff)

kd-tree hack active

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