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