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