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

Revision 2199, 19.4 KB checked in by mattausch, 17 years ago (diff)

using mutationsamples for evaluation

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