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

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