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

Revision 1145, 22.0 KB checked in by mattausch, 18 years ago (diff)

vsposp debug version

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::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
29  Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
30  Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
31  Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
32  Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
33 
34  Environment::GetSingleton()->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
35  Environment::GetSingleton()->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
36  Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
37  Environment::GetSingleton()->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
38  Environment::GetSingleton()->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
39  Environment::GetSingleton()->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// §§matt
435//      debuggerWidget = new GlDebuggerWidget(renderer);
436
437        /*debuggerWidget->mBeam = beam;
438        debuggerWidget->mSourceObject = sourceObj;
439        debuggerWidget->mSamples = 10000;
440       
441        Debug << "showing window" << endl;
442        debuggerWidget->show();
443       
444        renderer->makeCurrent();*/
445
446        for (int i = 0; i < 10; ++ i)
447        {
448                Beam beam;
449                Intersectable *sourceObj = mObjects[5];
450
451                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
452                VssTreeLeaf *leaf = leaves[index];
453
454                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
455                AxisAlignedBox3 box = tree->GetBBox(leaf);
456               
457                beam.Construct(box, dirBox);
458
459                // collect kd leaves and view cells
460                mKdTree->CastBeam(beam);
461                vm->CastBeam(beam);
462
463                Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
464                          << (int)beam.mKdNodes.size() << " kd nodes" << endl;
465
466                BeamSampleStatistics stats;
467// §§matt
468/*              renderer->SampleBeamContributions(sourceObj,
469                                                                                  beam,
470                                                                                  200000,
471                                                                                  stats);
472
473                char s[64]; sprintf(s, "shaft%04d.png", i);
474
475                QImage image = renderer->toImage();
476                image.save(s, "PNG");
477                Debug << "beam statistics: " << stats << endl << endl;
478*/
479                if (1)
480                {
481                        AxisAlignedBox3 sbox = mSceneGraph->GetBox();
482                        Vector3 bmin = sbox.Min() - 150.0f;
483                        Vector3 bmax = sbox.Max() + 150.0f;
484                        AxisAlignedBox3 vbox(bmin, bmax);
485               
486                        exporter->ExportBeam(beam, vbox);
487                }
488
489                bool exportViewCells = false;
490               
491                if (exportViewCells)
492                {
493                        ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
494                       
495                        for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
496                        {
497                                BspNodeGeometry geom;
498                                AxisAlignedBox3 vbox;
499                                vbox.Initialize();
500                                vbox.Include((*it)->GetMesh());
501                       
502                                exporter->SetWireframe();
503                                exporter->ExportBox(vbox);
504                                exporter->SetFilled();
505                                exporter->ExportViewCell(*it);
506                        }
507
508                        /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
509                       
510                        for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
511                        {
512                                exporter->ExportBox(mKdTree->GetBox((*it)));
513                        }*/
514                }
515        }
516        /*while (1)
517        { debuggerWidget->repaint();
518        };*/
519        delete exporter;
520}
521
522
523float
524VssPreprocessor::GetAvgPvsSize(VssTree *tree,
525                                                           const vector<AxisAlignedBox3> &viewcells
526                                                           )
527{
528  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
529
530  int sum = 0;
531  for (it = viewcells.begin(); it != it_end; ++ it)
532        sum += tree->GetPvsSize(*it);
533
534  return sum/(float)viewcells.size();
535}
536
537bool
538VssPreprocessor::ComputeVisibility()
539{
540        Debug << "type: vss" << endl;
541
542  long startTime = GetTime();
543
544  int totalSamples = 0;
545
546
547  AxisAlignedBox3 box(mKdTree->GetBox());
548 
549  if (!useViewspacePlane) {
550        float size = 0.05f;
551        float s = 0.5f - size;
552        float olds = Magnitude(box.Size());
553        box.Enlarge(box.Size()*Vector3(-s));
554        Vector3 translation = Vector3(-olds*0.1f, 0, 0);
555        box.SetMin(box.Min() + translation);
556        box.SetMax(box.Max() + translation);
557  } else {
558
559        // sample city like heights
560        box.SetMin(1, box.Min(1) + box.Size(1)*0.2f);
561        box.SetMax(1, box.Min(1) + box.Size(1)*0.3f);
562  }
563
564  if (use2dSampling)
565        box.SetMax(1, box.Min(1));
566
567  cout << "use view space box=" << mUseViewSpaceBox<<endl;
568
569 
570  if (mUseViewSpaceBox)
571  {
572          if (!mEnlargeViewSpace)
573          {
574                  mViewSpaceBox = new AxisAlignedBox3(box);
575          }
576          else
577          {
578                  mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
579
580                  if (0)
581                  {
582                          // HACK: enlarge in y directon
583                          Vector3 size = mViewSpaceBox->Size();
584                       
585                          size[1] *= 1.25;
586                          Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
587                          //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
588
589                          mViewSpaceBox->Enlarge(enlarge);
590                          mViewSpaceBox->SetMax(mViewSpaceBox->Max() + enlarge);
591                  }
592                  else
593                  {
594                          AxisAlignedBox3 tbox(*mViewSpaceBox);
595
596                          Vector3 size = mViewSpaceBox->Size();
597                          tbox.SetMax(0, mViewSpaceBox->Max(0) + size[0] * 0.5);
598                          tbox.SetMin(0, mViewSpaceBox->Min(0) + size[0]);
599                          *mViewSpaceBox = tbox;
600
601                          // $$ JB temporary
602                          /*AxisAlignedBox3 tempbox = *mViewSpaceBox;
603                         
604                          float s = tempbox.Size(0);
605                         
606                          tempbox.Scale(0.8f);
607                          tempbox.SetMax(0, box.Max(0) + s*0.8f);
608                          tempbox.SetMin(0, box.Min(0) + s*0.8f);
609                          *mViewSpaceBox = tempbox;*/
610                  }             
611          }
612          //Debug << "view space box: " << *mViewSpaceBox << endl;
613  }
614  else
615  {
616          mViewSpaceBox = NULL;
617  }
618 
619  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
620
621  mSceneGraph->CollectObjects(&mObjects);
622
623  //-- load view cells from file if requested
624  if (!mLoadViewCells)
625  {
626          mViewCellsManager->SetViewSpaceBox(vbox);
627          // construct view cells using it's own set of samples
628          mViewCellsManager->Construct(this);
629
630          //-- several visualizations and statistics
631          Debug << "view cells construction finished: " << endl;
632          mViewCellsManager->PrintStatistics(Debug);
633  }
634  else
635  {     
636          VssRayContainer dummies;
637          mViewCellsManager->Visualize(mObjects, dummies);
638          mViewCellsManager->ExportViewCells("test.xml", mViewCellsManager->GetExportPvs(), mObjects);
639  }
640
641  VssTree *vssTree = NULL;
642 
643
644  long initialTime = GetTime();
645
646  if (mLoadInitialSamples)
647  {
648          cout << "Loading samples from file ... ";
649          LoadSamples(mVssRays, mObjects);
650          cout << "finished\n" << endl;
651          totalSamples = (int)mVssRays.size();
652  }
653  else
654  {
655       
656        while (totalSamples < mInitialSamples) {
657                int passContributingSamples = 0;
658                int passSampleContributions = 0;
659                int passSamples = 0;
660
661                int index = 0;
662
663                int sampleContributions;
664
665                int s = Min(mSamplesPerPass, mInitialSamples);
666                for (int k=0; k < s; k++) {
667                        // changed by matt
668                        Vector3 viewpoint;
669                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
670                        mViewCellsManager->GetViewPoint(viewpoint);
671                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
672
673                        sampleContributions = CastRay(viewpoint, direction, mVssRays);
674
675                        if (sampleContributions) {
676                                passContributingSamples ++;
677                                passSampleContributions += sampleContributions;
678                        }
679                        passSamples++;
680                        totalSamples++;
681                }
682
683                mPass++;
684                int pvsSize = 0;
685                float avgRayContrib = (passContributingSamples > 0) ?
686                        passSampleContributions/(float)passContributingSamples : 0;
687
688                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
689                cout << "#TotalSamples=" << totalSamples/1000
690                        << "#SampleContributions=" << passSampleContributions << " ("
691                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
692                        << pvsSize/(float)mObjects.size() << endl
693                        << "avg ray contrib=" << avgRayContrib << endl;
694
695                mStats <<
696                        "#Pass\n" <<mPass<<endl<<
697                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
698                        "#TotalSamples\n" << totalSamples<< endl<<
699                        "#SampleContributions\n" << passSampleContributions << endl <<
700                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
701                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
702                        "#AvgRayContrib\n" << avgRayContrib << endl;
703          }
704 
705          cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
706
707
708         
709  }
710 
711
712  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
713  Debug << (int)mVssRays.size() << " rays generated in "
714            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
715
716  if (mStoreInitialSamples)
717  {
718          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
719          ExportSamples(mVssRays);
720          cout << "finished\n" << endl;
721
722          /*VssRayContainer dummyRays;
723          LoadSamples(dummyRays, mObjects);
724          Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
725
726          for (int i = 0; i < (int)mVssRays.size(); ++ i)
727          {
728                  Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
729                  Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
730          }*/
731  }
732
733 
734  //int numExportRays = 2000;
735  int numExportRays = 0;
736
737  if (numExportRays) {
738        char filename[64];
739        sprintf(filename, "vss-rays-initial.x3d");
740        ExportRays(filename, mVssRays, numExportRays);
741  }
742
743  vssTree = new VssTree;
744  // viewcells = Construct(mVssRays);
745
746  vssTree->Construct(mVssRays, mViewSpaceBox);
747  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
748
749  ExportRays("kdtree.x3d", mVssRays, 10);
750
751  if (0)
752  {
753          ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
754          ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
755          ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
756          ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
757          ExportVssTreeLeaves(vssTree, 10);
758  }
759
760  // viewcells->UpdatePVS(newVssRays);
761  // get viewcells as kd tree boxes
762  vector<AxisAlignedBox3> kdViewcells;
763  if (0) {
764        vector<KdLeaf *> leaves;
765        mKdTree->CollectLeaves(leaves);
766        vector<KdLeaf *>::const_iterator it;
767        int targetLeaves = 50;
768        float prob = targetLeaves/(float)leaves.size();
769        for (it = leaves.begin(); it != leaves.end(); ++it)
770          if (RandomValue(0.0f,1.0f) < prob)
771                kdViewcells.push_back(mKdTree->GetBox(*it));
772
773        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
774        cout<<"Initial average PVS size = "<<avgPvs<<endl;
775  }
776
777
778  int samples = 0;
779  int pass = 0;
780
781 
782  // cast view cell samples
783  while (samples < mVssSamples)
784  {
785       
786        int num = mVssSamplesPerPass;
787        SimpleRayContainer rays;
788        VssRayContainer vssRays;
789
790        if (!mUseImportanceSampling) {
791          for (int j=0; j < num; j++) {
792            // changed by matt
793                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
794                Vector3 viewpoint;
795                mViewCellsManager->GetViewPoint(viewpoint);
796                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
797                rays.push_back(SimpleRay(viewpoint, direction));
798          }
799        } else {
800          num = GenerateImportanceRays(vssTree, num, rays);
801        }
802
803        for (int i=0; i < rays.size(); i++)
804          CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
805
806        vssTree->AddRays(vssRays);
807
808        if (0) {
809          int subdivided = vssTree->UpdateSubdivision();
810          cout<<"subdivided leafs = "<<subdivided<<endl;
811        }
812
813        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
814        cout<<"Average PVS size = "<<avgPvs<<endl;
815
816        /// compute view cell contribution of rays
817        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
818       
819        if (numExportRays) {
820          char filename[64];
821          if (mUseImportanceSampling)
822                sprintf(filename, "vss-rays-i%04d.x3d", pass);
823          else
824                sprintf(filename, "vss-rays-%04d.x3d", pass);
825
826          ExportRays(filename, vssRays, numExportRays);
827        }
828
829        samples+=num;
830        float pvs = vssTree->GetAvgPvsSize();
831        cout<<"*****************************\n";
832        cout<<samples<<" avgPVS ="<<pvs<<endl;
833        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
834        cout<<"*****************************\n";
835//      if (samples >= mVssSamples) break;
836        pass ++;
837  }
838
839  if (mTestBeamSampling && mUseGlRenderer)
840  {     
841          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
842  }
843
844  if (0)
845          Debug << vssTree->stat << endl;
846
847  if (0)
848  {
849        VssRayContainer viewCellRays;
850 
851        // compute rays used for view cells construction
852        const int numRays = mViewCellsManager->GetVisualizationSamples();
853
854        vssTree->CollectRays(viewCellRays, numRays);
855  }
856
857  //-- render simulation after merge
858  cout << "\nevaluating bsp view cells render time after sampling ... ";
859  Debug << "\nStatistics after sampling: " << endl;
860
861  mRenderSimulator->RenderScene();
862  SimulationStatistics ss;
863  mRenderSimulator->GetStatistics(ss);
864 
865  cout << " finished" << endl;
866  cout << ss << endl;
867  Debug << ss << endl;
868
869  delete vssTree;
870 
871  return true;
872}
873
874}
Note: See TracBrowser for help on using the repository browser.