forked from corporateshark/poisson-disk-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poisson.cpp
388 lines (312 loc) · 9 KB
/
Poisson.cpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* \file Poisson.cpp
* \brief
*
* Poisson Disk Points Generator
*
* \version 1.1.1
* \date 23/05/2014
* \author Sergey Kosarevsky, 2014
* \author support@linderdaum.com http://www.linderdaum.com http://blog.linderdaum.com
*/
/*
To compile:
gcc Poisson.cpp -std=c++11 -lstdc++
*/
// 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/
#include <iostream>
#include <vector>
#include <random>
#include <stdint.h>
#include <time.h>
#include <fstream>
#include <memory.h>
#include "C:\OpenCV\build\include\opencv2\core\core.hpp"
#include "C:\OpenCV\build\include\opencv2\highgui\highgui.hpp"
#include "C:\OpenCV\build\include\opencv2\imgproc\imgproc.hpp"
///////////////// User selectable parameters ///////////////////////////////
const int NumPoints = 20000; // minimal number of points to generate
const bool Circle = true; // 'true' to fill a circle, 'false' to fill a rectangle
const int ImageSize = 1024; // generate RGB image [ImageSize x ImageSize]
const int k = 30; // refer to bridson-siggraph07-poissondisk.pdf for details
////////////////////////////////////////////////////////////////////////////
const char* Version = "1.1.1 (23/05/2014)";
const float MinDistance = sqrt( float(NumPoints) ) / float(NumPoints);
std::random_device rd;
std::mt19937 gen( rd() );
std::uniform_real_distribution<> dis( 0.0, 1.0 );
float* g_DensityMap = NULL;
struct sPoint
{
sPoint()
: x( 0 )
, y( 0 )
, m_Valid( false )
{}
sPoint( float X, float Y )
: x( X )
, y( Y )
, m_Valid( true )
{}
float x;
float y;
bool m_Valid;
//
bool IsInRectangle() const
{
return x >= 0 && y >= 0 && x <= 1 && y <= 1;
}
//
bool IsInCircle() const
{
float fx = x - 0.5f;
float fy = y - 0.5f;
return ( fx*fx + fy*fy ) <= 0.25f;
}
};
struct sGridPoint
{
sGridPoint( int X, int Y )
: x( X )
, y( Y )
{}
int x;
int y;
};
float RandomFloat()
{
return static_cast<float>( dis( gen ) );
}
float GetDistance( const sPoint& P1, const sPoint& P2 )
{
return sqrt( ( P1.x - P2.x ) * ( P1.x - P2.x ) + ( P1.y - P2.y ) * ( P1.y - P2.y ) );
}
sGridPoint ImageToGrid( const sPoint& P, float CellSize )
{
return sGridPoint( ( int )( P.x / CellSize ), ( int )( P.y / CellSize ) );
}
struct sGrid
{
sGrid( int W, int H, float CellSize )
: m_W( W )
, m_H( H )
, m_CellSize( CellSize )
{
m_Grid.resize( m_H );
for ( auto i = m_Grid.begin(); i != m_Grid.end(); i++ ) { i->resize( m_W ); }
}
void Insert( const sPoint& P )
{
sGridPoint G = ImageToGrid( P, m_CellSize );
m_Grid[ G.x ][ G.y ] = P;
}
bool IsInNeighbourhood( sPoint Point, float MinDist, float CellSize )
{
sGridPoint 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 < m_W && j >= 0 && j < m_H )
{
sPoint P = m_Grid[ i ][ j ];
if ( P.m_Valid && GetDistance( P, Point ) < MinDist ) { return true; }
}
}
}
return false;
}
private:
int m_W;
int m_H;
float m_CellSize;
std::vector< std::vector<sPoint> > m_Grid;
};
sPoint PopRandom( std::vector<sPoint>& Points )
{
std::uniform_int_distribution<> dis( 0, Points.size() - 1 );
int Idx = dis( gen );
sPoint P = Points[ Idx ];
Points.erase( Points.begin() + Idx );
return P;
}
sPoint GenerateRandomPointAround( const sPoint& P, float MinDist )
{
// start with non-uniform distribution
float R1 = RandomFloat();
float R2 = RandomFloat();
// radius should be between MinDist and 2 * MinDist
float Radius = MinDist * ( R1 + 1.0f );
// random angle
float Angle = 2 * 3.141592653589f * R2;
// the new point is generated around the point (x, y)
float X = P.x + Radius * cos( Angle );
float Y = P.y + Radius * sin( Angle );
return sPoint( X, Y );
}
std::vector<sPoint> GeneratePoissonPoints( float MinDist, int NewPointsCount, size_t NumPoints )
{
std::vector<sPoint> SamplePoints;
std::vector<sPoint> ProcessList;
// create the grid
float CellSize = MinDist / sqrt( 2.0f );
int GridW = ( int )ceil( 1.0f / CellSize );
int GridH = ( int )ceil( 1.0f / CellSize );
sGrid Grid( GridW, GridH, CellSize );
sPoint FirstPoint = sPoint( RandomFloat(), RandomFloat() );
// update containers
ProcessList.push_back( FirstPoint );
SamplePoints.push_back( FirstPoint );
Grid.Insert( FirstPoint );
// generate new points for each point in the queue
while ( !ProcessList.empty() && SamplePoints.size() < NumPoints )
{
// a progress indicator, kind of
if ( SamplePoints.size() % 100 == 0 ) std::cout << ".";
sPoint Point = PopRandom( ProcessList );
for ( int i = 0; i < NewPointsCount; i++ )
{
sPoint NewPoint = GenerateRandomPointAround( Point, MinDist );
bool Fits = Circle ? NewPoint.IsInCircle() : NewPoint.IsInRectangle();
if ( Fits && !Grid.IsInNeighbourhood( NewPoint, MinDist, CellSize ) )
{
ProcessList.push_back( NewPoint );
SamplePoints.push_back( NewPoint );
Grid.Insert( NewPoint );
continue;
}
}
}
std::cout << std::endl << std::endl;
return SamplePoints;
}
#if defined( __GNUC__ )
# define GCC_PACK(n) __attribute__((packed,aligned(n)))
#else
# define GCC_PACK(n) __declspec(align(n))
#endif // __GNUC__
#pragma pack(push, 1)
struct GCC_PACK( 1 ) sBMPHeader
{
// BITMAPFILEHEADER
unsigned short bfType;
uint32_t bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
uint32_t bfOffBits;
// BITMAPINFOHEADER
uint32_t biSize;
uint32_t biWidth;
uint32_t biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
uint32_t biCompression;
uint32_t biSizeImage;
uint32_t biXPelsPerMeter;
uint32_t biYPelsPerMeter;
uint32_t biClrUsed;
uint32_t biClrImportant;
};
#pragma pack(pop)
void SaveBMP( const char* FileName, const void* RawBGRImage, int Width, int Height )
{
sBMPHeader Header;
int ImageSize = Width * Height * 3;
Header.bfType = 0x4D * 256 + 0x42;
Header.bfSize = ImageSize + sizeof( sBMPHeader );
Header.bfReserved1 = 0;
Header.bfReserved2 = 0;
Header.bfOffBits = 0x36;
Header.biSize = 40;
Header.biWidth = Width;
Header.biHeight = Height;
Header.biPlanes = 1;
Header.biBitCount = 24;
Header.biCompression = 0;
Header.biSizeImage = ImageSize;
Header.biXPelsPerMeter = 6000;
Header.biYPelsPerMeter = 6000;
Header.biClrUsed = 0;
Header.biClrImportant = 0;
std::ofstream File( FileName, std::ios::out | std::ios::binary );
File.write( (const char*)&Header, sizeof( Header ) );
File.write( (const char*)RawBGRImage, ImageSize );
std::cout << "Saved " << FileName << std::endl;
}
unsigned char* LoadBMP( const char* FileName, int* OutWidth, int* OutHeight )
{
sBMPHeader Header;
std::ifstream File( FileName, std::ifstream::binary );
File.read( (char*)&Header, sizeof( Header ) );
*OutWidth = Header.biWidth;
*OutHeight = Header.biHeight;
size_t DataSize = 3 * Header.biWidth * Header.biHeight;
unsigned char* Img = new unsigned char[ DataSize ];
File.read( (char*)Img, DataSize );
return Img;
}
void LoadDensityMap( const char* FileName )
{
std::cout << "Loading density map " << FileName << std::endl;
int W, H;
unsigned char* Data = LoadBMP( FileName, &W, &H );
std::cout << "Loaded ( " << W << " x " << H << " ) " << std::endl;
if ( W != ImageSize || H != ImageSize )
{
std::cout << "ERROR: density map should be " << ImageSize << " x " << ImageSize << std::endl;
exit( 255 );
}
g_DensityMap = new float[ W * H ];
for ( int y = 0; y != H; y++ )
{
for ( int x = 0; x != W; x++ )
{
g_DensityMap[ x + y * W ] = float( Data[ 3 * (x + y * W) ] ) / 255.0f;
}
}
delete[]( Data );
}
void PrintBanner()
{
std::cout << "Poisson disk points generator" << std::endl;
std::cout << "Version " << Version << std::endl;
std::cout << "Sergey Kosarevsky, 2014" << std::endl;
std::cout << "support@linderdaum.com http://www.linderdaum.com http://blog.linderdaum.com" << std::endl;
std::cout << std::endl;
std::cout << "Usage: Poisson [density-map-rgb24.bmp]" << std::endl;
std::cout << std::endl;
}
int main( int argc, char** argv )
{
PrintBanner();
if ( argc > 1 )
{
LoadDensityMap( argv[1] );
}
// prepare PRNG
gen.seed( time( NULL ) );
std::vector<sPoint> Points = GeneratePoissonPoints( MinDistance, k, NumPoints );
// prepare BGR image
cv::Mat Img(cv::Size(ImageSize,ImageSize),CV_8UC1);
Img.setTo(0);
for ( auto i = Points.begin(); i != Points.end(); i++ )
{
int x = int( i->x * ImageSize );
int y = int( i->y * ImageSize );
if ( g_DensityMap )
{
// dice
float R = RandomFloat();
float P = g_DensityMap[ x + y * ImageSize ];
if ( R > P ) continue;
}
Img.ptr<uchar>(y)[x] = 255;
}
cv::imwrite("Poisson.bmp",Img);
return 0;
}