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

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