source: GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp @ 2726

Revision 2726, 4.9 KB checked in by mattausch, 17 years ago (diff)

worked on gvs efficiency

  • Property svn:executable set to *
Line 
1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "X3dExporter.h"
4#include "Environment.h"
5#include "Polygon3.h"
6#include "VssRay.h"
7#include "RssTree.h"
8#include "ViewCellsManager.h"
9#include "RenderSimulator.h"
10#include "GlRenderer.h"
11#include "SamplingStrategy.h"
12#include "PreprocessorThread.h"
13#include "CombinedPreprocessor.h"
14#include "RayCaster.h"
15#include <windows.h>
16
17
18namespace GtpVisibilityPreprocessor {
19
20
21const bool pruneInvalidRays = true;
22
23 
24  CombinedPreprocessor::CombinedPreprocessor():
25        Preprocessor()
26  {
27        // this should increase coherence of the samples
28//      Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mTotalSamples);
29//      Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
30//      Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass",
31//                                                                                       mSamplesPerEvaluation);
32       
33       
34  }
35
36
37
38bool
39CombinedPreprocessor::ComputeVisibility()
40{
41 
42  Debug << "Preprocessor type: combined" << endl;
43  cout<<"Combined Preprocessor started\n"<<flush;
44 
45  // if not already loaded, construct view cells from file
46  if (!mLoadViewCells) {
47        // construct view cells using it's own set of samples
48        mViewCellsManager->Construct(this);
49       
50        //-- several visualizations and statistics
51        Debug << "view cells construction finished: " << endl;
52        mViewCellsManager->PrintStatistics(Debug);
53  }
54 
55 
56  // now decode distribution string
57  char buff[1024];
58  Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions",
59                                                                                          buff);
60 
61 
62  mMixtureDistribution = new MixtureDistribution(*this);
63  mMixtureDistribution->Construct(buff);
64 
65  SimpleRayContainer rays;
66  VssRayContainer vssRays;
67
68  int lastEvaluation = 0;
69 
70  mPass = 0;
71 
72  vector<VssRayContainer> rayBuffer;
73  rays.reserve(mSamplesPerPass);
74  rayBuffer.reserve(2*mSamplesPerPass);
75 
76  long startTime = GetTime();
77
78  mCurrentSamples = 0;
79
80  for (mCurrentSamples =0; mCurrentSamples < mTotalSamples; mCurrentSamples += mSamplesPerPass, mPass++) {
81          while (mSynchronize)
82          {
83                  // hack: sleep during walthrough computation
84                  Sleep(1000);
85          }
86
87        mRayCaster->InitPass();
88       
89        cout<<"Progress :"<<mCurrentSamples/1e6f<<"M rays, "<<(100.0f*mCurrentSamples)/mTotalSamples<<"%"<<endl;
90       
91        rays.clear();
92        vssRays.clear();
93       
94        int invalidSamples = 0;
95        mMixtureDistribution->GenerateSamples(mSamplesPerPass, rays, invalidSamples);
96
97        cerr<<"Casting rays..."<<endl;
98       
99        bool doubleRays = true;
100        CastRays(rays, vssRays, doubleRays, pruneInvalidRays);
101        mTotalRaysCast += (int)vssRays.size();
102
103        cerr<<"Done..."<<endl;
104
105        cerr<<"Computing ray contributions..."<<endl;
106        mMixtureDistribution->ComputeContributions(vssRays);
107        cerr<<"Done..."<<endl;
108
109
110        if (mExportRays) {
111          Debug<<"Exporting rays..."<<endl<<flush;
112          char filename[256];
113
114          VssRayContainer contributingRays;
115          vssRays.GetContributingRays(contributingRays, 0);
116          sprintf(filename, "rss-crays-%04d.x3d", mPass);
117          ExportRays(filename, contributingRays, mExportNumRays);
118
119          sprintf(filename, "rss-rays-i%04d.x3d", mPass);
120          ExportRays(filename, vssRays, mExportNumRays);
121
122
123          if (mExportAnimation) {
124                rayBuffer.push_back(VssRayContainer());
125                vssRays.SelectRays(mExportNumRays, rayBuffer[rayBuffer.size()-1], true);
126          }
127
128          Debug<<"done."<<endl<<flush;
129        }
130       
131        mMixtureDistribution->UpdateDistributions(vssRays);
132       
133        if (mCurrentSamples - lastEvaluation >= mSamplesPerEvaluation) {
134          Real time = TimeDiff(startTime, GetTime());
135#if 0 // matt: remove temporarily
136          mViewCellsManager->PrintPvsStatistics(mStats);
137#endif
138          mStats <<
139                "#Pass\n" <<mPass<<endl<<
140                "#Time\n" << time <<endl<<
141                "#TotalSamples\n" << mCurrentSamples <<endl<<
142                "#RssSamples\n" <<mTotalRaysCast<<endl;
143
144          float last = 0.0f;
145          for (int k=0; k < 6; k++) {
146                float ratio = 0.0f;
147                if (k < mMixtureDistribution->mDistributions.size()) {
148                  ratio = mMixtureDistribution->mDistributions[k]->mRatio-last;
149                  last = mMixtureDistribution->mDistributions[k]->mRatio;;
150                }
151                mStats <<
152                  "#Distribution"<<k<<endl<<
153                  ratio<<endl;
154          }
155         
156          lastEvaluation = mCurrentSamples;
157         
158          if (renderer) {
159                cout<<"Evaluating render error"<<endl;
160                ComputeRenderError();
161                cout<<"finished evaluating error"<<endl;
162          }
163        }
164       
165       
166        Real time = TimeDiff(startTime, GetTime());
167        if (mTotalTime!= -1 && time/1000 > mTotalTime)
168          break;
169
170#if 0
171        cerr<<"deleting rays"<<endl;
172        //clear all unreferenced rays
173        for (int j=0; j < vssRays.size(); j++) {
174          if (vssRays[j]->RefCount() == 0)
175                delete vssRays[j];
176        }
177        cerr<<"done."<<endl;
178#endif 
179       
180        //      if (!mMixtureDistribution->RequiresRays())
181        //        CLEAR_CONTAINER(vssRays);
182
183       
184  }
185 
186  EvalViewCellHistogram();
187
188  if (mExportAnimation && mExportRays) {
189        char filename[64];
190        sprintf(filename, "rss-rays-i.x3d");
191        //rayBuffer.resize(mPass);
192        ExportRayAnimation(filename, rayBuffer);
193  }
194
195  return true;
196}
197
198}
Note: See TracBrowser for help on using the repository browser.