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

Revision 1221, 6.0 KB checked in by mattausch, 18 years ago (diff)

added intel ray tracing

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