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

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