(self,
             pos,
             size,
             inverted=False,
             visible=False,
             rgba=(1, 1, 1, 1),
             material=None,
             detected_rgba=(0, 1, 0, 0.25),
             retain_substep_detections=False,
             name='position_detector')
Builds the detector.

    Args:
      pos: The position at the center of this detector's active region. Should
        be an array-like object of length 3 in 3D mode, or length 2 in 2D mode.
      size: The half-lengths of this detector's active region. Should
        be an array-like object of length 3 in 3D mode, or length 2 in 2D mode.
      inverted: (optional) A boolean, whether to operate in inverted detection
        mode. If `True`, an entity is detected when it is not in the active
        region.
      visible: (optional) A boolean, whether this detector is visible by
        default in rendered images. If `False`, this detector's active zone
        is placed in MuJoCo rendering group 4, which is not rendered by default,
        but can be toggled on (e.g. in `dm_control.viewer`) for debugging
        purposes.
      rgba: (optional) The color to render when nothing is detected.
      material: (optional) The material of the position detector.
      detected_rgba: (optional) The color to render when an entity is detected.
      retain_substep_detections: (optional) If `True`, the detector will remain
        activated at the end of a control step if it became activated at any
        substep. If `False`, the detector reports its instantaneous state.
      name: (optional) XML element name of this position detector.

    Raises:
      ValueError: If the `pos` and `size` arrays do not have the same length.
Builds the detector.
Builds the detector .
def _build(self,
             pos,
             size,
             inverted=False,
             visible=False,
             rgba=(1, 1, 1, 1),
             material=None,
             detected_rgba=(0, 1, 0, 0.25),
             retain_substep_detections=False,
             name='position_detector'):
    """Builds the detector.

    Args:
      pos: The position at the center of this detector's active region. Should
        be an array-like object of length 3 in 3D mode, or length 2 in 2D mode.
      size: The half-lengths of this detector's active region. Should
        be an array-like object of length 3 in 3D mode, or length 2 in 2D mode.
      inverted: (optional) A boolean, whether to operate in inverted detection
        mode. If `True`, an entity is detected when it is not in the active
        region.
      visible: (optional) A boolean, whether this detector is visible by
        default in rendered images. If `False`, this detector's active zone
        is placed in MuJoCo rendering group 4, which is not rendered by default,
        but can be toggled on (e.g. in `dm_control.viewer`) for debugging
        purposes.
      rgba: (optional) The color to render when nothing is detected.
      material: (optional) The material of the position detector.
      detected_rgba: (optional) The color to render when an entity is detected.
      retain_substep_detections: (optional) If `True`, the detector will remain
        activated at the end of a control step if it became activated at any
        substep. If `False`, the detector reports its instantaneous state.
      name: (optional) XML element name of this position detector.

    Raises:
      ValueError: If the `pos` and `size` arrays do not have the same length.
    """
    if len(pos) != len(size):
      raise ValueError('`pos` and `size` should have the same length: '
                       'got {!r} and {!r}'.format(pos, size))

    self._inverted = inverted
    self._detected = False
    self._retain_substep_detections = retain_substep_detections
    self._lower = np.array(pos) - np.array(size)
    self._upper = np.array(pos) + np.array(size)
    self._lower_3d = _ensure_3d(self._lower)
    self._upper_3d = _ensure_3d(self._upper)
    self._mid_3d = (self._lower_3d + self._upper_3d) / 2.

    self._entities = []
    self._entity_geoms = {}

    self._rgba = np.asarray(rgba)
    self._detected_rgba = np.asarray(detected_rgba)

    render_pos = np.zeros(3)
    render_pos[:len(pos)] = pos

    render_size = np.full(3, _RENDERED_HEIGHT_IN_2D_MODE)
    render_size[:len(size)] = size

    self._mjcf_root = mjcf.RootElement(model=name)
    self._site = self._mjcf_root.worldbody.add(
        'site', name='detection_zone', type='box',
        pos=render_pos, size=render_size, rgba=self._rgba, material=material)
    self._lower_site = self._mjcf_root.worldbody.add(
        'site', name='lower', pos=self._lower_3d, size=[0.05],
        rgba=self._rgba)
    self._mid_site = self._mjcf_root.worldbody.add(
        'site', name='mid', pos=self._mid_3d, size=[0.05],
        rgba=self._rgba)
    self._upper_site = self._mjcf_root.worldbody.add(
        'site', name='upper', pos=self._upper_3d, size=[0.05],
        rgba=self._rgba)
    self._lower_sensor = self._mjcf_root.sensor.add(
        'framepos', objtype='site', objname=self._lower_site,
        name='{}_lower'.format(name))
    self._mid_sensor = self._mjcf_root.sensor.add(
        'framepos', objtype='site', objname=self._mid_site,
        name='{}_mid'.format(name))
    self._upper_sensor = self._mjcf_root.sensor.add(
        'framepos', objtype='site', objname=self._upper_site,
        name='{}_upper'.format(name))

    if not visible:
      self._site.group = composer.SENSOR_SITES_GROUP
      self._lower_site.group = composer.SENSOR_SITES_GROUP
      self._mid_site.group = composer.SENSOR_SITES_GROUP
      self._upper_site.group = composer.SENSOR_SITES_GROUP
def _build ( self , pos , size , inverted = False , visible = False , rgba = ( 1 , 1 , 1 , 1 ) , material = None , detected_rgba = ( 0 , 1 , 0 , 0.25 ) , retain_substep_detections = False , name = 'position_detector' ) : if len ( pos ) != len ( size ) : raise ValueError ( '`pos` and `size` should have the same length: ' 'got {!r} and {!r}' . format ( pos , size ) ) self . _inverted = inverted self . _detected = False self . _retain_substep_detections = retain_substep_detections self . _lower = np . array ( pos ) - np . array ( size ) self . _upper = np . array ( pos ) + np . array ( size ) self . _lower_3d = _ensure_3d ( self . _lower ) self . _upper_3d = _ensure_3d ( self . _upper ) self . _mid_3d = ( self . _lower_3d + self . _upper_3d ) / 2. self . _entities = [ ] self . _entity_geoms = { } self . _rgba = np . asarray ( rgba ) self . _detected_rgba = np . asarray ( detected_rgba ) render_pos = np . zeros ( 3 ) render_pos [ : len ( pos ) ] = pos render_size = np . full ( 3 , _RENDERED_HEIGHT_IN_2D_MODE ) render_size [ : len ( size ) ] = size self . _mjcf_root = mjcf . RootElement ( model = name ) self . _site = self . _mjcf_root . worldbody . add ( 'site' , name = 'detection_zone' , type = 'box' , pos = render_pos , size = render_size , rgba = self . _rgba , material = material ) self . _lower_site = self . _mjcf_root . worldbody . add ( 'site' , name = 'lower' , pos = self . _lower_3d , size = [ 0.05 ] , rgba = self . _rgba ) self . _mid_site = self . _mjcf_root . worldbody . add ( 'site' , name = 'mid' , pos = self . _mid_3d , size = [ 0.05 ] , rgba = self . _rgba ) self . _upper_site = self . _mjcf_root . worldbody . add ( 'site' , name = 'upper' , pos = self . _upper_3d , size = [ 0.05 ] , rgba = self . _rgba ) self . _lower_sensor = self . _mjcf_root . sensor . add ( 'framepos' , objtype = 'site' , objname = self . _lower_site , name = '{}_lower' . format ( name ) ) self . _mid_sensor = self . _mjcf_root . sensor . add ( 'framepos' , objtype = 'site' , objname = self . _mid_site , name = '{}_mid' . format ( name ) ) self . _upper_sensor = self . _mjcf_root . sensor . add ( 'framepos' , objtype = 'site' , objname = self . _upper_site , name = '{}_upper' . format ( name ) ) if not visible : self . _site . group = composer . SENSOR_SITES_GROUP self . _lower_site . group = composer . SENSOR_SITES_GROUP self . _mid_site . group = composer . SENSOR_SITES_GROUP self . _upper_site . group = composer . SENSOR_SITES_GROUP