Example #1
0
class PointQuadTreeProfileRunner:
    def __init__(self, node_capacity):
        """
        @param node_capacity Integer The node-capacity to use for the PointQuadTree
        """
        boundary = AxisAlignedBoundingBox.positive_quadrant_box(1, 1)
        self._tree = PointQuadTree(boundary=boundary, node_capacity=node_capacity)

    def run(self, seed, num_points):
        """
        @param seed Integer The random-number-generator seed
        @num_points Integer The number of points to use in the profile
        """
        print('Profiling PointQuadTree: node_capacity={}, num_points={}, seed={}.'.format(self._tree._node_capacity, num_points, seed))

        random.seed(seed)

        self._add_random_points(num_points)
        self._query_all_points()
        self._remove_random_points(num_points)
        assert len(self._get_points()) == 0

        self._add_random_points(num_points)
        half_num_points = int(num_points/2)
        self._remove_random_points(half_num_points)
        self._add_random_points(half_num_points)
        assert len(self._get_points()) == num_points

    def _add_random_points(self, num_points):
        for i in range(num_points):
            x = random.random()
            y = random.random()
            self._tree.insert(Point(x, y))

    def _remove_random_points(self, num_points):
        points = self._get_points()
        random.shuffle(points)
        assert num_points <= len(points)
        for i in range(num_points):
            point = points[i]
            self._tree.remove(point)

    def _query_all_points(self):
        for point in self._get_points():
            point_region = AxisAlignedBoundingBox(center_x=point.x,
                                                  center_y=point.y,
                                                  half_size_x=POINT_HALF_SIZE,
                                                  half_size_y=POINT_HALF_SIZE)
            self._tree.query_points_in_region(point_region)

    def _get_points(self):
        return self._tree.get_all_points()
Example #2
0
 def __init__(self, node_capacity):
     """
     @param node_capacity Integer The node-capacity to use for the PointQuadTree
     """
     boundary = AxisAlignedBoundingBox.positive_quadrant_box(1, 1)
     self._tree = PointQuadTree(boundary=boundary, node_capacity=node_capacity)