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