source: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp @ 654

Revision 654, 20.4 KB checked in by mattausch, 18 years ago (diff)

removed bug in split queue

Line 
1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "VssPreprocessor.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 "VssTree.h"
11#include "ViewCellsManager.h"
12#include "RenderSimulator.h"
13#include "Beam.h"
14#include "GlRenderer.h"
15
16bool use2dSampling = false;
17bool useViewspacePlane = false;
18
19VssPreprocessor::VssPreprocessor():
20  mPass(0),
21  mVssRays()
22{
23  // this should increase coherence of the samples
24  environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
25  environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
26  environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
27  environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
28  environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
29 
30  environment->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
31  environment->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
32  environment->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
33  environment->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
34
35  useViewspacePlane = mUseViewSpaceBox; //hack
36
37  mStats.open("stats.log");
38}
39
40VssPreprocessor::~VssPreprocessor()
41{
42  CLEAR_CONTAINER(mVssRays);
43  DEL_PTR(mViewSpaceBox);
44}
45
46void
47VssPreprocessor::SetupRay(Ray &ray,
48                                                  const Vector3 &point,
49                                                  const Vector3 &direction
50                                                  )
51{
52  ray.Clear();
53  // do not store anything else then intersections at the ray
54  ray.Init(point, direction, Ray::LOCAL_RAY);
55}
56
57
58void
59VssPreprocessor::CastRays(
60                                                  SimpleRayContainer &rays,
61                                                  VssRayContainer &vssRays
62                                                  )
63{
64  for (int i=0; i < rays.size(); i++)
65        CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
66}
67
68
69int
70VssPreprocessor::CastRay(
71                                                 Vector3 &viewPoint,
72                                                 Vector3 &direction,
73                                                 VssRayContainer &vssRays
74                                                 )
75{
76
77    int hits = 0;
78  static Ray ray;
79  AxisAlignedBox3 box =  mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
80
81  AxisAlignedBox3 sbox = box;
82  sbox.Enlarge(Vector3(-Limits::Small));
83  if (!sbox.IsInside(viewPoint))
84        return 0;
85       
86  SetupRay(ray, viewPoint, direction);
87  // cast ray to KD tree to find intersection with other objects
88  Intersectable *objectA, *objectB;
89  Vector3 pointA, pointB;
90  float bsize = Magnitude(box.Size());
91
92
93        if (!mDetectEmptyViewSpace)
94          ray.mFlags &= ~Ray::CULL_BACKFACES;
95        else
96          ray.mFlags |= Ray::CULL_BACKFACES;
97
98  if (mKdTree->CastRay(ray)) {
99        objectA = ray.intersections[0].mObject;
100        pointA = ray.Extrap(ray.intersections[0].mT);
101  } else {
102        objectA = NULL;
103        // compute intersection with the scene bounding box
104        float tmin, tmax;
105        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
106          pointA = ray.Extrap(tmax);
107        else
108          return 0;
109  }
110
111       
112  if (mDetectEmptyViewSpace) {
113        SetupRay(ray, pointA, -direction);
114  } else
115        SetupRay(ray, viewPoint, -direction);
116 
117  if (!mDetectEmptyViewSpace)
118        ray.mFlags &= ~Ray::CULL_BACKFACES;
119  else
120         ray.mFlags |= Ray::CULL_BACKFACES;
121
122  if (mKdTree->CastRay(ray)) {
123        objectB = ray.intersections[0].mObject;
124        pointB = ray.Extrap(ray.intersections[0].mT);
125  } else {
126        objectB = NULL;
127        float tmin, tmax;
128        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
129          pointB = ray.Extrap(tmax);
130        else
131          return 0;
132  }
133 
134  //  if (objectA == NULL && objectB != NULL) {
135  if (mDetectEmptyViewSpace) {
136        // cast again to ensure that there is no objectA
137        SetupRay(ray, pointB, direction);
138          ray.mFlags |= Ray::CULL_BACKFACES;
139        if (mKdTree->CastRay(ray)) {
140          objectA = ray.intersections[0].mObject;
141          pointA = ray.Extrap(ray.intersections[0].mT);
142        }
143  }
144
145 
146  VssRay *vssRay  = NULL;
147
148  bool validSample = (objectA != objectB);
149  if (0 && mDetectEmptyViewSpace) {   // consider all samples valid
150        // check if the viewpoint lies on the line segment AB
151        if (Distance(pointA, pointB) <
152                Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small) {
153          validSample = false;
154        }
155  }
156       
157  if (validSample) {
158        if (objectA) {
159          vssRay = new VssRay(pointB,
160                                                  pointA,
161                                                  objectB,
162                                                  objectA,
163                                                  mPass
164                                                  );
165          vssRays.push_back(vssRay);
166          hits ++;
167        }
168       
169        if (objectB) {
170          vssRay = new VssRay(pointA,
171                                                  pointB,
172                                                  objectA,
173                                                  objectB,
174                                                  mPass
175                                                  );
176          vssRays.push_back(vssRay);
177          hits ++;
178        }
179  }
180       
181  return hits;
182}
183
184
185Vector3
186VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
187{
188  AxisAlignedBox3 box;
189
190  if (viewSpaceBox)
191        box =*viewSpaceBox;
192  else
193        box = mKdTree->GetBox();
194
195  // shrink the box in the y direction
196  return box.GetRandomPoint();
197}
198
199Vector3
200VssPreprocessor::GetDirection(const Vector3 &viewpoint,
201                                                          AxisAlignedBox3 *viewSpaceBox
202                                                          )
203{
204  Vector3 point;
205  if (!use2dSampling)
206  {
207          if (0)
208          {
209                  Vector3 normal;
210                  int i = Random((int)mObjects.size());
211                  Intersectable *object = mObjects[i];
212                  object->GetRandomSurfacePoint(point, normal);
213          }
214          else
215                  point = mKdTree->GetBox().GetRandomPoint();
216        //        point = viewpoint + UniformRandomVector();
217  }
218  else
219  {
220          AxisAlignedBox3 box;
221
222          if (viewSpaceBox)
223                  box =*viewSpaceBox;
224          else
225                  box = mKdTree->GetBox();
226
227          point = box.GetRandomPoint();
228          point.y = viewpoint.y;
229  }
230
231  return point - viewpoint;
232}
233
234int
235VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
236                                                                                const int desiredSamples,
237                                                                                SimpleRayContainer &rays
238                                                                                )
239{
240  int num;
241  if (0) {
242        float minRayContribution;
243        float maxRayContribution;
244        float avgRayContribution;
245
246        vssTree->GetRayContributionStatistics(minRayContribution,
247                                                                                  maxRayContribution,
248                                                                                  avgRayContribution);
249
250        cout<<
251          "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
252          "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
253          "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
254
255        float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
256        num = vssTree->GenerateRays(p, rays);
257  } else {
258        int leaves = vssTree->stat.Leaves();
259        num = vssTree->GenerateRays(desiredSamples, leaves, rays);
260  }
261
262  cout<<"Generated "<<num<<" rays."<<endl;
263
264  return num;
265}
266
267
268bool
269VssPreprocessor::ExportRays(const char *filename,
270                                                        const VssRayContainer &vssRays,
271                                                        const int number
272                                                        )
273{
274  cout<<"Exporting vss rays..."<<endl<<flush;
275
276  Exporter *exporter = NULL;
277  exporter = Exporter::GetExporter(filename);
278  //    exporter->SetWireframe();
279  //    exporter->ExportKdTree(*mKdTree);
280  exporter->SetFilled();
281  exporter->ExportScene(mSceneGraph->mRoot);
282  exporter->SetWireframe();
283
284  if (mViewSpaceBox) {
285        exporter->SetForcedMaterial(RgbColor(1,0,1));
286        exporter->ExportBox(*mViewSpaceBox);
287        exporter->ResetForcedMaterial();
288  }
289
290  VssRayContainer rays;
291  vssRays.SelectRays(number, rays);
292 
293  exporter->ExportRays(rays, RgbColor(1, 0, 0));
294
295  delete exporter;
296
297  cout<<"done."<<endl<<flush;
298
299  return true;
300}
301
302
303bool
304VssPreprocessor::ExportVssTree(char *filename,
305                                                           VssTree *tree,
306                                                           const Vector3 &dir
307                                                           )
308{
309  Exporter *exporter = Exporter::GetExporter(filename);
310  exporter->SetFilled();
311  exporter->ExportScene(mSceneGraph->mRoot);
312  //  exporter->SetWireframe();
313  bool result = exporter->ExportVssTree2( *tree, dir );
314  delete exporter;
315  return result;
316}
317
318bool
319VssPreprocessor::ExportVssTreeLeaf(char *filename,
320                                                                   VssTree *tree,
321                                                                   VssTreeLeaf *leaf)
322{
323  Exporter *exporter = NULL;
324  exporter = Exporter::GetExporter(filename);
325  exporter->SetWireframe();
326  exporter->ExportKdTree(*mKdTree);
327
328  if (mViewSpaceBox) {
329        exporter->SetForcedMaterial(RgbColor(1,0,0));
330        exporter->ExportBox(*mViewSpaceBox);
331        exporter->ResetForcedMaterial();
332  }
333
334  exporter->SetForcedMaterial(RgbColor(0,0,1));
335  exporter->ExportBox(tree->GetBBox(leaf));
336  exporter->ResetForcedMaterial();
337
338  VssRayContainer rays[4];
339  for (int i=0; i < leaf->rays.size(); i++) {
340        int k = leaf->rays[i].GetRayClass();
341        rays[k].push_back(leaf->rays[i].mRay);
342  }
343
344  // SOURCE RAY
345  exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
346  // TERMINATION RAY
347  exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
348  // PASSING_RAY
349  exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
350  // CONTAINED_RAY
351  exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
352
353  delete exporter;
354  return true;
355}
356
357void
358VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
359{
360  vector<VssTreeLeaf *> leaves;
361  tree->CollectLeaves(leaves);
362
363  int num = 0;
364  int i;
365  float p = number / (float)leaves.size();
366  for (i=0; i < leaves.size(); i++) {
367        if (RandomValue(0,1) < p) {
368          char filename[64];
369          sprintf(filename, "vss-leaf-%04d.x3d", num);
370          ExportVssTreeLeaf(filename, tree, leaves[i]);
371          num++;
372        }
373        if (num >= number)
374          break;
375  }
376}
377#include "ViewCellBsp.h"
378
379void VssPreprocessor::TestBeamCasting(VssTree *tree,
380                                                                          ViewCellsManager *vm,
381                                                                          const ObjectContainer &objects)
382{
383        //debuggerWidget = new GlDebuggerWidget(renderer);
384        //  renderer->resize(640, 480);
385        //debuggerWidget->resize(640, 480);
386
387        vector<VssTreeLeaf *> leaves;
388        tree->CollectLeaves(leaves);
389
390        Exporter *exporter = Exporter::GetExporter("shafts.x3d");
391
392        exporter->SetWireframe();
393        exporter->ExportGeometry(objects);
394        exporter->SetFilled();
395        //Randomize();
396        debuggerWidget = new GlDebuggerWidget(renderer);
397
398        /*debuggerWidget->mBeam = beam;
399        debuggerWidget->mSourceObject = sourceObj;
400        debuggerWidget->mSamples = 10000;
401       
402        Debug << "showing window" << endl;
403        debuggerWidget->show();*/
404       
405        renderer->makeCurrent();
406
407        for (int i = 0; i < 10; ++ i)
408        {
409                Beam beam;
410                Intersectable *sourceObj = mObjects[5];
411
412                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
413                VssTreeLeaf *leaf = leaves[index];
414
415                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
416                AxisAlignedBox3 box = tree->GetBBox(leaf);
417               
418                beam.Construct(box, dirBox);
419
420                // collect kd leaves and view cells
421                mKdTree->CastBeam(beam);
422                vm->CastBeam(beam);
423
424                Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
425                          << (int)beam.mKdNodes.size() << " kd nodes" << endl;
426
427                BeamSampleStatistics stats;
428
429                renderer->SampleBeamContributions(sourceObj,
430                                                                                  beam,
431                                                                                  200000,
432                                                                                  stats);
433
434                char s[64]; sprintf(s, "shaft%04d.png", i);
435
436                QImage image = renderer->toImage();
437                image.save(s, "PNG");
438                Debug << "beam statistics: " << stats << endl << endl;
439
440                if (1)
441                {
442                        AxisAlignedBox3 sbox = mSceneGraph->GetBox();
443                        Vector3 bmin = sbox.Min() - 150.0f;
444                        Vector3 bmax = sbox.Max() + 150.0f;
445                        AxisAlignedBox3 vbox(bmin, bmax);
446               
447                        exporter->ExportBeam(beam, vbox);
448                }
449
450                bool exportViewCells = false;
451               
452                if (exportViewCells)
453                {
454                        ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
455                       
456                        for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
457                        {
458                                BspNodeGeometry geom;
459                                AxisAlignedBox3 vbox;
460                                vbox.Initialize();
461                                vbox.Include((*it)->GetMesh());
462                       
463                                exporter->SetWireframe();
464                                exporter->ExportBox(vbox);
465                                exporter->SetFilled();
466                                exporter->ExportViewCell(*it);
467                        }
468
469                        /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
470                       
471                        for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
472                        {
473                                exporter->ExportBox(mKdTree->GetBox((*it)));
474                        }*/
475                }
476        }
477        /*while (1)
478        { debuggerWidget->repaint();
479        };*/
480        delete exporter;
481}
482
483
484float
485VssPreprocessor::GetAvgPvsSize(VssTree *tree,
486                                                           const vector<AxisAlignedBox3> &viewcells
487                                                           )
488{
489  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
490
491  int sum = 0;
492  for (it = viewcells.begin(); it != it_end; ++ it)
493        sum += tree->GetPvsSize(*it);
494
495  return sum/(float)viewcells.size();
496}
497
498bool
499VssPreprocessor::ComputeVisibility()
500{
501        Debug << "type: vss" << endl;
502
503  long startTime = GetTime();
504
505  int totalSamples = 0;
506
507
508  AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
509 
510  if (!useViewspacePlane) {
511        float size = 0.05f;
512        float s = 0.5f - size;
513        float olds = Magnitude(box->Size());
514        box->Enlarge(box->Size()*Vector3(-s));
515        Vector3 translation = Vector3(-olds*0.1f, 0, 0);
516        box->SetMin(box->Min() + translation);
517        box->SetMax(box->Max() + translation);
518  } else {
519
520        // sample city like heights
521        box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
522        box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
523  }
524
525  if (use2dSampling)
526        box->SetMax(1, box->Min(1));
527
528  cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
529  Debug << "use view space box=" << mUseViewSpaceBox << endl;
530
531  if (mUseViewSpaceBox)
532  {
533          if (0)
534          {
535                  mViewSpaceBox = box;
536          }
537          else
538          {
539                // HACK: enlarge in y directon
540                mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
541                //Vector3 pmin = mViewSpaceBox->Min();
542                Vector3 size = mViewSpaceBox->Size();
543                //size[1] *= 1.25;
544                Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
545
546                mViewSpaceBox->Enlarge(enlarge);
547          }
548
549          //Debug << "view space box: " << *mViewSpaceBox << endl;
550  }
551  else
552  {
553          mViewSpaceBox = NULL;
554  }
555 
556  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
557
558  mSceneGraph->CollectObjects(&mObjects);
559
560  //-- load view cells from file if requested
561  if (!mLoadViewCells)
562  {
563          mViewCellsManager->SetViewSpaceBox(vbox);
564          // construct view cells using it's own set of samples
565          mViewCellsManager->Construct(this);
566
567          //-- several visualizations and statistics
568          Debug << "view cells construction finished: " << endl;
569          mViewCellsManager->PrintStatistics(Debug);
570  }
571  else
572  {     
573          VssRayContainer dummies;
574          mViewCellsManager->Visualize(mObjects, dummies);
575          mViewCellsManager->ExportViewCells("test.xml");
576  }
577
578  VssTree *vssTree = NULL;
579 
580
581  long initialTime = GetTime();
582
583  if (mLoadInitialSamples)
584  {
585          cout << "Loading samples from file ... ";
586          LoadSamples(mVssRays, mObjects);
587          cout << "finished\n" << endl;
588          totalSamples = (int)mVssRays.size();
589  }
590  else
591  {
592       
593        while (totalSamples < mInitialSamples) {
594                int passContributingSamples = 0;
595                int passSampleContributions = 0;
596                int passSamples = 0;
597
598                int index = 0;
599
600                int sampleContributions;
601
602                int s = Min(mSamplesPerPass, mInitialSamples);
603                for (int k=0; k < s; k++) {
604                        // changed by matt
605                        Vector3 viewpoint;
606                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
607                        mViewCellsManager->GetViewPoint(viewpoint);
608                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
609
610                        sampleContributions = CastRay(viewpoint, direction, mVssRays);
611
612                        if (sampleContributions) {
613                                passContributingSamples ++;
614                                passSampleContributions += sampleContributions;
615                        }
616                        passSamples++;
617                        totalSamples++;
618                }
619
620                mPass++;
621                int pvsSize = 0;
622                float avgRayContrib = (passContributingSamples > 0) ?
623                        passSampleContributions/(float)passContributingSamples : 0;
624
625                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
626                cout << "#TotalSamples=" << totalSamples/1000
627                        << "#SampleContributions=" << passSampleContributions << " ("
628                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
629                        << pvsSize/(float)mObjects.size() << endl
630                        << "avg ray contrib=" << avgRayContrib << endl;
631
632                mStats <<
633                        "#Pass\n" <<mPass<<endl<<
634                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
635                        "#TotalSamples\n" << totalSamples<< endl<<
636                        "#SampleContributions\n" << passSampleContributions << endl <<
637                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
638                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
639                        "#AvgRayContrib\n" << avgRayContrib << endl;
640          }
641 
642          cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
643
644
645         
646  }
647 
648
649  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
650  Debug << (int)mVssRays.size() << " rays generated in "
651            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
652
653  if (mStoreInitialSamples)
654  {
655          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
656          ExportSamples(mVssRays);
657          cout << "finished\n" << endl;
658
659          /*VssRayContainer dummyRays;
660          LoadSamples(dummyRays, mObjects);
661          Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
662
663          for (int i = 0; i < (int)mVssRays.size(); ++ i)
664          {
665                  Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
666                  Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
667          }*/
668  }
669
670 
671  //int numExportRays = 5000;
672  int numExportRays = 0;
673
674  if (numExportRays) {
675        char filename[64];
676        sprintf(filename, "vss-rays-initial.x3d");
677        ExportRays(filename, mVssRays, numExportRays);
678  }
679
680  vssTree = new VssTree;
681  // viewcells = Construct(mVssRays);
682
683  vssTree->Construct(mVssRays, mViewSpaceBox);
684  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
685
686  if (0)
687  {
688          ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
689          ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
690          ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
691          ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
692          ExportVssTreeLeaves(vssTree, 10);
693  }
694
695  // viewcells->UpdatePVS(newVssRays);
696  // get viewcells as kd tree boxes
697  vector<AxisAlignedBox3> kdViewcells;
698  if (0) {
699        vector<KdLeaf *> leaves;
700        mKdTree->CollectLeaves(leaves);
701        vector<KdLeaf *>::const_iterator it;
702        int targetLeaves = 50;
703        float prob = targetLeaves/(float)leaves.size();
704        for (it = leaves.begin(); it != leaves.end(); ++it)
705          if (RandomValue(0.0f,1.0f) < prob)
706                kdViewcells.push_back(mKdTree->GetBox(*it));
707
708        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
709        cout<<"Initial average PVS size = "<<avgPvs<<endl;
710  }
711
712
713  int samples = 0;
714  int pass = 0;
715
716 
717  // cast view cell samples
718  while (samples < mVssSamples)
719  {
720       
721        int num = mVssSamplesPerPass;
722        SimpleRayContainer rays;
723        VssRayContainer vssRays;
724
725        if (!mUseImportanceSampling) {
726          for (int j=0; j < num; j++) {
727            // changed by matt
728                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
729                Vector3 viewpoint;
730                mViewCellsManager->GetViewPoint(viewpoint);
731                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
732                rays.push_back(SimpleRay(viewpoint, direction));
733          }
734        } else {
735          num = GenerateImportanceRays(vssTree, num, rays);
736        }
737
738        for (int i=0; i < rays.size(); i++)
739          CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
740
741        vssTree->AddRays(vssRays);
742
743        if (0) {
744          int subdivided = vssTree->UpdateSubdivision();
745          cout<<"subdivided leafs = "<<subdivided<<endl;
746        }
747
748        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
749        cout<<"Average PVS size = "<<avgPvs<<endl;
750
751        /// compute view cell contribution of rays
752        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
753       
754        if (numExportRays) {
755          char filename[64];
756          if (mUseImportanceSampling)
757                sprintf(filename, "vss-rays-i%04d.x3d", pass);
758          else
759                sprintf(filename, "vss-rays-%04d.x3d", pass);
760
761          ExportRays(filename, vssRays, numExportRays);
762        }
763
764        samples+=num;
765        float pvs = vssTree->GetAvgPvsSize();
766        cout<<"*****************************\n";
767        cout<<samples<<" avgPVS ="<<pvs<<endl;
768        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
769        cout<<"*****************************\n";
770//      if (samples >= mVssSamples) break;
771        pass ++;
772  }
773
774  if (mTestBeamSampling && mUseGlRenderer)
775  {     
776          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
777  }
778
779  if (0)
780          Debug << vssTree->stat << endl;
781
782  if (0)
783  {
784        VssRayContainer viewCellRays;
785 
786        // compute rays used for view cells construction
787        const int numRays = mViewCellsManager->GetVisualizationSamples();
788
789        vssTree->CollectRays(viewCellRays, numRays);
790  }
791
792  //-- render simulation after merge
793  cout << "\nevaluating bsp view cells render time after sampling ... ";
794  Debug << "\nStatistics after sampling: " << endl;
795
796  mRenderSimulator->RenderScene();
797  SimulationStatistics ss;
798  mRenderSimulator->GetStatistics(ss);
799 
800  cout << " finished" << endl;
801  cout << ss << endl;
802  Debug << ss << endl;
803
804  delete vssTree;
805 
806  return true;
807}
Note: See TracBrowser for help on using the repository browser.