1 |
|
---|
2 | #include "SamplingStrategy.h"
|
---|
3 | #include "Ray.h"
|
---|
4 | #include "Intersectable.h"
|
---|
5 | #include "Preprocessor.h"
|
---|
6 | #include "ViewCellsManager.h"
|
---|
7 | #include "AxisAlignedBox3.h"
|
---|
8 | #include "RssTree.h"
|
---|
9 | #include "Mutation.h"
|
---|
10 | #include "FilterBasedDistribution.h"
|
---|
11 | #include "DifferenceSampling.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | #ifdef USE_PERFTIMER
|
---|
15 | #include "Timer/PerfTimer.h"
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | namespace GtpVisibilityPreprocessor {
|
---|
19 |
|
---|
20 |
|
---|
21 | #ifdef USE_PERFTIMER
|
---|
22 | extern PerfTimer haltonTimer;
|
---|
23 | extern PerfTimer pvsTimer;
|
---|
24 | extern PerfTimer viewCellCastTimer;
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | //HaltonSequence SamplingStrategy::sHalton;
|
---|
28 |
|
---|
29 | HaltonSequence ObjectBasedDistribution::sHalton;
|
---|
30 | HaltonSequence MixtureDistribution::sHalton;
|
---|
31 | HaltonSequence GlobalLinesDistribution::sHalton;
|
---|
32 | HaltonSequence SpatialBoxBasedDistribution::sHalton;
|
---|
33 | HaltonSequence ObjectDirectionBasedDistribution::sHalton;
|
---|
34 | HaltonSequence DirectionBasedDistribution::sHalton;
|
---|
35 | HaltonSequence HwGlobalLinesDistribution::sHalton;
|
---|
36 |
|
---|
37 | HaltonSequence ViewCellBasedDistribution::sHalton;
|
---|
38 |
|
---|
39 | SamplingStrategy::SamplingStrategy(Preprocessor &preprocessor):
|
---|
40 | mPreprocessor(preprocessor),
|
---|
41 | mRatio(1.0f),
|
---|
42 | mTotalRays(0),
|
---|
43 | mTotalContribution(0.0f)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | SamplingStrategy::~SamplingStrategy()
|
---|
49 | {
|
---|
50 | }
|
---|
51 |
|
---|
52 | int
|
---|
53 | SamplingStrategy::GenerateSamples(const int number,
|
---|
54 | SimpleRayContainer &rays)
|
---|
55 | {
|
---|
56 | SimpleRay ray;
|
---|
57 | int samples = 0;
|
---|
58 | int i = 0;
|
---|
59 | const int maxTries = 20;
|
---|
60 | // tmp changed matt. Q: should one rejected sample
|
---|
61 | // terminate the whole method?
|
---|
62 | for (; i < number; i++)
|
---|
63 | {
|
---|
64 | int j = 0;
|
---|
65 | bool sampleGenerated = false;
|
---|
66 |
|
---|
67 | for (j = 0; !sampleGenerated && (j < maxTries); ++ j)
|
---|
68 | {
|
---|
69 | sampleGenerated = GenerateSample(ray);
|
---|
70 |
|
---|
71 | if (sampleGenerated)
|
---|
72 | {
|
---|
73 | ++ samples;
|
---|
74 | rays.push_back(ray);
|
---|
75 | //cout << "d " << ray.mDirection << " ";
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | return samples;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /*********************************************************************/
|
---|
85 | /* Individual sampling strategies implementation */
|
---|
86 | /*********************************************************************/
|
---|
87 |
|
---|
88 |
|
---|
89 | bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
90 | {
|
---|
91 | Vector3 origin, direction;
|
---|
92 |
|
---|
93 | float r[5];
|
---|
94 | sHalton.GetNext(5, r);
|
---|
95 |
|
---|
96 | mPreprocessor.mViewCellsManager->GetViewPoint(origin,
|
---|
97 | Vector3(r[2],r[3],r[4]));
|
---|
98 |
|
---|
99 |
|
---|
100 | Vector3 point, normal;
|
---|
101 |
|
---|
102 | r[0] *= (float)mPreprocessor.mObjects.size() - 1;
|
---|
103 | const int i = (int)r[0];
|
---|
104 |
|
---|
105 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
106 |
|
---|
107 | // take the remainder as a parameter over objects surface
|
---|
108 | r[0] -= (float)i;
|
---|
109 |
|
---|
110 | object->GetRandomSurfacePoint(r[0], r[1], point, normal);
|
---|
111 |
|
---|
112 | direction = point - origin;
|
---|
113 |
|
---|
114 | const float c = Magnitude(direction);
|
---|
115 |
|
---|
116 | if (c <= Limits::Small)
|
---|
117 | return false;
|
---|
118 |
|
---|
119 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
120 | const float pdf = 1.0f;
|
---|
121 |
|
---|
122 | direction *= 1.0f / c;
|
---|
123 | ray = SimpleRay(origin, direction, OBJECT_BASED_DISTRIBUTION, pdf);
|
---|
124 |
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | bool
|
---|
130 | ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
131 | {
|
---|
132 | Vector3 origin, direction;
|
---|
133 |
|
---|
134 |
|
---|
135 | float r[4];
|
---|
136 | sHalton.GetNext(4, r);
|
---|
137 |
|
---|
138 | // static Halton<4> halton;
|
---|
139 | // halton.GetNext(r);
|
---|
140 |
|
---|
141 | r[0] *= (float)mPreprocessor.mObjects.size() - 1;
|
---|
142 | const int i = (int)r[0];
|
---|
143 |
|
---|
144 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
145 |
|
---|
146 | // take the remainder as a parameter over objects surface
|
---|
147 | r[0] -= (float)i;
|
---|
148 |
|
---|
149 | Vector3 normal;
|
---|
150 |
|
---|
151 | object->GetRandomSurfacePoint(r[0], r[1], origin, normal);
|
---|
152 |
|
---|
153 | direction = Normalize(CosineRandomVector(r[2], r[3], normal));
|
---|
154 |
|
---|
155 | origin += 1e-2f*direction;
|
---|
156 |
|
---|
157 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
158 | const float pdf = 1.0f;
|
---|
159 |
|
---|
160 | ray = SimpleRay(origin, direction, OBJECT_DIRECTION_BASED_DISTRIBUTION, pdf);
|
---|
161 |
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
167 | {
|
---|
168 |
|
---|
169 | float r[5];
|
---|
170 | sHalton.GetNext(5, r);
|
---|
171 |
|
---|
172 | Vector3 origin, direction;
|
---|
173 | mPreprocessor.mViewCellsManager->GetViewPoint(origin,
|
---|
174 | Vector3(r[2],r[3],r[4])
|
---|
175 | );
|
---|
176 |
|
---|
177 | direction = UniformRandomVector(r[0], r[1]);
|
---|
178 | const float c = Magnitude(direction);
|
---|
179 |
|
---|
180 | if (c <= Limits::Small)
|
---|
181 | return false;
|
---|
182 |
|
---|
183 | const float pdf = 1.0f;
|
---|
184 |
|
---|
185 | direction *= 1.0f / c;
|
---|
186 | ray = SimpleRay(origin, direction, DIRECTION_BASED_DISTRIBUTION, pdf);
|
---|
187 |
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
193 | {
|
---|
194 | Vector3 origin, direction;
|
---|
195 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
196 |
|
---|
197 | const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
|
---|
198 | const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
|
---|
199 |
|
---|
200 | direction = VssRay::GetDirection(alpha, beta);
|
---|
201 |
|
---|
202 | const float c = Magnitude(direction);
|
---|
203 |
|
---|
204 | if (c <= Limits::Small)
|
---|
205 | return false;
|
---|
206 |
|
---|
207 | const float pdf = 1.0f;
|
---|
208 |
|
---|
209 | direction *= 1.0f / c;
|
---|
210 | ray = SimpleRay(origin, direction, DIRECTION_BOX_BASED_DISTRIBUTION, pdf);
|
---|
211 |
|
---|
212 | return true;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | bool
|
---|
217 | SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
218 | {
|
---|
219 |
|
---|
220 | Vector3 origin, direction;
|
---|
221 |
|
---|
222 | float r[6];
|
---|
223 | sHalton.GetNext(6, r);
|
---|
224 |
|
---|
225 | // mPreprocessor.mViewCellsManager->GetViewPoint(origin, Vector3(r[0],
|
---|
226 | // r[1],
|
---|
227 | // r[2]));
|
---|
228 |
|
---|
229 | origin = mPreprocessor.sceneBox.GetRandomPoint(Vector3(r[0],
|
---|
230 | r[1],
|
---|
231 | r[2]));
|
---|
232 |
|
---|
233 | direction = mPreprocessor.sceneBox.GetRandomPoint(Vector3(r[3],
|
---|
234 | r[4],
|
---|
235 | r[5])) - origin;
|
---|
236 |
|
---|
237 | // cout<<mPreprocessor.sceneBox<<endl;
|
---|
238 | // cout<<r[0]<<" "<<r[1]<<" "<<r[2]<<" "<<r[3]<<" "<<r[4]<<" "<<r[5]<<endl;
|
---|
239 | // cout << " o: " << origin << "d " << direction << endl;
|
---|
240 |
|
---|
241 | const float c = Magnitude(direction);
|
---|
242 |
|
---|
243 | if (c <= Limits::Small)
|
---|
244 | return false;
|
---|
245 |
|
---|
246 | const float pdf = 1.0f;
|
---|
247 |
|
---|
248 | direction *= 1.0f / c;
|
---|
249 | ray = SimpleRay(origin, direction, SPATIAL_BOX_BASED_DISTRIBUTION, pdf);
|
---|
250 |
|
---|
251 |
|
---|
252 | return true;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | bool ReverseObjectBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
257 | {
|
---|
258 | Vector3 origin, direction;
|
---|
259 |
|
---|
260 | mPreprocessor.mViewCellsManager->GetViewPoint(origin);
|
---|
261 |
|
---|
262 | Vector3 point;
|
---|
263 | Vector3 normal;
|
---|
264 |
|
---|
265 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
266 |
|
---|
267 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
268 |
|
---|
269 | object->GetRandomSurfacePoint(point, normal);
|
---|
270 |
|
---|
271 | direction = origin - point;
|
---|
272 |
|
---|
273 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
274 | const float c = Magnitude(direction);
|
---|
275 |
|
---|
276 | if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
|
---|
277 | {
|
---|
278 | return false;
|
---|
279 | }
|
---|
280 |
|
---|
281 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
282 | const float pdf = 1.0f;
|
---|
283 |
|
---|
284 | direction *= 1.0f / c;
|
---|
285 | // a little offset
|
---|
286 | point += direction * 0.001f;
|
---|
287 |
|
---|
288 | ray = SimpleRay(point, direction, REVERSE_OBJECT_BASED_DISTRIBUTION, pdf);
|
---|
289 |
|
---|
290 | return true;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
295 | {
|
---|
296 | Vector3 origin, direction;
|
---|
297 |
|
---|
298 | ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
|
---|
299 |
|
---|
300 | Vector3 point;
|
---|
301 | Vector3 normal, normal2;
|
---|
302 |
|
---|
303 | const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
|
---|
304 | const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
305 |
|
---|
306 | Intersectable *object = mPreprocessor.mObjects[objIdx];
|
---|
307 | ViewCell *viewCell = viewCells[vcIdx];
|
---|
308 |
|
---|
309 | object->GetRandomSurfacePoint(point, normal);
|
---|
310 | viewCell->GetRandomEdgePoint(origin, normal2);
|
---|
311 |
|
---|
312 | direction = point - origin;
|
---|
313 |
|
---|
314 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
315 | const float c = Magnitude(direction);
|
---|
316 |
|
---|
317 | if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
|
---|
318 | {
|
---|
319 | return false;
|
---|
320 | }
|
---|
321 |
|
---|
322 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
323 | const float pdf = 1.0f;
|
---|
324 | //cout << "p: " << point << " ";
|
---|
325 | direction *= 1.0f / c;
|
---|
326 | ray = SimpleRay(origin, direction, VIEWCELL_BORDER_BASED_DISTRIBUTION, pdf);
|
---|
327 |
|
---|
328 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
|
---|
329 |
|
---|
330 | return true;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | bool ReverseViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
335 | {
|
---|
336 | Vector3 origin, direction;
|
---|
337 |
|
---|
338 | origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
|
---|
339 |
|
---|
340 | Vector3 point;
|
---|
341 | Vector3 normal;
|
---|
342 |
|
---|
343 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
344 |
|
---|
345 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
346 |
|
---|
347 | object->GetRandomSurfacePoint(point, normal);
|
---|
348 |
|
---|
349 | direction = origin - point;
|
---|
350 |
|
---|
351 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
352 | const float c = Magnitude(direction);
|
---|
353 |
|
---|
354 | if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
|
---|
355 | {
|
---|
356 | return false;
|
---|
357 | }
|
---|
358 |
|
---|
359 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
360 | const float pdf = 1.0f;
|
---|
361 | //cout << "p: " << point << " ";
|
---|
362 | direction *= 1.0f / c;
|
---|
363 | // a little offset
|
---|
364 | point += direction * 0.001f;
|
---|
365 |
|
---|
366 | ray = SimpleRay(point, direction, REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION, pdf);
|
---|
367 |
|
---|
368 | return true;
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | bool ViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
373 | {
|
---|
374 | Vector3 origin, direction;
|
---|
375 |
|
---|
376 | origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
|
---|
377 |
|
---|
378 | Vector3 point;
|
---|
379 | Vector3 normal;
|
---|
380 |
|
---|
381 | const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
|
---|
382 |
|
---|
383 | Intersectable *object = mPreprocessor.mObjects[i];
|
---|
384 |
|
---|
385 | object->GetRandomSurfacePoint(point, normal);
|
---|
386 | direction = point - origin;
|
---|
387 |
|
---|
388 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
389 | const float c = Magnitude(direction);
|
---|
390 |
|
---|
391 | if (c <= Limits::Small)
|
---|
392 | return false;
|
---|
393 |
|
---|
394 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
395 | const float pdf = 1.0f;
|
---|
396 |
|
---|
397 | direction *= 1.0f / c;
|
---|
398 |
|
---|
399 | // a little offset
|
---|
400 | origin += direction * 0.001f;
|
---|
401 |
|
---|
402 | ray = SimpleRay(origin, direction, VIEWSPACE_BORDER_BASED_DISTRIBUTION, pdf);
|
---|
403 |
|
---|
404 | return true;
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | bool
|
---|
409 | GlobalLinesDistribution::GenerateSample(SimpleRay &ray)
|
---|
410 | {
|
---|
411 | Vector3 origin, termination, direction;
|
---|
412 |
|
---|
413 | float radius = 0.5f*Magnitude(mPreprocessor.mViewCellsManager->GetViewSpaceBox().Size());
|
---|
414 | Vector3 center = mPreprocessor.mViewCellsManager->GetViewSpaceBox().Center();
|
---|
415 |
|
---|
416 | const int tries = 1000;
|
---|
417 | int i;
|
---|
418 | for (i=0; i < tries; i++) {
|
---|
419 | float r[4];
|
---|
420 | sHalton.GetNext(4, r);
|
---|
421 |
|
---|
422 | origin = center + (radius*UniformRandomVector(r[0], r[1]));
|
---|
423 | termination = center + (radius*UniformRandomVector(r[2], r[3]));
|
---|
424 |
|
---|
425 | direction = termination - origin;
|
---|
426 |
|
---|
427 |
|
---|
428 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
429 | const float c = Magnitude(direction);
|
---|
430 | if (c <= Limits::Small)
|
---|
431 | return false;
|
---|
432 |
|
---|
433 | direction *= 1.0f / c;
|
---|
434 |
|
---|
435 | // check if the ray intersects the view space box
|
---|
436 | static Ray ray;
|
---|
437 | ray.Init(origin, direction, Ray::LOCAL_RAY);
|
---|
438 |
|
---|
439 | float tmin, tmax;
|
---|
440 | if (mPreprocessor.mViewCellsManager->
|
---|
441 | GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
442 | break;
|
---|
443 | }
|
---|
444 |
|
---|
445 | if (i!=tries) {
|
---|
446 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
447 | const float pdf = 1.0f;
|
---|
448 |
|
---|
449 |
|
---|
450 | ray = SimpleRay(origin, direction, GLOBAL_LINES_DISTRIBUTION, pdf);
|
---|
451 | ray.mType = Ray::GLOBAL_RAY;
|
---|
452 | return true;
|
---|
453 | }
|
---|
454 |
|
---|
455 | return false;
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | /******************************************************************/
|
---|
460 | /* class MixtureDistribution implementation */
|
---|
461 | /******************************************************************/
|
---|
462 |
|
---|
463 |
|
---|
464 | // has to be called before first usage
|
---|
465 | void
|
---|
466 | MixtureDistribution::Init()
|
---|
467 | {
|
---|
468 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
469 | // small non-zero value
|
---|
470 | mDistributions[i]->mRays = 1;
|
---|
471 | mDistributions[i]->mGeneratedRays = 1;
|
---|
472 | // unit contribution per ray
|
---|
473 | if (1 || mDistributions[i]->mType != RSS_BASED_DISTRIBUTION)
|
---|
474 | mDistributions[i]->mContribution = 1.0f;
|
---|
475 | else
|
---|
476 | mDistributions[i]->mContribution = 0.0f;
|
---|
477 | }
|
---|
478 | UpdateRatios();
|
---|
479 | }
|
---|
480 |
|
---|
481 | void
|
---|
482 | MixtureDistribution::Reset()
|
---|
483 | {
|
---|
484 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
485 | // small non-zero value
|
---|
486 | mDistributions[i]->mTotalRays = 0;
|
---|
487 | // unit contribution per ray
|
---|
488 | mDistributions[i]->mTotalContribution = 0.0f;
|
---|
489 | }
|
---|
490 | UpdateRatios();
|
---|
491 | }
|
---|
492 |
|
---|
493 | // Generate a new sample according to a mixture distribution
|
---|
494 | bool
|
---|
495 | MixtureDistribution::GenerateSample(SimpleRay &ray)
|
---|
496 | {
|
---|
497 | float r;
|
---|
498 |
|
---|
499 | sHalton.GetNext(1, &r);
|
---|
500 | //static Halton<1> halton;
|
---|
501 | // halton.GetNext(&r);
|
---|
502 |
|
---|
503 | int i;
|
---|
504 | // pickup a distribution
|
---|
505 | for (i=0; i < (int)mDistributions.size()-1; i++)
|
---|
506 | if (r < mDistributions[i]->mRatio)
|
---|
507 | break;
|
---|
508 |
|
---|
509 | bool result = mDistributions[i]->GenerateSample(ray);
|
---|
510 |
|
---|
511 | if (result)
|
---|
512 | mDistributions[i]->mGeneratedRays++;
|
---|
513 |
|
---|
514 | return result;
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | #if TEST_PACKETS
|
---|
519 |
|
---|
520 | // add contributions of the sample to the strategies
|
---|
521 | void MixtureDistribution::ComputeContributions(VssRayContainer &vssRays)
|
---|
522 | {
|
---|
523 | int i;
|
---|
524 |
|
---|
525 | mPreprocessor.mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
|
---|
526 |
|
---|
527 | VssRayContainer::iterator it = vssRays.begin();
|
---|
528 |
|
---|
529 | for (i=0; i < mDistributions.size(); i++) {
|
---|
530 | mDistributions[i]->mContribution = 0;
|
---|
531 | mDistributions[i]->mRays = 0;
|
---|
532 | }
|
---|
533 |
|
---|
534 | for(; it != vssRays.end(); ++ it)
|
---|
535 | {
|
---|
536 | VssRay *ray = *it;
|
---|
537 |
|
---|
538 | for (i = 0; i < mDistributions.size() - 1; ++ i)
|
---|
539 | {
|
---|
540 | if (mDistributions[i]->mType == ray->mDistribution)
|
---|
541 | break;
|
---|
542 | }
|
---|
543 |
|
---|
544 | float contribution =
|
---|
545 | mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray, true, false);
|
---|
546 |
|
---|
547 | mDistributions[i]->mContribution += contribution;
|
---|
548 | mDistributions[i]->mRays ++;
|
---|
549 |
|
---|
550 | mDistributions[i]->mTotalContribution += contribution;
|
---|
551 | mDistributions[i]->mTotalRays ++;
|
---|
552 | }
|
---|
553 |
|
---|
554 | UpdateRatios();
|
---|
555 |
|
---|
556 |
|
---|
557 | const float vcTime = viewCellCastTimer.TotalTime();
|
---|
558 | const float pvsTime = pvsTimer.TotalTime();
|
---|
559 | const float haltonTime = haltonTimer.TotalTime();
|
---|
560 |
|
---|
561 | cout << "view cell cast time: " << vcTime << " s" << endl;
|
---|
562 | cout << "pvs time: " << pvsTime << " s" << endl;
|
---|
563 | cout << "halton time: "<< haltonTime << " s" << endl;
|
---|
564 |
|
---|
565 | Debug << "view cell cast time: " << vcTime << " s" << endl;
|
---|
566 | Debug << "pvs time: " << pvsTime << " s" << endl;
|
---|
567 | Debug << "halton time: "<< haltonTime << " s" << endl;
|
---|
568 | }
|
---|
569 |
|
---|
570 |
|
---|
571 | #else
|
---|
572 |
|
---|
573 | // add contributions of the sample to the strategies
|
---|
574 | void
|
---|
575 | MixtureDistribution::ComputeContributions(VssRayContainer &vssRays)
|
---|
576 | {
|
---|
577 | int i;
|
---|
578 |
|
---|
579 | VssRayContainer::iterator it = vssRays.begin();
|
---|
580 |
|
---|
581 | for (i=0; i < mDistributions.size(); i++) {
|
---|
582 | mDistributions[i]->mContribution = 0;
|
---|
583 | mDistributions[i]->mRays = 0;
|
---|
584 | }
|
---|
585 |
|
---|
586 |
|
---|
587 | for(; it != vssRays.end(); ++it)
|
---|
588 | {
|
---|
589 | VssRay *ray = *it;
|
---|
590 |
|
---|
591 | //if (ray->mTerminationObject->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
592 | // cout << "found transofrmed mesh instance: " << Intersectable::GetTypeName(ray->mTerminationObject) << " " << endl;
|
---|
593 |
|
---|
594 | for (i=0; i < mDistributions.size()-1; i++)
|
---|
595 | {
|
---|
596 | if (mDistributions[i]->mType == ray->mDistribution)
|
---|
597 | break;
|
---|
598 | }
|
---|
599 |
|
---|
600 | const float contribution =
|
---|
601 | mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray, true, false);
|
---|
602 |
|
---|
603 | mDistributions[i]->mContribution += contribution;
|
---|
604 | mDistributions[i]->mRays ++;
|
---|
605 |
|
---|
606 | mDistributions[i]->mTotalContribution += contribution;
|
---|
607 | mDistributions[i]->mTotalRays ++;
|
---|
608 | }
|
---|
609 |
|
---|
610 | UpdateRatios();
|
---|
611 |
|
---|
612 |
|
---|
613 | if (1)
|
---|
614 | {
|
---|
615 | #ifdef USE_PERFTIMER
|
---|
616 | const float vcTime = viewCellCastTimer.TotalTime();
|
---|
617 | const float pvsTime = pvsTimer.TotalTime();
|
---|
618 | const float haltonTime = haltonTimer.TotalTime();
|
---|
619 |
|
---|
620 | cout << "view cell cast time: " << vcTime << " s" << endl;
|
---|
621 | cout << "pvs time: " << pvsTime << " s" << endl;
|
---|
622 | cout << "halton time: "<< haltonTime << " s" << endl;
|
---|
623 |
|
---|
624 | Debug << "view cell cast time: " << vcTime << " s" << endl;
|
---|
625 | Debug << "pvs time: " << pvsTime << " s" << endl;
|
---|
626 | Debug << "halton time: "<< haltonTime << " s" << endl;
|
---|
627 | #else
|
---|
628 | cout << "Timers not supported" << endl;
|
---|
629 | Debug << "Timers for PVS,Halton,Viewcell not supported" << endl;
|
---|
630 | #endif
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | #endif
|
---|
635 |
|
---|
636 |
|
---|
637 | void
|
---|
638 | MixtureDistribution::UpdateDistributions(VssRayContainer &vssRays)
|
---|
639 | {
|
---|
640 | // now update the distributions with all the rays
|
---|
641 | for (int i=0; i < mDistributions.size(); i++) {
|
---|
642 | mDistributions[i]->Update(vssRays);
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | #define RAY_CAST_TIME 0.7f
|
---|
647 | #define VIEWCELL_CAST_TIME 0.3f
|
---|
648 |
|
---|
649 | void
|
---|
650 | MixtureDistribution::UpdateRatios()
|
---|
651 | {
|
---|
652 | // now compute importance (ratio) of all distributions
|
---|
653 | float sum = 0.0f;
|
---|
654 | int i;
|
---|
655 | for (i=0; i < mDistributions.size(); i++) {
|
---|
656 | cout<<i<<": c="<<mDistributions[i]->mContribution<<
|
---|
657 | " rays="<<mDistributions[i]->mRays<<endl;
|
---|
658 | float importance = 0.0f;
|
---|
659 | if (mDistributions[i]->mRays != 0) {
|
---|
660 | //importance = pow(mDistributions[i]->mContribution/mDistributions[i]->mRays, 2);
|
---|
661 | importance = mDistributions[i]->mContribution/
|
---|
662 | (RAY_CAST_TIME*mDistributions[i]->mGeneratedRays +
|
---|
663 | VIEWCELL_CAST_TIME*mDistributions[i]->mRays);
|
---|
664 | }
|
---|
665 | mDistributions[i]->mRatio = importance;
|
---|
666 | sum += importance;
|
---|
667 | }
|
---|
668 |
|
---|
669 | if (sum == 0.0f)
|
---|
670 | sum = Limits::Small;
|
---|
671 |
|
---|
672 | const float minratio = 0.01f;
|
---|
673 |
|
---|
674 | for (i=0; i < mDistributions.size(); i++) {
|
---|
675 | mDistributions[i]->mRatio /= sum;
|
---|
676 | if (mDistributions[i]->mRatio < minratio)
|
---|
677 | mDistributions[i]->mRatio = minratio;
|
---|
678 | }
|
---|
679 |
|
---|
680 | // recaluate the sum after clip
|
---|
681 | sum = 0.0f;
|
---|
682 | for (i=0; i < mDistributions.size(); i++)
|
---|
683 | sum += mDistributions[i]->mRatio;
|
---|
684 |
|
---|
685 | for (i=0; i < mDistributions.size(); i++)
|
---|
686 | mDistributions[i]->mRatio /= sum;
|
---|
687 |
|
---|
688 | for (i=1; i < mDistributions.size(); i++) {
|
---|
689 | mDistributions[i]->mRatio = mDistributions[i-1]->mRatio + mDistributions[i]->mRatio;
|
---|
690 | }
|
---|
691 |
|
---|
692 | cout<<"ratios: ";
|
---|
693 | float last = 0.0f;
|
---|
694 | for (i=0; i < mDistributions.size(); i++) {
|
---|
695 | cout<<mDistributions[i]->mRatio-last<<" ";
|
---|
696 | last = mDistributions[i]->mRatio;
|
---|
697 | }
|
---|
698 | cout<<endl;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 |
|
---|
703 | bool
|
---|
704 | MixtureDistribution::Construct(char *str)
|
---|
705 | {
|
---|
706 | char *curr = str;
|
---|
707 |
|
---|
708 | while (1) {
|
---|
709 | char *e = strchr(curr,'+');
|
---|
710 | if (e!=NULL) {
|
---|
711 | *e=0;
|
---|
712 | }
|
---|
713 |
|
---|
714 | if (strcmp(curr, "rss")==0) {
|
---|
715 | mDistributions.push_back(new RssBasedDistribution(mPreprocessor));
|
---|
716 | } else
|
---|
717 | if (strcmp(curr, "object")==0) {
|
---|
718 | mDistributions.push_back(new ObjectBasedDistribution(mPreprocessor));
|
---|
719 | } else
|
---|
720 | if (strcmp(curr, "spatial")==0) {
|
---|
721 | mDistributions.push_back(new SpatialBoxBasedDistribution(mPreprocessor));
|
---|
722 | } else
|
---|
723 | if (strcmp(curr, "global")==0) {
|
---|
724 | mDistributions.push_back(new GlobalLinesDistribution(mPreprocessor));
|
---|
725 | } else
|
---|
726 | if (strcmp(curr, "direction")==0) {
|
---|
727 | mDistributions.push_back(new DirectionBasedDistribution(mPreprocessor));
|
---|
728 | } else
|
---|
729 | if (strcmp(curr, "object_direction")==0) {
|
---|
730 | mDistributions.push_back(new ObjectDirectionBasedDistribution(mPreprocessor));
|
---|
731 | } else
|
---|
732 | if (strcmp(curr, "reverse_object")==0) {
|
---|
733 | mDistributions.push_back(new ReverseObjectBasedDistribution(mPreprocessor));
|
---|
734 | } else
|
---|
735 |
|
---|
736 | if (strcmp(curr, "reverse_viewspace_border")==0) {
|
---|
737 | mDistributions.push_back(new ReverseViewSpaceBorderBasedDistribution(mPreprocessor));
|
---|
738 | } else
|
---|
739 | if (strcmp(curr, "mutation")==0) {
|
---|
740 | mDistributions.push_back(new MutationBasedDistribution(mPreprocessor));
|
---|
741 | } else
|
---|
742 | if (strcmp(curr, "filter_based")==0) {
|
---|
743 | mDistributions.push_back(new FilterBasedDistribution(mPreprocessor));
|
---|
744 | }
|
---|
745 | else if (strcmp(curr, "difference")==0) {
|
---|
746 | mDistributions.push_back(new DifferenceSampling(mPreprocessor));
|
---|
747 | }
|
---|
748 |
|
---|
749 |
|
---|
750 |
|
---|
751 | if (e==NULL)
|
---|
752 | break;
|
---|
753 | curr = e+1;
|
---|
754 | }
|
---|
755 |
|
---|
756 | Init();
|
---|
757 | return true;
|
---|
758 | }
|
---|
759 |
|
---|
760 | int
|
---|
761 | MixtureDistribution::GenerateSamples(const int number,
|
---|
762 | SimpleRayContainer &rays)
|
---|
763 | {
|
---|
764 | for (int i=0; i < mDistributions.size(); i++)
|
---|
765 | mDistributions[i]->mGeneratedRays = 0;
|
---|
766 |
|
---|
767 | return SamplingStrategy::GenerateSamples(number, rays);
|
---|
768 | }
|
---|
769 |
|
---|
770 |
|
---|
771 | HwGlobalLinesDistribution::HwGlobalLinesDistribution(Preprocessor &preprocessor):
|
---|
772 | SamplingStrategy(preprocessor)
|
---|
773 | {
|
---|
774 | mType = HW_GLOBAL_LINES_DISTRIBUTION;
|
---|
775 | preprocessor.mUseHwGlobalLines = true;
|
---|
776 | }
|
---|
777 |
|
---|
778 |
|
---|
779 | bool HwGlobalLinesDistribution::GenerateSample(SimpleRay &ray)
|
---|
780 | {
|
---|
781 | Vector3 origin, termination, direction;
|
---|
782 |
|
---|
783 | const float radius = 0.5f *
|
---|
784 | Magnitude(mPreprocessor.mViewCellsManager->GetViewSpaceBox().Size());
|
---|
785 |
|
---|
786 | Vector3 center = mPreprocessor.mViewCellsManager->GetViewSpaceBox().Center();
|
---|
787 |
|
---|
788 | const int tries = 1000;
|
---|
789 | int i;
|
---|
790 | for (i=0; i < tries; i++)
|
---|
791 | {
|
---|
792 | float r[2];
|
---|
793 | sHalton.GetNext(2, r);
|
---|
794 |
|
---|
795 | origin = center + (radius * UniformRandomVector(r[0], r[1]));
|
---|
796 | termination = center;
|
---|
797 |
|
---|
798 | if (0)
|
---|
799 | {
|
---|
800 | // add a small offset to provide some more randomness in the sampling
|
---|
801 | Vector3 offset(Random(radius * 1e-3f),
|
---|
802 | Random(radius * 1e-3f),
|
---|
803 | Random(radius * 1e-3f));
|
---|
804 | termination += offset;
|
---|
805 | }
|
---|
806 |
|
---|
807 | direction = termination - origin;
|
---|
808 |
|
---|
809 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
810 | const float c = Magnitude(direction);
|
---|
811 |
|
---|
812 | if (c <= Limits::Small)
|
---|
813 | return false;
|
---|
814 |
|
---|
815 | direction *= 1.0f / c;
|
---|
816 |
|
---|
817 | // check if the ray intersects the view space box
|
---|
818 | static Ray ray;
|
---|
819 | ray.Init(origin, direction, Ray::LOCAL_RAY);
|
---|
820 |
|
---|
821 | float tmin, tmax;
|
---|
822 | if (mPreprocessor.mViewCellsManager->
|
---|
823 | GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
|
---|
824 | break;
|
---|
825 | }
|
---|
826 |
|
---|
827 | if (i != tries)
|
---|
828 | {
|
---|
829 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
830 | const float pdf = 1.0f;
|
---|
831 |
|
---|
832 | ray = SimpleRay(origin, direction, HW_GLOBAL_LINES_DISTRIBUTION, pdf);
|
---|
833 | ray.mType = Ray::GLOBAL_RAY;
|
---|
834 | return true;
|
---|
835 | }
|
---|
836 |
|
---|
837 | return false;
|
---|
838 | }
|
---|
839 |
|
---|
840 |
|
---|
841 | bool ViewCellBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
842 | {
|
---|
843 | Vector3 origin, direction;
|
---|
844 |
|
---|
845 | ViewCellContainer &viewCells =
|
---|
846 | mPreprocessor.mViewCellsManager->GetViewCells();
|
---|
847 |
|
---|
848 | Vector3 point;
|
---|
849 | Vector3 normal;
|
---|
850 |
|
---|
851 | //Vector normalObj;
|
---|
852 | // float r[1];
|
---|
853 | // sHalton.GetNext(1, r);
|
---|
854 | // const int objIdx = (int)(r[0] * (float)mPreprocessor.mObjects.size() - 1.0f);
|
---|
855 | // Intersectable *object = mPreprocessor.mObjects[objIdx];
|
---|
856 | // object->GetRandomSurfacePoint(point, normal);
|
---|
857 | // cout << "obj: " << objIdx << endl;
|
---|
858 |
|
---|
859 | float r[2];
|
---|
860 | sHalton.GetNext(2, r);
|
---|
861 |
|
---|
862 | direction = UniformRandomVector(r[0], r[1]);
|
---|
863 | const float c = Magnitude(direction);
|
---|
864 |
|
---|
865 | if (c <= Limits::Small)
|
---|
866 | return false;
|
---|
867 |
|
---|
868 | direction *= 1.0f / c;
|
---|
869 |
|
---|
870 | // get point on view cell surface
|
---|
871 | mViewCell->GetRandomSurfacePoint(origin, normal);
|
---|
872 |
|
---|
873 | //direction = point - origin;
|
---|
874 |
|
---|
875 | // move a little bit back to avoid piercing through walls
|
---|
876 | // that bound the view cell
|
---|
877 | origin -= 0.01f * normal;
|
---|
878 |
|
---|
879 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
880 | const float pdf = 1.0f;
|
---|
881 |
|
---|
882 | ray = SimpleRay(origin, direction, VIEWCELL_BASED_DISTRIBUTION, pdf);
|
---|
883 |
|
---|
884 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
|
---|
885 |
|
---|
886 | return true;
|
---|
887 | }
|
---|
888 |
|
---|
889 |
|
---|
890 | }
|
---|
891 |
|
---|
892 |
|
---|