-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.cpp
130 lines (108 loc) · 3.68 KB
/
mandelbrot.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
//
// Created by Sergey Fadeev on 30.03.2021.
//
#include "ui_main_view.h"
#include "mandelbrot.h"
#include "render_task.h"
#include <QPaintEngine>
#include <QWheelEvent>
#include <QScreen>
#include <QThread>
#include <complex>
mandelbrot::mandelbrot(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::mandelbrot) {
ui->setupUi(this);
resize(QApplication::screens().at(0)->availableSize());
threads_count = std::max(QThread::idealThreadCount() - 1, 1);
pool = std::make_unique<QThreadPool>(this);
pool->setMaxThreadCount(QThread::idealThreadCount());
memory_semaphore.release(1);
for(int i = 0; i < threads_count; i++) {
auto* task = new render_task(main_image.bits(),
main_image.bytesPerLine(),
main_image.height(),
main_image.width(),
scale,
center_offset,
threads_count,
i,
memory_semaphore);
tasks.push_back(task);
connect(task, &render_task::resultReady, this, &mandelbrot::threadIsReady);
pool->start(task);
}
}
mandelbrot::~mandelbrot() {
for(auto* task: tasks) {
task->stop_task();
}
pool->clear();
}
void mandelbrot::paintEvent(QPaintEvent* ev) {
QMainWindow::paintEvent(ev);
QPainter p(this);
p.drawImage(0, 0, main_image);
}
void mandelbrot::wheelEvent(QWheelEvent* event) {
if (event->angleDelta().y() == 0) {
return;
}
scale *= (1 - std::min(event->angleDelta().y(), 500) / 1000.);
sizee *= (1 - std::min(event->angleDelta().y(), 500) / 1000.);
restart_threads(false);
}
void mandelbrot::mousePressEvent(QMouseEvent* event) {
cur_mouse_move_start_pos = event->pos();
}
void mandelbrot::mouseMoveEvent(QMouseEvent* event) {
auto x_delta = cur_mouse_move_start_pos.x() - event->pos().x();
auto y_delta = cur_mouse_move_start_pos.y() - event->pos().y();
cur_mouse_move_start_pos = event->pos();
center_offset += std::complex<double>(x_delta * 0.005 * sizee, y_delta * 0.005 * sizee);
restart_threads(false);
}
void mandelbrot::keyPressEvent(QKeyEvent* event) {
bool need_update = true;
if (event->key() == Qt::Key_Left) {
center_offset += std::complex<double>(-0.01 * sizee, 0);
} else if (event->key() == Qt::Key_Right) {
center_offset += std::complex<double>(+0.01 * sizee, 0);
} else if (event->key() == Qt::Key_Up) {
center_offset += std::complex<double>(0, -0.01 * sizee);
} else if (event->key() == Qt::Key_Down) {
center_offset += std::complex<double>(0, +0.01 * sizee);
} else if (event->key() == Qt::Key_Return) {
scale *= (0.975);
sizee *= (0.975);
} else if (event->key() == Qt::Key_Space) {
scale *= (1.025);
sizee *= (1.025);
} else {
need_update = false;
}
if (need_update) {
restart_threads(false);
}
}
void mandelbrot::resizeEvent(QResizeEvent*) {
memory_semaphore.acquire();
main_image = QImage(size(), QImage::Format_RGB888);
restart_threads(true);
}
void mandelbrot::threadIsReady() {
update();
}
void mandelbrot::restart_threads(bool need_release_memory_semaphore) {
for(auto* i: tasks) {
i->set_values(main_image.bits(),
main_image.bytesPerLine(),
main_image.height(),
main_image.width(),
scale,
center_offset);
}
if(need_release_memory_semaphore) {
memory_semaphore.release(1);
}
}