source: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp @ 556

Revision 556, 20.9 KB checked in by bittner, 18 years ago (diff)

debug version looking for glrenderer bug...

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