source: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.h @ 2735

Revision 2735, 8.2 KB checked in by mattausch, 16 years ago (diff)

added new importance sampling method for gvs

Line 
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
12namespace GtpVisibilityPreprocessor {
13
14class Vector2;
15class Vector3;
16class VssRay;
17class Preprocessor;
18struct SimpleRay;
19class SimpleRayContainer;
20class ViewCell;
21
22struct VssRayContainer;
23
24/** This class generates a specific sampling strategy.
25*/
26class SamplingStrategy
27{
28public:
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
83public:
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
97protected:
98
99        //static HaltonSequence sHalton;
100        Preprocessor &mPreprocessor;
101};
102
103
104class ObjectBasedDistribution: public SamplingStrategy
105{
106 public:
107 
108 
109  ObjectBasedDistribution(Preprocessor &preprocessor):
110        SamplingStrategy(preprocessor) {
111        mType = OBJECT_BASED_DISTRIBUTION;
112  }
113
114private:
115 
116        virtual bool GenerateSample(SimpleRay &ray);
117    static HaltonSequence sHalton;
118};
119
120
121class ReverseObjectBasedDistribution: public SamplingStrategy
122{
123 public:
124       
125         ReverseObjectBasedDistribution(Preprocessor &preprocessor):
126           SamplingStrategy(preprocessor) {
127           mType = REVERSE_OBJECT_BASED_DISTRIBUTION;
128         }
129
130private:
131       
132        virtual bool GenerateSample(SimpleRay &ray);
133};
134
135
136class ObjectDirectionBasedDistribution: public SamplingStrategy
137{
138 
139public:
140         ObjectDirectionBasedDistribution(Preprocessor &preprocessor):
141         SamplingStrategy(preprocessor) {
142           mType = OBJECT_DIRECTION_BASED_DISTRIBUTION;
143         }
144
145private:
146
147        static HaltonSequence sHalton;
148        virtual bool GenerateSample(SimpleRay &ray);
149};
150
151
152class DirectionBasedDistribution: public SamplingStrategy
153{
154 public:
155  DirectionBasedDistribution(Preprocessor &preprocessor):
156        SamplingStrategy(preprocessor){
157        mType = DIRECTION_BASED_DISTRIBUTION;
158  }
159private:
160  virtual bool GenerateSample(SimpleRay &ray);
161  static HaltonSequence sHalton;
162};
163
164
165class DirectionBoxBasedDistribution: public SamplingStrategy
166{
167 public:
168         DirectionBoxBasedDistribution(Preprocessor &preprocessor):
169           SamplingStrategy(preprocessor){
170           mType = DIRECTION_BOX_BASED_DISTRIBUTION;
171         }
172           
173private:
174           virtual bool GenerateSample(SimpleRay &ray);
175};
176
177
178class SpatialBoxBasedDistribution: public SamplingStrategy
179{
180 public:
181         
182         SpatialBoxBasedDistribution(Preprocessor &preprocessor):
183        SamplingStrategy(preprocessor){
184        mType = SPATIAL_BOX_BASED_DISTRIBUTION;
185  }
186 
187private:
188       
189        virtual bool GenerateSample(SimpleRay &ray);
190        static HaltonSequence sHalton;
191};
192
193
194class ViewSpaceBorderBasedDistribution: public SamplingStrategy
195{
196 public:
197         ViewSpaceBorderBasedDistribution(Preprocessor &preprocessor):
198           SamplingStrategy(preprocessor)
199           {
200           mType = VIEWSPACE_BORDER_BASED_DISTRIBUTION;
201         }
202         
203private:
204
205         virtual bool GenerateSample(SimpleRay &ray);
206};
207
208
209class ReverseViewSpaceBorderBasedDistribution: public SamplingStrategy
210{
211 public:
212         ReverseViewSpaceBorderBasedDistribution(Preprocessor &preprocessor):
213           SamplingStrategy(preprocessor){
214           mType = REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION;
215         }
216         
217private:
218         virtual bool GenerateSample(SimpleRay &ray);
219};
220
221
222class GlobalLinesDistribution: public SamplingStrategy
223{
224public:
225 
226  GlobalLinesDistribution(Preprocessor &preprocessor):
227        SamplingStrategy(preprocessor) {
228        mType = GLOBAL_LINES_DISTRIBUTION;
229  }
230 
231private:
232
233        virtual bool GenerateSample(SimpleRay &ray);
234        static HaltonSequence sHalton;
235};
236
237
238class HwGlobalLinesDistribution: public SamplingStrategy
239{
240public:
241 
242  HwGlobalLinesDistribution(Preprocessor &preprocessor);
243 
244private:
245
246        virtual bool GenerateSample(SimpleRay &ray);
247        static HaltonSequence sHalton;
248};
249
250
251class MixtureDistribution: public SamplingStrategy
252{
253public:
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 
294private:
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
304class ViewCellBasedDistribution: public SamplingStrategy
305{
306public:
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
315private:
316
317        virtual bool GenerateSample(SimpleRay &ray);
318
319        ViewCell *mViewCell;
320
321
322        static HaltonSequence sHalton;
323};
324
325
326class 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
337private:
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
349class ProbablyVisibleDistribution: public SamplingStrategy
350{
351public:
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
362private:
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
Note: See TracBrowser for help on using the repository browser.