def recurse(self,bbox, depth): """ Internal function called on class contruction, this should create the BoundingBoxes. :param bbox: the initial BoundingBox :param depth: the depth of the QuadTree :To be implemented by the student: """ if depth >= self.depth: return centroid = bbox.centroid() initbox = bbox.data topright = bb.BoundingBox(centroid[0],initbox[0,1],centroid[1],initbox[1,1]) topleft = bb.BoundingBox(initbox[0,0],centroid[0],centroid[1],initbox[1,1]) botright= bb.BoundingBox(centroid[0],initbox[0,1],initbox[1,0],centroid[1]) botleft= bb.BoundingBox(initbox[0,0],centroid[0],initbox[1,0],centroid[1]) # self.quads[depth] = [topright, topleft,botleft,botright] self.quads[depth] += [topright] self.quads[depth] += [topleft] self.quads[depth] += [botleft] self.quads[depth] += [botright] depth = depth +1 self.recurse(topright, depth) self.recurse(topleft, depth) self.recurse(botright, depth) self.recurse(botleft, depth)
def recurse(self, _bbox, depth): # Stop the recursion when the below condition is satisfied if depth >= self.depth: return """ centroid - Calculate the centroid of the BoundingBox minx - minimum x of the bounding box maxx - maximum x of the bounding box miny - minimum y of the bounding box maxy - maximum y of the bounding box nw, ne, sw, se - The points that form the four BoundingBoxes """ centroid = _bbox.centroid() minx = _bbox.data[0, 0] maxx = _bbox.data[0, 1] miny = _bbox.data[1, 0] maxy = _bbox.data[1, 1] nw = bb.BoundingBox(minx, centroid[0], miny, centroid[1]) ne = bb.BoundingBox(centroid[0], maxx, miny, centroid[1]) sw = bb.BoundingBox(minx, centroid[0], centroid[1], maxy) se = bb.BoundingBox(centroid[0], maxx, centroid[1], maxy) # The bounding boxes created are added to the quads map, with the parameter depth used as key self.quads[depth] += [nw] self.quads[depth] += [ne] self.quads[depth] += [sw] self.quads[depth] += [se] depth = depth + 1 # each of the resulting bounding boxes are sent as parameters to the recursion function. self.recurse(nw, depth) self.recurse(ne, depth) self.recurse(sw, depth) self.recurse(se, depth)
def recurse(self, bbox, depth): """ Internal function called on class contruction, this should create the BoundingBoxes. :param bbox: the initial BoundingBox :param depth: the depth of the QuadTree :To be implemented by the student: """ try: self.quads[depth] # check if we havent reachex max-depth # Generate all the bounding boxes TOP r/l bottom r/l ctr = bbox.centroid() w = bbox.width() h = bbox.height() new_depth = depth + 1 # go down 1 level top_right = bb.BoundingBox(ctr[0], ctr[0] + w / 2, ctr[1], ctr[1] + h / 2) # push the bbs to the list based on the current level self.quads[depth].append(top_right) # we try to go one level down self.recurse(top_right, new_depth) top_left = bb.BoundingBox(ctr[0] - w / 2, ctr[0], ctr[1], ctr[1] + h / 2) # push the bbs to the list based on the current level self.quads[depth].append(top_left) # we try to go one level down self.recurse(top_left, new_depth) lower_left = bb.BoundingBox(ctr[0] - w / 2, ctr[0], ctr[1] - h / 2, ctr[1]) # push the bbs to the list based on the current level self.quads[depth].append(lower_left) # we try to go one level down self.recurse(lower_left, new_depth) lower_right = bb.BoundingBox(ctr[0], ctr[0] + w / 2, ctr[1] - h / 2, ctr[1]) # push the bbs to the list based on the current level self.quads[depth].append( lower_right) # we try to go one level down self.recurse(lower_right, new_depth) except KeyError: pass return
def example(): import os import boundingbox # Use my system's version of fmpeg instead of the one bundled with imageio-ffmpeg, # because my system's is more up to date os.environ['IMAGEIO_FFMPEG_EXE'] = 'ffmpeg' # The name of the recording to read name = '382321879' # Customize some bounding boxes x0 = 740 y0 = 60 bb1 = boundingbox.BoundingBox(-356, -270 + y0, 604, 270 + y0) bb2 = boundingbox.BoundingBox(-1248 + x0, -540 + y0, 672 + x0, 540 + y0) # Specify explicit time intervals t0 = 60 t1 = 456800 t2 = 478801 # Start the movie! By default use 'mandelbrot' for output files. m = movie.Movie(recording_name=name, filename='mandelbrot') m.start_movie(fps=60) scene = m.new_scene() scene.set_bb(bb1) scene.set_target_resolution(1920, 1080) # 120 seconds, 2 second pause at end, 1 second fade in, 0.5 second fade out scene.set_time_seconds(120, 2, 1, 0.5) scene.set_movie_real_time(t0, t1) scene.render_scene( False) # False means not to save a screenshot of the ending scene = m.new_scene() scene.set_bb(bb2) scene.set_target_resolution(1920, 1080) # 55 seconds, 5 second pause at end, 0.5 second fade in, 2 second fade out scene.set_time_seconds(55, 5, 0.5, 2) scene.set_movie_real_time(t1, t2) scene.render_scene() m.end_movie() # Add music. Requires ffmpeg to be installed on your system. m.add_music(movie.factorio_music_path + 'expansion.ogg')
def recurse(self, bbox, depth): """ Internal function called on class construction, this should create the BoundingBoxes. :param bbox: the initial BoundingBox :param depth: the depth of the QuadTree """ # check the condition of recursion if depth < self.depth: # To obtain the coordinate of X&Y and calculate the midpoint minX = bbox.data[0, 0] maxX = bbox.data[0, 1] midX = (minX + maxX) / 2.0 minY = bbox.data[1, 0] maxY = bbox.data[1, 1] midY = (minY + maxY) / 2.0 # To create an array consisting of above coordinates axis_X = [minX, midX, maxX] axis_Y = [minY, midY, maxY] for i in range(0, 2): for j in range(0, 2): # To generate offspring boundingboxes child_bbox = bb.BoundingBox(axis_X[i], axis_X[i + 1], axis_Y[j], axis_Y[j + 1]) # Add offspring boundingbox into quadtree self.quads[depth].append(child_bbox) # recurse self.recurse(child_bbox, depth + 1)
# search for the closest point by comparing coordinate of point # in the given axis and partition if point[axis] <= partition: boxes.extend(self.closest(point, sidx.left())) if partition < point[axis]: boxes.extend(self.closest(point, sidx.right())) return boxes if __name__ == '__main__': data = [[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]] database = db.Database(["x", "y"]) database.insert_iterable(data) tree = KDTree(database, {"max-depth": 3}) for node in tree.storage: print(node) print(tree.bounding_box()) for k, v in tree.partitions().items(): print(k, len(v)) bbox = bb.BoundingBox(1, 2, 1, 2) print(tree.rquery(bbox)) print(database.query(tree.rquery(bbox))) print(tree.closest([7, 2]))
:param size: requested size >>> print(QuadTree.level(1)) >>> 0 >>> print(QuadTree.level(5)) >>> 2 """ return int(math.ceil(math.log(size, 4))) def quadrants(self): """ Returns the quads member """ return self.quads if __name__ == '__main__': bbox = bb.BoundingBox(2, 9, 1, 7) print(QuadTree.at_least(900)) print(QuadTree.at_most(900)) print(QuadTree.level(1)) print(QuadTree.level(5)) print(QuadTree.level(900)) qt = QuadTree(bbox, 2) for k, v in qt.quads.items(): print(k, len(v)) for x in v: print(x.data)
def recurse(self, bbox, depth): """ Internal function called on class contruction, this should create the BoundingBoxes. :param bbox: the initial BoundingBox :param depth: the depth of the QuadTree :To be implemented by the student: """ root_node = bbox current_level = 1 while current_level < depth: if current_level == 1: bbWidth = root_node.width() bbHeight = root_node.height() lowerleft = root_node.lower_left() bottom_left = bb.BoundingBox(lowerleft[0], lowerleft[0] + bbWidth / 2, lowerleft[1], lowerleft[1] + bbHeight / 2) bottom_right = bb.BoundingBox(lowerleft[0] + bbWidth / 2, lowerleft[0] + bbWidth, lowerleft[1], lowerleft[1] + bbHeight / 2) top_left = bb.BoundingBox(lowerleft[0], lowerleft[0] + bbWidth / 2, lowerleft[1] + bbHeight / 2, lowerleft[1] + bbHeight) top_right = bb.BoundingBox(lowerleft[0] + bbWidth / 2, lowerleft[0] + bbWidth, lowerleft[1] + bbHeight / 2, lowerleft[1] + bbHeight) self.quads[current_level] = [ bottom_left, bottom_right, top_left, top_right ] current_level = current_level + 1 print('current level', current_level) else: print("HELLO") self.quads[current_level] = [] for quad in range(len(self.quads[current_level - 1])): parentQuad = self.quads[current_level - 1][quad] bbWidth = parentQuad.width() bbHeight = parentQuad.height() lowerleft = parentQuad.lower_left() bottom_left = bb.BoundingBox(lowerleft[0], lowerleft[0] + bbWidth / 2, lowerleft[1], lowerleft[1] + bbHeight / 2) bottom_right = bb.BoundingBox(lowerleft[0] + bbWidth / 2, lowerleft[0] + bbWidth, lowerleft[1], lowerleft[1] + bbHeight / 2) top_left = bb.BoundingBox(lowerleft[0], lowerleft[0] + bbWidth / 2, lowerleft[1] + bbHeight / 2, lowerleft[1] + bbHeight) top_right = bb.BoundingBox(lowerleft[0] + bbWidth / 2, lowerleft[0] + bbWidth, lowerleft[1] + bbHeight / 2, lowerleft[1] + bbHeight) self.quads[current_level].extend( [bottom_left, bottom_right, top_left, top_right]) current_level = current_level + 1
request `at most` an amount of bboxes. The returned value is <= than size. :param size: maximum requested size :Example: # >>> print(QuadTree.at_most(900)) # >>> 256 """ return 4**int(math.floor(math.log(size, 4))) @staticmethod def level(size): return int(math.ceil(math.log(size, 4))) def quadrants(self): return self.quads if __name__ == '__main__': bbox = bb.BoundingBox(0, 10, 0, 10) qt = QuadTree(bbox, 3) for k, v in qt.quads.items(): print(k, len(v)) for x in v: print(x.data)