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

Revision 1983, 8.3 KB checked in by bittner, 17 years ago (diff)

merge

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