source: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp @ 1292

Revision 1292, 5.7 KB checked in by bittner, 18 years ago (diff)

intel ray caster updates

RevLine 
[372]1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "SamplingPreprocessor.h"
4#include "X3dExporter.h"
5#include "Environment.h"
6#include "MutualVisibility.h"
7#include "Polygon3.h"
8#include "ViewCell.h"
[439]9#include "ViewCellsManager.h"
[406]10#include "RenderSimulator.h"
[372]11
[860]12
[1221]13
[863]14namespace GtpVisibilityPreprocessor {
[860]15
16
[1292]17SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
[372]18{
19  // this should increase coherence of the samples
[1199]20  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass);
21  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
22 
[372]23  mStats.open("stats.log");
24}
25
26SamplingPreprocessor::~SamplingPreprocessor()
27{
[1199]28  CLEAR_CONTAINER(mSampleRays);
29  CLEAR_CONTAINER(mVssSampleRays);
[372]30}
31
[1199]32Intersectable *
33SamplingPreprocessor::CastRay(
34                                                          const Vector3 &origin,
35                                                          const Vector3 &direction
36                                                          )
[372]37{
[1199]38  AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
[440]39 
[1199]40  AxisAlignedBox3 sbox = box;
41  sbox.Enlarge(Vector3(-Limits::Small));
42  if (!sbox.IsInside(origin))
43        return 0;
[1292]44 
45  Vector3 point, normal;
46  Intersectable *object;
[444]47
[1199]48  // cast ray to KD tree to find intersection with other objects
[1292]49  object = CastSimpleRay(origin,
50                                                 direction,
51                                                 mKdTree->GetBox(),
52                                                 point,
53                                                 normal
54                                                 );
[1199]55 
[1292]56  if (mDetectEmptyViewSpace && DotProd(normal, direction) >= 0) {
57        object = NULL;
[1199]58  }
[1292]59 
60  return object;
[372]61}
62
[429]63void
64SamplingPreprocessor::HoleSamplingPass()
65{
66  vector<KdLeaf *> leaves;
67  mKdTree->CollectLeaves(leaves);
68 
69  // go through all the leaves and evaluate their passing contribution
70  for (int i=0 ; i < leaves.size(); i++) {
71    KdLeaf *leaf = leaves[i];
72    cout<<leaf->mPassingRays<<endl;
73  }
74}
75
[372]76
77void
78SamplingPreprocessor::VerifyVisibility(Intersectable *object)
79{
80        // mail all nodes from the pvs
[1199]81  Intersectable::NewMail();
82  KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
83  for (; i != object->mKdPvs.mEntries.end(); i++) {
84        KdNode *node = (*i).first;
85        node->Mail();
86  }
87  Debug << "Get all neighbours from PVS" << endl;
88  vector<KdNode *> invisibleNeighbors;
89  // get all neighbors of all PVS nodes
90  i = object->mKdPvs.mEntries.begin();
91  for (; i != object->mKdPvs.mEntries.end(); i++) {
92        KdNode *node = (*i).first;
93        mKdTree->FindNeighbors(node, invisibleNeighbors, true);
94        AxisAlignedBox3 box = object->GetBox();
95        for (int j=0; j < invisibleNeighbors.size(); j++) {
96          int visibility = ComputeBoxVisibility(mSceneGraph,
97                                                                                        mKdTree,
98                                                                                        box,
99                                                                                        mKdTree->GetBox(invisibleNeighbors[j]),
100                                                                                        1e-6f);
101          //          exit(0);
[372]102        }
[1199]103        // now rank all the neighbors according to probability that a new
104        // sample creates some contribution
105  }
[372]106}
107
108bool
109SamplingPreprocessor::ComputeVisibility()
110{
111 
[1199]112  Debug << "type: sampling" << endl;
[487]113 
[1199]114  cout<<"Sampling Preprocessor started\n"<<flush;
115  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[372]116
[1199]117  Randomize(0);
118 
[372]119  long startTime = GetTime();
120 
121  int totalSamples = 0;
122
[1199]123  // if not already loaded, construct view cells from file
124  if (!mLoadViewCells)
125  {
126        mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
[372]127
[1199]128        // construct view cells using it's own set of samples
129        mViewCellsManager->Construct(this);
130       
131        //-- several visualizations and statistics
132        Debug << "view cells construction finished: " << endl;
133        mViewCellsManager->PrintStatistics(Debug);
134  }
135 
[372]136
[1199]137 
138  int samples = 0;
139  int i=0;
140  while (samples < mTotalSamples) {
141        for (i=0; i < mSamplesPerPass; i++, samples++) {
142         
143          if (i%10000 == 0)
144                cout<<"+";
145         
146          Vector3 origin, direction;
147          mViewCellsManager->GetViewPoint(origin);
148          direction = UniformRandomVector();
[372]149
[1199]150         
151          ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
152         
153          if (viewcell && viewcell->GetValid()) {
154                // cast rays in both directions to make the number of samples comparable
155                // with the global sampling method which also casts a "double" ray per sample
156                for (int j=0; j < 2; j++) {
157                  Intersectable *object = CastRay(origin, direction);
158                  if (object) {
159                        // if ray not outside of view space
160                        float contribution;
161                        int pvsContribution = 0;
162                        float relativePvsContribution = 0;
163                        if (object) {
164                          float pdf = 1.0f;
165                          if (viewcell->GetPvs().GetSampleContribution(object,
166                                                                                                                   pdf,
167                                                                                                                   contribution))
168                                ++pvsContribution;
169                          relativePvsContribution += contribution;
170                          viewcell->GetPvs().AddSample(object, pdf);
[372]171                        }
[1199]172                  }
173                  direction = -direction;
[372]174                }
[1199]175          }
176         
177          if (samples > mTotalSamples)
178                break;
[372]179        }
180       
[1199]181        //      mVssRays.PrintStatistics(mStats);
182        mStats <<
183          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
184          "#TotalSamples\n" <<samples<<endl;
185
186        mViewCellsManager->PrintPvsStatistics(mStats);
187        // ComputeRenderError();
188  }
189 
[372]190  //  HoleSamplingPass();
191        if (0) {
[1199]192          Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
193          exporter->SetExportRayDensity(true);
194          exporter->ExportKdTree(*mKdTree);
195          delete exporter;
[422]196        }
[452]197       
[1199]198       
[466]199        // $$JB temporary removed
200        //      mViewCellsManager->PostProcess(objects, mSampleRays);
[452]201       
202        //-- several visualizations and statistics
[475]203        Debug << "view cells after post processing: " << endl;
[452]204        mViewCellsManager->PrintStatistics(Debug);
[1199]205       
[448]206        //-- render simulation after merge
207        cout << "\nevaluating bsp view cells render time after merge ... ";
208       
[1199]209        mRenderSimulator->RenderScene();
210        SimulationStatistics ss;
211        mRenderSimulator->GetStatistics(ss);
[468]212       
[1199]213        cout << " finished" << endl;
214        cout << ss << endl;
215        Debug << ss << endl;
216       
[466]217        // $$JB temporary removed
218        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1199]219       
[441]220        return true;
[362]221}
[349]222
[441]223
[362]224}
Note: See TracBrowser for help on using the repository browser.