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

Revision 750, 21.5 KB checked in by mattausch, 18 years ago (diff)
Line 
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"
9#include "VssRay.h"
10#include "VssTree.h"
11#include "ViewCellsManager.h"
12#include "RenderSimulator.h"
13#include "Beam.h"
14#include "GlRenderer.h"
15#include "ViewCellBsp.h"
16
17bool use2dSampling = false;
18bool useViewspacePlane = false;
19
20VssPreprocessor::VssPreprocessor():
21  mPass(0),
22  mVssRays()
23{
24  // this should increase coherence of the samples
25  environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
26  environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
27  environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
28  environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
29  environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
30 
31  environment->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
32  environment->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
33  environment->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
34  environment->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
35  environment->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
36  environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
37 
38  useViewspacePlane = mUseViewSpaceBox; //hack
39
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
46  mStats.open("stats.log");
47}
48
49
50VssPreprocessor::~VssPreprocessor()
51{
52        CLEAR_CONTAINER(mVssRays);
53        DEL_PTR(mViewSpaceBox);
54}
55
56
57void
58VssPreprocessor::SetupRay(Ray &ray,
59                                                  const Vector3 &point,
60                                                  const Vector3 &direction
61                                                  )
62{
63  ray.Clear();
64  // do not store anything else then intersections at the ray
65  ray.Init(point, direction, Ray::LOCAL_RAY);
66}
67
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
80int
81VssPreprocessor::CastRay(
82                                                 Vector3 &viewPoint,
83                                                 Vector3 &direction,
84                                                 VssRayContainer &vssRays
85                                                 )
86{
87
88    int hits = 0;
89        static Ray ray;
90       
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;
103        // change matt
104        Vector3 pointA;
105        Vector3 pointB;
106
107        float bsize = Magnitude(box.Size());
108
109        if (!mDetectEmptyViewSpace)
110                ray.mFlags &= ~Ray::CULL_BACKFACES;
111        else
112                ray.mFlags |= Ray::CULL_BACKFACES;
113
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        }
129
130        // matt: point A could be undefined?
131        if (1 && mDetectEmptyViewSpace) {
132                SetupRay(ray, pointA, -direction);
133        } else
134                SetupRay(ray, viewPoint, -direction);
135 
136        if (!mDetectEmptyViewSpace)
137                ray.mFlags &= ~Ray::CULL_BACKFACES;
138        else
139                ray.mFlags |= Ray::CULL_BACKFACES;
140
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        }
156 
157        //bool thesame = objectA && (objectA == objectB);
158       
159        //  if (objectA == NULL && objectB != NULL) {
160        if (1 && mDetectEmptyViewSpace)
161        {
162                // cast again to ensure that there is no objectA
163                SetupRay(ray, pointB, direction);
164                ray.mFlags |= Ray::CULL_BACKFACES;
165               
166                if (mKdTree->CastRay(ray))
167                {
168                        objectA = ray.intersections[0].mObject;
169                        pointA = ray.Extrap(ray.intersections[0].mT);
170                }
171        }
172
173 
174        VssRay *vssRay  = NULL;
175
176        bool validSample = (objectA != objectB);
177       
178        //if (validSample && thesame) Debug << "warning!!" << endl;
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                }
189        }
190       
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                }
214        }
215       
216        return hits;
217}
218
219
220Vector3
221VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
222{
223  AxisAlignedBox3 box;
224
225  if (viewSpaceBox)
226        box =*viewSpaceBox;
227  else
228        box = mKdTree->GetBox();
229
230  // shrink the box in the y direction
231  return box.GetRandomPoint();
232}
233
234Vector3
235VssPreprocessor::GetDirection(const Vector3 &viewpoint,
236                                                          AxisAlignedBox3 *viewSpaceBox
237                                                          )
238{
239  Vector3 point;
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();
251        //        point = viewpoint + UniformRandomVector();
252  }
253  else
254  {
255          AxisAlignedBox3 box;
256
257          if (viewSpaceBox)
258                  box =*viewSpaceBox;
259          else
260                  box = mKdTree->GetBox();
261
262          point = box.GetRandomPoint();
263          point.y = viewpoint.y;
264  }
265
266  return point - viewpoint;
267}
268
269int
270VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
271                                                                                const int desiredSamples,
272                                                                                SimpleRayContainer &rays
273                                                                                )
274{
275  int num;
276  if (0) {
277        float minRayContribution;
278        float maxRayContribution;
279        float avgRayContribution;
280
281        vssTree->GetRayContributionStatistics(minRayContribution,
282                                                                                  maxRayContribution,
283                                                                                  avgRayContribution);
284
285        cout<<
286          "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
287          "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
288          "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
289
290        float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
291        num = vssTree->GenerateRays(p, rays);
292  } else {
293        int leaves = vssTree->stat.Leaves();
294        num = vssTree->GenerateRays(desiredSamples, leaves, rays);
295  }
296
297  cout<<"Generated "<<num<<" rays."<<endl;
298
299  return num;
300}
301
302
303bool
304VssPreprocessor::ExportRays(const char *filename,
305                                                        const VssRayContainer &vssRays,
306                                                        const int number
307                                                        )
308{
309  cout<<"Exporting vss rays..."<<endl<<flush;
310
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();
318
319  if (mViewSpaceBox) {
320        exporter->SetForcedMaterial(RgbColor(1,0,1));
321        exporter->ExportBox(*mViewSpaceBox);
322        exporter->ResetForcedMaterial();
323  }
324
325  VssRayContainer rays;
326  vssRays.SelectRays(number, rays);
327 
328  //exporter->ExportRays(rays, RgbColor(1, 0, 0));
329
330  delete exporter;
331
332  cout<<"done."<<endl<<flush;
333
334  return true;
335}
336
337
338bool
339VssPreprocessor::ExportVssTree(char *filename,
340                                                           VssTree *tree,
341                                                           const Vector3 &dir
342                                                           )
343{
344  Exporter *exporter = Exporter::GetExporter(filename);
345  exporter->SetFilled();
346  exporter->ExportScene(mSceneGraph->mRoot);
347  //  exporter->SetWireframe();
348  bool result = exporter->ExportVssTree2( *tree, dir );
349  delete exporter;
350  return result;
351}
352
353bool
354VssPreprocessor::ExportVssTreeLeaf(char *filename,
355                                                                   VssTree *tree,
356                                                                   VssTreeLeaf *leaf)
357{
358  Exporter *exporter = NULL;
359  exporter = Exporter::GetExporter(filename);
360  exporter->SetWireframe();
361  exporter->ExportKdTree(*mKdTree);
362
363  if (mViewSpaceBox) {
364        exporter->SetForcedMaterial(RgbColor(1,0,0));
365        exporter->ExportBox(*mViewSpaceBox);
366        exporter->ResetForcedMaterial();
367  }
368
369  exporter->SetForcedMaterial(RgbColor(0,0,1));
370  exporter->ExportBox(tree->GetBBox(leaf));
371  exporter->ResetForcedMaterial();
372
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  }
378
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));
387
388  delete exporter;
389  return true;
390}
391
392void
393VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
394{
395  vector<VssTreeLeaf *> leaves;
396  tree->CollectLeaves(leaves);
397
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++;
407        }
408        if (num >= number)
409          break;
410  }
411}
412
413
414void VssPreprocessor::TestBeamCasting(VssTree *tree,
415                                                                          ViewCellsManager *vm,
416                                                                          const ObjectContainer &objects)
417{
418        //debuggerWidget = new GlDebuggerWidget(renderer);
419        //  renderer->resize(640, 480);
420        //debuggerWidget->resize(640, 480);
421
422        vector<VssTreeLeaf *> leaves;
423        tree->CollectLeaves(leaves);
424
425        Exporter *exporter = Exporter::GetExporter("shafts.x3d");
426
427        exporter->SetWireframe();
428        exporter->ExportGeometry(objects);
429        exporter->SetFilled();
430        //Randomize();
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)
443        {
444                Beam beam;
445                Intersectable *sourceObj = mObjects[5];
446
447                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
448                VssTreeLeaf *leaf = leaves[index];
449
450                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
451                AxisAlignedBox3 box = tree->GetBBox(leaf);
452               
453                beam.Construct(box, dirBox);
454
455                // collect kd leaves and view cells
456                mKdTree->CastBeam(beam);
457                vm->CastBeam(beam);
458
459                Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
460                          << (int)beam.mKdNodes.size() << " kd nodes" << endl;
461
462                BeamSampleStatistics stats;
463
464                renderer->SampleBeamContributions(sourceObj,
465                                                                                  beam,
466                                                                                  200000,
467                                                                                  stats);
468
469                char s[64]; sprintf(s, "shaft%04d.png", i);
470
471                QImage image = renderer->toImage();
472                image.save(s, "PNG");
473                Debug << "beam statistics: " << stats << endl << endl;
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);
481               
482                        exporter->ExportBeam(beam, vbox);
483                }
484
485                bool exportViewCells = false;
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();
496                                vbox.Include((*it)->GetMesh());
497                       
498                                exporter->SetWireframe();
499                                exporter->ExportBox(vbox);
500                                exporter->SetFilled();
501                                exporter->ExportViewCell(*it);
502                        }
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                        }*/
510                }
511        }
512        /*while (1)
513        { debuggerWidget->repaint();
514        };*/
515        delete exporter;
516}
517
518
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);
529
530  return sum/(float)viewcells.size();
531}
532
533bool
534VssPreprocessor::ComputeVisibility()
535{
536        Debug << "type: vss" << endl;
537
538  long startTime = GetTime();
539
540  int totalSamples = 0;
541
542
543  AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
544 
545  if (!useViewspacePlane) {
546        float size = 0.05f;
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 {
554
555        // sample city like heights
556        box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
557        box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
558  }
559
560  if (use2dSampling)
561        box->SetMax(1, box->Min(1));
562
563  cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
564
565
566  if (mUseViewSpaceBox)
567  {
568          if (!mEnlargeViewSpace)
569          {
570                  mViewSpaceBox = box;
571          }
572          else
573          {
574                  // HACK: enlarge in y directon
575                  mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
576
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);
584
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                  }             
600          }
601          //Debug << "view space box: " << *mViewSpaceBox << endl;
602  }
603  else
604  {
605          mViewSpaceBox = NULL;
606  }
607 
608  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
609
610  mSceneGraph->CollectObjects(&mObjects);
611
612  //-- load view cells from file if requested
613  if (!mLoadViewCells)
614  {
615          mViewCellsManager->SetViewSpaceBox(vbox);
616          // construct view cells using it's own set of samples
617          mViewCellsManager->Construct(this);
618
619          //-- several visualizations and statistics
620          Debug << "view cells construction finished: " << endl;
621          mViewCellsManager->PrintStatistics(Debug);
622  }
623  else
624  {     
625          VssRayContainer dummies;
626          mViewCellsManager->Visualize(mObjects, dummies);
627          mViewCellsManager->ExportViewCells("test.xml");
628  }
629
630  VssTree *vssTree = NULL;
631 
632
633  long initialTime = GetTime();
634
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  {
644       
645        while (totalSamples < mInitialSamples) {
646                int passContributingSamples = 0;
647                int passSampleContributions = 0;
648                int passSamples = 0;
649
650                int index = 0;
651
652                int sampleContributions;
653
654                int s = Min(mSamplesPerPass, mInitialSamples);
655                for (int k=0; k < s; k++) {
656                        // changed by matt
657                        Vector3 viewpoint;
658                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
659                        mViewCellsManager->GetViewPoint(viewpoint);
660                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
661
662                        sampleContributions = CastRay(viewpoint, direction, mVssRays);
663
664                        if (sampleContributions) {
665                                passContributingSamples ++;
666                                passSampleContributions += sampleContributions;
667                        }
668                        passSamples++;
669                        totalSamples++;
670                }
671
672                mPass++;
673                int pvsSize = 0;
674                float avgRayContrib = (passContributingSamples > 0) ?
675                        passSampleContributions/(float)passContributingSamples : 0;
676
677                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
678                cout << "#TotalSamples=" << totalSamples/1000
679                        << "#SampleContributions=" << passSampleContributions << " ("
680                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
681                        << pvsSize/(float)mObjects.size() << endl
682                        << "avg ray contrib=" << avgRayContrib << endl;
683
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;
695
696
697         
698  }
699 
700
701  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
702  Debug << (int)mVssRays.size() << " rays generated in "
703            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
704
705  if (mStoreInitialSamples)
706  {
707          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
708          ExportSamples(mVssRays);
709          cout << "finished\n" << endl;
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          }*/
720  }
721
722 
723  //int numExportRays = 2000;
724  int numExportRays = 0;
725
726  if (numExportRays) {
727        char filename[64];
728        sprintf(filename, "vss-rays-initial.x3d");
729        ExportRays(filename, mVssRays, numExportRays);
730  }
731
732  vssTree = new VssTree;
733  // viewcells = Construct(mVssRays);
734
735  vssTree->Construct(mVssRays, mViewSpaceBox);
736  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
737
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  }
746
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));
759
760        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
761        cout<<"Initial average PVS size = "<<avgPvs<<endl;
762  }
763
764
765  int samples = 0;
766  int pass = 0;
767
768 
769  // cast view cell samples
770  while (samples < mVssSamples)
771  {
772       
773        int num = mVssSamplesPerPass;
774        SimpleRayContainer rays;
775        VssRayContainer vssRays;
776
777        if (!mUseImportanceSampling) {
778          for (int j=0; j < num; j++) {
779            // changed by matt
780                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
781                Vector3 viewpoint;
782                mViewCellsManager->GetViewPoint(viewpoint);
783                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
784                rays.push_back(SimpleRay(viewpoint, direction));
785          }
786        } else {
787          num = GenerateImportanceRays(vssTree, num, rays);
788        }
789
790        for (int i=0; i < rays.size(); i++)
791          CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
792
793        vssTree->AddRays(vssRays);
794
795        if (0) {
796          int subdivided = vssTree->UpdateSubdivision();
797          cout<<"subdivided leafs = "<<subdivided<<endl;
798        }
799
800        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
801        cout<<"Average PVS size = "<<avgPvs<<endl;
802
803        /// compute view cell contribution of rays
804        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
805       
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);
812
813          ExportRays(filename, vssRays, numExportRays);
814        }
815
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";
822//      if (samples >= mVssSamples) break;
823        pass ++;
824  }
825
826  if (mTestBeamSampling && mUseGlRenderer)
827  {     
828          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
829  }
830
831  if (0)
832          Debug << vssTree->stat << endl;
833
834  if (0)
835  {
836        VssRayContainer viewCellRays;
837 
838        // compute rays used for view cells construction
839        const int numRays = mViewCellsManager->GetVisualizationSamples();
840
841        vssTree->CollectRays(viewCellRays, numRays);
842  }
843
844  //-- render simulation after merge
845  cout << "\nevaluating bsp view cells render time after sampling ... ";
846  Debug << "\nStatistics after sampling: " << endl;
847
848  mRenderSimulator->RenderScene();
849  SimulationStatistics ss;
850  mRenderSimulator->GetStatistics(ss);
851 
852  cout << " finished" << endl;
853  cout << ss << endl;
854  Debug << ss << endl;
855
856  delete vssTree;
857 
858  return true;
859}
860
861
Note: See TracBrowser for help on using the repository browser.