1 | #include "SceneGraph.h"
|
---|
2 | #include "KdTree.h"
|
---|
3 | #include "VssPreprocessor.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 "VssTree.h"
|
---|
11 | #include "ViewCellsManager.h"
|
---|
12 | #include "RenderSimulator.h"
|
---|
13 | #include "Beam.h"
|
---|
14 | #include "GlRenderer.h"
|
---|
15 | #include "Intersectable.h"
|
---|
16 | #include "RayCaster.h"
|
---|
17 | #include "SamplingStrategy.h"
|
---|
18 | #include "ViewCellBsp.h"
|
---|
19 |
|
---|
20 |
|
---|
21 | namespace GtpVisibilityPreprocessor {
|
---|
22 |
|
---|
23 |
|
---|
24 | bool use2dSampling = false;
|
---|
25 | bool useViewspacePlane = false;
|
---|
26 |
|
---|
27 | VssPreprocessor::VssPreprocessor():
|
---|
28 | mVssRays()
|
---|
29 | {
|
---|
30 | // this should increase coherence of the samples
|
---|
31 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
32 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
33 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
34 | // Environment::GetSingleton()->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
35 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
36 |
|
---|
37 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
38 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
39 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
|
---|
40 | Environment::GetSingleton()->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
|
---|
41 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
42 |
|
---|
43 | useViewspacePlane = mUseViewSpaceBox; //hack
|
---|
44 |
|
---|
45 | Debug << "*********** vss preprocessor options **************" << endl;
|
---|
46 | Debug << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
47 | Debug << "enlarge view space=" << mEnlargeViewSpace << endl;
|
---|
48 | Debug << "*********** end vss preprocessor options **************" << endl;
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | VssPreprocessor::~VssPreprocessor()
|
---|
54 | {
|
---|
55 | CLEAR_CONTAINER(mVssRays);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | Vector3
|
---|
60 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
61 | {
|
---|
62 | AxisAlignedBox3 box;
|
---|
63 |
|
---|
64 | if (viewSpaceBox)
|
---|
65 | box =*viewSpaceBox;
|
---|
66 | else
|
---|
67 | box = mKdTree->GetBox();
|
---|
68 |
|
---|
69 | // shrink the box in the y direction
|
---|
70 | return box.GetRandomPoint();
|
---|
71 | }
|
---|
72 |
|
---|
73 | Vector3
|
---|
74 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
75 | AxisAlignedBox3 *viewSpaceBox
|
---|
76 | )
|
---|
77 | {
|
---|
78 | Vector3 point;
|
---|
79 | if (!use2dSampling)
|
---|
80 | {
|
---|
81 | if (0)
|
---|
82 | {
|
---|
83 | Vector3 normal;
|
---|
84 | int i = Random((int)mObjects.size());
|
---|
85 | Intersectable *object = mObjects[i];
|
---|
86 | object->GetRandomSurfacePoint(point, normal);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
90 | // point = viewpoint + UniformRandomVector();
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | AxisAlignedBox3 box;
|
---|
95 |
|
---|
96 | if (viewSpaceBox)
|
---|
97 | box =*viewSpaceBox;
|
---|
98 | else
|
---|
99 | box = mKdTree->GetBox();
|
---|
100 |
|
---|
101 | point = box.GetRandomPoint();
|
---|
102 | point.y = viewpoint.y;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return point - viewpoint;
|
---|
106 | }
|
---|
107 |
|
---|
108 | int
|
---|
109 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
110 | const int desiredSamples,
|
---|
111 | SimpleRayContainer &rays
|
---|
112 | )
|
---|
113 | {
|
---|
114 | int num;
|
---|
115 | if (0) {
|
---|
116 | float minRayContribution;
|
---|
117 | float maxRayContribution;
|
---|
118 | float avgRayContribution;
|
---|
119 |
|
---|
120 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
121 | maxRayContribution,
|
---|
122 | avgRayContribution);
|
---|
123 |
|
---|
124 | cout<<
|
---|
125 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
126 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
127 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
128 |
|
---|
129 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
130 | num = vssTree->GenerateRays(p, rays);
|
---|
131 | } else {
|
---|
132 | int leaves = vssTree->stat.Leaves();
|
---|
133 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
134 | }
|
---|
135 |
|
---|
136 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
137 |
|
---|
138 | return num;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | bool
|
---|
143 | VssPreprocessor::ExportRays(const char *filename,
|
---|
144 | const VssRayContainer &vssRays,
|
---|
145 | const int number,
|
---|
146 | const bool exportScene
|
---|
147 | )
|
---|
148 | {
|
---|
149 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
150 |
|
---|
151 | Exporter *exporter = NULL;
|
---|
152 | exporter = Exporter::GetExporter(filename);
|
---|
153 | exporter->SetWireframe();
|
---|
154 | exporter->ExportKdTree(*mKdTree);
|
---|
155 | exporter->SetFilled();
|
---|
156 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
157 | exporter->SetWireframe();
|
---|
158 |
|
---|
159 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
160 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
161 | exporter->ResetForcedMaterial();
|
---|
162 |
|
---|
163 |
|
---|
164 | VssRayContainer rays;
|
---|
165 | vssRays.SelectRays(number, rays);
|
---|
166 |
|
---|
167 | //exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
168 |
|
---|
169 | delete exporter;
|
---|
170 |
|
---|
171 | cout<<"done."<<endl<<flush;
|
---|
172 |
|
---|
173 | return true;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | bool
|
---|
178 | VssPreprocessor::ExportVssTree(char *filename,
|
---|
179 | VssTree *tree,
|
---|
180 | const Vector3 &dir
|
---|
181 | )
|
---|
182 | {
|
---|
183 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
184 | exporter->SetFilled();
|
---|
185 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
186 | // exporter->SetWireframe();
|
---|
187 | bool result = exporter->ExportVssTree2( *tree, dir );
|
---|
188 | delete exporter;
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 |
|
---|
192 | bool
|
---|
193 | VssPreprocessor::ExportVssTreeLeaf(char *filename,
|
---|
194 | VssTree *tree,
|
---|
195 | VssTreeLeaf *leaf)
|
---|
196 | {
|
---|
197 | Exporter *exporter = NULL;
|
---|
198 | exporter = Exporter::GetExporter(filename);
|
---|
199 | exporter->SetWireframe();
|
---|
200 | exporter->ExportKdTree(*mKdTree);
|
---|
201 |
|
---|
202 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
203 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
204 | exporter->ResetForcedMaterial();
|
---|
205 |
|
---|
206 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
207 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
208 | exporter->ResetForcedMaterial();
|
---|
209 |
|
---|
210 | VssRayContainer rays[4];
|
---|
211 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
212 | int k = leaf->rays[i].GetRayClass();
|
---|
213 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
214 | }
|
---|
215 |
|
---|
216 | // SOURCE RAY
|
---|
217 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
218 | // TERMINATION RAY
|
---|
219 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
220 | // PASSING_RAY
|
---|
221 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
222 | // CONTAINED_RAY
|
---|
223 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
224 |
|
---|
225 | delete exporter;
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | void
|
---|
230 | VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
|
---|
231 | {
|
---|
232 | vector<VssTreeLeaf *> leaves;
|
---|
233 | tree->CollectLeaves(leaves);
|
---|
234 |
|
---|
235 | int num = 0;
|
---|
236 | int i;
|
---|
237 | float p = number / (float)leaves.size();
|
---|
238 | for (i=0; i < leaves.size(); i++) {
|
---|
239 | if (RandomValue(0,1) < p) {
|
---|
240 | char filename[64];
|
---|
241 | sprintf(filename, "vss-leaf-%04d.x3d", num);
|
---|
242 | ExportVssTreeLeaf(filename, tree, leaves[i]);
|
---|
243 | num++;
|
---|
244 | }
|
---|
245 | if (num >= number)
|
---|
246 | break;
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | void VssPreprocessor::TestBeamCasting(VssTree *tree,
|
---|
252 | ViewCellsManager *vm,
|
---|
253 | const ObjectContainer &objects)
|
---|
254 | {
|
---|
255 | //debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
256 | // renderer->resize(640, 480);
|
---|
257 | //debuggerWidget->resize(640, 480);
|
---|
258 |
|
---|
259 | vector<VssTreeLeaf *> leaves;
|
---|
260 | tree->CollectLeaves(leaves);
|
---|
261 |
|
---|
262 | Exporter *exporter = Exporter::GetExporter("shafts.x3d");
|
---|
263 |
|
---|
264 | exporter->SetWireframe();
|
---|
265 | exporter->ExportGeometry(objects);
|
---|
266 | exporter->SetFilled();
|
---|
267 | //Randomize();
|
---|
268 | // §§matt
|
---|
269 | // debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
270 |
|
---|
271 | /*debuggerWidget->mBeam = beam;
|
---|
272 | debuggerWidget->mSourceObject = sourceObj;
|
---|
273 | debuggerWidget->mSamples = 10000;
|
---|
274 |
|
---|
275 | Debug << "showing window" << endl;
|
---|
276 | debuggerWidget->show();
|
---|
277 |
|
---|
278 | renderer->makeCurrent();*/
|
---|
279 |
|
---|
280 | for (int i = 0; i < 10; ++ i)
|
---|
281 | {
|
---|
282 | Beam beam;
|
---|
283 | Intersectable *sourceObj = mObjects[5];
|
---|
284 |
|
---|
285 | const int index = (int)RandomValue(0, (Real)((float)leaves.size() - 0.5f));
|
---|
286 | VssTreeLeaf *leaf = leaves[index];
|
---|
287 |
|
---|
288 | AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
|
---|
289 | AxisAlignedBox3 box = tree->GetBBox(leaf);
|
---|
290 |
|
---|
291 | beam.Construct(box, dirBox);
|
---|
292 |
|
---|
293 | // collect kd leaves and view cells
|
---|
294 | mKdTree->CastBeam(beam);
|
---|
295 | vm->CastBeam(beam);
|
---|
296 |
|
---|
297 | Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
|
---|
298 | << (int)beam.mKdNodes.size() << " kd nodes" << endl;
|
---|
299 |
|
---|
300 | BeamSampleStatistics stats;
|
---|
301 | // §§matt
|
---|
302 | /* renderer->SampleBeamContributions(sourceObj,
|
---|
303 | beam,
|
---|
304 | 200000,
|
---|
305 | stats);
|
---|
306 |
|
---|
307 | char s[64]; sprintf(s, "shaft%04d.png", i);
|
---|
308 |
|
---|
309 | QImage image = renderer->toImage();
|
---|
310 | image.save(s, "PNG");
|
---|
311 | Debug << "beam statistics: " << stats << endl << endl;
|
---|
312 | */
|
---|
313 | if (1)
|
---|
314 | {
|
---|
315 | AxisAlignedBox3 sbox = mSceneGraph->GetBox();
|
---|
316 | Vector3 bmin = sbox.Min() - 150.0f;
|
---|
317 | Vector3 bmax = sbox.Max() + 150.0f;
|
---|
318 | AxisAlignedBox3 vbox(bmin, bmax);
|
---|
319 |
|
---|
320 | exporter->ExportBeam(beam, vbox);
|
---|
321 | }
|
---|
322 |
|
---|
323 | bool exportViewCells = false;
|
---|
324 | if (exportViewCells)
|
---|
325 | {
|
---|
326 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
327 |
|
---|
328 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
329 | {
|
---|
330 | BspNodeGeometry geom;
|
---|
331 | AxisAlignedBox3 vbox;
|
---|
332 | vbox.Initialize();
|
---|
333 | vbox.Include((*it)->GetMesh());
|
---|
334 |
|
---|
335 | exporter->SetWireframe();
|
---|
336 | exporter->ExportBox(vbox);
|
---|
337 | exporter->SetFilled();
|
---|
338 | exporter->ExportViewCell(*it);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
342 |
|
---|
343 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
344 | {
|
---|
345 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
346 | }*/
|
---|
347 | }
|
---|
348 | }
|
---|
349 | /*while (1)
|
---|
350 | { debuggerWidget->repaint();
|
---|
351 | };*/
|
---|
352 | delete exporter;
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | float
|
---|
357 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
358 | const vector<AxisAlignedBox3> &viewcells
|
---|
359 | )
|
---|
360 | {
|
---|
361 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
362 |
|
---|
363 | int sum = 0;
|
---|
364 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
365 | sum += tree->GetPvsSize(*it);
|
---|
366 |
|
---|
367 | return sum/(float)viewcells.size();
|
---|
368 | }
|
---|
369 |
|
---|
370 | bool
|
---|
371 | VssPreprocessor::ComputeVisibility()
|
---|
372 | {
|
---|
373 | Debug << "type: vss" << endl;
|
---|
374 |
|
---|
375 | const long startTime = GetTime();
|
---|
376 | int totalSamples = 0;
|
---|
377 |
|
---|
378 | //mSceneGraph->CollectObjects(&mObjects);
|
---|
379 |
|
---|
380 | if (!mLoadViewCells)
|
---|
381 | {
|
---|
382 | //-- generate new view cells from the scratch
|
---|
383 | //-- for construction the manager uses it's own set of samples
|
---|
384 | ConstructViewCells();
|
---|
385 | }
|
---|
386 | #if 0
|
---|
387 | else
|
---|
388 | {
|
---|
389 | //-- load view cells from file
|
---|
390 | //-- test successful view cells loading by exporting them again
|
---|
391 | VssRayContainer dummies;
|
---|
392 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
393 | mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
394 | }
|
---|
395 | #endif
|
---|
396 |
|
---|
397 | VssTree *vssTree = NULL;
|
---|
398 | const long initialTime = GetTime();
|
---|
399 |
|
---|
400 | if (mLoadInitialSamples)
|
---|
401 | {
|
---|
402 | cout << "Loading samples from file ... ";
|
---|
403 | LoadSamples(mVssRays, mObjects);
|
---|
404 | cout << "finished\n" << endl;
|
---|
405 | totalSamples = (int)mVssRays.size();
|
---|
406 | }
|
---|
407 | else
|
---|
408 | {
|
---|
409 | while (totalSamples < mInitialSamples)
|
---|
410 | {
|
---|
411 | int passContributingSamples = 0;
|
---|
412 | int passSampleContributions = 0;
|
---|
413 | int passSamples = 0;
|
---|
414 |
|
---|
415 | int index = 0;
|
---|
416 |
|
---|
417 | int sampleContributions;
|
---|
418 |
|
---|
419 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
420 | for (int k=0; k < s; k++)
|
---|
421 | {
|
---|
422 | Vector3 viewpoint;
|
---|
423 |
|
---|
424 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
425 | AxisAlignedBox3 vspBox = mViewCellsManager->GetViewSpaceBox();
|
---|
426 | const Vector3 direction = GetDirection(viewpoint, &vspBox);
|
---|
427 |
|
---|
428 | const SimpleRay sray(viewpoint, direction,
|
---|
429 | SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION,
|
---|
430 | 1.0f);
|
---|
431 | sampleContributions = mRayCaster->CastRay(sray,
|
---|
432 | mVssRays,
|
---|
433 | mViewCellsManager->GetViewSpaceBox(),
|
---|
434 | true);
|
---|
435 |
|
---|
436 | if (sampleContributions) {
|
---|
437 | passContributingSamples ++;
|
---|
438 | passSampleContributions += sampleContributions;
|
---|
439 | }
|
---|
440 | passSamples++;
|
---|
441 | totalSamples++;
|
---|
442 | }
|
---|
443 |
|
---|
444 | mPass++;
|
---|
445 | int pvsSize = 0;
|
---|
446 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
447 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
448 |
|
---|
449 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
450 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
451 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
452 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
453 | << pvsSize/(float)mObjects.size() << endl
|
---|
454 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
455 |
|
---|
456 | mStats <<
|
---|
457 | "#Pass\n" <<mPass<<endl<<
|
---|
458 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
459 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
460 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
461 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
462 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
463 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
469 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
470 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
471 |
|
---|
472 | if (mStoreInitialSamples)
|
---|
473 | {
|
---|
474 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
475 | ExportSamples(mVssRays);
|
---|
476 | cout << "finished\n" << endl;
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | //int numExportRays = 2000;
|
---|
481 | int numExportRays = 0;
|
---|
482 |
|
---|
483 | if (numExportRays) {
|
---|
484 | char filename[64];
|
---|
485 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
486 | ExportRays(filename, mVssRays, numExportRays);
|
---|
487 | }
|
---|
488 |
|
---|
489 | vssTree = new VssTree;
|
---|
490 | // viewcells = Construct(mVssRays);
|
---|
491 |
|
---|
492 | vssTree->Construct(mVssRays, NULL);
|
---|
493 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
494 |
|
---|
495 | if (0) ExportRays("kdtree.x3d", mVssRays, 10);
|
---|
496 |
|
---|
497 | if (0)
|
---|
498 | {
|
---|
499 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
500 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
501 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
502 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
503 | ExportVssTreeLeaves(vssTree, 10);
|
---|
504 | }
|
---|
505 |
|
---|
506 | // viewcells->UpdatePVS(newVssRays);
|
---|
507 | // get viewcells as kd tree boxes
|
---|
508 | vector<AxisAlignedBox3> kdViewcells;
|
---|
509 | if (0)
|
---|
510 | {
|
---|
511 | vector<KdLeaf *> leaves;
|
---|
512 | mKdTree->CollectLeaves(leaves);
|
---|
513 | vector<KdLeaf *>::const_iterator it;
|
---|
514 | int targetLeaves = 50;
|
---|
515 | float prob = targetLeaves/(float)leaves.size();
|
---|
516 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
517 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
518 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
519 |
|
---|
520 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
521 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | int samples = 0;
|
---|
526 | int pass = 0;
|
---|
527 |
|
---|
528 |
|
---|
529 | // cast view cell samples
|
---|
530 | while (samples < mVssSamples)
|
---|
531 | {
|
---|
532 | int num = mVssSamplesPerPass;
|
---|
533 | SimpleRayContainer rays;
|
---|
534 | VssRayContainer vssRays;
|
---|
535 |
|
---|
536 | if (!mUseImportanceSampling)
|
---|
537 | {
|
---|
538 | for (int j=0; j < num; j++) {
|
---|
539 | Vector3 viewpoint;
|
---|
540 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
541 | Vector3 direction = GetDirection(viewpoint, NULL);
|
---|
542 | rays.push_back(SimpleRay(viewpoint, direction,
|
---|
543 | SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION,
|
---|
544 | 1.0f)
|
---|
545 | );
|
---|
546 | }
|
---|
547 | }
|
---|
548 | else
|
---|
549 | {
|
---|
550 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
551 | }
|
---|
552 |
|
---|
553 | CastRays(rays, vssRays, true);
|
---|
554 | vssTree->AddRays(vssRays);
|
---|
555 |
|
---|
556 | if (0)
|
---|
557 | {
|
---|
558 | int subdivided = vssTree->UpdateSubdivision();
|
---|
559 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
560 | }
|
---|
561 |
|
---|
562 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
563 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
564 |
|
---|
565 | /// compute view cell contribution of rays
|
---|
566 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
567 |
|
---|
568 | if (numExportRays) {
|
---|
569 | char filename[64];
|
---|
570 | if (mUseImportanceSampling)
|
---|
571 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
572 | else
|
---|
573 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
574 |
|
---|
575 | ExportRays(filename, vssRays, numExportRays);
|
---|
576 | }
|
---|
577 |
|
---|
578 | samples += num;
|
---|
579 |
|
---|
580 | float pvs = vssTree->GetAvgPvsSize();
|
---|
581 | cout<<"*****************************\n";
|
---|
582 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
583 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
584 | cout<<"*****************************\n";
|
---|
585 | // if (samples >= mVssSamples) break;
|
---|
586 | pass ++;
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
590 | {
|
---|
591 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
592 | }
|
---|
593 |
|
---|
594 | if (0) Debug << vssTree->stat << endl;
|
---|
595 |
|
---|
596 | if (0)
|
---|
597 | {
|
---|
598 | VssRayContainer viewCellRays;
|
---|
599 | // compute rays used for view cells construction
|
---|
600 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
601 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
602 | }
|
---|
603 |
|
---|
604 |
|
---|
605 | ////////////////////
|
---|
606 | //-- render simulation after construction
|
---|
607 |
|
---|
608 | mRenderSimulator->RenderScene();
|
---|
609 | SimulationStatistics ss;
|
---|
610 | mRenderSimulator->GetStatistics(ss);
|
---|
611 | Debug << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
612 | cout << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
613 |
|
---|
614 | delete vssTree;
|
---|
615 |
|
---|
616 | return true;
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 | void VssPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
621 | {
|
---|
622 | // store higher order object
|
---|
623 | mViewCellsManager->DeterminePvsObjects(rays, !mViewCellsManager->ViewCellsConstructed());
|
---|
624 | }
|
---|
625 |
|
---|
626 |
|
---|
627 | }
|
---|