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

Revision 1283, 24.2 KB checked in by bittner, 18 years ago (diff)

mlrt tests

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