Exemplo n.º 1
0
 def distance_from_point_to_point(self, position: Dict[str, float],
                                  target: Dict[str, float],
                                  allowedError: float) -> float:
     path = self.path_from_point_to_point(position, target, allowedError)
     if path:
         return metrics.path_distance(path)
     return -1.0
Exemplo n.º 2
0
    def distance_from_point_to_object_type(self, point: Dict[str, float],
                                           object_type: str) -> float:
        """Minimal geodesic distance from a point to an object of the given
        type.

        It might return -1.0 for unreachable targets.
        """
        path = self.path_from_point_to_object_type(point, object_type)
        if path:
            return metrics.path_distance(path)
        return -1.0
Exemplo n.º 3
0
 def distance_from_point_to_point(self, position: Dict[str, float],
                                  target: Dict[str, float],
                                  allowed_error: float) -> float:
     path = self.path_from_point_to_point(position, target, allowed_error)
     if path:
         # Because `allowed_error != 0` means that the path returned above might not start
         # or end exactly at the position/target points, we explictly add any offset there is.
         s_dist = math.sqrt((position["x"] - path[0]["x"])**2 +
                            (position["z"] - path[0]["z"])**2)
         t_dist = math.sqrt((target["x"] - path[-1]["x"])**2 +
                            (target["z"] - path[-1]["z"])**2)
         return metrics.path_distance(path) + s_dist + t_dist
     return -1.0
Exemplo n.º 4
0
    def distance_from_point_to_object_type(self, point: Dict[str, float],
                                           object_type: str,
                                           allowed_error: float) -> float:
        """Minimal geodesic distance from a point to an object of the given
        type.

        It might return -1.0 for unreachable targets.
        """
        path = self.path_from_point_to_object_type(point, object_type,
                                                   allowed_error)
        if path:
            # Because `allowed_error != 0` means that the path returned above might not start
            # at `point`, we explicitly add any offset there is.
            s_dist = math.sqrt((point["x"] - path[0]["x"])**2 +
                               (point["z"] - path[0]["z"])**2)
            return metrics.path_distance(path) + s_dist
        return -1.0
def get_points(controller,
               object_type,
               scene,
               objects_types_in_scene,
               failed_points,
               grid_size,
               rotate_by,
               desired_points=30):
    print("Getting points in scene {}, with target object {}...: ".format(
        scene, object_type))
    controller.reset(scene)
    event = controller.step(
        dict(action='ObjectTypeToObjectIds',
             objectType=object_type.replace(" ", "")))
    object_ids = event.metadata['actionReturn']

    if object_ids is None or len(object_ids) > 1 or len(object_ids) == 0:
        print("Object type '{}' not available in scene.".format(object_type))
        return None

    objects_types_in_scene.add(object_type)
    object_id = object_ids[0]

    event_reachable = controller.step(
        dict(action='GetReachablePositions', gridSize=grid_size))

    target_position = controller.step(
        action='GetObjectPosition',
        objectId=object_id).metadata['actionReturn']

    reachable_positions = event_reachable.metadata['actionReturn']

    reachable_pos_set = set([
        (pos['x'], pos['y'], pos['z']) for pos in reachable_positions
        # if sqr_dist_dict(pos, target_position) >= visibility_distance * visibility_multiplier_filter
    ])

    def filter_points(selected_points, point_set, minimum_distance):
        result = set()
        for selected in selected_points:
            if selected in point_set:
                result.add(selected)
                remove_set = set([
                    p for p in point_set
                    if sqr_dist(p, selected) <= minimum_distance *
                    minimum_distance
                ])
                point_set = point_set.difference(remove_set)
        return result

    random.seed()
    points = random.sample(reachable_pos_set, desired_points * 4)

    final_point_set = filter_points(points, reachable_pos_set, grid_size * 2)

    print("Total number of points: {}".format(len(final_point_set)))

    print("Id {}".format(event.metadata['actionReturn']))

    point_objects = []

    eps = 0.0001
    counter = 0
    for (x, y, z) in final_point_set:
        possible_orientations = list(range(0, 360, rotate_by))
        pos_unity = dict(x=x, y=y, z=z)
        try:
            path = metrics.get_shortest_path_to_object(controller, object_id,
                                                       pos_unity, {
                                                           'x': 0,
                                                           'y': 0,
                                                           'z': 0
                                                       })
            minimum_path_length = metrics.path_distance(path)

            rotation_allowed = False
            while not rotation_allowed:
                if len(possible_orientations) == 0:
                    break
                roatation_y = random.choice(possible_orientations)
                possible_orientations.remove(roatation_y)
                evt = controller.step(action="TeleportFull",
                                      x=pos_unity['x'],
                                      y=pos_unity['y'],
                                      z=pos_unity['z'],
                                      rotation=dict(x=0, y=roatation_y, z=0))
                rotation_allowed = evt.metadata['lastActionSuccess']
                if not evt.metadata['lastActionSuccess']:
                    print(evt.metadata['errorMessage'])
                    print("--------- Rotation not allowed! for pos {} rot {} ".
                          format(pos_unity, roatation_y))

            if minimum_path_length > eps and rotation_allowed:
                m = re.search('FloorPlan_([a-zA-Z\-]*)([0-9]+)_([0-9]+)',
                              scene)
                point_id = "{}_{}_{}_{}_{}".format(m.group(1), m.group(2),
                                                   m.group(3), object_type,
                                                   counter)
                point_objects.append({
                    'id':
                    point_id,
                    'scene':
                    scene,
                    'object_type':
                    object_type,
                    'object_id':
                    object_id,
                    'target_position':
                    target_position,
                    'initial_position':
                    pos_unity,
                    'initial_orientation':
                    roatation_y,
                    'shortest_path':
                    path,
                    'shortest_path_length':
                    minimum_path_length
                })
                counter += 1

        except ValueError:
            print("-----Invalid path discarding point...")
            failed_points.append({
                'scene': scene,
                'object_type': object_type,
                'object_id': object_id,
                'target_position': target_position,
                'initial_position': pos_unity
            })

    # sorted_objs = sorted(point_objects,
    #                      key=lambda m: sqr_dist_dict(m['initial_position'], m['target_position']))

    sorted_objs = sorted(point_objects,
                         key=lambda m: m['shortest_path_length'])
    return sorted_objs