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

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

vsposp debug version

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();
[1145]434// §§matt
435//      debuggerWidget = new GlDebuggerWidget(renderer);
[540]436
437        /*debuggerWidget->mBeam = beam;
438        debuggerWidget->mSourceObject = sourceObj;
439        debuggerWidget->mSamples = 10000;
440       
441        Debug << "showing window" << endl;
[1145]442        debuggerWidget->show();
[540]443       
[1145]444        renderer->makeCurrent();*/
[540]445
446        for (int i = 0; i < 10; ++ i)
[530]447        {
[540]448                Beam beam;
449                Intersectable *sourceObj = mObjects[5];
450
[531]451                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
452                VssTreeLeaf *leaf = leaves[index];
[530]453
[535]454                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
[531]455                AxisAlignedBox3 box = tree->GetBBox(leaf);
[532]456               
[535]457                beam.Construct(box, dirBox);
[532]458
459                // collect kd leaves and view cells
460                mKdTree->CastBeam(beam);
461                vm->CastBeam(beam);
462
[577]463                Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
464                          << (int)beam.mKdNodes.size() << " kd nodes" << endl;
[532]465
[530]466                BeamSampleStatistics stats;
[1145]467// §§matt
468/*              renderer->SampleBeamContributions(sourceObj,
[589]469                                                                                  beam,
470                                                                                  200000,
[531]471                                                                                  stats);
[530]472
[540]473                char s[64]; sprintf(s, "shaft%04d.png", i);
474
475                QImage image = renderer->toImage();
476                image.save(s, "PNG");
[532]477                Debug << "beam statistics: " << stats << endl << endl;
[1145]478*/
[540]479                if (1)
480                {
481                        AxisAlignedBox3 sbox = mSceneGraph->GetBox();
482                        Vector3 bmin = sbox.Min() - 150.0f;
483                        Vector3 bmax = sbox.Max() + 150.0f;
484                        AxisAlignedBox3 vbox(bmin, bmax);
[535]485               
[540]486                        exporter->ExportBeam(beam, vbox);
487                }
[535]488
[540]489                bool exportViewCells = false;
[535]490               
491                if (exportViewCells)
492                {
493                        ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
494                       
495                        for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
496                        {
497                                BspNodeGeometry geom;
498                                AxisAlignedBox3 vbox;
499                                vbox.Initialize();
[538]500                                vbox.Include((*it)->GetMesh());
501                       
502                                exporter->SetWireframe();
[535]503                                exporter->ExportBox(vbox);
[538]504                                exporter->SetFilled();
505                                exporter->ExportViewCell(*it);
[535]506                        }
[538]507
508                        /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
509                       
510                        for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
511                        {
512                                exporter->ExportBox(mKdTree->GetBox((*it)));
513                        }*/
[535]514                }
[530]515        }
[540]516        /*while (1)
517        { debuggerWidget->repaint();
518        };*/
[535]519        delete exporter;
[530]520}
521
[540]522
[434]523float
524VssPreprocessor::GetAvgPvsSize(VssTree *tree,
525                                                           const vector<AxisAlignedBox3> &viewcells
526                                                           )
527{
528  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
529
530  int sum = 0;
531  for (it = viewcells.begin(); it != it_end; ++ it)
532        sum += tree->GetPvsSize(*it);
[468]533
[434]534  return sum/(float)viewcells.size();
535}
536
[427]537bool
[372]538VssPreprocessor::ComputeVisibility()
539{
[579]540        Debug << "type: vss" << endl;
[468]541
[372]542  long startTime = GetTime();
[468]543
[372]544  int totalSamples = 0;
545
546
[1002]547  AxisAlignedBox3 box(mKdTree->GetBox());
[534]548 
[434]549  if (!useViewspacePlane) {
[446]550        float size = 0.05f;
[434]551        float s = 0.5f - size;
[1002]552        float olds = Magnitude(box.Size());
553        box.Enlarge(box.Size()*Vector3(-s));
[434]554        Vector3 translation = Vector3(-olds*0.1f, 0, 0);
[1002]555        box.SetMin(box.Min() + translation);
556        box.SetMax(box.Max() + translation);
[434]557  } else {
[468]558
[434]559        // sample city like heights
[1002]560        box.SetMin(1, box.Min(1) + box.Size(1)*0.2f);
561        box.SetMax(1, box.Min(1) + box.Size(1)*0.3f);
[434]562  }
[427]563
[434]564  if (use2dSampling)
[1002]565        box.SetMax(1, box.Min(1));
[468]566
[1052]567  cout << "use view space box=" << mUseViewSpaceBox<<endl;
[579]568
[1076]569 
[501]570  if (mUseViewSpaceBox)
[487]571  {
[674]572          if (!mEnlargeViewSpace)
[654]573          {
[1002]574                  mViewSpaceBox = new AxisAlignedBox3(box);
[654]575          }
[598]576          else
577          {
[750]578                  mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
[677]579
[1052]580                  if (0)
[750]581                  {
[1052]582                          // HACK: enlarge in y directon
[750]583                          Vector3 size = mViewSpaceBox->Size();
[1052]584                       
585                          size[1] *= 1.25;
586                          Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
587                          //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
[598]588
[1052]589                          mViewSpaceBox->Enlarge(enlarge);
[750]590                          mViewSpaceBox->SetMax(mViewSpaceBox->Max() + enlarge);
591                  }
592                  else
593                  {
[1052]594                          AxisAlignedBox3 tbox(*mViewSpaceBox);
595
596                          Vector3 size = mViewSpaceBox->Size();
597                          tbox.SetMax(0, mViewSpaceBox->Max(0) + size[0] * 0.5);
598                          tbox.SetMin(0, mViewSpaceBox->Min(0) + size[0]);
599                          *mViewSpaceBox = tbox;
600
[750]601                          // $$ JB temporary
[1052]602                          /*AxisAlignedBox3 tempbox = *mViewSpaceBox;
[750]603                         
[1002]604                          float s = tempbox.Size(0);
[750]605                         
[1002]606                          tempbox.Scale(0.8f);
607                          tempbox.SetMax(0, box.Max(0) + s*0.8f);
608                          tempbox.SetMin(0, box.Min(0) + s*0.8f);
[1052]609                          *mViewSpaceBox = tempbox;*/
[750]610                  }             
[598]611          }
612          //Debug << "view space box: " << *mViewSpaceBox << endl;
[487]613  }
[434]614  else
[487]615  {
[579]616          mViewSpaceBox = NULL;
[487]617  }
[534]618 
[577]619  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
620
[590]621  mSceneGraph->CollectObjects(&mObjects);
622
[518]623  //-- load view cells from file if requested
[577]624  if (!mLoadViewCells)
[574]625  {
[577]626          mViewCellsManager->SetViewSpaceBox(vbox);
[574]627          // construct view cells using it's own set of samples
628          mViewCellsManager->Construct(this);
[518]629
[574]630          //-- several visualizations and statistics
[587]631          Debug << "view cells construction finished: " << endl;
[574]632          mViewCellsManager->PrintStatistics(Debug);
633  }
[590]634  else
[651]635  {     
[590]636          VssRayContainer dummies;
637          mViewCellsManager->Visualize(mObjects, dummies);
[931]638          mViewCellsManager->ExportViewCells("test.xml", mViewCellsManager->GetExportPvs(), mObjects);
[590]639  }
[401]640
[651]641  VssTree *vssTree = NULL;
[590]642 
[468]643
[490]644  long initialTime = GetTime();
[468]645
[490]646  if (mLoadInitialSamples)
647  {
648          cout << "Loading samples from file ... ";
649          LoadSamples(mVssRays, mObjects);
650          cout << "finished\n" << endl;
651          totalSamples = (int)mVssRays.size();
652  }
653  else
654  {
[534]655       
656        while (totalSamples < mInitialSamples) {
[490]657                int passContributingSamples = 0;
658                int passSampleContributions = 0;
659                int passSamples = 0;
[468]660
[490]661                int index = 0;
[468]662
[490]663                int sampleContributions;
[468]664
[490]665                int s = Min(mSamplesPerPass, mInitialSamples);
666                for (int k=0; k < s; k++) {
667                        // changed by matt
668                        Vector3 viewpoint;
[534]669                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
[490]670                        mViewCellsManager->GetViewPoint(viewpoint);
671                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
[534]672
[490]673                        sampleContributions = CastRay(viewpoint, direction, mVssRays);
[468]674
[490]675                        if (sampleContributions) {
676                                passContributingSamples ++;
677                                passSampleContributions += sampleContributions;
678                        }
679                        passSamples++;
680                        totalSamples++;
681                }
[468]682
[490]683                mPass++;
684                int pvsSize = 0;
685                float avgRayContrib = (passContributingSamples > 0) ?
686                        passSampleContributions/(float)passContributingSamples : 0;
[468]687
[490]688                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
689                cout << "#TotalSamples=" << totalSamples/1000
[651]690                        << "#SampleContributions=" << passSampleContributions << " ("
[490]691                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
692                        << pvsSize/(float)mObjects.size() << endl
693                        << "avg ray contrib=" << avgRayContrib << endl;
[468]694
[490]695                mStats <<
696                        "#Pass\n" <<mPass<<endl<<
697                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
698                        "#TotalSamples\n" << totalSamples<< endl<<
699                        "#SampleContributions\n" << passSampleContributions << endl <<
700                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
701                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
702                        "#AvgRayContrib\n" << avgRayContrib << endl;
703          }
704 
705          cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
[534]706
707
708         
[490]709  }
[564]710 
711
[490]712  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
713  Debug << (int)mVssRays.size() << " rays generated in "
714            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
[401]715
[490]716  if (mStoreInitialSamples)
717  {
718          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
[508]719          ExportSamples(mVssRays);
[490]720          cout << "finished\n" << endl;
[491]721
722          /*VssRayContainer dummyRays;
723          LoadSamples(dummyRays, mObjects);
724          Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
725
726          for (int i = 0; i < (int)mVssRays.size(); ++ i)
727          {
728                  Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
729                  Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
730          }*/
[434]731  }
[468]732
[534]733 
[712]734  //int numExportRays = 2000;
735  int numExportRays = 0;
[372]736
[434]737  if (numExportRays) {
738        char filename[64];
739        sprintf(filename, "vss-rays-initial.x3d");
740        ExportRays(filename, mVssRays, numExportRays);
741  }
[468]742
[434]743  vssTree = new VssTree;
744  // viewcells = Construct(mVssRays);
[468]745
[434]746  vssTree->Construct(mVssRays, mViewSpaceBox);
747  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
[468]748
[1074]749  ExportRays("kdtree.x3d", mVssRays, 10);
750
[465]751  if (0)
752  {
753          ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
754          ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
755          ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
756          ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
757          ExportVssTreeLeaves(vssTree, 10);
758  }
[430]759
[434]760  // viewcells->UpdatePVS(newVssRays);
761  // get viewcells as kd tree boxes
762  vector<AxisAlignedBox3> kdViewcells;
763  if (0) {
764        vector<KdLeaf *> leaves;
765        mKdTree->CollectLeaves(leaves);
766        vector<KdLeaf *>::const_iterator it;
767        int targetLeaves = 50;
768        float prob = targetLeaves/(float)leaves.size();
769        for (it = leaves.begin(); it != leaves.end(); ++it)
770          if (RandomValue(0.0f,1.0f) < prob)
771                kdViewcells.push_back(mKdTree->GetBox(*it));
[468]772
[434]773        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
774        cout<<"Initial average PVS size = "<<avgPvs<<endl;
775  }
776
[468]777
[434]778  int samples = 0;
779  int pass = 0;
[448]780
[535]781 
[448]782  // cast view cell samples
[557]783  while (samples < mVssSamples)
784  {
785       
[434]786        int num = mVssSamplesPerPass;
787        SimpleRayContainer rays;
788        VssRayContainer vssRays;
[468]789
[434]790        if (!mUseImportanceSampling) {
791          for (int j=0; j < num; j++) {
[487]792            // changed by matt
793                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
794                Vector3 viewpoint;
795                mViewCellsManager->GetViewPoint(viewpoint);
[434]796                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
797                rays.push_back(SimpleRay(viewpoint, direction));
798          }
799        } else {
800          num = GenerateImportanceRays(vssTree, num, rays);
801        }
[468]802
[434]803        for (int i=0; i < rays.size(); i++)
804          CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
[468]805
[434]806        vssTree->AddRays(vssRays);
[468]807
[434]808        if (0) {
809          int subdivided = vssTree->UpdateSubdivision();
810          cout<<"subdivided leafs = "<<subdivided<<endl;
811        }
[427]812
[434]813        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
814        cout<<"Average PVS size = "<<avgPvs<<endl;
[430]815
[517]816        /// compute view cell contribution of rays
[574]817        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
[517]818       
[434]819        if (numExportRays) {
820          char filename[64];
821          if (mUseImportanceSampling)
822                sprintf(filename, "vss-rays-i%04d.x3d", pass);
823          else
824                sprintf(filename, "vss-rays-%04d.x3d", pass);
[468]825
[434]826          ExportRays(filename, vssRays, numExportRays);
[401]827        }
[386]828
[434]829        samples+=num;
830        float pvs = vssTree->GetAvgPvsSize();
831        cout<<"*****************************\n";
832        cout<<samples<<" avgPVS ="<<pvs<<endl;
833        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
834        cout<<"*****************************\n";
[557]835//      if (samples >= mVssSamples) break;
836        pass ++;
[434]837  }
[448]838
[574]839  if (mTestBeamSampling && mUseGlRenderer)
840  {     
841          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
[553]842  }
843
[587]844  if (0)
845          Debug << vssTree->stat << endl;
[468]846
[574]847  if (0)
848  {
849        VssRayContainer viewCellRays;
[517]850 
[574]851        // compute rays used for view cells construction
852        const int numRays = mViewCellsManager->GetVisualizationSamples();
[468]853
[574]854        vssTree->CollectRays(viewCellRays, numRays);
[540]855  }
[535]856
[452]857  //-- render simulation after merge
[574]858  cout << "\nevaluating bsp view cells render time after sampling ... ";
[605]859  Debug << "\nStatistics after sampling: " << endl;
860
[468]861  mRenderSimulator->RenderScene();
862  SimulationStatistics ss;
863  mRenderSimulator->GetStatistics(ss);
[474]864 
[452]865  cout << " finished" << endl;
866  cout << ss << endl;
867  Debug << ss << endl;
[468]868
[434]869  delete vssTree;
[475]870 
[434]871  return true;
[466]872}
[697]873
[860]874}
Note: See TracBrowser for help on using the repository browser.