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

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