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

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