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 |
|
---|
9 | namespace GtpVisibilityPreprocessor {
|
---|
10 |
|
---|
11 |
|
---|
12 | SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor):
|
---|
13 | mPreprocessor(preprocessor)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | SamplingStrategy::~SamplingStrategy()
|
---|
19 | {
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | /*********************************************************************/
|
---|
25 | /* Individual sampling strategies implementation */
|
---|
26 | /*********************************************************************/
|
---|
27 |
|
---|
28 |
|
---|
29 | bool 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 |
|
---|
61 | bool 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 |
|
---|
89 | bool 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 |
|
---|
109 | bool 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 |
|
---|
133 | bool 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 |
|
---|
154 | bool 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 | direction = origin - point;
|
---|
169 |
|
---|
170 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
171 | const float c = Magnitude(direction);
|
---|
172 |
|
---|
173 | if (c <= Limits::Small)
|
---|
174 | return false;
|
---|
175 |
|
---|
176 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
177 | const float pdf = 1.0f;
|
---|
178 | //cout << "p: " << point << " ";
|
---|
179 | direction *= 1.0f / c;
|
---|
180 | ray = SimpleRay(point, direction, pdf);
|
---|
181 |
|
---|
182 | return true;
|
---|
183 | }
|
---|
184 |
|
---|
185 | #if 0
|
---|
186 | bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const
|
---|
187 | {
|
---|
188 | Vector3 origin, direction;
|
---|
189 |
|
---|
190 | // get random object
|
---|
191 | const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1);
|
---|
192 |
|
---|
193 | const Intersectable *obj = mPreprocessor.mObjects[i];
|
---|
194 |
|
---|
195 | // note: if we load the polygons as meshes,
|
---|
196 | // asymtotically every second sample is lost!
|
---|
197 | origin = obj->GetBox().GetRandomPoint();
|
---|
198 |
|
---|
199 | // uniformly distributed direction
|
---|
200 | direction = UniformRandomVector();
|
---|
201 |
|
---|
202 | const float c = Magnitude(direction);
|
---|
203 |
|
---|
204 | if (c <= Limits::Small)
|
---|
205 | return false;
|
---|
206 |
|
---|
207 | const float pdf = 1.0f;
|
---|
208 |
|
---|
209 | direction *= 1.0f / c;
|
---|
210 | ray = SimpleRay(origin, direction, pdf);
|
---|
211 |
|
---|
212 | return true;
|
---|
213 | }
|
---|
214 | #endif
|
---|
215 | } |
---|