
    9iu1                        S SK r S SKrS SKrS SKJr  S SKrS SKJs  J	r
  SS jrS rSS jrS rS rS rS\R"                  S	\R"                  S
\R"                  4S jrS\R"                  S\R"                  S
\R"                  4S jrSS\R"                  S\S
\R"                  4S jjrSS\R"                  S\S
\R"                  4S jjrS rS rS rS rS rS rS rS\R"                  S
\R"                  4S jrg)    Nc           	      
   [        U S5       nUR                  5       nS S S 5        / n/ n/ n/ n/ n/ n	Sn
W GH  nUS S S:X  a]  UR                  5       R                  S5      SS   Vs/ s H  n[	        U5      S:  d  M  [        U5      PM!     nnUR                  U5        US S S:X  Ga  UR                  5       R                  S5      SS  nU Vs/ s H1  n[	        U5      S:  d  M  [        UR                  S5      S   5      PM3     nn[        U
[	        U5      5      n
UR                  U5        SUS   ;   as  [	        US   R                  S5      S   5      S:  aO  U Vs/ s H1  n[	        U5      S:  d  M  [        UR                  S5      S   5      PM3     nnUR                  U5        SUS   ;   a  [	        US   R                  S5      5      S	:  as  [	        US   R                  S5      S   5      S:  aO  U Vs/ s H1  n[	        U5      S:  d  M  [        UR                  S5      S   5      PM3     nnU	R                  U5        US S	 S
:X  a_  UR                  5       R                  S5      SS  nU Vs/ s H  n[	        U5      S:  d  M  [        U5      PM!     nnUR                  U5        US S	 S:X  d  GM  UR                  5       R                  S5      SS  nU Vs/ s H  n[	        U5      S:  d  M  [        U5      PM!     nnUR                  U5        GM     [        R                  " U5      R                  [        R                  5      nU
S	::  a4  [        R                  " U5      R                  [        R                  5      nO[        S5        UR                  S   S	:X  a  UUS.nOUS S 2S S	24   US S 2S	S 24   US.n[	        U5      S:  a8  [        R                  " U5      R                  [        R                  5      nUUS'   [	        U5      S:  a8  [        R                  " U5      R                  [        R                  5      nUUS'   [	        U5      S:  a>  U
S	::  a3  [        R                  " U5      R                  [        R                  5      nUUS'   [	        U	5      S:  a>  U
S	::  a3  [        R                  " U	5      R                  [        R                  5      n	U	US'   U(       a*  [        S[	        U5      5        [        S[	        U5      5        U$ ! , (       d  f       GN= fs  snf s  snf s  snf s  snf s  snf s  snf )Nrr      zv     f /   zvt zvn znot a triangle face mesh!)verticesfaces)r   colorsr   uvsnormalsfaces_uvfaces_normalznum of verticesznum of faces)open	readlinesstripsplitlenfloatappendintmaxnparrayastypefloat32int32printshape)obj_pathprint_shapef	bfm_linesr   r   r   vnsr   r   max_face_lengthlineavertexitemsfaceface_uvface_normaluvvnmeshs                        l/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/cv/human3d_animation/utils.pyread_objr3      s   	h	KKM	 
 HE
C
CHLO8t"&**,"4"4S"9!""="=QQ!a"=   OOF#8tJJL&&s+AB/E27F%Q3q6A:(CQ(%DF!/3t9=OLLeAh3uQx~~c':1'=#>#B9>MA#a&1*/3qwws|A/M(eAh3uQx~~c':#;q#@S!HNN3'*F,./F0 3827Q3q6A:(CQ(%   ##K08uJJL&&s+AB/E$)8EqSVaZ(%(EB8JJrN8uJJL&&s+AB/E$)8EqSVaZ(%(EB8JJrNA D xx!((4H!&&rxx0)*~~aA 
 !BQBq!"uo
 3x!|hhsm""2::.U
3x!|hhsm""2::.Y
8}qaxx)00:H#Z
<1a88L188BL+^X/nc%j)Kg 
	 G
 N
 9
 9sS   T%'T7>T7T<'!T<!U8!U>U!U8UU#U:U%
T4c                    [         R                  R                  U 5      n[         R                  R                  [         R                  R	                  U 5      5      S   nSU;   Ga	  [
        R                  " [         R                  R                  X#S-   5      US   5        [        [         R                  R                  X#S-   5      S5       nUR                  S5        UR                  S5        UR                  S5        UR                  S	5        UR                  S
5        UR                  S5        UR                  S5        UR                  SR                  US-   5      5        S S S 5        [        U S5       nSU;   a1  UR                  S5        UR                  SR                  U5      5        SU;   aa  [        US   5       HN  u  pVUR                  SR                  US   US   US   US   U   S   US   U   S   US   U   S   5      5        MP     O7US    H.  nUR                  SR                  US   US   US   5      5        M0     SU;   a3  US    H*  nUR                  SR                  US   US   5      5        M,     SU;   a7  US    H.  nUR                  SR                  US   US   US   5      5        M0     SU;   a  [        US   5       H  u  pSU;   d  SU;   at  SU;   a	  US   U	   nOU
nSU;   a	  US   U	   nOU
nSSR                  [        [        U
5      5       Vs/ s H  nSR                  X   X   X   5      PM     sn5      -   S -   nOKSSR                  [        [        U
5      5       Vs/ s H  nS!R                  X   5      PM     sn5      -   S -   nUR                  U5        M     S S S 5        g ! , (       d  f       GNi= fs  snf s  snf ! , (       d  f       g = f)"Nr   texture_mapz.pngz.mtlwznewmtl material_0
zKa 1.000000 0.000000 0.000000
zKd 1.000000 1.000000 1.000000
zKs 0.000000 0.000000 0.000000
zTr 0.000000
zillum 0
zNs 0.000000
z
map_Kd {}
z# Create by ModelScope
zmtllib ./{}.mtl
r   r   zv {} {} {} {} {} {}
r   r   zv {} {} {}
r   z	vt {} {}
r   zvn {} {} {}
r   r   r   r   r   z{}/{}/{}
z{})ospathdirnamesplitextbasenamecv2imwritejoinr   writeformat	enumerateranger   )	save_pathr1   save_dir	save_namewfivr/   r0   indr,   r-   r.   rows                 r2   	write_objrL   c   s   wwy)H  !1!1)!<=a@IGGLLv#56]8K	M "'',,xV);<cBbHH*+HH67HH67HH67HH_%HH[!HH_%HH])))f*<=> C 
i	D HH/0HH(//	:;t!$z"23077aD!A$!d8nQ&7&:N1%a($x.*;A*>@ A 4
 *%..qtQqT1Q4@A & D=5k,,RUBqE:; " 9o//1r!ubeDE & d?&tG}5	%4)?!T)"&z"23"7"&%-&*>&:3&?&*!&s4y!1+!1A #))$'7:{~N!1+ "  C
 "'D	"24"2Q TW-"24"5 57;<C % 6/ 
	 CBV+4O 
	s8   BO	;F5O%0#O/O%O !O%	
O
O%%
O3c           
          [         R                  " X-  SSS/SX-  SS/SSX!-   * X!-
  -  SU-  U-  * X!-
  -  // SQ/5      R                  [         R                  5      $ )Nr   r   )r   r   r   r   r   r   r   )xnr$   s      r2   
projectionrR      sk    88aeQ1%15!Q'7aeH.!a%!)0FG"$ %%+VBJJ%78    c                     [         R                  " SSSU /SSSU/SSSU// SQ/5      R                  [         R                  5      $ )Nr   r   r   r   r   r   rO   )rP   yzs      r2   	translaterX      sG    88aAq\Aq!Q<!Q1!# $$*F2::$67rS   c                     [         R                  " U 5      [         R                  " U 5      p![         R                  " / SQSX!S/SU* US// SQ/5      R	                  [         R
                  5      $ )N)r   r   r   r   r   rU   r   sincosr   r   r   r)   scs      r2   rotate_xr`      sV    66!9bffQiq88\AqQ<!aRA!# $$*F2::$67rS   c                     [         R                  " U 5      [         R                  " U 5      p![         R                  " USUS// SQU* SUS// SQ/5      R	                  [         R
                  5      $ )Nr   )r   r   r   r   rU   rZ   r]   s      r2   rotate_yrb      sX    66!9bffQiq88aAq\<1"aA!# $$*F2::$67rS   rP   rV   returnc                 2    [         R                  " X-  SSS9$ )NrN   Tkeepdim)torchsum)rP   rV   s     r2   dotri      s    99QUB--rS   rQ   c                 *    S[        X5      -  U-  U -
  $ )Nr   )ri   )rP   rQ   s     r2   reflectrk      s    s1y=1q  rS   epsc                 f    [         R                  " [         R                  " [        X 5      US95      $ )N)min)rg   sqrtclampri   rP   rl   s     r2   lengthrr      s(    ::ekkA	  rS   c                     U [        X5      -  $ )N)rr   rq   s     r2   safe_normalizert      s    va~rS   c                 j   [        U [        R                  5      (       a$  [        R                  " U 5      R                  5       OU n[        R                  " U[        R                  " UR                  S   S/5      R                  5       /SS9n[        R                  " X2R                  5       5      S   $ )Nr   r   )axisN.)
isinstancer   ndarrayrg   
from_numpycudacatonesr!   matmult)mtxpost_mtxposws       r2   transform_posr      s    ,6s79zz-C -CES!&&(HK 
99c5::syy|Q&78==?@qID<<ggi(33rS   c
                    [        X5      n
[        R                  " X
X7U/S9u  pU(       a9  [        R                  " US   UUUSS9u  p[        R                  " US   UUSU	S9nO6[        R                  " US   X5      u  nn[        R                  " US   USS9nUR                  [        R                  5      nX#S S 2S	4   S S 24   nX#S S 2S
4   S S 24   nX#S S 2S4   S S 24   n[        [        R                  " UU-
  UU-
  5      5      n[        R                  " S	UR                  S	   [        R                  SS9S S 2S 4   R                  S
S5      n[        R                  " US   UUR                  5       5      u  nnUS
-   S-  n[        R                  " USSS 24   S	S
5      nUU-  S
U-
  [        R                   " U5      -  -   nUU-  S
U-
  [        R                   " U5      -  -   nUUU4$ )N)
resolutionrw   all)rast_db
diff_attrszlinear-mipmap-linear)filter_modemax_mip_levellinear)r   r   r   r   r{   )dtypedevicer
         ?.rN   )r   dr	rasterizeinterpolatetexturetyperg   longrt   crossaranger!   int64repeatr   rp   	ones_like)glctxr   r   pos_idxr/   uv_idxtexr   
enable_mipr   pos_cliprast_outrast_out_dbtexctexdcolor_v0v1v2face_normalsface_normal_indicesgb_geometric_normalnormalmasks                            r2   renderr      s   S&HLL*-EGH ^^yM
 

	N.') ..IAa

3y>4XFll5::&G	QT]A	B	QT]A	B	QT]A	B!%++b2grBw"?@L <<	<a $w !'1   ^^L,CX,?,C,C,EG!A%,F;;xRS)1a0DDLAH(>>>Ed]a$h%//&*AAAF$rS   c                 H    U S:  US:  :g  n[         R                  " X * U 5      $ )a  
Return a tensor where each element has the absolute value taken from the,
corresponding element of a, with sign taken from the corresponding
element of b. This is like the standard copysign floating-point operation,
but is not careful about negative 0 and NaN.

Args:
    a: source tensor.
    b: tensor whose signs will be used, of the same shape as a.

Returns:
    Tensor of the same shape as a with the signs of b.
r   )rg   where)r)   bsigns_differs      r2   	_copysignr      s)     Eq1u%L;;|R++rS   c                 p    [         R                  " U 5      nU S:  n[         R                  " X   5      X'   U$ )zO
Returns torch.sqrt(torch.max(0, x))
but with a zero subgradient where x is 0.
r   )rg   
zeros_likero   )rP   retpositive_masks      r2   _sqrt_positive_partr     s6    
 

1
CEMA$45CJrS   c                    U R                  S5      S:w  d  U R                  S5      S:w  a  [        SU R                   S35      eU S   nU S   nU S   nS	[        S
U-   U-   U-   5      -  nS	[        S
U-   U-
  U-
  5      -  nS	[        S
U-
  U-   U-
  5      -  nS	[        S
U-
  U-
  U-   5      -  n[	        XPS   U S   -
  5      n[	        X`S   U S   -
  5      n	[	        XpS   U S   -
  5      n
[
        R                  " XHX4S5      $ )z
Convert rotations given as rotation matrices to quaternions.

Args:
    matrix: Rotation matrices as tensor of shape (..., 3, 3).

Returns:
    quaternions with real part first, as tensor of shape (..., 4).
rN   r
   z Invalid rotation matrix  shape f.).r   r   ).r   r   ).r   r   r   r   ).r   r   ).r   r   ).r   r   ).r   r   ).r   r   ).r   r   )size
ValueErrorr!   r   r   rg   stack)matrixm00m11m22o0rP   rV   rW   o1o2o3s              r2   matrix_to_quaternionr     s/    {{2!v{{2!3;FLL>KLL

C

C

C	"1s7S=3#67	7B!!c'C-#"566A!!c'C-#"566A!!c'C-#"566A	1Y'&*;;	<B	1Y'&*;;	<B	1Y'&*;;	<B;;',,rS   c                 V   [         R                  " U SSS24   SSSS9n[         R                  " XSSS24   5      nSU-  nSnUR                  5       U:  n[         R                  " U5      n[         R
                  " X%)    5      X5)    -  Xe) '   S	X5   X5   -  S
-  -
  Xe'   U SSS24   U-  $ )ak  
Convert rotations given as quaternions to axis/angle.

Args:
    quaternions: quaternions with real part first,
        as tensor of shape (..., 4).

Returns:
    Rotations given as a vector in axis angle form, as a tensor
        of shape (..., 3), where the magnitude is the angle
        turned anticlockwise in radians around the vector's
        direction.
.r   Nr   rN   T)pdimrf   gư>r   0   )rg   normatan2abs
empty_liker[   )quaternionsnormshalf_anglesanglesrl   small_anglessin_half_angles_over_angless          r2   quaternion_to_axis_angler   5  s     JJ{37+qb$GE++ebqb%9:K_F
C::<#%L"'"2"26":		+m,-}0EE  .
 	v#f&::b@@  -sABw"===rS   c                 *    [        [        U 5      5      $ )aW  
Convert rotations given as rotation matrices to axis/angle.

Args:
    matrix: Rotation matrices as tensor of shape (..., 3, 3).

Returns:
    Rotations given as a vector in axis angle form, as a tensor
        of shape (..., 3), where the magnitude is the angle
        turned anticlockwise in radians around the vector's
        direction.
)r   r   )r   s    r2   matrix_to_axis_angler   R  s     $$8$@AArS   d6c                     U SSS24   U SSS24   p![         R                  " USS9nX#U-  R                  SSS9U-  -
  n[         R                  " USS9n[        R                  " X4SS9n[        R
                  " X4U4SS9$ )	a  
Converts 6D rotation representation by Zhou et al. [1] to rotation matrix
using Gram--Schmidt orthogonalisation per Section B of [1].
Args:
    d6: 6D rotation representation, of size (*, 6)

Returns:
    batch of rotation matrices of size (*, 3, 3)

[1] Zhou, Y., Barnes, C., Lu, J., Yang, J., & Li, H.
On the Continuity of Rotation Representations in Neural Networks.
IEEE Conference on Computer Vision and Pattern Recognition, 2019.
Retrieved from http://arxiv.org/abs/1812.07035
.Nr
   rN   )r   Tre   r   )F	normalizerh   rg   r   r   )r   a1a2b1b2b3s         r2   rotation_6d_to_matrixr   b  s      RaR["S!"W+	
RR	 B	BwmmBm-2	2B	
RR	 B	R	$B;;|,,rS   )F)g?g      ?g      I@)g#B;)r8   r=   numpyr   nvdiffrast.torchrg   r   torch.nn.functionalnn
functionalr   r3   rL   rR   rX   r`   rb   Tensorri   rk   r   rr   rt   r   r   r   r   r   r   r   r    rS   r2   <module>r      s   
 
     Tn;|87
77.5<< .ELL .U\\ .!u|| ! ! !ell  5<< ell  5<< 4&\,$-2>:B -ell -u|| -rS   