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