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

Revision 2712, 5.5 KB checked in by mattausch, 16 years ago (diff)
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"
[2035]13#include "RayCaster.h"
[2709]14#include <windows.h>
[372]15
[860]16
[863]17namespace GtpVisibilityPreprocessor {
[860]18
19
[1292]20SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0)
[372]21{
22  // this should increase coherence of the samples
[1199]23 
[372]24}
25
26SamplingPreprocessor::~SamplingPreprocessor()
27{
28}
29
[444]30
[1520]31
[372]32
[429]33void
[372]34SamplingPreprocessor::VerifyVisibility(Intersectable *object)
35{
[1579]36#if 0 // 6.10. 2006 due to kdPVS removal from intersectable
37  // mail all nodes from the pvs
[1199]38  Intersectable::NewMail();
39  KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
40  for (; i != object->mKdPvs.mEntries.end(); i++) {
41        KdNode *node = (*i).first;
42        node->Mail();
43  }
44  Debug << "Get all neighbours from PVS" << endl;
45  vector<KdNode *> invisibleNeighbors;
46  // get all neighbors of all PVS nodes
47  i = object->mKdPvs.mEntries.begin();
48  for (; i != object->mKdPvs.mEntries.end(); i++) {
49        KdNode *node = (*i).first;
50        mKdTree->FindNeighbors(node, invisibleNeighbors, true);
51        AxisAlignedBox3 box = object->GetBox();
52        for (int j=0; j < invisibleNeighbors.size(); j++) {
53          int visibility = ComputeBoxVisibility(mSceneGraph,
54                                                                                        mKdTree,
55                                                                                        box,
56                                                                                        mKdTree->GetBox(invisibleNeighbors[j]),
57                                                                                        1e-6f);
58          //          exit(0);
[372]59        }
[1199]60        // now rank all the neighbors according to probability that a new
61        // sample creates some contribution
62  }
[1579]63#endif
[372]64}
65
66bool
67SamplingPreprocessor::ComputeVisibility()
68{
[1199]69  Debug << "type: sampling" << endl;
70  cout<<"Sampling Preprocessor started\n"<<flush;
71  //  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
[2653]72 
[1199]73  Randomize(0);
[1563]74  const long startTime = GetTime();
[372]75  int totalSamples = 0;
76
[1199]77  // if not already loaded, construct view cells from file
[1715]78  if (!mLoadViewCells) {
79        ConstructViewCells();
[1199]80  }
[2653]81 
[1743]82  int intersectables, faces;
83  mSceneGraph->GetStatistics(intersectables, faces);
[1966]84  HaltonSequence posHalton;
[1743]85 
[2712]86  mCurrentSamples = 0;
[1966]87  int rssSamples = 0;
88
89  map<ViewCell *, HaltonSequence> dirHalton;
[2035]90  int lastEvaluation = 0;
91  SimpleRayContainer rays;
92  VssRayContainer vssRays;
93
[2712]94  while (mCurrentSamples < mTotalSamples) {
[2035]95        for (int i=0; i < mSamplesPerPass;) {
[2709]96           while (mSynchronize)
97           {
98                   // hack: sleep during walthrough computation
99                   Sleep(1000);
100           }
101
[2035]102          mRayCaster->InitPass();
103         
[1966]104          float r[5];
105          Vector3 origin, direction;
106          posHalton.GetNext(3, r);
107
108          mViewCellsManager->GetViewPoint(origin, Vector3(r[0], r[1], r[2]));
109          ViewCell *viewcell = mViewCellsManager->GetViewCell(origin);
110         
111          if (!viewcell || !viewcell->GetValid())
112                continue;
[2035]113
114          rays.clear();
115          vssRays.clear();
116
[1743]117          for (; rays.size() < 16; ) {
[2035]118                if (i%100000 == 0) {
[2712]119                  cout<<mCurrentSamples/1e6f<<"M rays, progress: "<<(mCurrentSamples * 100.0f)/mTotalSamples<<" %   \r";
[2035]120                }
[1743]121               
[1966]122                dirHalton[viewcell].GetNext(2, r);
123                direction = UniformRandomVector(r[0],r[1]);
124                //direction = UniformRandomVector();
[1743]125               
[1966]126                // cast rays in both directions to make the number of samples comparable
127                // with the global sampling method which also casts a "double" ray per sample
128                rays.push_back(SimpleRay(origin,
129                                                                 direction,
130                                                                 SamplingStrategy::DIRECTION_BASED_DISTRIBUTION,
131                                                                 1.0f));
132                i++;
[2712]133                mCurrentSamples ++;
[1743]134          }
[1199]135         
[1743]136          CastRays(rays,
137                           vssRays,
138                           true,
[1966]139                           true);
140         
[1743]141
[2130]142          rssSamples += (int)vssRays.size();
[1771]143          for (int j=0; j < vssRays.size(); j++)
144                if (vssRays[j]->mFlags & VssRay::Valid) {
[2022]145                  Intersectable *obj = vssRays[j]->mTerminationObject;
[1771]146                  if (obj) {
147                        // if ray not outside of view space
[1743]148                        float pdf = 1.0f;
[1966]149                        //                      ViewCell *viewcell = viewcells[j/2];
[2035]150                       
[1966]151                        ObjectPvs &pvs = viewcell->GetPvs();
152                        pvs.AddSampleDirtyCheck(obj, pdf);
153                       
154                        if (pvs.RequiresResort())  {
155                          pvs.SimpleSort();
156                        }
[1771]157                  }
[372]158                }
[1199]159         
[2712]160          if (mCurrentSamples > mTotalSamples)
[1199]161                break;
[372]162        }
[1743]163
[2035]164
[2712]165        if (mCurrentSamples - lastEvaluation >= mSamplesPerEvaluation) {
[2066]166          Real time = TimeDiff(startTime, GetTime());
[2035]167          mViewCellsManager->PrintPvsStatistics(mStats);
168          mStats <<
169                "#Pass\n" <<mPass<<endl<<
[2046]170                "#Time\n" << time <<endl<<
[2712]171                "#TotalSamples\n" <<mCurrentSamples<<endl<<
[2035]172                "#RssSamples\n" <<rssSamples<<endl;
[2712]173          lastEvaluation = mCurrentSamples;
[2046]174         
[2035]175          if (renderer) {
176                ComputeRenderError();
177          }
178         
179        }
[2049]180
[2066]181        Real time = TimeDiff(startTime, GetTime());
[2049]182        if (mTotalTime != -1 && time/1000 > mTotalTime)
183          break;
184       
[1199]185  }
[1715]186  if (0) {
187        Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
188        exporter->SetExportRayDensity(true);
189        exporter->ExportKdTree(*mKdTree);
190        delete exporter;
191  }
[2035]192       
193       
194        // $$JB temporary removed
[1715]195  //    mViewCellsManager->PostProcess(objects, mSampleRays);
196 
197  //-- several visualizations and statistics
198  Debug << "view cells after post processing: " << endl;
199  mViewCellsManager->PrintStatistics(Debug);
200 
[1771]201  EvalViewCellHistogram();
202
[1715]203  //-- render simulation after merge
204  cout << "\nevaluating bsp view cells render time after merge ... ";
205 
206  mRenderSimulator->RenderScene();
207  SimulationStatistics ss;
208  mRenderSimulator->GetStatistics(ss);
209 
210  cout << " finished" << endl;
211  cout << ss << endl;
212  Debug << ss << endl;
213 
214  // $$JB temporary removed
[466]215        //mViewCellsManager->Visualize(objects, mSampleRays);   
[1715]216 
217  return true;
[362]218}
[349]219
[441]220
[362]221}
Note: See TracBrowser for help on using the repository browser.