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 | float rmax = 0.5f; //mRadius * sqrt(1.0f / (2.0f * sqrt(3.0f) * mNumSamples));
|
---|
45 |
|
---|
46 | // generates poisson distribution on disc
|
---|
47 | // start with some thresholds: all samples lie on the circumference of circle
|
---|
48 |
|
---|
49 | //float minDist = 2.0f * rmax;
|
---|
50 | float minDist = rmax;
|
---|
51 | float sqrMinDist = minDist * minDist;
|
---|
52 |
|
---|
53 | int tries = 0;
|
---|
54 |
|
---|
55 | //cout << "minDist before= " << minDist / rmax << endl;
|
---|
56 | cout << "minDist before= " << rmax << endl;
|
---|
57 |
|
---|
58 | Sample2 *s = (Sample2 *)samples;
|
---|
59 |
|
---|
60 | // check if on disc
|
---|
61 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
62 | {
|
---|
63 | // repeat until valid sample was found
|
---|
64 | while (1)
|
---|
65 | {
|
---|
66 | // q: should we use halton or does it conflict with the poisson disc properties?
|
---|
67 | //r[0] = RandomValue(0, 1); r[1] = RandomValue(0, 1);
|
---|
68 | mHalton->GetNext(r);
|
---|
69 |
|
---|
70 | #if 0
|
---|
71 | // scale to -1 .. 1
|
---|
72 | const float rx = r[0] * 2.0f - 1.0f;
|
---|
73 | const float ry = r[1] * 2.0f - 1.0f;
|
---|
74 |
|
---|
75 | // check if in disk, else exit early
|
---|
76 | const float distanceSquared = rx * rx + ry * ry;
|
---|
77 | if (rx * rx + ry * ry > mRadius * mRadius)
|
---|
78 | // also avoid case that sample exactly in center
|
---|
79 | //|| (distanceSquared <= 1e-3f)
|
---|
80 | {
|
---|
81 | continue;
|
---|
82 | }
|
---|
83 | #else
|
---|
84 | const float rx = r[0];
|
---|
85 | const float ry = r[1];
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | bool sampleValid = true;
|
---|
89 |
|
---|
90 | // check poisson property
|
---|
91 | for (int j = 0; ((j < i) && sampleValid); ++ j)
|
---|
92 | {
|
---|
93 | const float dist =
|
---|
94 | (s[j].x - rx) * (s[j].x - rx) +
|
---|
95 | (s[j].y - ry) * (s[j].y - ry);
|
---|
96 |
|
---|
97 | if (dist < sqrMinDist) sampleValid = false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (sampleValid)
|
---|
101 | {
|
---|
102 | s[i].x = rx;
|
---|
103 | s[i].y = ry;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 |
|
---|
107 | ++ tries;
|
---|
108 |
|
---|
109 | if (tries > maxTries)
|
---|
110 | {
|
---|
111 | minDist *= f_reduction;
|
---|
112 | sqrMinDist = minDist * minDist;
|
---|
113 |
|
---|
114 | maxTries += 1000;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
120 | {
|
---|
121 | const float a = 2.0f * M_PI * s[i].x;
|
---|
122 | const float r = sqrt(s[i].y);
|
---|
123 |
|
---|
124 | const float rad = mRadius * r;
|
---|
125 |
|
---|
126 | s[i].x = rad * cos(a);
|
---|
127 | s[i].y = rad * sin(a);
|
---|
128 | }
|
---|
129 |
|
---|
130 | rmax = mRadius * sqrt(1.0f / (2.0f * sqrt(3.0f) * mNumSamples));
|
---|
131 | cout << "minDist after= " << (float)minDist * mNumSamples << " #tries: " << tries << " samples: " << mNumSamples << endl;
|
---|
132 | cout << "minDist after= " << (float)minDist / rmax << " #tries: " << tries << endl;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RandomSampleGenerator2D::RandomSampleGenerator2D(int numSamples, float radius):
|
---|
137 | SampleGenerator(numSamples, radius)
|
---|
138 | {}
|
---|
139 |
|
---|
140 |
|
---|
141 | #if 0
|
---|
142 | void RandomSampleGenerator2D::Generate(float *samples) const
|
---|
143 | {
|
---|
144 | Sample2 *s = (Sample2 *)samples;
|
---|
145 |
|
---|
146 | int numSamples = 0;
|
---|
147 |
|
---|
148 | float r[2];
|
---|
149 |
|
---|
150 | while (numSamples < mNumSamples)
|
---|
151 | {
|
---|
152 | mHalton->GetNext(r);
|
---|
153 |
|
---|
154 | const float rx = r[0] * 2.0f - 1.0f;
|
---|
155 | const float ry = r[1] * 2.0f - 1.0f;
|
---|
156 |
|
---|
157 | // check if in disk, else exit early
|
---|
158 | if (rx * rx + ry * ry > mRadius * mRadius)
|
---|
159 | continue;
|
---|
160 |
|
---|
161 | s[numSamples].x = rx;
|
---|
162 | s[numSamples].y = ry;
|
---|
163 |
|
---|
164 | ++ numSamples;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | #else
|
---|
169 |
|
---|
170 | void RandomSampleGenerator2D::Generate(float *samples) const
|
---|
171 | {
|
---|
172 | Sample2 *s = (Sample2 *)samples;
|
---|
173 |
|
---|
174 | int numSamples = 0;
|
---|
175 | float x[2];
|
---|
176 |
|
---|
177 | static float total1 = 0;
|
---|
178 | static float total2 = 0;
|
---|
179 | static int totalSamples = 0;
|
---|
180 |
|
---|
181 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
182 | {
|
---|
183 | //x[0] = RandomValue(0, 1); x[1] = RandomValue(0, 1);
|
---|
184 | mHalton->GetNext(x);
|
---|
185 |
|
---|
186 | const float a = 2.0f * M_PI * x[0];
|
---|
187 | const float r = mRadius * sqrt(x[1]);
|
---|
188 |
|
---|
189 | s[i].x = r * cos(a);
|
---|
190 | s[i].y = r * sin(a);
|
---|
191 |
|
---|
192 | /*total1 += x[0];
|
---|
193 | total2 += x[1];
|
---|
194 | totalSamples ++;
|
---|
195 |
|
---|
196 | if (totalSamples % 1000 == 1)
|
---|
197 | {
|
---|
198 | float n1 = (float)total1 / totalSamples;
|
---|
199 | float n2 = (float)total2 / totalSamples;
|
---|
200 |
|
---|
201 | cout << "here3 " << n1 << " " << n2 << endl;
|
---|
202 | }*/
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | SphericalSampleGenerator3D::SphericalSampleGenerator3D(int numSamples, float radius):
|
---|
209 | SampleGenerator(numSamples, radius)
|
---|
210 | {}
|
---|
211 |
|
---|
212 |
|
---|
213 | void SphericalSampleGenerator3D::Generate(float *samples) const
|
---|
214 | {
|
---|
215 | float r[2];
|
---|
216 | Sample3 *s = (Sample3 *)samples;
|
---|
217 |
|
---|
218 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
219 | {
|
---|
220 | r[0] = RandomValue(0, 1);
|
---|
221 | r[1] = RandomValue(0, 1);
|
---|
222 |
|
---|
223 | const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
|
---|
224 | const float phi = 2.0f * M_PI * r[1];
|
---|
225 |
|
---|
226 | s[i].x = mRadius * sin(theta) * cos(phi);
|
---|
227 | s[i].y = mRadius * sin(theta) * sin(phi);
|
---|
228 | s[i].z = mRadius * cos(theta);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | QuadraticDiscSampleGenerator2D::QuadraticDiscSampleGenerator2D(int numSamples,
|
---|
234 | float radius):
|
---|
235 | //PoissonDiscSampleGenerator2D(numSamples, radius)
|
---|
236 | RandomSampleGenerator2D(numSamples, radius)
|
---|
237 | {
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | void QuadraticDiscSampleGenerator2D::Generate(float *samples) const
|
---|
242 | {
|
---|
243 | Sample2 *s = (Sample2 *)samples;
|
---|
244 |
|
---|
245 | int numSamples = 0;
|
---|
246 | float x[2];
|
---|
247 |
|
---|
248 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
249 | {
|
---|
250 | x[0] = RandomValue(0, 1); x[1] = RandomValue(0, 1);
|
---|
251 | //mHalton->GetNext(x);
|
---|
252 |
|
---|
253 | const float a = 2.0f * M_PI * x[0];
|
---|
254 | const float r = sqrt(x[1]);
|
---|
255 | //const float rad = mRadius * r * r * r * r;
|
---|
256 | const float rad = mRadius * r;// * r;
|
---|
257 |
|
---|
258 | s[i].x = rad * cos(a);
|
---|
259 | s[i].y = rad * sin(a);
|
---|
260 | }
|
---|
261 | } |
---|