HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Stratify.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Stratify.h ( UT Library, C++)
7  *
8  * COMMENTS: Generate stratified samples over a grid. The samples may be
9  * biased in either the u or the v direction if desired.
10  * This class isn't rocket science.
11  */
12 
13 #ifndef __UT_Stratify__
14 #define __UT_Stratify__
15 
16 #include "UT_API.h"
17 #include <SYS/SYS_Types.h>
18 
20 {
21 public:
22 
23  // Factor total_samples into a usize and vsize. usize*vsize will
24  // be less than or equal to total_samples and will be weighted in
25  // the u direction by uweight. If exact is set to true, we will ensure
26  // that usize*vsize == total_samples.
27  static void weightedFactor(unsigned int total_samples, fpreal uweight,
28  int &usize, int &vsize, bool exact = false);
29 
30  // Initialize so that N samples will be generated. The samples generated
31  // will be between 0 and (1-inc) where inc is based on the number of
32  // samples. With a u-weight of 1, the samples will be uniformly
33  // distributed in the u and v directions. With a u weight of 2, there will
34  // be twice as many samples in the u direction. Since it's impossible to
35  // generate a perfect number of samples (i.e. not all numbers are nice
36  // composites), there will be a number of un-stratified samples generated
37  // as well.
38  void init(unsigned int total_samples, fpreal uweight=1);
39 
40  //
41  // Reset the stratification
42  void reset()
43  {
44  myU = myV = 0;
45  myDoneRegular = false;
46  }
47  //
48  // Move on to the next sample
49  void nextSample()
50  {
51  if (!myDoneRegular)
52  {
53  myU++;
54  if (myU == myUSize)
55  {
56  myV++;
57  if (myV == myVSize)
58  myDoneRegular = true;
59  else myU = 0;
60  }
61  }
62  }
63  //
64  // Set an arbitrary sample
65  void setSample(unsigned int sample_number)
66  {
67  // s = usize*row + col
68  // col = s % usize
69  // row = s / vszie
70  myV = sample_number / myUSize;
71  myU = sample_number - myV*myUSize;
72  myDoneRegular = myU >= myUSize || myV >= myVSize;
73  }
74 
75  //
76  // As input to the stratify method, the variables u and v should be
77  // initialized with (random) numbers between 0 and 1.
79  {
80  if (!myDoneRegular)
81  {
82  u = (u + (fpreal)myU) * myUInc;
83  v = (v + (fpreal)myV) * myVInc;
84  }
85  }
87  {
88  if (!myDoneRegular)
89  {
90  u = (u + (fpreal)myU) * myUInc;
91  v = (v + (fpreal)myV) * myVInc;
92  }
93  }
94  int getUSize() const { return myUSize; }
95  int getVSize() const { return myVSize; }
96  fpreal getUInc() const { return myUInc; }
97  fpreal getVInc() const { return myVInc; }
98  int getStratifiedSize() const { return myUSize*myVSize; }
99  int isStratifiedSample() const { return myDoneRegular; }
100 
101 private:
102  fpreal myUInc, myVInc;
103  int myU, myV;
104  int myUSize, myVSize;
105  bool myDoneRegular;
106 };
107 
108 #endif
109 
int getUSize() const
Definition: UT_Stratify.h:94
const GLdouble * v
Definition: glcorearb.h:837
#define UT_API
Definition: UT_API.h:14
float fpreal32
Definition: SYS_Types.h:200
int getStratifiedSize() const
Definition: UT_Stratify.h:98
double fpreal64
Definition: SYS_Types.h:201
void setSample(unsigned int sample_number)
Definition: UT_Stratify.h:65
fpreal getVInc() const
Definition: UT_Stratify.h:97
fpreal getUInc() const
Definition: UT_Stratify.h:96
void stratify(fpreal32 &u, fpreal32 &v)
Definition: UT_Stratify.h:78
int getVSize() const
Definition: UT_Stratify.h:95
fpreal64 fpreal
Definition: SYS_Types.h:277
void reset()
Definition: UT_Stratify.h:42
int isStratifiedSample() const
Definition: UT_Stratify.h:99
void stratify(fpreal64 &u, fpreal64 &v)
Definition: UT_Stratify.h:86
void nextSample()
Definition: UT_Stratify.h:49