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

Revision 1771, 6.5 KB checked in by bittner, 18 years ago (diff)

merge, preparing sampling strategy support for mixed distributions, filter changes, histogram output for preprocessor

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):
13mPreprocessor(preprocessor)
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 i = 0;
28  for (; i < number; i++) {
29        if (!GenerateSample(ray))
30          return i;
31        rays.push_back(ray);
32  }
33
34  return i;
35}
36
37
38/*********************************************************************/
39/*            Individual sampling strategies implementation          */
40/*********************************************************************/
41
42
43bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
44{
45        Vector3 origin, direction;
46
47        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
48
49        Vector3 point;
50        Vector3 normal;
51        //cout << "y";
52        const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
53
54        Intersectable *object = mPreprocessor.mObjects[i];
55
56        object->GetRandomSurfacePoint(point, normal);
57        direction = point - origin;
58
59        // $$ jb the pdf is yet not correct for all sampling methods!
60        const float c = Magnitude(direction);
61
62        if (c <= Limits::Small)
63                return false;
64
65        // $$ jb the pdf is yet not correct for all sampling methods!
66        const float pdf = 1.0f;
67       
68        direction *= 1.0f / c;
69        ray = SimpleRay(origin, direction, pdf);
70
71        return true;
72}
73
74
75bool ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
76{       
77        Vector3 origin, direction;
78    const int i = (int)RandomValue(0, (Real)mPreprocessor.mObjects.size() - 0.5f);
79        Intersectable *object = mPreprocessor.mObjects[i];
80       
81        Vector3 normal;
82        //cout << "x";
83        object->GetRandomSurfacePoint(origin, normal);
84        direction = UniformRandomVector(normal);
85       
86        origin += 0.1f * direction;
87
88        const float c = Magnitude(direction);
89
90        if (c <= Limits::Small)
91                return false;
92       
93        // $$ jb the pdf is yet not correct for all sampling methods!
94        const float pdf = 1.0f;
95       
96        direction *= 1.0f / c;
97        ray = SimpleRay(origin, direction, pdf);
98
99        return true;
100}
101
102
103bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray) const
104{       
105        Vector3 origin, direction;
106        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
107         
108        direction = UniformRandomVector();
109        const float c = Magnitude(direction);
110
111        if (c <= Limits::Small)
112                return false;
113
114        const float pdf = 1.0f;
115
116        direction *= 1.0f / c;
117        ray = SimpleRay(origin, direction, pdf);
118
119        return true;
120}
121
122
123bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
124{
125        Vector3 origin, direction;
126        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
127
128        const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
129        const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
130       
131        direction = VssRay::GetDirection(alpha, beta);
132       
133        const float c = Magnitude(direction);
134
135        if (c <= Limits::Small)
136                return false;
137
138        const float pdf = 1.0f;
139
140        direction *= 1.0f / c;
141        ray = SimpleRay(origin, direction, pdf);
142
143        return true;
144}
145
146
147bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray) const
148{
149        Vector3 origin, direction;
150
151        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
152        direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint() - origin;
153        //cout << "z";
154        const float c = Magnitude(direction);
155
156        if (c <= Limits::Small)
157                return false;
158
159        const float pdf = 1.0f;
160
161        direction *= 1.0f / c;
162        ray = SimpleRay(origin, direction, pdf);
163
164        return true;
165}
166
167
168bool ReverseObjectBasedDistribution::GenerateSample(SimpleRay &ray) const
169{
170        Vector3 origin, direction;
171
172        mPreprocessor.mViewCellsManager->GetViewPoint(origin);
173
174        Vector3 point;
175        Vector3 normal;
176        //cout << "y";
177        const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
178
179        Intersectable *object = mPreprocessor.mObjects[i];
180
181        object->GetRandomSurfacePoint(point, normal);
182       
183        direction = origin - point;
184       
185        // $$ jb the pdf is yet not correct for all sampling methods!
186        const float c = Magnitude(direction);
187       
188        if ((c <= Limits::Small) || (DotProd(direction, normal) < 0))
189        {
190                return false;
191        }
192
193        // $$ jb the pdf is yet not correct for all sampling methods!
194        const float pdf = 1.0f;
195        //cout << "p: " << point << " ";
196        direction *= 1.0f / c;
197        // a little offset
198        point += direction * 0.01f;
199
200        ray = SimpleRay(point, direction, pdf);
201       
202        return true;
203}
204
205
206bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray) const
207{
208        Vector3 origin, direction;
209
210        ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells();
211
212        Vector3 point;
213        Vector3 normal, normal2;
214       
215        const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
216        const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
217
218        Intersectable *object = mPreprocessor.mObjects[objIdx];
219        ViewCell *viewCell = viewCells[vcIdx];
220
221        //cout << "vc: " << vcIdx << endl;
222        //cout << "obj: " << objIdx << endl;
223
224        object->GetRandomSurfacePoint(point, normal);
225        viewCell->GetRandomEdgePoint(origin, normal2);
226
227        direction = point - origin;
228
229        // $$ jb the pdf is yet not correct for all sampling methods!
230        const float c = Magnitude(direction);
231
232        if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
233        {
234                return false;
235        }
236
237        // $$ jb the pdf is yet not correct for all sampling methods!
238        const float pdf = 1.0f;
239        //cout << "p: " << point << " ";
240        direction *= 1.0f / c;
241        ray = SimpleRay(origin, direction, pdf);
242
243        //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
244
245        return true;
246}
247
248
249#if 0
250bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
251{
252        Vector3 origin, direction;
253
254        // get random object
255        const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
256
257        const Intersectable *obj = mPreprocessor.mObjects[i];
258
259        // note: if we load the polygons as meshes,
260        // asymtotically every second sample is lost!
261        origin = obj->GetBox().GetRandomPoint();
262
263        // uniformly distributed direction
264        direction = UniformRandomVector();
265
266        const float c = Magnitude(direction);
267
268        if (c <= Limits::Small)
269                return false;
270
271        const float pdf = 1.0f;
272
273        direction *= 1.0f / c;
274        ray = SimpleRay(origin, direction, pdf);
275
276        return true;
277}
278
279#endif
280
281bool
282RssBasedDistribution::GenerateSample(SimpleRay &ray) const
283{
284  return false;
285}
286
287}
288
289
Note: See TracBrowser for help on using the repository browser.