-
Notifications
You must be signed in to change notification settings - Fork 5
/
poisson_disk.hpp
304 lines (265 loc) · 7.33 KB
/
poisson_disk.hpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
////////////////////////////////////////////////////////////////////////////////
#include "poisson_disk.h"
#include "random.h"
#include "vec.h"
// -----------------------------------------------------------------------------
#include <iostream>
#include <array>
#include <cmath>
#include <cassert>
#include <vector>
////////////////////////////////////////////////////////////////////////////////
template<typename real_t, size_t n>
class Grid {
public:
typedef std::array<real_t, n> vXr;
typedef std::array<int, n> vXi;
private:
const real_t m_MinSqDist;
const real_t m_CellSize;
const vXr m_Extent;
const vXi m_Side;
const vXi m_Coeff;
std::vector<vXr> m_Content;
public:
Grid(real_t r, vXr extent)
: m_MinSqDist(r * r)
, m_CellSize(r / std::sqrt(n))
, m_Extent(extent)
, m_Side(computeSide(r, extent))
, m_Coeff(computeCoef(m_Side))
, m_Content(computeProd(m_Side), Vec::constant<real_t, n>(-1))
{ };
private:
static vXi computeSide(real_t r, vXr extent) {
vXi side;
for (size_t i = 0; i < n; ++i) {
side[i] = std::floor(1 + extent[i] * std::sqrt(n) / r);
}
return side;
}
static vXi computeCoef(vXi side) {
vXi coef;
coef[0] = 1;
for (size_t i = 1; i < n; ++i) {
coef[i] = coef[i - 1] * side[i - 1];
}
return coef;
}
static int computeProd(vXi side) {
int m = 1;
for (int c : side) { m *= c; }
return m;
}
private:
int toGridIndex(vXr p) {
int u = 0;
for (size_t i = 0; i < n; ++i) {
assert(p[i] >= 0 && p[i] <= m_Extent[i]);
u += m_Coeff[i] * std::floor(p[i] / m_CellSize);
}
assert(u >= 0 && u < (int) m_Content.size());
return u;
}
vXi toGridVect(vXr p) {
vXi u;
for (size_t i = 0; i < n; ++i) {
u[i] = std::floor(p[i] / m_CellSize);
}
return u;
}
bool recurseIsOccupied(vXr p, vXi u, int v, int i) {
if (i == n) {
if (m_Content[v][0] < 0) {
return false; // Cell is empty
} else {
return Vec::sqDistance(p, m_Content[v]) <= m_MinSqDist;
}
} else {
const int d = static_cast<int>(std::ceil(std::sqrt(n)));
for (int j = -d; j <= d; ++j) {
if (u[i] + j < 0 || u[i] + j >= m_Side[i]) {
// Don't recurse
} else if (recurseIsOccupied(p, u, v + j * m_Coeff[i], i + 1)) {
return true; // Stop recursion
}
}
return false;
}
}
public:
void insertInitPoint(vXr p) {
int u = toGridIndex(p);
m_Content[u] = p;
}
void insertPoint(vXr p) {
int u = toGridIndex(p);
assert(m_Content[u][0] < 0);
m_Content[u] = p;
}
bool isNeighborhoodOccupied(vXr p) {
return recurseIsOccupied(p, toGridVect(p), toGridIndex(p), 0);
}
};
////////////////////////////////////////////////////////////////////////////////
template<typename real_t, size_t n>
void PoissonSampling<real_t, n>::box(
int maxAttempts, std::vector<vXr> &result) const
{
domain(maxAttempts, PoissonSampling<real_t, n>::Domain(), result);
}
// Implemented after:
// Fast Poisson disk sampling in arbitrary dimensions, R. Bridson, ACM SIGGRAPH 2007 Sketches Program.
template<typename real_t, size_t n>
void PoissonSampling<real_t, n>::domain(
int maxAttempts,
const PoissonSampling<real_t, n>::Domain &outputArea,
std::vector<vXr> &result) const
{
// Data structures
const int maxDomainTrials = outputArea.maxTrials();
std::vector<vXr> active;
Grid<real_t, n> grid(m_MinDist, m_Extent);
// Initialization
if (!result.empty()) {
// Update containers
for (vXr p : result) {
active.push_back(p);
grid.insertInitPoint(p);
}
} else {
// Start with a random initial point
vXr firstPoint;
int j;
for (j = 0; j < maxDomainTrials; ++j) {
for (size_t i = 0; i < n; ++i) {
firstPoint[i] = Random::get<real_t>() * m_Extent[i];
}
if (outputArea.contains(firstPoint, m_Extent)) { break; }
}
if (j == maxDomainTrials) { return; }
// Update containers
result.push_back(firstPoint);
active.push_back(firstPoint);
grid.insertPoint(firstPoint);
}
// Main loop
while (!active.empty()) {
int selectedIndex = Random::uniform_int<int>(0, active.size() - 1);
const vXr currentPoint = active[selectedIndex];
int i;
for (i = 0; i < maxAttempts; ++i) {
vXr newPoint;
int j;
// Try to find a point both in the domain and the annulus (r, 2*r)
for (j = 0; j < maxDomainTrials; ++j) {
newPoint = currentPoint + Random::annulus<real_t, n>(m_MinDist, 2 * m_MinDist);
if (outputArea.contains(newPoint, m_Extent)) { break; }
}
if (j == maxDomainTrials) {
i = maxAttempts;
break;
} else if (!grid.isNeighborhoodOccupied(newPoint)) {
result.push_back(newPoint);
active.push_back(newPoint);
grid.insertPoint(newPoint);
}
}
if (i == maxAttempts) {
// Drop the selected point
std::swap(active[selectedIndex], active.back());
active.pop_back();
}
}
}
// -----------------------------------------------------------------------------
template<typename real_t, size_t n>
void PoissonSampling<real_t, n>::contour(
const std::vector<vXr> &poly, std::vector<vXr> &result) const
{
Grid<real_t, n> grid(m_MinDist, m_Extent);
for (int i = 0; i < (int) poly.size(); ++i) {
vXr a = poly[i];
vXr b = poly[(i + 1) % poly.size()];
real_t l = distance(a, b);
real_t minCoef = 0;
real_t maxCoef = std::min(l, m_MinDist);
int unsuccessfulTimes = 0;
while (minCoef < l && unsuccessfulTimes < maxContourTrials) {
real_t s = Random::uniform_real<real_t>(minCoef, maxCoef);
vXr p = s/l * a + (1 - s/l) * b;
if (!grid.isNeighborhoodOccupied(p)) {
unsuccessfulTimes = 0;
result.push_back(p);
grid.insertPoint(p);
minCoef = s + m_MinDist;
maxCoef = std::min(l, s + 2 * m_MinDist);
} else {
++unsuccessfulTimes;
if (unsuccessfulTimes >= maxContourTrials) {
minCoef = maxCoef;
maxCoef = std::min(l, 2 * maxCoef);
unsuccessfulTimes = 0;
}
}
}
}
}
// -----------------------------------------------------------------------------
template<typename real_t, size_t n>
void PoissonSampling<real_t, n>::subset(
const std::vector<vXr> &soup, std::vector<vXr> &result) const
{
Grid<real_t, n> grid(m_MinDist, m_Extent);
std::vector<vXr> active(soup);
// Initialization
for (vXr p : result) {
grid.insertInitPoint(p);
}
while (!active.empty()) {
int selectedIndex = Random::uniform_int<int>(0, active.size() - 1);
const vXr newPoint = active[selectedIndex];
if (!grid.isNeighborhoodOccupied(newPoint)) {
result.push_back(newPoint);
grid.insertPoint(newPoint);
}
// Drop the selected point
std::swap(active[selectedIndex], active.back());
active.pop_back();
}
}
// -----------------------------------------------------------------------------
template<typename real_t, size_t n>
void PoissonSampling<real_t, n>::naive(
std::vector<vXr> &result) const
{
Domain defaultDomain;
const int maxDomainTrials = defaultDomain.maxTrials();
const real_t minSqDist = m_MinDist * m_MinDist;
int unsuccessfulTimes = 0;
while (unsuccessfulTimes < maxDomainTrials) {
// Generate new random point
vXr newPoint;
for (size_t i = 0; i < n; ++i) {
newPoint[i] = Random::get<real_t>() * m_Extent[i];
}
if (!defaultDomain.contains(newPoint, m_Extent)) {
++unsuccessfulTimes;
continue;
}
// Test against existing neighbors
bool flag = false;
for (vXr q : result) {
if (Vec::sqDistance(newPoint, q) <= minSqDist) {
flag = true;
break;
}
}
if (flag == false) {
unsuccessfulTimes = 0;
result.push_back(newPoint);
} else {
++unsuccessfulTimes;
}
}
}