a
    i‚jH  ã                   @   sX   d Z ddlmZ ddlZddlZe eedƒd¡G dd„ dejƒƒZ	e
dkrTe ¡  dS )	z"Tests for MuJoCo Python rendering.é    )ÚabsltestNÚ	GLContextzMuJoCo rendering is disabledc                       s@   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Zd	d
„ Z‡  ZS )ÚMuJoCoRenderTestc                    s&   t ƒ  ¡  t dd¡| _| j ¡  d S )Né€  éà  )ÚsuperÚsetUpÚmujocor   ÚglZmake_current©Úself©Ú	__class__© úR/home/ghrups/robot-bench/.venv/lib64/python3.9/site-packages/mujoco/render_test.pyr      s    
zMuJoCoRenderTest.setUpc                    s   t ƒ  ¡  | `d S )N)r   ÚtearDownr
   r   r   r   r   r       s    
zMuJoCoRenderTest.tearDownc              	   C   sN  t j d¡| _t  | j¡| _t j| jdd}t  | j| jt  ¡ t  	¡ t  
¡ t jj|¡ t  | jt jj¡}t  t jj|¡ t  dddd¡}t  |dddd¡ t  ddd	d
¡}t  |dddd¡ tjdtjd}d|dd…dd…df< tjdtjd}t  |d||¡ tj ||¡ d|dd…< t  t |d¡d||¡ tj ||¡ | ¡  dS )aÿ  Test that the bindings can successfully render a simple image.

    This test sets up a basic MuJoCo rendering context similar to the example in
    https://mujoco.readthedocs.io/en/latest/programming#visualization
    It calls `mjr_rectangle` rather than `mjr_render` so that we can assert an
    exact rendered image without needing golden data. The purpose of this test
    is to ensure that the bindings can correctly return pixels in Python, rather
    than to test MuJoCo's rendering pipeline itself.
    ú<mujoco><worldbody/></mujoco>r   ©Zmaxgeomr   r   é   é8   éC   éê   é{   )r   r   é   )Zdtypeéÿ   é¾   i"  é   Néÿÿÿÿ)r	   ÚMjModelÚfrom_xml_stringÚmodelÚMjDataÚdataÚMjvSceneÚmjv_updateSceneÚ	MjvOptionZ
MjvPerturbÚ	MjvCameraÚ	mjtCatBitÚ	mjCAT_ALLÚ
MjrContextÚmjtFontScaleÚmjFONTSCALE_150Úmjr_setBufferÚmjtFramebufferÚmjFB_OFFSCREENÚMjrRectZmjr_rectangleÚnpZzerosZuint8ÚemptyZmjr_readPixelsZtestingZassert_array_equalZreshapeÚfree)r   ÚsceneÚcontextZ	full_rectZ	blue_rectZexpected_upside_down_imageZupside_down_imager   r   r   Útest_can_render$   s:    ù
ÿz MuJoCoRenderTest.test_can_renderc              	   C   sˆ   t j d¡| _t  | j¡| _t j| jdd}t  | j| jt  ¡ d t  	¡ t j
j|¡ t  | jt jj¡}t  t jj|¡ | ¡  | ¡  d S )Nr   r   r   )r	   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r2   )r   r3   r4   r   r   r   Útest_safe_to_free_context_twiceV   s     ù
z0MuJoCoRenderTest.test_safe_to_free_context_twicec                 C   s4   t  dddd¡}t|ƒ}|  d|¡ |  d|¡ d S )Nr   r   r   é   r/   zleft: 1)r	   r/   ÚreprZassertIn)r   ÚrectZ	rect_reprr   r   r   Útest_mjrrect_reprk   s    z"MuJoCoRenderTest.test_mjrrect_repr)	Ú__name__Ú
__module__Ú__qualname__r   r   r5   r6   r:   Ú__classcell__r   r   r   r   r      s
   2r   Ú__main__)Ú__doc__Zabsl.testingr   r	   Únumpyr0   Z
skipUnlessÚhasattrZTestCaser   r;   Úmainr   r   r   r   Ú<module>   s   
ÿY