1 | #include "SceneGraph.h"
|
---|
2 | #include "KdTree.h"
|
---|
3 | #include "RssPreprocessor.h"
|
---|
4 | #include "X3dExporter.h"
|
---|
5 | #include "Environment.h"
|
---|
6 | #include "MutualVisibility.h"
|
---|
7 | #include "Polygon3.h"
|
---|
8 | #include "ViewCell.h"
|
---|
9 | #include "VssRay.h"
|
---|
10 | #include "RssTree.h"
|
---|
11 | #include "ViewCellsManager.h"
|
---|
12 | #include "RenderSimulator.h"
|
---|
13 | #include "GlRenderer.h"
|
---|
14 | #include "SamplingStrategy.h"
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | namespace GtpVisibilityPreprocessor {
|
---|
19 |
|
---|
20 |
|
---|
21 | static bool useViewSpaceBox = false;
|
---|
22 | static bool use2dSampling = false;
|
---|
23 |
|
---|
24 |
|
---|
25 | // not supported anymore!
|
---|
26 | static bool fromBoxVisibility = false;
|
---|
27 |
|
---|
28 | #define ADD_RAYS_IN_PLACE 1
|
---|
29 |
|
---|
30 | RssPreprocessor::RssPreprocessor():
|
---|
31 | mVssRays()
|
---|
32 | {
|
---|
33 | // this should increase coherence of the samples
|
---|
34 | Environment::GetSingleton()->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
35 | Environment::GetSingleton()->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
|
---|
36 | Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
|
---|
37 | Environment::GetSingleton()->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
|
---|
38 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
39 |
|
---|
40 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useRssTree",
|
---|
41 | mUseRssTree);
|
---|
42 |
|
---|
43 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
|
---|
44 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
|
---|
45 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
|
---|
46 | Environment::GetSingleton()->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
|
---|
47 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
|
---|
48 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling);
|
---|
49 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling);
|
---|
50 |
|
---|
51 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
52 |
|
---|
53 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
54 |
|
---|
55 | Environment::GetSingleton()->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
|
---|
56 |
|
---|
57 | mRssTree = NULL;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | int
|
---|
62 | RssPreprocessor::GenerateRays(
|
---|
63 | const int number,
|
---|
64 | const int sampleType,
|
---|
65 | SimpleRayContainer &rays
|
---|
66 | )
|
---|
67 | {
|
---|
68 | int result = 0;
|
---|
69 |
|
---|
70 | Debug<<"Generate rays...\n"<<flush;
|
---|
71 |
|
---|
72 | switch (sampleType) {
|
---|
73 | case SamplingStrategy::RSS_BASED_DISTRIBUTION:
|
---|
74 | case SamplingStrategy::RSS_SILHOUETTE_BASED_DISTRIBUTION:
|
---|
75 | if (mRssTree) {
|
---|
76 | result = GenerateImportanceRays(mRssTree, number, rays);
|
---|
77 | }
|
---|
78 | break;
|
---|
79 |
|
---|
80 | default:
|
---|
81 | result = Preprocessor::GenerateRays(number, sampleType, rays);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // rays.NormalizePdf();
|
---|
85 | Debug<<"done.\n"<<flush;
|
---|
86 |
|
---|
87 | return result;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | RssPreprocessor::~RssPreprocessor()
|
---|
94 | {
|
---|
95 | // mVssRays get deleted in the tree
|
---|
96 | // CLEAR_CONTAINER(mVssRays);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 | void
|
---|
102 | RssPreprocessor::ExportObjectRays(VssRayContainer &rays,
|
---|
103 | const int objectId)
|
---|
104 | {
|
---|
105 | ObjectContainer::const_iterator oi;
|
---|
106 |
|
---|
107 | Intersectable *object = NULL;
|
---|
108 | for (oi = mObjects.begin(); oi != mObjects.end(); ++oi)
|
---|
109 | if (objectId == (*oi)->GetId()) {
|
---|
110 | object = *oi;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (object == NULL)
|
---|
115 | return;
|
---|
116 |
|
---|
117 | VssRayContainer selectedRays;
|
---|
118 | VssRayContainer::const_iterator it= rays.begin(), it_end = rays.end();
|
---|
119 |
|
---|
120 |
|
---|
121 | for (; it != it_end; ++it) {
|
---|
122 | if ((*it)->mTerminationObject == object)
|
---|
123 | selectedRays.push_back(*it);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | Exporter *exporter = Exporter::GetExporter("object-rays.x3d");
|
---|
128 | // exporter->SetWireframe();
|
---|
129 | // exporter->ExportKdTree(*mKdTree);
|
---|
130 | exporter->SetFilled();
|
---|
131 | exporter->ExportIntersectable(object);
|
---|
132 | exporter->ExportRays(selectedRays, RgbColor(1, 0, 0));
|
---|
133 |
|
---|
134 | delete exporter;
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | int
|
---|
140 | RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
|
---|
141 | const int desiredSamples,
|
---|
142 | SimpleRayContainer &rays
|
---|
143 | )
|
---|
144 | {
|
---|
145 | int num;
|
---|
146 |
|
---|
147 | rssTree->UpdateTreeStatistics();
|
---|
148 |
|
---|
149 | cout<<
|
---|
150 | "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<<
|
---|
151 | "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<<
|
---|
152 | "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<<
|
---|
153 | "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<<
|
---|
154 | "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<<
|
---|
155 | "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl;
|
---|
156 |
|
---|
157 | if (0) {
|
---|
158 | float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
|
---|
159 | num = rssTree->GenerateRays(p, rays);
|
---|
160 | } else {
|
---|
161 | int leaves = rssTree->stat.Leaves()/1;
|
---|
162 |
|
---|
163 | num = rssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | return num;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | bool
|
---|
172 | RssPreprocessor::ExportRays(const char *filename,
|
---|
173 | const VssRayContainer &vssRays,
|
---|
174 | const int number
|
---|
175 | )
|
---|
176 | {
|
---|
177 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
178 |
|
---|
179 | Exporter *exporter = NULL;
|
---|
180 | exporter = Exporter::GetExporter(filename);
|
---|
181 | if (0) {
|
---|
182 | exporter->SetWireframe();
|
---|
183 | exporter->ExportKdTree(*mKdTree);
|
---|
184 | }
|
---|
185 | exporter->SetFilled();
|
---|
186 | // $$JB temporarily do not export the scene
|
---|
187 | if (0)
|
---|
188 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
189 | exporter->SetWireframe();
|
---|
190 |
|
---|
191 | if (1) {
|
---|
192 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
193 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
194 | exporter->ResetForcedMaterial();
|
---|
195 | }
|
---|
196 |
|
---|
197 | VssRayContainer rays;
|
---|
198 |
|
---|
199 | vssRays.SelectRays(number, rays);
|
---|
200 |
|
---|
201 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
202 |
|
---|
203 | delete exporter;
|
---|
204 |
|
---|
205 | cout<<"done."<<endl<<flush;
|
---|
206 |
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 |
|
---|
210 | bool
|
---|
211 | RssPreprocessor::ExportRayAnimation(const char *filename,
|
---|
212 | const vector<VssRayContainer> &vssRays
|
---|
213 | )
|
---|
214 | {
|
---|
215 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
216 |
|
---|
217 | Exporter *exporter = NULL;
|
---|
218 | exporter = Exporter::GetExporter(filename);
|
---|
219 | if (0) {
|
---|
220 | exporter->SetWireframe();
|
---|
221 | exporter->ExportKdTree(*mKdTree);
|
---|
222 | }
|
---|
223 | exporter->SetFilled();
|
---|
224 | // $$JB temporarily do not export the scene
|
---|
225 | if (1)
|
---|
226 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
227 | exporter->SetWireframe();
|
---|
228 |
|
---|
229 | if (1) {
|
---|
230 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
231 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
232 | exporter->ResetForcedMaterial();
|
---|
233 | }
|
---|
234 |
|
---|
235 | exporter->ExportRaySets(vssRays, RgbColor(1, 0, 0));
|
---|
236 |
|
---|
237 | delete exporter;
|
---|
238 |
|
---|
239 | cout<<"done."<<endl<<flush;
|
---|
240 |
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | bool
|
---|
246 | RssPreprocessor::ExportRssTree(char *filename,
|
---|
247 | RssTree *tree,
|
---|
248 | const Vector3 &dir
|
---|
249 | )
|
---|
250 | {
|
---|
251 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
252 | exporter->SetFilled();
|
---|
253 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
254 | // exporter->SetWireframe();
|
---|
255 | bool result = exporter->ExportRssTree2( *tree, dir );
|
---|
256 | delete exporter;
|
---|
257 | return result;
|
---|
258 | }
|
---|
259 |
|
---|
260 | bool
|
---|
261 | RssPreprocessor::ExportRssTreeLeaf(char *filename,
|
---|
262 | RssTree *tree,
|
---|
263 | RssTreeLeaf *leaf)
|
---|
264 | {
|
---|
265 | Exporter *exporter = NULL;
|
---|
266 | exporter = Exporter::GetExporter(filename);
|
---|
267 | exporter->SetWireframe();
|
---|
268 | exporter->ExportKdTree(*mKdTree);
|
---|
269 |
|
---|
270 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
271 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
272 | exporter->ResetForcedMaterial();
|
---|
273 |
|
---|
274 | VssRayContainer rays[4];
|
---|
275 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
276 | int k = leaf->rays[i].GetRayClass();
|
---|
277 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
278 | }
|
---|
279 |
|
---|
280 | // SOURCE RAY
|
---|
281 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
282 | // TERMINATION RAY
|
---|
283 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
284 | // PASSING_RAY
|
---|
285 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
286 | // CONTAINED_RAY
|
---|
287 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
288 |
|
---|
289 | delete exporter;
|
---|
290 | return true;
|
---|
291 | }
|
---|
292 |
|
---|
293 | void
|
---|
294 | RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
|
---|
295 | {
|
---|
296 | vector<RssTreeLeaf *> leaves;
|
---|
297 | tree->CollectLeaves(leaves);
|
---|
298 |
|
---|
299 | int num = 0;
|
---|
300 | int i;
|
---|
301 | float p = number / (float)leaves.size();
|
---|
302 | for (i=0; i < leaves.size(); i++) {
|
---|
303 | if (RandomValue(0,1) < p) {
|
---|
304 | char filename[64];
|
---|
305 | sprintf(filename, "rss-leaf-%04d.x3d", num);
|
---|
306 | ExportRssTreeLeaf(filename, tree, leaves[i]);
|
---|
307 | num++;
|
---|
308 | }
|
---|
309 | if (num >= number)
|
---|
310 | break;
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | float
|
---|
316 | RssPreprocessor::GetAvgPvsSize(RssTree *tree,
|
---|
317 | const vector<AxisAlignedBox3> &viewcells
|
---|
318 | )
|
---|
319 | {
|
---|
320 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
321 |
|
---|
322 | int sum = 0;
|
---|
323 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
324 | sum += tree->GetPvsSize(*it);
|
---|
325 |
|
---|
326 | return sum/(float)viewcells.size();
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | void
|
---|
331 | RssPreprocessor::ExportPvs(char *filename,
|
---|
332 | RssTree *rssTree
|
---|
333 | )
|
---|
334 | {
|
---|
335 | ObjectContainer pvs;
|
---|
336 | if (rssTree->CollectRootPvs(pvs)) {
|
---|
337 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
338 | exporter->SetFilled();
|
---|
339 | exporter->ExportGeometry(pvs);
|
---|
340 | exporter->SetWireframe();
|
---|
341 | exporter->ExportBox(rssTree->bbox);
|
---|
342 | exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
|
---|
343 | delete exporter;
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | void
|
---|
349 | RssPreprocessor::ComputeRenderError()
|
---|
350 | {
|
---|
351 | // compute rendering error
|
---|
352 |
|
---|
353 | if (renderer && renderer->mPvsStatFrames) {
|
---|
354 | // emit EvalPvsStat();
|
---|
355 | // QMutex mutex;
|
---|
356 | // mutex.lock();
|
---|
357 | // renderer->mRenderingFinished.wait(&mutex);
|
---|
358 | // mutex.unlock();
|
---|
359 |
|
---|
360 | renderer->EvalPvsStat();
|
---|
361 |
|
---|
362 | mStats <<
|
---|
363 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
364 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
365 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
|
---|
366 | "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | void
|
---|
371 | NormalizeRatios(vector<SamplingStrategy *> &distributions)
|
---|
372 | {
|
---|
373 | int i;
|
---|
374 | float sumRatios = 0.0f;
|
---|
375 |
|
---|
376 | for (i=0; i < distributions.size(); i++)
|
---|
377 | sumRatios += distributions[i]->mRatio;
|
---|
378 |
|
---|
379 |
|
---|
380 | if (sumRatios == 0.0f) {
|
---|
381 | for (i=0; i < distributions.size(); i++) {
|
---|
382 | distributions[i]->mRatio = 1.0f;
|
---|
383 | sumRatios += 1.0f;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | for (i=0 ; i < distributions.size(); i++)
|
---|
388 | distributions[i]->mRatio/=sumRatios;
|
---|
389 |
|
---|
390 | #define MIN_RATIO 0.1f
|
---|
391 |
|
---|
392 | for (i = 0; i < distributions.size(); i++)
|
---|
393 | if (distributions[i]->mRatio < MIN_RATIO)
|
---|
394 | distributions[i]->mRatio = MIN_RATIO;
|
---|
395 |
|
---|
396 |
|
---|
397 | sumRatios = 0.0f;
|
---|
398 | for (i=0; i < distributions.size(); i++)
|
---|
399 | sumRatios += distributions[i]->mRatio;
|
---|
400 |
|
---|
401 | for (i=0 ; i < distributions.size(); i++)
|
---|
402 | distributions[i]->mRatio/=sumRatios;
|
---|
403 |
|
---|
404 |
|
---|
405 | cout<<"New ratios: ";
|
---|
406 | for (i=0 ; i < distributions.size(); i++)
|
---|
407 | cout<<distributions[i]->mRatio<<" ";
|
---|
408 | cout<<endl;
|
---|
409 | }
|
---|
410 |
|
---|
411 | void
|
---|
412 | EvaluateRatios(vector<SamplingStrategy *> &distributions)
|
---|
413 | {
|
---|
414 | // now evaluate the ratios for the next pass
|
---|
415 | #define TIME_WEIGHT 0.5f
|
---|
416 | for (int i=0; i < distributions.size(); i++) {
|
---|
417 | distributions[i]->mRatio = distributions[i]->mContribution/
|
---|
418 | (TIME_WEIGHT*distributions[i]->mTime +
|
---|
419 | (1 - TIME_WEIGHT)*distributions[i]->mRays);
|
---|
420 |
|
---|
421 | #if 1
|
---|
422 | distributions[i]->mRatio = sqr(distributions[i]->mRatio);
|
---|
423 | #endif
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 |
|
---|
429 | bool
|
---|
430 | RssPreprocessor::ComputeVisibility()
|
---|
431 | {
|
---|
432 |
|
---|
433 | Debug << "type: rss" << endl;
|
---|
434 |
|
---|
435 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
436 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
437 |
|
---|
438 | Randomize(0);
|
---|
439 |
|
---|
440 | // use ray buffer to export ray animation
|
---|
441 | const bool useRayBuffer = false;
|
---|
442 |
|
---|
443 | vector<VssRayContainer> rayBuffer(50);
|
---|
444 |
|
---|
445 | long startTime = GetTime();
|
---|
446 | long lastTime;
|
---|
447 | float totalTime;
|
---|
448 |
|
---|
449 | int totalSamples = 0;
|
---|
450 |
|
---|
451 | // if not already loaded, construct view cells from file
|
---|
452 | if (!mLoadViewCells)
|
---|
453 | {
|
---|
454 | // construct view cells using it's own set of samples
|
---|
455 | mViewCellsManager->Construct(this);
|
---|
456 |
|
---|
457 | //-- several visualizations and statistics
|
---|
458 | Debug << "view cells construction finished: " << endl;
|
---|
459 | mViewCellsManager->PrintStatistics(Debug);
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | int rssPass = 0;
|
---|
464 | int rssSamples = 0;
|
---|
465 |
|
---|
466 | if (mUseRssTree)
|
---|
467 | mDistributions.push_back(new RssBasedDistribution(*this));
|
---|
468 |
|
---|
469 |
|
---|
470 | if (1) {
|
---|
471 | // mDistributions.push_back(new SpatialBoxBasedDistribution(*this));
|
---|
472 | // mDistributions.push_back(new DirectionBasedDistribution(*this));
|
---|
473 | mDistributions.push_back(new ObjectDirectionBasedDistribution(*this));
|
---|
474 | // mDistributions.push_back(new ReverseObjectBasedDistribution(*this));
|
---|
475 | mDistributions.push_back(new ObjectBasedDistribution(*this));
|
---|
476 | mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(*this));
|
---|
477 | } else {
|
---|
478 | mDistributions.push_back(new GlobalLinesDistribution(*this));
|
---|
479 | }
|
---|
480 |
|
---|
481 | if (mLoadInitialSamples) {
|
---|
482 | cout << "Loading samples from file ... ";
|
---|
483 | LoadSamples(mVssRays, mObjects);
|
---|
484 | cout << "finished\n" << endl;
|
---|
485 | } else {
|
---|
486 | SimpleRayContainer rays;
|
---|
487 |
|
---|
488 | cout<<"Generating initial rays..."<<endl<<flush;
|
---|
489 |
|
---|
490 | if (mUseImportanceSampling) {
|
---|
491 | int count = 0;
|
---|
492 | int i;
|
---|
493 |
|
---|
494 | // Generate initial samples
|
---|
495 | for (i=0; i < mDistributions.size(); i++)
|
---|
496 | if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
|
---|
497 | count++;
|
---|
498 |
|
---|
499 | for (i=0; i < mDistributions.size(); i++)
|
---|
500 | if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
|
---|
501 | GenerateRays(mInitialSamples/count,
|
---|
502 | mDistributions[i]->mType,
|
---|
503 | rays);
|
---|
504 |
|
---|
505 | } else {
|
---|
506 | int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
507 | if (mObjectBasedSampling)
|
---|
508 | rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
|
---|
509 | else
|
---|
510 | if (mDirectionalSampling)
|
---|
511 | rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
|
---|
512 | cout<<"Generating rays..."<<endl;
|
---|
513 | GenerateRays(mRssSamplesPerPass, rayType, rays);
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | cout<<"Casting initial rays..."<<endl<<flush;
|
---|
518 | CastRays(rays, mVssRays, true);
|
---|
519 |
|
---|
520 | ExportObjectRays(mVssRays, 1546);
|
---|
521 | }
|
---|
522 |
|
---|
523 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
524 |
|
---|
525 | rssSamples += (int)mVssRays.size();
|
---|
526 | totalSamples += mInitialSamples;
|
---|
527 | mPass++;
|
---|
528 |
|
---|
529 | if (mExportRays) {
|
---|
530 |
|
---|
531 | char filename[64];
|
---|
532 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
533 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
534 |
|
---|
535 | if (useRayBuffer)
|
---|
536 | mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
|
---|
537 |
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (mStoreInitialSamples) {
|
---|
541 | cout << "Writing samples to file ... ";
|
---|
542 | ExportSamples(mVssRays);
|
---|
543 | cout << "finished\n" << endl;
|
---|
544 | }
|
---|
545 |
|
---|
546 | if (mUseViewcells) {
|
---|
547 |
|
---|
548 | cout<<"Computing sample contributions..."<<endl<<flush;
|
---|
549 | // evaluate contributions of the intitial rays
|
---|
550 | mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
551 | cout<<"done.\n"<<flush;
|
---|
552 |
|
---|
553 | long time = GetTime();
|
---|
554 | totalTime = TimeDiff(startTime, time)*1e-3f;
|
---|
555 | lastTime = time;
|
---|
556 |
|
---|
557 | mStats <<
|
---|
558 | "#Pass\n" <<mPass<<endl<<
|
---|
559 | "#RssPass\n" <<rssPass<<endl<<
|
---|
560 | "#Time\n" << totalTime <<endl<<
|
---|
561 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
562 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
563 |
|
---|
564 | {
|
---|
565 | VssRayContainer contributingRays;
|
---|
566 | mVssRays.GetContributingRays(contributingRays, 0);
|
---|
567 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
568 | if (mExportRays) {
|
---|
569 | char filename[64];
|
---|
570 | sprintf(filename, "rss-crays-%04d.x3d", 0);
|
---|
571 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | // viewcells->UpdatePVS(newVssRays);
|
---|
577 | Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
578 | // cull viewcells with PVS > median (0.5f)
|
---|
579 | //mViewCellsManager->SetValidityPercentage(0, 0.5f);
|
---|
580 | mViewCellsManager->SetValidityPercentage(0, 1.0f);
|
---|
581 | Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
582 |
|
---|
583 | mVssRays.PrintStatistics(mStats);
|
---|
584 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
585 |
|
---|
586 | ComputeRenderError();
|
---|
587 | }
|
---|
588 |
|
---|
589 | rssPass++;
|
---|
590 |
|
---|
591 | // mDistributions.resize(1);
|
---|
592 |
|
---|
593 | mRssTree = new RssTree;
|
---|
594 | mRssTree->SetPass(mPass);
|
---|
595 |
|
---|
596 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
597 | // mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
598 |
|
---|
599 | if (mUseImportanceSampling) {
|
---|
600 |
|
---|
601 | if (mUseRssTree) {
|
---|
602 | mRssTree->Construct(mObjects, mVssRays);
|
---|
603 |
|
---|
604 | mRssTree->stat.Print(mStats);
|
---|
605 | cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
|
---|
606 |
|
---|
607 | if (mExportRssTree) {
|
---|
608 | ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
|
---|
609 | ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
|
---|
610 | ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
|
---|
611 | ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
|
---|
612 | ExportRssTreeLeaves(mRssTree, 10);
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | if (mExportPvs) {
|
---|
617 | ExportPvs("rss-pvs-initial.x3d", mRssTree);
|
---|
618 | }
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 |
|
---|
623 | while (1) {
|
---|
624 |
|
---|
625 | static SimpleRayContainer rays;
|
---|
626 | static VssRayContainer vssRays;
|
---|
627 | static VssRayContainer tmpVssRays;
|
---|
628 |
|
---|
629 | rays.reserve((int)(1.1f*mRssSamplesPerPass));
|
---|
630 |
|
---|
631 | rays.clear();
|
---|
632 | vssRays.clear();
|
---|
633 | tmpVssRays.clear();
|
---|
634 |
|
---|
635 | int castRays = 0;
|
---|
636 | if (mUseImportanceSampling) {
|
---|
637 |
|
---|
638 | NormalizeRatios(mDistributions);
|
---|
639 |
|
---|
640 | long t1;
|
---|
641 |
|
---|
642 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
643 | t1 = GetTime();
|
---|
644 | rays.clear();
|
---|
645 | tmpVssRays.clear();
|
---|
646 | if (mDistributions[i]->mRatio != 0) {
|
---|
647 | GenerateRays(int(mRssSamplesPerPass*mDistributions[i]->mRatio),
|
---|
648 | mDistributions[i]->mType,
|
---|
649 | rays);
|
---|
650 |
|
---|
651 | rays.NormalizePdf((float)rays.size());
|
---|
652 |
|
---|
653 | CastRays(rays, tmpVssRays, true);
|
---|
654 | castRays += (int)rays.size();
|
---|
655 |
|
---|
656 | #if ADD_RAYS_IN_PLACE
|
---|
657 | mDistributions[i]->mContribution =
|
---|
658 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
|
---|
659 | #else
|
---|
660 | mDistributions[i]->mContribution =
|
---|
661 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
662 | #endif
|
---|
663 | }
|
---|
664 |
|
---|
665 | mDistributions[i]->mTime = TimeDiff(t1, GetTime());
|
---|
666 | mDistributions[i]->mRays = (int)rays.size();
|
---|
667 | // mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
|
---|
668 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
669 | }
|
---|
670 |
|
---|
671 | EvaluateRatios(mDistributions);
|
---|
672 |
|
---|
673 | // add contributions of all rays at once...
|
---|
674 | #if !ADD_RAYS_IN_PLACE
|
---|
675 | mViewCellsManager->AddSampleContributions(vssRays);
|
---|
676 | #endif
|
---|
677 | }
|
---|
678 | else {
|
---|
679 | int rayType = SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
680 | if (mObjectBasedSampling)
|
---|
681 | rayType = SamplingStrategy::OBJECT_BASED_DISTRIBUTION;
|
---|
682 | else
|
---|
683 | if (mDirectionalSampling)
|
---|
684 | rayType = SamplingStrategy::DIRECTION_BASED_DISTRIBUTION;
|
---|
685 |
|
---|
686 | cout<<"Generating rays..."<<endl;
|
---|
687 |
|
---|
688 | GenerateRays(mRssSamplesPerPass, rayType, rays);
|
---|
689 | cout<<"done."<<endl;
|
---|
690 |
|
---|
691 | cout<<"Casting rays..."<<endl; CastRays(rays, vssRays, true);
|
---|
692 | cout<<"done."<<endl;
|
---|
693 | castRays += (int)rays.size();
|
---|
694 | if (mUseViewcells) {
|
---|
695 | /// compute view cell contribution of rays
|
---|
696 | cout<<"Computing sample contributions..."<<endl;
|
---|
697 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
698 | cout<<"done."<<endl;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | }
|
---|
703 | totalSamples += castRays;
|
---|
704 | rssSamples += (int)vssRays.size();
|
---|
705 |
|
---|
706 | cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
|
---|
707 | mInitialSamples)<<"%\n";
|
---|
708 |
|
---|
709 |
|
---|
710 | long time = GetTime();
|
---|
711 | totalTime += TimeDiff(lastTime, time);
|
---|
712 | lastTime = time;
|
---|
713 |
|
---|
714 | mStats <<
|
---|
715 | "#Pass\n" <<mPass<<endl<<
|
---|
716 | "#RssPass\n" <<rssPass<<endl<<
|
---|
717 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
718 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
719 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
720 |
|
---|
721 |
|
---|
722 | if (mUseViewcells) {
|
---|
723 | Debug<<"Print statistics...\n"<<flush;
|
---|
724 | vssRays.PrintStatistics(mStats);
|
---|
725 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
726 | Debug<<"done.\n"<<flush;
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | if (renderer && mPass > 0) {
|
---|
731 | char buf[100];
|
---|
732 | if (mUseImportanceSampling)
|
---|
733 | {
|
---|
734 | sprintf(buf, "snap/i-%02d-", mPass);
|
---|
735 |
|
---|
736 | renderer->SetSnapPrefix(buf);
|
---|
737 | }
|
---|
738 | else
|
---|
739 | {
|
---|
740 | sprintf(buf, "snap/r-%02d-", mPass);
|
---|
741 |
|
---|
742 | renderer->SetSnapPrefix(buf);
|
---|
743 | }
|
---|
744 |
|
---|
745 | renderer->SetSnapErrorFrames(true);
|
---|
746 | }
|
---|
747 |
|
---|
748 | ComputeRenderError();
|
---|
749 |
|
---|
750 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
751 |
|
---|
752 | if (mExportRays) {
|
---|
753 | char filename[64];
|
---|
754 | if (mUseImportanceSampling)
|
---|
755 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
756 | else
|
---|
757 | sprintf(filename, "rss-rays-%04d.x3d", rssPass);
|
---|
758 |
|
---|
759 |
|
---|
760 |
|
---|
761 | if (useRayBuffer)
|
---|
762 | vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
|
---|
763 |
|
---|
764 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
765 |
|
---|
766 | // now export all contributing rays
|
---|
767 | VssRayContainer contributingRays;
|
---|
768 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
769 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
770 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
771 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
772 | }
|
---|
773 |
|
---|
774 |
|
---|
775 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
776 | // already when adding into the tree
|
---|
777 | // do not add those rays which have too low or no contribution....
|
---|
778 |
|
---|
779 |
|
---|
780 | if (mUseRssTree && mUseImportanceSampling) {
|
---|
781 | Debug<<"Adding rays...\n"<<flush;
|
---|
782 | mRssTree->AddRays(vssRays);
|
---|
783 | Debug<<"done.\n"<<flush;
|
---|
784 | if (mUpdateSubdivision) {
|
---|
785 | int updatePasses = 1;
|
---|
786 | if (mPass % updatePasses == 0) {
|
---|
787 | Debug<<"Updating rss tree...\n"<<flush;
|
---|
788 | int subdivided = mRssTree->UpdateSubdivision();
|
---|
789 | Debug<<"done.\n"<<flush;
|
---|
790 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
791 | cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
|
---|
792 | }
|
---|
793 | }
|
---|
794 | }
|
---|
795 |
|
---|
796 | if (mExportPvs) {
|
---|
797 | char filename[64];
|
---|
798 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
799 | ExportPvs(filename, mRssTree);
|
---|
800 | }
|
---|
801 |
|
---|
802 |
|
---|
803 | if (!mUseImportanceSampling || !mUseRssTree)
|
---|
804 | CLEAR_CONTAINER(vssRays);
|
---|
805 | // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
|
---|
806 |
|
---|
807 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
808 | break;
|
---|
809 |
|
---|
810 |
|
---|
811 | rssPass++;
|
---|
812 | mPass++;
|
---|
813 | mRssTree->SetPass(mPass);
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (mUseViewcells) {
|
---|
817 |
|
---|
818 | if(0) {
|
---|
819 | VssRayContainer selectedRays;
|
---|
820 | int desired = mViewCellsManager->GetVisualizationSamples();
|
---|
821 |
|
---|
822 | mVssRays.SelectRays(desired, selectedRays);
|
---|
823 |
|
---|
824 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
825 | }
|
---|
826 |
|
---|
827 | // view cells after sampling
|
---|
828 | mViewCellsManager->PrintStatistics(Debug);
|
---|
829 |
|
---|
830 | EvalViewCellHistogram();
|
---|
831 |
|
---|
832 | //-- render simulation after merge
|
---|
833 | cout << "\nevaluating bsp view cells render time after sampling ... ";
|
---|
834 |
|
---|
835 | mRenderSimulator->RenderScene();
|
---|
836 | SimulationStatistics ss;
|
---|
837 | mRenderSimulator->GetStatistics(ss);
|
---|
838 |
|
---|
839 | cout << " finished" << endl;
|
---|
840 | cout << ss << endl;
|
---|
841 | Debug << ss << endl;
|
---|
842 | }
|
---|
843 |
|
---|
844 |
|
---|
845 | if (useRayBuffer&& mExportRays && mUseImportanceSampling) {
|
---|
846 | char filename[64];
|
---|
847 | sprintf(filename, "rss-rays-i.x3d");
|
---|
848 |
|
---|
849 | rayBuffer.resize(mPass);
|
---|
850 | ExportRayAnimation(filename, rayBuffer);
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 | // do not delete rss tree now - can be used for visualization..
|
---|
855 | #if 0
|
---|
856 | Debug<<"Deleting RSS tree...\n";
|
---|
857 | delete mRssTree;
|
---|
858 | Debug<<"Done.\n";
|
---|
859 | #endif
|
---|
860 |
|
---|
861 | //mViewCellsManager->ExportViewCells("visibility.xml",
|
---|
862 | // true);
|
---|
863 |
|
---|
864 |
|
---|
865 | return true;
|
---|
866 | }
|
---|
867 |
|
---|
868 | }
|
---|