-
Notifications
You must be signed in to change notification settings - Fork 27
/
Poisson.cpp
233 lines (182 loc) · 6.47 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
/**
* \file Poisson.cpp
* \brief
*
* Poisson Disk Points Generator example
*
* \version 1.6.1
* \date 16/02/2024
* \author Sergey Kosarevsky, 2014-2024
* \author [email protected] http://www.linderdaum.com http://blog.linderdaum.com
*/
/*
To compile:
gcc Poisson.cpp -std=c++17 -lstdc++
*/
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <memory.h>
#include <vector>
#define POISSON_PROGRESS_INDICATOR 1
#include "PoissonGenerator.h"
#include "argh/argh.h"
///////////////// User selectable parameters ///////////////////////////////
const int kNumPointsDefaultPoisson = 20000; // default number of points to generate for Poisson disk
const int kNumPointsDefaultVogel = 2000; // default number of points to generate for Vogel disk
const int kNumPointsDefaultJittered = 2500; // default number of points to generate for jittered grid
const int kImageSize = 512; // generate RGB image [ImageSize x ImageSize]
////////////////////////////////////////////////////////////////////////////
float* g_DensityMap = nullptr;
#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;
const 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 != kImageSize || H != kImageSize) {
std::cout << "ERROR: density map should be " << kImageSize << " x " << kImageSize << 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 " << PoissonGenerator::Version << std::endl;
std::cout << "Sergey Kosarevsky, 2014-2023" << std::endl;
std::cout << "[email protected] http://www.linderdaum.com http://blog.linderdaum.com" << std::endl;
std::cout << std::endl;
std::cout << "Usage: Poisson [density-map-rgb24.bmp] [--raw-points] [--num-points=<value>] [--square] [--vogel-disk | --jittered-grid | "
"--hammersley]"
<< std::endl;
std::cout << std::endl;
}
int main(int argc, char** argv) {
PrintBanner();
argh::parser cmdl(argv);
if (!cmdl[1].empty()) {
LoadDensityMap(cmdl[1].c_str());
}
const bool cmdRawPointsOutput = cmdl[{"--raw-points"}];
const bool cmdSquare = cmdl[{"--square"}];
const bool cmdVogelDisk = cmdl[{"--vogel-disk"}];
const bool cmdJitteredGrid = cmdl[{"--jittered-grid"}];
;
const bool cmdHammersley = cmdl[{"--hammersley"}];
;
unsigned int numPoints;
cmdl("num-points", cmdVogelDisk ? kNumPointsDefaultVogel : (cmdJitteredGrid ? kNumPointsDefaultJittered : kNumPointsDefaultPoisson)) >>
numPoints;
std::cout << "NumPoints = " << numPoints << std::endl;
PoissonGenerator::DefaultPRNG PRNG;
const auto Points = cmdVogelDisk ? PoissonGenerator::generateVogelPoints(numPoints)
: cmdJitteredGrid ? PoissonGenerator::generateJitteredGridPoints(numPoints, PRNG, !cmdSquare)
: cmdHammersley ? PoissonGenerator::generateHammersleyPoints(numPoints)
: PoissonGenerator::generatePoissonPoints(numPoints, PRNG, !cmdSquare);
// prepare BGR image
const size_t DataSize = 3 * kImageSize * kImageSize;
unsigned char* Img = new unsigned char[DataSize];
memset(Img, 0, DataSize);
for (auto i = Points.begin(); i != Points.end(); i++) {
int x = int(i->x * kImageSize);
int y = int(i->y * kImageSize);
if (x < 0 || y < 0 || x >= kImageSize || y >= kImageSize)
continue;
if (g_DensityMap) {
// dice
float R = PRNG.randomFloat();
float P = g_DensityMap[x + y * kImageSize];
if (R > P)
continue;
}
int Base = 3 * (x + y * kImageSize);
Img[Base + 0] = Img[Base + 1] = Img[Base + 2] = 255;
}
SaveBMP("Points.bmp", Img, kImageSize, kImageSize);
delete[] (Img);
// dump points to a text file
std::ofstream File("points.txt", std::ios::out);
if (cmdRawPointsOutput) {
File << "NumPoints = " << Points.size() << std::endl;
for (const auto& p : Points) {
File << p.x << " " << p.y << std::endl;
}
} else {
File << "const vec2 points[" << Points.size() << "]" << std::endl;
File << "{" << std::endl;
File << std::fixed << std::setprecision(6);
for (const auto& p : Points) {
File << "\tvec2(" << p.x << "f, " << p.y << "f)," << std::endl;
}
File << "};" << std::endl;
}
return 0;
}