-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampling.cpp
More file actions
97 lines (74 loc) · 2.34 KB
/
Sampling.cpp
File metadata and controls
97 lines (74 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Sampling.h"
#include <random>
////////////////////////////////////////////////////////////////////////
std::random_device rd;
std::mt19937 engine(rd());
Sampling::Sampling(SamplingMethod& sm) :
m_sampMeth(sm)
{
// Sobol
initQuasirandomGenerator(m_sobolTable);
}
float Sampling::GetOneRndDigit(const uint32_t a_pos, const UINT a_SobolDim) const
{
switch (m_sampMeth)
{
case SamplingMethod::UNIFORM: return Simple1D();
case SamplingMethod::STRATIFIED: return Stratified(10);
case SamplingMethod::SOBOL: return rndQmcSobolN(a_pos, a_SobolDim, &m_sobolTable[0][0]);
default:
break;
}
return 0.0f;
}
float Sampling::Simple1D() const
{
std::uniform_real_distribution<> distr(0.0f, 1.0f);
return (float)(distr(engine));
//return (float)(rand() % RAND_MAX) / (float)RAND_MAX;
}
//////////////////////////
// hammersley 2d
//////////////////////////
//float radicalInverse_VdC(uint32_t bits)
//{
// bits = (bits << 16u) | (bits >> 16u);
// bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
// bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
// bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
// bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
// return float(float(bits) * 2.3283064365386963e-10); // / 0x100000000
//}
//
//vec2 Hammersley2D(uint32_t pos, uint32_t maxSample)
//{
// return vec2(float(pos) / float(maxSample), radicalInverse_VdC(pos));
//}
//////////////////////////
// Stratified MC
//////////////////////////
float RandCell(const uint32_t numCell)
{
std::uniform_int_distribution<> distr(0, numCell);
return (float)(distr(engine)) / (float)numCell;
//return (rand() % numCell) / (float)numCell;
}
void RandomCellArray(std::vector<float>& a_array)
{
const auto sizeArray = (int)(a_array.size());
std::uniform_int_distribution<> rnd(0, sizeArray);
for (int i = 0; i < sizeArray; i++)
{
const int numCell1 = rnd(engine);// rand() % sizeArray;
const int numCell2 = rnd(engine);// rand() % sizeArray;
const float a = a_array[numCell1];
const float b = a_array[numCell2];
a_array[numCell1] = b;
a_array[numCell2] = a;
}
}
float Sampling::Stratified(const uint32_t a_numStrat) const
{
const float strat = RandCell(a_numStrat);
return strat + Simple1D() / (float)(a_numStrat);
}