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

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

merge, preparing sampling strategy support for mixed distributions, filter changes, histogram output for preprocessor

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}
25
26SamplingPreprocessor::~SamplingPreprocessor()
27{
28  CLEAR_CONTAINER(mSampleRays);
29  CLEAR_CONTAINER(mVssSampleRays);
30}
31
32
33
34
35void
36SamplingPreprocessor::VerifyVisibility(Intersectable *object)
37{
38#if 0 // 6.10. 2006 due to kdPVS removal from intersectable
39  // mail all nodes from the pvs
40  Intersectable::NewMail();
41  KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
42  for (; i != object->mKdPvs.mEntries.end(); i++) {
43        KdNode *node = (*i).first;
44        node->Mail();
45  }
46  Debug << "Get all neighbours from PVS" << endl;
47  vector<KdNode *> invisibleNeighbors;
48  // get all neighbors of all PVS nodes
49  i = object->mKdPvs.mEntries.begin();
50  for (; i != object->mKdPvs.mEntries.end(); i++) {
51        KdNode *node = (*i).first;
52        mKdTree->FindNeighbors(node, invisibleNeighbors, true);
53        AxisAlignedBox3 box = object->GetBox();
54        for (int j=0; j < invisibleNeighbors.size(); j++) {
55          int visibility = ComputeBoxVisibility(mSceneGraph,
56                                                                                        mKdTree,
57                                                                                        box,
58                                                                                        mKdTree->GetBox(invisibleNeighbors[j]),
59                                                                                        1e-6f);
60          //          exit(0);
61        }
62        // now rank all the neighbors according to probability that a new
63        // sample creates some contribution
64  }
65#endif
66}
67
68bool
69SamplingPreprocessor::ComputeVisibility()
70{
71 
72  Debug << "type: sampling" << endl;
73 
74  cout<<"Sampling Preprocessor started\n"<<flush;
75  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
76
77  Randomize(0);
78  const long startTime = GetTime();
79  int totalSamples = 0;
80
81  // if not already loaded, construct view cells from file
82  if (!mLoadViewCells) {
83        ConstructViewCells();
84  }
85
86  int intersectables, faces;
87  mSceneGraph->GetStatistics(intersectables, faces);
88 
89  int samples = 0;
90  int i=0;
91  while (samples < mTotalSamples) {
92        for (i=0; i < mSamplesPerPass;) {
93          SimpleRayContainer rays;
94          VssRayContainer vssRays;
95          vector<ViewCell *> viewcells;
96          for (; rays.size() < 16; ) {
97                if (i%10000 == 0)
98                  cout<<"+";
99
100                Vector3 origin, direction;
101                mViewCellsManager->GetViewPoint( origin );
102               
103                direction = UniformRandomVector();
104                ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
105               
106                if (viewcell && viewcell->GetValid()) {
107                  viewcells.push_back(viewcell);
108                  // cast rays in both directions to make the number of samples comparable
109                  // with the global sampling method which also casts a "double" ray per sample
110                  rays.push_back(SimpleRay(origin, direction));
111                  i++;
112                  samples++;
113                }
114          }
115         
116          CastRays(rays,
117                           vssRays,
118                           true,
119                           false);
120
121          if (vssRays.size()!=32) {
122                cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
123                exit(1);
124          }
125               
126          for (int j=0; j < vssRays.size(); j++)
127                if (vssRays[j]->mFlags & VssRay::Valid) {
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  EvalViewCellHistogram();
183
184  //-- render simulation after merge
185  cout << "\nevaluating bsp view cells render time after merge ... ";
186 
187  mRenderSimulator->RenderScene();
188  SimulationStatistics ss;
189  mRenderSimulator->GetStatistics(ss);
190 
191  cout << " finished" << endl;
192  cout << ss << endl;
193  Debug << ss << endl;
194 
195  // $$JB temporary removed
196        //mViewCellsManager->Visualize(objects, mSampleRays);   
197 
198  return true;
199}
200
201
202}
Note: See TracBrowser for help on using the repository browser.