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