-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizers.py
408 lines (322 loc) · 15.1 KB
/
visualizers.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as mcoll
from matplotlib.widgets import Button
import json as js
from geometric_types import *
TOLERANCE = 0.15
def dist(point1, point2):
return np.sqrt(np.power(point1[0] - point2[0], 2) + np.power(point1[1] - point2[1], 2))
def upper_right(point1: Point, point2: Point):
return [max(cor1, cor2) for cor1, cor2 in zip(point1, point2)]
def lower_left(point1: Point, point2: Point):
return [min(cor1, cor2) for cor1, cor2 in zip(point1, point2)]
def upper_left(point1: Point, point2: Point):
return [min(point1[0], point2[0]), max(point1[1], point2[1])]
def lower_right(point1: Point, point2: Point):
return [max(point1[0], point2[0]), min(point1[1], point2[1])]
class _ButtonCallback(object):
def __init__(self, scenes):
self.i = 0
self.scenes = scenes
self.adding_points = False
self.added_points = []
self.adding_lines = False
self.added_lines = []
self.adding_rects = False
self.added_rects = []
self.ax = None
self.new_line_point = None
self.rect_points = None
def set_axes(self, ax):
self.ax = ax
def next(self, event):
self.i = (self.i + 1) % len(self.scenes)
self.draw(autoscaling=True)
def prev(self, event):
self.i = (self.i - 1) % len(self.scenes)
self.draw(autoscaling=True)
def add_point(self, event):
self.adding_points = not self.adding_points
self.new_line_point = None
if self.adding_points:
self.adding_lines = False
self.adding_rects = False
self.added_points.append(PointsCollection([]))
def add_line(self, event):
self.adding_lines = not self.adding_lines
self.new_line_point = None
if self.adding_lines:
self.adding_points = False
self.adding_rects = False
self.added_lines.append(LinesCollection([]))
def add_rect(self, event):
self.adding_rects = not self.adding_rects
self.new_line_point = None
if self.adding_rects:
self.adding_points = False
self.adding_lines = False
self.new_rect()
def new_rect(self):
self.added_rects.append(LinesCollection([]))
self.rect_points = []
def on_click(self, event):
if event.inaxes != self.ax:
return
new_point = (event.xdata, event.ydata)
if self.adding_points:
self.added_points[-1].add_points([new_point])
self.draw(autoscaling=False)
elif self.adding_lines:
if self.new_line_point is not None:
self.added_lines[-1].add([self.new_line_point, new_point])
self.new_line_point = None
self.draw(autoscaling=False)
else:
self.new_line_point = new_point
elif self.adding_rects:
if len(self.rect_points) == 0:
self.rect_points.append(new_point)
elif len(self.rect_points) == 1:
self.added_rects[-1].add([self.rect_points[-1], new_point])
self.rect_points.append(new_point)
self.draw(autoscaling=False)
elif len(self.rect_points) > 1:
if dist(self.rect_points[0], new_point) < (
np.mean([self.ax.get_xlim(), self.ax.get_ylim()]) * TOLERANCE):
self.added_rects[-1].add([self.rect_points[-1], self.rect_points[0]])
self.new_rect()
else:
self.added_rects[-1].add([self.rect_points[-1], new_point])
self.rect_points.append(new_point)
self.draw(autoscaling=False)
def draw(self, autoscaling=True):
if not autoscaling:
x_lim = self.ax.get_xlim()
y_lim = self.ax.get_ylim()
self.ax.clear()
for collection in (self.scenes[self.i].points + self.added_points):
if len(collection.points) > 0:
self.ax.scatter(*zip(*(np.array(collection.points))), **collection.kwargs)
for collection in (self.scenes[self.i].lines + self.added_lines + self.added_rects):
self.ax.add_collection(collection.get_collection())
self.ax.autoscale(autoscaling)
if not autoscaling:
self.ax.set_xlim(x_lim)
self.ax.set_ylim(y_lim)
plt.draw()
# %% md
class Scene:
def __init__(self, points=None, lines=None):
self.points = points or []
self.lines = lines or []
class PointsCollection:
def __init__(self, points, **kwargs):
self.points = points
self.kwargs = kwargs
def add_points(self, points):
self.points = self.points + points
class LinesCollection:
def __init__(self, lines, **kwargs):
self.lines = lines
self.kwargs = kwargs
def add(self, line):
self.lines.append(line)
def get_collection(self):
return mcoll.LineCollection(self.lines, **self.kwargs)
class Plot:
def __init__(self, scenes=None, points=None, lines=None, json=None):
if points is None:
points = []
if lines is None:
lines = []
self.callback = None
self.widgets = None
if json is None:
self.scenes = scenes or [Scene()]
if points or lines:
self.scenes[0].points = points
self.scenes[0].lines = lines
else:
self.scenes = [Scene([PointsCollection(pointsCol) for pointsCol in scene["points"]],
[LinesCollection(linesCol) for linesCol in scene["lines"]])
for scene in js.loads(json)]
def __configure_buttons(self):
plt.subplots_adjust(bottom=0.2)
ax_prev = plt.axes([0.6, 0.05, 0.15, 0.075])
ax_next = plt.axes([0.76, 0.05, 0.15, 0.075])
ax_add_point = plt.axes([0.44, 0.05, 0.15, 0.075])
ax_add_line = plt.axes([0.28, 0.05, 0.15, 0.075])
ax_add_rect = plt.axes([0.12, 0.05, 0.15, 0.075])
b_next = Button(ax_next, 'Next')
b_next.on_clicked(self.callback.next)
b_prev = Button(ax_prev, 'Previous')
b_prev.on_clicked(self.callback.prev)
b_add_point = Button(ax_add_point, 'Add point')
b_add_point.on_clicked(self.callback.add_point)
b_add_line = Button(ax_add_line, 'Add line')
b_add_line.on_clicked(self.callback.add_line)
b_add_rect = Button(ax_add_rect, 'Add figure')
b_add_rect.on_clicked(self.callback.add_rect)
return [b_prev, b_next, b_add_point, b_add_line, b_add_rect]
def add_scene(self, scene):
self.scenes.append(scene)
def add_scenes(self, scenes):
self.scenes = self.scenes + scenes
def toJson(self):
return js.dumps([{"points": [np.array(pointCol.points).tolist() for pointCol in scene.points],
"lines": [linesCol.lines for linesCol in scene.lines]}
for scene in self.scenes])
def get_added_points(self):
if self.callback:
return self.callback.added_points
else:
return None
def get_added_lines(self):
if self.callback:
return self.callback.added_lines
else:
return None
def get_added_figure(self):
if self.callback:
return self.callback.added_rects
else:
return None
def get_added_elements(self):
if self.callback:
return Scene(self.callback.added_points, self.callback.added_lines + self.callback.added_rects)
else:
return None
def draw(self):
plt.close()
fig = plt.figure()
self.callback = _ButtonCallback(self.scenes)
self.widgets = self.__configure_buttons()
ax = plt.axes(autoscale_on=False)
self.callback.set_axes(ax)
fig.canvas.mpl_connect('button_press_event', self.callback.on_click)
plt.show()
self.callback.draw()
@staticmethod
def save_to_file(self, file_name: str):
plt.savefig(file_name + '.png' if file_name.find('.') == -1 else file_name)
class KDTree2DVisualizer:
def __init__(self, all_points: list[Point]):
self.scenes = []
self.points = all_points
self.highlighted_points = []
self.lines = []
self.current_rectangle_lines = []
self.searched_rectangle_lines = []
self.tree_building_scenes = []
self.searches = []
self.scenes.append(self._create_scene())
def get_tree_building_plot(self):
return Plot(self.tree_building_scenes)
def get_searching_plot(self, i: int = -1):
if len(self.searches) <= 0:
return Plot()
return Plot(self.searches[i])
def end_tree_building(self):
self.tree_building_scenes = self.scenes
self.scenes = []
def end_searching(self):
self.searches.append(self.scenes)
self.scenes = []
def add_split(self, line: Line):
self.lines.append(line)
self.scenes.append(self._create_scene())
def _create_scene(self):
return Scene(
points=[
PointsCollection(
points=self.points,
color='black'
),
PointsCollection(
points=self.highlighted_points,
color='orange'),
],
lines=[
LinesCollection(
lines=self.lines.copy(),
color='blue'
),
LinesCollection(
lines=self.current_rectangle_lines.copy(),
color='green'
),
LinesCollection(
lines=self.searched_rectangle_lines.copy(),
color='red'
)
]
)
def highlight_points(self, points: list[Point]):
self.highlighted_points = points
self.scenes.append(self._create_scene())
def set_current_rectangle(self, rectangle: Rectangle):
self.current_rectangle_lines.clear()
for line in self._convert_rectangle_to_lines(rectangle):
self.current_rectangle_lines.append(line)
self.scenes.append(self._create_scene())
def set_searched_rectangle(self, rectangle: Rectangle):
self.searched_rectangle_lines.clear()
for line in self._convert_rectangle_to_lines(rectangle):
self.searched_rectangle_lines.append(line)
self.scenes.append(self._create_scene())
@staticmethod
def _convert_rectangle_to_lines(rectangle: Rectangle):
rectangle_lines = [(lower_left(*rectangle), lower_right(*rectangle)),
(lower_left(*rectangle), upper_left(*rectangle)),
(lower_right(*rectangle), upper_right(*rectangle)),
(upper_left(*rectangle), upper_right(*rectangle))]
return rectangle_lines
class QuadtreeVisualizer:
def __init__(self, points):
self.lines = []
self.scenes = [Scene([PointsCollection(points)])]
self.points = []
self.scenes_query = []
def create_build_plot(self):
return Plot(self.scenes)
def create_query_plot(self):
return Plot(self.scenes_query)
def _update_scenes(self):
self.scenes.append(Scene([PointsCollection(self.points.copy())], [LinesCollection(self.lines.copy())]))
def add_point(self, point):
self.points.append((point.x, point.y))
self._update_scenes()
def add_boundary(self, boundary):
lower_left_point, upper_right_point = boundary
mid_point = ((lower_left_point.x + upper_right_point.x) / 2, (upper_right_point.y + lower_left_point.y) / 2)
self._add_boundary_util(((lower_left_point.x, mid_point[1]), (mid_point[0], upper_right_point.y)))
self._add_boundary_util((mid_point, (upper_right_point.x, upper_right_point.y)))
self._add_boundary_util(((mid_point[0], lower_left_point.y), (upper_right_point.x, mid_point[1])))
self._add_boundary_util(((lower_left_point.x, lower_left_point.y), mid_point))
self._update_scenes()
def add_starting_boundary(self, boundary):
lower_left_point, upper_right_point = boundary
self._add_boundary_util(((lower_left_point.x, lower_left_point.y), (upper_right_point.x, upper_right_point.y)))
self._update_scenes()
def _add_boundary_util(self, boundary):
lower_left_point, upper_right_point = boundary
upper_left_point = (lower_left_point[0], upper_right_point[1])
lower_right_point = (upper_right_point[0], lower_left_point[1])
self.lines.append([lower_left_point, lower_right_point])
self.lines.append([lower_left_point, upper_left_point])
self.lines.append([upper_left_point, upper_right_point])
self.lines.append([upper_right_point, lower_right_point])
def update_query_visualization(self, searched_area_boundary, node_boundary = None, points_in_range = None, added_points = None):
lower_left_area_point, upper_right_area_point = (searched_area_boundary[0].x, searched_area_boundary[0].y), (searched_area_boundary[1].x, searched_area_boundary[1].y)
upper_left_area_point, lower_right_area_point = (lower_left_area_point[0], upper_right_area_point[1]), (upper_right_area_point[0], lower_left_area_point[1])
searched_area_lines = [[lower_left_area_point, lower_right_area_point], [lower_left_area_point, upper_left_area_point], [upper_left_area_point, upper_right_area_point], [upper_right_area_point, lower_right_area_point]]
if searched_area_boundary is not None and node_boundary is None and points_in_range is None:
self.scenes_query.append(Scene([PointsCollection(self.points.copy())], [LinesCollection(self.lines.copy()), LinesCollection(searched_area_lines.copy(), color = "green")]))
elif node_boundary is None:
self.scenes_query.append(Scene([PointsCollection(self.points.copy()), PointsCollection(points_in_range.copy(), color = "green")], [LinesCollection(self.lines.copy()), LinesCollection(searched_area_lines.copy(), color = "green")]))
else:
lower_left_boundary_point, upper_right_boundary_point = (node_boundary[0].x, node_boundary[0].y), (node_boundary[1].x, node_boundary[1].y)
upper_left_boundary_point, lower_right_boundary_point = (lower_left_boundary_point[0], upper_right_boundary_point[1]), (upper_right_boundary_point[0], lower_left_boundary_point[1])
node_boundary_lines = [[lower_left_boundary_point, lower_right_boundary_point], [lower_left_boundary_point, upper_left_boundary_point], [upper_left_boundary_point, upper_right_boundary_point], [upper_right_boundary_point, lower_right_boundary_point]]
self.scenes_query.append(Scene([PointsCollection(self.points.copy()), PointsCollection(points_in_range.copy(), color = "green"), PointsCollection(added_points.copy(), color = "red")], [LinesCollection(self.lines.copy()), LinesCollection(node_boundary_lines.copy(), color = "red"), LinesCollection(searched_area_lines.copy(), color = "green")]))