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

Revision 1588, 4.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         
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       
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       
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#if 0
154bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
155{
156        Vector3 origin, direction;
157
158        // get random object
159        const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
160
161        const Intersectable *obj = mPreprocessor.mObjects[i];
162
163        // note: if we load the polygons as meshes,
164        // asymtotically every second sample is lost!
165        origin = obj->GetBox().GetRandomPoint();
166
167        // uniformly distributed direction
168        direction = UniformRandomVector();
169
170        const float c = Magnitude(direction);
171
172        if (c <= Limits::Small)
173                return false;
174
175        const float pdf = 1.0f;
176
177        direction *= 1.0f / c;
178        ray = SimpleRay(origin, direction, pdf);
179
180        return true;
181}
182#endif
183}
Note: See TracBrowser for help on using the repository browser.