1 | #include "SceneGraph.h" |
---|
2 | #include "KdTree.h" |
---|
3 | #include "SamplingPreprocessor.h" |
---|
4 | #include "X3dExporter.h" |
---|
5 | #include "Environment.h" |
---|
6 | #include "MutualVisibility.h" |
---|
7 | #include "Polygon3.h" |
---|
8 | #include "ViewCell.h" |
---|
9 | |
---|
10 | SamplingPreprocessor::SamplingPreprocessor(): mPass(0), mSampleRays(NULL) |
---|
11 | { |
---|
12 | // this should increase coherence of the samples |
---|
13 | environment->GetIntValue("Sampling.samplesPerPass", mSamplesPerPass); |
---|
14 | environment->GetIntValue("Sampling.totalSamples", mTotalSamples); |
---|
15 | environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples); |
---|
16 | |
---|
17 | mKdPvsDepth = 100; |
---|
18 | mStats.open("stats.log"); |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | SamplingPreprocessor::~SamplingPreprocessor() |
---|
23 | { |
---|
24 | CLEAR_CONTAINER(mSampleRays); |
---|
25 | } |
---|
26 | |
---|
27 | void |
---|
28 | SamplingPreprocessor::SetupRay(Ray &ray, |
---|
29 | const Vector3 &point, |
---|
30 | const Vector3 &direction, |
---|
31 | const int type) |
---|
32 | { |
---|
33 | ray.intersections.clear(); |
---|
34 | ray.leaves.clear(); |
---|
35 | ray.meshes.clear(); |
---|
36 | ray.viewCells.clear(); |
---|
37 | |
---|
38 | // cout<<point<<" "<<direction<<endl; |
---|
39 | ray.Init(point, direction, type); |
---|
40 | } |
---|
41 | |
---|
42 | KdNode * |
---|
43 | SamplingPreprocessor::GetNodeForPvs(KdLeaf *leaf) |
---|
44 | { |
---|
45 | KdNode *node = leaf; |
---|
46 | while (node->mParent && node->mDepth > mKdPvsDepth) |
---|
47 | node = node->mParent; |
---|
48 | return node; |
---|
49 | } |
---|
50 | |
---|
51 | bool |
---|
52 | SamplingPreprocessor::BuildBspTree() |
---|
53 | { |
---|
54 | // delete old tree |
---|
55 | DEL_PTR(mBspTree); |
---|
56 | mBspTree = new BspTree(&mUnbounded); |
---|
57 | ObjectContainer objects; |
---|
58 | |
---|
59 | switch (BspTree::sConstructionMethod) |
---|
60 | { |
---|
61 | case BspTree::FROM_INPUT_VIEW_CELLS: |
---|
62 | mBspTree->SetGenerateViewCells(false); |
---|
63 | mBspTree->Construct(mViewCells); |
---|
64 | break; |
---|
65 | case BspTree::FROM_SCENE_GEOMETRY: |
---|
66 | DeleteViewCells(); // we generate new view cells |
---|
67 | mBspTree->SetGenerateViewCells(true); |
---|
68 | mSceneGraph->CollectObjects(&objects); |
---|
69 | mBspTree->Construct(objects); |
---|
70 | |
---|
71 | mBspTree->CollectViewCells(mViewCells); |
---|
72 | break; |
---|
73 | case BspTree::FROM_RAYS: |
---|
74 | DeleteViewCells(); // we generate new view cells |
---|
75 | mBspTree->SetGenerateViewCells(true); |
---|
76 | mBspTree->Construct(mSampleRays); |
---|
77 | |
---|
78 | mBspTree->CollectViewCells(mViewCells); |
---|
79 | break; |
---|
80 | default: |
---|
81 | Debug << "Error: Method not available\n"; |
---|
82 | break; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | int |
---|
90 | SamplingPreprocessor::AddNodeSamples(Intersectable *object, |
---|
91 | const Ray &ray |
---|
92 | ) |
---|
93 | { |
---|
94 | int contributingSamples = 0; |
---|
95 | int j; |
---|
96 | for (j=0; j < ray.leaves.size(); j++) { |
---|
97 | KdNode *node = GetNodeForPvs( ray.leaves[j] ); |
---|
98 | contributingSamples += object->mKdPvs.AddSample(node); |
---|
99 | } |
---|
100 | |
---|
101 | if (mPass > 10) |
---|
102 | for (j=1; j < ((int)ray.leaves.size() - 1); j++) { |
---|
103 | ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); |
---|
104 | } |
---|
105 | |
---|
106 | return contributingSamples; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | int SamplingPreprocessor::AddObjectSamples(Intersectable *obj, const Ray &ray) |
---|
111 | { |
---|
112 | int contributingSamples = 0; |
---|
113 | int j; |
---|
114 | |
---|
115 | // object can be seen from the view cell => add to view cell pvs |
---|
116 | for (j=0; j < ray.viewCells.size(); ++ j) |
---|
117 | { // if ray not in unbounded space |
---|
118 | if (ray.viewCells[j] != &mUnbounded) |
---|
119 | contributingSamples += ray.viewCells[j]->GetPvs().AddSample(obj); |
---|
120 | } |
---|
121 | |
---|
122 | // rays passing through this viewcell |
---|
123 | if (mPass > 1) |
---|
124 | for (j=1; j < ((int)ray.viewCells.size() - 1); ++ j) |
---|
125 | { |
---|
126 | if (ray.viewCells[j] != &mUnbounded) |
---|
127 | ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0); |
---|
128 | } |
---|
129 | |
---|
130 | return contributingSamples; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void |
---|
135 | SamplingPreprocessor::HoleSamplingPass() |
---|
136 | { |
---|
137 | vector<KdLeaf *> leaves; |
---|
138 | mKdTree->CollectLeaves(leaves); |
---|
139 | |
---|
140 | // go through all the leaves and evaluate their passing contribution |
---|
141 | for (int i=0 ; i < leaves.size(); i++) { |
---|
142 | KdLeaf *leaf = leaves[i]; |
---|
143 | cout<<leaf->mPassingRays<<endl; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | int |
---|
149 | SamplingPreprocessor::CastRay(Intersectable *object, |
---|
150 | Ray &ray, |
---|
151 | const bool reverseRay |
---|
152 | ) |
---|
153 | { |
---|
154 | int sampleContributions = 0; |
---|
155 | |
---|
156 | long t1 = GetRealTime(); |
---|
157 | // cast ray to KD tree to find intersection with other objects |
---|
158 | mKdTree->CastRay(ray); |
---|
159 | long t2 = GetRealTime(); |
---|
160 | |
---|
161 | if (0 && object->GetId() > 2197) { |
---|
162 | object->Describe(cout)<<endl; |
---|
163 | cout<<ray<<endl; |
---|
164 | } |
---|
165 | |
---|
166 | if (mViewCellsType == BSP_VIEW_CELLS) |
---|
167 | { |
---|
168 | // cast ray to BSP tree to get intersection with view cells |
---|
169 | if (mBspTree) |
---|
170 | { |
---|
171 | mBspTree->CastRay(ray); |
---|
172 | |
---|
173 | if (!reverseRay) |
---|
174 | sampleContributions += AddObjectSamples(object, ray); |
---|
175 | |
---|
176 | if (!ray.intersections.empty()) // second intersection found |
---|
177 | { |
---|
178 | sampleContributions += |
---|
179 | AddObjectSamples(ray.intersections[0].mObject, ray); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | if (ray.leaves.size()) { |
---|
186 | if (!reverseRay) |
---|
187 | sampleContributions += AddNodeSamples(object, ray); |
---|
188 | |
---|
189 | if (ray.intersections.size()) { |
---|
190 | sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray); |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | return sampleContributions; |
---|
196 | } |
---|
197 | |
---|
198 | // void |
---|
199 | // SamplingPreprocessor::AvsGenerateRandomRay(Ray &ray) |
---|
200 | // { |
---|
201 | // int objId = RandomValue(0, mObjects.size()); |
---|
202 | // Intersectable *object = objects[objId]; |
---|
203 | // object->GetRandomSurfacePoint(point, normal); |
---|
204 | // direction = UniformRandomVector(normal); |
---|
205 | // SetupRay(ray, point, direction); |
---|
206 | // } |
---|
207 | |
---|
208 | // void |
---|
209 | // SamplingPreprocessor::AvsHandleRay(Ray &ray) |
---|
210 | // { |
---|
211 | // int sampleContributions = 0; |
---|
212 | |
---|
213 | // mKdTree->CastRay(ray); |
---|
214 | |
---|
215 | // if (ray.leaves.size()) { |
---|
216 | // sampleContributions += AddNodeSamples(object, ray, pass); |
---|
217 | |
---|
218 | // if (ray.intersections.size()) { |
---|
219 | // sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass); |
---|
220 | // } |
---|
221 | // } |
---|
222 | // } |
---|
223 | |
---|
224 | // void |
---|
225 | // SamplingPreprocessor::AvsBorderSampling(Ray &ray) |
---|
226 | // { |
---|
227 | |
---|
228 | |
---|
229 | // } |
---|
230 | |
---|
231 | // void |
---|
232 | // SamplingPreprocessor::AvsPass() |
---|
233 | // { |
---|
234 | // Ray ray; |
---|
235 | // while (1) { |
---|
236 | // AvsGenerateRay(ray); |
---|
237 | // HandleRay(ray); |
---|
238 | // while ( !mRayQueue.empty() ) { |
---|
239 | // Ray ray = mRayQueue.pop(); |
---|
240 | // mRayQueue.pop(); |
---|
241 | // AdaptiveBorderSampling(ray); |
---|
242 | // } |
---|
243 | // } |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | // } |
---|
248 | |
---|
249 | |
---|
250 | int |
---|
251 | SamplingPreprocessor::CastEdgeSamples( |
---|
252 | Intersectable *object, |
---|
253 | const Vector3 &point, |
---|
254 | MeshInstance *mi, |
---|
255 | const int samples |
---|
256 | ) |
---|
257 | { |
---|
258 | Ray ray; |
---|
259 | int maxTries = samples*10; |
---|
260 | int i; |
---|
261 | int rays = 0; |
---|
262 | int edgeSamplesContributions = 0; |
---|
263 | for (i=0; i < maxTries && rays < samples; i++) { |
---|
264 | // pickup a random face of each mesh |
---|
265 | Mesh *mesh = mi->GetMesh(); |
---|
266 | int face = RandomValue(0, mesh->mFaces.size()-1); |
---|
267 | |
---|
268 | Polygon3 poly(mesh->mFaces[face], mesh); |
---|
269 | poly.Scale(1.001); |
---|
270 | // now extend a random edge of the face |
---|
271 | int edge = RandomValue(0, poly.mVertices.size()-1); |
---|
272 | float t = RandomValue(0.0f,1.0f); |
---|
273 | Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)% |
---|
274 | poly.mVertices.size()]; |
---|
275 | SetupRay(ray, point, target - point, Ray::LOCAL_RAY); |
---|
276 | if (!mesh->CastRay(ray, mi)) { |
---|
277 | // the rays which intersect the mesh have been discarded since they are not tangent |
---|
278 | // to the mesh |
---|
279 | rays++; |
---|
280 | edgeSamplesContributions += CastRay(object, ray, false); |
---|
281 | } |
---|
282 | } |
---|
283 | return edgeSamplesContributions; |
---|
284 | } |
---|
285 | |
---|
286 | KdNode * |
---|
287 | SamplingPreprocessor::GetNodeToSample(Intersectable *object) |
---|
288 | { |
---|
289 | int pvsSize = object->mKdPvs.GetSize(); |
---|
290 | KdNode *nodeToSample = NULL; |
---|
291 | |
---|
292 | bool samplePvsBoundary = false; |
---|
293 | if (pvsSize && samplePvsBoundary) { |
---|
294 | // this samples the nodes from the boundary of the current PVS |
---|
295 | // mail all nodes from the pvs |
---|
296 | Intersectable::NewMail(); |
---|
297 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
298 | |
---|
299 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
300 | KdNode *node = (*i).first; |
---|
301 | node->Mail(); |
---|
302 | } |
---|
303 | |
---|
304 | int maxTries = 2*pvsSize; |
---|
305 | Debug << "Finding random neighbour" << endl; |
---|
306 | for (int tries = 0; tries < 10; tries++) { |
---|
307 | int index = RandomValue(0, pvsSize - 1); |
---|
308 | KdPvsData data; |
---|
309 | KdNode *node; |
---|
310 | object->mKdPvs.GetData(index, node, data); |
---|
311 | nodeToSample = mKdTree->FindRandomNeighbor(node, true); |
---|
312 | if (nodeToSample) |
---|
313 | break; |
---|
314 | } |
---|
315 | } else { |
---|
316 | // just pickup a random node |
---|
317 | // nodeToSample = mKdTree->GetRandomLeaf(Plane3(normal, point)); |
---|
318 | nodeToSample = mKdTree->GetRandomLeaf(); |
---|
319 | } |
---|
320 | return nodeToSample; |
---|
321 | } |
---|
322 | |
---|
323 | void |
---|
324 | SamplingPreprocessor::VerifyVisibility(Intersectable *object) |
---|
325 | { |
---|
326 | // mail all nodes from the pvs |
---|
327 | Intersectable::NewMail(); |
---|
328 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
329 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
330 | KdNode *node = (*i).first; |
---|
331 | node->Mail(); |
---|
332 | } |
---|
333 | Debug << "Get all neighbours from PVS" << endl; |
---|
334 | vector<KdNode *> invisibleNeighbors; |
---|
335 | // get all neighbors of all PVS nodes |
---|
336 | i = object->mKdPvs.mEntries.begin(); |
---|
337 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
338 | KdNode *node = (*i).first; |
---|
339 | mKdTree->FindNeighbors(node, invisibleNeighbors, true); |
---|
340 | AxisAlignedBox3 box = object->GetBox(); |
---|
341 | for (int j=0; j < invisibleNeighbors.size(); j++) { |
---|
342 | int visibility = ComputeBoxVisibility(mSceneGraph, |
---|
343 | mKdTree, |
---|
344 | box, |
---|
345 | mKdTree->GetBox(invisibleNeighbors[j]), |
---|
346 | 1e-6f); |
---|
347 | // exit(0); |
---|
348 | } |
---|
349 | // now rank all the neighbors according to probability that a new |
---|
350 | // sample creates some contribution |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | bool |
---|
355 | SamplingPreprocessor::ComputeVisibility() |
---|
356 | { |
---|
357 | |
---|
358 | // pickup an object |
---|
359 | ObjectContainer objects; |
---|
360 | |
---|
361 | mSceneGraph->CollectObjects(&objects); |
---|
362 | |
---|
363 | Vector3 point, normal, direction; |
---|
364 | Ray ray; |
---|
365 | |
---|
366 | long startTime = GetTime(); |
---|
367 | |
---|
368 | int i; |
---|
369 | int totalSamples = 0; |
---|
370 | |
---|
371 | int pvsOut = Min((int)objects.size(), 10); |
---|
372 | |
---|
373 | vector<Ray> rays[10]; |
---|
374 | |
---|
375 | vector<Ray> vcRays[5]; |
---|
376 | ViewCellContainer pvsViewCells; |
---|
377 | vector<Ray> viewCellRays; // used for BSP tree construction |
---|
378 | |
---|
379 | while (totalSamples < mTotalSamples) { |
---|
380 | int passContributingSamples = 0; |
---|
381 | int passSampleContributions = 0; |
---|
382 | int passSamples = 0; |
---|
383 | int index = 0; |
---|
384 | Real maxTime = 0; |
---|
385 | int maxTimeIdx = 0; |
---|
386 | int reverseSamples = 0; |
---|
387 | bool collectSamplesForBsp = |
---|
388 | (mViewCellsType == BSP_VIEW_CELLS) && |
---|
389 | (BspTree::sConstructionMethod == BspTree::FROM_RAYS) && |
---|
390 | (totalSamples < mBspConstructionSamples); |
---|
391 | |
---|
392 | cout << "totalSamples: " << totalSamples << endl; |
---|
393 | |
---|
394 | for (i = 0; i < objects.size(); i++) { |
---|
395 | |
---|
396 | KdNode *nodeToSample = NULL; |
---|
397 | Intersectable *object = objects[i]; |
---|
398 | |
---|
399 | int pvsSize = 0; |
---|
400 | if (mViewCellsType == KD_VIEW_CELLS) |
---|
401 | pvsSize = object->mKdPvs.GetSize(); |
---|
402 | |
---|
403 | |
---|
404 | if (0 && pvsSize && mPass == 1000 ) { |
---|
405 | VerifyVisibility(object); |
---|
406 | } |
---|
407 | |
---|
408 | int faceIndex = object->GetRandomSurfacePoint(point, normal); |
---|
409 | |
---|
410 | long samplesPerObjStart = GetTime(); |
---|
411 | |
---|
412 | bool viewcellSample = true; |
---|
413 | int sampleContributions; |
---|
414 | bool debug = false; //(object->GetId() >= 2199); |
---|
415 | if (viewcellSample) { |
---|
416 | //mKdTree->GetRandomLeaf(Plane3(normal, point)); |
---|
417 | |
---|
418 | |
---|
419 | for (int k=0; k < mSamplesPerPass; k++) { |
---|
420 | bool reverseSample = false; |
---|
421 | |
---|
422 | nodeToSample = GetNodeToSample(object); |
---|
423 | |
---|
424 | if (nodeToSample) { |
---|
425 | AxisAlignedBox3 box = mKdTree->GetBox(nodeToSample); |
---|
426 | Vector3 pointToSample = box.GetRandomPoint(); |
---|
427 | // pointToSample.y = 0.9*box.Min().y + 0.1*box.Max().y; |
---|
428 | if (object->GetRandomVisibleSurfacePoint( point, normal, pointToSample, 3 )) { |
---|
429 | direction = pointToSample - point; |
---|
430 | } else { |
---|
431 | reverseSamples++; |
---|
432 | reverseSample = true; |
---|
433 | direction = point - pointToSample; |
---|
434 | point = pointToSample; |
---|
435 | } |
---|
436 | } |
---|
437 | else { |
---|
438 | direction = UniformRandomVector(normal); |
---|
439 | } |
---|
440 | |
---|
441 | // construct a ray |
---|
442 | SetupRay(ray, point, direction, Ray::LOCAL_RAY); |
---|
443 | |
---|
444 | sampleContributions = CastRay(object, ray, reverseSample); |
---|
445 | |
---|
446 | //-- CORR matt: put block inside loop |
---|
447 | if (sampleContributions) { |
---|
448 | passContributingSamples ++; |
---|
449 | passSampleContributions += sampleContributions; |
---|
450 | } |
---|
451 | |
---|
452 | if ( i < pvsOut ) |
---|
453 | rays[i].push_back(ray); |
---|
454 | |
---|
455 | if (!ray.intersections.empty()) { |
---|
456 | // check whether we can add this to the rays |
---|
457 | for (int j = 0; j < pvsOut; j++) { |
---|
458 | if (objects[j] == ray.intersections[0].mObject) { |
---|
459 | rays[j].push_back(ray); |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | //------------------- |
---|
464 | |
---|
465 | if (mViewCellsType == BSP_VIEW_CELLS) |
---|
466 | { |
---|
467 | // save rays for bsp tree construction |
---|
468 | if (collectSamplesForBsp) |
---|
469 | { |
---|
470 | // also add origin to sample in order to extract it as input polygons |
---|
471 | MeshInstance *mi = dynamic_cast<MeshInstance *>(object); |
---|
472 | ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex); |
---|
473 | |
---|
474 | mSampleRays.push_back(new Ray(ray)); |
---|
475 | } |
---|
476 | else |
---|
477 | { |
---|
478 | // construct BSP tree using the samples |
---|
479 | if (!mBspTree) |
---|
480 | { |
---|
481 | BuildBspTree(); |
---|
482 | |
---|
483 | cout << "generated " << (int)mViewCells.size() << " view cells" << endl; |
---|
484 | |
---|
485 | passContributingSamples += mBspTree->GetStat().contributingSamples; |
---|
486 | passSampleContributions += mBspTree->GetStat().sampleContributions; |
---|
487 | |
---|
488 | BspTreeStatistics(Debug); |
---|
489 | Export("vc_bsptree.x3d", false, false, true); |
---|
490 | } |
---|
491 | |
---|
492 | // some random view cells for output |
---|
493 | if (pvsViewCells.empty()) |
---|
494 | { |
---|
495 | int vcPvsOut = Min((int)mViewCells.size(), 5); |
---|
496 | |
---|
497 | for (int j = 0; j < vcPvsOut; ++ j) |
---|
498 | { |
---|
499 | int idx = Random((int)mViewCells.size()); |
---|
500 | Debug << "output view cell=" << idx << endl; |
---|
501 | pvsViewCells.push_back(mViewCells[Random((int)mViewCells.size())]); |
---|
502 | } |
---|
503 | } |
---|
504 | else |
---|
505 | { |
---|
506 | // check whether we can add the current ray to the rays |
---|
507 | for (int k = 0; k < (int)ray.viewCells.size(); ++ k) |
---|
508 | for (int j = 0; j < (int)pvsViewCells.size(); ++ j) |
---|
509 | if (pvsViewCells[j] == ray.viewCells[k]) |
---|
510 | vcRays[j].push_back(ray); |
---|
511 | } |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | } |
---|
516 | } else { |
---|
517 | // edge samples |
---|
518 | // get random visible mesh |
---|
519 | // object->GetRandomVisibleMesh(Plane3(normal, point)); |
---|
520 | } |
---|
521 | |
---|
522 | // measure maximal time for samples per object |
---|
523 | Real t = TimeDiff(samplesPerObjStart, GetTime()); |
---|
524 | |
---|
525 | if (t > maxTime) |
---|
526 | { |
---|
527 | maxTime = t; |
---|
528 | maxTimeIdx = i; |
---|
529 | } |
---|
530 | |
---|
531 | // CORR matt: must add all samples |
---|
532 | passSamples += mSamplesPerPass; |
---|
533 | } |
---|
534 | |
---|
535 | totalSamples += passSamples; |
---|
536 | |
---|
537 | // if (pass>10) |
---|
538 | // HoleSamplingPass(); |
---|
539 | |
---|
540 | mPass++; |
---|
541 | |
---|
542 | int pvsSize = 0; |
---|
543 | |
---|
544 | if (mViewCellsType == BSP_VIEW_CELLS) { |
---|
545 | for (i=0; i < mViewCells.size(); i++) { |
---|
546 | ViewCell *vc = mViewCells[i]; |
---|
547 | pvsSize += vc->GetPvs().GetSize(); |
---|
548 | } |
---|
549 | } else { |
---|
550 | for (i=0; i < objects.size(); i++) { |
---|
551 | Intersectable *object = objects[i]; |
---|
552 | pvsSize += object->mKdPvs.GetSize(); |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
556 | Debug << "maximal time needed for pass: " << maxTime << " (object " << maxTimeIdx << ")" << endl; |
---|
557 | |
---|
558 | float avgRayContrib = (passContributingSamples > 0) ? |
---|
559 | passSampleContributions/(float)passContributingSamples : 0; |
---|
560 | |
---|
561 | cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; |
---|
562 | cout << "#TotalSamples=" << totalSamples/1000 |
---|
563 | << "k #SampleContributions=" << passSampleContributions << " (" |
---|
564 | << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" |
---|
565 | << pvsSize/(float)objects.size() << endl |
---|
566 | << "avg ray contrib=" << avgRayContrib << endl |
---|
567 | << "reverse samples [%]" << reverseSamples/(float)passSamples*100.0f << endl; |
---|
568 | |
---|
569 | mStats << |
---|
570 | "#Pass\n" <<mPass<<endl<< |
---|
571 | "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< |
---|
572 | "#TotalSamples\n" << totalSamples<< endl<< |
---|
573 | "#SampleContributions\n" << passSampleContributions << endl << |
---|
574 | "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << |
---|
575 | "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl << |
---|
576 | "#AvgRayContrib\n" << avgRayContrib << endl; |
---|
577 | } |
---|
578 | |
---|
579 | if (mViewCellsType == KD_VIEW_CELLS) |
---|
580 | cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; |
---|
581 | |
---|
582 | // HoleSamplingPass(); |
---|
583 | if (0) { |
---|
584 | Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); |
---|
585 | exporter->SetExportRayDensity(true); |
---|
586 | exporter->ExportKdTree(*mKdTree); |
---|
587 | |
---|
588 | if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS)) |
---|
589 | exporter->ExportBspTree(*mBspTree); |
---|
590 | |
---|
591 | delete exporter; |
---|
592 | } |
---|
593 | |
---|
594 | bool exportRays = false; |
---|
595 | if (exportRays) { |
---|
596 | Exporter *exporter = NULL; |
---|
597 | exporter = Exporter::GetExporter("sample-rays.x3d"); |
---|
598 | exporter->SetWireframe(); |
---|
599 | exporter->ExportKdTree(*mKdTree); |
---|
600 | exporter->ExportBspTree(*mBspTree); |
---|
601 | |
---|
602 | for (i=0; i < pvsOut; i++) |
---|
603 | exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); |
---|
604 | exporter->SetFilled(); |
---|
605 | |
---|
606 | delete exporter; |
---|
607 | } |
---|
608 | |
---|
609 | //-- several visualizations and statistics |
---|
610 | if (1) { |
---|
611 | if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS)) |
---|
612 | { |
---|
613 | bool exportSplits = false; |
---|
614 | environment->GetBoolValue("BspTree.exportSplits", exportSplits); |
---|
615 | |
---|
616 | // export the bsp splits |
---|
617 | if (exportSplits)
|
---|
618 | ExportSplits(objects);
|
---|
619 | |
---|
620 | Exporter *exporter = Exporter::GetExporter("viewCells.x3d"); |
---|
621 | |
---|
622 | if (exporter) |
---|
623 | { |
---|
624 | //exporter->ExportLeavesGeometry(mBspTree, leaves); |
---|
625 | |
---|
626 | delete exporter; |
---|
627 | } |
---|
628 | |
---|
629 | for (int j = 0; j < pvsViewCells.size(); ++ j) |
---|
630 | { |
---|
631 | ViewCell *vc = pvsViewCells[j]; |
---|
632 | |
---|
633 | Intersectable::NewMail(); |
---|
634 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", j); |
---|
635 | |
---|
636 | Exporter *exporter = Exporter::GetExporter(s); |
---|
637 | exporter->SetFilled(); |
---|
638 | |
---|
639 | ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin(); |
---|
640 | |
---|
641 | Material m;//= RandomMaterial(); |
---|
642 | m.mDiffuseColor = RgbColor(0, 1, 0); |
---|
643 | exporter->SetForcedMaterial(m); |
---|
644 | |
---|
645 | exporter->ExportViewCell(vc); |
---|
646 | |
---|
647 | Debug << j << ": pvs size=" << (int)vc->GetPvs().GetSize() |
---|
648 | << ", piercing rays=" << (int)vcRays[j].size() << endl; |
---|
649 | |
---|
650 | exporter->SetWireframe(); |
---|
651 | |
---|
652 | // export view cells |
---|
653 | m.mDiffuseColor = RgbColor(1, 0, 1); |
---|
654 | exporter->SetForcedMaterial(m); |
---|
655 | exporter->ExportViewCells(mViewCells); |
---|
656 | |
---|
657 | // export rays piercing this view cell |
---|
658 | exporter->ExportRays(vcRays[j], 1000, RgbColor(0, 1, 0)); |
---|
659 | |
---|
660 | m.mDiffuseColor = RgbColor(1, 0, 0); |
---|
661 | exporter->SetForcedMaterial(m); |
---|
662 | |
---|
663 | // output PVS of view cell |
---|
664 | for (; it != vc->GetPvs().mEntries.end(); ++ it) |
---|
665 | { |
---|
666 | Intersectable *intersect = (*it).first; |
---|
667 | if (!intersect->Mailed()) |
---|
668 | { |
---|
669 | exporter->ExportIntersectable(intersect); |
---|
670 | intersect->Mail(); |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | // output rest of the objects |
---|
675 | if (0) |
---|
676 | { |
---|
677 | Material m;//= RandomMaterial(); |
---|
678 | m.mDiffuseColor = RgbColor(0, 0, 1); |
---|
679 | exporter->SetForcedMaterial(m); |
---|
680 | |
---|
681 | for (int j = 0; j < objects.size(); ++ j) |
---|
682 | if (!objects[j]->Mailed()) |
---|
683 | { |
---|
684 | //if (j == 2198)m.mDiffuseColor = RgbColor(1, 0, 1); |
---|
685 | //else m.mDiffuseColor = RgbColor(1, 1, 0); |
---|
686 | exporter->SetForcedMaterial(m); |
---|
687 | exporter->ExportIntersectable(objects[j]); |
---|
688 | objects[j]->Mail(); |
---|
689 | } |
---|
690 | } |
---|
691 | DEL_PTR(exporter); |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | for (int k=0; k < pvsOut; k++) { |
---|
696 | Intersectable *object = objects[k]; |
---|
697 | char s[64]; |
---|
698 | sprintf(s, "sample-pvs%04d.x3d", k); |
---|
699 | Exporter *exporter = Exporter::GetExporter(s); |
---|
700 | exporter->SetWireframe(); |
---|
701 | |
---|
702 | |
---|
703 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
704 | Intersectable::NewMail(); |
---|
705 | |
---|
706 | // avoid adding the object to the list |
---|
707 | object->Mail(); |
---|
708 | ObjectContainer visibleObjects; |
---|
709 | |
---|
710 | for (; i != object->mKdPvs.mEntries.end(); i++) |
---|
711 | { |
---|
712 | KdNode *node = (*i).first; |
---|
713 | exporter->ExportBox(mKdTree->GetBox(node)); |
---|
714 | mKdTree->CollectObjects(node, visibleObjects); |
---|
715 | } |
---|
716 | |
---|
717 | exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0)); |
---|
718 | exporter->SetFilled(); |
---|
719 | |
---|
720 | for (int j = 0; j < visibleObjects.size(); j++) |
---|
721 | exporter->ExportIntersectable(visibleObjects[j]); |
---|
722 | |
---|
723 | |
---|
724 | Material m; |
---|
725 | m.mDiffuseColor = RgbColor(1, 0, 0); |
---|
726 | exporter->SetForcedMaterial(m); |
---|
727 | exporter->ExportIntersectable(object); |
---|
728 | |
---|
729 | delete exporter; |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | return true; |
---|
734 | } |
---|
735 | |
---|
736 |
|
---|
737 | void SamplingPreprocessor::ExportSplits(const ObjectContainer &objects)
|
---|
738 | {
|
---|
739 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
740 | |
---|
741 | if (exporter) |
---|
742 | { |
---|
743 | cout << "exporting splits ... "; |
---|
744 | |
---|
745 | Material m;
|
---|
746 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
747 | exporter->SetForcedMaterial(m);
|
---|
748 | exporter->SetWireframe();
|
---|
749 | exporter->ExportBspSplits(*mBspTree);
|
---|
750 | |
---|
751 | // take forced material, else big scenes cannot be viewed
|
---|
752 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
753 | exporter->SetForcedMaterial(m);
|
---|
754 | exporter->SetFilled();
|
---|
755 |
|
---|
756 | exporter->ResetForcedMaterial();
|
---|
757 |
|
---|
758 | // export rays
|
---|
759 | if (0)
|
---|
760 | {
|
---|
761 | RayContainer outRays;
|
---|
762 |
|
---|
763 | for (int i = 0; i < mSampleRays.size(); ++ i)
|
---|
764 | {
|
---|
765 | // only rays piercing geometry
|
---|
766 | if (!mSampleRays[i]->intersections.empty())
|
---|
767 | outRays.push_back(mSampleRays[i]);
|
---|
768 | }
|
---|
769 | if (BspTree::sConstructionMethod == BspTree::FROM_RAYS)
|
---|
770 | {
|
---|
771 | // export rays
|
---|
772 | exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | // export scene geometry
|
---|
777 | if (0)
|
---|
778 | {
|
---|
779 | Material m;//= RandomMaterial(); |
---|
780 | m.mDiffuseColor = RgbColor(0, 1, 0); |
---|
781 | exporter->SetForcedMaterial(m); |
---|
782 | exporter->SetWireframe(); |
---|
783 | |
---|
784 | for (int j = 0; j < objects.size(); ++ j) |
---|
785 | exporter->ExportIntersectable(objects[j]); |
---|
786 | } |
---|
787 | |
---|
788 | delete exporter; |
---|
789 | |
---|
790 | cout << "finished" << endl; |
---|
791 | } |
---|
792 | } |
---|