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 |
|
---|
16 | bool use2dSampling = false;
|
---|
17 | bool useViewspacePlane = false;
|
---|
18 |
|
---|
19 | VssPreprocessor::VssPreprocessor():
|
---|
20 | mPass(0),
|
---|
21 | mVssRays()
|
---|
22 | {
|
---|
23 | // this should increase coherence of the samples
|
---|
24 | environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
25 | environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
26 | environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
27 | environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
28 | environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
29 | environment->GetBoolValue("ViewCells.delayedConstruction", mDelayedViewCellsConstruction);
|
---|
30 |
|
---|
31 | environment->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
32 | environment->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
33 | environment->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
34 | environment->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
|
---|
35 |
|
---|
36 | useViewspacePlane = mUseViewSpaceBox; //hack
|
---|
37 |
|
---|
38 | mStats.open("stats.log");
|
---|
39 | }
|
---|
40 |
|
---|
41 | VssPreprocessor::~VssPreprocessor()
|
---|
42 | {
|
---|
43 | CLEAR_CONTAINER(mVssRays);
|
---|
44 | }
|
---|
45 |
|
---|
46 | void
|
---|
47 | VssPreprocessor::SetupRay(Ray &ray,
|
---|
48 | const Vector3 &point,
|
---|
49 | const Vector3 &direction
|
---|
50 | )
|
---|
51 | {
|
---|
52 | ray.Clear();
|
---|
53 | // do not store anything else then intersections at the ray
|
---|
54 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
55 | }
|
---|
56 |
|
---|
57 | int
|
---|
58 | VssPreprocessor::CastRay(
|
---|
59 | Vector3 &viewPoint,
|
---|
60 | Vector3 &direction,
|
---|
61 | VssRayContainer &vssRays
|
---|
62 | )
|
---|
63 | {
|
---|
64 |
|
---|
65 | int hits = 0;
|
---|
66 | static Ray ray;
|
---|
67 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
68 |
|
---|
69 | AxisAlignedBox3 sbox = box;
|
---|
70 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
71 | if (!sbox.IsInside(viewPoint))
|
---|
72 | return 0;
|
---|
73 |
|
---|
74 | SetupRay(ray, viewPoint, direction);
|
---|
75 | // cast ray to KD tree to find intersection with other objects
|
---|
76 | Intersectable *objectA, *objectB;
|
---|
77 | Vector3 pointA, pointB;
|
---|
78 | float bsize = Magnitude(box.Size());
|
---|
79 |
|
---|
80 |
|
---|
81 | if (!mDetectEmptyViewSpace) |
---|
82 | ray.mFlags &= ~Ray::CULL_BACKFACES; |
---|
83 | else |
---|
84 | ray.mFlags |= Ray::CULL_BACKFACES; |
---|
85 | |
---|
86 | if (mKdTree->CastRay(ray)) {
|
---|
87 | objectA = ray.intersections[0].mObject;
|
---|
88 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
89 | } else {
|
---|
90 | objectA = NULL;
|
---|
91 | // compute intersection with the scene bounding box
|
---|
92 | float tmin, tmax;
|
---|
93 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
94 | pointA = ray.Extrap(tmax);
|
---|
95 | else
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | if (mDetectEmptyViewSpace) {
|
---|
101 | SetupRay(ray, pointA, -direction);
|
---|
102 | } else
|
---|
103 | SetupRay(ray, viewPoint, -direction);
|
---|
104 |
|
---|
105 | if (!mDetectEmptyViewSpace)
|
---|
106 | ray.mFlags &= ~Ray::CULL_BACKFACES; |
---|
107 | else |
---|
108 | ray.mFlags |= Ray::CULL_BACKFACES; |
---|
109 | |
---|
110 | if (mKdTree->CastRay(ray)) {
|
---|
111 | objectB = ray.intersections[0].mObject;
|
---|
112 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
113 | } else {
|
---|
114 | objectB = NULL;
|
---|
115 | float tmin, tmax;
|
---|
116 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
117 | pointB = ray.Extrap(tmax);
|
---|
118 | else
|
---|
119 | return 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // if (objectA == NULL && objectB != NULL) {
|
---|
123 | if (mDetectEmptyViewSpace) {
|
---|
124 | // cast again to ensure that there is no objectA
|
---|
125 | SetupRay(ray, pointB, direction);
|
---|
126 | ray.mFlags |= Ray::CULL_BACKFACES; |
---|
127 | if (mKdTree->CastRay(ray)) {
|
---|
128 | objectA = ray.intersections[0].mObject;
|
---|
129 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | VssRay *vssRay = NULL;
|
---|
135 |
|
---|
136 | bool validSample = (objectA != objectB);
|
---|
137 | if (0 && mDetectEmptyViewSpace) { // consider all samples valid
|
---|
138 | // check if the viewpoint lies on the line segment AB
|
---|
139 | if (Distance(pointA, pointB) <
|
---|
140 | Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small) {
|
---|
141 | validSample = false;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (validSample) {
|
---|
146 | if (objectA) {
|
---|
147 | vssRay = new VssRay(pointB,
|
---|
148 | pointA,
|
---|
149 | objectB,
|
---|
150 | objectA,
|
---|
151 | mPass
|
---|
152 | );
|
---|
153 | vssRays.push_back(vssRay);
|
---|
154 | hits ++;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (objectB) {
|
---|
158 | vssRay = new VssRay(pointA,
|
---|
159 | pointB,
|
---|
160 | objectA,
|
---|
161 | objectB,
|
---|
162 | mPass
|
---|
163 | );
|
---|
164 | vssRays.push_back(vssRay);
|
---|
165 | hits ++;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | return hits;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | Vector3
|
---|
174 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
175 | {
|
---|
176 | AxisAlignedBox3 box;
|
---|
177 |
|
---|
178 | if (viewSpaceBox)
|
---|
179 | box =*viewSpaceBox;
|
---|
180 | else
|
---|
181 | box = mKdTree->GetBox();
|
---|
182 |
|
---|
183 | // shrink the box in the y direction
|
---|
184 | return box.GetRandomPoint();
|
---|
185 | }
|
---|
186 |
|
---|
187 | Vector3
|
---|
188 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
189 | AxisAlignedBox3 *viewSpaceBox
|
---|
190 | )
|
---|
191 | {
|
---|
192 | Vector3 point;
|
---|
193 | if (!use2dSampling) {
|
---|
194 | if (0) {
|
---|
195 | Vector3 normal;
|
---|
196 | int i = Random((int)mObjects.size());
|
---|
197 | Intersectable *object = mObjects[i];
|
---|
198 | object->GetRandomSurfacePoint(point, normal);
|
---|
199 | } else
|
---|
200 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
201 | // point = viewpoint + UniformRandomVector();
|
---|
202 |
|
---|
203 | } else {
|
---|
204 | AxisAlignedBox3 box;
|
---|
205 |
|
---|
206 | if (viewSpaceBox)
|
---|
207 | box =*viewSpaceBox;
|
---|
208 | else
|
---|
209 | box = mKdTree->GetBox();
|
---|
210 |
|
---|
211 | point = box.GetRandomPoint();
|
---|
212 | point.y = viewpoint.y;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return point - viewpoint;
|
---|
216 | }
|
---|
217 |
|
---|
218 | int
|
---|
219 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
220 | const int desiredSamples,
|
---|
221 | SimpleRayContainer &rays
|
---|
222 | )
|
---|
223 | {
|
---|
224 | int num;
|
---|
225 | if (0) {
|
---|
226 | float minRayContribution;
|
---|
227 | float maxRayContribution;
|
---|
228 | float avgRayContribution;
|
---|
229 |
|
---|
230 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
231 | maxRayContribution,
|
---|
232 | avgRayContribution);
|
---|
233 |
|
---|
234 | cout<<
|
---|
235 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
236 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
237 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
238 |
|
---|
239 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
240 | num = vssTree->GenerateRays(p, rays);
|
---|
241 | } else {
|
---|
242 | int leaves = vssTree->stat.Leaves();
|
---|
243 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
244 | }
|
---|
245 |
|
---|
246 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
247 |
|
---|
248 | return num;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | bool
|
---|
253 | VssPreprocessor::ExportRays(const char *filename,
|
---|
254 | const VssRayContainer &vssRays,
|
---|
255 | const int number
|
---|
256 | )
|
---|
257 | {
|
---|
258 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
259 |
|
---|
260 | Exporter *exporter = NULL;
|
---|
261 | exporter = Exporter::GetExporter(filename);
|
---|
262 | // exporter->SetWireframe();
|
---|
263 | // exporter->ExportKdTree(*mKdTree);
|
---|
264 | exporter->SetFilled();
|
---|
265 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
266 | exporter->SetWireframe();
|
---|
267 |
|
---|
268 | if (mViewSpaceBox) {
|
---|
269 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
270 | exporter->ExportBox(*mViewSpaceBox);
|
---|
271 | exporter->ResetForcedMaterial();
|
---|
272 | }
|
---|
273 |
|
---|
274 | VssRayContainer rays;
|
---|
275 | vssRays.SelectRays(number, rays);
|
---|
276 |
|
---|
277 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
278 |
|
---|
279 | delete exporter;
|
---|
280 |
|
---|
281 | cout<<"done."<<endl<<flush;
|
---|
282 |
|
---|
283 | return true;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | bool
|
---|
288 | VssPreprocessor::ExportVssTree(char *filename,
|
---|
289 | VssTree *tree,
|
---|
290 | const Vector3 &dir
|
---|
291 | )
|
---|
292 | {
|
---|
293 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
294 | exporter->SetFilled();
|
---|
295 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
296 | // exporter->SetWireframe();
|
---|
297 | bool result = exporter->ExportVssTree2( *tree, dir );
|
---|
298 | delete exporter;
|
---|
299 | return result;
|
---|
300 | }
|
---|
301 |
|
---|
302 | bool
|
---|
303 | VssPreprocessor::ExportVssTreeLeaf(char *filename,
|
---|
304 | VssTree *tree,
|
---|
305 | VssTreeLeaf *leaf)
|
---|
306 | {
|
---|
307 | Exporter *exporter = NULL;
|
---|
308 | exporter = Exporter::GetExporter(filename);
|
---|
309 | exporter->SetWireframe();
|
---|
310 | exporter->ExportKdTree(*mKdTree);
|
---|
311 |
|
---|
312 | if (mViewSpaceBox) {
|
---|
313 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
314 | exporter->ExportBox(*mViewSpaceBox);
|
---|
315 | exporter->ResetForcedMaterial();
|
---|
316 | }
|
---|
317 |
|
---|
318 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
319 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
320 | exporter->ResetForcedMaterial();
|
---|
321 |
|
---|
322 | VssRayContainer rays[4];
|
---|
323 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
324 | int k = leaf->rays[i].GetRayClass();
|
---|
325 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
326 | }
|
---|
327 |
|
---|
328 | // SOURCE RAY
|
---|
329 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
330 | // TERMINATION RAY
|
---|
331 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
332 | // PASSING_RAY
|
---|
333 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
334 | // CONTAINED_RAY
|
---|
335 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
336 |
|
---|
337 | delete exporter;
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 |
|
---|
341 | void
|
---|
342 | VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
|
---|
343 | {
|
---|
344 | vector<VssTreeLeaf *> leaves;
|
---|
345 | tree->CollectLeaves(leaves);
|
---|
346 |
|
---|
347 | int num = 0;
|
---|
348 | int i;
|
---|
349 | float p = number / (float)leaves.size();
|
---|
350 | for (i=0; i < leaves.size(); i++) {
|
---|
351 | if (RandomValue(0,1) < p) {
|
---|
352 | char filename[64];
|
---|
353 | sprintf(filename, "vss-leaf-%04d.x3d", num);
|
---|
354 | ExportVssTreeLeaf(filename, tree, leaves[i]);
|
---|
355 | num++;
|
---|
356 | }
|
---|
357 | if (num >= number)
|
---|
358 | break;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | #include "ViewCellBsp.h"
|
---|
362 |
|
---|
363 | void VssPreprocessor::TestBeamCasting(VssTree *tree,
|
---|
364 | ViewCellsManager *vm,
|
---|
365 | const ObjectContainer &objects)
|
---|
366 | {
|
---|
367 | //debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
368 | // renderer->resize(640, 480);
|
---|
369 | //debuggerWidget->resize(640, 480);
|
---|
370 |
|
---|
371 | vector<VssTreeLeaf *> leaves;
|
---|
372 | tree->CollectLeaves(leaves);
|
---|
373 |
|
---|
374 | Exporter *exporter = Exporter::GetExporter("shafts.x3d");
|
---|
375 |
|
---|
376 | exporter->SetWireframe();
|
---|
377 | exporter->ExportGeometry(objects);
|
---|
378 | exporter->SetFilled();
|
---|
379 | //Randomize();
|
---|
380 | debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
381 |
|
---|
382 | /*debuggerWidget->mBeam = beam;
|
---|
383 | debuggerWidget->mSourceObject = sourceObj;
|
---|
384 | debuggerWidget->mSamples = 10000;
|
---|
385 |
|
---|
386 | Debug << "showing window" << endl;
|
---|
387 | debuggerWidget->show();*/
|
---|
388 |
|
---|
389 | renderer->makeCurrent();
|
---|
390 |
|
---|
391 | for (int i = 0; i < 10; ++ i)
|
---|
392 | {
|
---|
393 | Beam beam;
|
---|
394 | Intersectable *sourceObj = mObjects[5];
|
---|
395 |
|
---|
396 | const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
|
---|
397 | VssTreeLeaf *leaf = leaves[index];
|
---|
398 |
|
---|
399 | AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
|
---|
400 | AxisAlignedBox3 box = tree->GetBBox(leaf);
|
---|
401 |
|
---|
402 | beam.Construct(box, dirBox);
|
---|
403 |
|
---|
404 | // collect kd leaves and view cells
|
---|
405 | mKdTree->CastBeam(beam);
|
---|
406 | vm->CastBeam(beam);
|
---|
407 |
|
---|
408 | Debug << "found " << beam.mViewCells.size() << " view cells and "
|
---|
409 | << beam.mKdNodes.size() << " kd nodes" << endl;
|
---|
410 |
|
---|
411 | BeamSampleStatistics stats;
|
---|
412 |
|
---|
413 | renderer->SampleBeamContributions(sourceObj, |
---|
414 | beam, |
---|
415 | 200000, |
---|
416 | stats);
|
---|
417 |
|
---|
418 | char s[64]; sprintf(s, "shaft%04d.png", i);
|
---|
419 |
|
---|
420 | QImage image = renderer->toImage();
|
---|
421 | image.save(s, "PNG");
|
---|
422 | Debug << "beam statistics: " << stats << endl << endl;
|
---|
423 |
|
---|
424 | if (1)
|
---|
425 | {
|
---|
426 | AxisAlignedBox3 sbox = mSceneGraph->GetBox();
|
---|
427 | Vector3 bmin = sbox.Min() - 150.0f;
|
---|
428 | Vector3 bmax = sbox.Max() + 150.0f;
|
---|
429 | AxisAlignedBox3 vbox(bmin, bmax);
|
---|
430 |
|
---|
431 | exporter->ExportBeam(beam, vbox);
|
---|
432 | }
|
---|
433 |
|
---|
434 | bool exportViewCells = false;
|
---|
435 |
|
---|
436 | if (exportViewCells)
|
---|
437 | {
|
---|
438 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
439 |
|
---|
440 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
441 | {
|
---|
442 | BspNodeGeometry geom;
|
---|
443 | AxisAlignedBox3 vbox;
|
---|
444 | vbox.Initialize();
|
---|
445 | vbox.Include((*it)->GetMesh());
|
---|
446 |
|
---|
447 | exporter->SetWireframe();
|
---|
448 | exporter->ExportBox(vbox);
|
---|
449 | exporter->SetFilled();
|
---|
450 | exporter->ExportViewCell(*it);
|
---|
451 | }
|
---|
452 |
|
---|
453 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
454 |
|
---|
455 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
456 | {
|
---|
457 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
458 | }*/
|
---|
459 | }
|
---|
460 | }
|
---|
461 | /*while (1)
|
---|
462 | { debuggerWidget->repaint();
|
---|
463 | };*/
|
---|
464 | delete exporter;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | float
|
---|
469 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
470 | const vector<AxisAlignedBox3> &viewcells
|
---|
471 | )
|
---|
472 | {
|
---|
473 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
474 |
|
---|
475 | int sum = 0;
|
---|
476 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
477 | sum += tree->GetPvsSize(*it);
|
---|
478 |
|
---|
479 | return sum/(float)viewcells.size();
|
---|
480 | }
|
---|
481 |
|
---|
482 | bool
|
---|
483 | VssPreprocessor::ComputeVisibility()
|
---|
484 | {
|
---|
485 |
|
---|
486 |
|
---|
487 | long startTime = GetTime();
|
---|
488 |
|
---|
489 | int totalSamples = 0;
|
---|
490 |
|
---|
491 |
|
---|
492 | AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
493 |
|
---|
494 | if (!useViewspacePlane) {
|
---|
495 | float size = 0.05f;
|
---|
496 | float s = 0.5f - size;
|
---|
497 | float olds = Magnitude(box->Size());
|
---|
498 | box->Enlarge(box->Size()*Vector3(-s));
|
---|
499 | Vector3 translation = Vector3(-olds*0.1f, 0, 0);
|
---|
500 | box->SetMin(box->Min() + translation);
|
---|
501 | box->SetMax(box->Max() + translation);
|
---|
502 | } else {
|
---|
503 |
|
---|
504 | // sample city like heights
|
---|
505 | box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
|
---|
506 | box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (use2dSampling)
|
---|
510 | box->SetMax(1, box->Min(1));
|
---|
511 |
|
---|
512 | cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
|
---|
513 | Debug << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
514 | if (mUseViewSpaceBox)
|
---|
515 | {
|
---|
516 | mViewSpaceBox = box;
|
---|
517 | mViewCellsManager->SetViewSpaceBox(*box);
|
---|
518 | }
|
---|
519 | else
|
---|
520 | {
|
---|
521 | mViewSpaceBox = NULL;
|
---|
522 | mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
|
---|
523 | }
|
---|
524 |
|
---|
525 | //-- load view cells from file if requested
|
---|
526 | if (mLoadViewCells)
|
---|
527 | {
|
---|
528 | // load now because otherwise bounding box not correct
|
---|
529 | mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | VssTree *vssTree = NULL;
|
---|
534 |
|
---|
535 | mSceneGraph->CollectObjects(&mObjects);
|
---|
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, mVssRays);
|
---|
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 | << "k #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 = 5000;
|
---|
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 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
637 | mViewCellsManager->ComputeSampleContributions(mVssRays);
|
---|
638 |
|
---|
639 | // construct view cells
|
---|
640 | if (!mDelayedViewCellsConstruction)
|
---|
641 | mViewCellsManager->Construct(mObjects, mVssRays);
|
---|
642 |
|
---|
643 | vssTree = new VssTree;
|
---|
644 | // viewcells = Construct(mVssRays);
|
---|
645 |
|
---|
646 | vssTree->Construct(mVssRays, mViewSpaceBox);
|
---|
647 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
648 |
|
---|
649 | if (0)
|
---|
650 | {
|
---|
651 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
652 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
653 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
654 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
655 | ExportVssTreeLeaves(vssTree, 10);
|
---|
656 | }
|
---|
657 |
|
---|
658 | // viewcells->UpdatePVS(newVssRays);
|
---|
659 | // get viewcells as kd tree boxes
|
---|
660 | vector<AxisAlignedBox3> kdViewcells;
|
---|
661 | if (0) {
|
---|
662 | vector<KdLeaf *> leaves;
|
---|
663 | mKdTree->CollectLeaves(leaves);
|
---|
664 | vector<KdLeaf *>::const_iterator it;
|
---|
665 | int targetLeaves = 50;
|
---|
666 | float prob = targetLeaves/(float)leaves.size();
|
---|
667 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
668 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
669 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
670 |
|
---|
671 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
672 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 | int samples = 0;
|
---|
677 | int pass = 0;
|
---|
678 |
|
---|
679 |
|
---|
680 | // cast view cell samples
|
---|
681 | while (samples < mVssSamples)
|
---|
682 | {
|
---|
683 |
|
---|
684 | int num = mVssSamplesPerPass;
|
---|
685 | SimpleRayContainer rays;
|
---|
686 | VssRayContainer vssRays;
|
---|
687 |
|
---|
688 | if (!mUseImportanceSampling) {
|
---|
689 | for (int j=0; j < num; j++) {
|
---|
690 | // changed by matt
|
---|
691 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
692 | Vector3 viewpoint;
|
---|
693 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
694 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
695 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
696 | }
|
---|
697 | } else {
|
---|
698 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
699 | }
|
---|
700 |
|
---|
701 | for (int i=0; i < rays.size(); i++)
|
---|
702 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
703 |
|
---|
704 | vssTree->AddRays(vssRays);
|
---|
705 |
|
---|
706 | if (0) {
|
---|
707 | int subdivided = vssTree->UpdateSubdivision();
|
---|
708 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
709 | }
|
---|
710 |
|
---|
711 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
712 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
713 |
|
---|
714 | // construct view cells after vss
|
---|
715 | if (!mViewCellsManager->ViewCellsConstructed() &&
|
---|
716 | (samples + mInitialSamples >= mViewCellsManager->GetConstructionSamples()))
|
---|
717 | {
|
---|
718 | Debug << "samples " << samples << " initial " << mInitialSamples << " " << samples + mInitialSamples << endl;
|
---|
719 | VssRayContainer constructionRays;
|
---|
720 | vssTree->CollectRays(constructionRays, mViewCellsManager->GetConstructionSamples());
|
---|
721 |
|
---|
722 | Debug << "collected rays: " << constructionRays.size() << endl;
|
---|
723 |
|
---|
724 | // test if we really have enough rays
|
---|
725 | //if (constructionRays.size() >= nSamples)
|
---|
726 | mViewCellsManager->Construct(mObjects, constructionRays);
|
---|
727 | }
|
---|
728 |
|
---|
729 | /// compute view cell contribution of rays
|
---|
730 | mViewCellsManager->ComputeSampleContributions(vssRays);
|
---|
731 |
|
---|
732 | if (numExportRays) {
|
---|
733 | char filename[64];
|
---|
734 | if (mUseImportanceSampling)
|
---|
735 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
736 | else
|
---|
737 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
738 |
|
---|
739 | ExportRays(filename, vssRays, numExportRays);
|
---|
740 | }
|
---|
741 |
|
---|
742 | samples+=num;
|
---|
743 | float pvs = vssTree->GetAvgPvsSize();
|
---|
744 | cout<<"*****************************\n";
|
---|
745 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
746 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
747 | cout<<"*****************************\n";
|
---|
748 | // if (samples >= mVssSamples) break;
|
---|
749 | pass ++;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | // at the latest create view cells here
|
---|
754 | if (!mViewCellsManager->ViewCellsConstructed())
|
---|
755 | {
|
---|
756 | VssRayContainer constructionRays;
|
---|
757 |
|
---|
758 | vssTree->CollectRays(constructionRays,
|
---|
759 | mViewCellsManager->GetConstructionSamples());
|
---|
760 | Debug << "collected rays: " << constructionRays.size() << endl;
|
---|
761 |
|
---|
762 | mViewCellsManager->Construct(mObjects, constructionRays);
|
---|
763 | }
|
---|
764 |
|
---|
765 | Debug << vssTree->stat << endl;
|
---|
766 | VssRayContainer viewCellRays;
|
---|
767 |
|
---|
768 | // compute rays used for view cells construction
|
---|
769 | int numRays = Max(mViewCellsManager->GetPostProcessSamples(),
|
---|
770 | mViewCellsManager->GetVisualizationSamples());
|
---|
771 |
|
---|
772 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
773 |
|
---|
774 | //-- post process view cells
|
---|
775 | mViewCellsManager->PostProcess(mObjects, viewCellRays);
|
---|
776 |
|
---|
777 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
778 | {
|
---|
779 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
780 | }
|
---|
781 |
|
---|
782 | //-- several visualizations and statistics
|
---|
783 | Debug << "\nview cells after post processing: " << endl;
|
---|
784 | mViewCellsManager->PrintStatistics(Debug);
|
---|
785 |
|
---|
786 | mViewCellsManager->Visualize(mObjects, viewCellRays);
|
---|
787 |
|
---|
788 | //-- render simulation after merge
|
---|
789 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
790 | mRenderSimulator->RenderScene();
|
---|
791 | SimulationStatistics ss;
|
---|
792 | mRenderSimulator->GetStatistics(ss);
|
---|
793 |
|
---|
794 | cout << " finished" << endl;
|
---|
795 | cout << ss << endl;
|
---|
796 | Debug << ss << endl;
|
---|
797 |
|
---|
798 | delete vssTree;
|
---|
799 |
|
---|
800 | return true;
|
---|
801 | }
|
---|