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