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

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