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