U
    h                     @   sh   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 d	d
 Z
dddZdddZdS )a@  
functions about rendering mesh(from 3d obj to 2d image).
only use rasterization render here.
Note that:
1. Generally, render func includes camera, light, raterize. Here no camera and light(I write these in other files)
2. Generally, the input vertices are normalized to [-1,1] and cetered on [0, 0]. (in world space)
   Here, the vertices are using image coords, which centers on [w/2, h/2] with the y-axis pointing to oppisite direction.
 Means: render here only conducts interpolation.(I just want to make the input flexible)

Author: Yao Feng 
Mail: yaofeng1995@gmail.com
    )absolute_import)division)print_functionN)time   )mesh_core_cythonc                 C   s   t ||gd }t j||gt jdd }t j||dgt jd}| t j } |t j }t| ||||| jd |jd ||	 dS )a   
    Args:
        vertices: [nver, 3]
        triangles: [ntri, 3]
        h: height
        w: width
    Returns:
        depth_buffer: [h, w] saves the depth, here, the bigger the z, the fronter the point.
        triangle_buffer: [h, w] saves the tri id(-1 for no triangle). 
        barycentric_weight: [h, w, 3] saves corresponding barycentric weight.

    # Each triangle has 3 vertices & Each vertex has 3 coordinates x, y, z.
    # h, w is the size of rendering
        ~.Adtyper      r   N)	npzerosint32float32astypecopyr   Zrasterize_triangles_coreshape)vertices	triangleshwdepth_bufferZtriangle_bufferZbarycentric_weight r   M/tmp/pip-unpacked-wheel-5oclok7i/insightface/thirdparty/face3d/mesh/render.pyrasterize_triangles   s          r   r   c           
      C   s   |dkr t j|||ft jd}n2|jd |krJ|jd |krJ|jd |ksNt|}t j||gt jddd }| t j } |t j }|t j }t }	t	
|| |||| jd |jd |||
 |S )	a   render mesh with colors
    Args:
        vertices: [nver, 3]
        triangles: [ntri, 3] 
        colors: [nver, 3]
        h: height
        w: width  
        c: channel
        BG: background image
    Returns:
        image: [h, w, c]. rendered image./rendering.
    Nr	   r   r      Cr
   orderr   )r   r   r   r   AssertionErrorr   r   r   r   r   Zrender_colors_core)
r   r   colorsr   r   cBGimager   str   r   r   render_colors4   s,    .     r%   nearestc
                 C   s4  |	dkr t j|||ft jd}
n2|	jd |krJ|	jd |krJ|	jd |ksNt|	}
t j||gt jddd }|j\}}}|d	krtd}n|d
krtd}ntd}| t j } |t j }|t j }|t j }|t j }t	
|
| |||||| jd |jd |jd ||||||| |
S )aK   render mesh with texture map
    Args:
        vertices: [3, nver]
        triangles: [3, ntri]
        texture: [tex_h, tex_w, 3]
        tex_coords: [ntexcoords, 3]
        tex_triangles: [ntri, 3]
        h: height of rendering
        w: width of rendering
        c: channel
        mapping_type: 'bilinear' or 'nearest'
    Nr	   r   r   r   r   r   r   r&   Zbilinear)r   r   r   r   r   intr   r   r   r   Zrender_texture_core)r   r   ZtextureZ
tex_coordsZtex_trianglesr   r   r!   Zmapping_typer"   r#   r   Ztex_hZtex_wZtex_cmtr   r   r   render_textureY   sH    .

          r)   )r   N)r   r&   N)__doc__
__future__r   r   r   Znumpyr   r   Zcythonr   r   r%   r)   r   r   r   r   <module>   s   
%