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

Revision 752, 20.6 KB checked in by mattausch, 18 years ago (diff)

after rendering workshop submissioin
x3dparser can use def - use constructs
implemented improved evaluation (samples are only stored in leaves, only propagate pvs size)

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