
    h                     f    S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJ	r	  S r
S rS	 rS
 rS rg)zio: 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                     g)zread mesh
        r    )obj_names    e/var/www/fran/franai/venv/lib/python3.13/site-packages/insightface/thirdparty/face3d/mesh_numpy/io.pyread_objr      s     	
    c                     U R                  S5      S   S:X  a  [        R                  " X5        g[        R                  " U S-   U5        g)z'
Args:
    vertices: shape = (nver, 3)
.ascz.ascN)splitnpsavetxt)pathverticess     r
   	write_ascr      s7    
 zz#re#


4"


4&=(+r   c                 *   UR                  5       nUS-  nU R                  S5      S   S:w  a  U S-   n [        U S5       n[        UR                  S   5       HB  nSR                  XS4   XS4   XS	4   X5S4   X5S4   X5S	4   5      nUR                  U5        MD     UR                  u  px[        UR                  S   5       H3  nS
R                  X%S	4   X%S4   X%S4   5      nUR                  U5        M5     SSS5        g! , (       d  f       g= f)zSave 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kntris	            r
   write_obj_with_colorsr+   $   s/     INI~~c2%'f$ 
h	 x~~a()A'..x1~x1~x[\X\~_eijfj_kmswxtxmy  |B  FG  CG  |H  IAGGAJ * OO	yq)*A%%i1oyA	UVRVXAGGAJ + 
		s   B<D
Dc                    U R                  S5      S   S:w  a  U S-   n U R                  SS5      nU R                  SS5      nUR                  5       nUS-  n[        U S5       nS	R	                  [
        R                  R                  U5      5      nUR                  U5        [        UR                  S
   5       H3  n	SR	                  XS
4   XS4   XS4   5      nUR                  U5        M5     [        UR                  S
   5       H.  n	SR	                  XIS
4   XIS4   5      nUR                  U5        M0     UR                  S5        [        UR                  S
   5       HB  n	SR	                  X)S4   X)S4   X)S4   X)S4   X)S
4   X)S
4   5      nUR                  U5        MD     SSS5        [        US5       nUR                  S5        SR	                  [
        R                  R                  U5      5      nUR                  U5        SSS5        [        Xc5        g! , (       d  f       N= f! , (       d  f       N+= f)aT  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 {}/{} {}/{} {}/{}
Nnewmtl FaceTexture

map_Kd {}
)r   replacer   r   r"   osr   abspathr#   r    r!   imsave)
r	   r   r$   texture	uv_coordsmtl_nametexture_namer&   r(   r'   s
             r
   write_obj_with_texturer=   C   s    ~~c2%'f$/H##FN;L INI 
h	  !:;	
 x~~a()A%%h!tnh!tnhRStnUAGGAJ *
 yq)*A##IcNIcNCAGGAJ +
 	
&' yq)*A'..y1~y1~y[\Y\~_hklil_mox{|y|o}  @I  LM  JM  @N  OAGGAJ +' 
0 
h		&'  !>?	
 
 <!= 
	0 
	s   #EH$?AH5$
H25
Ic                    U R                  S5      S   S:w  a  U S-   n U R                  SS5      nU R                  SS5      nUR                  5       nUS-  n[        U S5       nS	R	                  [
        R                  R                  U5      5      n	UR                  U	5        [        UR                  S
   5       HB  n
SR	                  XS
4   XS4   XS4   X:S
4   X:S4   X:S4   5      n	UR                  U	5        MD     [        UR                  S
   5       H.  n
SR	                  XZS
4   XZS4   5      n	UR                  U	5        M0     UR                  S5        [        UR                  S
   5       HB  n
SR	                  X*S4   X*S4   X*S4   X*S4   X*S
4   X*S
4   5      n	UR                  U	5        MD     SSS5        [        US5       nUR                  S5        SR	                  [
        R                  R                  U5      5      n	UR                  U	5        SSS5        [        R                  " Xt5        g! , (       d  f       N= f! , (       d  f       N6= f)aX  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   r0   r1   r2   Nr3   r4   )r   r5   r   r   r"   r6   r   r7   r#   r    r!   r   r8   )r	   r   r$   r%   r9   r:   r;   r<   r&   r(   r'   s              r
   write_obj_with_colors_texturer?   w   sZ    ~~c2%'f$/H##FN;L INI 
h	  !:;	
 x~~a()A'..x1~x1~x[\X\~_eijfj_kmswxtxmy  |B  FG  CG  |H  IAGGAJ *
 yq)*A##IcNIcNCAGGAJ +
 	
&' yq)*A'..y1~y1~y[\Y\~_hklil_mox{|y|o}  @I  LM  JM  @N  OAGGAJ +' 
2 
h		&'  !>?	
 
 IIl$? 
	2 
	s   #EH>AI>
I
I)__doc__
__future__r   r   r   numpyr   r6   skimager   r   r   r+   r=   r?   r   r   r
   <module>rD      s8    '  %  	 
,>1"h3%r   