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

Revision 1785, 8.7 KB checked in by bittner, 18 years ago (diff)

merge, filter update, renderebuffer made functional

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
8
9namespace GtpVisibilityPreprocessor {
10
11
12SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor):
13  mPreprocessor(preprocessor), mRatio(1.0f)
14{
15}
16
17
18SamplingStrategy::~SamplingStrategy()
19{
20}
21
22int
23SamplingStrategy::GenerateSamples(const int number,
24                                                                  SimpleRayContainer &rays) const
25{
26        SimpleRay ray;
27        int samples = 0;
28        int i = 0;
29        const int maxTries = 20;
30        // tmp changed matt. Q: should one rejected sample
31        // terminate the whole method?
32        if (0)
33        {
34                for (; i < number; i++) {
35                        if (!GenerateSample(ray))
36                                return i;
37                        rays.push_back(ray);
38                }
39        }
40        else
41        {
42                for (; i < number; i++)
43                {
44                        int j = 0;
45                        bool sampleGenerated = false;
46
47                        for (j = 0; !sampleGenerated && (j < maxTries); ++ j)
48                        {
49                                sampleGenerated = GenerateSample(ray);
50
51                                if (sampleGenerated)
52                                {               
53                                        ++ samples;
54                                        rays.push_back(ray);
55                                }
56                        }
57                }
58        }
59       
60        return samples;
61}
62
63
64/*********************************************************************/
65/*            Individual sampling strategies implementation          */
66/*********************************************************************/
67
68
69bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
70{
71        Vector3 origin, direction;
72
73        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
74
75        Vector3 point;
76        Vector3 normal;
77        //cout << "y";
78        const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
79
80        Intersectable *object = mPreprocessor.mObjects[i];
81
82        object->GetRandomSurfacePoint(point, normal);
83        direction = point - origin;
84
85        // $$ jb the pdf is yet not correct for all sampling methods!
86        const float c = Magnitude(direction);
87
88        if (c <= Limits::Small)
89                return false;
90
91        // $$ jb the pdf is yet not correct for all sampling methods!
92        const float pdf = 1.0f;
93       
94        direction *= 1.0f / c;
95        ray = SimpleRay(origin, direction, pdf);
96
97        return true;
98}
99
100
101bool ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
102{       
103        Vector3 origin, direction;
104    const int i = (int)RandomValue(0, (Real)mPreprocessor.mObjects.size() - 0.5f);
105        Intersectable *object = mPreprocessor.mObjects[i];
106       
107        Vector3 normal;
108        //cout << "x";
109        object->GetRandomSurfacePoint(origin, normal);
110        direction = UniformRandomVector(normal);
111       
112        origin += 0.1f * direction;
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, pdf);
124
125        return true;
126}
127
128
129bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
130{       
131        Vector3 origin, direction;
132        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
133         
134        direction = UniformRandomVector();
135        const float c = Magnitude(direction);
136
137        if (c <= Limits::Small)
138                return false;
139
140        const float pdf = 1.0f;
141
142        direction *= 1.0f / c;
143        ray = SimpleRay(origin, direction, pdf);
144
145        return true;
146}
147
148
149bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
150{
151        Vector3 origin, direction;
152        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
153
154        const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
155        const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
156       
157        direction = VssRay::GetDirection(alpha, beta);
158       
159        const float c = Magnitude(direction);
160
161        if (c <= Limits::Small)
162                return false;
163
164        const float pdf = 1.0f;
165
166        direction *= 1.0f / c;
167        ray = SimpleRay(origin, direction, pdf);
168
169        return true;
170}
171
172
173bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
174{
175        Vector3 origin, direction;
176
177        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
178        direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint() - origin;
179        //cout << "z";
180        const float c = Magnitude(direction);
181
182        if (c <= Limits::Small)
183                return false;
184
185        const float pdf = 1.0f;
186
187        direction *= 1.0f / c;
188        ray = SimpleRay(origin, direction, pdf);
189
190        return true;
191}
192
193
194bool ReverseObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
195{
196        Vector3 origin, direction;
197
198        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
199
200        Vector3 point;
201        Vector3 normal;
202        //cout << "y";
203        const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
204
205        Intersectable *object = mPreprocessor.mObjects[i];
206
207        object->GetRandomSurfacePoint(point, normal);
208       
209        direction = origin - point;
210       
211        // $$ jb the pdf is yet not correct for all sampling methods!
212        const float c = Magnitude(direction);
213       
214        if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
215        {
216                return false;
217        }
218
219        // $$ jb the pdf is yet not correct for all sampling methods!
220        const float pdf = 1.0f;
221        //cout << "p: " << point << " ";
222        direction *= 1.0f / c;
223        // a little offset
224        point += direction * 0.001f;
225
226        ray = SimpleRay(point, direction, pdf);
227       
228        return true;
229}
230
231
232bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
233{
234        Vector3 origin, direction;
235
236        ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
237
238        Vector3 point;
239        Vector3 normal, normal2;
240       
241        const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
242        const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
243
244        Intersectable *object = mPreprocessor.mObjects[objIdx];
245        ViewCell *viewCell = viewCells[vcIdx];
246
247        //cout << "vc: " << vcIdx << endl;
248        //cout << "obj: " << objIdx << endl;
249
250        object->GetRandomSurfacePoint(point, normal);
251        viewCell->GetRandomEdgePoint(origin, normal2);
252
253        direction = point - origin;
254
255        // $$ jb the pdf is yet not correct for all sampling methods!
256        const float c = Magnitude(direction);
257
258        if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
259        {
260                return false;
261        }
262
263        // $$ jb the pdf is yet not correct for all sampling methods!
264        const float pdf = 1.0f;
265        //cout << "p: " << point << " ";
266        direction *= 1.0f / c;
267        ray = SimpleRay(origin, direction, pdf);
268
269        //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
270
271        return true;
272}
273
274
275#if 0
276bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
277{
278        Vector3 origin, direction;
279
280        // get random object
281        const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
282
283        const Intersectable *obj = mPreprocessor.mObjects[i];
284
285        // note: if we load the polygons as meshes,
286        // asymtotically every second sample is lost!
287        origin = obj->GetBox().GetRandomPoint();
288
289        // uniformly distributed direction
290        direction = UniformRandomVector();
291
292        const float c = Magnitude(direction);
293
294        if (c <= Limits::Small)
295                return false;
296
297        const float pdf = 1.0f;
298
299        direction *= 1.0f / c;
300        ray = SimpleRay(origin, direction, pdf);
301
302        return true;
303}
304
305#endif
306
307
308bool ReverseViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
309{
310        Vector3 origin, direction;
311
312        origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
313
314        Vector3 point;
315        Vector3 normal;
316        //cout << "y";
317        const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
318
319        Intersectable *object = mPreprocessor.mObjects[i];
320
321        object->GetRandomSurfacePoint(point, normal);
322       
323        direction = origin - point;
324       
325        // $$ jb the pdf is yet not correct for all sampling methods!
326        const float c = Magnitude(direction);
327       
328        if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
329        {
330                return false;
331        }
332
333        // $$ jb the pdf is yet not correct for all sampling methods!
334        const float pdf = 1.0f;
335        //cout << "p: " << point << " ";
336        direction *= 1.0f / c;
337        // a little offset
338        point += direction * 0.001f;
339
340        ray = SimpleRay(point, direction, pdf);
341       
342        return true;
343}
344
345
346bool ViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
347{
348        Vector3 origin, direction;
349
350        origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint();
351
352        Vector3 point;
353        Vector3 normal;
354        //cout << "w";
355        const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
356
357        Intersectable *object = mPreprocessor.mObjects[i];
358
359        object->GetRandomSurfacePoint(point, normal);
360        direction = point - origin;
361
362        // $$ jb the pdf is yet not correct for all sampling methods!
363        const float c = Magnitude(direction);
364
365        if (c <= Limits::Small)
366                return false;
367
368        // $$ jb the pdf is yet not correct for all sampling methods!
369        const float pdf = 1.0f;
370       
371        direction *= 1.0f / c;
372
373        // a little offset
374        origin += direction * 0.001f;
375
376        ray = SimpleRay(origin, direction, pdf);
377
378        return true;
379}
380
381
382bool
383RssBasedDistribution::GenerateSample(SimpleRay &ray) const
384{
385  return false;
386}
387
388}
389
390
Note: See TracBrowser for help on using the repository browser.