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