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 |
|
---|
167 |
|
---|
168 |
|
---|
169 | bool
|
---|
170 | RssPreprocessor::ExportRssTree(char *filename,
|
---|
171 | RssTree *tree,
|
---|
172 | const Vector3 &dir
|
---|
173 | )
|
---|
174 | {
|
---|
175 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
176 | exporter->SetFilled();
|
---|
177 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
178 | // exporter->SetWireframe();
|
---|
179 | bool result = exporter->ExportRssTree2( *tree, dir );
|
---|
180 | delete exporter;
|
---|
181 | return result;
|
---|
182 | }
|
---|
183 |
|
---|
184 | bool
|
---|
185 | RssPreprocessor::ExportRssTreeLeaf(char *filename,
|
---|
186 | RssTree *tree,
|
---|
187 | RssTreeLeaf *leaf)
|
---|
188 | {
|
---|
189 | Exporter *exporter = NULL;
|
---|
190 | exporter = Exporter::GetExporter(filename);
|
---|
191 | exporter->SetWireframe();
|
---|
192 | exporter->ExportKdTree(*mKdTree);
|
---|
193 |
|
---|
194 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
195 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
196 | exporter->ResetForcedMaterial();
|
---|
197 |
|
---|
198 | VssRayContainer rays[4];
|
---|
199 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
200 | int k = leaf->rays[i].GetRayClass();
|
---|
201 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
202 | }
|
---|
203 |
|
---|
204 | // SOURCE RAY
|
---|
205 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
206 | // TERMINATION RAY
|
---|
207 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
208 | // PASSING_RAY
|
---|
209 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
210 | // CONTAINED_RAY
|
---|
211 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
212 |
|
---|
213 | delete exporter;
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 |
|
---|
217 | void
|
---|
218 | RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
|
---|
219 | {
|
---|
220 | vector<RssTreeLeaf *> leaves;
|
---|
221 | tree->CollectLeaves(leaves);
|
---|
222 |
|
---|
223 | int num = 0;
|
---|
224 | int i;
|
---|
225 | float p = number / (float)leaves.size();
|
---|
226 | for (i=0; i < leaves.size(); i++) {
|
---|
227 | if (RandomValue(0,1) < p) {
|
---|
228 | char filename[64];
|
---|
229 | sprintf(filename, "rss-leaf-%04d.x3d", num);
|
---|
230 | ExportRssTreeLeaf(filename, tree, leaves[i]);
|
---|
231 | num++;
|
---|
232 | }
|
---|
233 | if (num >= number)
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | float
|
---|
240 | RssPreprocessor::GetAvgPvsSize(RssTree *tree,
|
---|
241 | const vector<AxisAlignedBox3> &viewcells
|
---|
242 | )
|
---|
243 | {
|
---|
244 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
245 |
|
---|
246 | int sum = 0;
|
---|
247 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
248 | sum += tree->GetPvsSize(*it);
|
---|
249 |
|
---|
250 | return sum/(float)viewcells.size();
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | void
|
---|
255 | RssPreprocessor::ExportPvs(char *filename,
|
---|
256 | RssTree *rssTree
|
---|
257 | )
|
---|
258 | {
|
---|
259 | ObjectContainer pvs;
|
---|
260 | if (rssTree->CollectRootPvs(pvs)) {
|
---|
261 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
262 | exporter->SetFilled();
|
---|
263 | exporter->ExportGeometry(pvs);
|
---|
264 | exporter->SetWireframe();
|
---|
265 | exporter->ExportBox(rssTree->bbox);
|
---|
266 | exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
|
---|
267 | delete exporter;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|
273 | void
|
---|
274 | NormalizeRatios(vector<SamplingStrategy *> &distributions)
|
---|
275 | {
|
---|
276 | int i;
|
---|
277 | float sumRatios = 0.0f;
|
---|
278 |
|
---|
279 | for (i=0; i < distributions.size(); i++)
|
---|
280 | sumRatios += distributions[i]->mRatio;
|
---|
281 |
|
---|
282 |
|
---|
283 | if (sumRatios == 0.0f) {
|
---|
284 | for (i=0; i < distributions.size(); i++) {
|
---|
285 | distributions[i]->mRatio = 1.0f;
|
---|
286 | sumRatios += 1.0f;
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | for (i=0 ; i < distributions.size(); i++)
|
---|
291 | distributions[i]->mRatio/=sumRatios;
|
---|
292 |
|
---|
293 | #define MIN_RATIO 0.1f
|
---|
294 |
|
---|
295 | for (i = 0; i < distributions.size(); i++)
|
---|
296 | if (distributions[i]->mRatio < MIN_RATIO)
|
---|
297 | distributions[i]->mRatio = MIN_RATIO;
|
---|
298 |
|
---|
299 |
|
---|
300 | sumRatios = 0.0f;
|
---|
301 | for (i=0; i < distributions.size(); i++)
|
---|
302 | sumRatios += distributions[i]->mRatio;
|
---|
303 |
|
---|
304 | for (i=0 ; i < distributions.size(); i++)
|
---|
305 | distributions[i]->mRatio/=sumRatios;
|
---|
306 |
|
---|
307 |
|
---|
308 | cout<<"New ratios: ";
|
---|
309 | for (i=0 ; i < distributions.size(); i++)
|
---|
310 | cout<<distributions[i]->mRatio<<" ";
|
---|
311 | cout<<endl;
|
---|
312 | }
|
---|
313 |
|
---|
314 | void
|
---|
315 | EvaluateRatios(vector<SamplingStrategy *> &distributions)
|
---|
316 | {
|
---|
317 | // now evaluate the ratios for the next pass
|
---|
318 | #define TIME_WEIGHT 0.0f
|
---|
319 | for (int i=0; i < distributions.size(); i++) {
|
---|
320 | distributions[i]->mRatio = distributions[i]->mContribution/
|
---|
321 | (Limits::Small +
|
---|
322 | (TIME_WEIGHT*distributions[i]->mTime +
|
---|
323 | (1 - TIME_WEIGHT)*distributions[i]->mRays)
|
---|
324 | );
|
---|
325 |
|
---|
326 | #if 1
|
---|
327 | distributions[i]->mRatio = sqr(distributions[i]->mRatio);
|
---|
328 | #endif
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 |
|
---|
334 | bool
|
---|
335 | RssPreprocessor::ComputeVisibility()
|
---|
336 | {
|
---|
337 |
|
---|
338 | Debug << "type: rss" << endl;
|
---|
339 |
|
---|
340 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
341 | // cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
342 |
|
---|
343 | Randomize(0);
|
---|
344 |
|
---|
345 | if (0) {
|
---|
346 | Exporter *exporter = Exporter::GetExporter("samples.wrl");
|
---|
347 | exporter->SetFilled();
|
---|
348 | Halton<4> halton;
|
---|
349 |
|
---|
350 | // for (int i=1; i < 7; i++)
|
---|
351 | // cout<<i<<" prime= "<<FindPrime(i)<<endl;
|
---|
352 |
|
---|
353 | Material mA, mB;
|
---|
354 | mA.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
355 | mB.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
356 |
|
---|
357 | for (int i=0; i < 10000; i++) {
|
---|
358 | float r[4];
|
---|
359 | halton.GetNext(r);
|
---|
360 | Vector3 v1 = UniformRandomVector(r[0], r[1]);
|
---|
361 | Vector3 v2 = UniformRandomVector(r[2], r[3]);
|
---|
362 |
|
---|
363 | const float size =0.002f;
|
---|
364 | AxisAlignedBox3 box(v1-Vector3(size), v1+Vector3(size));
|
---|
365 | exporter->SetForcedMaterial(mA);
|
---|
366 | exporter->ExportBox(box);
|
---|
367 | AxisAlignedBox3 box2(v2-Vector3(size), v2+Vector3(size));
|
---|
368 |
|
---|
369 | exporter->SetForcedMaterial(mB);
|
---|
370 | exporter->ExportBox(box2);
|
---|
371 | }
|
---|
372 | delete exporter;
|
---|
373 | }
|
---|
374 |
|
---|
375 | if (!GetThread())
|
---|
376 | cerr << "!!no thread available!" << endl;
|
---|
377 |
|
---|
378 | cout<<"COMPUTATION THREAD="<<GetThread()->GetCurrentThreadId()<<endl<<flush;
|
---|
379 |
|
---|
380 | // use ray buffer to export ray animation
|
---|
381 | const bool useRayBuffer = false;
|
---|
382 |
|
---|
383 | vector<VssRayContainer> rayBuffer(50);
|
---|
384 |
|
---|
385 | long startTime = GetTime();
|
---|
386 | long lastTime;
|
---|
387 | float totalTime;
|
---|
388 |
|
---|
389 | int totalSamples = 0;
|
---|
390 |
|
---|
391 | // if not already loaded, construct view cells from file
|
---|
392 | if (!mLoadViewCells)
|
---|
393 | {
|
---|
394 | // construct view cells using it's own set of samples
|
---|
395 | mViewCellsManager->Construct(this);
|
---|
396 |
|
---|
397 | //-- several visualizations and statistics
|
---|
398 | Debug << "view cells construction finished: " << endl;
|
---|
399 | mViewCellsManager->PrintStatistics(Debug);
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | int rssPass = 0;
|
---|
404 | int rssSamples = 0;
|
---|
405 |
|
---|
406 | // now decode distribution string
|
---|
407 | char buff[1024];
|
---|
408 | Environment::GetSingleton()->GetStringValue("RssPreprocessor.distributions",
|
---|
409 | buff);
|
---|
410 |
|
---|
411 | char *curr = buff;
|
---|
412 | mUseRssTree = false;
|
---|
413 |
|
---|
414 | while (1) {
|
---|
415 | char *e = strchr(curr,'+');
|
---|
416 | if (e!=NULL) {
|
---|
417 | *e=0;
|
---|
418 | }
|
---|
419 |
|
---|
420 | if (strcmp(curr, "rss")==0) {
|
---|
421 | mDistributions.push_back(new RssBasedDistribution(*this));
|
---|
422 | mUseRssTree = true;
|
---|
423 | } else
|
---|
424 | if (strcmp(curr, "object")==0) {
|
---|
425 | mDistributions.push_back(new ObjectBasedDistribution(*this));
|
---|
426 | } else
|
---|
427 | if (strcmp(curr, "spatial")==0) {
|
---|
428 | mDistributions.push_back(new SpatialBoxBasedDistribution(*this));
|
---|
429 | } else
|
---|
430 | if (strcmp(curr, "global")==0) {
|
---|
431 | mDistributions.push_back(new GlobalLinesDistribution(*this));
|
---|
432 | } else
|
---|
433 | if (strcmp(curr, "direction")==0) {
|
---|
434 | mDistributions.push_back(new DirectionBasedDistribution(*this));
|
---|
435 | } else
|
---|
436 | if (strcmp(curr, "object_direction")==0) {
|
---|
437 | mDistributions.push_back(new ObjectDirectionBasedDistribution(*this));
|
---|
438 | } else
|
---|
439 | if (strcmp(curr, "reverse_object")==0) {
|
---|
440 | mDistributions.push_back(new ReverseObjectBasedDistribution(*this));
|
---|
441 | } else
|
---|
442 | if (strcmp(curr, "reverse_viewspace_border")==0) {
|
---|
443 | mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(*this));
|
---|
444 | }
|
---|
445 |
|
---|
446 | if (e==NULL)
|
---|
447 | break;
|
---|
448 | curr = e+1;
|
---|
449 | }
|
---|
450 |
|
---|
451 | mMixtureDistribution = new MixtureDistribution(*this);
|
---|
452 | mMixtureDistribution->mDistributions = mDistributions;
|
---|
453 | mMixtureDistribution->Init();
|
---|
454 | bool oldGenerator = false;
|
---|
455 |
|
---|
456 | const int batch = 100000;
|
---|
457 |
|
---|
458 | if (mLoadInitialSamples) {
|
---|
459 | cout << "Loading samples from file ... ";
|
---|
460 | LoadSamples(mVssRays, mObjects);
|
---|
461 | cout << "finished\n" << endl;
|
---|
462 | } else {
|
---|
463 | SimpleRayContainer rays;
|
---|
464 |
|
---|
465 | if (oldGenerator) {
|
---|
466 |
|
---|
467 | cout<<"Generating initial rays..."<<endl<<flush;
|
---|
468 |
|
---|
469 | int count = 0;
|
---|
470 | int i;
|
---|
471 |
|
---|
472 | // Generate initial samples
|
---|
473 | for (i=0; i < mDistributions.size(); i++)
|
---|
474 | if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
|
---|
475 | count++;
|
---|
476 |
|
---|
477 | for (i=0; i < mDistributions.size(); i++)
|
---|
478 | if (mDistributions[i]->mType != SamplingStrategy::RSS_BASED_DISTRIBUTION)
|
---|
479 | GenerateRays(mInitialSamples/count,
|
---|
480 | *mDistributions[i],
|
---|
481 | rays);
|
---|
482 |
|
---|
483 | cout<<"Casting initial rays..."<<endl<<flush;
|
---|
484 | CastRays(rays, mVssRays, true, pruneInvalidRays);
|
---|
485 |
|
---|
486 | ExportObjectRays(mVssRays, 1546);
|
---|
487 |
|
---|
488 | cout<<"Computing sample contributions..."<<endl<<flush;
|
---|
489 | // evaluate contributions of the intitial rays
|
---|
490 | mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
491 | cout<<"done.\n"<<flush;
|
---|
492 |
|
---|
493 | } else {
|
---|
494 | for (int i=0; i < mInitialSamples; i+=batch) {
|
---|
495 | rays.clear();
|
---|
496 | mVssRays.clear();
|
---|
497 | mMixtureDistribution->GenerateSamples(batch, rays);
|
---|
498 | CastRays(rays, mVssRays, true, pruneInvalidRays);
|
---|
499 | mMixtureDistribution->ComputeContributions(mVssRays);
|
---|
500 | }
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
505 |
|
---|
506 | rssSamples += (int)mVssRays.size();
|
---|
507 | totalSamples += mInitialSamples;
|
---|
508 | mPass++;
|
---|
509 |
|
---|
510 | if (mExportRays) {
|
---|
511 |
|
---|
512 | char filename[64];
|
---|
513 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
514 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
515 |
|
---|
516 | if (useRayBuffer)
|
---|
517 | mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
|
---|
518 |
|
---|
519 | }
|
---|
520 |
|
---|
521 | if (mStoreInitialSamples) {
|
---|
522 | cout << "Writing samples to file ... ";
|
---|
523 | ExportSamples(mVssRays);
|
---|
524 | cout << "finished\n" << endl;
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | long time = GetTime();
|
---|
529 | totalTime = TimeDiff(startTime, time)*1e-3f;
|
---|
530 | lastTime = time;
|
---|
531 |
|
---|
532 | mStats <<
|
---|
533 | "#Pass\n" <<mPass<<endl<<
|
---|
534 | "#RssPass\n" <<rssPass<<endl<<
|
---|
535 | "#Time\n" << totalTime <<endl<<
|
---|
536 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
537 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
538 |
|
---|
539 | {
|
---|
540 | VssRayContainer contributingRays;
|
---|
541 | mVssRays.GetContributingRays(contributingRays, 0);
|
---|
542 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
543 | if (mExportRays) {
|
---|
544 | char filename[64];
|
---|
545 | sprintf(filename, "rss-crays-%04d.x3d", 0);
|
---|
546 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
547 | }
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | // viewcells->UpdatePVS(newVssRays);
|
---|
552 | Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
553 | // cull viewcells with PVS > median (0.5f)
|
---|
554 | //mViewCellsManager->SetValidityPercentage(0, 0.5f);
|
---|
555 | // mViewCellsManager->SetValidityPercentage(0, 1.0f);
|
---|
556 | Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
557 |
|
---|
558 | mVssRays.PrintStatistics(mStats);
|
---|
559 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
560 |
|
---|
561 |
|
---|
562 | if (0) {
|
---|
563 | char str[64];
|
---|
564 | sprintf(str, "tmp/v-");
|
---|
565 |
|
---|
566 | // visualization
|
---|
567 | const bool exportRays = true;
|
---|
568 | const bool exportPvs = true;
|
---|
569 | ObjectContainer objects;
|
---|
570 | mViewCellsManager->ExportSingleViewCells(objects,
|
---|
571 | 1000,
|
---|
572 | false,
|
---|
573 | exportPvs,
|
---|
574 | exportRays,
|
---|
575 | 10000,
|
---|
576 | str);
|
---|
577 | }
|
---|
578 |
|
---|
579 | if (renderer) {
|
---|
580 | ComputeRenderError();
|
---|
581 | }
|
---|
582 |
|
---|
583 | rssPass++;
|
---|
584 |
|
---|
585 | // mDistributions.resize(1);
|
---|
586 |
|
---|
587 | mRssTree = new RssTree;
|
---|
588 | mRssTree->SetPass(mPass);
|
---|
589 |
|
---|
590 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
591 | // mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
592 |
|
---|
593 | if (mUseRssTree) {
|
---|
594 | mRssTree->Construct(mObjects, mVssRays);
|
---|
595 |
|
---|
596 | mRssTree->stat.Print(mStats);
|
---|
597 | cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
|
---|
598 |
|
---|
599 | if (mExportRssTree) {
|
---|
600 | ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
|
---|
601 | ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
|
---|
602 | ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
|
---|
603 | ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
|
---|
604 | ExportRssTreeLeaves(mRssTree, 10);
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | if (mExportPvs) {
|
---|
609 | ExportPvs("rss-pvs-initial.x3d", mRssTree);
|
---|
610 | }
|
---|
611 |
|
---|
612 |
|
---|
613 | // keep only rss
|
---|
614 | // mDistributions.resize(1);
|
---|
615 |
|
---|
616 | while (1) {
|
---|
617 |
|
---|
618 | static SimpleRayContainer rays;
|
---|
619 | static VssRayContainer vssRays;
|
---|
620 | static VssRayContainer tmpVssRays;
|
---|
621 |
|
---|
622 | cout<<"H1"<<endl<<flush;
|
---|
623 |
|
---|
624 | //rays.reserve((int)(1.1f*mRssSamplesPerPass));
|
---|
625 |
|
---|
626 | cout<<"H10"<<endl<<flush;
|
---|
627 |
|
---|
628 | rays.clear();
|
---|
629 | vssRays.clear();
|
---|
630 | tmpVssRays.clear();
|
---|
631 |
|
---|
632 | int castRays = 0;
|
---|
633 |
|
---|
634 | cout<<"H11"<<endl<<flush;
|
---|
635 |
|
---|
636 | NormalizeRatios(mDistributions);
|
---|
637 |
|
---|
638 | long t1;
|
---|
639 | cout<<"H2"<<endl<<flush;
|
---|
640 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
641 | t1 = GetTime();
|
---|
642 | rays.clear();
|
---|
643 | tmpVssRays.clear();
|
---|
644 | if (mDistributions[i]->mRatio != 0) {
|
---|
645 | GenerateRays(int(mRssSamplesPerPass*mDistributions[i]->mRatio),
|
---|
646 | *mDistributions[i],
|
---|
647 | rays);
|
---|
648 |
|
---|
649 | #if 0
|
---|
650 | rays.NormalizePdf((float)rays.size());
|
---|
651 | #endif
|
---|
652 | CastRays(rays, tmpVssRays, true, pruneInvalidRays);
|
---|
653 | castRays += (int)rays.size();
|
---|
654 |
|
---|
655 | cout<<"Computing sample contributions..."<<endl<<flush;
|
---|
656 |
|
---|
657 | #if ADD_RAYS_IN_PLACE
|
---|
658 | float contribution =
|
---|
659 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
|
---|
660 | #else
|
---|
661 | float contribution =
|
---|
662 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
663 | #endif
|
---|
664 | mDistributions[i]->mContribution = contribution;
|
---|
665 | mDistributions[i]->mTotalContribution += contribution;
|
---|
666 |
|
---|
667 | cout<<"done."<<endl;
|
---|
668 | }
|
---|
669 |
|
---|
670 | mDistributions[i]->mTime = TimeDiff(t1, GetTime());
|
---|
671 | // mDistributions[i]->mRays = (int)rays.size();
|
---|
672 | mDistributions[i]->mRays = (int)tmpVssRays.size();
|
---|
673 | mDistributions[i]->mTotalRays = (int)tmpVssRays.size();
|
---|
674 |
|
---|
675 | // mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
|
---|
676 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
677 | }
|
---|
678 |
|
---|
679 | EvaluateRatios(mDistributions);
|
---|
680 |
|
---|
681 | // add contributions of all rays at once...
|
---|
682 | #if !ADD_RAYS_IN_PLACE
|
---|
683 | mViewCellsManager->AddSampleContributions(vssRays);
|
---|
684 | #endif
|
---|
685 |
|
---|
686 |
|
---|
687 | totalSamples += castRays;
|
---|
688 | rssSamples += (int)vssRays.size();
|
---|
689 |
|
---|
690 | cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
|
---|
691 | mInitialSamples)<<"%\n";
|
---|
692 |
|
---|
693 |
|
---|
694 | long time = GetTime();
|
---|
695 | totalTime += TimeDiff(lastTime, time);
|
---|
696 | lastTime = time;
|
---|
697 |
|
---|
698 | mStats <<
|
---|
699 | "#Pass\n" <<mPass<<endl<<
|
---|
700 | "#RssPass\n" <<rssPass<<endl<<
|
---|
701 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
702 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
703 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
704 |
|
---|
705 |
|
---|
706 | Debug<<"Print statistics...\n"<<flush;
|
---|
707 | vssRays.PrintStatistics(mStats);
|
---|
708 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
709 | Debug<<"done.\n"<<flush;
|
---|
710 |
|
---|
711 | if (renderer && mPass > 0) {
|
---|
712 | Debug<<"Computing render errror..."<<endl<<flush;
|
---|
713 | char buf[100];
|
---|
714 | sprintf(buf, "snap/i-%02d-", mPass);
|
---|
715 |
|
---|
716 | renderer->SetSnapPrefix(buf);
|
---|
717 |
|
---|
718 | renderer->SetSnapErrorFrames(true);
|
---|
719 | ComputeRenderError();
|
---|
720 | Debug<<"done."<<endl<<flush;
|
---|
721 | }
|
---|
722 |
|
---|
723 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
724 |
|
---|
725 | if (mExportRays) {
|
---|
726 | Debug<<"Exporting rays..."<<endl<<flush;
|
---|
727 | char filename[64];
|
---|
728 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
729 |
|
---|
730 | if (useRayBuffer)
|
---|
731 | vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
|
---|
732 |
|
---|
733 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
734 |
|
---|
735 | // now export all contributing rays
|
---|
736 | VssRayContainer contributingRays;
|
---|
737 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
738 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
739 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
740 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
741 |
|
---|
742 | Debug<<"done."<<endl<<flush;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
747 | // already when adding into the tree
|
---|
748 | // do not add those rays which have too low or no contribution....
|
---|
749 | if (mUseRssTree) {
|
---|
750 | Debug<<"Adding rays...\n"<<flush;
|
---|
751 | mRssTree->AddRays(vssRays);
|
---|
752 | Debug<<"done.\n"<<flush;
|
---|
753 | if (mUpdateSubdivision) {
|
---|
754 | int updatePasses = 1;
|
---|
755 | if (mPass % updatePasses == 0) {
|
---|
756 | Debug<<"Updating rss tree...\n"<<flush;
|
---|
757 | int subdivided = mRssTree->UpdateSubdivision();
|
---|
758 | Debug<<"done.\n"<<flush;
|
---|
759 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
760 | cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
|
---|
761 | }
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | if (mExportPvs) {
|
---|
766 | char filename[64];
|
---|
767 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
768 | ExportPvs(filename, mRssTree);
|
---|
769 | }
|
---|
770 |
|
---|
771 |
|
---|
772 | if (!mUseRssTree)
|
---|
773 | CLEAR_CONTAINER(vssRays);
|
---|
774 | // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
|
---|
775 |
|
---|
776 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
777 | break;
|
---|
778 |
|
---|
779 |
|
---|
780 | rssPass++;
|
---|
781 | mPass++;
|
---|
782 | mRssTree->SetPass(mPass);
|
---|
783 | }
|
---|
784 |
|
---|
785 | if(0) {
|
---|
786 | VssRayContainer selectedRays;
|
---|
787 | int desired = mViewCellsManager->GetVisualizationSamples();
|
---|
788 |
|
---|
789 | mVssRays.SelectRays(desired, selectedRays);
|
---|
790 |
|
---|
791 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
792 | }
|
---|
793 |
|
---|
794 | // view cells after sampling
|
---|
795 | mViewCellsManager->PrintStatistics(Debug);
|
---|
796 |
|
---|
797 | EvalViewCellHistogram();
|
---|
798 |
|
---|
799 | //-- render simulation after merge
|
---|
800 | cout << "\nEvaluating view cells render time after sampling ... ";
|
---|
801 |
|
---|
802 | mRenderSimulator->RenderScene();
|
---|
803 | SimulationStatistics ss;
|
---|
804 | mRenderSimulator->GetStatistics(ss);
|
---|
805 |
|
---|
806 | cout << " finished" << endl;
|
---|
807 | cout << ss << endl;
|
---|
808 | Debug << ss << endl;
|
---|
809 |
|
---|
810 | if (useRayBuffer && mExportRays) {
|
---|
811 | char filename[64];
|
---|
812 | sprintf(filename, "rss-rays-i.x3d");
|
---|
813 |
|
---|
814 | rayBuffer.resize(mPass);
|
---|
815 | ExportRayAnimation(filename, rayBuffer);
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | // do not delete rss tree now - can be used for visualization..
|
---|
820 | #if 0
|
---|
821 | Debug<<"Deleting RSS tree...\n";
|
---|
822 | delete mRssTree;
|
---|
823 | Debug<<"Done.\n";
|
---|
824 | #endif
|
---|
825 |
|
---|
826 |
|
---|
827 | return true;
|
---|
828 | }
|
---|
829 |
|
---|
830 | }
|
---|