1 | #ifndef _SamplingStategy_H__
|
---|
2 | #define _SamplingStategy_H__
|
---|
3 |
|
---|
4 | #include <vector>
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | #include "Halton.h"
|
---|
8 | namespace GtpVisibilityPreprocessor {
|
---|
9 |
|
---|
10 | class Vector2;
|
---|
11 | class Vector3;
|
---|
12 | class VssRay;
|
---|
13 | class Preprocessor;
|
---|
14 | struct SimpleRay;
|
---|
15 | class SimpleRayContainer;
|
---|
16 |
|
---|
17 | struct VssRayContainer;
|
---|
18 |
|
---|
19 | /** This class generates a specific sampling strategy.
|
---|
20 | */
|
---|
21 | class SamplingStrategy
|
---|
22 | {
|
---|
23 | public:
|
---|
24 |
|
---|
25 | /** Sample rays of particular type
|
---|
26 | */
|
---|
27 | enum
|
---|
28 | {
|
---|
29 | DUMMY_DISTRIBUTION = 0,
|
---|
30 | DIRECTION_BASED_DISTRIBUTION,
|
---|
31 | OBJECT_BASED_DISTRIBUTION,
|
---|
32 | DIRECTION_BOX_BASED_DISTRIBUTION,
|
---|
33 | SPATIAL_BOX_BASED_DISTRIBUTION,
|
---|
34 | VSS_BASED_DISTRIBUTION,
|
---|
35 | RSS_BASED_DISTRIBUTION,
|
---|
36 | RSS_SILHOUETTE_BASED_DISTRIBUTION,
|
---|
37 | OBJECT_DIRECTION_BASED_DISTRIBUTION,
|
---|
38 | OBJECTS_INTERIOR_DISTRIBUTION,
|
---|
39 | REVERSE_OBJECT_BASED_DISTRIBUTION,
|
---|
40 | VIEWCELL_BORDER_BASED_DISTRIBUTION,
|
---|
41 | VIEWSPACE_BORDER_BASED_DISTRIBUTION,
|
---|
42 | REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION,
|
---|
43 | GLOBAL_LINES_DISTRIBUTION,
|
---|
44 | GVS,
|
---|
45 | MUTATION_BASED_DISTRIBUTION,
|
---|
46 | HW_GLOBAL_LINES_DISTRIBUTION
|
---|
47 | };
|
---|
48 |
|
---|
49 | /** Default constructor
|
---|
50 | */
|
---|
51 | SamplingStrategy(Preprocessor &preprocessor);
|
---|
52 |
|
---|
53 | virtual ~SamplingStrategy();
|
---|
54 |
|
---|
55 | /** Each strategy has to implement this function.
|
---|
56 | @returns true if generated valid sample.
|
---|
57 | */
|
---|
58 |
|
---|
59 | virtual int GenerateSamples(const int number, SimpleRayContainer &rays);
|
---|
60 |
|
---|
61 | virtual bool GenerateSample(SimpleRay &ray) = 0;
|
---|
62 |
|
---|
63 | // true if the strategy keeps pointers to rays and thus they should get deleted
|
---|
64 | // outside, in that case the strategy has to use reference counting and deleting the rays
|
---|
65 | // by itself if the number of references drops to zero
|
---|
66 | virtual bool RequiresRays() { return false; }
|
---|
67 |
|
---|
68 |
|
---|
69 | virtual void Update(VssRayContainer &vssRays) {}
|
---|
70 |
|
---|
71 | friend bool LowerRatio(const SamplingStrategy *a, const SamplingStrategy *b) {
|
---|
72 | return a->mRatio < b->mRatio;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public:
|
---|
76 |
|
---|
77 | /// variables usefull for mixed distribution sampling
|
---|
78 | int mType;
|
---|
79 | int mRays;
|
---|
80 | float mContribution;
|
---|
81 |
|
---|
82 | int mTotalRays;
|
---|
83 | int mGeneratedRays;
|
---|
84 | float mTotalContribution;
|
---|
85 |
|
---|
86 | float mTime;
|
---|
87 | float mRatio;
|
---|
88 |
|
---|
89 | protected:
|
---|
90 |
|
---|
91 | //static HaltonSequence sHalton;
|
---|
92 | Preprocessor &mPreprocessor;
|
---|
93 | };
|
---|
94 |
|
---|
95 |
|
---|
96 | class ObjectBasedDistribution: public SamplingStrategy
|
---|
97 | {
|
---|
98 | public:
|
---|
99 |
|
---|
100 |
|
---|
101 | ObjectBasedDistribution(Preprocessor &preprocessor):
|
---|
102 | SamplingStrategy(preprocessor) {
|
---|
103 | mType = OBJECT_BASED_DISTRIBUTION;
|
---|
104 | }
|
---|
105 |
|
---|
106 | private:
|
---|
107 |
|
---|
108 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
109 | static HaltonSequence sHalton;
|
---|
110 | };
|
---|
111 |
|
---|
112 |
|
---|
113 | class ReverseObjectBasedDistribution: public SamplingStrategy
|
---|
114 | {
|
---|
115 | public:
|
---|
116 |
|
---|
117 | ReverseObjectBasedDistribution(Preprocessor &preprocessor):
|
---|
118 | SamplingStrategy(preprocessor) {
|
---|
119 | mType = REVERSE_OBJECT_BASED_DISTRIBUTION;
|
---|
120 | }
|
---|
121 |
|
---|
122 | private:
|
---|
123 |
|
---|
124 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
125 | };
|
---|
126 |
|
---|
127 |
|
---|
128 | class ObjectDirectionBasedDistribution: public SamplingStrategy
|
---|
129 | {
|
---|
130 |
|
---|
131 | public:
|
---|
132 | ObjectDirectionBasedDistribution(Preprocessor &preprocessor):
|
---|
133 | SamplingStrategy(preprocessor) {
|
---|
134 | mType = OBJECT_DIRECTION_BASED_DISTRIBUTION;
|
---|
135 | }
|
---|
136 |
|
---|
137 | private:
|
---|
138 |
|
---|
139 | static HaltonSequence sHalton;
|
---|
140 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
141 | };
|
---|
142 |
|
---|
143 |
|
---|
144 | class DirectionBasedDistribution: public SamplingStrategy
|
---|
145 | {
|
---|
146 | public:
|
---|
147 | DirectionBasedDistribution(Preprocessor &preprocessor):
|
---|
148 | SamplingStrategy(preprocessor){
|
---|
149 | mType = DIRECTION_BASED_DISTRIBUTION;
|
---|
150 | }
|
---|
151 | private:
|
---|
152 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
153 | static HaltonSequence sHalton;
|
---|
154 | };
|
---|
155 |
|
---|
156 |
|
---|
157 | class DirectionBoxBasedDistribution: public SamplingStrategy
|
---|
158 | {
|
---|
159 | public:
|
---|
160 | DirectionBoxBasedDistribution(Preprocessor &preprocessor):
|
---|
161 | SamplingStrategy(preprocessor){
|
---|
162 | mType = DIRECTION_BOX_BASED_DISTRIBUTION;
|
---|
163 | }
|
---|
164 |
|
---|
165 | private:
|
---|
166 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
167 | };
|
---|
168 |
|
---|
169 |
|
---|
170 | class SpatialBoxBasedDistribution: public SamplingStrategy
|
---|
171 | {
|
---|
172 | public:
|
---|
173 |
|
---|
174 | SpatialBoxBasedDistribution(Preprocessor &preprocessor):
|
---|
175 | SamplingStrategy(preprocessor){
|
---|
176 | mType = SPATIAL_BOX_BASED_DISTRIBUTION;
|
---|
177 | }
|
---|
178 |
|
---|
179 | private:
|
---|
180 |
|
---|
181 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
182 | static HaltonSequence sHalton;
|
---|
183 | };
|
---|
184 |
|
---|
185 |
|
---|
186 | class ViewSpaceBorderBasedDistribution: public SamplingStrategy
|
---|
187 | {
|
---|
188 | public:
|
---|
189 | ViewSpaceBorderBasedDistribution(Preprocessor &preprocessor):
|
---|
190 | SamplingStrategy(preprocessor){
|
---|
191 | mType = VIEWSPACE_BORDER_BASED_DISTRIBUTION;
|
---|
192 | }
|
---|
193 |
|
---|
194 | private:
|
---|
195 |
|
---|
196 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
197 | };
|
---|
198 |
|
---|
199 |
|
---|
200 | class ReverseViewSpaceBorderBasedDistribution: public SamplingStrategy
|
---|
201 | {
|
---|
202 | public:
|
---|
203 | ReverseViewSpaceBorderBasedDistribution(Preprocessor &preprocessor):
|
---|
204 | SamplingStrategy(preprocessor){
|
---|
205 | mType = REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION;
|
---|
206 | }
|
---|
207 |
|
---|
208 | private:
|
---|
209 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
210 | };
|
---|
211 |
|
---|
212 |
|
---|
213 | class ViewCellBorderBasedDistribution: public SamplingStrategy
|
---|
214 | {
|
---|
215 | public:
|
---|
216 | ViewCellBorderBasedDistribution(Preprocessor &preprocessor):
|
---|
217 | SamplingStrategy(preprocessor) {
|
---|
218 | mType = VIEWCELL_BORDER_BASED_DISTRIBUTION;
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 | private:
|
---|
223 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
224 | };
|
---|
225 |
|
---|
226 | class MutationBasedDistribution: public SamplingStrategy
|
---|
227 | {
|
---|
228 | public:
|
---|
229 | MutationBasedDistribution(Preprocessor &preprocessor);
|
---|
230 | virtual void Update(VssRayContainer &vssRays);
|
---|
231 |
|
---|
232 | virtual bool RequiresRays() { return true; }
|
---|
233 |
|
---|
234 | private:
|
---|
235 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
236 |
|
---|
237 | struct RayEntry {
|
---|
238 | // halton sequence for generatin gmutations of this ray
|
---|
239 | VssRay *mRay;
|
---|
240 | int mSamples;
|
---|
241 | HaltonSequence mHalton;
|
---|
242 | float mImportance;
|
---|
243 | float mCdf;
|
---|
244 |
|
---|
245 | float GetSamplingFactor() const { return mSamples/mImportance; }
|
---|
246 | RayEntry() {}
|
---|
247 | RayEntry(VssRay *r):mRay(r), mSamples(0), mHalton(), mImportance(1.0f) {}
|
---|
248 | };
|
---|
249 |
|
---|
250 |
|
---|
251 | Vector3
|
---|
252 | ComputeOriginMutation(const VssRay &ray,
|
---|
253 | const Vector3 &U,
|
---|
254 | const Vector3 &V,
|
---|
255 | const Vector2 vr2,
|
---|
256 | const float radius
|
---|
257 | );
|
---|
258 |
|
---|
259 | Vector3
|
---|
260 | ComputeTerminationMutation(const VssRay &ray,
|
---|
261 | const Vector3 &U,
|
---|
262 | const Vector3 &V,
|
---|
263 | const Vector2 vr2,
|
---|
264 | const float radius
|
---|
265 | );
|
---|
266 |
|
---|
267 | RayEntry &GetEntry(const int index) {
|
---|
268 | return mRays[(mBufferStart+index)%mRays.size()];
|
---|
269 | }
|
---|
270 |
|
---|
271 | vector<RayEntry> mRays;
|
---|
272 | int mMaxRays;
|
---|
273 | float mOriginMutationSize;
|
---|
274 | int mBufferStart;
|
---|
275 | int mLastIndex;
|
---|
276 | };
|
---|
277 |
|
---|
278 | class GlobalLinesDistribution: public SamplingStrategy
|
---|
279 | {
|
---|
280 | public:
|
---|
281 |
|
---|
282 | GlobalLinesDistribution(Preprocessor &preprocessor):
|
---|
283 | SamplingStrategy(preprocessor) {
|
---|
284 | mType = GLOBAL_LINES_DISTRIBUTION;
|
---|
285 | }
|
---|
286 |
|
---|
287 | private:
|
---|
288 |
|
---|
289 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
290 | static HaltonSequence sHalton;
|
---|
291 | };
|
---|
292 |
|
---|
293 |
|
---|
294 | class HwGlobalLinesDistribution: public SamplingStrategy
|
---|
295 | {
|
---|
296 | public:
|
---|
297 |
|
---|
298 | HwGlobalLinesDistribution(Preprocessor &preprocessor):
|
---|
299 | SamplingStrategy(preprocessor) {
|
---|
300 | //mType = HW_GLOBAL_LINES_DISTRIBUTION;
|
---|
301 | }
|
---|
302 |
|
---|
303 | private:
|
---|
304 |
|
---|
305 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
306 | static HaltonSequence sHalton;
|
---|
307 | };
|
---|
308 |
|
---|
309 | /** This strategy generates samples inside of the objects, e.g.,
|
---|
310 | for sampling the inside of a colon.
|
---|
311 | */
|
---|
312 | /*class ObjectsInteriorDistribution: public SamplingStrategy
|
---|
313 | {
|
---|
314 | public:
|
---|
315 | ObjectsInteriorDistribution(Preprocessor &preprocessor):
|
---|
316 | SamplingStrategy(preprocessor) {}
|
---|
317 |
|
---|
318 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
319 | };
|
---|
320 | */
|
---|
321 |
|
---|
322 | class MixtureDistribution: public SamplingStrategy
|
---|
323 | {
|
---|
324 | public:
|
---|
325 |
|
---|
326 | // container for the distributions
|
---|
327 | vector<SamplingStrategy *> mDistributions;
|
---|
328 |
|
---|
329 | MixtureDistribution(Preprocessor &preprocessor):
|
---|
330 | SamplingStrategy(preprocessor)
|
---|
331 | {
|
---|
332 | }
|
---|
333 |
|
---|
334 | // has to called before first usage
|
---|
335 | void Init();
|
---|
336 |
|
---|
337 | // equalize distribution contributions
|
---|
338 | void
|
---|
339 | Reset();
|
---|
340 |
|
---|
341 | // add contributions of the sample to the strategies
|
---|
342 | void ComputeContributions(VssRayContainer &vssRays);
|
---|
343 |
|
---|
344 | // update distributions with new rays
|
---|
345 | // warning: some rays can get deleted (if maintained internally by the
|
---|
346 | // particular distribution)!
|
---|
347 | void UpdateDistributions(VssRayContainer &vssRays);
|
---|
348 |
|
---|
349 | void
|
---|
350 | UpdateRatios();
|
---|
351 |
|
---|
352 | // construct distribution mixture from a string describing the required distributions
|
---|
353 | bool
|
---|
354 | Construct(char *str);
|
---|
355 |
|
---|
356 | virtual bool RequiresRays() {
|
---|
357 | for (int i=0; i < (int)mDistributions.size(); i++)
|
---|
358 | if (mDistributions[i]->RequiresRays())
|
---|
359 | return true;
|
---|
360 | return false;
|
---|
361 | }
|
---|
362 |
|
---|
363 | virtual int GenerateSamples(const int number, SimpleRayContainer &rays);
|
---|
364 |
|
---|
365 | private:
|
---|
366 |
|
---|
367 | // Generate a new sample according to a mixture distribution
|
---|
368 | virtual bool GenerateSample(SimpleRay &ray);
|
---|
369 |
|
---|
370 | // halton sequence generator for deciding between distributions
|
---|
371 | static HaltonSequence sHalton;
|
---|
372 | };
|
---|
373 |
|
---|
374 | };
|
---|
375 |
|
---|
376 | #endif
|
---|