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

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