-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadtreemap.py
289 lines (243 loc) · 8.41 KB
/
quadtreemap.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
import pygame
import sys, os
import shutil
import copy
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return self.print()
def print(self):
return f"X: {self.x}\tY: {self.y}"
class Rectangle:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def __str__(self):
p1 = (self.x, self.y)
p2 = (self.x + self.w, self.y + self.h)
return f"|P1: {p1}|\t|P2: {p2}|"
def contains(self, point):
if point.x > self.x and point.y > self.y:
if point.x < self.x + self.w and point.y < self.y + self.h:
return True
return False
class QuadTreeNode:
def __init__(self, boundary=None):
self.boundary = boundary
self.parent = None
self.occupancy = False
self.children = {}
def addChild(self, child=None, label="NW"):
child.parent = self
self.children[label] = child
def get_level(self):
level = 0
p = self.parent
while p:
level += 1
p = p.parent
return level
def containsChildren(self):
return any(self.children.values())
def print_tree(self):
prefix = f"{self.get_level()} "
spacer = ' ' * self.get_level()*3 + "|____" if self.parent else ""
statement = prefix + spacer + f"{self.boundary}" + f"\tOccupied: {self.occupancy}"
print(statement)
if self.containsChildren():
for child in self.children.values():
child.print_tree()
def add_level(self):
x = self.boundary.x
y = self.boundary.y
w = self.boundary.w
h = self.boundary.h
# nw point
self.addChild(QuadTreeNode(Rectangle(x, y, w/2, h/2)), "NW")
# sw point
self.addChild(QuadTreeNode(Rectangle(x, y + h/2, w/2, h/2)), "SW")
# se point
self.addChild(QuadTreeNode(Rectangle(x + w/2, y + h/2, w/2, h/2)), "SE")
# ne point
self.addChild(QuadTreeNode(Rectangle(x + w/2, y, w/2, h/2)), "NE")
def add_until_level(self, maxLevel):
if maxLevel <= self.get_level() : return
self.add_level()
for child in self.children.values(): child.add_until_level(maxLevel)
def insertPoint(self, point, maxLevel):
if not self.boundary.contains(point):
return
if maxLevel <= self.get_level():
if self.boundary.contains(point):
self.occupancy = True
return
if (not self.containsChildren()):
if (not self.occupancy):
self.add_level()
else:
return
for child in self.children.values():
child.insertPoint(point, maxLevel)
def insertPCData(self, pcData, maxLevel):
if pcData:
for point in pcData.getPoints():
self.insertPoint(point, maxLevel)
def insert(self, data, maxLevel):
if isinstance(data, Point):
self.insertPoint(data, maxLevel)
elif isinstance(data, PointCloud):
self.insertPCData(data, maxLevel)
def mergeOccupiedNodes(self):
occupancy = []
if not self.containsChildren():
return
for child in self.children.values():
child.mergeOccupiedNodes()
occupancy.append(child.occupancy)
if len(occupancy) == 4 and all(occupancy):
self.children = {}
self.occupancy = True
def mergeFreeNodes(self):
if not self.containsChildren():
return
occupancy = []
for child in self.children.values():
child.mergeFreeNodes()
occupancy.append(child.occupancy)
if len(occupancy) == 4 and (not any(occupancy)) :
self.children = {}
self.occupancy = False
def isOccupied(self, point):
if self.boundary.contains(point):
if self.occupancy:
return True
else:
if self.containsChildren():
occupied = False
for child in self.children.values():
occupied = occupied or child.isOccupied(point)
return occupied
else:
return False
def getSize(self):
if self.containsChildren():
if self.boundary is None:
return sys.getsizeof(self)
else:
sz = 0
for child in self.children.values():
sz += child.getSize()
return sz
else:
return sys.getsizeof(self.boundary) + sys.getsizeof(self.occupancy)
class QuadTree:
def __init__(self, boundary=None, maxlevel=0):
self.root = QuadTreeNode(boundary)
self.maxLevel = maxlevel
def insert(self, data):
self.root.insert(data, self.maxLevel)
self.root.mergeOccupiedNodes()
def print_tree(self):
self.root.print_tree()
print(f"Size: {self.getSize()}")
def isOccupied(self, point):
return self.root.isOccupied(point)
def getSize(self):
return self.root.getSize()
def invertTree(self, tree):
if tree.containsChildren():
keys = []
for key, child in tree.children.items():
if child.occupancy: keys.append(key)
else: self.invertTree(child)
for key in keys: tree.children.pop(key)
else: return
def invert(self):
qtree = copy.copy(self)
self.invertTree(qtree.root)
# qtree.root.mergeFreeNodes()
return qtree
class Tree:
pad = 100, 100
center = pad[0]/2, pad[1]/2
points = []
def __init__(self, width=100, height=100):
self._init__pygame()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((width + self.pad[0], height + self.pad[1]))
self.clock = pygame.time.Clock()
def _init__pygame(self):
pygame.init()
pygame.display.set_caption("QuadTree Map Visualisation")
def draw(self, quadtree):
self.clearScreen()
self.drawTree(quadtree)
def drawTree(self, quadtree):
w = quadtree.boundary.w
h = quadtree.boundary.h
x1 = quadtree.boundary.x + self.center[0]
y1 = quadtree.boundary.y + self.center[1]
if quadtree.occupancy:
pygame.draw.rect(self.screen, GREEN, (x1, y1, w, h))
pygame.draw.rect(self.screen, BLACK, (x1, y1, w, h), 1)
if quadtree.containsChildren():
for child in quadtree.children.values():
self.drawTree(child)
def draw_point(self, point, memory):
if memory:
self.points.append(point)
else:
self.points = []
for point in self.points:
x = self.center[0] + point.x
y = self.center[1] + point.y
pygame.draw.circle(self.screen, RED, (x, y), 2)
def drawPCData(self, pcdata):
for point in pcdata.getPoints():
x = self.center[0] + point.x
y = self.center[1] + point.y
pygame.draw.circle(self.screen, RED, (x, y), 2)
def clearScreen(self):
self.screen.fill(WHITE)
def update(self, recorder=None):
pygame.display.flip()
if recorder:
array2d = pygame.image.tostring(self.screen, "RGBA")
recorder.capture(array2d, (self.width, self.height))
self.clock.tick(60)
def quit(self):
pygame.quit()
def eventCheck(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
return True
class PointCloud:
pcData = None
def __init__(self, pcdata):
self.points = []
self.pcData = pcdata
self.createPoints()
def createPoints(self):
if not self.pcData.any():
return
for pts in self.pcData:
self.points.append(Point(pts[0], pts[1]))
def getPoints(self):
return self.points
def __str__(self):
string = ""
for point in self.points:
string += point.print() + "\n"
print(point)
return string