File indexing completed on 2025-01-18 10:04:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #ifndef _OpenGl_HaltonSampler_H
0022 #define _OpenGl_HaltonSampler_H
0023
0024 #include <vector>
0025
0026
0027 class OpenGl_HaltonSampler
0028 {
0029 public:
0030
0031
0032 static unsigned get_num_dimensions() { return 3u; }
0033
0034 public:
0035
0036
0037 OpenGl_HaltonSampler()
0038 {
0039 initFaure();
0040 }
0041
0042
0043
0044
0045 float sample (unsigned theDimension, unsigned theIndex) const
0046 {
0047 switch (theDimension)
0048 {
0049 case 0: return halton2 (theIndex);
0050 case 1: return halton3 (theIndex);
0051 case 2: return halton5 (theIndex);
0052 }
0053 return 0.0f;
0054 }
0055
0056 private:
0057
0058
0059 void initFaure();
0060
0061 static unsigned short invert (unsigned short theBase, unsigned short theDigits,
0062 unsigned short theIndex, const std::vector<unsigned short>& thePerm)
0063 {
0064 unsigned short aResult = 0;
0065 for (unsigned short i = 0; i < theDigits; ++i)
0066 {
0067 aResult = aResult * theBase + thePerm[theIndex % theBase];
0068 theIndex /= theBase;
0069 }
0070 return aResult;
0071 }
0072
0073 void initTables (const std::vector<std::vector<unsigned short> >& thePerm)
0074 {
0075 for (unsigned short i = 0; i < 243; ++i)
0076 {
0077 myPerm3[i] = invert (3, 5, i, thePerm[3]);
0078 }
0079 for (unsigned short i = 0; i < 125; ++i)
0080 {
0081 myPerm5[i] = invert (5, 3, i, thePerm[5]);
0082 }
0083 }
0084
0085
0086 float halton2 (unsigned theIndex) const
0087 {
0088 theIndex = (theIndex << 16) | (theIndex >> 16);
0089 theIndex = ((theIndex & 0x00ff00ff) << 8) | ((theIndex & 0xff00ff00) >> 8);
0090 theIndex = ((theIndex & 0x0f0f0f0f) << 4) | ((theIndex & 0xf0f0f0f0) >> 4);
0091 theIndex = ((theIndex & 0x33333333) << 2) | ((theIndex & 0xcccccccc) >> 2);
0092 theIndex = ((theIndex & 0x55555555) << 1) | ((theIndex & 0xaaaaaaaa) >> 1);
0093 union Result { unsigned u; float f; } aResult;
0094 aResult.u = 0x3f800000u | (theIndex >> 9);
0095 return aResult.f - 1.0f;
0096 }
0097
0098 float halton3 (unsigned theIndex) const
0099 {
0100 return (myPerm3[theIndex % 243u] * 14348907u
0101 + myPerm3[(theIndex / 243u) % 243u] * 59049u
0102 + myPerm3[(theIndex / 59049u) % 243u] * 243u
0103 + myPerm3[(theIndex / 14348907u) % 243u]) * float(0.999999999999999 / 3486784401u);
0104 }
0105
0106 float halton5 (unsigned theIndex) const
0107 {
0108 return (myPerm5[theIndex % 125u] * 1953125u
0109 + myPerm5[(theIndex / 125u) % 125u] * 15625u
0110 + myPerm5[(theIndex / 15625u) % 125u] * 125u
0111 + myPerm5[(theIndex / 1953125u) % 125u]) * float(0.999999999999999 / 244140625u);
0112 }
0113
0114 private:
0115
0116 unsigned short myPerm3[243];
0117 unsigned short myPerm5[125];
0118
0119 };
0120
0121 inline void OpenGl_HaltonSampler::initFaure()
0122 {
0123 const unsigned THE_MAX_BASE = 5u;
0124 std::vector<std::vector<unsigned short> > aPerms(THE_MAX_BASE + 1);
0125 for (unsigned k = 1; k <= 3; ++k)
0126 {
0127 aPerms[k].resize(k);
0128 for (unsigned i = 0; i < k; ++i)
0129 {
0130 aPerms[k][i] = static_cast<unsigned short> (i);
0131 }
0132 }
0133
0134 for (unsigned aBase = 4; aBase <= THE_MAX_BASE; ++aBase)
0135 {
0136 aPerms[aBase].resize(aBase);
0137 const unsigned b = aBase / 2;
0138 if (aBase & 1)
0139 {
0140 for (unsigned i = 0; i < aBase - 1; ++i)
0141 {
0142 aPerms[aBase][i + (i >= b)] = aPerms[aBase - 1][i] + (aPerms[aBase - 1][i] >= b);
0143 }
0144 aPerms[aBase][b] = static_cast<unsigned short> (b);
0145 }
0146 else
0147 {
0148 for (unsigned i = 0; i < b; ++i)
0149 {
0150 aPerms[aBase][i] = 2 * aPerms[b][i];
0151 aPerms[aBase][b + i] = 2 * aPerms[b][i] + 1;
0152 }
0153 }
0154 }
0155 initTables (aPerms);
0156 }
0157
0158 #endif