Пример #1
0
def build_user_input(points=None, rectangulars=None, balls=None):
    points, rectangulars, balls = _prepare_user_input(points, rectangulars,
                                                      balls)
    user_input = scene_if.UserInput(
        flattened_point_list=points.flatten().tolist(), balls=[], polygons=[])
    for start in range(0, len(rectangulars), 8):
        rect = rectangulars[start:start + 8]
        vertices = []
        for i in range(4):
            vertices.append(
                scene_if.Vector(rect[2 * i] - rect[0],
                                rect[2 * i + 1] - rect[1]))
        user_input.polygons.append(
            scene_if.PolygonWithPosition(
                vertices=vertices,
                position=scene_if.Vector(rect[0], rect[1]),
                angle=0,
            ))
    for start in range(0, len(balls), 3):
        ball = balls[start:start + 3]
        user_input.balls.append(
            scene_if.CircleWithPosition(position=scene_if.Vector(
                ball[0], ball[1]),
                                        radius=ball[2]))
    return user_input
def featurized_objects_vector_to_scene(featurized_objects: np.ndarray
                                      ) -> scene_if.Scene:
    """Convert an array of featurized objects into a Scene.

        Args:
            featurized_objects: np.ndarray of size (num_objects, OBJECT_FEATURE_SIZE)
        
        Returns:
            A scene_if.Scene contianing each of the featurized objects.
    """
    task = creator.creator.TaskCreator()
    user_input = scene_if.UserInput(flattened_point_list=[],
                                    balls=[],
                                    polygons=[])
    for features in featurized_objects:
        object_properties = _object_features_to_values(features)
        if object_properties['user_input']:
            assert object_properties['shape_type'] == 'ball', (
                'User input objects must be balls')
            user_input.balls.append(
                scene_if.CircleWithPosition(
                    position=scene_if.Vector(x=object_properties['x'],
                                             y=object_properties['y']),
                    radius=object_properties['diameter'] / 2.0))
        else:
            builder = shapes_lib.get_builders()[object_properties['shape_type']]
            shapes, phantom_vertices = builder.build(
                diameter=object_properties['diameter'])

            body = Body(shapes, object_properties['dynamic'],
                        object_properties['shape_type'],
                        object_properties['diameter'], phantom_vertices)
            body.push(object_properties['x'], object_properties['y'])
            body.set_angle(object_properties['angle'])
            body.set_color(object_properties['color'])

            task.scene.bodies.append(body._thrift_body)
            task.body_list.append(body)
    scene = phyre.simulator.add_user_input_to_scene(task.scene,
                                                    user_input,
                                                    allow_occlusions=True)
    return scene
Пример #3
0
 def add_to_user_input(cls, action, user_input):
     x, y, radius = cls.scale(*action)
     user_input.balls.append(
         scene_if.CircleWithPosition(position=scene_if.Vector(x=x, y=y),
                                     radius=radius))