Ë
    :vžj6B  ã                   ó^   — d Z ddlmZ ddlZddl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)Ú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                          ú_/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/remesh.pyÚ	subdivider:      ss  € ðD ÐÜ—G‘GœC ›J¬dÔ3‰	ä—H‘HœS ›Z¬tÔ4ˆ	Ø $ˆ	�*Ñð ˜Ñ#€Lô �G‰G”N <Ó0°qÔ9€EÜ×*Ñ*¨5Ó1�O€FˆGà
�5˜‘=Ñ
!×
&Ñ
&¨AÐ
&Ó
.€CØ�o‰o˜gÓ&¬¨X«Ñ6€Gô 	�‰àš˜A˜ÑØ’A�q�D‰MØ’A�q�D‰MØ’A�q�D‰MØš˜A˜ÑØ’A�q�D‰MØ’A�q�D‰MØ’A�q�D‰MØš˜A˜ÑØ’A�q�D‰MØ’A�q�D‰MØ’A�q�D‰Mð	
ó	÷ �gˆgÓð ô( —	‘	˜5 ) Ñ,¨aÐ0Ó1€Iä—9‘9˜h¨˜_Ó-€LàÐ$ØˆØ,×2Ñ2Ö4‰KˆC�Ü�6‹{œc (›mÒ+ØØ˜e F™mÑ,×1Ñ1°qÐ1Ó9ˆHÜ"$§)¡)¨V°XÐ,>Ó"?ˆN˜3Òð	 5ð
 ˜Y¨Ð6Ð6áä—*‘*˜YÓ'¨Ñ*ˆô �E“
œS ›\Ñ)ˆä—	‘	˜% ¬¨Q«°!©Ñ!3Ó4×<Ñ<¸WÓEˆäœ#˜g uÓ-Ó.ˆ
à˜Y¨
Ð2Ð2à˜Ð"Ð"ó    c                 ó^
  — t        j                  | t         j                  d¬«      } t        j                  |t         j                  d¬«      }t        j                  t        |«      «      }t        |dz   «      D �]Ž  }t        j                  t        |«      d¬«      }t        j                  |«      \  }}	||   }
| |
dd…df   dd…f   | |
dd…df   dd…f   z
  dz  j                  d¬«      d	z  }||kD  }|j                  «       s �n||k\  rt        d
«      ‚t        j                  t        |«      dt         j                  ¬«      }t        j                  t        |j                  «       «      «      t        | «      z   ||<   t        j                   | | |
|      j#                  d¬«      f«      } ||	   j%                  d«      }|dk\  }|j                  d¬«      }|dk(  }||   }||   }|dk(  }t        j&                  ||   d¬«      }t        j                  t        |«      «      }||   ||   }}|||f   |||dz   dz  f   |||dz   dz  f   }}}|||f   }t        j(                  ||||||f«      j%                  d«      }t        j*                  ||   d«      }|dk(  }t        j,                  ||   d¬«      dz   dz  }t        j                  t        |«      «      }||   ||   }!} | ||f   | ||dz   dz  f   | ||dz   dz  f   }}}|!||f   |!||dz   dz  f   }"}| |dd…f   | |"dd…f   z
  dz  j                  d¬«      | |dd…f   | |dd…f   z
  dz  j                  d¬«      k  dd…df   }#t        j(                  |||"f«      }$t        j.                  |#t        j(                  |||"f«      t        j(                  |||f«      «      }%t        j.                  |#t        j(                  ||"|f«      t        j(                  ||"|f«      «      }&t        j                   |$|%|&f«      }'t        j0                  ||   d«      }(|dk(  })||)   ||)   }+}*|*j2                  \  },}-}.|+j2                  \  }/}0}1t        j(                  |,|/|1|/|-|0|1|0|.|/|0|1f«      j%                  d«      }2t        j*                  ||)   d«      }3t        j                   |||'|2f«      j5                  t         j                  «      }t        j6                  |||(|3f«      }�Œ‘ |rt        |«      t        |«      k(  sJ ‚| ||fS | |fS )uË  
    Subdivide a mesh until every edge is shorter than a
    specified length.

    Every edge longer than `max_edge` is bisected at a single shared
    midpoint, so the two faces on either side stay in sync and a
    watertight input stays watertight â€” no T-junctions (cracks) are
    introduced. Faces already small enough are left untouched.

    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,) int
      Only returned if `return_index`, index of
      original face for each new face.
    T)r   Úcopyr   r   Nr   r   r   g      à?zmax_iter exceeded!r   r
   r   r   )r   ÚarrayÚfloat64Úint64r    r   Úranger   r   r   r   ÚsumÚanyÚ
ValueErrorÚfullÚintr   r   r   Úargmaxr   ÚrepeatÚargminÚwhereÚtileÚTÚastypeÚconcatenate)4r#   r$   Úmax_edgeÚmax_iterr'   ÚindexÚir*   r+   r,   Úedges_uniqueÚlengthsÚ	long_edgeÚmidpointÚface_midÚsplitÚcountÚkeepÚ
faces_keepÚ
index_keepÚoneÚjÚrÚf1Úmid1ÚaÚbÚcÚpÚ	faces_oneÚ	index_oneÚtwoÚf2Úmid2ÚqÚuse_aqÚcornerÚquad_aÚquad_bÚ	faces_twoÚ	index_twoÚthreeÚf3Úmid3Úv0Úv1Úv2Úm0Úm1Úm2Úfaces_threeÚindex_threes4                                                       r9   Úsubdivide_to_sizer}   u   sD  € ôD �x‰x˜¬¯
©
¸Ô>€HÜ�H‰H�U¤"§(¡(°Ô6€Eä�I‰I”c˜%“jÓ!€Eä�8˜a‘<× ˆä—‘œ uÓ-°AÔ6ˆÜ"×.Ñ.¨uÓ5‰ˆ�Ø˜V‘}ˆð �l¢1 a 4Ñ(¨"¨1¨"Ð,Ñ-°¸ÂaÈÀdÑ9KÈRÈaÈRÐ9OÑ0PÑPÐUVÑVß
‰#�1ˆ#‹+˜ñˆð ˜hÑ&ˆ	ð �}‰}ŒÚà�Š=ÜÐ1Ó2Ð2ô —7‘7œ3˜v›;¨´"·(±(Ô;ˆÜ Ÿi™i¬¨I¯M©M«OÓ(<Ó=ÄÀHÃÑMˆ�ÑÜ—9‘9˜h¨°¸iÑ1HÑ(I×(NÑ(NÐTUÐ(NÓ(VÐWÓXˆð ˜GÑ$×,Ñ,¨WÓ5ˆØ˜A‘ˆà—	‘	˜q�	Ó!ˆð ˜‰zˆØ˜4‘[ˆ
Ø˜4‘[ˆ
ð
 �q‰jˆÜ�I‰I�e˜C‘j qÔ)ˆÜ�I‰I”c˜!“fÓˆØ˜‘:˜x¨™}ˆDˆØ�Q˜�T‘(˜B˜q 1 q¡5¨A¡+˜~Ñ.°°1°q¸1±uÀ±k°>Ñ0Bˆaˆ1ˆØ��A�‰JˆÜ—O‘O Q¨¨1¨a°°AÐ$6Ó7×?Ñ?ÀÓHˆ	Ü—I‘I˜e C™j¨!Ó,ˆ	ð
 �q‰jˆÜ�Y‰Y�u˜S‘z¨Ô*¨QÑ.°!Ñ3ˆÜ�I‰I”c˜!“fÓˆØ˜‘:˜x¨™}ˆDˆØ�Q˜�T‘(˜B˜q 1 q¡5¨A¡+˜~Ñ.°°1°q¸1±uÀ±k°>Ñ0Bˆaˆ1ˆØ�A�q�D‰z˜4  A¨¡E¨Q¡; Ñ/ˆ1ˆð �q˜"˜1˜"�u‰o ¨¨B¨Q¨B¨¡Ñ/°AÑ5×:Ñ:ÀÐ:ÓBØ˜!˜R˜a˜R˜%‘ 8¨A¨r°¨r¨E¡?Ñ2°qÑ8×=Ñ=À1Ð=ÓEñFâ
ˆTˆ'ñˆô —‘ ! Q¨ Ó+ˆÜ—‘˜&¤"§/¡/°1°a¸°)Ó"<¼b¿o¹oÈqÐRSÐUVÈiÓ>XÓYˆÜ—‘˜&¤"§/¡/°1°a¸°)Ó"<¼b¿o¹oÈqÐRSÐUVÈiÓ>XÓYˆÜ—I‘I˜v v¨vÐ6Ó7ˆ	Ü—G‘G˜E #™J¨Ó*ˆ	ð ˜‘
ˆØ˜‘< ¨%¡ˆDˆØ—T‘T‰
ˆˆB�Ø—V‘V‰
ˆˆB�Ü—o‘oØ��R˜˜R  R¨¨R°°R¸Ð<ó
ç
‰'�'Ó
ð 	ô —i‘i  e¡¨aÓ0ˆä—	‘	˜: y°)¸[ÐIÓJ×QÑQÜ�H‰Hó
ˆô —‘ 
¨I°yÀ+ÐNÓOŠð_ !ñb Ü�5‹zœS ›ZÒ'Ð'Ð'Ø˜ Ð%Ð%à�Uˆ?Ð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(  sèt        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   )Úreturn_inverser   )r#   r$   r
   g      Ø?g       @)r*   Ú	max_indexÚ	fillvalue)ç        r„   r„   g      D@g       @r   é@   g      è?)r   r   r   r   r   Ú
group_rowsr   r   Úconnected_componentsrD   Úunique_bincountr   Úappendr   r   r   rM   rF   r   rC   Ú	neighborsr>   Úlistr   rL   rB   Ú
zeros_likeÚcosÚpir+   r   ))r#   r$   r*   Ú
edges_facer+   r,   Ú
edge_interÚ
edge_boundÚfaces_groupÚ	seq_vertsÚ	seq_facesrY   r/   Ú	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_v3rŠ   Ú	vertices_ÚkÚevenÚbetaÚvrt_bound_maskÚboundary_neighborsÚodd_idxr0   r1   Ú
_subdivides)                                           €r9   r­   z"subdivide_loop.<locals>._subdivide&  s³  ø€ ä*¨5¸tÔDÑˆˆzØ�
‰
˜ˆ
ÔÜ"×.Ñ.¨uÓ5‰ˆ�ô —W‘WœX×0Ñ0°ÀaÔHÈqÔQˆ
Ü×(Ñ(¨¸aÔ@ˆ
ä�:‹ Ñ"¤S¨£_Ñ4¼¸E»
ÒBô
  ×4Ñ4°ZÀ
Ñ5KÓLˆKä�;Ó 1Ò$Ü Ð!MÓNÐNð ˆIØˆIð ˆEã �ô
 #+×":Ñ":Ø˜!‘H×$Ñ$ RÓ(¸ô#‘�˜ñ
 (2Ø% fÑ-°W·_±_ÀWÓ5Mô(Ñ$�	˜9ð ˜UÑ"�	àœ˜Y›Ñ'�à× Ñ  Ô+Ø× Ñ  Õ+ð+ !ô0 —9‘9˜YÓ'¬¯©°9Ó)=Ð=Ð=ô Ÿ(™(¤3 u£:´TÔ:ˆØ&*ˆ˜
Ñ#Ø)¨&Ñ1ˆØ*Ð*ˆô —H‘HœS ›ZÓ(×/Ñ/´Ó4ˆ	Ø&0²°A°Ñ&6ˆ	�*šQ ˜TÑ"Ñ#Ø&0²°A°Ñ&6ˆ	�*šQ ˜TÑ"Ñ#Ø# FÑ+ˆØ# I¨fÑ$5Ñ6ˆð �u˜V‘}Ñ%×*Ñ*°Ð*Ó2ˆà�&˜Ñ)Ñ*ˆØ˜‰{š1˜a˜4Ñ ˆØ˜‰{š1˜a˜4Ñ ˆØ�^ OÑ4Ñ5ˆØ�^ OÑ4Ñ5ˆØ˜$šq¢! T˜zÑ*¨a²°4º°
©mÑ;×@Ñ@ÀÓDÐDÑEˆØ˜$šq¢! T˜zÑ*¨a²°4º°
©mÑ;×@Ñ@ÀÓDÐDÑEˆØ˜Ñ!ˆØ˜Ñ!ˆð  % t™|¨e°d©lÑ:¸TÀC¹ZÑGÈ$ÐQTÉ*ÑTˆˆOÑô —O‘O¨%°©-Ä3ÀxÃ=ÔQˆ	ô —H‘HœT¤+¨yÐ"GÀBÑ"GÓHÓI×KÑKˆ	ô —I‘I˜xªÐ9Ó:ˆ	à˜‰]×"Ñ"¤4Ó(×,Ñ,°!Ð,Ó4ˆô �}‰}˜XÓ&ˆð ˜œbŸf™f Q¬¯©¡Y°¡]Ó3Ñ3°aÑ7¸AÑ=Ñ=À"ÀqÁ&ÑIˆà’�D�‰M˜I iÑ0×4Ñ4°QÓ7Ñ7Ø�1’Q˜�W‘: ¢Q¨ W¡Ñ-Ñ-°Ñ9ñ:ð 	ð ×ÑÔ äŸX™X¤c¨(£m¼4Ô@ˆNØIMˆNœ2Ÿ9™9 U¨6¡]°OÐ3CÑ%DÓEÑFð "+¨>Ñ!:ÐØMOÐ ¨y¸Ñ/HÑ IÐIÑJð Ð,Ñ-×1Ñ1°qÐ1Ó9¸CÑ?Ø ¨Ñ 8Ñ8ñ9ð �Ñ ð —/‘/ 'Ó*¬S°«]Ñ:ˆÜ—O‘Oà’a˜�d‘Øš˜1˜‘Øš˜1˜‘Øš˜1˜‘Ø’a˜�d‘Øš˜1˜‘Øš˜1˜‘Øš˜1˜‘Ø’a˜�d‘Øš˜1˜‘Øš˜1˜‘Øš˜1˜‘ðó
÷ ‰'�'Ó
ð 	ô$ —y‘y $¨ Ó-ˆà˜YÐ&Ð&r;   )rA   r   Ústrictr   ÚisfiniteÚall)r#   r$   Ú
iterationsÚ_r­   s       @r9   Úsubdivide_loopr³   ô   sÒ   ø€ ð^ ÐØˆ
ôQ'ôf �:ÖˆÙ$ X¨uÓ5‰ˆ‘%ð ô ‡z‚z�TÜ�{‰{˜8Ó$×(Ñ(Ô*Ð*Ð*Ü�{‰{˜5Ó!×%Ñ%Ô'Ð'Ð'ä�{‰{˜8 E™?Ó+×/Ñ/Ô1Ð1Ð1ð ’a˜™�e‘ ¢a¨¨!¨ e¡Ñ,×1Ñ1Ô3Ð3Ð3à�Uˆ?Ðr;   )NNF)é
   F)N)Ú__doc__Ú	itertoolsr   Únumpyr   Ú r   r   Ú	constantsr   Úgeometryr   r:   r}   r³   © r;   r9   Ú<module>r¼      s6   ðñõ "ã ç Ý Ý $ð LQóa#óH|ô~Rr;   