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

Revision 2653, 5.3 KB checked in by bittner, 16 years ago (diff)

merge - pvs error code
glrenderer.cpp

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
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 
70  Debug << "type: sampling" << endl;
71 
72  cout<<"Sampling Preprocessor started\n"<<flush;
73  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
74 
75  Randomize(0);
76  const long startTime = GetTime();
77  int totalSamples = 0;
78
79  // if not already loaded, construct view cells from file
80  if (!mLoadViewCells) {
81        ConstructViewCells();
82  }
83 
84  int intersectables, faces;
85  mSceneGraph->GetStatistics(intersectables, faces);
86  HaltonSequence posHalton;
87 
88  int samples = 0;
89  int rssSamples = 0;
90
91  map<ViewCell *, HaltonSequence> dirHalton;
92  int lastEvaluation = 0;
93  SimpleRayContainer rays;
94  VssRayContainer vssRays;
95
96  while (samples < mTotalSamples) {
97        for (int i=0; i < mSamplesPerPass;) {
98         
99          mRayCaster->InitPass();
100         
101          float r[5];
102          Vector3 origin, direction;
103          posHalton.GetNext(3, r);
104
105          mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
106          ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
107         
108          if (!viewcell || !viewcell->GetValid())
109                continue;
110
111          rays.clear();
112          vssRays.clear();
113
114          for (; rays.size() < 16; ) {
115                if (i%100000 == 0) {
116                  cout<<samples/1e6f<<"M rays, progress: "<<(samples*100.0f)/mTotalSamples<<" %   \r";
117                }
118               
119                dirHalton[viewcell].GetNext(2, r);
120                direction = UniformRandomVector(r[0],r[1]);
121                //direction = UniformRandomVector();
122               
123                // cast rays in both directions to make the number of samples comparable
124                // with the global sampling method which also casts a "double" ray per sample
125                rays.push_back(SimpleRay(origin,
126                                                                 direction,
127                                                                 SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
128                                                                 1.0f));
129                i++;
130                samples++;
131          }
132         
133          CastRays(rays,
134                           vssRays,
135                           true,
136                           true);
137         
138
139          rssSamples += (int)vssRays.size();
140          for (int j=0; j < vssRays.size(); j++)
141                if (vssRays[j]->mFlags & VssRay::Valid) {
142                  Intersectable *obj = vssRays[j]->mTerminationObject;
143                  if (obj) {
144                        // if ray not outside of view space
145                        float pdf = 1.0f;
146                        //                      ViewCell *viewcell = viewcells[j/2];
147                       
148                        ObjectPvs &pvs = viewcell->GetPvs();
149                        pvs.AddSampleDirtyCheck(obj, pdf);
150                       
151                        if (pvs.RequiresResort())  {
152                          pvs.SimpleSort();
153                        }
154                  }
155                }
156         
157          if (samples > mTotalSamples)
158                break;
159        }
160
161
162        if (samples - lastEvaluation >= mSamplesPerEvaluation) {
163          Real time = TimeDiff(startTime, GetTime());
164          mViewCellsManager->PrintPvsStatistics(mStats);
165          mStats <<
166                "#Pass\n" <<mPass<<endl<<
167                "#Time\n" << time <<endl<<
168                "#TotalSamples\n" <<samples<<endl<<
169                "#RssSamples\n" <<rssSamples<<endl;
170          lastEvaluation = samples;
171         
172          if (renderer) {
173                ComputeRenderError();
174          }
175         
176        }
177
178        Real time = TimeDiff(startTime, GetTime());
179        if (mTotalTime != -1 && time/1000 > mTotalTime)
180          break;
181       
182  }
183  if (0) {
184        Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
185        exporter->SetExportRayDensity(true);
186        exporter->ExportKdTree(*mKdTree);
187        delete exporter;
188  }
189       
190       
191        // $$JB temporary removed
192  //    mViewCellsManager->PostProcess(objects, mSampleRays);
193 
194  //-- several visualizations and statistics
195  Debug << "view cells after post processing: " << endl;
196  mViewCellsManager->PrintStatistics(Debug);
197 
198  EvalViewCellHistogram();
199
200  //-- render simulation after merge
201  cout << "\nevaluating bsp view cells render time after merge ... ";
202 
203  mRenderSimulator->RenderScene();
204  SimulationStatistics ss;
205  mRenderSimulator->GetStatistics(ss);
206 
207  cout << " finished" << endl;
208  cout << ss << endl;
209  Debug << ss << endl;
210 
211  // $$JB temporary removed
212        //mViewCellsManager->Visualize(objects, mSampleRays);   
213 
214  return true;
215}
216
217
218}
Note: See TracBrowser for help on using the repository browser.