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

Revision 1883, 5.5 KB checked in by bittner, 18 years ago (diff)

mixture distribution initial coding

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