1 | #include "SampleGenerator.h"
|
---|
2 | #include "common.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 | using namespace CHCDemoEngine;
|
---|
7 |
|
---|
8 |
|
---|
9 | SampleGenerator::SampleGenerator(int numSamples, float radius):
|
---|
10 | mNumSamples(numSamples), mRadius(radius)
|
---|
11 | {
|
---|
12 | mHalton = new HaltonSequence(2);
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | SampleGenerator::~SampleGenerator()
|
---|
17 | {
|
---|
18 | DEL_PTR(mHalton);
|
---|
19 | }
|
---|
20 |
|
---|
21 |
|
---|
22 | PoissonDiscSampleGenerator2D::PoissonDiscSampleGenerator2D(int numSamples, float radius):
|
---|
23 | SampleGenerator(numSamples, radius)
|
---|
24 | {}
|
---|
25 |
|
---|
26 |
|
---|
27 | void PoissonDiscSampleGenerator2D::Generate(float *samples) const
|
---|
28 | {
|
---|
29 | // Poisson disc sampling generator using relaxation
|
---|
30 | // dart-throwing as proposed by McCool et al.
|
---|
31 | // the min distance requirement is relaxed if we are not
|
---|
32 | // able to place any dart for a number of tries
|
---|
33 | // the solution is a possion sampling with respect
|
---|
34 | // to the adjusted min distance
|
---|
35 | // this sampling scheme has the benefit that it is hierarchical
|
---|
36 | int maxTries = 1000;
|
---|
37 | const float f_reduction = 0.95f;
|
---|
38 |
|
---|
39 | float r[2];
|
---|
40 |
|
---|
41 | // the maximal possible radius: our radius is a fraction of this radius
|
---|
42 | // this is used as a measure of the quality of distribution of the point samples
|
---|
43 | const float rmax = 2.0f * mRadius * sqrt(1.0f / (2.0f * sqrt(3.0f) * mNumSamples));
|
---|
44 |
|
---|
45 | // generates poisson distribution on disc
|
---|
46 | // start with some thresholds: all samples lie on the circumference of circle
|
---|
47 |
|
---|
48 | float minDist = 2.0f * rmax;
|
---|
49 | float sqrMinDist = minDist * minDist;
|
---|
50 |
|
---|
51 | int tries = 0;
|
---|
52 |
|
---|
53 | //cout << "minDist before= " << minDist << endl;
|
---|
54 | Sample2 *s = (Sample2 *)samples;
|
---|
55 |
|
---|
56 | // check if on disc
|
---|
57 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
58 | {
|
---|
59 | // repeat until valid sample was found
|
---|
60 | while (1)
|
---|
61 | {
|
---|
62 | // q: should we use halton or does it conflict with the poisson disc properties?
|
---|
63 | r[0] = RandomValue(0, 1); r[1] = RandomValue(0, 1);
|
---|
64 | //mHalton->GetNext(r);
|
---|
65 |
|
---|
66 | // scale to -1 .. 1
|
---|
67 | const float rx = r[0] * 2.0f - 1.0f;
|
---|
68 | const float ry = r[1] * 2.0f - 1.0f;
|
---|
69 |
|
---|
70 | // check if in disk, else exit early
|
---|
71 | const float distanceSquared = rx * rx + ry * ry;
|
---|
72 |
|
---|
73 | if ((rx * rx + ry * ry > mRadius * mRadius)
|
---|
74 | // also avoid case that sample exactly in center
|
---|
75 | //|| (distanceSquared <= 1e-3f)
|
---|
76 | )
|
---|
77 | continue;
|
---|
78 |
|
---|
79 | bool sampleValid = true;
|
---|
80 |
|
---|
81 | // check poisson property
|
---|
82 | for (int j = 0; ((j < i) && sampleValid); ++ j)
|
---|
83 | {
|
---|
84 | const float dist =
|
---|
85 | (s[j].x - rx) * (s[j].x - rx) +
|
---|
86 | (s[j].y - ry) * (s[j].y - ry);
|
---|
87 |
|
---|
88 | if (dist < sqrMinDist)
|
---|
89 | sampleValid = false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (sampleValid)
|
---|
93 | {
|
---|
94 | s[i].x = rx;
|
---|
95 | s[i].y = ry;
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | ++ tries;
|
---|
100 |
|
---|
101 | if (tries > maxTries)
|
---|
102 | {
|
---|
103 | minDist *= f_reduction;
|
---|
104 | sqrMinDist = minDist * minDist;
|
---|
105 |
|
---|
106 | maxTries += 1000;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | //cout << "minDist after= " << (float)minDist / mNumSamples<< " #tries: " << tries << endl;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RandomSampleGenerator2D::RandomSampleGenerator2D(int numSamples, float radius):
|
---|
116 | SampleGenerator(numSamples, radius)
|
---|
117 | {}
|
---|
118 |
|
---|
119 |
|
---|
120 | void RandomSampleGenerator2D::Generate(float *samples) const
|
---|
121 | {
|
---|
122 | Sample2 *s = (Sample2 *)samples;
|
---|
123 |
|
---|
124 | int numSamples = 0;
|
---|
125 |
|
---|
126 | float r[2];
|
---|
127 |
|
---|
128 | while (numSamples < mNumSamples)
|
---|
129 | {
|
---|
130 | mHalton->GetNext(r);
|
---|
131 |
|
---|
132 | const float rx = r[0] * 2.0f - 1.0f;
|
---|
133 | const float ry = r[1] * 2.0f - 1.0f;
|
---|
134 |
|
---|
135 | // check if in disk, else exit early
|
---|
136 | if (rx * rx + ry * ry > mRadius * mRadius)
|
---|
137 | continue;
|
---|
138 |
|
---|
139 | s[numSamples].x = rx;
|
---|
140 | s[numSamples].y = ry;
|
---|
141 |
|
---|
142 | ++ numSamples;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | SphericalSampleGenerator3D::SphericalSampleGenerator3D(int numSamples, float radius):
|
---|
148 | SampleGenerator(numSamples, radius)
|
---|
149 | {}
|
---|
150 |
|
---|
151 |
|
---|
152 | void SphericalSampleGenerator3D::Generate(float *samples) const
|
---|
153 | {
|
---|
154 | float r[2];
|
---|
155 | Sample3 *s = (Sample3 *)samples;
|
---|
156 |
|
---|
157 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
158 | {
|
---|
159 | r[0] = RandomValue(0, 1);
|
---|
160 | r[1] = RandomValue(0, 1);
|
---|
161 |
|
---|
162 | // create stratified samples over sphere
|
---|
163 | const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
|
---|
164 | const float phi = 2.0f * M_PI * r[1];
|
---|
165 |
|
---|
166 | s[i].x = mRadius * sin(theta) * cos(phi);
|
---|
167 | s[i].y = mRadius * sin(theta) * sin(phi);
|
---|
168 | s[i].z = mRadius * cos(theta);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | QuadraticDiscSampleGenerator2D::QuadraticDiscSampleGenerator2D(int numSamples, float radius):
|
---|
174 | PoissonDiscSampleGenerator2D(numSamples, radius)
|
---|
175 | {}
|
---|
176 |
|
---|
177 |
|
---|
178 | void QuadraticDiscSampleGenerator2D::Generate(float *samples) const
|
---|
179 | {
|
---|
180 | #if 0
|
---|
181 | float r[2];
|
---|
182 | Sample2 *s = (Sample2 *)samples;
|
---|
183 |
|
---|
184 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
185 | {
|
---|
186 | mHalton->GetNext(r);
|
---|
187 |
|
---|
188 | // create samples over disc: the sample density
|
---|
189 | // decreases quadratically with the distance to the origin
|
---|
190 | s[i].x = mRadius * r[1] * sin(2.0f * M_PI * r[0]);
|
---|
191 | s[i].y = mRadius * r[1] * cos(2.0f * M_PI * r[0]);
|
---|
192 | }
|
---|
193 | #else
|
---|
194 |
|
---|
195 | PoissonDiscSampleGenerator2D::Generate(samples);
|
---|
196 |
|
---|
197 | Sample2 *s = (Sample2 *)samples;
|
---|
198 |
|
---|
199 | // multiply with lenght to get quadratic dependence on the distance
|
---|
200 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
201 | {
|
---|
202 | Sample2 &spl = s[i];
|
---|
203 |
|
---|
204 | float len = sqrt(spl.x * spl.x + spl.y * spl.y);
|
---|
205 | spl.x *= len * mRadius;
|
---|
206 | spl.y *= len * mRadius;
|
---|
207 | }
|
---|
208 | #endif
|
---|
209 | } |
---|