
    Mi9                     h    S r SSKJr  SSKrSSKJrJrJr  SSK	J
r
  SSKJr   SS jrSS	 jrSS
 jrg)zJ
remesh.py
-------------

Deal with re- triangulation of existing meshes.
    )zip_longestN   )graphgroupingutil)tol)faces_to_edgesc                    Uc#  [         R                  " [        U5      [        S9nO&[         R                  " [        U5      [        S9nSXR'   X   n[         R
                  " [        U5      SS9n[        R                  " U5      u  pXU      R                  SS9n
U	R                  S5      [        U 5      -   n[         R                  " USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   USS2S4   /5      R                  S5      n[         R                  " X)    U45      n[         R                  " X
45      nUbi  0 nUR                  5        HO  u  nn[        U5      [        U 5      :w  a  M   UXx      R                  SS9n[         R                  " UU45      UU'   MQ     XU4$ U(       a~  [         R                  " U5      S   n[        U5      [        U5      -
  n[         R                  " UU[        U5      S	-  -   5      R                  S
5      n[!        [#        UU5      5      nXU4$ X4$ )a,  
Subdivide a mesh into smaller triangles.

Note that if `face_index` is passed, only those
faces will be subdivided and their neighbors won't
be modified making the mesh no longer "watertight."

Parameters
------------
vertices : (n, 3) float
  Vertices in space
faces : (m, 3) int
  Indexes of vertices which make up triangular faces
face_index : faces to subdivide.
  if None: all faces of mesh will be subdivided
  if (n,) int array of indices: only specified faces
vertex_attributes : dict
  Contains (n, d) attribute data
return_index : bool
  If True, return index of original face for new faces

Returns
----------
new_vertices : (q, 3) float
  Vertices in space
new_faces : (p, 3) int
  Remeshed faces
index_dict : dict
  Only returned if `return_index`, {index of
  original face : index of new faces}.
NdtypeTr   axis   r         )r   r   )nponeslenboolzerossortr	   r   unique_rowsmeanreshapecolumn_stackvstackitemsnonzeroarangedictzip)verticesfaces
face_indexvertex_attributesreturn_index	face_maskfaces_subsetedgesuniqueinversemidmid_idxf	new_facesnew_verticesnew_attributeskeyvaluesattr_midr    startstack
index_dicts                          h/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/remesh.py	subdivider;      s_   D GGCJd3	HHSZt4	 $	 #L GGN<0q9E**51OF
=
!
&
&A
&
.Coog&X6G 	AAqDMAqDMAqDMAAqDMAqDMAqDMAAqDMAqDMAqDM	
	 gg ( 		5,a01I99h_-L$,224KC6{c(m+em,11q19H"$))VX,>"?N3	 5
 66**Y'* E
S\)		%Q!!34<<WE#gu-.

22""    c                    / n/ n/ n[         R                  " U[         R                  SS9n[         R                  " U [         R                  SS9n	[         R                  " [        U5      5      n
[        US-   5       GH,  n[         R                  " XSS2/ SQ4   SS24   SS9S-  R                  SS9S	-  nX:  R                  SS9nU) n[        R                  " X   R                  5       SS
9u  nnUR                  X   5        UR                  UR                  S5      5        U(       aD  UR                  X   5        [         R                  " X   S5      R                   R#                  5       n
UR                  5       (       d    O%X:  a  [%        S5      e['        XU   5      u  pGM/     [(        R*                  " Xe5      u  nnU(       a5  [         R,                  " U5      n[        U5      [        U5      :X  d   eUUU4$ UU4$ )a  
Subdivide a mesh until every edge is shorter than a
specified length.

Will return a triangle soup, not a nicely structured mesh.

Parameters
------------
vertices : (n, 3) float
  Vertices in space
faces : (m, 3) int
  Indices of vertices which make up triangles
max_edge : float
  Maximum length of any edge in the result
max_iter : int
  The maximum number of times to run subdivision
return_index : bool
  If True, return index of original face for new faces

Returns
------------
vertices : (j, 3) float
  Vertices in space
faces : (q, 3) int
  Indices of vertices
index : (q, 3) int
  Only returned if `return_index`, index of
  original face for each new face.
T)r   copyr   N)r   r   r   r   r   r   r   g      ?return_inverser   )r   r   zmax_iter exceeded!)r   arrayint64float64r!   r   rangediffsumanyr   unique_bincountflattenappendr   tileTravel
ValueErrorr;   r   append_facesconcatenate)r$   r%   max_edgemax_iterr(   	done_face	done_vertdone_idxcurrent_facescurrent_verticescurrent_indexiedge_lengthtoo_longface_okr,   r-   final_verticesfinal_facesfinal_indexs                       r:   subdivide_to_sizer`   u   s   > IIH HHU"((>Mxx

F IIc%j)M 8a<  GG$1l?%CRaR%GHqQUVV
#1#+  *//Q/7) #22"**,T

 	)1212OOM23GGM$;VDFFLLNM ||~~ =122 -6H5-
)	=C !L #'"3"3I"INKnnX.;3{#3333{K77;&&r<   c                   ^ Uc  SnU4S jm[        U5       H  nT" X5      u  pM     [        R                  (       d   [        R                  " U 5      R                  5       (       d   e[        R                  " U5      R                  5       (       d   e[        R                  " X   5      R                  5       (       d   eUSS2SS24   USS2SS24   :g  R                  5       (       d   eX4$ )a	  
Subdivide a mesh by dividing each triangle into four triangles
and approximating their smoothed surface (loop subdivision).
This function is an array-based implementation of loop subdivision,
which avoids slow for loop and enables faster calculation.

Overall process:
1. Calculate odd vertices.
  Assign a new odd vertex on each edge and
  calculate the value for the boundary case and the interior case.
  The value is calculated as follows.
      v2
    / f0 \        0
  v0--e--v1      /   \
    \f1 /     v0--e--v1
      v3
  - interior case : 3:1 ratio of mean(v0,v1) and mean(v2,v3)
  - boundary case : mean(v0,v1)
2. Calculate even vertices.
  The new even vertices are calculated with the existing
  vertices and their adjacent vertices.
    1---2
   / \/ \      0---1
  0---v---3     / \/ \
   \ /\/    b0---v---b1
    k...4
  - interior case : (1-kB):B ratio of v and k adjacencies
  - boundary case : 3:1 ratio of v and mean(b0,b1)
3. Compose new faces with new vertices.

Parameters
------------
vertices : (n, 3) float
  Vertices in space
faces : (m, 3) int
  Indices of vertices which make up triangles

Returns
------------
vertices : (j, 3) float
  Vertices in space
faces : (q, 3) int
  Indices of vertices
iterations : int
      Number of iterations to run subdivision
Nr   c                 Z
  > [        USS9u  p#UR                  SS9  [        R                  " U5      u  pE[        R                  " [        R
                  " USS9SS9n[        R
                  " USS9n[        U5      S-  [        U5      -   [        U5      :X  d  [        R                  " X6   5      n[        U5      S:X  a  [        S5      e/ n	/ n
SnU Hy  n[        R                  " X   R                  S	5      SS
9u  pET(" X   UR                  S5      S9u  pX-  nU[        U5      -  nU	R                  U5        U
R                  U5        M{     [        R                  " U	5      [        R                  " U
5      4$ [        R                  " [        U5      [        S9nSX'   X   nU) n[        R                  " [        U5      5      R!                  ["        5      nUS S 2S4   UUS S 2S4   '   US S 2S4   UUS S 2S4   '   X4   nUUU      nXU      R%                  SS9nX$U      nU U   S S 2S4   nU U   S S 2S4   nUUU      nUUU      nUUS S 2S S 2S 4   US S 2S S S 24   :H  R'                  S	5      )    nUUS S 2S S 2S 4   US S 2S S S 24   :H  R'                  S	5      )    nU U   nU U   nSU-  SU-  -   US-  -   US-  -   UU'   [        R(                  " X$   [        U 5      S9n[        R*                  " [-        [/        USS	065      5      R0                  n[        R                  " U / SQ/5      nUS-   R!                  [        5      R3                  SS9n [        R4                  " U 5      n!SS[        R6                  " S[        R8                  -  U -  5      -  S-   S-  -
  SU -  -  n"U"S S 2S 4   UU   R3                  S5      -  SU S S 2S 4   U"S S 2S 4   -  -
  U -  -   n!UR'                  5       (       as  [        R                  " [        U 5      [        S9n#SU#[        R:                  " X$   U)    5      '   UU#   n$S	U$U#UU#      ) '   UU$   R3                  SS9S-  SU U#   -  -   U!U#'   UR                  S5      [        U 5      -   n%[        R<                  " US S 2S4   U%S S 2S4   U%S S 2S4   U%S S 2S4   US S 2S4   U%S S 2S4   U%S S 2S4   U%S S 2S4   US S 2S4   U%S S 2S4   U%S S 2S4   U%S S 2S4   /5      R                  S5      n&[        R                  " U!U45      n'U'U&4$ )NT)r(   r   r   r   )require_countz*Some edges are shared by more than 2 facesr   r   r?   r   )r$   r%   r   g      ?g       @)r+   	max_index	fillvalue)        rf   rf   g      D@g       @r   @   g      ?)r	   r   r   r   r   
group_rowsr   r   connected_componentsrN   rH   r   rJ   r   r   r   astypeintr   rG   	neighborsrA   listr   rL   rF   
zeros_likecospir,   r   ))r$   r%   r+   
edges_facer,   r-   
edge_inter
edge_boundfaces_group	seq_verts	seq_facescountr0   	cur_verts	cur_facesedge_bound_maskedge_inter_mask	edge_pairopposite_face1opposite_face2oddee_v0e_v1e_f0e_f1e_v2_idxe_v3_idxe_v2e_v3rl   	vertices_kevenbetavrt_bound_maskboundary_neighborsodd_idxr1   r2   
_subdivides)                                           r:   r   "subdivide_loop.<locals>._subdivide  s   *5tD


"..u5 WWX00aHqQ
((a@
:"S_4E
B
  44Z5KLK;1$ !MNN II E 
 #+":":H$$R(#
 (2%-W__W5M($	 "	Y'  +  ++ !0 99Y'9)=== ((3u:T:&*#)1** HHSZ(//4	&0A&6	*QT"#&0A&6	*QT"##+#If$56 V}%***2)*{1a4 {1a4 ^O45^O45$q!Tz*a4
m;@@DDE$q!Tz*a4
m;@@DDE!!  %t|edl:TCZG$QT*TO OO%-3x=Q	 HHT+y"GB"GHIKK	 IIx9:	]""4(,,!,4 }}X& bffQY]33a7A=="q&IDMIi044Q771QW:QW--9: 	   XXc(m4@NIMN299U]O3C%DEF "+>!:MOy/H IIJ ,-11q19C? 889   //'*S]:OOad111ad111ad111
 ''
 	$ yy$-Y&&r<   )rD   r   strictr   isfiniteall)r$   r%   
iterations_r   s       @r:   subdivide_loopr      s    ^ 
Q'f :$X5%  zzT{{8$((****{{5!%%''''{{8?+//1111 aea!e,113333?r<   )NNF)
   F)N)__doc__	itertoolsr   numpyr    r   r   r   	constantsr   geometryr	   r;   r`   r    r<   r:   <module>r      s9    "  # #  $ LQa#HX'vRr<   