1 | #include "common.h"
|
---|
2 | #include "SampleGenerator.h"
|
---|
3 | #include "Halton.h"
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | //SampleGenerator::SampleGenerator() {}
|
---|
8 |
|
---|
9 | SampleGenerator::SampleGenerator(int numSamples, float radius):
|
---|
10 | mNumSamples(numSamples), mRadius(radius)
|
---|
11 | {}
|
---|
12 |
|
---|
13 |
|
---|
14 | PoissonDiscSampleGenerator::PoissonDiscSampleGenerator(int numSamples, float radius):
|
---|
15 | SampleGenerator(numSamples, radius)
|
---|
16 | {}
|
---|
17 |
|
---|
18 |
|
---|
19 | void PoissonDiscSampleGenerator::Generate(Sample2 *samples) const
|
---|
20 | {
|
---|
21 | static HaltonSequence halton;
|
---|
22 | float r[2];
|
---|
23 |
|
---|
24 | // generates poisson distribution on disc
|
---|
25 | float minDist = 2.0f / sqrt((float)mNumSamples);
|
---|
26 |
|
---|
27 | //cout << "minDist before= " << minDist << endl;
|
---|
28 |
|
---|
29 | for (int i = 0; i < mNumSamples; ++ i)
|
---|
30 | {
|
---|
31 | int tries = 0, totalTries = 0;
|
---|
32 |
|
---|
33 | // repeat until valid sample was found
|
---|
34 | while (1)
|
---|
35 | {
|
---|
36 | ++ tries;
|
---|
37 | ++ totalTries;
|
---|
38 |
|
---|
39 | halton.GetNext(2, r);
|
---|
40 |
|
---|
41 | const float rx = r[0] * 2.0f - 1.0f;
|
---|
42 | const float ry = r[1] * 2.0f - 1.0f;
|
---|
43 |
|
---|
44 | // check if in disk, else exit early
|
---|
45 | if (rx * rx + ry * ry > 1)
|
---|
46 | continue;
|
---|
47 |
|
---|
48 | bool sampleValid = true;
|
---|
49 |
|
---|
50 | // check poisson property
|
---|
51 | for (int j = 0; ((j < i) && sampleValid); ++ j)
|
---|
52 | {
|
---|
53 | const float dist =
|
---|
54 | sqrt((samples[j].x - rx) * (samples[j].x - rx) +
|
---|
55 | (samples[j].y - ry) * (samples[j].y - ry));
|
---|
56 |
|
---|
57 | if (dist < minDist)
|
---|
58 | sampleValid = false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (sampleValid)
|
---|
62 | {
|
---|
63 | samples[i].x = rx;
|
---|
64 | samples[i].y = ry;
|
---|
65 | break;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (tries > 2000)
|
---|
69 | {
|
---|
70 | minDist *= 0.9f;
|
---|
71 | tries = 0;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | //cout << "sample: " << samples[i].x << " " << i << " " << samples[i].y << " r: " << sqrt(samples[i].x * samples[i].x + samples[i].y * samples[i].y) << " tries: " << totalTries << endl;
|
---|
76 | }
|
---|
77 |
|
---|
78 | //cout << "minDist after= " << minDist << endl;
|
---|
79 | } |
---|