-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.py
414 lines (263 loc) · 12.2 KB
/
viewer.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
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
import customtkinter as ctk
import matplotlib.pyplot as plt
import copy
import time
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from PIL import Image
#Importy z moich plikow
from random_points_generator import Random
from smallestcircle import Welzl
from convexhull import Graham
from mbr import Mbr
from geometry import Rectangle, PointSet
from bitalg.visualizer.main import Visualizer
#Zmienna przechowująca punkty na wykresie
points = []
vis_img = None
vis_gif = None
display_type = "png"
current_job = None
gif_interval = 100
#Fuknkcja pokazuje przekazane jako argument
#punkty z wykorzystaniem matplotlib
def show_points():
vis = Visualizer()
vis.add_point(points)
clearPlot()
fig, ax = vis.get_fig()
canvas = FigureCanvasTkAgg(fig, master = right_frame)
canvas.draw()
canvas.get_tk_widget().place(relx=0.5, rely=0.5, anchor=ctk.CENTER)
def wezlz_vis_img(circle):
vis = Visualizer()
vis.add_point(points)
vis.add_circle((circle.S[0], circle.S[1], circle.r), color='purple')
return vis
def graham_vis_img(hull_points):
vis = Visualizer()
vis.add_point(points)
vis.add_line_segment(PointSet.getEdges(hull_points), color = 'green')
return vis
def mbr_vis_img(mbr_rectangle):
vis = Visualizer()
vis.add_point(points)
vis.add_line_segment(mbr_rectangle.getEdges(), color = 'red')
return vis
def combo_vis_img(circle, hull_points, mbr_area_points, mbr_perimeter_points):
vis = Visualizer()
vis.add_point(points)
vis.add_circle((circle.S[0], circle.S[1], circle.r), color='purple')
vis.add_line_segment(PointSet.getEdges(hull_points), color = 'green')
vis.add_line_segment(mbr_area_points.getEdges(), color = 'orange')
vis.add_line_segment(mbr_perimeter_points.getEdges(), color = 'black')
return vis
def show_image():
global current_job
if current_job != None:
window.after_cancel(current_job)
current_job = None
clearPlot()
if display_type == "png":
if vis_img == None:
return
fig, ax = vis_img.get_fig()
ax.set_xlim(-150, 150)
ax.set_ylim(-150, 150)
canvas = FigureCanvasTkAgg(fig, master = right_frame)
canvas.draw()
canvas.get_tk_widget().place(relx=0.5, rely=0.5, anchor=ctk.CENTER)
elif display_type == "gif":
if vis_gif == None:
return
def update(idx):
if idx >= openImage.n_frames:
idx = 0
openImage.seek(idx)
gifDriver.configure(light_image = openImage)
gif_Label.configure(image = gifDriver)
global current_job
current_job = window.after(int(gif_interval), update, idx + 1)
gifImage = vis_gif.save_gif(filename="anim")
openImage = Image.open(gifImage)
openImage.seek(0)
gifDriver = ctk.CTkImage(light_image=openImage, size=(640, 480))
gif_Label = ctk.CTkLabel(right_frame, image=gifDriver, text = "")
gif_Label.place(relx=0.5, rely=0.5, anchor=ctk.CENTER)
update(1)
def clearPlot():
plt.clf()
plt.cla()
plt.close()
def getFigure(n = 10, xLimit = (0, 100), yLimit = (0, 100)):
vertices = []
def on_click(event):
if event.inaxes:
plt.plot(event.xdata, event.ydata, 'o')
vertices.append((event.xdata, event.ydata))
plt.title(str(n - len(vertices)) + ' points left')
plt.show()
if(len(vertices) >= n):
plt.close()
clearPlot()
fig, ax = plt.subplots()
plt.setp(ax, xlim=xLimit, ylim=yLimit)
plt.connect('button_press_event', on_click)
plt.title(str(n) + ' points left')
plt.show()
return vertices
def generate():
global points
points_type = combo_box.get()
number_of_points = int(NumOfPoints_sl.get())
if points_type == "random_uniform":
points = Random.generate_uniform_points(n = number_of_points)
show_points()
elif points_type == "random_circle":
points = Random.generate_circle_points(n = number_of_points)
show_points()
elif points_type == "random_rectangle":
points = Random.generate_rectangle_points(n = number_of_points)
show_points()
elif points_type == "custom":
points = getFigure(n = number_of_points)
show_points()
else:
raise Exception("Incorrect argument!")
def apply_algorithm():
global vis_img
global vis_gif
vis_img = None
vis_gif = None
selected_algorithm = algorithm_selection_combo_box.get()
if selected_algorithm == "Wezlz":
#----------img----------
start = time.time()
welzl_points = Welzl.welzl_algorithm(copy.deepcopy(points))
stop = time.time()
#print("Welzl: ", stop - start)
algorithm_runtime_label.configure( text = str( round(stop - start, 5) ) )
vis_img = wezlz_vis_img(welzl_points)
#----------gif----------
vis_gif = Visualizer()
vis_gif.add_point(points)
circle = Welzl.welzl_algorithm_draw(copy.deepcopy(points), vis_gif)
vis_gif.add_circle((circle.S[0], circle.S[1], circle.r), color='green')
show_image()
elif selected_algorithm == "Graham":
#----------img----------
start = time.time()
hull_points = Graham.graham_algorithm(copy.deepcopy(points))
stop = time.time()
#print("Graham: ", stop - start)
algorithm_runtime_label.configure( text = str(round(stop - start, 5)) )
vis_img = graham_vis_img(hull_points)
#----------gif----------
hull_points, vis_gif = Graham.graham_algorithm_draw(copy.deepcopy(points))
show_image()
elif selected_algorithm == "MBR - Area":
#----------img----------
start = time.time()
mbr_area_rectangle, area = Mbr.smallest_rectangle(copy.deepcopy(points), Mbr.compare_area)
stop = time.time()
algorithm_runtime_label.configure( text = str(round(stop - start, 5)) )
vis_img = mbr_vis_img(mbr_area_rectangle)
#----------gif----------
vis_gif = Mbr.smallest_rectangle_draw(copy.deepcopy(points), Mbr.compare_area)
show_image()
elif selected_algorithm == "MBR - Perimiter":
#----------img----------
start = time.time()
mbr_perimeter_rectangle, perimeter = Mbr.smallest_rectangle(copy.deepcopy(points), Mbr.compare_perimeter)
stop = time.time()
algorithm_runtime_label.configure( text = str(round(stop - start, 5)) )
vis_img = mbr_vis_img(mbr_perimeter_rectangle)
#----------gif----------
vis_gif = Mbr.smallest_rectangle_draw(copy.deepcopy(points), Mbr.compare_perimeter)
show_image()
elif selected_algorithm == "Combo":
#----------img----------
circle = Welzl.welzl_algorithm(copy.deepcopy(points))
hull_points = Graham.graham_algorithm(copy.deepcopy(points))
mbr_area_points, area = Mbr.smallest_rectangle(copy.deepcopy(points), Mbr.compare_area)
mbr_perimeter_points, perimeter = Mbr.smallest_rectangle(copy.deepcopy(points), Mbr.compare_perimeter)
vis_img = combo_vis_img(circle, hull_points, mbr_area_points, mbr_perimeter_points)
algorithm_runtime_label.configure( text = "-" )
#----------gif----------
vis_gif = None
show_image()
else:
raise Exception("Incorrect argument!")
def updateTextBox_NumOfPoints(value):
NumOfPoints_tb.configure(text = (str(int(value))))
def segmented_button_callback(value):
global display_type
display_type = value
show_image()
def update_interval(value):
global gif_interval
gif_interval = int(value)
interval_selection_tb.configure(text = str(int(value)))
window = ctk.CTk()
window.resizable(False, False)
window.title('Minimum bounding circle')
window.geometry("1200x600")
ctk.set_appearance_mode("dark")
#----------LEFT FRAME----------
left_frame = ctk.CTkFrame(master = window, width=300, height=600, corner_radius = 0, border_color = "gray", border_width = 1)
left_frame.pack(side = ctk.LEFT)
text_label = ctk.CTkLabel(master = left_frame, height = 20, width = 298, text = "Select a set of points", text_color = "white", font = ("Arial", 10))
text_label.place(x = 1, y = 20)
combo_values = ["random_uniform", "random_circle", "random_rectangle", "custom"]
combo_box = ctk.CTkComboBox(master = left_frame, height = 20, width = 280, values = combo_values, corner_radius = 0, state = "readonly")
combo_box.set("random_uniform")
combo_box.place(x = 10, y = 50)
#----------Number of points slider + valueLabel + label----------
NumOfPoints_label = ctk.CTkLabel(master = left_frame, width = 280, height = 20, corner_radius = 0, text = "Set number of points to be generated", text_color = "white", font = ("Arial", 10))
NumOfPoints_label.place(x = 10, y = 80)
NumOfPoints_tb = ctk.CTkLabel(master = left_frame, width = 40, height = 20, corner_radius=0, text = "50", text_color = "white", font = ("Arial", 10))
NumOfPoints_tb.place(x = 240, y = 100)
NumOfPoints_sl = ctk.CTkSlider(master = left_frame, from_ = 0, to = 1000, number_of_steps = 1000, height = 20, width = 230, command = updateTextBox_NumOfPoints)
NumOfPoints_sl.set(50)
NumOfPoints_sl.place(x = 10, y = 100)
plot_button = ctk.CTkButton(master = left_frame, command = generate, height = 20, width = 280, text = "Generate", corner_radius = 0)
plot_button.place(x = 10, y = 130)
#----------ALGORITHM SELECTION----------
algorithm_selection_label = ctk.CTkLabel(master = left_frame, width = 280, height = 20, corner_radius = 0, text = "Select algorithm")
algorithm_selection_label.place(x = 10, y = 515)
algorithm_selection_combo_values = ["Wezlz", "Graham", "MBR - Perimiter", "MBR - Area", "Combo"]
algorithm_selection_combo_box = ctk.CTkComboBox(master = left_frame, height = 20, width = 280, values = algorithm_selection_combo_values, corner_radius = 0, state = "readonly")
algorithm_selection_combo_box.set("Wezlz")
algorithm_selection_combo_box.place(x = 10, y = 540)
algorithm_selection_button = ctk.CTkButton(master = left_frame, command = apply_algorithm, height = 20, width = 280, text = "Apply", corner_radius = 0)
algorithm_selection_button.place(x = 10, y = 570)
#----------RIGHT FRAME----------
right_frame = ctk.CTkFrame(master = window, width=900, height=600, corner_radius = 0)
right_frame.pack(side=ctk.RIGHT)
#----------Display selection----------
segemented_button = ctk.CTkSegmentedButton(master = right_frame, values=["png", "gif"], command=segmented_button_callback, corner_radius = 0, height = 30 ,width = 400, dynamic_resizing = False)
segemented_button.set("png")
segemented_button.place(x = 250, y = 560)
'''#----------Picture/Gif save------
def save_file():
folder_selected = ctk.filedialog.askdirectory()
print(folder_selected)
folder_selected = []
image_save_button = ctk.CTkButton(master = right_frame, command = save_file, height = 30, width = 50, text = "Save", corner_radius = 0)
image_save_button.place(x = 840, y = 10)
'''
#----------CHOOSE INTERVAL----------
interval_selection_label = ctk.CTkLabel(master = right_frame, width = 280, height = 20, corner_radius = 0, text = "Select gif interval")
interval_selection_label.place(x = 310, y = 10)
interval_selection_slider = ctk.CTkSlider(master = right_frame, from_ = 1, to = 1000, number_of_steps = 1000, height = 20, width = 230, command = update_interval)
interval_selection_slider.set(100)
interval_selection_slider.place(x = 335, y = 30)
interval_selection_tb = ctk.CTkLabel(master = right_frame, width = 40, height = 20, corner_radius=0, text = "100", text_color = "white", font = ("Arial", 10))
interval_selection_tb.place(x = 550, y = 30)
#----------ALGORITHM RUNTIME----------
algorithm_runtime_name_label = ctk.CTkLabel(master = right_frame, width = 100, height = 20, corner_radius = 0, text = "Algorithm runtime")
algorithm_runtime_name_label.place(x = 20, y = 566)
algorithm_runtime_label = ctk.CTkLabel(master = right_frame, width = 40, height = 20, corner_radius = 0, text = "-")
algorithm_runtime_label.place(x = 150, y = 566)
# run the gui
window.mainloop()