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

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}
25
26SamplingPreprocessor::~SamplingPreprocessor()
27{
[1199]28  CLEAR_CONTAINER(mSampleRays);
29  CLEAR_CONTAINER(mVssSampleRays);
[372]30}
31
[444]32
[1520]33
[372]34
[429]35void
[372]36SamplingPreprocessor::VerifyVisibility(Intersectable *object)
37{
[1579]38#if 0 // 6.10. 2006 due to kdPVS removal from intersectable
39  // mail all nodes from the pvs
[1199]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);
[372]61        }
[1199]62        // now rank all the neighbors according to probability that a new
63        // sample creates some contribution
64  }
[1579]65#endif
[372]66}
67
68bool
69SamplingPreprocessor::ComputeVisibility()
70{
71 
[1199]72  Debug << "type: sampling" << endl;
[487]73 
[1199]74  cout<<"Sampling Preprocessor started\n"<<flush;
75  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[372]76
[1199]77  Randomize(0);
[1563]78  const long startTime = GetTime();
[372]79  int totalSamples = 0;
80
[1199]81  // if not already loaded, construct view cells from file
[1715]82  if (!mLoadViewCells) {
83        ConstructViewCells();
[1199]84  }
[1743]85
86  int intersectables, faces;
87  mSceneGraph->GetStatistics(intersectables, faces);
88 
[1199]89  int samples = 0;
90  int i=0;
91  while (samples < mTotalSamples) {
[1743]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<<"+";
[372]99
[1743]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          }
[1199]115         
[1743]116          CastRays(rays,
117                           vssRays,
118                           true,
119                           false);
120
121          if (vssRays.size()!=32) {
[1749]122                cerr<<"wrong number of rays "<<(int)vssRays.size()<<endl;
[1743]123                exit(1);
124          }
125               
[1771]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
[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);
[1771]143                  }
[372]144                }
[1199]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 
[1771]182  EvalViewCellHistogram();
183
[1715]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
[466]196        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1715]197 
198  return true;
[362]199}
[349]200
[441]201
[362]202}
Note: See TracBrowser for help on using the repository browser.