Exemple #1
0
    def __init__(self, physics):
        super(Visualizer, self).__init__()
        # initialize rendering engine
        enginewrapper.init()
        # save a reference to the physics
        self._physics = physics
        # create the scene for the abstract visualization stage
        self._scene = wrapper.MjvScene()
        self._scene_option = wrapper.MjvOption()
        # a perturbation object, just for completion
        self._perturb = wrapper.MjvPerturb()
        self._perturb.active = 0
        self._perturb.select = 0

        # create a mjvcamera, as it seems is needed for this stage
        self._render_camera = wrapper.MjvCamera()
        self._render_camera.fixedcamid = -1
        self._render_camera.type_ = enums.mjtCamera.mjCAMERA_FREE

        # a list to store the geometries from the abstract visualization stage
        self._geometries = {}
        # the meshes wrapped by the bindings
        self._meshes = {}

        # keys
        self._single_keys = [False for i in range(1024)]

        # make a first update to initialize objects
        mjlib.mjv_updateScene(self._physics.model.ptr, self._physics.data.ptr,
                              self._scene_option.ptr, self._perturb.ptr,
                              self._render_camera.ptr,
                              enums.mjtCatBit.mjCAT_ALL, self._scene.ptr)
        self._collect_geometries()
        self._update_geometries_meshes()
Exemple #2
0
  def egocentric_camera(self):
    options = mj_wrapper.MjvOption()

    # Don't render this walker's geoms.
    options.geomgroup[_WALKER_INVIS_GROUP] = 0
    return observable.MJCFCamera(self._entity.egocentric_camera,
                                 width=64, height=64, scene_option=options)
Exemple #3
0
    def testSegmentationRender(self, enable_geom_frame_rendering):
        box_four_corners = """
    <mujoco>
      <visual>
        <scale framelength="2"/>
      </visual>
      <worldbody>
        <geom name="box0" type="box" size=".2 .2 .2" pos="-1 1 .1"/>
        <geom name="box1" type="box" size=".2 .2 .2" pos="1 1 .1"/>
        <site name="box2" type="box" size=".2 .2 .2" pos="1 -1 .1"/>
        <site name="box3" type="box" size=".2 .2 .2" pos="-1 -1 .1"/>
        <camera name="top" pos="0 0 3"/>
      </worldbody>
    </mujoco>
    """
        physics = engine.Physics.from_xml_string(box_four_corners)
        obj_type_geom = enums.mjtObj.mjOBJ_GEOM  # Geom object type
        obj_type_site = enums.mjtObj.mjOBJ_SITE  # Site object type
        obj_type_decor = enums.mjtObj.mjOBJ_UNKNOWN  # Decor object type
        scene_options = wrapper.MjvOption()
        if enable_geom_frame_rendering:
            scene_options.frame = wrapper.mjbindings.enums.mjtFrame.mjFRAME_GEOM
        pixels = physics.render(height=200,
                                width=200,
                                camera_id='top',
                                segmentation=True,
                                scene_option=scene_options)

        # The pixel indices below were chosen so that toggling the frame decors do
        # not affect the segmentation results.
        with self.subTest('Center pixels should have background label'):
            np.testing.assert_equal(pixels[95:105, 95:105, 0], -1)
            np.testing.assert_equal(pixels[95:105, 95:105, 1], -1)
        with self.subTest('Geoms have correct object type'):
            np.testing.assert_equal(pixels[15:25, 0:10, 1], obj_type_geom)
            np.testing.assert_equal(pixels[15:25, 190:200, 1], obj_type_geom)
        with self.subTest('Sites have correct object type'):
            np.testing.assert_equal(pixels[190:200, 190:200, 1], obj_type_site)
            np.testing.assert_equal(pixels[190:200, 0:10, 1], obj_type_site)
        with self.subTest('Geoms have correct object IDs'):
            np.testing.assert_equal(
                pixels[15:25, 0:10, 0],
                physics.model.name2id('box0', obj_type_geom))
            np.testing.assert_equal(
                pixels[15:25, 190:200, 0],
                physics.model.name2id('box1', obj_type_geom))
        with self.subTest('Sites have correct object IDs'):
            np.testing.assert_equal(
                pixels[190:200, 190:200, 0],
                physics.model.name2id('box2', obj_type_site))
            np.testing.assert_equal(
                pixels[190:200, 0:10, 0],
                physics.model.name2id('box3', obj_type_site))
        with self.subTest(
                'Decor elements present if and only if geom frames are '
                'enabled'):
            contains_decor = np.any(pixels[:, :, 1] == obj_type_decor)
            self.assertEqual(contains_decor, enable_geom_frame_rendering)
Exemple #4
0
  def testSceneOption(self):
    height, width = 480, 640
    scene_option = wrapper.MjvOption()
    mjlib.mjv_defaultOption(scene_option.ptr)

    # Render geoms as semi-transparent.
    scene_option.flags[enums.mjtVisFlag.mjVIS_TRANSPARENT] = 1

    no_scene_option = self._physics.render(height, width, camera_id=0)
    with_scene_option = self._physics.render(height, width, camera_id=0,
                                             scene_option=scene_option)
    self.assertFalse(np.all(no_scene_option == with_scene_option),
                     msg='Images are identical with and without scene option.')
Exemple #5
0
    def egocentric_camera(self):
        """Observable of the egocentric camera."""

        if not hasattr(self, '_scene_options'):
            # Don't render this walker's geoms.
            self._scene_options = mj_wrapper.MjvOption()
            collision_geom_group = 2
            self._scene_options.geomgroup[collision_geom_group] = 0
            cosmetic_geom_group = 1
            self._scene_options.geomgroup[cosmetic_geom_group] = 0

        return observable.MJCFCamera(self._entity.egocentric_camera,
                                     width=64,
                                     height=64,
                                     scene_option=self._scene_options)
Exemple #6
0
    def __init__(self,
                 physics,
                 height=240,
                 width=320,
                 camera_id=-1,
                 max_geom=None):
        """Initializes a new `Camera`.

    Args:
      physics: Instance of `Physics`.
      height: Optional image height. Defaults to 240.
      width: Optional image width. Defaults to 320.
      camera_id: Optional camera name or index. Defaults to -1, the free
        camera, which is always defined. A nonnegative integer or string
        corresponds to a fixed camera, which must be defined in the model XML.
        If `camera_id` is a string then the camera must also be named.
      max_geom: Optional integer specifying the maximum number of geoms that can
        be rendered in the same scene. If None this will be chosen automatically
        based on the estimated maximum number of renderable geoms in the model.
    Raises:
      ValueError: If `camera_id` is outside the valid range, or if `width` or
        `height` exceed the dimensions of MuJoCo's offscreen framebuffer.
    """
        buffer_width = physics.model.vis.global_.offwidth
        buffer_height = physics.model.vis.global_.offheight
        if width > buffer_width:
            raise ValueError(
                'Image width {} > framebuffer width {}. Either reduce '
                'the image width or specify a larger offscreen '
                'framebuffer in the model XML using the clause\n'
                '<visual>\n'
                '  <global offwidth="my_width"/>\n'
                '</visual>'.format(width, buffer_width))
        if height > buffer_height:
            raise ValueError(
                'Image height {} > framebuffer height {}. Either reduce '
                'the image height or specify a larger offscreen '
                'framebuffer in the model XML using the clause\n'
                '<visual>\n'
                '  <global offheight="my_height"/>\n'
                '</visual>'.format(height, buffer_height))
        if isinstance(camera_id, six.string_types):
            camera_id = physics.model.name2id(camera_id, 'camera')
        if camera_id < -1:
            raise ValueError('camera_id cannot be smaller than -1.')
        if camera_id >= physics.model.ncam:
            raise ValueError(
                'model has {} fixed cameras. camera_id={} is invalid.'.format(
                    physics.model.ncam, camera_id))

        self._width = width
        self._height = height
        self._physics = physics

        # Variables corresponding to structs needed by Mujoco's rendering functions.
        self._scene = wrapper.MjvScene(model=physics.model, max_geom=max_geom)
        self._scene_option = wrapper.MjvOption()

        self._perturb = wrapper.MjvPerturb()
        self._perturb.active = 0
        self._perturb.select = 0

        self._rect = types.MJRRECT(0, 0, self._width, self._height)

        self._render_camera = wrapper.MjvCamera()
        self._render_camera.fixedcamid = camera_id

        if camera_id == -1:
            self._render_camera.type_ = enums.mjtCamera.mjCAMERA_FREE
        else:
            # As defined in the Mujoco documentation, mjCAMERA_FIXED refers to a
            # camera explicitly defined in the model.
            self._render_camera.type_ = enums.mjtCamera.mjCAMERA_FIXED

        # Internal buffers.
        self._rgb_buffer = np.empty((self._height, self._width, 3),
                                    dtype=np.uint8)
        self._depth_buffer = np.empty((self._height, self._width),
                                      dtype=np.float32)

        if self._physics.contexts.mujoco is not None:
            with self._physics.contexts.gl.make_current() as ctx:
                ctx.call(mjlib.mjr_setBuffer,
                         enums.mjtFramebuffer.mjFB_OFFSCREEN,
                         self._physics.contexts.mujoco.ptr)
 def __init__(self):
     self._visualization_options = wrapper.MjvOption()
     self._stereo_mode = enums.mjtStereo.mjSTEREO_NONE
     self._render_flags = _DEFAULT_RENDER_FLAGS
Exemple #8
0
 def top_down_camera_invisible_robot(self):
   # Custom scene options for making robot geoms invisible.
   robot_geoms_invisible = wrapper.MjvOption()
   robot_geoms_invisible.geomgroup[ROBOT_GEOM_GROUP] = 0
   return observable.MJCFCamera(mjcf_element=self._entity.top_down_camera,
                                scene_option=robot_geoms_invisible)