U
    ‰±ËhJ  ã                   @   s„   d dl mZ d dl mZ d dl mZ d dlZd dlZd dlmZ d dl	m	Z	 ddl
mZ d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    )Úabsolute_import)Údivision)Úprint_functionN)Úio)Útimeé   )Úmesh_core_cythonc                 C   s   dS )z read mesh
	r   © )Úobj_namer	   r	   úI/tmp/pip-unpacked-wheel-5oclok7i/insightface/thirdparty/face3d/mesh/io.pyÚread_obj   s    r   c                 C   s4   |   d¡d dkr t | |¡ nt | d |¡ dS )z3
    Args:
        vertices: shape = (nver, 3)
    Ú.éÿÿÿÿZascz.ascN)ÚsplitÚnpZsavetxt)ÚpathÚverticesr	   r	   r   Ú	write_asc   s    r   c           	      C   sô   |  ¡ }|d7 }|  d¡d dkr*| d } t| dƒ¶}t|jd ƒD ]R}d ||df ||df ||d	f ||df ||df ||d	f ¡}| |¡ qD|j\}}t|jd ƒD ]4}d
 ||d	f ||df ||df ¡}| |¡ q°W 5 Q R X dS )zÇ Save 3D face model with texture represented by colors.
    Args:
        obj_name: str
        vertices: shape = (nver, 3)
        triangles: shape = (ntri, 3)
        colors: shape = (nver, 3)
    r   r   r   Úobjú.objÚwr   zv {} {} {} {} {} {}
é   zf {} {} {}
N)Úcopyr   ÚopenÚrangeÚshapeÚformatÚwrite)	r
   r   Ú	trianglesÚcolorsÚfÚiÚsÚkZntrir	   r	   r   Úwrite_obj_with_colors   s    D
&r$   c           
      C   s°  |   d¡d dkr| d } |  dd¡}|  dd¡}| ¡ }|d7 }t| dƒ}d	 tj |¡¡}| |¡ t	|j
d
 ƒD ]4}	d ||	d
f ||	df ||	df ¡}| |¡ qzt	|j
d
 ƒD ].}	d ||	d
f d||	df  ¡}| |¡ q¾| d¡ t	|j
d
 ƒD ]T}	d ||	df ||	df ||	df ||	df ||	d
f ||	d
f ¡}| |¡ qW 5 Q R X t|dƒ,}| d¡ d tj |¡¡}| |¡ W 5 Q R X t||ƒ dS )au   Save 3D face model with texture represented by texture map.
    Ref: https://github.com/patrikhuber/eos/blob/bd00155ebae4b1a13b08bf5a991694d682abbada/include/eos/core/Mesh.hpp
    Args:
        obj_name: str
        vertices: shape = (nver, 3)
        triangles: shape = (ntri, 3)
        texture: shape = (256,256,3)
        uv_coords: shape = (nver, 3) max value<=1
    r   r   r   r   ú.mtlú_texture.pngr   r   z
mtllib {}
r   zv {} {} {}
r   z	vt {} {}
zusemtl FaceTexture
zf {}/{} {}/{} {}/{}
únewmtl FaceTexture
ú
map_Kd {}
N)r   Úreplacer   r   r   Úosr   Úabspathr   r   r   Úimsave)
r
   r   r   ÚtextureÚ	uv_coordsÚmtl_nameÚtexture_namer    r"   r!   r	   r	   r   Úwrite_obj_with_texture=   s0    

& 
D
r1   c           
      C   s  |   d¡d dkr| d } |  dd¡}|  dd¡}| ¡ }|d7 }| tj¡ ¡ | tj¡ ¡ | tj¡ ¡   }}}t t 	| ¡t 	t
j |¡¡|||||jd |jd |jd ¡	 t|d	ƒ,}| d
¡ d t
j |¡¡}	| |	¡ W 5 Q R X t ||¡ dS )a}   Save 3D face model with texture. 
    Ref: https://github.com/patrikhuber/eos/blob/bd00155ebae4b1a13b08bf5a991694d682abbada/include/eos/core/Mesh.hpp
    Args:
        obj_name: str
        vertices: shape = (nver, 3)
        triangles: shape = (ntri, 3)
        colors: shape = (nver, 3)
        texture: shape = (256,256,3)
        uv_coords: shape = (nver, 3) max value<=1
    r   r   r   r   r%   r&   r   r   r   r'   r(   N)r   r)   r   Zastyper   Zfloat32r   Z"write_obj_with_colors_texture_coreÚstrÚencoder*   r   r+   r   r   r   r   r   r,   )
r
   r   r   r   r-   r.   r/   r0   r    r"   r	   r	   r   Úwrite_obj_with_colors_texturep   s    4@
r4   )Ú
__future__r   r   r   Znumpyr   r*   Zskimager   r   Zcythonr   r   r   r$   r1   r4   r	   r	   r	   r   Ú<module>   s   
3