Changeset 492 for trunk/VUT/GtpVisibilityPreprocessor/src/Halton.h
- Timestamp:
- 01/03/06 23:33:45 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/Halton.h
r355 r492 2 2 #define __HALTON_H 3 3 4 #include <iostream> 5 using namespace std; 6 4 7 class Halton2 { 5 6 float _invBases[2]; 7 float _prev[2]; 8 static float _invBases[2]; 9 float _prev[2]; 10 11 float halton(float baseRec, float prev) const { 12 float r = 1 - prev - 1e-10; 13 if (baseRec < r) 14 return prev + baseRec; 15 float h = baseRec; 16 float hh; 17 do { 18 hh = h; 19 h *= baseRec; 20 } while (h >= r); 21 return prev + hh + h - 1; 22 } 23 24 public: 25 26 void Reset() { 27 _prev[0] =_prev[1] = 0; 28 } 29 30 Halton2() { 31 _invBases[0] = 1./2; 32 _invBases[1] = 1./3; 33 Reset(); 34 } 35 36 void 37 GetNext(float &a, float &b) { 38 a = halton(_invBases[0], _prev[0]); 39 b = halton(_invBases[1], _prev[1]); 40 _prev[0] = a; 41 _prev[1] = b; 42 } 43 }; 8 44 9 float halton(float baseRec, float prev) const { 10 float r = 1 - prev - 1e-10; 11 if (baseRec < r) return prev + baseRec; 12 float h = baseRec; 13 float hh; 14 do { 15 hh = h; 16 h *= baseRec; 17 } while (h >= r); 18 return prev + hh + h - 1; 45 46 /** 47 * Assert whether the argument is a prime number. 48 * @param number the number to be checked 49 */ 50 inline bool IsPrime(const int number) { 51 bool isIt = true; 52 for(int i = 2; i < number; i++) { 53 if(number % i == 0) { 54 isIt = false; 55 break; 19 56 } 20 57 } 58 if(number == 2) { 59 isIt = false; 60 } 61 return isIt; 62 } 63 64 /** 65 * Find the nth prime number. 66 * @param index the ordinal position in the sequence 67 */ 68 inline int FindPrime(const int index) { 69 if(index < 1) { 70 cerr<<"FindPrime: The argument must be non-negative."<<endl; 71 return -1; 72 } 73 int prime = 1; 74 int found = 1; 75 while(found != index) { 76 prime += 2; 77 if(IsPrime(prime) == true) { 78 found++; 79 } 80 } 81 return prime; 82 } 83 84 struct HaltonSequence { 21 85 public: 86 int index; 22 87 23 void Reset() { 24 _prev[0] =_prev[1] = 0; 88 HaltonSequence():index(1) {} 89 90 void Reset() { 91 index = 1; 92 } 93 94 void GenerateNext() { 95 index++; 96 } 97 98 /** 99 * Returns the nth number in the sequence, taken from a specified dimension. 100 * @param index the ordinal position in the sequence 101 * @param dimension the dimension 102 */ 103 104 double GetNumber(const int dimension) { 105 int base = FindPrime(dimension); 106 if(base == 1) { 107 base++; //The first dimension uses base 2. 108 } 109 double remainder; 110 double output = 0.0; 111 double fraction = 1.0 / (double)base; 112 int N1 = 0; 113 int copyOfIndex = index; 114 if(base >= 2 & index >= 1) { 115 while(copyOfIndex > 0) { 116 N1 = (copyOfIndex / base); 117 remainder = copyOfIndex % base; 118 output += fraction * remainder; 119 copyOfIndex = (int)(copyOfIndex / base); 120 fraction /= (double)base; 121 } 122 return output; 25 123 } 26 27 Halton2() { 28 _invBases[0] = 1./2; 29 _invBases[1] = 1./3; 30 Reset(); 124 else { 125 cerr<<"Error generating Halton sequence."<<endl; 126 exit(1); 31 127 } 32 33 void 34 GetNext(float &a, float &b) { 35 a = halton(_invBases[0], _prev[0]); 36 b = halton(_invBases[1], _prev[1]); 37 _prev[0] = a; 38 _prev[1] = b; 39 } 128 } 40 129 }; 41 130
Note: See TracChangeset
for help on using the changeset viewer.