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

Revision 1251, 5.9 KB checked in by mattausch, 18 years ago (diff)
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{
[1199]38  static Ray ray;
39  AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox();
[440]40 
[1199]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);
[444]49
[1199]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;
[372]66}
67
[429]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
[372]81
82void
83SamplingPreprocessor::VerifyVisibility(Intersectable *object)
84{
85        // mail all nodes from the pvs
[1199]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);
[372]107        }
[1199]108        // now rank all the neighbors according to probability that a new
109        // sample creates some contribution
110  }
[372]111}
112
113bool
114SamplingPreprocessor::ComputeVisibility()
115{
116 
[1199]117  Debug << "type: sampling" << endl;
[487]118 
[1199]119  cout<<"Sampling Preprocessor started\n"<<flush;
120  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[372]121
[1199]122  Randomize(0);
123 
[372]124  long startTime = GetTime();
125 
126  int totalSamples = 0;
127
[1199]128  // if not already loaded, construct view cells from file
129  if (!mLoadViewCells)
130  {
131        mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
[372]132
[1199]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 
[372]141
[1199]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();
[372]154
[1199]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);
[372]176                        }
[1199]177                  }
178                  direction = -direction;
[372]179                }
[1199]180          }
181         
182          if (samples > mTotalSamples)
183                break;
[372]184        }
185       
[1199]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 
[372]195  //  HoleSamplingPass();
196        if (0) {
[1199]197          Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
198          exporter->SetExportRayDensity(true);
199          exporter->ExportKdTree(*mKdTree);
200          delete exporter;
[422]201        }
[452]202       
[1199]203       
[466]204        // $$JB temporary removed
205        //      mViewCellsManager->PostProcess(objects, mSampleRays);
[452]206       
207        //-- several visualizations and statistics
[475]208        Debug << "view cells after post processing: " << endl;
[452]209        mViewCellsManager->PrintStatistics(Debug);
[1199]210       
[448]211        //-- render simulation after merge
212        cout << "\nevaluating bsp view cells render time after merge ... ";
213       
[1199]214        mRenderSimulator->RenderScene();
215        SimulationStatistics ss;
216        mRenderSimulator->GetStatistics(ss);
[468]217       
[1199]218        cout << " finished" << endl;
219        cout << ss << endl;
220        Debug << ss << endl;
221       
[466]222        // $$JB temporary removed
223        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1199]224       
[441]225        return true;
[362]226}
[349]227
[441]228
[362]229}
Note: See TracBrowser for help on using the repository browser.