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

Revision 1769, 6.2 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        const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f);
202        const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
203
204        Intersectable *object = mPreprocessor.mObjects[objIdx];
205        ViewCell *viewCell = viewCells[vcIdx];
206
207        //cout << "vc: " << vcIdx << endl;
208        //cout << "obj: " << objIdx << endl;
209
210        object->GetRandomSurfacePoint(point, normal);
211        viewCell->GetRandomEdgePoint(origin, normal2);
212
213        direction = point - origin;
214
215        // $$ jb the pdf is yet not correct for all sampling methods!
216        const float c = Magnitude(direction);
217
218        if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/)
219        {
220                return false;
221        }
222
223        // $$ jb the pdf is yet not correct for all sampling methods!
224        const float pdf = 1.0f;
225        //cout << "p: " << point << " ";
226        direction *= 1.0f / c;
227        ray = SimpleRay(origin, direction, pdf);
228
229        //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
230
231        return true;
232}
233
234
235#if 0
236bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
237{
238        Vector3 origin, direction;
239
240        // get random object
241        const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
242
243        const Intersectable *obj = mPreprocessor.mObjects[i];
244
245        // note: if we load the polygons as meshes,
246        // asymtotically every second sample is lost!
247        origin = obj->GetBox().GetRandomPoint();
248
249        // uniformly distributed direction
250        direction = UniformRandomVector();
251
252        const float c = Magnitude(direction);
253
254        if (c <= Limits::Small)
255                return false;
256
257        const float pdf = 1.0f;
258
259        direction *= 1.0f / c;
260        ray = SimpleRay(origin, direction, pdf);
261
262        return true;
263}
264
265#endif
266}
Note: See TracBrowser for help on using the repository browser.