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