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

Revision 1761, 5.3 KB checked in by bittner, 18 years ago (diff)

filter updates, kd + bvh PVS coexistence

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
13
14
15namespace GtpVisibilityPreprocessor {
16
17
18SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
19{
20  // this should increase coherence of the samples
21  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass);
22  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
23 
24  mStats.open("stats.log");
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, direction));
112                  i++;
113                  samples++;
114                }
115          }
116         
117          CastRays(rays,
118                           vssRays,
119                           true,
120                           false);
121
122          if (vssRays.size()!=32) {
123                cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
124                exit(1);
125          }
126               
127          for (int j=0; j < vssRays.size(); j++) {
128                Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
129                                                                                                                                 true);
130                if (obj) {
131                  // if ray not outside of view space
132                        float contribution;
133                        int pvsContribution = 0;
134                        float relativePvsContribution = 0;
135                        float pdf = 1.0f;
136                        ViewCell *viewcell = viewcells[j/2];
137                        if (viewcell->GetPvs().GetSampleContribution(obj,
138                                                                                                                 pdf,
139                                                                                                                 contribution))
140                          ++pvsContribution;
141                        relativePvsContribution += contribution;
142                        viewcell->GetPvs().AddSample(obj, pdf);
143                }
144          }
145         
146          CLEAR_CONTAINER(vssRays);
147
148          if (samples > mTotalSamples)
149                break;
150        }
151
152#if 0
153        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
154        mViewCellsManager->SetValidity(0, intersectables/2);
155        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
156#endif
157       
158        //      mVssRays.PrintStatistics(mStats);
159        mStats <<
160          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
161          "#TotalSamples\n" <<samples<<endl;
162
163        mViewCellsManager->PrintPvsStatistics(mStats);
164        // ComputeRenderError();
165  }
166 
167  if (0) {
168        Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
169        exporter->SetExportRayDensity(true);
170        exporter->ExportKdTree(*mKdTree);
171        delete exporter;
172  }
173 
174 
175  // $$JB temporary removed
176  //    mViewCellsManager->PostProcess(objects, mSampleRays);
177 
178  //-- several visualizations and statistics
179  Debug << "view cells after post processing: " << endl;
180  mViewCellsManager->PrintStatistics(Debug);
181 
182  //-- render simulation after merge
183  cout << "\nevaluating bsp view cells render time after merge ... ";
184 
185  mRenderSimulator->RenderScene();
186  SimulationStatistics ss;
187  mRenderSimulator->GetStatistics(ss);
188 
189  cout << " finished" << endl;
190  cout << ss << endl;
191  Debug << ss << endl;
192 
193  // $$JB temporary removed
194        //mViewCellsManager->Visualize(objects, mSampleRays);   
195 
196  return true;
197}
198
199
200}
Note: See TracBrowser for help on using the repository browser.