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

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