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

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