-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot_navigator.py
109 lines (88 loc) · 3.76 KB
/
mandelbrot_navigator.py
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
"""
Author: Lukas Bisgaard Kristensen
Date: 26. April 2023
Course: Numerical Scientific Computing, AAU
Description: This program computes the Mandelbrot set using OpenCL.
"""
import pyopencl
import time
import cv2 as cv
import mandelbrot_opencl
xmin, xmax, ymin, ymax = -2.3, 0.8, -1.2, 1.2
heatmaps = [cv.COLORMAP_HOT, cv.COLORMAP_BONE, cv.COLORMAP_PINK, cv.COLORMAP_INFERNO, cv.COLORMAP_PLASMA, cv.COLORMAP_TWILIGHT, cv.COLORMAP_OCEAN, cv.COLORMAP_MAGMA, cv.COLORMAP_PARULA, cv.COLORMAP_TURBO]
selected_heatmap = cv.COLORMAP_HOT
def update_heatmap(index):
global selected_heatmap
try:
selected_heatmap = heatmaps[index]
except IndexError:
print('Index out of bounds')
def draw_window():
start_time = time.time()
mandelbrot_figure = mandelbrot_opencl.mandelbrot_opencl(device=device, context=context, queue=queue, width=1000, height=1000, local_size=25, show_figure=False, x_min=xmin, x_max=xmax, y_min=ymin, y_max=ymax)
# Add opencv text
font = cv.FONT_HERSHEY_SIMPLEX
# Controls
cv.putText(mandelbrot_figure, 'Controls:', (10, 50), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 'w: up', (10, 100), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 's: down', (10, 150), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 'a: left', (10, 200), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 'd: right', (10, 250), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 'q: zoom in', (10, 300), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 'e: zoom out', (10, 350), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, 'r: reset', (10, 400), font, 0.5, 255, 1, cv.LINE_AA)
# Position
cv.putText(mandelbrot_figure, f'x {round(xmin, 5)} : {round(xmax, 5)}', (10, 500), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, f'y {round(ymin, 5)} : {round(ymax, 5)}', (10, 550), font, 0.5, 255, 1, cv.LINE_AA)
# Computation time
cv.putText(mandelbrot_figure, f'Computation time: {round(time.time() - start_time, 3)} s', (10, 600), font, 0.5, 255, 1, cv.LINE_AA)
# Switch colormap
cv.putText(mandelbrot_figure, 'Switch colormap:', (10, 700), font, 0.5, 255, 1, cv.LINE_AA)
cv.putText(mandelbrot_figure, '0 - 9', (10, 730), font, 0.5, 255, 1, cv.LINE_AA)
mandelbrot_figure = cv.convertScaleAbs(mandelbrot_figure)
mandelbrot_figure = cv.applyColorMap(mandelbrot_figure, selected_heatmap)
cv.imshow('Mandelbrot Set Navigator', mandelbrot_figure)
def main():
"""
Script for navigating the Mandelbrot set.
:return:
"""
global xmin, xmax, ymin, ymax
global context, queue, device, name
context, queue, device, name = mandelbrot_opencl.create_opencl_context(pyopencl.get_platforms()[0])
draw_window()
while True:
y_scale = (ymax - ymin) / 10
x_scale = (xmax - xmin) / 10
k = cv.waitKey(1) & 0xFF
if k == ord('w'):
ymin -= y_scale
ymax -= y_scale
elif k == ord('s'):
ymin += y_scale
ymax += y_scale
elif k == ord('a'):
xmin -= x_scale
xmax -= x_scale
elif k == ord('d'):
xmin += x_scale
xmax += x_scale
elif k == ord('e'):
ymin -= y_scale
ymax += y_scale
xmin -= x_scale
xmax += x_scale
elif k == ord('q'):
ymin += y_scale
ymax -= y_scale
xmin += x_scale
xmax -= x_scale
elif k == ord('r'):
xmin, xmax, ymin, ymax = -2.3, 0.8, -1.2, 1.2
elif k in range(48, 58):
update_heatmap(k-48)
else:
continue
draw_window()
if __name__ == '__main__':
main()