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

Revision 1486, 18.1 KB checked in by mattausch, 18 years ago (diff)

worked on guided visibility sampling

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