1 #pragma once
2 
3 #ifndef Y_SCRHALTON_H
4 #define Y_SCRHALTON_H
5 
6 #include <yafray_constants.h>
7 
8 extern YAFRAYCORE_EXPORT const int* faure[];
9 
10 __BEGIN_YAFRAY
11 
12 const int prims[50] = {   1,   2,   3,   5,   7,  11,  13,  17,  19,  23,
13 					     29,  31,  37,  41,  43,  47,  53,  59,  61,  67,
14 					     71,  73,  79,  83,  89,  97, 101, 103, 107, 109,
15 					    113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
16 					    173, 179, 181, 191, 193, 197, 199, 211, 223, 227  };
17 
18 const double invPrims[50] = { 1.000000000,0.500000000,0.333333333,0.200000000,0.142857143,
19 							  0.090909091,0.076923077,0.058823529,0.052631579,0.043478261,
20 							  0.034482759,0.032258065,0.027027027,0.024390244,0.023255814,
21 							  0.021276596,0.018867925,0.016949153,0.016393443,0.014925373,
22 							  0.014084507,0.013698630,0.012658228,0.012048193,0.011235955,
23 							  0.010309278,0.009900990,0.009708738,0.009345794,0.009174312,
24 							  0.008849558,0.007874016,0.007633588,0.007299270,0.007194245,
25 							  0.006711409,0.006622517,0.006369427,0.006134969,0.005988024,
26 							  0.005780347,0.005586592,0.005524862,0.005235602,0.005181347,
27 							  0.005076142,0.005025126,0.004739336,0.004484305,0.004405286 };
28 
29 /** Low Discrepancy Halton sampling */
30 // dim MUST NOT be larger than 50! Above that, random numbers may be
31 // the better choice anyway, not even scrambling is realiable at high dimensions.
scrHalton(int dim,unsigned int n)32 inline double scrHalton(int dim, unsigned int n)
33 {
34 	double value = 0.0;
35 	if(dim < 50)
36 	{
37 		const int *sigma = faure[dim];
38 		unsigned int base = prims[dim];
39 		double f, factor, dn = (double)n;
40 
41 		f = factor = invPrims[dim];
42 		while (n>0) {
43 			value += double(sigma[n % base]) * factor;
44 			dn *= f;
45 			n = (unsigned int)dn;
46 			factor *= f;
47 		}
48 	}
49 	else
50 	{
51 		value = (double)ourRandom();
52 	}
53 	return std::max(1.0e-36, std::min(1.0, value));	//A minimum value very small 1.0e-36 is set to avoid issues with pdf1D sampling in the Sample function with s2=0.f Hopefully in practice the numerical difference between 0.f and 1.0e-36 will not be significant enough to cause other issues.
54 }
55 
56 __END_YAFRAY
57 
58 #endif // Y_SCRHALTON_H
59