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