Ejemplo n.º 1
0
 def voronoi(self, points, points_center):
     vor = Voronoi(points)
     vor.filtered_points = points_center
     vor.filtered_regions = [
         vor.regions[vor.point_region[i]] for i in range(len(points_center))
     ]
     return vor
Ejemplo n.º 2
0
def voronoi(cells, bounding_box):
    """
    Solution based on
    https://stackoverflow.com/a/33602171
    with only slight modification
    """
    def in_box(cells, bounding_box):
        return np.logical_and(
            np.logical_and(bounding_box[0] <= cells[:, 0],
                           cells[:, 0] <= bounding_box[1]),
            np.logical_and(bounding_box[2] <= cells[:, 1],
                           cells[:, 1] <= bounding_box[3]))

    eps = sys.float_info.epsilon
    # Select cells inside the bounding box
    i = in_box(cells, bounding_box)
    # Mirror points
    points_center = cells[i, :]
    points_left = np.copy(points_center)
    points_left[:, 0] = bounding_box[0] - \
        (points_left[:, 0] - bounding_box[0])
    points_right = np.copy(points_center)
    points_right[:, 0] = bounding_box[1] + \
        (bounding_box[1] - points_right[:, 0])
    points_down = np.copy(points_center)
    points_down[:, 1] = bounding_box[2] - \
        (points_down[:, 1] - bounding_box[2])
    points_up = np.copy(points_center)
    points_up[:, 1] = bounding_box[3] + (bounding_box[3] - points_up[:, 1])
    points = np.append(points_center,
                       np.append(np.append(points_left, points_right, axis=0),
                                 np.append(points_down, points_up, axis=0),
                                 axis=0),
                       axis=0)
    # Compute Voronoi
    vor = Voronoi(points)
    # Filter regions
    regions = []
    for region in vor.regions:
        flag = True
        for index in region:
            if index == -1:
                flag = False
                break
            else:
                x = vor.vertices[index, 0]
                y = vor.vertices[index, 1]
                if not (bounding_box[0] - eps <= x
                        and x <= bounding_box[1] + eps
                        and bounding_box[2] - eps <= y
                        and y <= bounding_box[3] + eps):
                    flag = False
                    break
        if region != [] and flag:
            regions.append(region)
    vor.filtered_points = points_center
    vor.filtered_regions = regions
    return vor
Ejemplo n.º 3
0
def voronoi(coords, factor):
    """
    Calculate diagram voronoi for a set of datapoints (coords) with a bounding factor
    :param coords: Voronoi diagram will be applied to coords points
    :param factor: Factor by which we want to limit our diagram
    :return: An object of Voronoi type with properties attributes like regions,ets.
    """
    bounding_box = np.array([
        min(coords[:, 0]) - factor,
        max(coords[:, 0]) + factor,
        min(coords[:, 1]) - factor,
        max(coords[:, 1]) + factor
    ])  # [x_min, x_max, y_min, y_max] in the map

    # Select coords inside the bounding box
    i = _in_box(coords, bounding_box)
    # Mirror points
    points_center = coords[i, :]
    points_left = np.copy(points_center)
    points_left[:, 0] = bounding_box[0] - (points_left[:, 0] - bounding_box[0])
    points_right = np.copy(points_center)
    points_right[:,
                 0] = bounding_box[1] + (bounding_box[1] - points_right[:, 0])
    points_down = np.copy(points_center)
    points_down[:, 1] = bounding_box[2] - (points_down[:, 1] - bounding_box[2])
    points_up = np.copy(points_center)
    points_up[:, 1] = bounding_box[3] + (bounding_box[3] - points_up[:, 1])
    points = np.append(points_center,
                       np.append(np.append(points_left, points_right, axis=0),
                                 np.append(points_down, points_up, axis=0),
                                 axis=0),
                       axis=0)

    plt.show()
    # Compute Voronoi
    vor = Voronoi(points)
    # Filter regions
    regions = []
    for region in vor.regions:
        flag = True
        for index in region:
            if index == -1:
                flag = False
                break
            x = vor.vertices[index, 0]
            y = vor.vertices[index, 1]
            if not (bounding_box[0] - eps <= x <= bounding_box[1] + eps
                    and bounding_box[2] - eps <= y <= bounding_box[3] + eps):
                flag = False
                break
        if region != [] and flag:
            regions.append(region)
    vor.filtered_points = points_center
    vor.filtered_regions = regions
    return vor
 def voronoi(self, towers, bounding_box=None):
     if bounding_box is None:
         bounding_box = self.bounding_box
     self.points = towers
     # Select towers inside the bounding box
     i = self.in_box(towers, bounding_box)
     # Mirror points
     points_center = towers[i, :]
     points_left = np.copy(points_center)
     points_left[:, 0] = bounding_box[0] - (points_left[:, 0] -
                                            bounding_box[0])
     points_right = np.copy(points_center)
     points_right[:, 0] = bounding_box[1] + (bounding_box[1] -
                                             points_right[:, 0])
     points_down = np.copy(points_center)
     points_down[:, 1] = bounding_box[2] - (points_down[:, 1] -
                                            bounding_box[2])
     points_up = np.copy(points_center)
     points_up[:, 1] = bounding_box[3] + (bounding_box[3] - points_up[:, 1])
     points = np.append(points_center,
                        np.append(np.append(points_left,
                                            points_right,
                                            axis=0),
                                  np.append(points_down, points_up, axis=0),
                                  axis=0),
                        axis=0)
     # Compute Voronoi
     vor = Voronoi(points)
     # Filter regions
     regions = []
     for region in vor.regions:
         flag = True
         for index in region:
             if index == -1:
                 flag = False
                 break
             else:
                 x = vor.vertices[index, 0]
                 y = vor.vertices[index, 1]
                 if not (bounding_box[0] - self.eps <= x
                         and x <= bounding_box[1] + self.eps
                         and bounding_box[2] - self.eps <= y
                         and y <= bounding_box[3] + self.eps):
                     flag = False
                     break
         if region != [] and flag:
             regions.append(region)
     vor.filtered_points = points_center
     vor.filtered_regions = regions
     return vor