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

Revision 1563, 24.1 KB checked in by mattausch, 18 years ago (diff)

fixed bug with view space box

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