
    h(                     p    S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  S rS r	S	 r
SS
 jrSS jrSS jrg)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)

Preparation knowledge:
z-buffer: https://cs184.eecs.berkeley.edu/lecture/pipeline

Author: Yao Feng 
Mail: yaofeng1995@gmail.com
    )absolute_import)division)print_functionN)timec                 @   UnUSSS24   USSS24   -
  nUSSS24   USSS24   -
  nXSSS24   -
  n[         R                  " UR                  U5      n[         R                  " UR                  U5      n[         R                  " UR                  U5      n[         R                  " UR                  U5      n	[         R                  " UR                  U5      n
Xi-  Xw-  -
  S:X  a  SnOSXi-  Xw-  -
  -  nX-  Xz-  -
  U-  nXj-  Xx-  -
  U-  nUS:  US:  -  X-   S:  -  $ )a  Judge whether the point is in the triangle
Method:
    http://blackpawn.com/texts/pointinpoly/
Args:
    point: (2,). [u, v] or [x, y] 
    tri_points: (3 vertices, 2 coords). three vertices(2d points) of a triangle. 
Returns:
    bool: true for in triangle
   Nr      npdotT)point
tri_pointstpv0v1v2dot00dot01dot02dot11dot12	inverDenouvs                 i/var/www/fran/franai/venv/lib/python3.13/site-packages/insightface/thirdparty/face3d/mesh_numpy/render.pyisPointInTrir      s%    
B 
AaC2ac7	B	AaC2ac7	B	AaCB FF244EFF244EFF244EFF244EFF244E {U[ A%	u{U[01		u{	"I-A	u{	"I-A FqAv!%!),,    c                 :   UnUSSS24   USSS24   -
  nUSSS24   USSS24   -
  nXSSS24   -
  n[         R                  " UR                  U5      n[         R                  " UR                  U5      n[         R                  " UR                  U5      n[         R                  " UR                  U5      n	[         R                  " UR                  U5      n
Xi-  Xw-  -
  S:X  a  SnOSXi-  Xw-  -
  -  nX-  Xz-  -
  U-  nXj-  Xx-  -
  U-  nSU-
  U-
  nUnUnXU4$ )a  Get the weights of the position
Methods: https://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates
 -m1.compute the area of the triangles formed by embedding the point P inside the triangle
 -m2.Christer Ericson's book "Real-Time Collision Detection". faster.(used)
Args:
    point: (2,). [u, v] or [x, y] 
    tri_points: (3 vertices, 2 coords). three vertices(2d points) of a triangle. 
Returns:
    w0: weight of v0
    w1: weight of v1
    w2: weight of v3
 r   Nr   r	   r
   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   w0w1w2s                    r   get_point_weightr#   ;   s$    
B	AaC2ac7	B	AaC2ac7	B	AaCB FF244EFF244EFF244EFF244EFF244E {U[ A%	u{U[01		u{	"I-A	u{	"I-A	
QB	
B	
B2:r   c                    [         R                  " X#/5      S-
  n[         R                  " X#/[         R                  S9S-
  n[         R                  " X#S/[         R                  S9n[	        UR
                  S   5       GH  nXSS24   n[        [        [         R                  " [         R                  " XS4   5      5      5      S5      n	[        [        [         R                  " [         R                  " XS4   5      5      5      US-
  5      n
[        [        [         R                  " [         R                  " XS4   5      5      5      S5      n[        [        [         R                  " [         R                  " XS4   5      5      5      US-
  5      nX:  d  X:  a  GM#  [	        XS-   5       H  n[	        XS-   5       H  n[        X/XSS24   5      (       d  M  [        X/XSS24   5      u  nnnXUS   S4   -  UXS   S4   -  -   UXS   S4   -  -   nUXNU4   :  d  Mf  UXNU4'   XuX4'   [         R                  " UUU/5      XnUSS24'   M     M     GM     XEU4$ )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   Nr   )r   zerosint32float32rangeshapemaxintceilminfloorr   r#   array)vertices	triangleshwdepth_buffertriangle_bufferbarycentric_weightitriuminumaxvminvmaxr   r   r    r!   r"   point_depths                      r   rasterize_trianglesrA   d   s&     88QF#g-Lhhvrxx81<O1)RZZ@9??1%&1o 3rwwrvvhAv&67891=3rxxxQ'7 89:AaC@3rwwrvvhAv&67891=3rxxxQ'7 89:AaC@9	t!V$A4a(#QE8!G+<==-qfhBQBw6GH
B #a&!)!44r(q619:M7MMPRS[`a\bde\eSfPffd!33)4LA&,-AD)24((BB<2H&!Qw/ ) % '0 *<<<r   c                    U R                   S   UR                   S   :X  d   e[        XX45      u  pgn[        R                  " US/5      n	[        R                  " USU/5      n
U
SS2SS2[        R                  4   nX!U	SS24   SS24   nX-  n[        R
                  " US5      n[        R                  " XXE/5      nU$ )zrender mesh with colors(rasterize triangle first)
Args:
    vertices: [nver, 3]
    triangles: [ntri, 3] 
    colors: [nver, 3]
    h: height
    w: width    
    c: channel
Returns:
    image: [h, w, c]. rendering.
r   Nr	   )r,   rA   r   reshapenewaxissum)r3   r4   colorsr5   r6   cr7   r8   r9   triangle_buffer_flatbarycentric_weight_flatweightcolors_flatimages                 r   render_colors_rasrN      s     >>!Q///8KHab8f5L#5::ot< jj);b!WE$Q2::%56F#7#:;Q>?K$K&&a(KJJ{I.E Lr   c                    U R                   S   UR                   S   :X  d   e[        R                  " X4U45      n[        R                  " X4/5      S-
  n[        UR                   S   5       GH  nXSS24   n	[	        [        [        R                  " [        R                  " X	S4   5      5      5      S5      n
[        [        [        R                  " [        R                  " X	S4   5      5      5      US-
  5      n[	        [        [        R                  " [        R                  " X	S4   5      5      5      S5      n[        [        [        R                  " [        R                  " X	S4   5      5      5      US-
  5      nX:  d  X:  a  GM#  [        XS-   5       H  n[        XS-   5       H  n[        X/X	SS24   5      (       d  M  [        X/X	SS24   5      u  nnnUX	S   S4   -  UX	S   S4   -  -   UX	S   S4   -  -   nUXU4   :  d  Mf  UXU4'   UX)S   SS24   -  UX)S   SS24   -  -   UX)S   SS24   -  -   XoUSS24'   M     M     GM     U$ )zrender mesh with colors
Args:
    vertices: [nver, 3]
    triangles: [ntri, 3] 
    colors: [nver, 3]
    h: height
    w: width    
Returns:
    image: [h, w, c]. 
r   r%   Nr	   r   )r,   r   r(   r+   r-   r.   r/   r0   r1   r   r#   )r3   r4   rG   r5   r6   rH   rM   r7   r:   r;   r<   r=   r>   r?   r   r   r    r!   r"   r@   s                       r   render_colorsrP      sC    >>!Q/// HHaAYE88QF#g-L9??1%&1o 3rwwrvvhAv&67891=3rxxxQ'7 89:AaC@3rwwrvvhAv&67891=3rxxxQ'7 89:AaC@9	t!V$A4a(#QE8!G+<==-qfhBQBw6GH
B a&!)!44r(q619:M7MMPRS[`a\bde\eSfPffd!33)4LA&%'1vqy(9%9Bv!fai?P<P%PSUV\ab]cef]fVgSg%gEQ'N ) % '. Lr   c	                 z	   UR                   S   UR                   S   :X  d   eUR                   u  pn[        R                  " XVU45      n[        R                  " XV/5      S-
  n[        UR                   S   5       GH:  nXSS24   nXNSS24   n[	        [        [        R                  " [        R                  " XS4   5      5      5      S5      n[        [        [        R                  " [        R                  " XS4   5      5      5      US-
  5      n[	        [        [        R                  " [        R                  " XS4   5      5      5      S5      n[        [        [        R                  " [        R                  " XS4   5      5      5      US-
  5      nUU:  d  UU:  a  GM-  [        UUS-   5       GH  n[        UUS-   5       GH  n[        UU/XSS24   5      (       d  M  [        UU/XSS24   5      u  nnnUXS   S4   -  UXS   S4   -  -   UXS   S4   -  -   nUUUU4   :  d  Mj  UUUU4'   UUUS   SS24   -  UUUS   SS24   -  -   UUUS   SS24   -  -   n[	        [        US   [        U
S-
  5      5      S5      US'   [	        [        US   [        U	S-
  5      5      S5      US'   US:X  aE  [        R                  " U5      R                  [        R                  5      nUUS   US   SS24   nGOUS:X  Ga  U[        [        R                  " US   5      5      [        [        R                  " US   5      5      SS24   nU[        [        R                  " US   5      5      [        [        R                  " US   5      5      SS24   nU[        [        R                  " US   5      5      [        [        R                  " US   5      5      SS24   nU[        [        R                  " US   5      5      [        [        R                  " US   5      5      SS24   n US   [        R                  " US   5      -
  n!US   [        R                  " US   5      -
  n"USU"-
  -  SU!-
  -  UU"-  SU!-
  -  -   USU"-
  -  U!-  -   U U"-  U!-  -   nWUUUSS24'   GM     GM     GM=     U$ )	a  render mesh with texture map
Args:
    vertices: [nver], 3
    triangles: [ntri, 3]
    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'
r   r%   Nr	   r   g        nearestbilinear)r,   r   r(   r+   r-   r.   r/   r0   r1   r   r#   floatroundastyper)   )#r3   r4   texture
tex_coordstex_trianglesr5   r6   rH   mapping_typetex_htex_w_rM   r7   r:   r;   tex_trir<   r=   r>   r?   r   r   r    r!   r"   r@   tex_xy	tex_valueulurdldrydxds#                                      r   render_texturerg      sq    ??1!4!4Q!7777mmOE! HHaAYE88QF#g-L9??1%&1o1% 3rwwrvvhAv&67891=3rxxxQ'7 89:AaC@3rwwrvvhAv&67891=3rxxxQ'7 89:AaC@9T	tT!V$A4a(#QqE8!G+<==-q!fhBQBw6GH
B a&!)!44r(q619:M7MMPRS[`a\bde\eSfPffad!33)4LA&  
71:q= 99Bz'RS*VW-?X<XX[]^hipqrisuviv^w[wwF #Cq	53C$Dc JF1I #Cq	53C$Dc JF1I $y0!#&!1!8!8!B$+F1Ivay!,C$D	 &3$S&))<%=s288FSTICV?WYZ%Z[$S&))<%=s2776RS9CU?VXY%YZ$S);%<c"((6RS9BU>VXY%YZ$S);%<c"''&QR)BT>UWX%XY#AY&))<<#AY&))<<$&"Iqt$4r"uad|$Cb!B$iPRl$RUWXZUZ[]U]$]	%.E!Q'N? ) % '^ Lr   )r'   )r'   rR   )__doc__
__future__r   r   r   numpyr   r   r   r#   rA   rN   rP   rg    r   r   <module>rl      s>    '  %  "-H'R,=^<(VCr   