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 | 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 | renderer->EvalPvsStat();
|
---|
457 | mStats <<
|
---|
458 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
459 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
460 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl;
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | bool
|
---|
465 | RssPreprocessor::ComputeVisibility()
|
---|
466 | {
|
---|
467 |
|
---|
468 | // connect(this, SIGNAL(EvalPvsStat()), renderer, SLOT(EvalPvsStat()) );
|
---|
469 |
|
---|
470 | cout<<"Rss Preprocessor started\n"<<flush;
|
---|
471 | cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
|
---|
472 |
|
---|
473 | Randomize(0);
|
---|
474 |
|
---|
475 |
|
---|
476 | long startTime = GetTime();
|
---|
477 |
|
---|
478 | int totalSamples = 0;
|
---|
479 |
|
---|
480 | AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
481 |
|
---|
482 | if (fromBoxVisibility) {
|
---|
483 | float m = box->Min(1);
|
---|
484 | float bsize = box->Size(1);
|
---|
485 |
|
---|
486 | float size = 0.02f;
|
---|
487 | float s = 0.5f - size;
|
---|
488 | float olds = Magnitude(box->Size());
|
---|
489 | box->Enlarge(box->Size()*Vector3(-s));
|
---|
490 | // Vector3 translation = Vector3(-olds*0.2f, 0, 0);
|
---|
491 | Vector3 translation = Vector3(-0.05f*olds, 0, 0);
|
---|
492 | box->SetMin(box->Min() + translation);
|
---|
493 | box->SetMax(box->Max() + translation);
|
---|
494 |
|
---|
495 | box->SetMin(1, m + bsize*0.1f);
|
---|
496 | box->SetMax(1, m + bsize*0.6f);
|
---|
497 |
|
---|
498 |
|
---|
499 | } else {
|
---|
500 |
|
---|
501 | // sample city like heights
|
---|
502 | float m = box->Min(1);
|
---|
503 | float bsize = box->Size(1);
|
---|
504 | box->SetMin(1, m + bsize*0.2f);
|
---|
505 | box->SetMax(1, m + bsize*0.3f);
|
---|
506 | }
|
---|
507 |
|
---|
508 | if (use2dSampling)
|
---|
509 | box->SetMax(1, box->Min(1));
|
---|
510 |
|
---|
511 | if (useViewSpaceBox)
|
---|
512 | {
|
---|
513 | mViewSpaceBox = box;
|
---|
514 | mViewCellsManager->SetViewSpaceBox(*box);
|
---|
515 | }
|
---|
516 | else
|
---|
517 | {
|
---|
518 | mViewSpaceBox = NULL;
|
---|
519 | mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
|
---|
520 | }
|
---|
521 |
|
---|
522 |
|
---|
523 | RssTree *rssTree = NULL;
|
---|
524 | int rssPass = 0;
|
---|
525 | int rssSamples = 0;
|
---|
526 |
|
---|
527 | #if 0
|
---|
528 | if (mLoadInitialSamples) {
|
---|
529 | cout << "Loading samples from file ... ";
|
---|
530 | LoadSamples(mVssRays, mObjects);
|
---|
531 | cout << "finished\n" << endl;
|
---|
532 | } else {
|
---|
533 | #endif
|
---|
534 | while (totalSamples < mInitialSamples) {
|
---|
535 | int passContributingSamples = 0;
|
---|
536 | int passSampleContributions = 0;
|
---|
537 | int passSamples = 0;
|
---|
538 | int index = 0;
|
---|
539 |
|
---|
540 | int sampleContributions;
|
---|
541 |
|
---|
542 | //int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
543 | int s = mInitialSamples;
|
---|
544 |
|
---|
545 | for (int k=0; k < s; k++) {
|
---|
546 |
|
---|
547 |
|
---|
548 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
549 | Vector3 viewpoint;
|
---|
550 | // mViewCellsManager->GetViewPoint(viewpoint);
|
---|
551 | viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
552 | Vector3 direction = InitialGetDirection(viewpoint, mViewSpaceBox);
|
---|
553 |
|
---|
554 | sampleContributions = CastRay(viewpoint, direction, mVssRays);
|
---|
555 |
|
---|
556 |
|
---|
557 | //-- CORR matt: put block inside loop
|
---|
558 | if (sampleContributions) {
|
---|
559 | passContributingSamples ++;
|
---|
560 | passSampleContributions += sampleContributions;
|
---|
561 | }
|
---|
562 | passSamples++;
|
---|
563 | totalSamples++;
|
---|
564 | }
|
---|
565 |
|
---|
566 |
|
---|
567 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
568 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
569 |
|
---|
570 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
571 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
572 | << "k #SampleContributions=" << passSampleContributions << " ("
|
---|
573 | << 100*passContributingSamples/(float)passSamples<<"%)"
|
---|
574 | << "avgcontrib=" << avgRayContrib << endl;
|
---|
575 |
|
---|
576 | mPass++;
|
---|
577 | }
|
---|
578 |
|
---|
579 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
580 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
|
---|
581 |
|
---|
582 | rssSamples += mVssRays.size();
|
---|
583 |
|
---|
584 |
|
---|
585 | if (mExportRays) {
|
---|
586 | char filename[64];
|
---|
587 | sprintf(filename, "rss-rays-initial.x3d");
|
---|
588 | ExportRays(filename, mVssRays, mExportNumRays);
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (mStoreInitialSamples)
|
---|
592 | {
|
---|
593 | cout << "Writing samples to file ... ";
|
---|
594 | WriteSamples(mVssRays);
|
---|
595 | cout << "finished\n" << endl;
|
---|
596 | }
|
---|
597 |
|
---|
598 | if (mUseViewcells) {
|
---|
599 | // construct view cells
|
---|
600 | mViewCellsManager->Construct(mObjects, mVssRays);
|
---|
601 | // evaluate contributions of the intitial rays
|
---|
602 | mViewCellsManager->ComputeSampleContributions(mVssRays);
|
---|
603 |
|
---|
604 |
|
---|
605 | mStats <<
|
---|
606 | "#Pass\n" <<mPass<<endl<<
|
---|
607 | "#RssPass\n" <<rssPass<<endl<<
|
---|
608 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
609 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
610 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
611 |
|
---|
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 | ComputeRenderError();
|
---|
634 |
|
---|
635 | }
|
---|
636 |
|
---|
637 |
|
---|
638 |
|
---|
639 | rssPass++;
|
---|
640 |
|
---|
641 | rssTree = new RssTree;
|
---|
642 | rssTree->SetPass(mPass);
|
---|
643 |
|
---|
644 | if (mUseImportanceSampling) {
|
---|
645 |
|
---|
646 | if (fromBoxVisibility)
|
---|
647 | rssTree->Construct(mVssRays, mViewSpaceBox);
|
---|
648 | else
|
---|
649 | rssTree->Construct(mVssRays, NULL);
|
---|
650 |
|
---|
651 | rssTree->stat.Print(mStats);
|
---|
652 | cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
|
---|
653 |
|
---|
654 | if (mExportRssTree) {
|
---|
655 | ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
|
---|
656 | ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
|
---|
657 | ExportRssTree("rss-tree-101.x3d", rssTree, Vector3(1,0,1));
|
---|
658 | ExportRssTree("rss-tree-101m.x3d", rssTree, Vector3(-1,0,-1));
|
---|
659 | ExportRssTreeLeaves(rssTree, 10);
|
---|
660 | }
|
---|
661 |
|
---|
662 | if (mExportPvs) {
|
---|
663 | ExportPvs("rss-pvs-initial.x3d", rssTree);
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | // viewcells->UpdatePVS(newVssRays);
|
---|
668 |
|
---|
669 |
|
---|
670 | while (1) {
|
---|
671 | int num = mRssSamplesPerPass;
|
---|
672 | SimpleRayContainer rays;
|
---|
673 | VssRayContainer vssRays;
|
---|
674 |
|
---|
675 |
|
---|
676 | if (!mUseImportanceSampling) {
|
---|
677 | for (int j=0; j < num; j++) {
|
---|
678 | //changed by matt
|
---|
679 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
680 | Vector3 viewpoint;
|
---|
681 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
682 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
683 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
684 | }
|
---|
685 | } else {
|
---|
686 | num = GenerateImportanceRays(rssTree, num, rays);
|
---|
687 | }
|
---|
688 |
|
---|
689 |
|
---|
690 | for (int i=0; i < rays.size(); i++)
|
---|
691 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
692 |
|
---|
693 |
|
---|
694 | totalSamples += rays.size();
|
---|
695 | rssSamples += vssRays.size();
|
---|
696 |
|
---|
697 |
|
---|
698 |
|
---|
699 | mStats <<
|
---|
700 | "#Pass\n" <<mPass<<endl<<
|
---|
701 | "#RssPass\n" <<rssPass<<endl<<
|
---|
702 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<<endl<<
|
---|
703 | "#TotalSamples\n" <<totalSamples<<endl<<
|
---|
704 | "#RssSamples\n" <<rssSamples<<endl;
|
---|
705 |
|
---|
706 |
|
---|
707 | if (mUseViewcells) {
|
---|
708 |
|
---|
709 | /// compute view cell contribution of rays
|
---|
710 | mViewCellsManager->ComputeSampleContributions(vssRays);
|
---|
711 |
|
---|
712 | vssRays.PrintStatistics(mStats);
|
---|
713 | mViewCellsManager->PrintPvsStatistics(mStats);
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | ComputeRenderError();
|
---|
718 |
|
---|
719 | // epxort rays before adding them to the tree -> some of them can be deleted
|
---|
720 |
|
---|
721 | if (mExportRays) {
|
---|
722 | char filename[64];
|
---|
723 | if (mUseImportanceSampling)
|
---|
724 | sprintf(filename, "rss-rays-i%04d.x3d", rssPass);
|
---|
725 | else
|
---|
726 | sprintf(filename, "rss-rays-%04d.x3d", rssPass);
|
---|
727 |
|
---|
728 | ExportRays(filename, vssRays, mExportNumRays);
|
---|
729 |
|
---|
730 | // now export all contributing rays
|
---|
731 | VssRayContainer contributingRays;
|
---|
732 | vssRays.GetContributingRays(contributingRays, mPass);
|
---|
733 | mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<contributingRays.size()<<endl;
|
---|
734 | sprintf(filename, "rss-crays-%04d.x3d", rssPass);
|
---|
735 | ExportRays(filename, contributingRays, mExportNumRays);
|
---|
736 | }
|
---|
737 |
|
---|
738 |
|
---|
739 | // add rays to the tree after the viewcells have been cast to have their contributions
|
---|
740 | // already when adding into the tree
|
---|
741 | // do not add those rays which have too low or no contribution....
|
---|
742 |
|
---|
743 | if (mUseImportanceSampling) {
|
---|
744 | rssTree->AddRays(vssRays);
|
---|
745 |
|
---|
746 | if (0) {
|
---|
747 | cout<<"############# Rss PVS STAT ##################\n";
|
---|
748 | cout<<"#AVG_RSS_PVS\n"<<rssTree->GetAvgPvsSize()<<endl;
|
---|
749 | cout<<"#RSS_ROOT_PVS\n"<<rssTree->GetRootPvsSize()<<endl;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | if (mUpdateSubdivision) {
|
---|
754 | int updatePasses = 1;
|
---|
755 | if (mPass % updatePasses == 0) {
|
---|
756 | int subdivided = rssTree->UpdateSubdivision();
|
---|
757 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
758 | cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
|
---|
759 | }
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 |
|
---|
764 |
|
---|
765 | if (mExportPvs) {
|
---|
766 | char filename[64];
|
---|
767 | sprintf(filename, "rss-pvs-%04d.x3d", rssPass);
|
---|
768 | ExportPvs(filename, rssTree);
|
---|
769 | }
|
---|
770 |
|
---|
771 |
|
---|
772 | if (!mUseImportanceSampling)
|
---|
773 | CLEAR_CONTAINER(vssRays);
|
---|
774 |
|
---|
775 | if (totalSamples >= mRssSamples + mInitialSamples)
|
---|
776 | break;
|
---|
777 |
|
---|
778 |
|
---|
779 | rssPass++;
|
---|
780 | mPass++;
|
---|
781 | rssTree->SetPass(mPass);
|
---|
782 | }
|
---|
783 |
|
---|
784 | if (mUseViewcells) {
|
---|
785 |
|
---|
786 |
|
---|
787 | //-- render simulation after merge
|
---|
788 | cout << "\nevaluating bsp view cells render time after merge ... ";
|
---|
789 |
|
---|
790 | mRenderSimulator->RenderScene();
|
---|
791 | SimulationStatistics ss;
|
---|
792 | mRenderSimulator->GetStatistics(ss);
|
---|
793 |
|
---|
794 | cout << " finished" << endl;
|
---|
795 | cout << ss << endl;
|
---|
796 | Debug << ss << endl;
|
---|
797 |
|
---|
798 | }
|
---|
799 |
|
---|
800 | delete rssTree;
|
---|
801 |
|
---|
802 | return true;
|
---|
803 | }
|
---|
804 |
|
---|