def test_item_crossing_border(self, max_depth, coords): # adding item that cross borders shouldn't trigger nested subdivisions qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*coords) qt.insert(rect) assert qt.get_children() == [rect] assert qt._get_depth() == 1 assert qt._get_number_of_nodes() == 5
def test_insert_small_rect_in_corner(self, max_depth, coords): # inserting small rectangle in top left corner should make the # quadtree perform maximum number of subdivisions qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*coords) qt.insert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 qt.remove(rect) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1
def test_clear(self): qt = Node(0, 0, 1000, 1000) [qt.insert(random_rectangle(qt.bounds)) for _ in range(300)] assert len(qt.get_children()) == 300 assert qt._get_depth() in [3, 4] # can't know for sure qt.clear() assert qt.get_children() == [] assert qt.direct_children == []
def test_reinsert_into_same_quadrant(self, max_depth): bounds = (0.001, 0.001, 0.002, 0.002) qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*bounds) qt.insert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 # make sure that top-left quadrant is the only one subdivided depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [max_depth - 1, 0, 0, 0] # change coords and reinsert into same place qt.reinsert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [max_depth - 1, 0, 0, 0]
def test_reinsert_into_another_quadrant(self, max_depth): bounds_1 = (0.01, 0.01, 0.02, 0.02) bounds_2 = (1023.998, 1023.998, 1023.999, 1023.999) qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*bounds_1) qt.insert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 # make sure that top-left quadrant is the only one subdivided depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [max_depth - 1, 0, 0, 0] # change coords and reinsert rect.bounds = bounds_2 qt.reinsert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 # make sure that bottom-right quadrant is the only one subdivided depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [0, 0, 0, max_depth - 1]