Esempio n. 1
0
    def __init__(self, width=1, height=1):
        """Instance initializer.

    Args:
      width: Viewport width, in pixels.
      height: Viewport height, in pixels.
    """
        self._screen_size = types.MJRRECT(0, 0, width, height)
Esempio n. 2
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)
Esempio n. 3
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import absltest
from absl.testing import parameterized
from dm_control.mujoco import wrapper
from dm_control.mujoco.wrapper.mjbindings import enums
from dm_control.mujoco.wrapper.mjbindings import types
from dm_control.viewer import renderer
import mock
import numpy as np

renderer.mjlib = mock.MagicMock()

_SCREEN_SIZE = types.MJRRECT(0, 0, 320, 240)


class BaseRendererTest(absltest.TestCase):
    class MockRenderer(renderer.BaseRenderer):
        pass

    class MockRenderComponent(renderer.Component):

        counter = 0

        def __init__(self):
            self._call_order = -1

        def render(self, context, viewport):
            self._call_order = BaseRendererTest.MockRenderComponent.counter