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

Revision 1002, 21.6 KB checked in by mattausch, 18 years ago (diff)

debug run: fixing memory holes

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