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

Revision 677, 21.1 KB checked in by bittner, 18 years ago (diff)

changes for visualization of distant view space sampling

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