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 | int invalidSamples = 0;
|
---|
495 | for (int i=0; i < mInitialSamples; i+=batch) {
|
---|
496 | rays.clear();
|
---|
497 | mVssRays.clear();
|
---|
498 | mMixtureDistribution->GenerateSamples(batch, rays, invalidSamples);
|
---|
499 | CastRays(rays, mVssRays, true, pruneInvalidRays);
|
---|
500 | mMixtureDistribution->ComputeContributions(mVssRays);
|
---|
501 | }
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
506 |
|
---|
507 | rssSamples += (int)mVssRays.size();
|
---|
508 | totalSamples += mInitialSamples;
|
---|
509 | mPass++;
|
---|
510 |
|
---|
511 | if (mExportRays) {
|
---|
512 |
|
---|
513 | char filename[64];
|
---|
514 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
515 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
516 |
|
---|
517 | if (useRayBuffer)
|
---|
518 | mVssRays.SelectRays(mExportNumRays, rayBuffer[0], true);
|
---|
519 |
|
---|
520 | }
|
---|
521 |
|
---|
522 | if (mStoreInitialSamples) {
|
---|
523 | cout << "Writing samples to file ... ";
|
---|
524 | ExportSamples(mVssRays);
|
---|
525 | cout << "finished\n" << endl;
|
---|
526 | }
|
---|
527 |
|
---|
528 |
|
---|
529 | long time = GetTime();
|
---|
530 | totalTime = TimeDiff(startTime, time)*1e-3f;
|
---|
531 | lastTime = time;
|
---|
532 |
|
---|
533 | mStats <<
|
---|
534 | "#Pass\n" <<mPass<<endl<<
|
---|
535 | "#RssPass\n" <<rssPass<<endl<<
|
---|
536 | "#Time\n" << totalTime <<endl<<
|
---|
537 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
538 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
539 |
|
---|
540 | {
|
---|
541 | VssRayContainer contributingRays;
|
---|
542 | mVssRays.GetContributingRays(contributingRays, 0);
|
---|
543 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
544 | if (mExportRays) {
|
---|
545 | char filename[64];
|
---|
546 | sprintf(filename, "rss-crays-%04d.x3d", 0);
|
---|
547 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | // viewcells->UpdatePVS(newVssRays);
|
---|
553 | Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
554 | // cull viewcells with PVS > median (0.5f)
|
---|
555 | //mViewCellsManager->SetValidityPercentage(0, 0.5f);
|
---|
556 | // mViewCellsManager->SetValidityPercentage(0, 1.0f);
|
---|
557 | Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
|
---|
558 |
|
---|
559 | mVssRays.PrintStatistics(mStats);
|
---|
560 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
561 |
|
---|
562 |
|
---|
563 | if (0) {
|
---|
564 | char str[64];
|
---|
565 | sprintf(str, "tmp/v-");
|
---|
566 |
|
---|
567 | // visualization
|
---|
568 | const bool exportRays = true;
|
---|
569 | const bool exportPvs = true;
|
---|
570 | ObjectContainer objects;
|
---|
571 | mViewCellsManager->ExportSingleViewCells(objects,
|
---|
572 | 1000,
|
---|
573 | false,
|
---|
574 | exportPvs,
|
---|
575 | exportRays,
|
---|
576 | 10000,
|
---|
577 | str);
|
---|
578 | }
|
---|
579 |
|
---|
580 | if (renderer) {
|
---|
581 | ComputeRenderError();
|
---|
582 | }
|
---|
583 |
|
---|
584 | rssPass++;
|
---|
585 |
|
---|
586 | // mDistributions.resize(1);
|
---|
587 |
|
---|
588 | mRssTree = new RssTree;
|
---|
589 | mRssTree->SetPass(mPass);
|
---|
590 |
|
---|
591 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
592 | // mViewCellsManager->ComputeSampleContributions(mVssRays, true, false);
|
---|
593 |
|
---|
594 | if (mUseRssTree) {
|
---|
595 | mRssTree->Construct(mObjects, mVssRays);
|
---|
596 |
|
---|
597 | mRssTree->stat.Print(mStats);
|
---|
598 | cout<<"RssTree root PVS size = "<<mRssTree->GetRootPvsSize()<<endl;
|
---|
599 |
|
---|
600 | if (mExportRssTree) {
|
---|
601 | ExportRssTree("rss-tree-100.x3d", mRssTree, Vector3(1,0,0));
|
---|
602 | ExportRssTree("rss-tree-001.x3d", mRssTree, Vector3(0,0,1));
|
---|
603 | ExportRssTree("rss-tree-101.x3d", mRssTree, Vector3(1,0,1));
|
---|
604 | ExportRssTree("rss-tree-101m.x3d", mRssTree, Vector3(-1,0,-1));
|
---|
605 | ExportRssTreeLeaves(mRssTree, 10);
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | if (mExportPvs) {
|
---|
610 | ExportPvs("rss-pvs-initial.x3d", mRssTree);
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | // keep only rss
|
---|
615 | // mDistributions.resize(1);
|
---|
616 |
|
---|
617 | while (1) {
|
---|
618 |
|
---|
619 | static SimpleRayContainer rays;
|
---|
620 | static VssRayContainer vssRays;
|
---|
621 | static VssRayContainer tmpVssRays;
|
---|
622 |
|
---|
623 | cout<<"H1"<<endl<<flush;
|
---|
624 |
|
---|
625 | //rays.reserve((int)(1.1f*mRssSamplesPerPass));
|
---|
626 |
|
---|
627 | cout<<"H10"<<endl<<flush;
|
---|
628 |
|
---|
629 | rays.clear();
|
---|
630 | vssRays.clear();
|
---|
631 | tmpVssRays.clear();
|
---|
632 |
|
---|
633 | int castRays = 0;
|
---|
634 |
|
---|
635 | cout<<"H11"<<endl<<flush;
|
---|
636 |
|
---|
637 | NormalizeRatios(mDistributions);
|
---|
638 |
|
---|
639 | long t1;
|
---|
640 | cout<<"H2"<<endl<<flush;
|
---|
641 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
642 | t1 = GetTime();
|
---|
643 | rays.clear();
|
---|
644 | tmpVssRays.clear();
|
---|
645 | if (mDistributions[i]->mRatio != 0) {
|
---|
646 | GenerateRays(int(mRssSamplesPerPass*mDistributions[i]->mRatio),
|
---|
647 | *mDistributions[i],
|
---|
648 | rays);
|
---|
649 |
|
---|
650 | #if 0
|
---|
651 | rays.NormalizePdf((float)rays.size());
|
---|
652 | #endif
|
---|
653 | CastRays(rays, tmpVssRays, true, pruneInvalidRays);
|
---|
654 | castRays += (int)rays.size();
|
---|
655 |
|
---|
656 | cout<<"Computing sample contributions..."<<endl<<flush;
|
---|
657 |
|
---|
658 | #if ADD_RAYS_IN_PLACE
|
---|
659 | float contribution =
|
---|
660 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
|
---|
661 | #else
|
---|
662 | float contribution =
|
---|
663 | mViewCellsManager->ComputeSampleContributions(tmpVssRays, false, true);
|
---|
664 | #endif
|
---|
665 | mDistributions[i]->mContribution = contribution;
|
---|
666 | mDistributions[i]->mTotalContribution += contribution;
|
---|
667 |
|
---|
668 | cout<<"done."<<endl;
|
---|
669 | }
|
---|
670 |
|
---|
671 | mDistributions[i]->mTime = TimeDiff(t1, GetTime());
|
---|
672 | // mDistributions[i]->mRays = (int)rays.size();
|
---|
673 | mDistributions[i]->mRays = (int)tmpVssRays.size();
|
---|
674 | mDistributions[i]->mTotalRays = (int)tmpVssRays.size();
|
---|
675 |
|
---|
676 | // mStats<<"#RssRelContrib"<<endl<<contributions[0]/nrays[0]<<endl;
|
---|
677 | vssRays.insert(vssRays.end(), tmpVssRays.begin(), tmpVssRays.end() );
|
---|
678 | }
|
---|
679 |
|
---|
680 | EvaluateRatios(mDistributions);
|
---|
681 |
|
---|
682 | // add contributions of all rays at once...
|
---|
683 | #if !ADD_RAYS_IN_PLACE
|
---|
684 | mViewCellsManager->AddSampleContributions(vssRays);
|
---|
685 | #endif
|
---|
686 |
|
---|
687 |
|
---|
688 | totalSamples += castRays;
|
---|
689 | rssSamples += (int)vssRays.size();
|
---|
690 |
|
---|
691 | cout<<"Generated "<<castRays<<" rays, progress "<<100.0f*totalSamples/((float) mRssSamples +
|
---|
692 | mInitialSamples)<<"%\n";
|
---|
693 |
|
---|
694 |
|
---|
695 | long time = GetTime();
|
---|
696 | totalTime += TimeDiff(lastTime, time);
|
---|
697 | lastTime = time;
|
---|
698 |
|
---|
699 | mStats <<
|
---|
700 | "#Pass\n" <<mPass<<endl<<
|
---|
701 | "#RssPass\n" <<rssPass<<endl<<
|
---|
702 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
703 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
704 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
705 |
|
---|
706 |
|
---|
707 | Debug<<"Print statistics...\n"<<flush;
|
---|
708 | vssRays.PrintStatistics(mStats);
|
---|
709 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
710 | Debug<<"done.\n"<<flush;
|
---|
711 |
|
---|
712 | if (renderer && mPass > 0) {
|
---|
713 | Debug<<"Computing render errror..."<<endl<<flush;
|
---|
714 | char buf[100];
|
---|
715 | sprintf(buf, "snap/i-%02d-", mPass);
|
---|
716 |
|
---|
717 | renderer->SetSnapPrefix(buf);
|
---|
718 |
|
---|
719 | renderer->SetSnapErrorFrames(true);
|
---|
720 | ComputeRenderError();
|
---|
721 | Debug<<"done."<<endl<<flush;
|
---|
722 | }
|
---|
723 |
|
---|
724 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
725 |
|
---|
726 | if (mExportRays) {
|
---|
727 | Debug<<"Exporting rays..."<<endl<<flush;
|
---|
728 | char filename[64];
|
---|
729 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
730 |
|
---|
731 | if (useRayBuffer)
|
---|
732 | vssRays.SelectRays(mExportNumRays, rayBuffer[mPass], true);
|
---|
733 |
|
---|
734 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
735 |
|
---|
736 | // now export all contributing rays
|
---|
737 | VssRayContainer contributingRays;
|
---|
738 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
739 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
740 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
741 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
742 |
|
---|
743 | Debug<<"done."<<endl<<flush;
|
---|
744 | }
|
---|
745 |
|
---|
746 |
|
---|
747 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
748 | // already when adding into the tree
|
---|
749 | // do not add those rays which have too low or no contribution....
|
---|
750 | if (mUseRssTree) {
|
---|
751 | Debug<<"Adding rays...\n"<<flush;
|
---|
752 | mRssTree->AddRays(vssRays);
|
---|
753 | Debug<<"done.\n"<<flush;
|
---|
754 | if (mUpdateSubdivision) {
|
---|
755 | int updatePasses = 1;
|
---|
756 | if (mPass % updatePasses == 0) {
|
---|
757 | Debug<<"Updating rss tree...\n"<<flush;
|
---|
758 | int subdivided = mRssTree->UpdateSubdivision();
|
---|
759 | Debug<<"done.\n"<<flush;
|
---|
760 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
761 | cout<<"#total leaves = "<<mRssTree->stat.Leaves()<<endl;
|
---|
762 | }
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | if (mExportPvs) {
|
---|
767 | char filename[64];
|
---|
768 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
769 | ExportPvs(filename, mRssTree);
|
---|
770 | }
|
---|
771 |
|
---|
772 |
|
---|
773 | if (!mUseRssTree)
|
---|
774 | CLEAR_CONTAINER(vssRays);
|
---|
775 | // otherwise the rays get deleted by the rss tree update according to RssTree.maxRays ....
|
---|
776 |
|
---|
777 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
778 | break;
|
---|
779 |
|
---|
780 |
|
---|
781 | rssPass++;
|
---|
782 | mPass++;
|
---|
783 | mRssTree->SetPass(mPass);
|
---|
784 | }
|
---|
785 |
|
---|
786 | if(0) {
|
---|
787 | VssRayContainer selectedRays;
|
---|
788 | int desired = mViewCellsManager->GetVisualizationSamples();
|
---|
789 |
|
---|
790 | mVssRays.SelectRays(desired, selectedRays);
|
---|
791 |
|
---|
792 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
793 | }
|
---|
794 |
|
---|
795 | // view cells after sampling
|
---|
796 | mViewCellsManager->PrintStatistics(Debug);
|
---|
797 |
|
---|
798 | EvalViewCellHistogram();
|
---|
799 |
|
---|
800 | //-- render simulation after merge
|
---|
801 | cout << "\nEvaluating view cells render time after sampling ... ";
|
---|
802 |
|
---|
803 | mRenderSimulator->RenderScene();
|
---|
804 | SimulationStatistics ss;
|
---|
805 | mRenderSimulator->GetStatistics(ss);
|
---|
806 |
|
---|
807 | cout << " finished" << endl;
|
---|
808 | cout << ss << endl;
|
---|
809 | Debug << ss << endl;
|
---|
810 |
|
---|
811 | if (useRayBuffer && mExportRays) {
|
---|
812 | char filename[64];
|
---|
813 | sprintf(filename, "rss-rays-i.x3d");
|
---|
814 |
|
---|
815 | rayBuffer.resize(mPass);
|
---|
816 | ExportRayAnimation(filename, rayBuffer);
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | // do not delete rss tree now - can be used for visualization..
|
---|
821 | #if 0
|
---|
822 | Debug<<"Deleting RSS tree...\n";
|
---|
823 | delete mRssTree;
|
---|
824 | Debug<<"Done.\n";
|
---|
825 | #endif
|
---|
826 |
|
---|
827 |
|
---|
828 | return true;
|
---|
829 | }
|
---|
830 |
|
---|
831 | }
|
---|