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 | if (exportViewCells)
|
---|
334 | {
|
---|
335 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
336 |
|
---|
337 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
338 | {
|
---|
339 | BspNodeGeometry geom;
|
---|
340 | AxisAlignedBox3 vbox;
|
---|
341 | vbox.Initialize();
|
---|
342 | vbox.Include((*it)->GetMesh());
|
---|
343 |
|
---|
344 | exporter->SetWireframe();
|
---|
345 | exporter->ExportBox(vbox);
|
---|
346 | exporter->SetFilled();
|
---|
347 | exporter->ExportViewCell(*it);
|
---|
348 | }
|
---|
349 |
|
---|
350 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
351 |
|
---|
352 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
353 | {
|
---|
354 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
355 | }*/
|
---|
356 | }
|
---|
357 | }
|
---|
358 | /*while (1)
|
---|
359 | { debuggerWidget->repaint();
|
---|
360 | };*/
|
---|
361 | delete exporter;
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | float
|
---|
366 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
367 | const vector<AxisAlignedBox3> &viewcells
|
---|
368 | )
|
---|
369 | {
|
---|
370 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
371 |
|
---|
372 | int sum = 0;
|
---|
373 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
374 | sum += tree->GetPvsSize(*it);
|
---|
375 |
|
---|
376 | return sum/(float)viewcells.size();
|
---|
377 | }
|
---|
378 |
|
---|
379 | bool
|
---|
380 | VssPreprocessor::ComputeVisibility()
|
---|
381 | {
|
---|
382 | Debug << "type: vss" << endl;
|
---|
383 |
|
---|
384 | long startTime = GetTime();
|
---|
385 |
|
---|
386 | int totalSamples = 0;
|
---|
387 |
|
---|
388 |
|
---|
389 | AxisAlignedBox3 box(mKdTree->GetBox());
|
---|
390 |
|
---|
391 | if (!useViewspacePlane) {
|
---|
392 | float size = 0.05f;
|
---|
393 | float s = 0.5f - size;
|
---|
394 | float olds = Magnitude(box.Size());
|
---|
395 | box.Enlarge(box.Size()*Vector3(-s));
|
---|
396 | Vector3 translation = Vector3(-olds*0.1f, 0, 0);
|
---|
397 | box.SetMin(box.Min() + translation);
|
---|
398 | box.SetMax(box.Max() + translation);
|
---|
399 | } else {
|
---|
400 |
|
---|
401 | // sample city like heights
|
---|
402 | box.SetMin(1, box.Min(1) + box.Size(1)*0.2f);
|
---|
403 | box.SetMax(1, box.Min(1) + box.Size(1)*0.3f);
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (use2dSampling)
|
---|
407 | box.SetMax(1, box.Min(1));
|
---|
408 |
|
---|
409 | cout << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
410 |
|
---|
411 |
|
---|
412 | if (mUseViewSpaceBox)
|
---|
413 | {
|
---|
414 | //mViewSpaceBox = ConstructViewSpaceBox();
|
---|
415 |
|
---|
416 | if (!mEnlargeViewSpace)
|
---|
417 | {
|
---|
418 | mViewSpaceBox = new AxisAlignedBox3(box);
|
---|
419 | }
|
---|
420 | else
|
---|
421 | {
|
---|
422 | mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
423 |
|
---|
424 | if (0)
|
---|
425 | {
|
---|
426 | // HACK: enlarge in y directon
|
---|
427 | Vector3 size = mViewSpaceBox->Size();
|
---|
428 |
|
---|
429 | size[1] *= 1.25;
|
---|
430 | Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
|
---|
431 | //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
|
---|
432 |
|
---|
433 | mViewSpaceBox->Enlarge(enlarge);
|
---|
434 | mViewSpaceBox->SetMax(mViewSpaceBox->Max() + enlarge);
|
---|
435 | }
|
---|
436 | else
|
---|
437 | {
|
---|
438 | AxisAlignedBox3 tbox(*mViewSpaceBox);
|
---|
439 |
|
---|
440 | Vector3 size = mViewSpaceBox->Size();
|
---|
441 | tbox.SetMax(0, mViewSpaceBox->Max(0) + size[0] * 0.5f);
|
---|
442 | tbox.SetMin(0, mViewSpaceBox->Min(0) + size[0]);
|
---|
443 | *mViewSpaceBox = tbox;
|
---|
444 |
|
---|
445 | // $$ JB temporary
|
---|
446 | /*AxisAlignedBox3 tempbox = *mViewSpaceBox;
|
---|
447 |
|
---|
448 | float s = tempbox.Size(0);
|
---|
449 |
|
---|
450 | tempbox.Scale(0.8f);
|
---|
451 | tempbox.SetMax(0, box.Max(0) + s*0.8f);
|
---|
452 | tempbox.SetMin(0, box.Min(0) + s*0.8f);
|
---|
453 | *mViewSpaceBox = tempbox;*/
|
---|
454 | }
|
---|
455 | }
|
---|
456 | //Debug << "view space box: " << *mViewSpaceBox << endl;
|
---|
457 | }
|
---|
458 | else
|
---|
459 | {
|
---|
460 | mViewSpaceBox = NULL;
|
---|
461 | }
|
---|
462 |
|
---|
463 | AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
|
---|
464 |
|
---|
465 | mSceneGraph->CollectObjects(&mObjects);
|
---|
466 |
|
---|
467 | if (!mLoadViewCells)
|
---|
468 | {
|
---|
469 | //-- generate new view cells or reconstruct them
|
---|
470 | //-- for construction the manager uses it's own set of samples
|
---|
471 | mViewCellsManager->SetViewSpaceBox(vbox);
|
---|
472 | mViewCellsManager->Construct(this);
|
---|
473 |
|
---|
474 | // output visualizations and statistics
|
---|
475 | Debug << "view cells construction finished: " << endl;
|
---|
476 | mViewCellsManager->PrintStatistics(Debug);
|
---|
477 | }
|
---|
478 | #if 0
|
---|
479 | else
|
---|
480 | {
|
---|
481 | //-- load view cells from file
|
---|
482 | //-- test successful view cells loading by exporting them again
|
---|
483 | VssRayContainer dummies;
|
---|
484 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
485 | mViewCellsManager->ExportViewCells("test.xml.zip", mViewCellsManager->GetExportPvs(), mObjects);
|
---|
486 | }
|
---|
487 | #endif
|
---|
488 |
|
---|
489 | VssTree *vssTree = NULL;
|
---|
490 | const long initialTime = GetTime();
|
---|
491 |
|
---|
492 | if (mLoadInitialSamples)
|
---|
493 | {
|
---|
494 | cout << "Loading samples from file ... ";
|
---|
495 | LoadSamples(mVssRays, mObjects);
|
---|
496 | cout << "finished\n" << endl;
|
---|
497 | totalSamples = (int)mVssRays.size();
|
---|
498 | }
|
---|
499 | else
|
---|
500 | {
|
---|
501 | while (totalSamples < mInitialSamples) {
|
---|
502 | int passContributingSamples = 0;
|
---|
503 | int passSampleContributions = 0;
|
---|
504 | int passSamples = 0;
|
---|
505 |
|
---|
506 | int index = 0;
|
---|
507 |
|
---|
508 | int sampleContributions;
|
---|
509 |
|
---|
510 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
511 | for (int k=0; k < s; k++) {
|
---|
512 | // changed by matt
|
---|
513 | Vector3 viewpoint;
|
---|
514 | // viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
515 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
516 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
517 |
|
---|
518 | sampleContributions = CastRay(viewpoint, direction, 1, mVssRays, vbox);
|
---|
519 |
|
---|
520 | if (sampleContributions) {
|
---|
521 | passContributingSamples ++;
|
---|
522 | passSampleContributions += sampleContributions;
|
---|
523 | }
|
---|
524 | passSamples++;
|
---|
525 | totalSamples++;
|
---|
526 | }
|
---|
527 |
|
---|
528 | mPass++;
|
---|
529 | int pvsSize = 0;
|
---|
530 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
531 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
532 |
|
---|
533 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
534 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
535 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
536 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
537 | << pvsSize/(float)mObjects.size() << endl
|
---|
538 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
539 |
|
---|
540 | mStats <<
|
---|
541 | "#Pass\n" <<mPass<<endl<<
|
---|
542 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
543 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
544 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
545 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
546 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
547 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
548 | }
|
---|
549 |
|
---|
550 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
551 |
|
---|
552 |
|
---|
553 |
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
558 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
559 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
560 |
|
---|
561 | if (mStoreInitialSamples)
|
---|
562 | {
|
---|
563 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
564 | ExportSamples(mVssRays);
|
---|
565 | cout << "finished\n" << endl;
|
---|
566 |
|
---|
567 | /*VssRayContainer dummyRays;
|
---|
568 | LoadSamples(dummyRays, mObjects);
|
---|
569 | Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
|
---|
570 |
|
---|
571 | for (int i = 0; i < (int)mVssRays.size(); ++ i)
|
---|
572 | {
|
---|
573 | Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
|
---|
574 | Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
|
---|
575 | }*/
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | //int numExportRays = 2000;
|
---|
580 | int numExportRays = 0;
|
---|
581 |
|
---|
582 | if (numExportRays) {
|
---|
583 | char filename[64];
|
---|
584 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
585 | ExportRays(filename, mVssRays, numExportRays);
|
---|
586 | }
|
---|
587 |
|
---|
588 | vssTree = new VssTree;
|
---|
589 | // viewcells = Construct(mVssRays);
|
---|
590 |
|
---|
591 | vssTree->Construct(mVssRays, mViewSpaceBox);
|
---|
592 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
593 |
|
---|
594 | if (0) ExportRays("kdtree.x3d", mVssRays, 10);
|
---|
595 |
|
---|
596 | if (0)
|
---|
597 | {
|
---|
598 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
599 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
600 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
601 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
602 | ExportVssTreeLeaves(vssTree, 10);
|
---|
603 | }
|
---|
604 |
|
---|
605 | // viewcells->UpdatePVS(newVssRays);
|
---|
606 | // get viewcells as kd tree boxes
|
---|
607 | vector<AxisAlignedBox3> kdViewcells;
|
---|
608 | if (0) {
|
---|
609 | vector<KdLeaf *> leaves;
|
---|
610 | mKdTree->CollectLeaves(leaves);
|
---|
611 | vector<KdLeaf *>::const_iterator it;
|
---|
612 | int targetLeaves = 50;
|
---|
613 | float prob = targetLeaves/(float)leaves.size();
|
---|
614 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
615 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
616 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
617 |
|
---|
618 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
619 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
620 | }
|
---|
621 |
|
---|
622 |
|
---|
623 | int samples = 0;
|
---|
624 | int pass = 0;
|
---|
625 |
|
---|
626 |
|
---|
627 | // cast view cell samples
|
---|
628 | while (samples < mVssSamples)
|
---|
629 | {
|
---|
630 |
|
---|
631 | int num = mVssSamplesPerPass;
|
---|
632 | SimpleRayContainer rays;
|
---|
633 | VssRayContainer vssRays;
|
---|
634 |
|
---|
635 | if (!mUseImportanceSampling) {
|
---|
636 | for (int j=0; j < num; j++) {
|
---|
637 | // changed by matt
|
---|
638 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
639 | Vector3 viewpoint;
|
---|
640 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
641 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
642 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
643 | }
|
---|
644 | } else {
|
---|
645 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
646 | }
|
---|
647 |
|
---|
648 | for (int i=0; i < rays.size(); i++)
|
---|
649 | CastRay(rays[i].mOrigin, rays[i].mDirection, 1, vssRays, vbox);
|
---|
650 |
|
---|
651 | vssTree->AddRays(vssRays);
|
---|
652 |
|
---|
653 | if (0) {
|
---|
654 | int subdivided = vssTree->UpdateSubdivision();
|
---|
655 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
656 | }
|
---|
657 |
|
---|
658 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
659 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
660 |
|
---|
661 | /// compute view cell contribution of rays
|
---|
662 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
663 |
|
---|
664 | if (numExportRays) {
|
---|
665 | char filename[64];
|
---|
666 | if (mUseImportanceSampling)
|
---|
667 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
668 | else
|
---|
669 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
670 |
|
---|
671 | ExportRays(filename, vssRays, numExportRays);
|
---|
672 | }
|
---|
673 |
|
---|
674 | samples+=num;
|
---|
675 | float pvs = vssTree->GetAvgPvsSize();
|
---|
676 | cout<<"*****************************\n";
|
---|
677 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
678 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
679 | cout<<"*****************************\n";
|
---|
680 | // if (samples >= mVssSamples) break;
|
---|
681 | pass ++;
|
---|
682 | }
|
---|
683 |
|
---|
684 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
685 | {
|
---|
686 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
687 | }
|
---|
688 |
|
---|
689 | if (0) Debug << vssTree->stat << endl;
|
---|
690 |
|
---|
691 | if (0)
|
---|
692 | {
|
---|
693 | VssRayContainer viewCellRays;
|
---|
694 | // compute rays used for view cells construction
|
---|
695 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
696 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | //////////////////////////////////
|
---|
701 | //-- render simulation after
|
---|
702 |
|
---|
703 | mRenderSimulator->RenderScene();
|
---|
704 | SimulationStatistics ss;
|
---|
705 | mRenderSimulator->GetStatistics(ss);
|
---|
706 | Debug << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
707 | cout << "\nFinal view cells partition render time\n" << ss << endl;
|
---|
708 |
|
---|
709 | delete vssTree;
|
---|
710 |
|
---|
711 | return true;
|
---|
712 | }
|
---|
713 |
|
---|
714 | } |
---|