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