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

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

changes for visualization of distant view space sampling

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