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

RevLine 
[372]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"
[439]9#include "ViewCellsManager.h"
[406]10#include "RenderSimulator.h"
[1743]11#include "VssRay.h"
[372]12
[860]13
[1221]14
[863]15namespace GtpVisibilityPreprocessor {
[860]16
17
[1292]18SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
[372]19{
20  // this should increase coherence of the samples
[1199]21  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass);
22  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
23 
[372]24  mStats.open("stats.log");
25}
26
27SamplingPreprocessor::~SamplingPreprocessor()
28{
[1199]29  CLEAR_CONTAINER(mSampleRays);
30  CLEAR_CONTAINER(mVssSampleRays);
[372]31}
32
[444]33
[1520]34
[372]35
[429]36void
[372]37SamplingPreprocessor::VerifyVisibility(Intersectable *object)
38{
[1579]39#if 0 // 6.10. 2006 due to kdPVS removal from intersectable
40  // mail all nodes from the pvs
[1199]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);
[372]62        }
[1199]63        // now rank all the neighbors according to probability that a new
64        // sample creates some contribution
65  }
[1579]66#endif
[372]67}
68
69bool
70SamplingPreprocessor::ComputeVisibility()
71{
72 
[1199]73  Debug << "type: sampling" << endl;
[487]74 
[1199]75  cout<<"Sampling Preprocessor started\n"<<flush;
76  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[372]77
[1199]78  Randomize(0);
[1563]79  const long startTime = GetTime();
[372]80  int totalSamples = 0;
81
[1199]82  // if not already loaded, construct view cells from file
[1715]83  if (!mLoadViewCells) {
84        ConstructViewCells();
[1199]85  }
[1743]86
87  int intersectables, faces;
88  mSceneGraph->GetStatistics(intersectables, faces);
89 
[1199]90  int samples = 0;
91  int i=0;
92  while (samples < mTotalSamples) {
[1743]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<<"+";
[372]100
[1743]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          }
[1199]116         
[1743]117          CastRays(rays,
118                           vssRays,
119                           true,
120                           false);
121
122          if (vssRays.size()!=32) {
[1749]123                cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
[1743]124                exit(1);
125          }
126               
127          for (int j=0; j < vssRays.size(); j++) {
[1757]128                Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
129                                                                                                                                 true);
[1743]130                if (obj) {
131                  // if ray not outside of view space
[1199]132                        float contribution;
133                        int pvsContribution = 0;
134                        float relativePvsContribution = 0;
[1743]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);
[372]143                }
[1199]144          }
145         
[1761]146          CLEAR_CONTAINER(vssRays);
147
[1199]148          if (samples > mTotalSamples)
149                break;
[372]150        }
[1743]151
[1757]152#if 0
[1743]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;
[1757]156#endif
157       
[1199]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 
[1715]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
[466]194        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1715]195 
196  return true;
[362]197}
[349]198
[441]199
[362]200}
Note: See TracBrowser for help on using the repository browser.