forked from corporateshark/poisson-disk-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoissonGenerator.h
285 lines (239 loc) · 6.87 KB
/
PoissonGenerator.h
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
/**
* \file PoissonGenerator.h
* \brief
*
* Poisson Disk Points Generator
*
* \version 1.2.0
* \date 28/12/2019
* \author Sergey Kosarevsky, 2014-2019
* \author [email protected] http://www.linderdaum.com http://blog.linderdaum.com
*/
/*
Usage example:
#define POISSON_PROGRESS_INDICATOR 1
#include "PoissonGenerator.h"
...
PoissonGenerator::DefaultPRNG PRNG;
const auto Points = PoissonGenerator::GeneratePoissonPoints( NumPoints, PRNG );
*/
// Fast Poisson Disk Sampling in Arbitrary Dimensions
// http://people.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf
// Implementation based on http://devmag.org.za/2009/05/03/poisson-disk-sampling/
/* Versions history:
* 1.2 Dec 28, 2019 Bugfixes; more consistent progress indicator; new command line options in demo app
* 1.1.6 Dec 7, 2019 Removed duplicate seed initialization; fixed warnings
* 1.1.5 Jun 16, 2019 In-class initializers; default ctors; naming, shorter code
* 1.1.4 Oct 19, 2016 POISSON_PROGRESS_INDICATOR can be defined outside of the header file, disabled by default
* 1.1.3a Jun 9, 2016 Update constructor for DefaultPRNG
* 1.1.3 Mar 10, 2016 Header-only library, no global mutable state
* 1.1.2 Apr 9, 2015 Output a text file with XY coordinates
* 1.1.1 May 23, 2014 Initialize PRNG seed, fixed uninitialized fields
* 1.1 May 7, 2014 Support of density maps
* 1.0 May 6, 2014
*/
#include <vector>
#include <random>
#include <stdint.h>
namespace PoissonGenerator
{
const char* Version = "1.2.0 (28/12/2019)";
class DefaultPRNG
{
public:
DefaultPRNG() = default;
explicit DefaultPRNG( uint32_t seed )
: gen_( seed )
{}
float randomFloat()
{
return static_cast<float>( dis_( gen_ ) );
}
int randomInt( int maxValue )
{
std::uniform_int_distribution<> disInt( 0, maxValue );
return disInt( gen_ );
}
private:
std::mt19937 gen_ = std::mt19937( std::random_device()() );
std::uniform_real_distribution<float> dis_ = std::uniform_real_distribution<float>( 0.0f, 1.0f );
};
struct Point
{
Point() = default;
Point( float X, float Y )
: x( X )
, y( Y )
, valid_( true )
{}
float x = 0.0f;
float y = 0.0f;
bool valid_ = false;
//
bool isInRectangle() const
{
return x >= 0 && y >= 0 && x <= 1 && y <= 1;
}
//
bool isInCircle() const
{
const float fx = x - 0.5f;
const float fy = y - 0.5f;
return ( fx*fx + fy*fy ) <= 0.25f;
}
};
struct GridPoint
{
GridPoint() = delete;
GridPoint( int X, int Y )
: x( X )
, y( Y )
{}
int x;
int y;
};
float getDistance( const Point& P1, const Point& P2 )
{
return sqrt( ( P1.x - P2.x ) * ( P1.x - P2.x ) + ( P1.y - P2.y ) * ( P1.y - P2.y ) );
}
GridPoint imageToGrid( const Point& P, float cellSize )
{
return GridPoint( ( int )( P.x / cellSize ), ( int )( P.y / cellSize ) );
}
struct Grid
{
Grid( int w, int h, float cellSize )
: w_( w )
, h_( h )
, cellSize_( cellSize )
{
grid_.resize( h_ );
for ( auto i = grid_.begin(); i != grid_.end(); i++ ) { i->resize( w ); }
}
void insert( const Point& p )
{
const GridPoint g = imageToGrid( p, cellSize_ );
grid_[ g.x ][ g.y ] = p;
}
bool isInNeighbourhood( const Point& point, float minDist, float cellSize )
{
const GridPoint g = imageToGrid( point, cellSize );
// number of adjucent cells to look for neighbour points
const int D = 5;
// scan the neighbourhood of the point in the grid
for ( int i = g.x - D; i < g.x + D; i++ )
{
for ( int j = g.y - D; j < g.y + D; j++ )
{
if ( i >= 0 && i < w_ && j >= 0 && j < h_ )
{
const Point P = grid_[ i ][ j ];
if ( P.valid_ && getDistance( P, point ) < minDist ) { return true; }
}
}
}
return false;
}
private:
int w_;
int h_;
float cellSize_;
std::vector< std::vector<Point> > grid_;
};
template <typename PRNG>
Point popRandom( std::vector<Point>& points, PRNG& generator )
{
const int idx = generator.randomInt( static_cast<int>(points.size())-1 );
const Point p = points[ idx ];
points.erase( points.begin() + idx );
return p;
}
template <typename PRNG>
Point generateRandomPointAround( const Point& p, float minDist, PRNG& generator )
{
// start with non-uniform distribution
const float R1 = generator.randomFloat();
const float R2 = generator.randomFloat();
// radius should be between MinDist and 2 * MinDist
const float radius = minDist * ( R1 + 1.0f );
// random angle
const float angle = 2 * 3.141592653589f * R2;
// the new point is generated around the point (x, y)
const float x = p.x + radius * cos( angle );
const float y = p.y + radius * sin( angle );
return Point( x, y );
}
/**
Return a vector of generated points
NewPointsCount - refer to bridson-siggraph07-poissondisk.pdf for details (the value 'k')
Circle - 'true' to fill a circle, 'false' to fill a rectangle
MinDist - minimal distance estimator, use negative value for default
**/
template <typename PRNG = DefaultPRNG>
std::vector<Point> generatePoissonPoints(
size_t numPoints,
PRNG& generator,
bool isCircle = true,
int newPointsCount = 30,
float minDist = -1.0f
)
{
numPoints *= 2;
if ( minDist < 0.0f )
{
minDist = sqrt( float(numPoints) ) / float(numPoints);
}
std::vector<Point> samplePoints;
std::vector<Point> processList;
if (!numPoints) return samplePoints;
// create the grid
const float cellSize = minDist / sqrt( 2.0f );
const int gridW = ( int )ceil( 1.0f / cellSize );
const int gridH = ( int )ceil( 1.0f / cellSize );
Grid grid( gridW, gridH, cellSize );
Point firstPoint;
do {
firstPoint = Point( generator.randomFloat(), generator.randomFloat() );
} while (!(isCircle ? firstPoint.isInCircle() : firstPoint.isInRectangle()));
// update containers
processList.push_back( firstPoint );
samplePoints.push_back( firstPoint );
grid.insert( firstPoint );
#if POISSON_PROGRESS_INDICATOR
size_t progress = 0;
#endif
// generate new points for each point in the queue
while ( !processList.empty() && samplePoints.size() < numPoints )
{
#if POISSON_PROGRESS_INDICATOR
// a progress indicator, kind of
if ((samplePoints.size()) % 1000 == 0)
{
const size_t newProgress = 200 * (samplePoints.size() + processList.size()) / numPoints;
if (newProgress != progress)
{
progress = newProgress;
std::cout << ".";
}
}
#endif // POISSON_PROGRESS_INDICATOR
const Point point = popRandom<PRNG>( processList, generator );
for ( int i = 0; i < newPointsCount; i++ )
{
const Point newPoint = generateRandomPointAround( point, minDist, generator );
const bool canFitPoint = isCircle ? newPoint.isInCircle() : newPoint.isInRectangle();
if ( canFitPoint && !grid.isInNeighbourhood( newPoint, minDist, cellSize ) )
{
processList.push_back( newPoint );
samplePoints.push_back( newPoint );
grid.insert( newPoint );
continue;
}
}
}
#if POISSON_PROGRESS_INDICATOR
std::cout << std::endl << std::endl;
#endif // POISSON_PROGRESS_INDICATOR
return samplePoints;
}
} // namespace PoissonGenerator