-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
614 lines (508 loc) · 21.1 KB
/
Copy pathmain.cpp
File metadata and controls
614 lines (508 loc) · 21.1 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
#include <cmath>
#include <chrono>
#include <memory>
#include <fstream>
#include <unistd.h>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <iomanip>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core/utils/filesystem.hpp>
class Circle
{
public:
Circle(float radius, int x = -1, int y = -1) : radius(radius), x(x), y(y) {}
void setPoint(int x, int y) {
this->x = x;
this->y = y;
}
void print() const {
std::cout << "Radius: " << radius << ", Center: ";
if (x == -1 && y == -1) {
std::cout << "Not Set";
} else {
std::cout << "(" << x << ", " << y << ")";
}
std::cout << std::endl;
}
int getX() const { return x; }
int getY() const { return y; }
float getRadius() const { return radius; }
private:
float radius;
int x, y;
};
class Grid
{
public:
Grid(int width, int height, int minCellSize)
: width(width), height(height), minCellSize(minCellSize) {
// Calculate the number of cells in each dimension
numCellsX = (width + minCellSize - 1) / minCellSize;
numCellsY = (height + minCellSize - 1) / minCellSize;
cellSizeX = static_cast<float>(width) / numCellsX;
cellSizeY = static_cast<float>(height) / numCellsY;
bins.resize(numCellsX * numCellsY);
}
void addCircle(const Circle& circle) {
int cellIndex = getCellIndex(circle.getX(), circle.getY());
if (cellIndex != -1) {
bins[cellIndex].push_back(circle);
}
}
std::vector<Circle> getBin(int x, int y, int distance = 0) const {
std::vector<Circle> result;
int cellIndex = getCellIndex(x, y);
if (cellIndex == -1) {
return result;
}
std::vector<std::pair<int, int>> offsets = { {0, 0} };
if (distance == 1) {
offsets = { {0, 0}, {-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1} };
}
if (distance == 2) {
offsets = {
{0, 0}, {-2, -2}, {-1, -2}, {0, -2}, {1, -2}, {2, -2},
{-2, -1}, {-1, -1}, {0, -1}, {1, -1}, {2, -1},
{-2, 0}, {-1, 0}, {1, 0}, {2, 0},
{-2, 1}, {-1, 1}, {0, 1}, {1, 1}, {2, 1},
{-2, 2}, {-1, 2}, {0, 2}, {1, 2}, {2, 2}
};
}
for (const auto& offset : offsets) {
int newX = x + offset.first * static_cast<int>(cellSizeX);
int newY = y + offset.second * static_cast<int>(cellSizeY);
int newCellIndex = getCellIndex(newX, newY);
if (newCellIndex != -1) {
result.insert(result.end(), bins[newCellIndex].begin(), bins[newCellIndex].end());
}
}
return result;
}
void print() const {
for (int i = 0; i < numCellsY; i++) {
for (int j = 0; j < numCellsX; j++) {
std::cout << "Grid [" << i << ", " << j << "] Contains:\n";
const std::vector<Circle>& circles = bins[i * numCellsX + j];
for (const auto& circle : circles) {
circle.print();
}
}
}
}
int getNumCellsX() const { return numCellsX; }
int getNumCellsY() const { return numCellsY; }
float getCellSizeX() const { return cellSizeX; }
float getCellSizeY() const { return cellSizeY; }
private:
int width, height, minCellSize;
int numCellsX, numCellsY;
float cellSizeX, cellSizeY;
std::vector<std::vector<Circle>> bins;
int getCellIndex(int x, int y) const {
if (x < 0 || x >= width || y < 0 || y >= height) {
return -1;
}
int cellX = static_cast<int>(x / cellSizeX);
int cellY = static_cast<int>(y / cellSizeY);
return cellY * numCellsX + cellX;
}
};
class CircleGroup
{
public:
CircleGroup(int width,
int height,
float mean,
float mean_delta,
float std_dev,
float std_dev_delta,
int count,
std::vector<Circle> excludeCircles = {})
: width(width),
height(height),
mean(mean),
mean_delta(mean_delta),
std_dev(std_dev),
std_dev_delta(std_dev_delta),
count(count),
maxRadius(0.0f),
calculatedMean(0.0f),
calculatedStdDev(0.0f),
excludeCircles(std::move(excludeCircles))
{
// if no circles requested, skip all packing logic
if (count <= 0) {
// leave circles empty, stats = 0
grid.reset();
return;
}
bool goodPack = false;
while (!goodPack) {
circles.clear();
grid.reset();
generateCircles();
goodPack = assignCircleCenters();
}
}
const std::vector<Circle>& getCircles() const {
return circles;
}
float getMaxRadius() const {
return maxRadius;
}
std::pair<float, float> getStats() const {
return { calculatedMean, calculatedStdDev };
}
void print() {
for (const auto& circle : getCircles()) {
circle.print();
}
std::cout << "Maximum Radius: " << getMaxRadius() << std::endl;
std::cout << "Mean Radius: " << calculatedMean << std::endl;
std::cout << "Standard Deviation: " << calculatedStdDev << std::endl;
}
const Grid& getGrid() const {
return *grid;
}
private:
int width, height;
float mean, mean_delta;
float std_dev, std_dev_delta;
float maxRadius;
int count;
float calculatedMean, calculatedStdDev;
std::vector<Circle> circles;
std::unique_ptr<Grid> grid;
std::vector<Circle> excludeCircles;
bool hasGrid() const noexcept { return static_cast<bool>(grid); }
void generateCircles() {
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> dist(mean, std_dev);
bool withinDelta = false;
while (!withinDelta) {
circles.clear();
maxRadius = 0.0f;
for (int i = 0; i < count; ++i) {
float radius = dist(gen);
if (radius < 0) radius = -radius; // Ensure radius is non-negative
circles.emplace_back(radius);
if (radius > maxRadius) {
maxRadius = radius;
}
}
// Calculate mean and standard deviation of the generated radii
float sum = 0;
for (const auto& circle : circles) {
sum += circle.getRadius();
}
calculatedMean = sum / circles.size();
float varianceSum = 0;
for (const auto& circle : circles) {
varianceSum += std::pow(circle.getRadius() - calculatedMean, 2);
}
calculatedStdDev = std::sqrt(varianceSum / circles.size());
// Check if the calculated mean and standard deviation are within the specified deltas
withinDelta = std::abs(calculatedMean - mean) <= mean_delta &&
std::abs(calculatedStdDev - std_dev) <= std_dev_delta;
}
std::sort(circles.begin(), circles.end(), [](const Circle& a, const Circle& b) {
return a.getRadius() > b.getRadius();
});
}
bool assignCircleCenters() {
grid = std::make_unique<Grid>(width, height, static_cast<int>(std::ceil(maxRadius)));
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distX(0, width);
std::uniform_int_distribution<> distY(0, height);
int numCircles = 0;
int maxcount = 50000;
for (auto& circle : circles) {
bool placed = false;
int counter = 0;
if (numCircles / count >= 0.5) {
double exp = (count * 3.1415926535 * calculatedMean) / (height * width);
maxcount = (numCircles / count) * pow(50000, exp);
}
while (!placed) {
counter++;
if (counter > maxcount) {
//std::cout << "RESET" << std::endl;
return false;
}
int x = distX(gen);
int y = distY(gen);
if (x - circle.getRadius() >= 0 && x + circle.getRadius() <= width &&
y - circle.getRadius() >= 0 && y + circle.getRadius() <= height) {
} else {
continue;
}
if (doesOverlap(x, y, circle.getRadius())) {
continue;
}
if (doesOverlapFully(x, y, circle.getRadius(), excludeCircles)) {
continue;
}
circle.setPoint(x, y);
grid->addCircle(circle);
placed = true;
numCircles++;
}
}
return true;
}
bool doesOverlap(int newX, int newY, float newRadius) const {
std::vector<Circle> nearbyCircles = grid->getBin(newX, newY, 2);
for (const auto& circle : nearbyCircles) {
float minDistance = circle.getRadius() + newRadius;
if (std::abs(circle.getX() - newX) > minDistance) {
continue;
}
if (std::abs(circle.getY() - newY) > minDistance) {
continue;
}
if (std::pow(circle.getX() - newX, 2) + std::pow(circle.getY() - newY, 2) <= std::pow(minDistance, 2)) {
return true;
}
}
return false;
}
bool doesOverlapFully(int newX, int newY, float newRadius, const std::vector<Circle>& excludeCircles) const {
if (excludeCircles.empty()) {
return false;
}
for (const auto& circle : excludeCircles) {
float distanceSquared = std::pow(circle.getX() - newX, 2) + std::pow(circle.getY() - newY, 2);
float radiusDifference = std::abs(circle.getRadius() - newRadius);
float radiusSum = circle.getRadius() + newRadius;
// Check if the new circle is fully contained within the existing circle
if (distanceSquared <= std::pow(circle.getRadius() - newRadius, 2)) {
return true;
}
// Check if the existing circle is fully contained within the new circle
if (distanceSquared <= std::pow(newRadius - circle.getRadius(), 2)) {
return true;
}
}
return false;
}
};
void writeXYR(const std::string& filepath, const std::vector<Circle>& circles) {
std::ofstream ofs(filepath);
if (!ofs) {
std::cerr << "Error: could not open '" << filepath << "' for writing\n";
return;
}
ofs.setf(std::ios::fixed);
ofs << std::setprecision(3);
for (const auto& c : circles) {
ofs << c.getX() << ' ' // x [px]
<< c.getY() << ' ' // y [px]
<< c.getRadius() // r [px]
<< '\n';
}
}
void saveImage(const cv::Mat& image, const std::string& outputDir, int index) {
// Create output directory if it does not exist
cv::utils::fs::createDirectory(outputDir);
// Save the original image with index
std::string originalFilename = outputDir + "/circles_" + std::to_string(index) + ".png";
cv::imwrite(originalFilename, image);
// Create a blurred version of the image
cv::Mat blurredImage;
cv::GaussianBlur(image, blurredImage, cv::Size(9, 9), 0);
// Save the blurred image with index
std::string blurredFilename = outputDir + "/circles_blurred_" + std::to_string(index) + ".png";
cv::imwrite(blurredFilename, blurredImage);
}
void drawGridLines(cv::Mat& image, const Grid& grid) {
int numCellsX = grid.getNumCellsX();
int numCellsY = grid.getNumCellsY();
float cellSizeX = grid.getCellSizeX();
float cellSizeY = grid.getCellSizeY();
for (int i = 0; i <= numCellsX; ++i) {
cv::line(image, cv::Point(i * cellSizeX, 0), cv::Point(i * cellSizeX, 1000), cv::Scalar(0, 0, 255));
}
for (int i = 0; i <= numCellsY; ++i) {
cv::line(image, cv::Point(0, i * cellSizeY), cv::Point(1000, i * cellSizeY), cv::Scalar(0, 0, 255));
}
}
void drawCircles(cv::Mat& image, const std::vector<Circle>& circles, cv::Scalar color) {
for (const auto& circle : circles) {
cv::circle(image, cv::Point(circle.getX(), circle.getY()), circle.getRadius(), color, -1);
cv::circle(image, cv::Point(circle.getX(), circle.getY()), 1, color, -1);
}
}
std::string getExecutablePath() {
char buffer[1024];
ssize_t count = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
if (count != -1) {
buffer[count] = '\0';
return std::string(buffer);
}
return "";
}
void loadConfig(const std::string& filename, int& width, int& height, int& numOutputs, float& g1Mean, float& g1Mean_delta, float& g1Std_dev, float& g1Std_dev_delta, int& g1Count, cv::Scalar& g1Color, float& g2Mean, float& g2Mean_delta, float& g2Std_dev, float& g2Std_dev_delta, int& g2Count, cv::Scalar& g2Color) {
std::ifstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Could not open config file: " + filename);
}
std::string line;
while (std::getline(file, line)) {
// Remove whitespace from the beginning and end of the line
line.erase(0, line.find_first_not_of(" \t\n\r"));
line.erase(line.find_last_not_of(" \t\n\r") + 1);
// Skip empty lines and lines starting with '#'
if (line.empty() || line[0] == '#') {
continue;
}
std::istringstream iss(line);
std::string key;
if (std::getline(iss, key, '=')) {
std::string value;
if (std::getline(iss, value)) {
if (key == "width") width = std::stoi(value);
else if (key == "height") height = std::stoi(value);
else if (key == "numOutputs") numOutputs = std::stoi(value);
else if (key == "group1_mean") g1Mean = std::stof(value);
else if (key == "group1_mean_delta") g1Mean_delta = std::stof(value);
else if (key == "group1_std_dev") g1Std_dev = std::stof(value);
else if (key == "group1_std_dev_delta") g1Std_dev_delta = std::stof(value);
else if (key == "group1_count") g1Count = std::stoi(value);
else if (key == "group1_color_r") g1Color[2] = std::stoi(value);
else if (key == "group1_color_g") g1Color[1] = std::stoi(value);
else if (key == "group1_color_b") g1Color[0] = std::stoi(value);
else if (key == "group2_mean") g2Mean = std::stof(value);
else if (key == "group2_mean_delta") g2Mean_delta = std::stof(value);
else if (key == "group2_std_dev") g2Std_dev = std::stof(value);
else if (key == "group2_std_dev_delta") g2Std_dev_delta = std::stof(value);
else if (key == "group2_count") g2Count = std::stoi(value);
else if (key == "group2_color_r") g2Color[2] = std::stoi(value);
else if (key == "group2_color_g") g2Color[1] = std::stoi(value);
else if (key == "group2_color_b") g2Color[0] = std::stoi(value);
}
}
}
}
std::string formatFloat(float value) {
std::ostringstream out;
out << std::fixed << std::setprecision(2) << value;
return out.str();
}
std::queue<int> taskQueue;
std::mutex queueMutex;
std::condition_variable condVar;
bool stopThreads = false;
void workerFunction(int width, int height,
float g1Mean, float g1Mean_delta, float g1Std_dev, float g1Std_dev_delta, int g1Count, cv::Scalar g1Color,
float g2Mean, float g2Mean_delta, float g2Std_dev, float g2Std_dev_delta, int g2Count, cv::Scalar g2Color,
const std::string& outputDir, int totalTasks)
{
while (true) {
int taskIndex;
{
std::unique_lock<std::mutex> lock(queueMutex);
condVar.wait(lock, [] { return !taskQueue.empty() || stopThreads; });
if (stopThreads && taskQueue.empty()) return;
taskIndex = taskQueue.front();
taskQueue.pop();
}
// Group 1 (always)
CircleGroup g1(width, height, g1Mean, g1Mean_delta, g1Std_dev, g1Std_dev_delta, g1Count);
const std::vector<Circle>& g1Circles = g1.getCircles();
cv::Mat g1Image = cv::Mat::zeros(height, width, CV_8UC3);
drawCircles(g1Image, g1Circles, g1Color);
// Group 2 (only if requested)
std::vector<Circle> allCircles;
allCircles.reserve(g1Circles.size() + static_cast<size_t>(std::max(0, g2Count)));
cv::Mat finalImage;
if (g2Count > 0) {
CircleGroup g2(width, height, g2Mean, g2Mean_delta, g2Std_dev, g2Std_dev_delta, g2Count, g1Circles);
const std::vector<Circle>& g2Circles = g2.getCircles();
cv::Mat g2Image = cv::Mat::zeros(height, width, CV_8UC3);
drawCircles(g2Image, g2Circles, g2Color);
cv::addWeighted(g1Image, 1.0, g2Image, 1.0, 0.0, finalImage, -1);
allCircles.insert(allCircles.end(), g1Circles.begin(), g1Circles.end());
allCircles.insert(allCircles.end(), g2Circles.begin(), g2Circles.end());
} else {
// No Group 2: just use Group 1 image and circles
finalImage = g1Image;
allCircles.insert(allCircles.end(), g1Circles.begin(), g1Circles.end());
}
saveImage(finalImage, outputDir, taskIndex);
// Save x,y,r (px)
const std::string base = outputDir + "/circles_" + std::to_string(taskIndex);
writeXYR(base + ".xyr", allCircles);
std::cout << "Finished " << taskIndex + 1 << "/" << totalTasks << std::endl;
if (taskIndex + 1 == totalTasks) {
// signal everyone to exit cleanly
{
std::lock_guard<std::mutex> lock(queueMutex);
stopThreads = true;
}
condVar.notify_all();
}
}
}
int main(int argc, char* argv[]) {
if (argc != 3 || std::string(argv[1]) != "-config") {
std::cerr << "Usage: " << argv[0] << " -config <config_path>" << std::endl;
return 1;
}
std::string config_path = argv[2];
auto start = std::chrono::high_resolution_clock::now();
int width, height, numOutputs;
float g1Mean, g1Mean_delta, g1Std_dev, g1Std_dev_delta;
int g1Count;
cv::Scalar g1Color;
float g2Mean, g2Mean_delta, g2Std_dev, g2Std_dev_delta;
int g2Count;
cv::Scalar g2Color;
loadConfig(config_path, width, height, numOutputs, g1Mean, g1Mean_delta, g1Std_dev, g1Std_dev_delta, g1Count, g1Color, g2Mean, g2Mean_delta, g2Std_dev, g2Std_dev_delta, g2Count, g2Color);
std::cout << "Generating " << numOutputs << " images with parameters:\n"
<< "GROUP 1 - " << "Mean: " << g1Mean << ", Mean delta: " << g1Mean_delta << ", Std dev: " << g1Std_dev << ", Std dev delta: " << g1Std_dev_delta << ", Count: " << g1Count << "\n"
<< "GROUP 2 - " << "Mean: " << g2Mean << ", Mean delta: " << g2Mean_delta << ", Std dev: " << g2Std_dev << ", Std dev delta: " << g2Std_dev_delta << ", Count: " << g2Count << std::endl;
std::cout << std::endl;
std::string executablePath = getExecutablePath();
std::string parentDir = cv::utils::fs::getParent(executablePath);
std::string baseOutputDir = parentDir + "/output";
// Create the base output directory if it doesn't exist
cv::utils::fs::createDirectory(baseOutputDir);
// Create a directory name based on the statistics with two decimal places
std::string outputDir = baseOutputDir + "/output_"
+ "g1_mean_" + formatFloat(g1Mean) + "_std_" + formatFloat(g1Std_dev)
+ "_g1_count_" + std::to_string(g1Count) // Add group 1 count
+ "_g2_mean_" + formatFloat(g2Mean) + "_std_" + formatFloat(g2Std_dev)
+ "_g2_count_" + std::to_string(g2Count); // Add group 2 count
// Create the detailed output directory
cv::utils::fs::createDirectory(outputDir);
// Populate the task queue
for (int i = 0; i < numOutputs; ++i) {
taskQueue.push(i);
}
int numThreads = std::thread::hardware_concurrency();
std::vector<std::thread> workers;
for (int i = 0; i < numThreads; ++i) {
workers.emplace_back(workerFunction, width, height, g1Mean, g1Mean_delta, g1Std_dev, g1Std_dev_delta, g1Count, g1Color, g2Mean, g2Mean_delta, g2Std_dev, g2Std_dev_delta, g2Count, g2Color, outputDir, numOutputs);
}
for (auto& worker : workers) {
worker.join();
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << std::endl;
std::cout << "Saved " << numOutputs << " images to " << outputDir << std::endl;
std::cout << "Total execution time: " << elapsed.count() << " seconds" << std::endl;
return 0;
}