1 | #include "SceneGraph.h"
|
---|
2 | #include "KdTree.h"
|
---|
3 | #include "VssPreprocessor.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 "VssTree.h"
|
---|
11 | #include "ViewCellsManager.h"
|
---|
12 | #include "RenderSimulator.h"
|
---|
13 | #include "Beam.h"
|
---|
14 | #include "GlRenderer.h"
|
---|
15 | #include "ViewCellBsp.h"
|
---|
16 |
|
---|
17 | bool use2dSampling = false;
|
---|
18 | bool useViewspacePlane = false;
|
---|
19 |
|
---|
20 | VssPreprocessor::VssPreprocessor():
|
---|
21 | mPass(0),
|
---|
22 | mVssRays()
|
---|
23 | {
|
---|
24 | // this should increase coherence of the samples
|
---|
25 | environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
|
---|
26 | environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
|
---|
27 | environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
|
---|
28 | environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
|
---|
29 | environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
|
---|
30 |
|
---|
31 | environment->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
|
---|
32 | environment->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
|
---|
33 | environment->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
34 | environment->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
|
---|
35 | environment->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
|
---|
36 | environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
37 |
|
---|
38 | useViewspacePlane = mUseViewSpaceBox; //hack
|
---|
39 |
|
---|
40 |
|
---|
41 | Debug << "*********** vss preprocessor options **************" << endl;
|
---|
42 | Debug << "use view space box=" << mUseViewSpaceBox << endl;
|
---|
43 | Debug << "enlarge view space=" << mEnlargeViewSpace << endl;
|
---|
44 | Debug << "*********** end vss preprocessor options **************" << endl;
|
---|
45 |
|
---|
46 | mStats.open("stats.log");
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | VssPreprocessor::~VssPreprocessor()
|
---|
51 | {
|
---|
52 | CLEAR_CONTAINER(mVssRays);
|
---|
53 | DEL_PTR(mViewSpaceBox);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | void
|
---|
58 | VssPreprocessor::SetupRay(Ray &ray,
|
---|
59 | const Vector3 &point,
|
---|
60 | const Vector3 &direction
|
---|
61 | )
|
---|
62 | {
|
---|
63 | ray.Clear();
|
---|
64 | // do not store anything else then intersections at the ray
|
---|
65 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | void
|
---|
70 | VssPreprocessor::CastRays(
|
---|
71 | SimpleRayContainer &rays,
|
---|
72 | VssRayContainer &vssRays
|
---|
73 | )
|
---|
74 | {
|
---|
75 | for (int i=0; i < rays.size(); i++)
|
---|
76 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | int
|
---|
81 | VssPreprocessor::CastRay(
|
---|
82 | Vector3 &viewPoint,
|
---|
83 | Vector3 &direction,
|
---|
84 | VssRayContainer &vssRays
|
---|
85 | )
|
---|
86 | {
|
---|
87 |
|
---|
88 | int hits = 0;
|
---|
89 | static Ray ray;
|
---|
90 |
|
---|
91 | AxisAlignedBox3 box = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
|
---|
92 | AxisAlignedBox3 sbox = box;
|
---|
93 |
|
---|
94 | sbox.Enlarge(Vector3(-Limits::Small));
|
---|
95 |
|
---|
96 | if (!sbox.IsInside(viewPoint))
|
---|
97 | return 0;
|
---|
98 |
|
---|
99 | SetupRay(ray, viewPoint, direction);
|
---|
100 |
|
---|
101 | // cast ray to KD tree to find intersection with other objects
|
---|
102 | Intersectable *objectA, *objectB;
|
---|
103 | // change matt
|
---|
104 | Vector3 pointA;
|
---|
105 | Vector3 pointB;
|
---|
106 |
|
---|
107 | float bsize = Magnitude(box.Size());
|
---|
108 |
|
---|
109 | if (!mDetectEmptyViewSpace)
|
---|
110 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
111 | else
|
---|
112 | ray.mFlags |= Ray::CULL_BACKFACES;
|
---|
113 |
|
---|
114 | if (mKdTree->CastRay(ray))
|
---|
115 | {
|
---|
116 | objectA = ray.intersections[0].mObject;
|
---|
117 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | objectA = NULL;
|
---|
122 | // compute intersection with the scene bounding box
|
---|
123 | float tmin, tmax;
|
---|
124 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
125 | pointA = ray.Extrap(tmax);
|
---|
126 | else
|
---|
127 | return 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // matt: point A could be undefined?
|
---|
131 | if (1 && mDetectEmptyViewSpace) {
|
---|
132 | SetupRay(ray, pointA, -direction);
|
---|
133 | } else
|
---|
134 | SetupRay(ray, viewPoint, -direction);
|
---|
135 |
|
---|
136 | if (!mDetectEmptyViewSpace)
|
---|
137 | ray.mFlags &= ~Ray::CULL_BACKFACES;
|
---|
138 | else
|
---|
139 | ray.mFlags |= Ray::CULL_BACKFACES;
|
---|
140 |
|
---|
141 | if (mKdTree->CastRay(ray))
|
---|
142 | {
|
---|
143 | objectB = ray.intersections[0].mObject;
|
---|
144 | pointB = ray.Extrap(ray.intersections[0].mT);
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | objectB = NULL;
|
---|
149 | float tmin, tmax;
|
---|
150 |
|
---|
151 | if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
|
---|
152 | pointB = ray.Extrap(tmax);
|
---|
153 | else
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | //bool thesame = objectA && (objectA == objectB);
|
---|
158 |
|
---|
159 | // if (objectA == NULL && objectB != NULL) {
|
---|
160 | if (1 && mDetectEmptyViewSpace)
|
---|
161 | {
|
---|
162 | // cast again to ensure that there is no objectA
|
---|
163 | SetupRay(ray, pointB, direction);
|
---|
164 | ray.mFlags |= Ray::CULL_BACKFACES;
|
---|
165 |
|
---|
166 | if (mKdTree->CastRay(ray))
|
---|
167 | {
|
---|
168 | objectA = ray.intersections[0].mObject;
|
---|
169 | pointA = ray.Extrap(ray.intersections[0].mT);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | VssRay *vssRay = NULL;
|
---|
175 |
|
---|
176 | bool validSample = (objectA != objectB);
|
---|
177 |
|
---|
178 | //if (validSample && thesame) Debug << "warning!!" << endl;
|
---|
179 |
|
---|
180 | if (0 && mDetectEmptyViewSpace)
|
---|
181 | {
|
---|
182 | // consider all samples valid
|
---|
183 | // check if the viewpoint lies on the line segment AB
|
---|
184 | if (Distance(pointA, pointB) <
|
---|
185 | Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small)
|
---|
186 | {
|
---|
187 | validSample = false;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (validSample)
|
---|
192 | {
|
---|
193 | if (objectA)
|
---|
194 | {
|
---|
195 | vssRay = new VssRay(pointB,
|
---|
196 | pointA,
|
---|
197 | objectB,
|
---|
198 | objectA,
|
---|
199 | mPass);
|
---|
200 | vssRays.push_back(vssRay);
|
---|
201 | hits ++;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (objectB)
|
---|
205 | {
|
---|
206 | vssRay = new VssRay(pointA,
|
---|
207 | pointB,
|
---|
208 | objectA,
|
---|
209 | objectB,
|
---|
210 | mPass);
|
---|
211 | vssRays.push_back(vssRay);
|
---|
212 | hits ++;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | return hits;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | Vector3
|
---|
221 | VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
|
---|
222 | {
|
---|
223 | AxisAlignedBox3 box;
|
---|
224 |
|
---|
225 | if (viewSpaceBox)
|
---|
226 | box =*viewSpaceBox;
|
---|
227 | else
|
---|
228 | box = mKdTree->GetBox();
|
---|
229 |
|
---|
230 | // shrink the box in the y direction
|
---|
231 | return box.GetRandomPoint();
|
---|
232 | }
|
---|
233 |
|
---|
234 | Vector3
|
---|
235 | VssPreprocessor::GetDirection(const Vector3 &viewpoint,
|
---|
236 | AxisAlignedBox3 *viewSpaceBox
|
---|
237 | )
|
---|
238 | {
|
---|
239 | Vector3 point;
|
---|
240 | if (!use2dSampling)
|
---|
241 | {
|
---|
242 | if (0)
|
---|
243 | {
|
---|
244 | Vector3 normal;
|
---|
245 | int i = Random((int)mObjects.size());
|
---|
246 | Intersectable *object = mObjects[i];
|
---|
247 | object->GetRandomSurfacePoint(point, normal);
|
---|
248 | }
|
---|
249 | else
|
---|
250 | point = mKdTree->GetBox().GetRandomPoint();
|
---|
251 | // point = viewpoint + UniformRandomVector();
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
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 | VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
|
---|
271 | const int desiredSamples,
|
---|
272 | SimpleRayContainer &rays
|
---|
273 | )
|
---|
274 | {
|
---|
275 | int num;
|
---|
276 | if (0) {
|
---|
277 | float minRayContribution;
|
---|
278 | float maxRayContribution;
|
---|
279 | float avgRayContribution;
|
---|
280 |
|
---|
281 | vssTree->GetRayContributionStatistics(minRayContribution,
|
---|
282 | maxRayContribution,
|
---|
283 | avgRayContribution);
|
---|
284 |
|
---|
285 | cout<<
|
---|
286 | "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
|
---|
287 | "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
|
---|
288 | "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
|
---|
289 |
|
---|
290 | float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
|
---|
291 | num = vssTree->GenerateRays(p, rays);
|
---|
292 | } else {
|
---|
293 | int leaves = vssTree->stat.Leaves();
|
---|
294 | num = vssTree->GenerateRays(desiredSamples, leaves, rays);
|
---|
295 | }
|
---|
296 |
|
---|
297 | cout<<"Generated "<<num<<" rays."<<endl;
|
---|
298 |
|
---|
299 | return num;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | bool
|
---|
304 | VssPreprocessor::ExportRays(const char *filename,
|
---|
305 | const VssRayContainer &vssRays,
|
---|
306 | const int number
|
---|
307 | )
|
---|
308 | {
|
---|
309 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
310 |
|
---|
311 | Exporter *exporter = NULL;
|
---|
312 | exporter = Exporter::GetExporter(filename);
|
---|
313 | // exporter->SetWireframe();
|
---|
314 | // exporter->ExportKdTree(*mKdTree);
|
---|
315 | exporter->SetFilled();
|
---|
316 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
317 | exporter->SetWireframe();
|
---|
318 |
|
---|
319 | if (mViewSpaceBox) {
|
---|
320 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
321 | exporter->ExportBox(*mViewSpaceBox);
|
---|
322 | exporter->ResetForcedMaterial();
|
---|
323 | }
|
---|
324 |
|
---|
325 | VssRayContainer rays;
|
---|
326 | vssRays.SelectRays(number, rays);
|
---|
327 |
|
---|
328 | //exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
329 |
|
---|
330 | delete exporter;
|
---|
331 |
|
---|
332 | cout<<"done."<<endl<<flush;
|
---|
333 |
|
---|
334 | return true;
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | bool
|
---|
339 | VssPreprocessor::ExportVssTree(char *filename,
|
---|
340 | VssTree *tree,
|
---|
341 | const Vector3 &dir
|
---|
342 | )
|
---|
343 | {
|
---|
344 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
345 | exporter->SetFilled();
|
---|
346 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
347 | // exporter->SetWireframe();
|
---|
348 | bool result = exporter->ExportVssTree2( *tree, dir );
|
---|
349 | delete exporter;
|
---|
350 | return result;
|
---|
351 | }
|
---|
352 |
|
---|
353 | bool
|
---|
354 | VssPreprocessor::ExportVssTreeLeaf(char *filename,
|
---|
355 | VssTree *tree,
|
---|
356 | VssTreeLeaf *leaf)
|
---|
357 | {
|
---|
358 | Exporter *exporter = NULL;
|
---|
359 | exporter = Exporter::GetExporter(filename);
|
---|
360 | exporter->SetWireframe();
|
---|
361 | exporter->ExportKdTree(*mKdTree);
|
---|
362 |
|
---|
363 | if (mViewSpaceBox) {
|
---|
364 | exporter->SetForcedMaterial(RgbColor(1,0,0));
|
---|
365 | exporter->ExportBox(*mViewSpaceBox);
|
---|
366 | exporter->ResetForcedMaterial();
|
---|
367 | }
|
---|
368 |
|
---|
369 | exporter->SetForcedMaterial(RgbColor(0,0,1));
|
---|
370 | exporter->ExportBox(tree->GetBBox(leaf));
|
---|
371 | exporter->ResetForcedMaterial();
|
---|
372 |
|
---|
373 | VssRayContainer rays[4];
|
---|
374 | for (int i=0; i < leaf->rays.size(); i++) {
|
---|
375 | int k = leaf->rays[i].GetRayClass();
|
---|
376 | rays[k].push_back(leaf->rays[i].mRay);
|
---|
377 | }
|
---|
378 |
|
---|
379 | // SOURCE RAY
|
---|
380 | exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
|
---|
381 | // TERMINATION RAY
|
---|
382 | exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
|
---|
383 | // PASSING_RAY
|
---|
384 | exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
|
---|
385 | // CONTAINED_RAY
|
---|
386 | exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
|
---|
387 |
|
---|
388 | delete exporter;
|
---|
389 | return true;
|
---|
390 | }
|
---|
391 |
|
---|
392 | void
|
---|
393 | VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
|
---|
394 | {
|
---|
395 | vector<VssTreeLeaf *> leaves;
|
---|
396 | tree->CollectLeaves(leaves);
|
---|
397 |
|
---|
398 | int num = 0;
|
---|
399 | int i;
|
---|
400 | float p = number / (float)leaves.size();
|
---|
401 | for (i=0; i < leaves.size(); i++) {
|
---|
402 | if (RandomValue(0,1) < p) {
|
---|
403 | char filename[64];
|
---|
404 | sprintf(filename, "vss-leaf-%04d.x3d", num);
|
---|
405 | ExportVssTreeLeaf(filename, tree, leaves[i]);
|
---|
406 | num++;
|
---|
407 | }
|
---|
408 | if (num >= number)
|
---|
409 | break;
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | void VssPreprocessor::TestBeamCasting(VssTree *tree,
|
---|
415 | ViewCellsManager *vm,
|
---|
416 | const ObjectContainer &objects)
|
---|
417 | {
|
---|
418 | //debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
419 | // renderer->resize(640, 480);
|
---|
420 | //debuggerWidget->resize(640, 480);
|
---|
421 |
|
---|
422 | vector<VssTreeLeaf *> leaves;
|
---|
423 | tree->CollectLeaves(leaves);
|
---|
424 |
|
---|
425 | Exporter *exporter = Exporter::GetExporter("shafts.x3d");
|
---|
426 |
|
---|
427 | exporter->SetWireframe();
|
---|
428 | exporter->ExportGeometry(objects);
|
---|
429 | exporter->SetFilled();
|
---|
430 | //Randomize();
|
---|
431 | debuggerWidget = new GlDebuggerWidget(renderer);
|
---|
432 |
|
---|
433 | /*debuggerWidget->mBeam = beam;
|
---|
434 | debuggerWidget->mSourceObject = sourceObj;
|
---|
435 | debuggerWidget->mSamples = 10000;
|
---|
436 |
|
---|
437 | Debug << "showing window" << endl;
|
---|
438 | debuggerWidget->show();*/
|
---|
439 |
|
---|
440 | renderer->makeCurrent();
|
---|
441 |
|
---|
442 | for (int i = 0; i < 10; ++ i)
|
---|
443 | {
|
---|
444 | Beam beam;
|
---|
445 | Intersectable *sourceObj = mObjects[5];
|
---|
446 |
|
---|
447 | const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
|
---|
448 | VssTreeLeaf *leaf = leaves[index];
|
---|
449 |
|
---|
450 | AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
|
---|
451 | AxisAlignedBox3 box = tree->GetBBox(leaf);
|
---|
452 |
|
---|
453 | beam.Construct(box, dirBox);
|
---|
454 |
|
---|
455 | // collect kd leaves and view cells
|
---|
456 | mKdTree->CastBeam(beam);
|
---|
457 | vm->CastBeam(beam);
|
---|
458 |
|
---|
459 | Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
|
---|
460 | << (int)beam.mKdNodes.size() << " kd nodes" << endl;
|
---|
461 |
|
---|
462 | BeamSampleStatistics stats;
|
---|
463 |
|
---|
464 | renderer->SampleBeamContributions(sourceObj,
|
---|
465 | beam,
|
---|
466 | 200000,
|
---|
467 | stats);
|
---|
468 |
|
---|
469 | char s[64]; sprintf(s, "shaft%04d.png", i);
|
---|
470 |
|
---|
471 | QImage image = renderer->toImage();
|
---|
472 | image.save(s, "PNG");
|
---|
473 | Debug << "beam statistics: " << stats << endl << endl;
|
---|
474 |
|
---|
475 | if (1)
|
---|
476 | {
|
---|
477 | AxisAlignedBox3 sbox = mSceneGraph->GetBox();
|
---|
478 | Vector3 bmin = sbox.Min() - 150.0f;
|
---|
479 | Vector3 bmax = sbox.Max() + 150.0f;
|
---|
480 | AxisAlignedBox3 vbox(bmin, bmax);
|
---|
481 |
|
---|
482 | exporter->ExportBeam(beam, vbox);
|
---|
483 | }
|
---|
484 |
|
---|
485 | bool exportViewCells = false;
|
---|
486 |
|
---|
487 | if (exportViewCells)
|
---|
488 | {
|
---|
489 | ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
|
---|
490 |
|
---|
491 | for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
|
---|
492 | {
|
---|
493 | BspNodeGeometry geom;
|
---|
494 | AxisAlignedBox3 vbox;
|
---|
495 | vbox.Initialize();
|
---|
496 | vbox.Include((*it)->GetMesh());
|
---|
497 |
|
---|
498 | exporter->SetWireframe();
|
---|
499 | exporter->ExportBox(vbox);
|
---|
500 | exporter->SetFilled();
|
---|
501 | exporter->ExportViewCell(*it);
|
---|
502 | }
|
---|
503 |
|
---|
504 | /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
505 |
|
---|
506 | for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
|
---|
507 | {
|
---|
508 | exporter->ExportBox(mKdTree->GetBox((*it)));
|
---|
509 | }*/
|
---|
510 | }
|
---|
511 | }
|
---|
512 | /*while (1)
|
---|
513 | { debuggerWidget->repaint();
|
---|
514 | };*/
|
---|
515 | delete exporter;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | float
|
---|
520 | VssPreprocessor::GetAvgPvsSize(VssTree *tree,
|
---|
521 | const vector<AxisAlignedBox3> &viewcells
|
---|
522 | )
|
---|
523 | {
|
---|
524 | vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
|
---|
525 |
|
---|
526 | int sum = 0;
|
---|
527 | for (it = viewcells.begin(); it != it_end; ++ it)
|
---|
528 | sum += tree->GetPvsSize(*it);
|
---|
529 |
|
---|
530 | return sum/(float)viewcells.size();
|
---|
531 | }
|
---|
532 |
|
---|
533 | bool
|
---|
534 | VssPreprocessor::ComputeVisibility()
|
---|
535 | {
|
---|
536 | Debug << "type: vss" << endl;
|
---|
537 |
|
---|
538 | long startTime = GetTime();
|
---|
539 |
|
---|
540 | int totalSamples = 0;
|
---|
541 |
|
---|
542 |
|
---|
543 | AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
544 |
|
---|
545 | if (!useViewspacePlane) {
|
---|
546 | float size = 0.05f;
|
---|
547 | float s = 0.5f - size;
|
---|
548 | float olds = Magnitude(box->Size());
|
---|
549 | box->Enlarge(box->Size()*Vector3(-s));
|
---|
550 | Vector3 translation = Vector3(-olds*0.1f, 0, 0);
|
---|
551 | box->SetMin(box->Min() + translation);
|
---|
552 | box->SetMax(box->Max() + translation);
|
---|
553 | } else {
|
---|
554 |
|
---|
555 | // sample city like heights
|
---|
556 | box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
|
---|
557 | box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
|
---|
558 | }
|
---|
559 |
|
---|
560 | if (use2dSampling)
|
---|
561 | box->SetMax(1, box->Min(1));
|
---|
562 |
|
---|
563 | cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
|
---|
564 |
|
---|
565 |
|
---|
566 | if (mUseViewSpaceBox)
|
---|
567 | {
|
---|
568 | if (!mEnlargeViewSpace)
|
---|
569 | {
|
---|
570 | mViewSpaceBox = box;
|
---|
571 | }
|
---|
572 | else
|
---|
573 | {
|
---|
574 | // HACK: enlarge in y directon
|
---|
575 | mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
|
---|
576 |
|
---|
577 | if (0) {
|
---|
578 | //Vector3 pmin = mViewSpaceBox->Min();
|
---|
579 | Vector3 size = mViewSpaceBox->Size();
|
---|
580 | //size[1] *= 1.25;
|
---|
581 | Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
|
---|
582 |
|
---|
583 | mViewSpaceBox->Enlarge(enlarge);
|
---|
584 | } else {
|
---|
585 | // $$ JB temporary
|
---|
586 | AxisAlignedBox3 box = *mViewSpaceBox;
|
---|
587 | float s = box.Size(0);
|
---|
588 | box.Scale(0.8f);
|
---|
589 | box.SetMax(0, box.Max(0) + s*0.8f);
|
---|
590 | box.SetMin(0, box.Min(0) + s*0.8f);
|
---|
591 | *mViewSpaceBox = box;
|
---|
592 | }
|
---|
593 |
|
---|
594 | }
|
---|
595 | //Debug << "view space box: " << *mViewSpaceBox << endl;
|
---|
596 | }
|
---|
597 | else
|
---|
598 | {
|
---|
599 | mViewSpaceBox = NULL;
|
---|
600 | }
|
---|
601 |
|
---|
602 | AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
|
---|
603 |
|
---|
604 | mSceneGraph->CollectObjects(&mObjects);
|
---|
605 |
|
---|
606 | //-- load view cells from file if requested
|
---|
607 | if (!mLoadViewCells)
|
---|
608 | {
|
---|
609 | mViewCellsManager->SetViewSpaceBox(vbox);
|
---|
610 | // construct view cells using it's own set of samples
|
---|
611 | mViewCellsManager->Construct(this);
|
---|
612 |
|
---|
613 | //-- several visualizations and statistics
|
---|
614 | Debug << "view cells construction finished: " << endl;
|
---|
615 | mViewCellsManager->PrintStatistics(Debug);
|
---|
616 | }
|
---|
617 | else
|
---|
618 | {
|
---|
619 | VssRayContainer dummies;
|
---|
620 | mViewCellsManager->Visualize(mObjects, dummies);
|
---|
621 | mViewCellsManager->ExportViewCells("test.xml");
|
---|
622 | }
|
---|
623 |
|
---|
624 | VssTree *vssTree = NULL;
|
---|
625 |
|
---|
626 |
|
---|
627 | long initialTime = GetTime();
|
---|
628 |
|
---|
629 | if (mLoadInitialSamples)
|
---|
630 | {
|
---|
631 | cout << "Loading samples from file ... ";
|
---|
632 | LoadSamples(mVssRays, mObjects);
|
---|
633 | cout << "finished\n" << endl;
|
---|
634 | totalSamples = (int)mVssRays.size();
|
---|
635 | }
|
---|
636 | else
|
---|
637 | {
|
---|
638 |
|
---|
639 | while (totalSamples < mInitialSamples) {
|
---|
640 | int passContributingSamples = 0;
|
---|
641 | int passSampleContributions = 0;
|
---|
642 | int passSamples = 0;
|
---|
643 |
|
---|
644 | int index = 0;
|
---|
645 |
|
---|
646 | int sampleContributions;
|
---|
647 |
|
---|
648 | int s = Min(mSamplesPerPass, mInitialSamples);
|
---|
649 | for (int k=0; k < s; k++) {
|
---|
650 | // changed by matt
|
---|
651 | Vector3 viewpoint;
|
---|
652 | // viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
653 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
654 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
655 |
|
---|
656 | sampleContributions = CastRay(viewpoint, direction, mVssRays);
|
---|
657 |
|
---|
658 | if (sampleContributions) {
|
---|
659 | passContributingSamples ++;
|
---|
660 | passSampleContributions += sampleContributions;
|
---|
661 | }
|
---|
662 | passSamples++;
|
---|
663 | totalSamples++;
|
---|
664 | }
|
---|
665 |
|
---|
666 | mPass++;
|
---|
667 | int pvsSize = 0;
|
---|
668 | float avgRayContrib = (passContributingSamples > 0) ?
|
---|
669 | passSampleContributions/(float)passContributingSamples : 0;
|
---|
670 |
|
---|
671 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
672 | cout << "#TotalSamples=" << totalSamples/1000
|
---|
673 | << "#SampleContributions=" << passSampleContributions << " ("
|
---|
674 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
|
---|
675 | << pvsSize/(float)mObjects.size() << endl
|
---|
676 | << "avg ray contrib=" << avgRayContrib << endl;
|
---|
677 |
|
---|
678 | mStats <<
|
---|
679 | "#Pass\n" <<mPass<<endl<<
|
---|
680 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
|
---|
681 | "#TotalSamples\n" << totalSamples<< endl<<
|
---|
682 | "#SampleContributions\n" << passSampleContributions << endl <<
|
---|
683 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
|
---|
684 | "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
|
---|
685 | "#AvgRayContrib\n" << avgRayContrib << endl;
|
---|
686 | }
|
---|
687 |
|
---|
688 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
|
---|
689 |
|
---|
690 |
|
---|
691 |
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
|
---|
696 | Debug << (int)mVssRays.size() << " rays generated in "
|
---|
697 | << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
|
---|
698 |
|
---|
699 | if (mStoreInitialSamples)
|
---|
700 | {
|
---|
701 | cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
|
---|
702 | ExportSamples(mVssRays);
|
---|
703 | cout << "finished\n" << endl;
|
---|
704 |
|
---|
705 | /*VssRayContainer dummyRays;
|
---|
706 | LoadSamples(dummyRays, mObjects);
|
---|
707 | Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
|
---|
708 |
|
---|
709 | for (int i = 0; i < (int)mVssRays.size(); ++ i)
|
---|
710 | {
|
---|
711 | Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
|
---|
712 | Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
|
---|
713 | }*/
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | //int numExportRays = 2000;
|
---|
718 | int numExportRays = 0;
|
---|
719 |
|
---|
720 | if (numExportRays) {
|
---|
721 | char filename[64];
|
---|
722 | sprintf(filename, "vss-rays-initial.x3d");
|
---|
723 | ExportRays(filename, mVssRays, numExportRays);
|
---|
724 | }
|
---|
725 |
|
---|
726 | vssTree = new VssTree;
|
---|
727 | // viewcells = Construct(mVssRays);
|
---|
728 |
|
---|
729 | vssTree->Construct(mVssRays, mViewSpaceBox);
|
---|
730 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
731 |
|
---|
732 | if (0)
|
---|
733 | {
|
---|
734 | ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
|
---|
735 | ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
|
---|
736 | ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
|
---|
737 | ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
|
---|
738 | ExportVssTreeLeaves(vssTree, 10);
|
---|
739 | }
|
---|
740 |
|
---|
741 | // viewcells->UpdatePVS(newVssRays);
|
---|
742 | // get viewcells as kd tree boxes
|
---|
743 | vector<AxisAlignedBox3> kdViewcells;
|
---|
744 | if (0) {
|
---|
745 | vector<KdLeaf *> leaves;
|
---|
746 | mKdTree->CollectLeaves(leaves);
|
---|
747 | vector<KdLeaf *>::const_iterator it;
|
---|
748 | int targetLeaves = 50;
|
---|
749 | float prob = targetLeaves/(float)leaves.size();
|
---|
750 | for (it = leaves.begin(); it != leaves.end(); ++it)
|
---|
751 | if (RandomValue(0.0f,1.0f) < prob)
|
---|
752 | kdViewcells.push_back(mKdTree->GetBox(*it));
|
---|
753 |
|
---|
754 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
755 | cout<<"Initial average PVS size = "<<avgPvs<<endl;
|
---|
756 | }
|
---|
757 |
|
---|
758 |
|
---|
759 | int samples = 0;
|
---|
760 | int pass = 0;
|
---|
761 |
|
---|
762 |
|
---|
763 | // cast view cell samples
|
---|
764 | while (samples < mVssSamples)
|
---|
765 | {
|
---|
766 |
|
---|
767 | int num = mVssSamplesPerPass;
|
---|
768 | SimpleRayContainer rays;
|
---|
769 | VssRayContainer vssRays;
|
---|
770 |
|
---|
771 | if (!mUseImportanceSampling) {
|
---|
772 | for (int j=0; j < num; j++) {
|
---|
773 | // changed by matt
|
---|
774 | //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
|
---|
775 | Vector3 viewpoint;
|
---|
776 | mViewCellsManager->GetViewPoint(viewpoint);
|
---|
777 | Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
|
---|
778 | rays.push_back(SimpleRay(viewpoint, direction));
|
---|
779 | }
|
---|
780 | } else {
|
---|
781 | num = GenerateImportanceRays(vssTree, num, rays);
|
---|
782 | }
|
---|
783 |
|
---|
784 | for (int i=0; i < rays.size(); i++)
|
---|
785 | CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
|
---|
786 |
|
---|
787 | vssTree->AddRays(vssRays);
|
---|
788 |
|
---|
789 | if (0) {
|
---|
790 | int subdivided = vssTree->UpdateSubdivision();
|
---|
791 | cout<<"subdivided leafs = "<<subdivided<<endl;
|
---|
792 | }
|
---|
793 |
|
---|
794 | float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
|
---|
795 | cout<<"Average PVS size = "<<avgPvs<<endl;
|
---|
796 |
|
---|
797 | /// compute view cell contribution of rays
|
---|
798 | mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
799 |
|
---|
800 | if (numExportRays) {
|
---|
801 | char filename[64];
|
---|
802 | if (mUseImportanceSampling)
|
---|
803 | sprintf(filename, "vss-rays-i%04d.x3d", pass);
|
---|
804 | else
|
---|
805 | sprintf(filename, "vss-rays-%04d.x3d", pass);
|
---|
806 |
|
---|
807 | ExportRays(filename, vssRays, numExportRays);
|
---|
808 | }
|
---|
809 |
|
---|
810 | samples+=num;
|
---|
811 | float pvs = vssTree->GetAvgPvsSize();
|
---|
812 | cout<<"*****************************\n";
|
---|
813 | cout<<samples<<" avgPVS ="<<pvs<<endl;
|
---|
814 | cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
|
---|
815 | cout<<"*****************************\n";
|
---|
816 | // if (samples >= mVssSamples) break;
|
---|
817 | pass ++;
|
---|
818 | }
|
---|
819 |
|
---|
820 | if (mTestBeamSampling && mUseGlRenderer)
|
---|
821 | {
|
---|
822 | TestBeamCasting(vssTree, mViewCellsManager, mObjects);
|
---|
823 | }
|
---|
824 |
|
---|
825 | if (0)
|
---|
826 | Debug << vssTree->stat << endl;
|
---|
827 |
|
---|
828 | if (0)
|
---|
829 | {
|
---|
830 | VssRayContainer viewCellRays;
|
---|
831 |
|
---|
832 | // compute rays used for view cells construction
|
---|
833 | const int numRays = mViewCellsManager->GetVisualizationSamples();
|
---|
834 |
|
---|
835 | vssTree->CollectRays(viewCellRays, numRays);
|
---|
836 | }
|
---|
837 |
|
---|
838 | //-- render simulation after merge
|
---|
839 | cout << "\nevaluating bsp view cells render time after sampling ... ";
|
---|
840 | Debug << "\nStatistics after sampling: " << endl;
|
---|
841 |
|
---|
842 | mRenderSimulator->RenderScene();
|
---|
843 | SimulationStatistics ss;
|
---|
844 | mRenderSimulator->GetStatistics(ss);
|
---|
845 |
|
---|
846 | cout << " finished" << endl;
|
---|
847 | cout << ss << endl;
|
---|
848 | Debug << ss << endl;
|
---|
849 |
|
---|
850 | delete vssTree;
|
---|
851 |
|
---|
852 | return true;
|
---|
853 | }
|
---|
854 |
|
---|
855 |
|
---|