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

Revision 1966, 5.5 KB checked in by bittner, 17 years ago (diff)

samplign preprocessor updates, merge

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 
23}
24
25SamplingPreprocessor::~SamplingPreprocessor()
26{
27  CLEAR_CONTAINER(mSampleRays);
28  CLEAR_CONTAINER(mVssSampleRays);
29}
30
31
32
33
34void
35SamplingPreprocessor::VerifyVisibility(Intersectable *object)
36{
37#if 0 // 6.10. 2006 due to kdPVS removal from intersectable
38  // mail all nodes from the pvs
39  Intersectable::NewMail();
40  KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
41  for (; i != object->mKdPvs.mEntries.end(); i++) {
42        KdNode *node = (*i).first;
43        node->Mail();
44  }
45  Debug << "Get all neighbours from PVS" << endl;
46  vector<KdNode *> invisibleNeighbors;
47  // get all neighbors of all PVS nodes
48  i = object->mKdPvs.mEntries.begin();
49  for (; i != object->mKdPvs.mEntries.end(); i++) {
50        KdNode *node = (*i).first;
51        mKdTree->FindNeighbors(node, invisibleNeighbors, true);
52        AxisAlignedBox3 box = object->GetBox();
53        for (int j=0; j < invisibleNeighbors.size(); j++) {
54          int visibility = ComputeBoxVisibility(mSceneGraph,
55                                                                                        mKdTree,
56                                                                                        box,
57                                                                                        mKdTree->GetBox(invisibleNeighbors[j]),
58                                                                                        1e-6f);
59          //          exit(0);
60        }
61        // now rank all the neighbors according to probability that a new
62        // sample creates some contribution
63  }
64#endif
65}
66
67bool
68SamplingPreprocessor::ComputeVisibility()
69{
70 
71  Debug << "type: sampling" << endl;
72 
73  cout<<"Sampling Preprocessor started\n"<<flush;
74  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
75
76  Randomize(0);
77  const long startTime = GetTime();
78  int totalSamples = 0;
79
80  // if not already loaded, construct view cells from file
81  if (!mLoadViewCells) {
82        ConstructViewCells();
83  }
84
85  int intersectables, faces;
86  mSceneGraph->GetStatistics(intersectables, faces);
87  HaltonSequence posHalton;
88 
89  int samples = 0;
90  int rssSamples = 0;
91
92  map<ViewCell *, HaltonSequence> dirHalton;
93 
94  while (samples < mTotalSamples) {
95        for (int i=0; i < mSamplesPerEvaluation;) {
96          SimpleRayContainer rays;
97          VssRayContainer vssRays;
98          //      vector<ViewCell *> viewcells;
99         
100          float r[5];
101          Vector3 origin, direction;
102          posHalton.GetNext(3, r);
103
104          mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
105          ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
106         
107          if (!viewcell || !viewcell->GetValid())
108                continue;
109         
110          for (; rays.size() < 16; ) {
111                if (i%100000 == 0)
112                  cout<<"+";
113               
114                //              dirHalton.GetNext(2, r);
115                dirHalton[viewcell].GetNext(2, r);
116                direction = UniformRandomVector(r[0],r[1]);
117                //direction = UniformRandomVector();
118               
119                //              viewcells.push_back(viewcell);
120                // cast rays in both directions to make the number of samples comparable
121                // with the global sampling method which also casts a "double" ray per sample
122                rays.push_back(SimpleRay(origin,
123                                                                 direction,
124                                                                 SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
125                                                                 1.0f));
126                i++;
127                samples++;
128          }
129         
130          CastRays(rays,
131                           vssRays,
132                           true,
133                           true);
134         
135          //      if (vssRays.size()!=32) {
136          //            cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
137          //            exit(1);
138          //      }
139
140          rssSamples+=vssRays.size();
141          for (int j=0; j < vssRays.size(); j++)
142                if (vssRays[j]->mFlags & VssRay::Valid) {
143                  Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
144                                                                                                                                   true);
145                  if (obj) {
146                        // if ray not outside of view space
147                        float pdf = 1.0f;
148                        //                      ViewCell *viewcell = viewcells[j/2];
149
150                        ObjectPvs &pvs = viewcell->GetPvs();
151                        pvs.AddSampleDirtyCheck(obj, pdf);
152                       
153                        if (pvs.RequiresResort())  {
154                          pvs.SimpleSort();
155                        }
156                  }
157                }
158         
159          CLEAR_CONTAINER(vssRays);
160         
161          if (samples > mTotalSamples)
162                break;
163        }
164
165#if 0
166        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
167        mViewCellsManager->SetValidity(0, intersectables/2);
168        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
169#endif
170       
171        //      mVssRays.PrintStatistics(mStats);
172        mStats <<
173          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
174          "#TotalSamples\n" <<samples<<endl<<
175          "#RssSamples\n" <<rssSamples<<endl;
176                 
177        mViewCellsManager->PrintPvsStatistics(mStats);
178        // ComputeRenderError();
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.