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

Revision 1520, 5.4 KB checked in by mattausch, 18 years ago (diff)

moved raycasting out of preprocessor into specific ray casting interface

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