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

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
[411]17SamplingPreprocessor::SamplingPreprocessor(): 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{
[1221]38        //float *pforg;
39        //float *pfdir;
40        //mlrtaStoreRayAS4(pforg, pfdir, 4);
41        //////////////////////////////////
[1199]42  static Ray ray;
43  AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
[440]44 
[1199]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);
[444]53
[1199]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;
[372]70}
71
[429]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
[372]85
86void
87SamplingPreprocessor::VerifyVisibility(Intersectable *object)
88{
89        // mail all nodes from the pvs
[1199]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);
[372]111        }
[1199]112        // now rank all the neighbors according to probability that a new
113        // sample creates some contribution
114  }
[372]115}
116
117bool
118SamplingPreprocessor::ComputeVisibility()
119{
120 
[1199]121  Debug << "type: sampling" << endl;
[487]122 
[1199]123  cout<<"Sampling Preprocessor started\n"<<flush;
124  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[372]125
[1199]126  Randomize(0);
127 
[372]128  long startTime = GetTime();
129 
130  int totalSamples = 0;
131
[1199]132  // if not already loaded, construct view cells from file
133  if (!mLoadViewCells)
134  {
135        mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
[372]136
[1199]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 
[372]145
[1199]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();
[372]158
[1199]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);
[372]180                        }
[1199]181                  }
182                  direction = -direction;
[372]183                }
[1199]184          }
185         
186          if (samples > mTotalSamples)
187                break;
[372]188        }
189       
[1199]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 
[372]199  //  HoleSamplingPass();
200        if (0) {
[1199]201          Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
202          exporter->SetExportRayDensity(true);
203          exporter->ExportKdTree(*mKdTree);
204          delete exporter;
[422]205        }
[452]206       
[1199]207       
[466]208        // $$JB temporary removed
209        //      mViewCellsManager->PostProcess(objects, mSampleRays);
[452]210       
211        //-- several visualizations and statistics
[475]212        Debug << "view cells after post processing: " << endl;
[452]213        mViewCellsManager->PrintStatistics(Debug);
[1199]214       
[448]215        //-- render simulation after merge
216        cout << "\nevaluating bsp view cells render time after merge ... ";
217       
[1199]218        mRenderSimulator->RenderScene();
219        SimulationStatistics ss;
220        mRenderSimulator->GetStatistics(ss);
[468]221       
[1199]222        cout << " finished" << endl;
223        cout << ss << endl;
224        Debug << ss << endl;
225       
[466]226        // $$JB temporary removed
227        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1199]228       
[441]229        return true;
[362]230}
[349]231
[441]232
[362]233}
Note: See TracBrowser for help on using the repository browser.