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

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

merge on notes

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