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

Revision 1004, 21.0 KB checked in by mattausch, 18 years ago (diff)

environment as a singleton

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
26RssPreprocessor::RssPreprocessor():
27  mPass(0),
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          //      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<<flush;
515
516        GenerateRays(mInitialSamples/4, SPATIAL_BOX_BASED_DISTRIBUTION, rays);
517        GenerateRays(mInitialSamples/4, OBJECT_BASED_DISTRIBUTION, rays);
518        GenerateRays(mInitialSamples/4, DIRECTION_BASED_DISTRIBUTION, rays);
519        GenerateRays(mInitialSamples/4, OBJECT_DIRECTION_BASED_DISTRIBUTION, rays);
520       
521        cout<<"Casting initial rays..."<<endl<<flush;
522        CastRays(rays, mVssRays);
523
524        ExportObjectRays(mVssRays, 1546);
525  }
526 
527  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
528 
529  rssSamples += (int)mVssRays.size();
530  totalSamples += mInitialSamples;
531  mPass++;
532 
533  if (mExportRays) {
534       
535        char filename[64];
536        sprintf(filename, "rss-rays-initial.x3d");
537        ExportRays(filename, mVssRays, mExportNumRays);
538       
539  }
540 
541  if (mStoreInitialSamples) {
542        cout << "Writing samples to file ... ";
543        ExportSamples(mVssRays);
544        cout << "finished\n" << endl;
545  }
546       
547  if (mUseViewcells) {
548
549        cout<<"Computing sample contributions..."<<endl<<flush;
550        // evaluate contributions of the intitial rays
551        mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
552        cout<<"done.\n"<<flush;
553       
554        mStats <<
555          "#Pass\n" <<mPass<<endl<<
556          "#RssPass\n" <<rssPass<<endl<<
557          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
558          "#TotalSamples\n" <<totalSamples<<endl<<
559          "#RssSamples\n" <<rssSamples<<endl;
560
561        {
562          VssRayContainer contributingRays;
563          mVssRays.GetContributingRays(contributingRays, mPass);
564          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
565        }
566       
567        mVssRays.PrintStatistics(mStats);
568        mViewCellsManager->PrintPvsStatistics(mStats);
569         
570        // viewcells->UpdatePVS(newVssRays);
571        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
572        // cull viewcells with PVS > median (0.5f)
573        //mViewCellsManager->SetValidityPercentage(0, 0.5f);
574        mViewCellsManager->SetValidityPercentage(0, 1.0f);
575        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
576       
577        ComputeRenderError();
578  }
579 
580  rssPass++;
581 
582  mRssTree = new RssTree;
583  mRssTree->SetPass(mPass);
584 
585  /// compute view cell contribution of rays if view cells manager already constructed
586  //  mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
587
588  if (mUseImportanceSampling) {
589       
590        mRssTree->Construct(mObjects, mVssRays);
591       
592        mRssTree->stat.Print(mStats);
593        cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
594       
595        if (mExportRssTree) {
596          ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
597          ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
598          ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
599          ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
600          ExportRssTreeLeaves(mRssTree, 10);
601        }
602       
603        if (mExportPvs) {
604          ExportPvs("rss-pvs-initial.x3d", mRssTree);
605        }
606  }
607 
608 
609  while (1) {
610        SimpleRayContainer rays;
611        VssRayContainer vssRays;
612        int castRays = 0;
613        if (mUseImportanceSampling) {
614          VssRayContainer tmpVssRays;
615
616          float ratios[] = {0.8f,0.1f,0.1f};
617
618          GenerateRays(mRssSamplesPerPass*ratios[0], RSS_BASED_DISTRIBUTION, rays);
619          CastRays(rays, tmpVssRays);
620          castRays += rays.size();
621          float c1 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
622          mStats<<"#RssRelContrib"<<endl<<c1/rays.size()<<endl;
623
624          vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
625          rays.clear();
626          tmpVssRays.clear();
627
628          if (ratios[1]!=0.0f) {
629                GenerateRays(mRssSamplesPerPass*ratios[1], SPATIAL_BOX_BASED_DISTRIBUTION, rays);
630                CastRays(rays, tmpVssRays);
631                castRays += rays.size();
632                float c2 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
633                mStats<<"#SpatialRelContrib"<<endl<<c2/rays.size()<<endl;
634               
635                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
636               
637                rays.clear();
638                tmpVssRays.clear();
639          }
640         
641         
642          if (ratios[2]!=0.0f) {
643                GenerateRays(mRssSamplesPerPass*ratios[2], DIRECTION_BASED_DISTRIBUTION, rays);
644                CastRays(rays, tmpVssRays);
645                castRays += rays.size();
646                float c3 = mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
647                mStats<<"#DirectionalRelContrib"<<endl<<c3/rays.size()<<endl;
648               
649                vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
650               
651                rays.clear();
652                tmpVssRays.clear();
653          }
654         
655          // add contributions of all rays at once...
656          mViewCellsManager->AddSampleContributions(vssRays);
657         
658        }
659        else {
660          int rayType = SPATIAL_BOX_BASED_DISTRIBUTION;
661          if (mObjectBasedSampling)
662                rayType = OBJECT_BASED_DISTRIBUTION;
663          else
664                if (mDirectionalSampling)
665                  rayType = DIRECTION_BASED_DISTRIBUTION;
666
667          cout<<"Generating rays..."<<endl;
668
669          GenerateRays(mRssSamplesPerPass, rayType, rays);
670          cout<<"done."<<endl;
671
672          cout<<"Casting rays..."<<endl;
673          CastRays(rays, vssRays);
674          cout<<"done."<<endl;
675          castRays += rays.size();
676          if (mUseViewcells) {
677                /// compute view cell contribution of rays
678                cout<<"Computing sample contributions..."<<endl;
679                mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
680                cout<<"done."<<endl;
681          }
682               
683         
684        }
685        totalSamples += castRays;
686        rssSamples += (int)vssRays.size();
687
688        cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
689                                                                                                                                                   mInitialSamples)<<"%\n";
690       
691       
692        mStats <<
693          "#Pass\n" <<mPass<<endl<<
694          "#RssPass\n" <<rssPass<<endl<<
695          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
696          "#TotalSamples\n" <<totalSamples<<endl<<
697          "#RssSamples\n" <<rssSamples<<endl;
698
699
700        if (mUseViewcells) {
701          vssRays.PrintStatistics(mStats);
702          mViewCellsManager->PrintPvsStatistics(mStats);
703        }
704
705        if (0 && mPass > 0) {
706          if (mUseImportanceSampling)
707                renderer->mSnapPrefix.sprintf("snap/i-%02d-", mPass);
708          else
709                renderer->mSnapPrefix.sprintf("snap/r-%02d-", mPass);
710          renderer->mSnapErrorFrames = true;
711        }
712
713        ComputeRenderError();
714       
715        // epxort rays before adding them to the tree -> some of them can be deleted
716
717        if (mExportRays) {
718          char filename[64];
719          if (mUseImportanceSampling)
720                sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
721          else
722                sprintf(filename, "rss-rays-%04d.x3d", rssPass);
723         
724          ExportRays(filename, vssRays, mExportNumRays);
725
726          // now export all contributing rays
727          VssRayContainer contributingRays;
728          vssRays.GetContributingRays(contributingRays, mPass);
729          mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
730          sprintf(filename, "rss-crays-%04d.x3d", rssPass);
731          ExportRays(filename, contributingRays, mExportNumRays);
732        }
733
734       
735        // add rays to the tree after the viewcells have been cast to have their contributions
736        // already when adding into the tree
737        // do not add those rays which have too low or no contribution....
738       
739        if (mUseImportanceSampling) {
740          mRssTree->AddRays(vssRays);
741         
742          if (mUpdateSubdivision) {
743                int updatePasses = 1;
744                if (mPass % updatePasses == 0) {
745                  int subdivided = mRssTree->UpdateSubdivision();
746                  cout<<"subdivided leafs = "<<subdivided<<endl;
747                  cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
748                }
749          }
750        }
751       
752        if (mExportPvs) {
753          char filename[64];
754          sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
755          ExportPvs(filename, mRssTree);
756        }
757       
758       
759        if (!mUseImportanceSampling)
760          CLEAR_CONTAINER(vssRays);
761        // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
762
763        if (totalSamples >= mRssSamples + mInitialSamples)
764          break;
765
766       
767        rssPass++;
768        mPass++;
769        mRssTree->SetPass(mPass);
770  }
771 
772  if (mUseViewcells) {
773
774          if(0)
775          {
776                  VssRayContainer selectedRays;
777                  int desired = mViewCellsManager->GetVisualizationSamples();
778
779                  mVssRays.SelectRays(desired, selectedRays);
780
781                  mViewCellsManager->Visualize(mObjects, selectedRays);
782          }
783         
784          // view cells after sampling
785          mViewCellsManager->PrintStatistics(Debug);
786     
787          //-- render simulation after merge
788          cout << "\nevaluating bsp view cells render time after sampling ... ";
789         
790          mRenderSimulator->RenderScene();
791          SimulationStatistics ss;
792          mRenderSimulator->GetStatistics(ss);
793         
794          cout << " finished" << endl;
795          cout << ss << endl;
796          Debug << ss << endl;
797       
798  }
799 
800  Debug<<"Deleting RSS tree...\n";
801  delete mRssTree;
802  Debug<<"Done.\n";
803
804
805  //  mViewCellsManager->ExportViewCells("visibility.xml",
806  //                                                                     true);
807 
808 
809  return true;
810}
811
812}
Note: See TracBrowser for help on using the repository browser.