U
    ‰±ËhD  ã                   @   sd   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	„ Z	d
d„ Z
ddd„Zddd„ZdS )z¦
Functions about lighting mesh(changing colors/texture of mesh).
1. add light to colors/texture (shade each vertex)
2. fit light according to colors/texture & image.
é    )Úabsolute_import)Údivision)Úprint_functionNé   )Úmesh_core_cythonc           	      C   sú   | |dd…df dd…f }| |dd…df dd…f }| |dd…df dd…f }t  || || ¡}t j| t jd ¡ }t || t j¡ ¡ | ¡ |jd ¡ t  	|d d¡}|dk}d||< t  
t  	|¡¡||df< |t  |dd…t jf ¡ }|S )z™ calculate normal direction in each vertex
    Args:
        vertices: [nver, 3]
        triangles: [ntri, 3]
    Returns:
        normal: [nver, 3]
    Nr   r   é   )Zdtype)ÚnpZcrossÚ
zeros_likeZfloat32Úcopyr   Zget_normal_coreÚastypeÚshapeÚsumÚonesÚsqrtÚnewaxis)	ÚverticesÚ	trianglesZpt0Zpt1Zpt2Z
tri_normalÚnormalZmagZzero_ind© r   úL/tmp/pip-unpacked-wheel-5oclok7i/insightface/thirdparty/face3d/mesh/light.pyÚ
get_normal   s    &r   c           	      C   s  | j d |j d kst‚| j d }t| |ƒ}t t |¡tdd…df tdd…df tdd…df tdd…df tdd…df  tdd…df tdd…df  tdd…df tdd…df  tdd…df d tdd…df d  dtdd…df d  d f	¡}| |¡}|| }|S )a³   
    In 3d face, usually assume:
    1. The surface of face is Lambertian(reflect only the low frequencies of lighting)
    2. Lighting can be an arbitrary combination of point sources
    --> can be expressed in terms of spherical harmonics(omit the lighting coefficients)
    I = albedo * (sh(n) x sh_coeff)
    
    albedo: n x 1
    sh_coeff: 9 x 1
    Y(n) = (1, n_x, n_y, n_z, n_xn_y, n_xn_z, n_yn_z, n_x^2 - n_y^2, 3n_z^2 - 1)': n x 9 
    # Y(n) = (1, n_x, n_y, n_z)': n x 4

    Args:
        vertices: [nver, 3]
        triangles: [ntri, 3]
        colors: [nver, 3] albedo
        sh_coeff: [9, 1] spherical harmonics coefficients

    Returns:
        lit_colors: [nver, 3]
    r   Nr   r   é   )r   ÚAssertionErrorr   r   Úarrayr   ÚnÚdot)	r   r   ÚcolorsZsh_coeffÚnverr   ÚshÚrefÚ
lit_colorsr   r   r   Úadd_light_sh-   s    

Ö
r!   c                 C   s  | j d }t| |ƒ}| tjdd…dd…f |dd…tjdd…f  }t tj|d dd¡}||dd…dd…tjf  }|tjdd…dd…f | }	tj|	dd}	|tjdd…dd…f |	dd…dd…tjf  |dd…tjdd…f  }
tj|
dd}
|
}t t |d¡d¡}|S )a	   Gouraud shading. add point lights.
    In 3d face, usually assume:
    1. The surface of face is Lambertian(reflect only the low frequencies of lighting)
    2. Lighting can be an arbitrary combination of point sources
    3. No specular (unless skin is oil, 23333)

    Ref: https://cs184.eecs.berkeley.edu/lecture/pipeline    
    Args:
        vertices: [nver, 3]
        triangles: [ntri, 3]
        light_positions: [nlight, 3] 
        light_intensities: [nlight, 3]
    Returns:
        lit_colors: [nver, 3]
    r   Nr   )Zaxisr   )r   r   r   r   r   r   ÚminimumÚmaximum)r   r   r   Zlight_positionsZlight_intensitiesr   ZnormalsZdirection_to_lightsZdirection_to_lights_nZnormals_dot_lightsZdiffuse_outputr    r   r   r   Ú	add_lightL   s    

0Hr$   é
   r   c           "      C   s(  | j \}}}	t||ƒ}
|j d }|d d…d d …f }t t |dd d …f d¡|d ¡|dd d …f< t t |dd d …f d¡|d ¡|dd d …f< t |¡ tj¡}| |dd d …f |dd d …f d d …f }|j}d}|
dd d …f }|
dd d …f }|
dd d …f }t 	||f¡}tj
}t dd|  ¡t |f¡ |d d …df< t dd|  ¡| |d d …df< t dd|  ¡| |d d …df< t dd|  ¡| |d d …df< dt dd|  ¡ d|d  |d  |d   |d d …df< dt dd	|  ¡ ||  |d d …df< dt dd	|  ¡ ||  |d d …d
f< dt dd	|  ¡ ||  |d d …df< dt dd	|  ¡ || ||   |d d …df< t|ƒ}||	 }t 	|df¡}t 	|df¡}t 	d¡}t|	ƒD ]Ò}|||f d d …tjf ||| |d | …d d …f< t||f d d …tjf ||d d …f  ||| |d | …d d …f< t||f d d …tjf }|||f d d …tjf }|j |¡|j |¡ ||< qÚt|ƒD ]ú}| ¡ }t|	ƒD ]2}||| |d | …d d …f  ||   < qÊt |j|¡|t |¡  }t |j|¡}t tj |¡|¡}t|	ƒD ]h}||| |d | …d d …f  |¡}||| |d | …d d …f }|j |¡|j |¡ ||< qDq¶t t¡} t|	ƒD ]J}t |t|d d …f d d …tjf  |||  ¡}!|!j| |d d …f< qÄt t | d¡d¡} | S )Nr   r   r   é	   é   r   g      à?é   é   é   é   g      ø?é   )r   r   )r   r   r   r"   r#   Úroundr   Zint32ÚTÚzerosÚpir   r   ÚlenÚranger   Ztexturer   r
   ZeyeZlinalgÚinvr	   )"Úimager   r   r   Zvis_indZlambZmax_iterÚhÚwÚcZnormr   Zpt2dZimage_pixelZharmonic_dimZnxÚnyZnzZharmonicr0   Z	n_vis_indr   ÚYÚAÚlightÚkZAcZYcÚiZequation_leftZequation_rightÚalphaZ
appearanceÚtmpr   r   r   Ú	fit_lighty   sh    

00**""">***2
6F 0& $
2r@   )r   r   )r%   r   )Ú__doc__Ú
__future__r   r   r   Znumpyr   Zcythonr   r   r!   r$   r@   r   r   r   r   Ú<module>   s   
-