1 | #include "SceneGraph.h"
|
---|
2 | #include "KdTree.h"
|
---|
3 | #include "RssPreprocessor.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 "RssTree.h"
|
---|
11 | #include "ViewCellsManager.h"
|
---|
12 | #include "RenderSimulator.h"
|
---|
13 | #include "GlRenderer.h"
|
---|
14 |
|
---|
15 | static bool useViewSpaceBox = false;
|
---|
16 | static bool use2dSampling = false;
|
---|
17 |
|
---|
18 |
|
---|
19 | // not supported anymore!
|
---|
20 | static bool fromBoxVisibility = false;
|
---|
21 |
|
---|
22 |
|
---|
23 | RssPreprocessor::RssPreprocessor():
|
---|
24 | mPass(0),
|
---|
25 | mVssRays()
|
---|
26 | {
|
---|
27 | // this should increase coherence of the samples
|
---|
28 | environment->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
29 | environment->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples);
|
---|
30 | environment->GetIntValue("RssPreprocessor.vssSamples", mRssSamples);
|
---|
31 | environment->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass);
|
---|
32 | environment->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
33 |
|
---|
34 | environment->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
|
---|
35 | environment->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
|
---|
36 | environment->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
|
---|
37 | environment->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
|
---|
38 | environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
|
---|
39 | environment->GetBoolValue("RssPreprocessor.objectBasedSampling", mObjectBasedSampling);
|
---|
40 | environment->GetBoolValue("RssPreprocessor.directionalSampling", mDirectionalSampling);
|
---|
41 |
|
---|
42 | environment->GetBoolValue("RssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
43 | environment->GetBoolValue("RssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
44 |
|
---|
45 | mStats.open("stats.log");
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | RssPreprocessor::~RssPreprocessor()
|
---|
50 | {
|
---|
51 | // mVssRays get deleted in the tree
|
---|
52 | // CLEAR_CONTAINER(mVssRays);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void
|
---|
56 | RssPreprocessor::SetupRay(Ray &ray,
|
---|
57 | const Vector3 &point,
|
---|
58 | const Vector3 &direction
|
---|
59 | )
|
---|
60 | {
|
---|
61 | ray.intersections.clear();
|
---|
62 | // do not store anything else then intersections at the ray
|
---|
63 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
64 | }
|
---|
65 |
|
---|
66 | int
|
---|
67 | RssPreprocessor::CastRay(
|
---|
68 | Vector3 &viewPoint,
|
---|
69 | Vector3 &direction,
|
---|
70 | const float probability,
|
---|
71 | VssRayContainer &vssRays
|
---|
72 | )
|
---|
73 | {
|
---|
74 | int hits = 0;
|
---|
75 | static Ray ray;
|
---|
76 | AxisAlignedBox3 box = mKdTree->GetBox();
|
---|
77 |
|
---|
78 | AxisAlignedBox3 sbox = box;
|
---|
79 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
80 | if (!sbox.IsInside(viewPoint))
|
---|
81 | return 0;
|
---|
82 |
|
---|
83 | SetupRay(ray, viewPoint, direction);
|
---|
84 |
|
---|
85 | if (!mDetectEmptyViewSpace)
|
---|
86 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
87 |
|
---|
88 | // cast ray to KD tree to find intersection with other objects
|
---|
89 | Intersectable *objectA, *objectB;
|
---|
90 | Vector3 pointA, pointB;
|
---|
91 | float bsize = Magnitude(box.Size());
|
---|
92 |
|
---|
93 |
|
---|
94 | if (mKdTree->CastRay(ray)) {
|
---|
95 | objectA = ray.intersections[0].mObject;
|
---|
96 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
97 | } else {
|
---|
98 | objectA = NULL;
|
---|
99 | // compute intersection with the scene bounding box
|
---|
100 | float tmin, tmax;
|
---|
101 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
102 | pointA = ray.Extrap(tmax);
|
---|
103 | else
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | if (mDetectEmptyViewSpace) {
|
---|
109 | SetupRay(ray, pointA, -direction);
|
---|
110 | } else
|
---|
111 | SetupRay(ray, viewPoint, -direction);
|
---|
112 |
|
---|
113 | if (!mDetectEmptyViewSpace)
|
---|
114 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
115 |
|
---|
116 | if (mKdTree->CastRay(ray)) {
|
---|
117 | objectB = ray.intersections[0].mObject;
|
---|
118 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
119 | } else {
|
---|
120 | objectB = NULL;
|
---|
121 | float tmin, tmax;
|
---|
122 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
123 | pointB = ray.Extrap(tmax);
|
---|
124 | else
|
---|
125 | return 0;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // if (objectA == NULL && objectB != NULL) {
|
---|
129 | if (mDetectEmptyViewSpace) {
|
---|
130 | // cast again to ensure that there is no objectA
|
---|
131 | SetupRay(ray, pointB, direction);
|
---|
132 | if (mKdTree->CastRay(ray)) {
|
---|
133 | objectA = ray.intersections[0].mObject;
|
---|
134 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | VssRay *vssRay = NULL;
|
---|
140 |
|
---|
141 | bool validSample = (objectA != objectB);
|
---|
142 | if (validSample) {
|
---|
143 | if (objectA) {
|
---|
144 | vssRay = new VssRay(pointB,
|
---|
145 | pointA,
|
---|
146 | objectB,
|
---|
147 | objectA,
|
---|
148 | mPass,
|
---|
149 | probability
|
---|
150 | );
|
---|
151 | vssRays.push_back(vssRay);
|
---|
152 | hits ++;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (objectB) {
|
---|
156 | vssRay = new VssRay(pointA,
|
---|
157 | pointB,
|
---|
158 | objectA,
|
---|
159 | objectB,
|
---|
160 | mPass,
|
---|
161 | probability
|
---|
162 | );
|
---|
163 | vssRays.push_back(vssRay);
|
---|
164 | hits ++;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | return hits;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | Vector3
|
---|
173 | RssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
174 | {
|
---|
175 | AxisAlignedBox3 box;
|
---|
176 |
|
---|
177 | if (viewSpaceBox)
|
---|
178 | box =*viewSpaceBox;
|
---|
179 | else
|
---|
180 | box = mKdTree->GetBox();
|
---|
181 |
|
---|
182 | // shrink the box in the y direction
|
---|
183 | return box.GetRandomPoint();
|
---|
184 | }
|
---|
185 |
|
---|
186 | Vector3
|
---|
187 | RssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
188 | AxisAlignedBox3 *viewSpaceBox
|
---|
189 | )
|
---|
190 | {
|
---|
191 | Vector3 point;
|
---|
192 | if (!use2dSampling) {
|
---|
193 | if (mObjectBasedSampling) {
|
---|
194 | Vector3 normal;
|
---|
195 | int i = RandomValue(0, mObjects.size()-1);
|
---|
196 | Intersectable *object = mObjects[i];
|
---|
197 | object->GetRandomSurfacePoint(point, normal);
|
---|
198 | } else {
|
---|
199 | // select
|
---|
200 | if (mDirectionalSampling) {
|
---|
201 | point = viewpoint + UniformRandomVector();
|
---|
202 | } else {
|
---|
203 | // select the other point uniformly distruted in the whole viewspace
|
---|
204 | AxisAlignedBox3 box;
|
---|
205 |
|
---|
206 | if (viewSpaceBox)
|
---|
207 | box =*viewSpaceBox;
|
---|
208 | else
|
---|
209 | box = mKdTree->GetBox();
|
---|
210 |
|
---|
211 | point = box.GetRandomPoint();
|
---|
212 | }
|
---|
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 | Vector3
|
---|
230 | RssPreprocessor::InitialGetDirection(const Vector3 &viewpoint,
|
---|
231 | AxisAlignedBox3 *viewSpaceBox
|
---|
232 | )
|
---|
233 | {
|
---|
234 | Vector3 point;
|
---|
235 | if (!use2dSampling) {
|
---|
236 | if (1 || mObjectBasedSampling) {
|
---|
237 | Vector3 normal;
|
---|
238 | int i = RandomValue(0, mObjects.size()-1);
|
---|
239 | Intersectable *object = mObjects[i];
|
---|
240 | object->GetRandomSurfacePoint(point, normal);
|
---|
241 | } else {
|
---|
242 | // select
|
---|
243 | if (1 || mDirectionalSampling) {
|
---|
244 | point = viewpoint + UniformRandomVector();
|
---|
245 | } else {
|
---|
246 | // select the other point uniformly distruted in the whole viewspace
|
---|
247 | AxisAlignedBox3 box;
|
---|
248 |
|
---|
249 | if (viewSpaceBox)
|
---|
250 | box =*viewSpaceBox;
|
---|
251 | else
|
---|
252 | box = mKdTree->GetBox();
|
---|
253 |
|
---|
254 | point = box.GetRandomPoint();
|
---|
255 | }
|
---|
256 | }
|
---|
257 | } else {
|
---|
258 | AxisAlignedBox3 box;
|
---|
259 |
|
---|
260 | if (viewSpaceBox)
|
---|
261 | box =*viewSpaceBox;
|
---|
262 | else
|
---|
263 | box = mKdTree->GetBox();
|
---|
264 |
|
---|
265 | point = box.GetRandomPoint();
|
---|
266 | point.y = viewpoint.y;
|
---|
267 | }
|
---|
268 |
|
---|
269 | return point - viewpoint;
|
---|
270 | }
|
---|
271 |
|
---|
272 | int
|
---|
273 | RssPreprocessor::GenerateImportanceRays(RssTree *rssTree,
|
---|
274 | const int desiredSamples,
|
---|
275 | SimpleRayContainer &rays
|
---|
276 | )
|
---|
277 | {
|
---|
278 | int num;
|
---|
279 |
|
---|
280 | rssTree->UpdateTreeStatistics();
|
---|
281 |
|
---|
282 | cout<<
|
---|
283 | "#RSS_AVG_PVS_SIZE\n"<<rssTree->stat.avgPvsSize<<endl<<
|
---|
284 | "#RSS_AVG_RAYS\n"<<rssTree->stat.avgRays<<endl<<
|
---|
285 | "#RSS_AVG_RAY_CONTRIB\n"<<rssTree->stat.avgRayContribution<<endl<<
|
---|
286 | "#RSS_AVG_PVS_ENTROPY\n"<<rssTree->stat.avgPvsEntropy<<endl<<
|
---|
287 | "#RSS_AVG_RAY_LENGTH_ENTROPY\n"<<rssTree->stat.avgRayLengthEntropy<<endl<<
|
---|
288 | "#RSS_AVG_IMPORTANCE\n"<<rssTree->stat.avgImportance<<endl;
|
---|
289 |
|
---|
290 | if (0) {
|
---|
291 | float p = desiredSamples/(float)(rssTree->stat.avgRayContribution*rssTree->stat.Leaves());
|
---|
292 | num = rssTree->GenerateRays(p, rays);
|
---|
293 | } else {
|
---|
294 | int leaves = rssTree->stat.Leaves()/1;
|
---|
295 | num = rssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
296 | }
|
---|
297 |
|
---|
298 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
299 |
|
---|
300 | return num;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | bool
|
---|
305 | RssPreprocessor::ExportRays(const char *filename,
|
---|
306 | const VssRayContainer &vssRays,
|
---|
307 | const int number
|
---|
308 | )
|
---|
309 | {
|
---|
310 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
311 |
|
---|
312 | Exporter *exporter = NULL;
|
---|
313 | exporter = Exporter::GetExporter(filename);
|
---|
314 | // exporter->SetWireframe();
|
---|
315 | // exporter->ExportKdTree(*mKdTree);
|
---|
316 | exporter->SetFilled();
|
---|
317 | // temporarily do not export the scene
|
---|
318 | if (0)
|
---|
319 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
320 | exporter->SetWireframe();
|
---|
321 |
|
---|
322 | if (mViewSpaceBox) {
|
---|
323 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
324 | exporter->ExportBox(*mViewSpaceBox);
|
---|
325 | exporter->ResetForcedMaterial();
|
---|
326 | }
|
---|
327 |
|
---|
328 | VssRayContainer rays;
|
---|
329 |
|
---|
330 | vssRays.SelectRays( number, rays);
|
---|
331 |
|
---|
332 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
333 |
|
---|
334 | delete exporter;
|
---|
335 |
|
---|
336 | cout<<"done."<<endl<<flush;
|
---|
337 |
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | bool
|
---|
343 | RssPreprocessor::ExportRssTree(char *filename,
|
---|
344 | RssTree *tree,
|
---|
345 | const Vector3 &dir
|
---|
346 | )
|
---|
347 | {
|
---|
348 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
349 | exporter->SetFilled();
|
---|
350 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
351 | // exporter->SetWireframe();
|
---|
352 | bool result = exporter->ExportRssTree2( *tree, dir );
|
---|
353 | delete exporter;
|
---|
354 | return result;
|
---|
355 | }
|
---|
356 |
|
---|
357 | bool
|
---|
358 | RssPreprocessor::ExportRssTreeLeaf(char *filename,
|
---|
359 | RssTree *tree,
|
---|
360 | RssTreeLeaf *leaf)
|
---|
361 | {
|
---|
362 | Exporter *exporter = NULL;
|
---|
363 | exporter = Exporter::GetExporter(filename);
|
---|
364 | exporter->SetWireframe();
|
---|
365 | exporter->ExportKdTree(*mKdTree);
|
---|
366 |
|
---|
367 | if (mViewSpaceBox) {
|
---|
368 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
369 | exporter->ExportBox(*mViewSpaceBox);
|
---|
370 | exporter->ResetForcedMaterial();
|
---|
371 | }
|
---|
372 |
|
---|
373 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
374 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
375 | exporter->ResetForcedMaterial();
|
---|
376 |
|
---|
377 | VssRayContainer rays[4];
|
---|
378 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
379 | int k = leaf->rays[i].GetRayClass();
|
---|
380 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
381 | }
|
---|
382 |
|
---|
383 | // SOURCE RAY
|
---|
384 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
385 | // TERMINATION RAY
|
---|
386 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
387 | // PASSING_RAY
|
---|
388 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
389 | // CONTAINED_RAY
|
---|
390 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
391 |
|
---|
392 | delete exporter;
|
---|
393 | return true;
|
---|
394 | }
|
---|
395 |
|
---|
396 | void
|
---|
397 | RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number)
|
---|
398 | {
|
---|
399 | vector<RssTreeLeaf *> leaves;
|
---|
400 | tree->CollectLeaves(leaves);
|
---|
401 |
|
---|
402 | int num = 0;
|
---|
403 | int i;
|
---|
404 | float p = number / (float)leaves.size();
|
---|
405 | for (i=0; i < leaves.size(); i++) {
|
---|
406 | if (RandomValue(0,1) < p) {
|
---|
407 | char filename[64];
|
---|
408 | sprintf(filename, "rss-leaf-%04d.x3d", num);
|
---|
409 | ExportRssTreeLeaf(filename, tree, leaves[i]);
|
---|
410 | num++;
|
---|
411 | }
|
---|
412 | if (num >= number)
|
---|
413 | break;
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | float
|
---|
419 | RssPreprocessor::GetAvgPvsSize(RssTree *tree,
|
---|
420 | const vector<AxisAlignedBox3> &viewcells
|
---|
421 | )
|
---|
422 | {
|
---|
423 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
424 |
|
---|
425 | int sum = 0;
|
---|
426 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
427 | sum += tree->GetPvsSize(*it);
|
---|
428 |
|
---|
429 | return sum/(float)viewcells.size();
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | void
|
---|
434 | RssPreprocessor::ExportPvs(char *filename,
|
---|
435 | RssTree *rssTree
|
---|
436 | )
|
---|
437 | {
|
---|
438 | ObjectContainer pvs;
|
---|
439 | if (rssTree->CollectRootPvs(pvs)) {
|
---|
440 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
441 | exporter->SetFilled();
|
---|
442 | exporter->ExportGeometry(pvs);
|
---|
443 | exporter->SetWireframe();
|
---|
444 | exporter->ExportBox(rssTree->bbox);
|
---|
445 | exporter->ExportViewpoint(rssTree->bbox.Center(), Vector3(1,0,0));
|
---|
446 | delete exporter;
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | void
|
---|
452 | RssPreprocessor::ComputeRenderError()
|
---|
453 | {
|
---|
454 | // compute rendering error
|
---|
455 | if (renderer) {
|
---|
456 | // emit EvalPvsStat();
|
---|
457 | // QMutex mutex;
|
---|
458 | // mutex.lock();
|
---|
459 | // renderer->mRenderingFinished.wait(&mutex);
|
---|
460 | // mutex.unlock();
|
---|
461 | renderer->EvalPvsStat();
|
---|
462 | mStats <<
|
---|
463 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
464 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
465 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl;
|
---|
466 | }
|
---|
467 | }
|
---|
468 |
|
---|
469 | bool
|
---|
470 | RssPreprocessor::ComputeVisibility()
|
---|
471 | {
|
---|
472 |
|
---|
473 | // connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) );
|
---|
474 |
|
---|
475 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
476 | cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
477 |
|
---|
478 | Randomize(0);
|
---|
479 |
|
---|
480 |
|
---|
481 | long startTime = GetTime();
|
---|
482 |
|
---|
483 | int totalSamples = 0;
|
---|
484 |
|
---|
485 | AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
486 |
|
---|
487 | if (fromBoxVisibility) {
|
---|
488 | float m = box->Min(1);
|
---|
489 | float bsize = box->Size(1);
|
---|
490 |
|
---|
491 | float size = 0.02f;
|
---|
492 | float s = 0.5f - size;
|
---|
493 | float olds = Magnitude(box->Size());
|
---|
494 | box->Enlarge(box->Size()*Vector3(-s));
|
---|
495 | // Vector3 translation = Vector3(-olds*0.2f, 0, 0);
|
---|
496 | Vector3 translation = Vector3(-0.05f*olds, 0, 0);
|
---|
497 | box->SetMin(box->Min() + translation);
|
---|
498 | box->SetMax(box->Max() + translation);
|
---|
499 |
|
---|
500 | box->SetMin(1, m + bsize*0.1f);
|
---|
501 | box->SetMax(1, m + bsize*0.6f);
|
---|
502 |
|
---|
503 |
|
---|
504 | } else {
|
---|
505 |
|
---|
506 | // sample city like heights
|
---|
507 | float m = box->Min(1);
|
---|
508 | float bsize = box->Size(1);
|
---|
509 | box->SetMin(1, m + bsize*0.2f);
|
---|
510 | box->SetMax(1, m + bsize*0.3f);
|
---|
511 | }
|
---|
512 |
|
---|
513 | if (use2dSampling)
|
---|
514 | box->SetMax(1, box->Min(1));
|
---|
515 |
|
---|
516 | if (useViewSpaceBox)
|
---|
517 | {
|
---|
518 | mViewSpaceBox = box;
|
---|
519 | mViewCellsManager->SetViewSpaceBox(*box);
|
---|
520 | }
|
---|
521 | else
|
---|
522 | {
|
---|
523 | mViewSpaceBox = NULL;
|
---|
524 | mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
|
---|
525 | }
|
---|
526 |
|
---|
527 | //-- load view cells from file if requested
|
---|
528 | if (mLoadViewCells)
|
---|
529 | {
|
---|
530 | // load now because otherwise bounding box not correct
|
---|
531 | mViewCellsManager->LoadViewCells(mViewCellsFilename, &mObjects);
|
---|
532 | }
|
---|
533 |
|
---|
534 | RssTree *rssTree = NULL;
|
---|
535 | int rssPass = 0;
|
---|
536 | int rssSamples = 0;
|
---|
537 |
|
---|
538 | #if 0
|
---|
539 | if (mLoadInitialSamples) {
|
---|
540 | cout << "Loading samples from file ... ";
|
---|
541 | LoadSamples(mVssRays, mObjects);
|
---|
542 | cout << "finished\n" << endl;
|
---|
543 | } else {
|
---|
544 | #endif
|
---|
545 | while (totalSamples < mInitialSamples) {
|
---|
546 | int passContributingSamples = 0;
|
---|
547 | int passSampleContributions = 0;
|
---|
548 | int passSamples = 0;
|
---|
549 | int index = 0;
|
---|
550 |
|
---|
551 | int sampleContributions;
|
---|
552 |
|
---|
553 | //int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
554 | int s = mInitialSamples;
|
---|
555 |
|
---|
556 | float probability = 1.0f/mInitialSamples;
|
---|
557 | for (int k=0; k < s; k++) {
|
---|
558 |
|
---|
559 |
|
---|
560 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
561 | Vector3 viewpoint;
|
---|
562 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
563 | // viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
564 | Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox);
|
---|
565 |
|
---|
566 | sampleContributions = CastRay(viewpoint, direction, probability, mVssRays);
|
---|
567 |
|
---|
568 |
|
---|
569 | //-- CORR matt: put block inside loop
|
---|
570 | if (sampleContributions) {
|
---|
571 | passContributingSamples ++;
|
---|
572 | passSampleContributions += sampleContributions;
|
---|
573 | }
|
---|
574 | passSamples++;
|
---|
575 | totalSamples++;
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
580 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
581 |
|
---|
582 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
583 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
584 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
585 | << 100*passContributingSamples/(float)passSamples<<"%)"
|
---|
586 | << "avgcontrib=" << avgRayContrib << endl;
|
---|
587 |
|
---|
588 | mPass++;
|
---|
589 | }
|
---|
590 |
|
---|
591 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
592 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
593 |
|
---|
594 | rssSamples += (int)mVssRays.size();
|
---|
595 |
|
---|
596 |
|
---|
597 | if (mExportRays) {
|
---|
598 | char filename[64];
|
---|
599 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
600 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
601 | }
|
---|
602 |
|
---|
603 | if (mStoreInitialSamples)
|
---|
604 | {
|
---|
605 | cout << "Writing samples to file ... ";
|
---|
606 | ExportSamples(mVssRays);
|
---|
607 | cout << "finished\n" << endl;
|
---|
608 | }
|
---|
609 |
|
---|
610 | if (mUseViewcells) {
|
---|
611 | // construct view cells
|
---|
612 | if (!mLoadViewCells)
|
---|
613 | mViewCellsManager->Construct(mObjects, mVssRays);
|
---|
614 | // evaluate contributions of the intitial rays
|
---|
615 | mViewCellsManager->ComputeSampleContributions(mVssRays);
|
---|
616 |
|
---|
617 |
|
---|
618 | mStats <<
|
---|
619 | "#Pass\n" <<mPass<<endl<<
|
---|
620 | "#RssPass\n" <<rssPass<<endl<<
|
---|
621 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
622 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
623 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
624 |
|
---|
625 |
|
---|
626 | mVssRays.PrintStatistics(mStats);
|
---|
627 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
628 |
|
---|
629 | VssRayContainer selectedRays;
|
---|
630 | int desired = Max(
|
---|
631 | mViewCellsManager->GetPostProcessSamples(),
|
---|
632 | mViewCellsManager->GetVisualizationSamples());
|
---|
633 |
|
---|
634 | mVssRays.SelectRays(desired, selectedRays);
|
---|
635 |
|
---|
636 | //-- post process view cells
|
---|
637 | mViewCellsManager->PostProcess(mObjects, selectedRays);
|
---|
638 |
|
---|
639 | //-- several visualizations and statistics
|
---|
640 | Debug << "view cells after post processing: " << endl;
|
---|
641 | mViewCellsManager->PrintStatistics(Debug);
|
---|
642 |
|
---|
643 | if (1)
|
---|
644 | mViewCellsManager->Visualize(mObjects, selectedRays);
|
---|
645 |
|
---|
646 | ComputeRenderError();
|
---|
647 |
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 |
|
---|
652 | rssPass++;
|
---|
653 |
|
---|
654 | rssTree = new RssTree;
|
---|
655 | rssTree->SetPass(mPass);
|
---|
656 |
|
---|
657 | /// compute view cell contribution of rays if view cells manager already constructed
|
---|
658 | mViewCellsManager->ComputeSampleContributions(mVssRays);
|
---|
659 |
|
---|
660 | if (mUseImportanceSampling) {
|
---|
661 |
|
---|
662 | // if (fromBoxVisibility)
|
---|
663 | // rssTree->Construct(mObjects, mVssRays, mViewSpaceBox);
|
---|
664 | // else
|
---|
665 | rssTree->Construct(mObjects, mVssRays);
|
---|
666 |
|
---|
667 | rssTree->stat.Print(mStats);
|
---|
668 | cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
|
---|
669 |
|
---|
670 | if (mExportRssTree) {
|
---|
671 | ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
|
---|
672 | ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
|
---|
673 | ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
|
---|
674 | ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
|
---|
675 | ExportRssTreeLeaves(rssTree, 10);
|
---|
676 | }
|
---|
677 |
|
---|
678 | if (mExportPvs) {
|
---|
679 | ExportPvs("rss-pvs-initial.x3d", rssTree);
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | // viewcells->UpdatePVS(newVssRays);
|
---|
684 |
|
---|
685 |
|
---|
686 | while (1) {
|
---|
687 | int num = mRssSamplesPerPass;
|
---|
688 | SimpleRayContainer rays;
|
---|
689 | VssRayContainer vssRays;
|
---|
690 |
|
---|
691 |
|
---|
692 | if (!mUseImportanceSampling) {
|
---|
693 | for (int j=0; j < num; j++) {
|
---|
694 | //changed by matt
|
---|
695 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
696 | Vector3 viewpoint;
|
---|
697 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
698 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
699 | rays.push_back(SimpleRay(viewpoint, direction, 1.0f));
|
---|
700 | rays.mSumProbabilities+=1.0f;
|
---|
701 | }
|
---|
702 | } else {
|
---|
703 | num = GenerateImportanceRays(rssTree, num, rays);
|
---|
704 | }
|
---|
705 |
|
---|
706 | rays.NormalizeProbabilities();
|
---|
707 |
|
---|
708 | for (int i=0; i < rays.size(); i++)
|
---|
709 | CastRay(rays[i].mOrigin, rays[i].mDirection, rays[i].mProbability, vssRays);
|
---|
710 |
|
---|
711 |
|
---|
712 | totalSamples += (int)rays.size();
|
---|
713 | rssSamples += (int)vssRays.size();
|
---|
714 |
|
---|
715 |
|
---|
716 |
|
---|
717 | mStats <<
|
---|
718 | "#Pass\n" <<mPass<<endl<<
|
---|
719 | "#RssPass\n" <<rssPass<<endl<<
|
---|
720 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
721 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
722 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
723 |
|
---|
724 |
|
---|
725 | if (mUseViewcells) {
|
---|
726 |
|
---|
727 | /// compute view cell contribution of rays
|
---|
728 | mViewCellsManager->ComputeSampleContributions(vssRays);
|
---|
729 |
|
---|
730 | vssRays.PrintStatistics(mStats);
|
---|
731 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | ComputeRenderError();
|
---|
736 |
|
---|
737 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
738 |
|
---|
739 | if (mExportRays) {
|
---|
740 | char filename[64];
|
---|
741 | if (mUseImportanceSampling)
|
---|
742 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
743 | else
|
---|
744 | sprintf(filename, "rss-rays-%04d.x3d", rssPass);
|
---|
745 |
|
---|
746 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
747 |
|
---|
748 | // now export all contributing rays
|
---|
749 | VssRayContainer contributingRays;
|
---|
750 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
751 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
|
---|
752 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
753 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
754 | }
|
---|
755 |
|
---|
756 |
|
---|
757 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
758 | // already when adding into the tree
|
---|
759 | // do not add those rays which have too low or no contribution....
|
---|
760 |
|
---|
761 | if (mUseImportanceSampling) {
|
---|
762 | rssTree->AddRays(vssRays);
|
---|
763 |
|
---|
764 | if (0) {
|
---|
765 | cout<<"############# Rss PVS STAT ##################\n";
|
---|
766 | cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl;
|
---|
767 | cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl;
|
---|
768 | }
|
---|
769 |
|
---|
770 |
|
---|
771 | if (mUpdateSubdivision) {
|
---|
772 | int updatePasses = 1;
|
---|
773 | if (mPass % updatePasses == 0) {
|
---|
774 | int subdivided = rssTree->UpdateSubdivision();
|
---|
775 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
776 | cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
|
---|
777 | }
|
---|
778 | }
|
---|
779 | }
|
---|
780 |
|
---|
781 |
|
---|
782 |
|
---|
783 | if (mExportPvs) {
|
---|
784 | char filename[64];
|
---|
785 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
786 | ExportPvs(filename, rssTree);
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | if (!mUseImportanceSampling)
|
---|
791 | CLEAR_CONTAINER(vssRays);
|
---|
792 |
|
---|
793 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
794 | break;
|
---|
795 |
|
---|
796 |
|
---|
797 | rssPass++;
|
---|
798 | mPass++;
|
---|
799 | rssTree->SetPass(mPass);
|
---|
800 | }
|
---|
801 |
|
---|
802 | if (mUseViewcells) {
|
---|
803 |
|
---|
804 |
|
---|
805 | //-- render simulation after merge
|
---|
806 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
807 |
|
---|
808 | mRenderSimulator->RenderScene();
|
---|
809 | SimulationStatistics ss;
|
---|
810 | mRenderSimulator->GetStatistics(ss);
|
---|
811 |
|
---|
812 | cout << " finished" << endl;
|
---|
813 | cout << ss << endl;
|
---|
814 | Debug << ss << endl;
|
---|
815 |
|
---|
816 | }
|
---|
817 |
|
---|
818 | delete rssTree;
|
---|
819 |
|
---|
820 | return true;
|
---|
821 | }
|
---|
822 |
|
---|