a
    ijd,                     @   sj   d Z ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
ZG d	d
 d
ZdS )z?Defines a renderer class for the MuJoCo Python native bindings.    )OptionalUnion)_enums)
_functions)_render)_structs)
gl_contextNc                   @   s   e Zd ZdZd*ejeeeddddZed	d
 Z	eej
dddZedd Zedd Zdd Zdd Zdd Zdd Zddeej ejdddZd+ejeeeejf eej dd d!Zddd"d#Zd$d% Zd&d' Zddd(d)ZdS ),RendererzRenders MuJoCo scenes.   @  '  N)modelheightwidthmax_geomreturnc                 C   s  |j jj}|j jj}||kr6td| d| d ||krXtd| d| d || _|| _|| _t	j
||d| _t	 | _tdd| j| j| _d	| _tjd	urt||| _| jr| j  t|tjjj| _ttjjj| j tjj| j_ d
| _!d
| _"d	S )a0  Initializes a new `Renderer`.

    Args:
      model: an MjModel instance.
      height: image height in pixels.
      width: image width in pixels.
      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.
    z
Image width z > framebuffer width z. Either reduce the image
width or specify a larger offscreen framebuffer in the model XML using the
clause:
<visual>
  <global offwidth="my_width"/>
</visual>z
Image height z > framebuffer height z. Either reduce the
image height or specify a larger offscreen framebuffer in the model XML using
the clause:
<visual>
  <global offheight="my_height"/>
</visual>)r   Zmaxgeomr   NF)#visZglobal_ZoffwidthZ	offheight
ValueErrorlstrip_width_height_modelr   MjvScene_scene	MjvOption_scene_optionr   ZMjrRect_rect_gl_contextr   Z	GLContextmake_currentZ
MjrContextr   ZmjtFontScaleZmjFONTSCALE_150value_mjr_contextZmjr_setBufferZmjtFramebufferZmjFB_OFFSCREENZmjtDepthMapZmjDEPTH_ZEROFARZreadDepthMap_depth_rendering_segmentation_rendering)selfr   r   r   r   Zbuffer_widthZbuffer_height r$   O/home/ghrups/robot-bench/.venv/lib64/python3.9/site-packages/mujoco/renderer.py__init__   sD    





zRenderer.__init__c                 C   s   | j S N)r   r#   r$   r$   r%   r   c   s    zRenderer.model)r   c                 C   s   | j S r'   )r   r(   r$   r$   r%   sceneg   s    zRenderer.scenec                 C   s   | j S r'   )r   r(   r$   r$   r%   r   k   s    zRenderer.heightc                 C   s   | j S r'   )r   r(   r$   r$   r%   r   o   s    zRenderer.widthc                 C   s   d| _ d| _d S )NFTr"   r!   r(   r$   r$   r%   enable_depth_renderings   s    zRenderer.enable_depth_renderingc                 C   s
   d| _ d S NF)r!   r(   r$   r$   r%   disable_depth_renderingw   s    z Renderer.disable_depth_renderingc                 C   s   d| _ d| _d S )NTFr*   r(   r$   r$   r%   enable_segmentation_renderingz   s    z&Renderer.enable_segmentation_renderingc                 C   s
   d| _ d S r,   )r"   r(   r$   r$   r%   disable_segmentation_rendering~   s    z'Renderer.disable_segmentation_rendering)out)r0   r   c             
   C   s,  | j j }| js| jr8d| j jtjj< d| j jtjj< | j	du rJt
d| jrZ| j  | jrt| j| jf}tj}n| j| jdf}tj}|du rtj||d}n8|j|krtd| d|j d| j| jf d	| j d
	t| j| j | j	 | jrtd|| j| j	 | jjj}| jjjj | }| jjjj!| }t|}t|}	||	  ||	  }
td| |	  ||	  }td|
 td }
td| }|"tj#}|||
  }|"tj|dd< t$| j j| n<| jrt|d| j| j	 |"tj%}|dddddf |dddddf d  |dddddf d  }| j j&}tj'|d dfdtj(d}dd | j j)d| D }t*dd |D tj(}t*dd |D tj(}t*dd |D tj(}|||df< |||df< || }t$| j j| nt|d| j| j	 t+||dd< |S )a  Renders the scene as a numpy array of pixel values.

    Args:
      out: Alternative output array in which to place the resulting pixels. It
        must have the same shape as the expected output but the type will be
        cast if necessary. The expted shape depends on the value of
        `self._depth_rendering`: when `True`, we expect `out.shape == (width,
        height)`, and `out.shape == (width, height, 3)` when `False`.

    Returns:
      A new numpy array holding the pixels with shape `(H, W)` or `(H, W, 3)`,
      depending on the value of `self._depth_rendering` unless
      `out is None`, in which case a reference to `out` is returned.

    Raises:
      RuntimeError: if this method is called after the close method.
    TNz$render cannot be called after close.   )dtypezExpected `out.shape == z`. Got `out.shape=z` instead. When using depth rendering, the out array should be of shape `(width, height)` and otherwise (width, height, 3). Got `(self.height, self.width)=z` and `self._depth_rendering=z`.   g      g      ?r         i   )Z
fill_valuer2   c                 S   s   g | ]}|j d kr|qS )r6   Zsegid.0gr$   r$   r%   
<listcomp>       z#Renderer.render.<locals>.<listcomp>c                 S   s   g | ]}|j d  qS )r4   r7   r8   r$   r$   r%   r;      r<   c                 S   s   g | ]
}|j qS r$   )Zobjidr8   r$   r$   r%   r;      r<   c                 S   s   g | ]
}|j qS r$   )objtyper8   r$   r$   r%   r;      r<   ),r   flagscopyr!   r"   r   Z
mjtRndFlagZmjRND_SEGMENTZmjRND_IDCOLORr    RuntimeErrorr   r   r   r   npZfloat32Zuint8emptyshaper   r   r   r   Z
mjr_renderr   Zmjr_readPixelsr   statextentr   mapznearzfarZastypeZfloat64ZcopytoZuint32ZngeomfullZint32ZgeomsarrayZflipud)r#   r0   Zoriginal_flagsZ	out_shapeZ	out_dtyperE   ZnearZfarrH   rG   Zc_coefZd_coefZout_64Zimage3ZsegimageZngeomsZsegid2outputZvisible_geomsZvisible_segidsZvisible_objidZvisible_objtyper$   r$   r%   render   sv    



	


HzRenderer.renderr6   )datacamerascene_optionc              	   C   s   t |tjs|}t |trHt| jtjj	j
|}|dkrHtd| d|dk s\|| jjkrvtd| d| jj dt }||_|dkrtjj|_t| j| n
tjj|_|p| j}t| j||d|tjjj
| j dS )av  Updates geometry used for rendering.

    Args:
      data: An instance of `MjData`.
      camera: An instance of `MjvCamera`, a string or an integer
      scene_option: A custom `MjvOption` instance to use to render the scene
        instead of the default.

    Raises:
      ValueError: If `camera_id` is outside the valid range, or if camera does
        not exist.
    r6   zThe camera "z" does not exist.zThe camera id z is out of range [-1, z).N)
isinstancer   	MjvCamerastrr   Z
mj_name2idr   r   ZmjtObjZmjOBJ_CAMERAr   r   ZncamZ
fixedcamidZ	mjtCameraZmjCAMERA_FREEtypeZmjv_defaultFreeCameraZmjCAMERA_FIXEDr   Zmjv_updateSceneZ	mjtCatBitZ	mjCAT_ALLr   )r#   rL   rM   rN   Z	camera_idr$   r$   r%   update_scene   s<    



zRenderer.update_scenec                 C   s0   | j r| j   d| _ | jr&| j  d| _dS )a"  Frees the resources used by the renderer.

    This method can be used directly:

    ```python
    renderer = Renderer(...)
    # Use renderer.
    renderer.close()
    ```

    or via a context manager:

    ```python
    with Renderer(...) as renderer:
      # Use renderer.
    ```
    N)r   freer    r(   r$   r$   r%   close0  s    

zRenderer.closec                 C   s   | S r'   r$   r(   r$   r$   r%   	__enter__I  s    zRenderer.__enter__c                 C   s   ~~~|    d S r'   rU   )r#   exc_type	exc_value	tracebackr$   r$   r%   __exit__L  s    zRenderer.__exit__c                 C   s   |    d S r'   rW   r(   r$   r$   r%   __del__P  s    zRenderer.__del__)r
   r   r   )r6   N) __name__
__module____qualname____doc__r   ZMjModelintr&   propertyr   r   r)   r   r   r+   r-   r.   r/   r   rA   ZndarrayrK   ZMjDatar   rQ   rP   r   rS   rU   rV   r[   r\   r$   r$   r$   r%   r	      sF      E


{  7r	   )r`   typingr   r   Zmujocor   r   r   r   r   numpyrA   r	   r$   r$   r$   r%   <module>   s   