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

Revision 1328, 18.9 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               
334                if (exportViewCells)
335                {
336                        ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
337                       
338                        for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
339                        {
340                                BspNodeGeometry geom;
341                                AxisAlignedBox3 vbox;
342                                vbox.Initialize();
[538]343                                vbox.Include((*it)->GetMesh());
344                       
345                                exporter->SetWireframe();
[535]346                                exporter->ExportBox(vbox);
[538]347                                exporter->SetFilled();
348                                exporter->ExportViewCell(*it);
[535]349                        }
[538]350
351                        /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
352                       
353                        for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
354                        {
355                                exporter->ExportBox(mKdTree->GetBox((*it)));
356                        }*/
[535]357                }
[530]358        }
[540]359        /*while (1)
360        { debuggerWidget->repaint();
361        };*/
[535]362        delete exporter;
[530]363}
364
[540]365
[434]366float
367VssPreprocessor::GetAvgPvsSize(VssTree *tree,
368                                                           const vector<AxisAlignedBox3> &viewcells
369                                                           )
370{
371  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
372
373  int sum = 0;
374  for (it = viewcells.begin(); it != it_end; ++ it)
375        sum += tree->GetPvsSize(*it);
[468]376
[434]377  return sum/(float)viewcells.size();
378}
379
[427]380bool
[372]381VssPreprocessor::ComputeVisibility()
382{
[579]383        Debug << "type: vss" << endl;
[468]384
[372]385  long startTime = GetTime();
[468]386
[372]387  int totalSamples = 0;
388
389
[1002]390  AxisAlignedBox3 box(mKdTree->GetBox());
[534]391 
[434]392  if (!useViewspacePlane) {
[446]393        float size = 0.05f;
[434]394        float s = 0.5f - size;
[1002]395        float olds = Magnitude(box.Size());
396        box.Enlarge(box.Size()*Vector3(-s));
[434]397        Vector3 translation = Vector3(-olds*0.1f, 0, 0);
[1002]398        box.SetMin(box.Min() + translation);
399        box.SetMax(box.Max() + translation);
[434]400  } else {
[468]401
[434]402        // sample city like heights
[1002]403        box.SetMin(1, box.Min(1) + box.Size(1)*0.2f);
404        box.SetMax(1, box.Min(1) + box.Size(1)*0.3f);
[434]405  }
[427]406
[434]407  if (use2dSampling)
[1002]408        box.SetMax(1, box.Min(1));
[468]409
[1221]410  cout << "use view space box=" << mUseViewSpaceBox << endl;
[579]411
[1076]412 
[501]413  if (mUseViewSpaceBox)
[487]414  {
[1221]415        //mViewSpaceBox = ConstructViewSpaceBox();
416
[674]417          if (!mEnlargeViewSpace)
[654]418          {
[1002]419                  mViewSpaceBox = new AxisAlignedBox3(box);
[654]420          }
[598]421          else
422          {
[750]423                  mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
[677]424
[1052]425                  if (0)
[750]426                  {
[1052]427                          // HACK: enlarge in y directon
[750]428                          Vector3 size = mViewSpaceBox->Size();
[1052]429                       
430                          size[1] *= 1.25;
431                          Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
432                          //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
[598]433
[1052]434                          mViewSpaceBox->Enlarge(enlarge);
[750]435                          mViewSpaceBox->SetMax(mViewSpaceBox->Max() + enlarge);
436                  }
437                  else
438                  {
[1052]439                          AxisAlignedBox3 tbox(*mViewSpaceBox);
440
441                          Vector3 size = mViewSpaceBox->Size();
[1264]442                          tbox.SetMax(0, mViewSpaceBox->Max(0) + size[0] * 0.5f);
[1052]443                          tbox.SetMin(0, mViewSpaceBox->Min(0) + size[0]);
444                          *mViewSpaceBox = tbox;
445
[750]446                          // $$ JB temporary
[1052]447                          /*AxisAlignedBox3 tempbox = *mViewSpaceBox;
[750]448                         
[1002]449                          float s = tempbox.Size(0);
[750]450                         
[1002]451                          tempbox.Scale(0.8f);
452                          tempbox.SetMax(0, box.Max(0) + s*0.8f);
453                          tempbox.SetMin(0, box.Min(0) + s*0.8f);
[1052]454                          *mViewSpaceBox = tempbox;*/
[750]455                  }             
[598]456          }
457          //Debug << "view space box: " << *mViewSpaceBox << endl;
[487]458  }
[434]459  else
[487]460  {
[579]461          mViewSpaceBox = NULL;
[487]462  }
[534]463 
[577]464  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
465
[590]466  mSceneGraph->CollectObjects(&mObjects);
467
[1264]468  //-- load view cells from file or reconstruct them
[577]469  if (!mLoadViewCells)
[574]470  {
[577]471          mViewCellsManager->SetViewSpaceBox(vbox);
[574]472          // construct view cells using it's own set of samples
473          mViewCellsManager->Construct(this);
[518]474
[574]475          //-- several visualizations and statistics
[587]476          Debug << "view cells construction finished: " << endl;
[574]477          mViewCellsManager->PrintStatistics(Debug);
478  }
[1264]479#if 1 // test successful view cells loading by exporting them again
480  else
[651]481  {     
[590]482          VssRayContainer dummies;
483          mViewCellsManager->Visualize(mObjects, dummies);
[1264]484          mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
[590]485  }
[1264]486#endif
[1311]487  VssTree *vssTree = NULL;
[468]488
[490]489  long initialTime = GetTime();
[468]490
[490]491  if (mLoadInitialSamples)
492  {
493          cout << "Loading samples from file ... ";
494          LoadSamples(mVssRays, mObjects);
495          cout << "finished\n" << endl;
496          totalSamples = (int)mVssRays.size();
497  }
498  else
499  {
[534]500        while (totalSamples < mInitialSamples) {
[490]501                int passContributingSamples = 0;
502                int passSampleContributions = 0;
503                int passSamples = 0;
[468]504
[490]505                int index = 0;
[468]506
[490]507                int sampleContributions;
[468]508
[490]509                int s = Min(mSamplesPerPass, mInitialSamples);
510                for (int k=0; k < s; k++) {
511                        // changed by matt
512                        Vector3 viewpoint;
[534]513                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
[490]514                        mViewCellsManager->GetViewPoint(viewpoint);
515                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
[534]516
[1251]517                        sampleContributions = CastRay(viewpoint, direction, 1, mVssRays, vbox);
[468]518
[490]519                        if (sampleContributions) {
520                                passContributingSamples ++;
521                                passSampleContributions += sampleContributions;
522                        }
523                        passSamples++;
524                        totalSamples++;
525                }
[468]526
[490]527                mPass++;
528                int pvsSize = 0;
529                float avgRayContrib = (passContributingSamples > 0) ?
530                        passSampleContributions/(float)passContributingSamples : 0;
[468]531
[490]532                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
533                cout << "#TotalSamples=" << totalSamples/1000
[651]534                        << "#SampleContributions=" << passSampleContributions << " ("
[490]535                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
536                        << pvsSize/(float)mObjects.size() << endl
537                        << "avg ray contrib=" << avgRayContrib << endl;
[468]538
[490]539                mStats <<
540                        "#Pass\n" <<mPass<<endl<<
541                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
542                        "#TotalSamples\n" << totalSamples<< endl<<
543                        "#SampleContributions\n" << passSampleContributions << endl <<
544                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
545                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
546                        "#AvgRayContrib\n" << avgRayContrib << endl;
547          }
548 
549          cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
[534]550
551
552         
[490]553  }
[564]554 
555
[490]556  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
557  Debug << (int)mVssRays.size() << " rays generated in "
558            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
[401]559
[490]560  if (mStoreInitialSamples)
561  {
562          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
[508]563          ExportSamples(mVssRays);
[490]564          cout << "finished\n" << endl;
[491]565
566          /*VssRayContainer dummyRays;
567          LoadSamples(dummyRays, mObjects);
568          Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
569
570          for (int i = 0; i < (int)mVssRays.size(); ++ i)
571          {
572                  Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
573                  Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
574          }*/
[434]575  }
[468]576
[534]577 
[712]578  //int numExportRays = 2000;
579  int numExportRays = 0;
[372]580
[434]581  if (numExportRays) {
582        char filename[64];
583        sprintf(filename, "vss-rays-initial.x3d");
584        ExportRays(filename, mVssRays, numExportRays);
585  }
[468]586
[434]587  vssTree = new VssTree;
588  // viewcells = Construct(mVssRays);
[468]589
[434]590  vssTree->Construct(mVssRays, mViewSpaceBox);
591  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
[468]592
[1074]593  ExportRays("kdtree.x3d", mVssRays, 10);
594
[465]595  if (0)
596  {
597          ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
598          ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
599          ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
600          ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
601          ExportVssTreeLeaves(vssTree, 10);
602  }
[430]603
[434]604  // viewcells->UpdatePVS(newVssRays);
605  // get viewcells as kd tree boxes
606  vector<AxisAlignedBox3> kdViewcells;
607  if (0) {
608        vector<KdLeaf *> leaves;
609        mKdTree->CollectLeaves(leaves);
610        vector<KdLeaf *>::const_iterator it;
611        int targetLeaves = 50;
612        float prob = targetLeaves/(float)leaves.size();
613        for (it = leaves.begin(); it != leaves.end(); ++it)
614          if (RandomValue(0.0f,1.0f) < prob)
615                kdViewcells.push_back(mKdTree->GetBox(*it));
[468]616
[434]617        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
618        cout<<"Initial average PVS size = "<<avgPvs<<endl;
619  }
620
[468]621
[434]622  int samples = 0;
623  int pass = 0;
[448]624
[535]625 
[448]626  // cast view cell samples
[557]627  while (samples < mVssSamples)
628  {
629       
[434]630        int num = mVssSamplesPerPass;
631        SimpleRayContainer rays;
632        VssRayContainer vssRays;
[468]633
[434]634        if (!mUseImportanceSampling) {
635          for (int j=0; j < num; j++) {
[487]636            // changed by matt
637                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
638                Vector3 viewpoint;
639                mViewCellsManager->GetViewPoint(viewpoint);
[434]640                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
641                rays.push_back(SimpleRay(viewpoint, direction));
642          }
643        } else {
644          num = GenerateImportanceRays(vssTree, num, rays);
645        }
[468]646
[434]647        for (int i=0; i < rays.size(); i++)
[1251]648          CastRay(rays[i].mOrigin, rays[i].mDirection, 1, vssRays, vbox);
[468]649
[434]650        vssTree->AddRays(vssRays);
[468]651
[434]652        if (0) {
653          int subdivided = vssTree->UpdateSubdivision();
654          cout<<"subdivided leafs = "<<subdivided<<endl;
655        }
[427]656
[434]657        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
658        cout<<"Average PVS size = "<<avgPvs<<endl;
[430]659
[517]660        /// compute view cell contribution of rays
[574]661        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
[517]662       
[434]663        if (numExportRays) {
664          char filename[64];
665          if (mUseImportanceSampling)
666                sprintf(filename, "vss-rays-i%04d.x3d", pass);
667          else
668                sprintf(filename, "vss-rays-%04d.x3d", pass);
[468]669
[434]670          ExportRays(filename, vssRays, numExportRays);
[401]671        }
[386]672
[434]673        samples+=num;
674        float pvs = vssTree->GetAvgPvsSize();
675        cout<<"*****************************\n";
676        cout<<samples<<" avgPVS ="<<pvs<<endl;
677        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
678        cout<<"*****************************\n";
[557]679//      if (samples >= mVssSamples) break;
680        pass ++;
[434]681  }
[448]682
[574]683  if (mTestBeamSampling && mUseGlRenderer)
684  {     
685          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
[553]686  }
687
[1221]688  if (0)  Debug << vssTree->stat << endl;
[468]689
[574]690  if (0)
691  {
692        VssRayContainer viewCellRays;
[1221]693        // compute rays used for view cells construction
[574]694        const int numRays = mViewCellsManager->GetVisualizationSamples();
695        vssTree->CollectRays(viewCellRays, numRays);
[540]696  }
[535]697
[452]698  //-- render simulation after merge
[574]699  cout << "\nevaluating bsp view cells render time after sampling ... ";
[605]700  Debug << "\nStatistics after sampling: " << endl;
701
[468]702  mRenderSimulator->RenderScene();
703  SimulationStatistics ss;
704  mRenderSimulator->GetStatistics(ss);
[474]705 
[452]706  cout << " finished" << endl;
707  cout << ss << endl;
708  Debug << ss << endl;
[468]709
[434]710  delete vssTree;
[475]711 
[434]712  return true;
[466]713}
[697]714
[1281]715}
Note: See TracBrowser for help on using the repository browser.