
    j9                     b    d Z ddlmZ ddlZddlmZmZmZ ddl	m
Z
 ddlmZ 	 ddZdd	Zdd
Zy)zJ
remesh.py
-------------

Deal with re- triangulation of existing meshes.
    )zip_longestN   )graphgroupingutil)tol)faces_to_edgesc                    |%t        j                  t        |      t              }n)t        j                  t        |      t              }d||<   ||   }t        j
                  t        |      d      }t        j                  |      \  }}	| ||      j                  d      }
|	j                  d      t        |       z   }t        j                  |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   |dddf   g      j                  d      }t        j                  ||    |f      }t        j                  | |
f      }|ii }|j                         D ]O  \  }}t        |      t        |       k7  r|||      j                  d      }t        j                  ||f      ||<   Q |||fS |r}t        j                  |      d   }t        |      t        |      z
  }t        j                  ||t        |      d	z  z         j                  d
      }t!        t#        ||            }|||fS ||fS )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                          ;/DATA/.local/lib/python3.12/site-packages/trimesh/remesh.py	subdivider;      ss   D GGCJd3	HHSZt4	 $	* #L GGN<0q9E**51OFG
5=
!
&
&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+eFm,11q19H"$))VX,>"?N3	 5
 Y66**Y'* E
S\)		%Q!!34<<WE#gu-.
Y
22""    c                    g }g }g }t        j                  |t         j                  d      }t        j                  | t         j                  d      }	t        j                  t        |            }
t        |dz         D ]2  }t        j                  |	|ddg df   ddf   d      dz  j                  d      d	z  }||kD  j                  d      }| }t        j                  ||   j                         d
      \  }}|j                  |	|          |j                  |j                  d             |rE|j                  |
|          t        j                  |
|   d      j                   j#                         }
|j                         s n%||k\  rt%        d      t'        |	||         \  }	}5 t)        j*                  ||      \  }}|r3t        j,                  |      }t        |      t        |      k(  sJ |||fS ||fS )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mH5-
)	=C !L #'"3"3Iy"INKnnX.;3{#3333{K77;&&r<   c                    |d}fdt        |      D ]  } | |      \  } } t        j                  s	 t        j                  |       j                         sJ t        j                  |      j                         sJ t        j                  | |         j                         sJ |ddddf   |ddddf   k7  j                         sJ | |fS )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                 
   t        |d      \  }}|j                  d       t        j                  |      \  }}t	        j                  t        j
                  |d      d      }t        j
                  |d      }t        |      dz  t        |      z   t        |      k(  st        j                  ||         }t        |      dk(  rt        d      g }	g }
d}|D ]  }t        j                  ||   j                  d	      d
      \  }} (| |   |j                  d            \  }}||z  }|t        |      z  }|	j                  |       |
j                  |        t	        j                  |	      t	        j                  |
      fS t	        j                  t        |      t              }d||<   ||   }| }t	        j                  t        |            j!                  t"              }|d d df   ||d d df   <   |d d df   ||d d df   <   ||   }|||      }| ||      j%                  d      }|||      }| |   d d df   }| |   d d df   }|||      }|||      }||d d d d d f   |d d d d d f   k(  j'                  d	          }||d d d d d f   |d d d d d f   k(  j'                  d	          }| |   }| |   }d|z  d|z  z   |dz  z   |dz  z   ||<   t        j(                  ||   t        |             }t	        j*                  t-        t/        |dd	i            j0                  }t	        j                  | g dg      }|dz   j!                  t              j3                  d      } t	        j4                  |       }!ddt	        j6                  dt        j8                  z  | z        z  dz   dz  z
  d| z  z  }"|"d d d f   ||   j3                  d      z  d| d d d f   |"d d d f   z  z
  | z  z   }!|j'                         rxt	        j                  t        |       t              }#d|#t	        j:                  ||   |          <   ||#   }$d	|$|#||#       <   ||$   j3                  d      dz  d| |#   z  z   |!|#<   |j                  d      t        |       z   }%t	        j<                  |d d df   |%d d df   |%d d df   |%d d df   |d d df   |%d d df   |%d d df   |%d d df   |d d df   |%d d df   |%d d df   |%d d df   g      j                  d      }&t	        j                  |!|f      }'|'|&fS )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   z"subdivide_loop.<locals>._subdivide  s   *5tDz


"..u5 WWX00aHqQ
((a@
:"S_4E
B
  44Z
5KLK;1$ !MNN II E 
 #+":":!H$$R(#
 (2%f-W__W5M($	9 U"	Y'  +  ++ !0 99Y'9)=== ((3u:T:&*
#)&1** HHSZ(//4	&0A&6	*QT"#&0A&6	*QT"##F+#If$56 uV}%***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@NIMN299U6]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 :$Xu5%  zzT{{8$((***{{5!%%'''{{8E?+//111 aea!e,11333U?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<   