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

Revision 871, 21.6 KB checked in by bittner, 18 years ago (diff)

RenderSampler?

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