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

Revision 1900, 5.6 KB checked in by bittner, 18 years ago (diff)

experiments with different contribution computations

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"
[1883]12#include "SamplingStrategy.h"
[372]13
[860]14
[1221]15
[863]16namespace GtpVisibilityPreprocessor {
[860]17
18
[1292]19SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
[372]20{
21  // this should increase coherence of the samples
[1199]22  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass);
23  Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples);
24 
[372]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);
[1900]89  HaltonSequence halton;
[1743]90 
[1199]91  int samples = 0;
92  int i=0;
93  while (samples < mTotalSamples) {
[1743]94        for (i=0; i < mSamplesPerPass;) {
95          SimpleRayContainer rays;
96          VssRayContainer vssRays;
97          vector<ViewCell *> viewcells;
98          for (; rays.size() < 16; ) {
99                if (i%10000 == 0)
100                  cout<<"+";
[372]101
[1900]102                float r[5];
103                halton.GetNext(5, r);
[1743]104                Vector3 origin, direction;
[1900]105                mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
[1743]106               
[1900]107                direction = UniformRandomVector(r[3],r[4]);
[1743]108                ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
109               
110                if (viewcell && viewcell->GetValid()) {
111                  viewcells.push_back(viewcell);
112                  // cast rays in both directions to make the number of samples comparable
113                  // with the global sampling method which also casts a "double" ray per sample
[1883]114                  rays.push_back(SimpleRay(origin,
115                                                                   direction,
116                                                                   SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
117                                                                   1.0f));
[1743]118                  i++;
119                  samples++;
120                }
121          }
[1199]122         
[1743]123          CastRays(rays,
124                           vssRays,
125                           true,
126                           false);
127
128          if (vssRays.size()!=32) {
[1749]129                cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
[1743]130                exit(1);
131          }
132               
[1771]133          for (int j=0; j < vssRays.size(); j++)
134                if (vssRays[j]->mFlags & VssRay::Valid) {
135                  Intersectable *obj = mViewCellsManager->GetIntersectable(*(vssRays[j]),
136                                                                                                                                   true);
137                  if (obj) {
138                        // if ray not outside of view space
[1199]139                        float contribution;
140                        int pvsContribution = 0;
141                        float relativePvsContribution = 0;
[1743]142                        float pdf = 1.0f;
143                        ViewCell *viewcell = viewcells[j/2];
144                        if (viewcell->GetPvs().GetSampleContribution(obj,
145                                                                                                                 pdf,
146                                                                                                                 contribution))
147                          ++pvsContribution;
148                        relativePvsContribution += contribution;
149                        viewcell->GetPvs().AddSample(obj, pdf);
[1771]150                  }
[372]151                }
[1199]152         
[1761]153          CLEAR_CONTAINER(vssRays);
154
[1199]155          if (samples > mTotalSamples)
156                break;
[372]157        }
[1743]158
[1757]159#if 0
[1743]160        Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
161        mViewCellsManager->SetValidity(0, intersectables/2);
162        Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
[1757]163#endif
164       
[1199]165        //      mVssRays.PrintStatistics(mStats);
166        mStats <<
167          "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
168          "#TotalSamples\n" <<samples<<endl;
169
170        mViewCellsManager->PrintPvsStatistics(mStats);
171        // ComputeRenderError();
172  }
173 
[1715]174  if (0) {
175        Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
176        exporter->SetExportRayDensity(true);
177        exporter->ExportKdTree(*mKdTree);
178        delete exporter;
179  }
180 
181 
182  // $$JB temporary removed
183  //    mViewCellsManager->PostProcess(objects, mSampleRays);
184 
185  //-- several visualizations and statistics
186  Debug << "view cells after post processing: " << endl;
187  mViewCellsManager->PrintStatistics(Debug);
188 
[1771]189  EvalViewCellHistogram();
190
[1715]191  //-- render simulation after merge
192  cout << "\nevaluating bsp view cells render time after merge ... ";
193 
194  mRenderSimulator->RenderScene();
195  SimulationStatistics ss;
196  mRenderSimulator->GetStatistics(ss);
197 
198  cout << " finished" << endl;
199  cout << ss << endl;
200  Debug << ss << endl;
201 
202  // $$JB temporary removed
[466]203        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1715]204 
205  return true;
[362]206}
[349]207
[441]208
[362]209}
Note: See TracBrowser for help on using the repository browser.