U
    ‰±ËhÒ  ã                   @   sp   d Z 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„ Z
d	d
„ Zdd„ Zdd„ Zdd„ ZdS )z» io: read&write mesh
1. read obj as array(TODO)
2. write arrays to obj

Preparation knowledge:
representations of 3d face: mesh, point cloud...
storage format: obj, ply, bin, asc, mat...
é    )Úabsolute_import)Údivision)Úprint_functionN)Úioc                 C   s   dS )z read mesh
	r   © )Úobj_namer   r   úO/tmp/pip-unpacked-wheel-5oclok7i/insightface/thirdparty/face3d/mesh_numpy/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   Úobjú.objÚwr   ú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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   ú
mtllib {}
r   zv {} {} {}
r   ú	vt {} {}
úusemtl FaceTexture
ú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_textureC   s0    

&
D
r4   c                 C   sÌ  |   d¡d dkr| d } |  dd¡}|  dd¡}| ¡ }|d7 }t| dƒ,}d	 tj |¡¡}	| |	¡ t	|j
d
 ƒD ]R}
d ||
d
f ||
df ||
df ||
d
f ||
df ||
df ¡}	| |	¡ qzt	|j
d
 ƒD ]*}
d ||
d
f ||
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 )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   r   r'   r(   r)   r*   r+   N)r   r,   r   r   r   r-   r   r.   r   r   r   r   r/   )r   r   r   r   r0   r1   r2   r3   r   r!   r    r   r   r   Úwrite_obj_with_colors_texturew   s0    
D
D
r5   )Ú__doc__Ú
__future__r   r   r   Znumpyr   r-   Zskimager   r	   r   r#   r4   r5   r   r   r   r   Ú<module>   s   	
4