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

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