Example #1
0
def compute_prob_neighbors(geometry: Geometry, grid: Grid, ped: Pedestrian,
                           floorfield, weight_direction: bool):
    prob = {}

    # compute visible area
    x, y = grid.get_coordinates(ped.i(), ped.j())
    vis = geometry.visible_area(x, y)

    # compute voronoi polygons of neighbors
    neighbor_voronoi_polygons = compute_voronoi_neighbors(geometry, grid, ped)

    intersections = []

    weight_distance = compute_point_distance(geometry, grid,
                                             [ped.i(), ped.j()])
    weight_prob = distance_to_prob_dec(weight_distance, 40, 0.15)

    weighted_floorfield = weight_prob * floorfield

    inside = grid.get_inside_polygon_cells(geometry, vis.exterior.coords)

    # sum up every cell in neighbor polygon to neighbor cell
    # sum is weighted by distance, closer = more important
    for key, polygon in neighbor_voronoi_polygons.items():
        if key == Neighbors.self:
            intersections.append(polygon)
            prob[key] = floorfield.filled(0)[ped.i()][ped.j()]

        elif polygon is not None:
            p = Polygon(polygon.coords)
            inter = p.intersection(vis)

            points = []
            if inter.geom_type == 'Polygon':
                points = inter.exterior.coords
            elif inter.geom_type == 'GeometryCollection':
                for i in inter.geoms:
                    if i.geom_type == 'Polygon':
                        for ppp in i.exterior.coords:
                            points.append([ppp[0], ppp[1]])
            elif inter.geom_type == 'MultiPolygon':
                for i in inter.geoms:
                    if i.geom_type == 'Polygon':
                        for ppp in i.exterior.coords:
                            points.append([ppp[0], ppp[1]])
            else:
                print(inter)

            if len(points) == 0:
                prob[key] = 0
            else:
                intersection = sg.Polygon(points)
                intersections.append(intersection)

                inside_cells = grid.get_inside_polygon_cells(geometry, points)
                combination = inside_cells * weighted_floorfield

                prob[key] = np.ma.max(combination, fill_value=0)

    # weight cells by moving direction
    if weight_direction:
        weights = {}
        for key, p in prob.items():
            weights[key] = weighted_neighbors[ped.direction][key]
        weights = normalize_dict(weights)
        for key, p in prob.items():
            prob[key] = p * weights[key]

    prob = normalize_dict(prob)
    return prob