-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
340 lines (289 loc) · 11.2 KB
/
main.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
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <GLFW/glfw3.h>
#include <vector>
#include <stdlib.h>
#include <math.h>
#include <limits>
#include <omp.h>
#include <string>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "tinyfiledialogs.h"
std::vector<unsigned char> currentImage;
std::string savePath;
std::vector<int> voronoiCellSizes;
std::vector<int> voronoiCells;
struct Point
{
int x, y;
unsigned char r, g, b;
};
GLuint LoadTextureFromMemory(const std::vector<unsigned char> &img, int width, int height)
{
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data());
return texture;
}
GLuint LoadTextureFromFile(const char *filename, int &width, int &height)
{
GLuint texture;
glGenTextures(1, &texture);
int n;
unsigned char *data = stbi_load(filename, &width, &height, &n, 0);
if (data)
{
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
stbi_image_free(data);
return texture;
}
std::vector<unsigned char> generateVoronoiDiagram(int width, int height, int numPoints, int seed, bool grayscale, bool drawBoundaries = true)
{
srand(seed);
std::vector<unsigned char> img(width * height * 3, 255);
std::vector<Point> points;
for (int i = 0; i < numPoints; ++i)
{
points.push_back({rand() % width, rand() % height, static_cast<unsigned char>(rand() % 256), static_cast<unsigned char>(rand() % 256), static_cast<unsigned char>(rand() % 256)});
}
voronoiCellSizes.resize(numPoints, 0);
voronoiCells.resize(width * height, -1);
#pragma omp parallel for
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
Point p{x, y};
float minDist = std::numeric_limits<float>::max();
int closestPointIndex = -1;
for (int i = 0; i < numPoints; ++i)
{
const Point &point = points[i];
float dist = std::pow(p.x - point.x, 2) + std::pow(p.y - point.y, 2);
if (dist < minDist)
{
minDist = dist;
closestPointIndex = i;
}
}
voronoiCells[y * width + x] = closestPointIndex;
voronoiCellSizes[closestPointIndex]++;
const Point &closestPoint = points[closestPointIndex];
unsigned char r = closestPoint.r;
unsigned char g = closestPoint.g;
unsigned char b = closestPoint.b;
if (grayscale)
{
// converts the RGB color to grayscale using the formula 0.299*R + 0.587*G + 0.114*B,
// which is a common method to convert color to grayscale.
// unsigned char gray = 0.299 * r + 0.587 * g + 0.114 * b;
// converts the RGB color to grayscale by normalizing the closest point index to the range [0, 255].
unsigned char gray = closestPointIndex * 255.0 / numPoints;
r = g = b = gray;
}
img[(y * width + x) * 3 + 0] = r;
img[(y * width + x) * 3 + 1] = g;
img[(y * width + x) * 3 + 2] = b;
}
}
if (drawBoundaries)
{
// Draw black boundaries between Voronoi cells
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
for (int dir = 0; dir < 4; ++dir)
{
int nx = x + dx[dir];
int ny = y + dy[dir];
if (nx < 0 || nx >= width || ny < 0 || ny >= height || voronoiCells[ny * width + nx] != voronoiCells[y * width + x])
{
img[(y * width + x) * 3 + 0] = 0;
img[(y * width + x) * 3 + 1] = 0;
img[(y * width + x) * 3 + 2] = 0;
break;
}
}
}
}
}
return img;
}
int main()
{
// Setup window
glfwInit();
GLFWwindow *window = glfwCreateWindow(1280, 720, "ImGuiVoronoiGen", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Setup ImGui context
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
// Setup ImGui style
ImGui::StyleColorsDark();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
// Our state
int width = 1000;
int height = 1000;
int numPoints = 100;
int seed = 0;
int styleIdx = 1;
bool randomSeed = true;
bool grayscale = false;
bool drawBoundaries = true;
GLuint my_image_texture = 0;
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
// Start the ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
ImGui::Begin("Input Parameters");
const char *styles[] = {"Classic", "Dark", "Light"};
ImGui::Combo("Style", &styleIdx, styles, IM_ARRAYSIZE(styles));
if (styleIdx == 0)
{
ImGui::StyleColorsClassic();
}
else if (styleIdx == 1)
{
ImGui::StyleColorsDark();
}
else if (styleIdx == 2)
{
ImGui::StyleColorsLight();
}
ImGui::InputInt("Width", &width);
ImGui::InputInt("Height", &height);
ImGui::InputInt("Number of points", &numPoints);
ImGui::InputInt("Seed", &seed);
if (ImGui::Checkbox("Random Seed", &randomSeed))
{
if (randomSeed)
{
seed = rand();
}
}
if (ImGui::Checkbox("Draw Boundaries", &drawBoundaries))
{
currentImage = generateVoronoiDiagram(width, height, numPoints, seed, grayscale, drawBoundaries);
my_image_texture = LoadTextureFromMemory(currentImage, width, height);
}
if (ImGui::Checkbox("Grayscale", &grayscale))
{
currentImage = generateVoronoiDiagram(width, height, numPoints, seed, grayscale, drawBoundaries);
my_image_texture = LoadTextureFromMemory(currentImage, width, height);
}
if (ImGui::Button("Generate"))
{
if (randomSeed)
{
seed = rand();
}
currentImage = generateVoronoiDiagram(width, height, numPoints, seed, grayscale, drawBoundaries);
my_image_texture = LoadTextureFromMemory(currentImage, width, height);
}
if (ImGui::Button("Save"))
{
const char *filterPatterns[1] = {"*.png"};
const char *filePath = tinyfd_saveFileDialog("Save Image", "", 1, filterPatterns, NULL);
if (filePath != NULL)
{
savePath = filePath;
stbi_write_png(savePath.c_str(), width, height, 3, currentImage.data(), width * 3);
}
}
if (ImGui::BeginPopupModal("Save Image", NULL, ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::Text("Enter the path where you want to save the image:");
ImGui::InputTextWithHint("", "Path", &savePath[0], savePath.size() + 1);
if (ImGui::Button("OK", ImVec2(120, 0)))
{
stbi_write_png(savePath.c_str(), width, height, 3, currentImage.data(), width * 3);
ImGui::CloseCurrentPopup();
}
ImGui::SetItemDefaultFocus();
ImGui::EndPopup();
}
ImGui::End();
ImGui::SetNextWindowSize(ImVec2(600, 600), ImGuiCond_FirstUseEver);
ImGui::Begin("Voronoi Tessellation");
ImVec2 available = ImGui::GetContentRegionAvail();
float window_aspect = available.x / available.y;
float image_aspect = (float)width / (float)height;
ImVec2 image_size;
if (window_aspect > image_aspect)
{
image_size.x = available.y * image_aspect;
image_size.y = available.y;
}
else
{
image_size.x = available.x;
image_size.y = available.x / image_aspect;
}
ImGui::Image((void *)(intptr_t)my_image_texture, image_size);
if (ImGui::IsItemHovered() && !currentImage.empty())
{
ImVec2 mouse_pos = ImGui::GetMousePos();
ImVec2 image_pos = ImGui::GetItemRectMin();
ImVec2 mouse_pos_in_image = ImVec2(mouse_pos.x - image_pos.x, mouse_pos.y - image_pos.y);
mouse_pos_in_image.x /= image_size.x;
mouse_pos_in_image.y /= image_size.y;
int pixel_x = static_cast<int>(mouse_pos_in_image.x * width);
int pixel_y = static_cast<int>(mouse_pos_in_image.y * height);
if (pixel_x >= 0 && pixel_x < width && pixel_y >= 0 && pixel_y < height && (pixel_y * width + pixel_x) * 3 + 2 < currentImage.size())
{
unsigned char r = currentImage[(pixel_y * width + pixel_x) * 3 + 0];
unsigned char g = currentImage[(pixel_y * width + pixel_x) * 3 + 1];
unsigned char b = currentImage[(pixel_y * width + pixel_x) * 3 + 2];
ImGui::BeginTooltip();
int cell = voronoiCells[pixel_y * width + pixel_x];
int cellSize = voronoiCellSizes[cell];
ImGui::Text("Cell id: %d", cell);
ImGui::Text("Cell size: %d pixels", cellSize);
ImGui::Text("Pixel: (%d, %d)", pixel_x, pixel_y);
ImGui::Text("Color: (%d, %d, %d)", r, g, b);
ImGui::EndTooltip();
}
}
ImGui::End();
}
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}