source: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp @ 2076

Revision 2076, 19.8 KB checked in by bittner, 17 years ago (diff)

merge

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