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

Revision 557, 21.0 KB checked in by mattausch, 19 years ago (diff)

started implementing merge visualization !not working!!

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