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