sampler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Sampler *
  23. * *
  24. * $Archive:: /VSS_Sync/wwlib/sampler.cpp $*
  25. * *
  26. * Original Author:: Hector Yee *
  27. * *
  28. * $Author:: Vss_sync $*
  29. * *
  30. * $Modtime:: 8/29/01 10:25p $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * RandomSamplingClass::Sample -- Samples randomly over a hypercube using Mersenne Twister *
  37. * RegularSamplingClass::Sample -- Samples over a regular hypergrid *
  38. * StratifiedSamplingClass::Sample -- samples over a regular hypergrid with random offset *
  39. * QMCSamplingClass::Sample -- Samples using the Halton sequence *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. // Use Random Sampling if you're not sure
  42. // Use Regular Sampling if you're not concerned about bias
  43. // Use Stratified Sampling if you want good results in low dimensions
  44. // Use QMC if you want low discrepancy and you want reproduceablility
  45. #include "sampler.h"
  46. #include "random.h"
  47. #include <math.h>
  48. #include <assert.h>
  49. #include <memory.h>
  50. Random4Class Random;
  51. RandomSamplingClass::RandomSamplingClass(unsigned int dimensions,unsigned char divisions):
  52. SamplingClass(dimensions,divisions)
  53. {
  54. }
  55. /***********************************************************************************************
  56. * RandomSamplingClass::Sample -- Samples randomly over a hypercube using Mersenne Twister *
  57. * *
  58. * *
  59. * *
  60. * *
  61. * INPUT: *
  62. * *
  63. * OUTPUT: *
  64. * *
  65. * WARNINGS: *
  66. * *
  67. * HISTORY: *
  68. * 6/11/2001 hy : Created. *
  69. *=============================================================================================*/
  70. void RandomSamplingClass::Sample(float *target)
  71. {
  72. unsigned int i;
  73. for (i=0; i<Dimensions; i++)
  74. {
  75. target[i]=Random.Get_Float();
  76. }
  77. }
  78. RegularSamplingClass::RegularSamplingClass(unsigned int dimensions,unsigned char divisions):
  79. SamplingClass(dimensions,divisions)
  80. {
  81. index=new unsigned char[Dimensions];
  82. Reset();
  83. }
  84. void RegularSamplingClass::Reset()
  85. {
  86. memset(index,0,sizeof(unsigned char)*Dimensions);
  87. }
  88. RegularSamplingClass::~RegularSamplingClass()
  89. {
  90. delete [] index;
  91. }
  92. /***********************************************************************************************
  93. * RegularSamplingClass::Sample -- Samples over a regular hypergrid *
  94. * *
  95. * *
  96. * *
  97. * *
  98. * INPUT: *
  99. * *
  100. * OUTPUT: *
  101. * *
  102. * WARNINGS: *
  103. * *
  104. * HISTORY: *
  105. * 6/11/2001 hy : Created. *
  106. *=============================================================================================*/
  107. void RegularSamplingClass::Sample(float *target)
  108. {
  109. unsigned int i;
  110. for (i=0; i<Dimensions; i++)
  111. {
  112. // minus one because we want to get 1.0f also
  113. target[i]=(float) index[i]/(Divisions-1.0f);
  114. }
  115. // index[i] will always be 0..Divisons-1
  116. // add 1 and carry mod Divisions
  117. // e.g. increase x until x reaches Divisions
  118. // then and only then increase y. Now z increases
  119. // only when x=Divisions and y=Divisions etc..
  120. for (i=0; i<Dimensions; i++)
  121. {
  122. index[i]++;
  123. if (index[i]<Divisions) break;
  124. index[i]=0;
  125. }
  126. }
  127. StratifiedSamplingClass::StratifiedSamplingClass(unsigned int dimensions,unsigned char divisions):
  128. SamplingClass(dimensions,divisions)
  129. {
  130. index=new unsigned char[Dimensions];
  131. Reset();
  132. }
  133. void StratifiedSamplingClass::Reset()
  134. {
  135. memset(index,0,sizeof(unsigned char)*Dimensions);
  136. }
  137. StratifiedSamplingClass::~StratifiedSamplingClass()
  138. {
  139. delete [] index;
  140. }
  141. /***********************************************************************************************
  142. * StratifiedSamplingClass::Sample -- samples over a regular hypergrid with random offset *
  143. * *
  144. * *
  145. * *
  146. * *
  147. * INPUT: *
  148. * *
  149. * OUTPUT: *
  150. * *
  151. * WARNINGS: *
  152. * *
  153. * HISTORY: *
  154. * 6/11/2001 hy : Created. *
  155. *=============================================================================================*/
  156. void StratifiedSamplingClass::Sample(float *target)
  157. {
  158. unsigned int i;
  159. for (i=0; i<Dimensions; i++)
  160. {
  161. target[i]=(index[i]+Random.Get_Float())/(float) Divisions;
  162. }
  163. // index[i] will always be 0..Divisons-1
  164. // add 1 and carry mod Divisions
  165. // e.g. increase x until x reaches Divisions
  166. // then and only then increase y. Now z increases
  167. // only when x=Divisions and y=Divisions etc..
  168. for (i=0; i<Dimensions; i++)
  169. {
  170. index[i]++;
  171. if (index[i]<Divisions) break;
  172. index[i]=0;
  173. }
  174. }
  175. // first 100 primes
  176. const static int primes[]=
  177. {
  178. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
  179. , 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
  180. , 73, 79, 83, 89, 97,101,103,107,109,113
  181. ,127,131,137,139,149,151,157,163,167,173
  182. ,179,181,191,193,197,199,211,223,227,229
  183. ,233,239,241,251,257,263,269,271,277,281
  184. ,283,293,307,311,313,317,331,337,347,349
  185. ,353,359,367,373,379,383,389,397,401,409
  186. ,419,421,431,433,439,443,449,457,461,463
  187. ,467,479,487,491,499,503,509,521,523,541
  188. };
  189. inline float RadInv(int i,int base)
  190. // returns the radical inverse of i in base b
  191. // basically write a number in base b and reverse it over the decimal point
  192. // e.g. RadInv(1) base 2 = 0.1 base 2 = 0.5
  193. {
  194. float sum=0;
  195. int residue;
  196. float power=1.0f/base;
  197. while (i!=0)
  198. {
  199. residue=i%base;
  200. i/=base;
  201. sum+=residue*power;
  202. power/=base;
  203. }
  204. return sum;
  205. }
  206. QMCSamplingClass::QMCSamplingClass(unsigned int dimensions,unsigned char divisions):
  207. SamplingClass(dimensions,divisions),
  208. index(0)
  209. {
  210. assert(Dimensions<100);
  211. }
  212. /***********************************************************************************************
  213. * QMCSamplingClass::Sample -- Samples using the Halton sequence *
  214. * *
  215. * *
  216. * *
  217. * *
  218. * INPUT: *
  219. * *
  220. * OUTPUT: *
  221. * *
  222. * WARNINGS: *
  223. * *
  224. * HISTORY: *
  225. * 6/11/2001 hy : Created. *
  226. *=============================================================================================*/
  227. void QMCSamplingClass::Sample(float *target)
  228. {
  229. unsigned int i;
  230. for (i=0; i<Dimensions; i++)
  231. {
  232. target[i]=RadInv(index,primes[i]);
  233. }
  234. index++;
  235. }