
    Mi9                        S SK rSSKJr  SSKJr  SSKJr   S SKr	S rSS jrSS	 jrS
 r\R$                  S4S\S\4S jjrS rSS jr SS jrSS jrg! \
 a"  rSSKJr  \R                  " \5      r	 SrCN^SrCff = f)    N   )util)log)NDArray)
exceptionsc                     [        U/ SQ5      nU b7  [        R                  " U[        R                  " U S5      5      SS * USS2S4'   U$ )ae  
Given the origin and normal of a plane find the transform
that will move that plane to be coplanar with the XY plane.

Parameters
----------
origin : (3,) float
    Point that lies on the plane
normal : (3,) float
    Vector that points along normal of plane

Returns
---------
transform: (4,4) float
    Transformation matrix to move points onto XY plane
)r   r   r   Nr      )align_vectorsnpdotappend)originnormal	transforms      j/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/geometry.pyplane_transformr      sN    " fi0IFF9bii.BCBQGG	"1"a%    c                    [         R                  " U [         R                  S9n [         R                  " U[         R                  S9nU R                  S:w  d  UR                  S:w  a  [	        S5      e[         R
                  R                  U R                  S5      5      S   n[         R
                  R                  UR                  S5      5      S   n[         R
                  R                  U5      S:  a  USS2S4==   S-  ss'   [         R
                  R                  U5      S:  a  USS2S4==   S-  ss'   [         R                  " S	5      nUR                  UR                  5      USS
2SS
24'   U(       ae  [         R                  " US   US   5      n[         R                  " [         R                  " USS5      5      nUS:  a  U[         R                  -  nXW4$ U$ )a|  
Find the rotation matrix that transforms one 3D vector
to another.

Parameters
------------
a : (3,) float
  Unit vector
b : (3,) float
  Unit vector
return_angle : bool
  Return the angle between vectors or not

Returns
-------------
matrix : (4, 4) float
  Homogeneous transform to rotate from `a` to `b`
angle : float
  If `return_angle` angle in radians between `a` and `b`

dtype)r	   zvectors must be (3,)!r   r   Nr            r	         ?gh㈵)r   arrayfloat64shape
ValueErrorlinalgsvdreshapedeteyer   Tarccosclippi)abreturn_angleaubumatrixr   angles           r   r
   r
   '   sf   , 	"**%A
"**%Aww$!''T/011 
qyy)	*1	-B	qyy)	*1	-B	yy}}R1
1b5	T		yy}}R1
1b5	T	 VVAYFVVBDD\F2A2rr6N ffRUBqE"		"''#tS12;RUUNE}Mr   c                 2   [         R                  " U [         R                  5      n U SS2/ SQ4   R                  S5      nU(       aP  [         R                  " [         R
                  " [        U 5      5      S5      R                  R                  S5      nX#4$ U$ )z
Given a list of faces (n,3), return a list of edges (n*3,2)

Parameters
-----------
faces : (n, 3) int
  Vertex indices representing faces

Returns
-----------
edges : (n*3, 2) int
  Vertex indices representing edges
N)r   r   r      r1   r   )r   r1   )r	   r   r   )r   
asanyarrayint64r"   tilearangelenr%   )facesreturn_indexedges
face_indexs       r   faces_to_edgesr;   \   st     MM%*E !''(009EWWRYYs5z2F;==EEbI
  Lr   c                    [         R                  " U [         R                  S9n [        U 5      S:X  a  [         R                  " / 5      $ [
        R                  " U S5      (       a  U R                  S5      n O'[
        R                  " U S5      (       d  [        S5      e[
        R                  " U SS2S4   U SS2S4   5      n[         R                  " US	S
5      n[         R                  " [         R                  " U5      5      nU$ )z
Find the angles between pairs of unit vectors.

Parameters
----------
pairs : (n, 2, 3) float
  Unit vector pairs

Returns
----------
angles : (n,) float
  Angles between vectors in radians
r   r   r1   r	   )r   r1   r	   )r   r1   r=   zpairs must be (n,2,(2|3))!Nr   r   r   )r   r2   r   r6   r   r   is_shaper"   r   diagonal_dotr'   absr&   )pairsdotsangless      r   vector_anglerD   v   s     MM%rzz2E
5zQxx|	uf	%	%j)]]5/22566 U1a4[%1+6D774s#DVVBIIdO$FMr   Tuse_fanreturnc                    [        U 5      S:X  a  [        R                  " SUS9$  [        R                  " XS9n [        U R                  5      S:X  a$  U R                  S   S:X  a  U R                  U5      $ [        U R                  5      S:X  aL  U R                  S   S:X  a9  [        R                  " U SS2/ SQ4   U SS2/ S	Q4   45      R                  U5      $ [        R                  " U  Vs/ s H  n[        U5      PM     sn[        R                  S9n[        R                  " [        R                  " US:H  5      S    Vs/ s H  o0U   PM	     sn[        R                  S9n[        R                  " [        R                  " US:H  5      S    Vs/ s H  o0U   PM	     sn[        R                  S9nU(       a/  [        R                  " US:  5      S    Vs/ s H  o0U   PM	     nnO/ n[        U5      S:X  a   [        U5      S:X  a  UR                  U5      $ [        U5      S:  a  [        R                  " U5      n[        U5      S:  a*  [        R                  " USS2/ SQ4   USS2/ S	Q4   45      n[        R                  " XVU/5      R                  U5      $ ! [         a     GNf = fs  snf s  snf s  snf s  snf )
a  
Given an array of quad faces return them as triangle faces,
also handles pure triangles and mixed triangles and quads.

Parameters
-----------
quads: (n, 4) int
  Vertex indices of quad faces.
dtype
  Data type requested for the return
use_fan
  Triangulate holes larger than quads with fans,
  which may be wrong if the holes are non-convex

Returns
-----------
faces : (m, 3) int
  Vertex indices of triangular faces.
r   r   r1   r   r	   r   N)r   r   r1   )r1   r	   r   )r6   r   zerosr   r   astypevstackr   r3   nonzeror   triangle_fans_to_facesvstack_empty)quadsr   rE   ilengthstriquadpolys           r   triangulate_quadsrT      s?   * 5zQxx'' ,u{{q U[[^q%8<<&&u{{q U[[^q%899eAyL15I3FGHOOPUVV hh.1A.bhh?G ((bjjA&>q&AB&A!H&AB"((
SC88rzz'Q,'?'BC'B!1X'BC288TD"$**Wq["9!"<="<Qa"<= 
4yA~#d)q.zz%  
4y1}**40
4y1}yy$q)|,d1i<.@ABc./66u==;   / CC >s1   AJ% 7A$J% 0J6J;K K%
J32J3c                 H    [         R                  " UR                  5       U S9n[        U5      U :X  d   eUR                  5       U :  d   e[         R                  " S[         R                  " U5      SS 5      n[         R                  " UR                  5       5      USS2S4   -   nXTU-   SS2S4   :  R                  [         R                  5      * n [        R                  R!                  [        U5      ["        S	9nUR%                  U5      R'                  5       S   nXUS:H  '   U$ ! [         a@    [        R
                  " S5        [         R                  " UR                  5       SS9S   n GNOf = f! [(         a    [        R
                  " S
SS9  [         R*                  " UR,                  [         R                  S	9n	UR                  5       n
[/        U 5       H3  n[         R0                  " X:H  5      S   S-  SSS2   XU   XK   X;   -   & M5     XUS:H  '    U$ f = f)a  
Find vertex face indices from the faces array of vertices

Parameters
-----------
vertex_count : int
  The number of vertices faces refer to
faces : (n, 3) int
  List of vertex indices
faces_sparse : scipy.sparse.COO
  Sparse matrix

Returns
-----------
vertex_faces : (vertex_count, ) int
  Face indices for every vertex
  Array padded with -1 in each row for all vertices with fewer
  face indices than the max number of face indices.
)	minlengthzcasting failed, falling back!T)return_countsr   r   Nr   r   zJvertex_faces falling back to slow loop! mesh probably has degenerate facesexc_infor	   )r   bincountflatten	TypeErrorr   warninguniquer6   maxr   cumsumr5   rI   r3   scipysparseidentityintr   rK   BaseExceptionrH   sizerangewhere)vertex_countr7   faces_sparsecountsstartspackpaddedrc   sorted_facessortflatvs               r   vertex_face_indicesrs      s   ,CU]]_E v;,&&&99;%%% YYq"))F+CR01F99VZZ\"VAtG_4DD1199"((CCF#<<((U3(?#''199;A>
 +v{ MK  C345==?$?B	C0  #3	

 xx

"((3}}|$A79xx	7J17MQR7RTXVXTX6YDVY23 % #v{M#s&   #D+ AE8 +AE54E58B%H! H!c                    ^ ^^^ UUUU 4S jnUUU 4S jn U" 5       n[        R                  " U5      nU$ ! [          a    [        R                  " SSS9  U" 5       n N@f = f)a  
Find vertex normals from the mean of the faces that contain
that vertex.

Parameters
-----------
vertex_count : int
  The number of vertices faces refer to
faces : (n, 3) int
  List of vertex indices
face_normals : (n, 3) float
  Normal vector for each face

Returns
-----------
vertex_normals : (vertex_count, 3) float
  Normals for every vertex
  Vertices unreferenced by faces will be zero.
c                  N   > Tc  [        TT5      n OTn U R                  T5      nU$ N)index_sparser   )r.   summedface_normalsr7   rb   ri   s     r   summed_sparse*mean_vertex_normals.<locals>.summed_sparse/  s.     >!,6FFL)r   c                  z   > [         R                  " TS45      n [        TT5       H  u  pX==   U-  ss'   M     U $ Nr	   )r   rH   zip)rx   facer   ry   r7   ri   s      r   summed_loop(mean_vertex_normals.<locals>.summed_loop:  s<     <+,|4LDLF"L 5r   *unable to use sparse matrix, falling back!TrX   )re   r   r]   r   unitize)	ri   r7   ry   rb   kwargsrz   r   rx   vertex_normalss	   ````     r   mean_vertex_normalsr     s[    *	 	 \\&)N  @4Ps   6 &AAc                 <  ^ ^^^ UUUU 4S jnUUUU 4S jnTS-  R                  SS9S:  nTU   mTU   mTU   mU(       d   [        R                  " U" 5       5      $ [        R                  " U" 5       5      $ ! [         a    [        R
                  " SSS	9   N<f = f)
a  
Compute vertex normals from the faces that contain that vertex.
The contribution of a face's normal to a vertex normal is the
ratio of the corner-angle in which the vertex is, with respect
to the sum of all corner-angles surrounding the vertex.

Grit Thuerrner & Charles A. Wuethrich (1998)
Computing Vertex Normals from Polygonal Facets,
Journal of Graphics Tools, 3:1, 43-46

Parameters
-----------
vertex_count : int
  The number of vertices faces refer to
faces : (n, 3) int
  List of vertex indices
face_normals : (n, 3) float
  Normal vector for each face
face_angles : (n, 3) float
  Angles at each vertex in the face

Returns
-----------
vertex_normals : (vertex_count, 3) float
  Normals for every vertex
  Vertices unreferenced by faces will be zero.
c                  X   > [        TTTR                  5       S9n U R                  T5      $ )N)data)rw   ravelr   )r.   face_anglesry   r7   ri   s    r   rz   .weighted_vertex_normals.<locals>.summed_sparsem  s+    
 lE8I8I8KLzz,''r   c                  *  > [         R                  " TS4[         R                  5      n [         R                  " T5       HP  n[         R                  " TU:H  5      u  p#TX#4   n[         R
                  " XDR                  5       -  TU   5      X'   MR     U $ r}   )r   rH   r   r5   rh   r   sum)	rx   
vertex_idx	face_idxsinface_idxssurrounding_anglesr   ry   r7   ri   s	        r   r   ,weighted_vertex_normals.<locals>.summed_loopu  s    <+RZZ8))L1J &(XXez.A%B"I!,Y-C!D!#"%;%;%==|I?V"F 2 r   r1   r   )axisg      ?r   TrX   )r   r   r   re   r   r]   )ri   r7   ry   r   use_looprz   r   face_oks   ````    r   weighted_vertex_normalsr   N  s    >( (  Q###+c1G'NE(Lg&K	U<<00 <<&&  	UKKDtT	Us   A9 9BBc                 R   [         R                  " U5      n[        U 5      n UR                  S5      n[         R                  " [         R
                  " [        U5      5      R                  S5      SUR                  S   45      R                  S5      nU [        U5      4nUc#  [         R                  " [        U5      [        S9nO#[        U5      [        U5      :w  a  [        S5      eUb  UR                  U5      n[        R                  R                  X$U44XbR                  S9$ )a3  
Return a sparse matrix for which vertices are contained in which faces.
A data vector can be passed which is then used instead of booleans

Parameters
------------
columns : int
  Number of columns, usually number of vertices
indices : (m, d) int
  Usually mesh.faces

Returns
---------
sparse: scipy.sparse.coo_matrix of shape (columns, len(faces))
        dtype is boolean

Examples
 ----------
In [1]: sparse = faces_sparse(len(mesh.vertices), mesh.faces)

In [2]: sparse.shape
Out[2]: (12, 20)

In [3]: mesh.faces.shape
Out[3]: (20, 3)

In [4]: mesh.vertices.shape
Out[4]: (12, 3)

In [5]: dense = sparse.toarray().astype(int)

In [6]: dense
Out[6]:
array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0],
       [0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1],
       [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0],
       [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1]])

In [7]: dense.sum(axis=0)
Out[7]: array([3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
r   r   r   r   zdata incorrect length)r   r   )r   r2   rd   r"   r4   r5   r6   r   onesboolr   rI   ra   rb   
coo_matrixr   )columnsindicesr   r   rowcolr   s          r   rw   rw     s    d mmG$G'lG //"
C
''
		#g,''01gmmA6F2Ggbk  c'l#E|wws3xt,	Tc#h	011{{5! <<""D*#5U**"UUr   )Frv   )NN)numpyr    r   	constantsr   typedr   scipy.sparsera   re   Er   ExceptionWrapperr   r
   r;   rD   r3   r   rT   rs   r   r   rw    r   r   <module>r      s       +.2j4@ $&88T A>d A>g A>H=@1j >CB'JEVU  + ''*E	+s   A A?A::A?