source: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp @ 587

Revision 587, 19.7 KB checked in by mattausch, 18 years ago (diff)

updated vspkdtree for regular sudbivision

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