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

Revision 1076, 22.0 KB checked in by mattausch, 18 years ago (diff)

version for performance testing

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