Ë
    :vžj35  ã                   ó¼   — d dl ZddlmZ ddlmZ ddlmZmZ d„ Z	d„ Z
e
Z	 	 dded	eez  d
edz  fd„Zd„ Zdd„Zdd„Zej                   fd„Zd„ Zdd„Zdd„Zdd„Zy)é    Né   )Úutil)Úlog)Ú	ArrayLikeÚNumberc                 ó<  ‡‡‡— | j                   Št        j                  t        j                  d„ ‰D «       ddiŽd¬«      Št        j                  | «      Šˆˆˆfd„} |d¬«      }t        dt        ‰«      «      D ]  }t        j                  | ||«      «      }Œ  |S )	Nc              3   óF   K  — | ]  }t        j                  |«      –— Œ y ­w)N)ÚnpÚarange)Ú.0Úss     úb/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/voxel/ops.pyÚ	<genexpr>z$fill_orthographic.<locals>.<genexpr>   s   è ø€ Ð2©E q”b—i‘i —l©Eùs   ‚!ÚindexingÚijéÿÿÿÿ©Úaxisc                 ó  •— ‰d| f   }|j                  «       }‰|    |‰<   t        j                  || d¬«      }|j                  «       }d|‰<   t        j                  || d¬«      }t        j                  ||k\  ||k  «      S )N.T)r   Úkeepdimsr   )Úcopyr
   ÚminÚmaxÚlogical_and)r   Úbase_local_indicesÚlocal_indicesÚminsÚmaxsÚemptyÚindicesÚshapes        €€€r   Ú	fill_axisz$fill_orthographic.<locals>.fill_axis   s‹   ø€ Ø$ S¨$ YÑ/ÐØ*×/Ñ/Ó1ˆØ$ T™{ˆ�eÑÜ�v‰v�m¨$¸Ô>ˆØ*×/Ñ/Ó1ˆØ!ˆ�eÑÜ�v‰v�m¨$¸Ô>ˆä�~‰~Ø $Ñ&Ø $Ñ&ó
ð 	
ó    r   é   )r!   r
   ÚstackÚmeshgridÚlogical_notÚrangeÚlenr   )Údenser"   Úfilledr   r   r    r!   s       @@@r   Úfill_orthographicr,      s‚   ú€ Ø�K‰K€EÜ�h‰hÜ
�‰Ñ2©EÓ2ÐB¸TÑBÈô€Gô �N‰N˜5Ó!€Eö
ñ ˜AÔ€FÜ�aœ˜U›Ö$ˆÜ—‘ ©	°$«Ó8‰ð %à€Mr#   c                 óŒ  — t        j                  | t         j                  ¬«      } t        j                  | d«      st        d«      ‚| j                  «       dz   }t        j                  |||ft        «      }t        j                  | d«      }d|t        |j                  «      <   t        |«      D �]c  }d}t        d|dz
  «      D ]£  }g }t        j                  |||dd	…f   |||dd…f   «      }t        j                  t        j                  |«      «      d   dz   }t!        |«      }	|	d
z  dkD  xr |	d
kD  }|	d
k  rŒyt        d|	|	d
z  z
  d
«      D ]  }
d|||||
   ||
dz      …f<   Œ Œ¥ |sŒ¾t        d|dz
  «      D ]”  }g }t        j                  ||dd	…|f   ||dd…|f   «      }t        j                  t        j                  |«      «      d   dz   }t!        |«      }	|	d
k  rŒjt        d|	|	d
z  z
  d
«      D ]  }
d||||
   ||
dz      …|f<   Œ Œ– �Œf t        j"                  t        j                  |«      «      }|dz  }|S )zô
    Given a sparse surface voxelization, fill in between columns.

    Parameters
    --------------
    sparse_indices: (n, 3) int, location of filled cells

    Returns
    --------------
    filled: (m, 3) int, location of filled cells
    ©Údtype©r   é   zincorrect shaper1   r$   Fr   Nr   é   )r
   Ú
asanyarrayÚint64r   Úis_shapeÚ
ValueErrorr   ÚzerosÚboolÚaddÚtupleÚTr(   ÚequalÚwherer'   r)   Úcolumn_stack)Úsparse_indicesÚ	max_valueÚgridÚvoxels_sparseÚiÚ
check_dir2ÚjÚidxÚeqÚcr   Úkr+   s                r   Ú	fill_baserJ   #   sG  € ô —]‘] >¼¿¹ÔB€NÜ�=‰=˜¨Ô1ÜÐ*Ó+Ð+ð ×"Ñ"Ó$ qÑ(€Iä�8‰8�Y 	¨9Ð5´tÓ<€DÜ—F‘F˜>¨1Ó-€Mà#$€DŒˆ}�‰Ó	Ñ ä�9×ˆØˆ
Ü�q˜) a™-Ö(ˆAØˆCô —‘˜$˜q ! S b S˜y™/¨4°°1°a±b°©>Ó:ˆBÜ—(‘(œ2Ÿ>™>¨"Ó-Ó.¨qÑ1°AÑ5ˆCÜ�C“ˆAØ˜a™% 1™Ò.¨¨Q©ˆJØ�1ŠuØÜ˜1˜a ! a¡%™i¨Ö+�Ø23��Q˜˜3˜q™6 C¨¨A©¡JÐ.Ð.Ò/ñ ,ð )ñ Øô �q˜) a™-Ö(ˆAØˆCä—‘˜$˜q # 2 # q˜y™/¨4°°1±2°q°©>Ó:ˆBÜ—(‘(œ2Ÿ>™>¨"Ó-Ó.¨qÑ1°AÑ5ˆCÜ�C“ˆAØ�1ŠuØÜ˜1˜a ! a¡%™i¨Ö+�Ø23��Q˜˜A™  Q¨¡U¡Ð+¨QÐ.Ò/ñ ,ò )ð% ô< �_‰_œRŸX™X d›^Ó,€FØ
ˆa�K€Fà€Mr#   ÚmatrixÚpitchÚ	thresholdc                 óf  — ddl m} ddlm} |�t	        j
                  | «      |kD  } nt	        j
                  | t        ¬«      } t	        j                  | «      }d}t	        j                  ||dd¬	«      }t        |d
«      r|j                  }n|j                  }t	        j
                  |«      }|j                  dk(  r|fdz  } ||d|¬«      }t        |«      dk(  rt        j                  d«       |\  }	}
d}nt        |«      dk(  r|\  }	}
}}t	        j                   	||z  «      }	 ||	
¬«      }|S )a&  
    Convert an (n, m, p) matrix into a mesh, using marching_cubes.

    Parameters
    -----------
    matrix : (n, m, p) bool
      Occupancy array
    pitch : float or length-3 tuple of floats, optional
      Voxel spacing in each dimension
    threshold : float or None, optional
      If specified, converts the input into a boolean
      matrix by considering values above `threshold` as True


    Returns
    ----------
    mesh : trimesh.Trimesh
      Mesh generated by meshing voxels using
      the marching cubes algorithm in skimage
    r   )Úmeasurer   ©ÚTrimeshNr.   r$   Úconstant)Ú	pad_widthÚmodeÚconstant_valuesÚmarching_cubes_lewinerr1   g      à?)ÚvolumeÚlevelÚspacingz0using old marching cubes, may not be watertight!r2   )ÚverticesÚfacesÚvertex_normals)ÚskimagerO   ÚbaserQ   r
   r3   r8   r'   ÚpadÚhasattrrV   Úmarching_cubesÚsizer)   r   ÚwarningÚsubtract)rK   rL   rM   rO   rQ   Ú
rev_matrixrS   ÚfuncÚmeshedrZ   r[   ÚnormalsÚ_valsÚmeshs                 r   Úmatrix_to_marching_cubesrk   c   s(  € õ2  åàÐÜ—‘˜vÓ&¨Ñ2‰ä—‘˜v¬TÔ2ˆä—‘ Ó'€Jð €IÜ—‘Ø˜y°
ÈQô€Jô
 ˆwÐ0Ô1Ø×-Ñ-‰à×%Ñ%ˆô �M‰M˜%Ó €EØ‡z�z�Q‚Ø�˜1‘ˆÙØØØô€Fô ˆ6ƒ{�aÒÜ�‰ÐFÔGØ ‰ˆ�%Ø‰Ü	ˆV‹˜Ò	Ø*0Ñ'ˆ�%˜ %ô �{‰{˜8 Y°Ñ%6Ó7€Há˜H¨EÀ'ÔJ€DØ€Kr#   c                 óÐ  — t        j                  | t         j                  ¬«      } t        j                  | d«      st        d«      ‚| j                  d¬«      dz   }t        j                  t        j                  |«      t        ¬«      }t        j                  t        j                  |dd «      |d   dg«      }| |z  j                  d¬«      }d	||<   |j                  |«      }|S )
a  
    Take a sparse (n,3) list of integer indexes of filled cells,
    turn it into a dense (m,o,p) matrix.

    Parameters
    -----------
    sparse : (n, 3) int
      Index of filled cells

    Returns
    ------------
    dense : (m, o, p) bool
      Matrix of filled cells
    r.   r0   zsparse must be (n,3)!r   r   r$   Nr   T)r
   r3   r4   r   r5   r6   r   r7   Úprodr8   ÚarrayÚsumÚreshape)Úsparser!   rK   Ú
multiplierÚindexr*   s         r   Úsparse_to_matrixrt   ®   s»   € ô  �]‰]˜6¬¯©Ô2€FÜ�=‰=˜ Ô)ÜÐ0Ó1Ð1à�J‰J˜AˆJÓ Ñ"€EÜ�X‰X”b—g‘g˜e“n¬DÔ1€FÜ—‘œ2Ÿ7™7 5¨¨ 9Ó-¨u°Q©x¸Ð;Ó<€Jà�jÑ ×%Ñ%¨1Ð%Ó-€EØ€Fˆ5�Mà�N‰N˜5Ó!€EØ€Lr#   c                 ój  — t        j                  | t         j                  ¬«      } t        j                  |t        ¬«      }| j	                  d¬«      }| |z
  |z  j                  «       j                  t         j                  «      }t        |«      }t        ||¬«      }|xj                  |z  c_
        |S )a  
    Mesh points by assuming they fill a voxel box, and then
    running marching cubes on them

    Parameters
    ------------
    points : (n, 3) float
      Points in 3D space

    Returns
    -------------
    mesh : trimesh.Trimesh
      Points meshed using marching cubes
    r.   r   r   )rL   )r
   r3   Úfloat64Úfloatr   ÚroundÚastyper4   rt   rk   rZ   )ÚpointsrL   Úoriginrs   rK   rj   s         r   Úpoints_to_marching_cubesr|   Í   s�   € ô  �]‰]˜6¬¯©Ô4€FÜ�M‰M˜%¤uÔ-€Eð �Z‰Z˜QˆZÓ€Fà�v‰o Ñ&×-Ñ-Ó/×6Ñ6´r·x±xÓ@€Eô ˜eÓ$€Fô $ F°%Ô8€DØ‡M‚M�VÑ…Mà€Kr#   c                 óP  — ddl m} ddlm} t	        j
                  | t        j                  ¬«      } |j                  «       }|j                  t        |«      «       t	        j                  | dt        |j                  «      f«      j                  d«      }|t	        j                  |j                  t        | «      df«      z  }t	        j                  |j                  t        | «      df«      }|t	        j                  t	        j                   t        | «      «      t        |j                  «      z  t        |j                  «      «      dd…df   z  }|�rt	        j"                  |j$                  «      j'                  t(        «      }	t	        j                  |	t        | «      df«      }
t	        j                  t	        j                   t        | «      «      t        |j                  «      «      }t	        j"                  | |z  «      j'                  t(        «      }t+        t-        t.        |«      «      }||   }||
z   }t	        j0                  |D �cg c]  }t/        |«      |v‘Œ c}«      }n$t	        j2                  t        |«      t4        ¬«      }d}|�t	        j6                  |«      }|j8                  dk(  r|d   j                  t        | «      d¬	«      }|j8                  dk(  r-t        |«      t        | «      k(  r|j                  d
d¬	«      |   } ||||   |¬«      }|S c c}w )aý  
    Return a Trimesh object with a box at every center.

    Doesn't do anything nice or fancy.

    Parameters
    -----------
    centers : (n, 3) float
      Center of boxes that are occupied
    pitch : float
      The edge length of a voxel
    colors : (3,) or (4,) or (n,3) or (n, 4) float
      Color of boxes
    remove_internal_faces : bool
      If True, removes internal faces shared between adjacent boxes

    Returns
    ---------
    rough : Trimesh
      Mesh object representing inputs
    r   )Ú
primitivesrP   r.   r$   r0   Nr   r   é   )rZ   r[   Úface_colors)Ú r~   r^   rQ   r
   r3   rv   ÚBoxÚapply_scalerw   Útiler)   rZ   rp   r[   Úrepeatr   rx   Úface_normalsry   ÚintÚsetÚmapr:   rn   Úonesr8   ÚasarrayÚndim)ÚcentersrL   ÚcolorsÚremove_internal_facesr~   rQ   ÚbÚvÚfÚbase_normalsr†   Úface_voxel_idxÚvoxel_coordsÚ	voxel_setÚvoxel_face_coordsÚneighbor_coordsrH   Ú	keep_maskr€   rj   s                       r   Úmultiboxrš   ï   sZ  € õ, Ýô �m‰m˜G¬2¯:©:Ô6€Gð 	�‰Ó€Aà‡M�M”%˜“,Ôä
�‰�˜!œS §¡›_Ð-Ó.×6Ñ6°wÓ?€AàŒ�‰�—‘œc '›l¨AÐ.Ó	/Ñ/€Aä
�‰�—‘œ#˜g›,¨Ð*Ó+€AØŒ�‰”2—9‘9œS ›\Ó*¬S°·±«_Ñ<¼cÀ!Ç'Á'»lÓ	KÊAÈtÈGÑ	TÑT€Aâä—x‘x §¡Ó/×6Ñ6´sÓ;ˆä—w‘w˜|¬c°'«l¸AÐ->Ó?ˆô Ÿ™Ü�I‰I”c˜'“lÓ#¤S¨¯©£\ó
ˆô —x‘x ¨%¡Ó0×7Ñ7¼Ó<ˆäœœE <Ó0Ó1ˆ	ð )¨Ñ8Ðà+¨lÑ:ˆä—H‘HÁÓQÁ¸Aœe A›h¨iÒ7ÀÑQÓR‰	ä—G‘GœC ›F¬$Ô/ˆ	à€KØÐÜ—‘˜FÓ#ˆØ�;‰;˜!ÒØ˜D‘\×(Ñ(¬¨W«¸AÐ(Ó>ˆFØ�;‰;˜!Ò¤ F£¬s°7«|Ò ;Ø Ÿ-™-¨°˜-Ó3°IÑ>ˆKá˜A Q y¡\¸{ÔK€Dà€Kùò Rs   ÉL#c                 ó’  — ddl }t        j                  | j                  d¬«      | j	                  d¬«      |j                  d¬«      |j	                  d¬«      g«      }|j                  d¬«      dz
  }t        t        j                  |d¬«      dz   «      }|j                  | |z
  j                  t        j                  t        | «      t        ¬«      |¬«      }|j                  ||z
  j                  t        j                  t        |«      t        ¬«      |¬«      } |||«      }	t        j                  |	j                  «      |z   }
|
S )aŠ  
    Find common rows between two arrays very quickly
    using 3D boolean sparse matrices.

    Parameters
    -----------
    a: (n, d)  int, coordinates in space
    b: (m, d)  int, coordinates in space
    operation: numpy operation function, ie:
                  np.logical_and
                  np.logical_or

    Returns
    -----------
    coords: (q, d) int, coordinates in space
    r   Nr   r$   r   r.   )Údatar!   )rq   r
   rn   r   r   r:   ÚptpÚCOOr;   rŠ   r)   r8   r>   Úcoords)Úar�   Ú	operationrq   Úextremar{   rb   Úsp_aÚsp_bÚappliedrŸ   s              r   Úboolean_sparser¦   <  sú   € ó& ô �h‰h˜Ÿ™ 1˜› q§u¡u°! u£}°a·e±eÀ°e³mÀQÇUÁUÐPQÀUÃ]ÐSÓT€GØ�[‰[˜aˆ[Ó  1Ñ$€FÜ”—‘˜ aÔ(¨1Ñ,Ó-€Dð �:‰:�q˜6‘z—n‘n¬2¯7©7´3°q³6ÄÔ+FÈdˆ:ÓS€DØ�:‰:�q˜6‘z—n‘n¬2¯7©7´3°q³6ÄÔ+FÈdˆ:ÓS€Dñ ˜˜dÓ#€Gä�_‰_˜WŸ^™^Ó,¨vÑ5€Fà€Mr#   c                 óà  — | j                   }t        |«      }g }g }t        t        |«      «      D ]•  }t        t        |«      «      t        t        |dz   |«      «      z   }t	        j
                  | |¬«      }t	        j                  |«      \  }|d   }	|d   }
|j                  |	|
g«       |j                  t        |	|
«      «       Œ— | t        |«         t	        j                  |t        «      fS )Nr$   r   r   r   )r!   r)   r(   r:   r
   ÚanyÚnonzeroÚappendÚslicern   r‡   )rœ   r!   ÚndimsÚpaddingÚslicesÚdimr   r+   r    Úpad_leftÚ	pad_rights              r   Ústrip_arrayr²   c  sË   € Ø�J‰J€EÜ�‹J€EØ€GØ€FÜ”S˜“ZÖ ˆÜ”U˜3“ZÓ ¤5¬¨s°Q©w¸Ó)>Ó#?Ñ?ˆÜ—‘˜ 4Ô(ˆÜ—Z‘Z Ó'‰
ˆØ˜1‘:ˆØ˜B‘Kˆ	Ø�‰˜ )Ð,Ô-Ø�‰”e˜H iÓ0Õ1ð !ð ”�f“Ñ¤§¡¨´#Ó 6Ð6Ð6r#   c                 ó@  — t        j                  | «      } | j                  dd dk7  rt        d«      ‚t        j                  | t         j
                  ¬«      }|�|t        |«      z  }|�4t        j                  |«      }|j                  dk7  rt        d«      ‚||z  }|S )ak  
    Convert indices of an (n,m,p) matrix into a set of voxel center points.

    Parameters
    ----------
    indices: (q, 3) int, index of voxel matrix (n,m,p)
    pitch: float, what pitch was the voxel matrix computed with
    origin: (3,) float, what is the origin of the voxel matrix

    Returns
    ----------
    points: (q, 3) float, list of points
    r$   N©r1   zshape of indices must be (q, 3)r.   úshape of origin must be (3,))r
   r3   r!   r6   rn   rv   rw   )r    rL   r{   rz   s       r   Úindices_to_pointsr¶   s  s”   € ô �m‰m˜GÓ$€GØ‡}�}�Q�RÐ˜DÒ ÜÐ:Ó;Ð;ä�X‰X�g¤R§Z¡ZÔ0€FØÐØ”%˜“,ÑˆØÐÜ—‘˜vÓ&ˆØ�<‰<˜4ÒÜÐ;Ó<Ð<Ø�&Ñˆà€Mr#   c                 ór   — t        j                  t        j                  | «      «      }t        |||¬«      }|S )aS  
    Convert an (n,m,p) matrix into a set of points for each voxel center.

    Parameters
    -----------
    matrix: (n,m,p) bool, voxel matrix
    pitch: float, what pitch was the voxel matrix computed with
    origin: (3,) float, what is the origin of the voxel matrix

    Returns
    ----------
    points: (q, 3) list of points
    )r    rL   r{   )r
   r>   r©   r¶   )rK   rL   r{   r    rz   s        r   Úmatrix_to_pointsr¸   ‘  s.   € ô �o‰oœbŸj™j¨Ó0Ó1€GÜ w°eÀFÔK€FØ€Mr#   c                 óÌ  — t        j                  | t         j                  ¬«      } | j                  | j                  d   dfk7  rt	        d«      ‚|�4t        j
                  |«      }|j                  dk7  rt	        d«      ‚| |z  } |�| |z  } t        j
                  |t         j                  ¬«      }t        |«      }t        j                  | «      j                  t        «      }|S )a�  
    Convert center points of an (n,m,p) matrix into its indices.

    Parameters
    ----------
    points : (q, 3) float
      Center points of voxel matrix (n,m,p)
    pitch : float
      What pitch was the voxel matrix computed with
    origin : (3,) float
      What is the origin of the voxel matrix

    Returns
    ----------
    indices : (q, 3) int
      List of indices
    r.   r   r1   zshape of points must be (q, 3)r´   rµ   )
r
   rn   rv   r!   r6   r3   rw   rx   ry   r‡   )rz   rL   r{   r    s       r   Úpoints_to_indicesrº   ¤  s½   € ô$ �X‰X�f¤B§J¡JÔ/€FØ‡|�|˜Ÿ™ Q™¨Ð+Ò+ÜÐ9Ó:Ð:àÐÜ—‘˜vÓ&ˆØ�<‰<˜4ÒÜÐ;Ó<Ð<Ø�&ÑˆØÐØ�%‰ˆä�]‰]˜6¬¯©Ô4€FÜ�%‹L€Eä�h‰h�vÓ×%Ñ%¤cÓ*€GØ€Nr#   )ç      ð?N)r»   )r»   NF)NN)Únumpyr
   r�   r   Ú	constantsr   Útypedr   r   r,   rJ   Úfill_voxelizationrk   rt   r|   rš   r   r¦   r²   r¶   r¸   rº   © r#   r   Ú<module>rÁ      sŽ   ðÛ å Ý ß %òò6:ðz Ð ð
 !$Ø#ñHØðHà�IÑðHð ˜‰}óHòVó>óDJðZ $&§>¡>ó $òN7ó ó<ô&"r#   