Ë
    :vžjw1  ã                   ó¢   — d Z ddlmZ ddlZddlmZ ddlmZm	Z	 ddlm
Z ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ  G d„ de«      Zy)zW
voxel.py
-----------

Convert meshes to a simple voxel data structure and back again.
é    )Úsha256Né   )Úbounds)ÚcachingÚutil)Útransformations)Úlog)Úexport_binvox)ÚGeometryé   )Ú
morphologyÚopsÚ
transforms)ÚDenseEncodingÚEncodingc                   ó\  — e Zd ZdZd&d„Zd„ Zedefd„«       Zed„ «       Z	e	j                  d„ «       Z	ed	„ «       Zej                  d
„ «       Zed„ «       Zed„ «       Zed„ «       Zed„ «       Zd„ Zd„ Zej&                  d„ «       Zej&                  d„ «       Zej&                  d„ «       Zed„ «       Zej&                  d„ «       Zd„ Zd'd„Zd„ Zej&                  d„ «       Zed„ «       Zej&                  d„ «       Zej&                  d„ «       Zed„ «       Z d(d„Z!d„ Z"d „ Z#d!„ Z$d"„ Z%d&d#„Z&d$„ Z'd%„ Z(y))Ú	VoxelGridz
    Store 3D voxels.
    Nc                 óN  — |€t        j                  d«      }t        |t         j                  «      rt	        |j                  t        «      «      }|j                  t        k7  rt        d«      ‚t        j                  «       | _        || _        t        j                  || j                  ¬«      | _        t        j                   | j                  j"                  ¬«      | _        i | _        t        |t(        «      r| j&                  j+                  |«       y |�t        d|›�«      ‚y )Né   zencoding must have dtype bool)Ú	datastore)Úid_functionz'metadata should be a dict or None, got )ÚnpÚeyeÚ
isinstanceÚndarrayr   ÚastypeÚboolÚdtypeÚ
ValueErrorr   Ú	DataStoreÚ_dataÚencodingr   Ú	TransformÚ
_transformÚCacheÚ__hash__Ú_cacheÚmetadataÚdictÚupdate)Úselfr"   Ú	transformr(   s       úc/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/voxel/base.pyÚ__init__zVoxelGrid.__init__   sØ   € ØÐÜŸ™˜q›	ˆIÜ�h¤§
¡
Ô+Ü$ X§_¡_´TÓ%:Ó;ˆHØ�>‰>œTÒ!ÜÐ<Ó=Ð=Ü×&Ñ&Ó(ˆŒ
Ø ˆŒÜ$×.Ñ.¨yÀDÇJÁJÔOˆŒÜ—m‘m°·
±
×0CÑ0CÔDˆŒàˆŒä�h¤Ô%Ø�M‰M× Ñ  Õ*ØÐ!ÜÐFÀxÀlÐSÓTÐTð "ó    c                 ó6   — | j                   j                  «       S )z¥
        Get the hash of the current transformation matrix.

        Returns
        ------------
        hash : str
          Hash of transformation matrix
        )r!   r&   ©r+   s    r-   r&   zVoxelGrid.__hash__.   s   € ð �z‰z×"Ñ"Ó$Ð$r/   Úreturnc                 ób   — t        t        | «      j                  «       «      j                  «       S ©N)r   ÚhashÚto_bytesÚ	hexdigestr1   s    r-   Úidentifier_hashzVoxelGrid.identifier_hash9   s#   € ä”d˜4“j×)Ñ)Ó+Ó,×6Ñ6Ó8Ð8r/   c                 ó    — | j                   d   S )z|
        `Encoding` object providing the occupancy grid.

        See `trimesh.voxel.encoding` for implementations.
        r"   )r!   r1   s    r-   r"   zVoxelGrid.encoding=   s   € ð �z‰z˜*Ñ%Ð%r/   c                 ó`  — t        |t        j                  «      rt        |«      }nt        |t        «      st        d|›�«      ‚t        |j                  «      dk7  rt        d|j                  ›�«      ‚|j                  t        k7  rt        d|j                  › �«      ‚|| j                  d<   y )Nz"encoding must be an Encoding, got é   z#encoding must be rank 3, got shape zencoding must be binary, got r"   )r   r   r   r   r   r   ÚlenÚshaper   r   r!   )r+   r"   s     r-   r"   zVoxelGrid.encodingF   s’   € ä�h¤§
¡
Ô+Ü$ XÓ.‰HÜ˜H¤hÔ/ÜÐAÀ(ÀÐNÓOÐOÜˆx�~‰~Ó !Ò#ÜÐBÀ8Ç>Á>ÐBTÐUÓVÐVØ�>‰>œTÒ!ÜÐ<¸X¿^¹^Ð<LÐMÓNÐNØ!)ˆ�
‰
�:Òr/   c                 ó.   — | j                   j                  S )ú&4x4 homogeneous transformation matrix.©r$   Úmatrixr1   s    r-   r,   zVoxelGrid.transformR   s   € ð �‰×%Ñ%Ð%r/   c                 ó&   — || j                   _        y)r?   Nr@   ©r+   rA   s     r-   r,   zVoxelGrid.transformW   s   € ð "(ˆ�‰Õr/   c                 ó.   — | j                   j                  S )zLocation of voxel at [0, 0, 0].)r$   Útranslationr1   s    r-   rE   zVoxelGrid.translation\   s   € ð �‰×*Ñ*Ð*r/   c                 ó.   — | j                   j                  S )z�
        3-element float representing per-axis scale.

        Raises a `RuntimeError` if `self.transform` has rotation or
        shear components.
        )r$   Úscaler1   s    r-   rG   zVoxelGrid.scalea   s   € ð �‰×$Ñ$Ð$r/   c                 ó.   — | j                   j                  S )a\  
        Uniform scaling factor representing the side length of
        each voxel.

        Returns
        -----------
        pitch : float
          Pitch of the voxels.

        Raises
        ------------
        `RuntimeError`
          If `self.transformation` has rotation or shear
          components of has non-uniform scaling.
        )r$   Úpitchr1   s    r-   rI   zVoxelGrid.pitchk   s   € ð" �‰×$Ñ$Ð$r/   c                 ó.   — | j                   j                  S r4   )r$   Úunit_volumer1   s    r-   Úelement_volumezVoxelGrid.element_volume~   s   € à�‰×*Ñ*Ð*r/   c                 ó<   — | j                   j                  |«       | S r4   )r$   Úapply_transformrC   s     r-   rN   zVoxelGrid.apply_transform‚   s   € Ø�‰×'Ñ'¨Ô/Øˆr/   c                 ó¬   — | j                   j                  \  }}|| _         | j                  |dd…df   «      | j                  j                  dd…df<   | S )zš
        Mutate self by stripping leading/trailing planes of zeros.

        Returns
        --------
        self after mutation occurs in-place
        Nr   r;   )r"   ÚstrippedÚindices_to_pointsr$   rA   )r+   r"   Úpaddings      r-   ÚstripzVoxelGrid.strip†   sS   € ð !ŸM™M×2Ñ2Ñˆ�'Ø ˆŒØ(,×(>Ñ(>¸wÂqÈ!Àt¹}Ó(Mˆ�‰×Ñ˜r ˜r 1˜uÑ%Øˆr/   c                 ó^  — | j                   }t        j                  |j                  d¬«      dz
  |j	                  d¬«      dz   g«      }| j
                  j                  |«      }t        j                  |j                  d¬«      |j	                  d¬«      g«      }d|j                  _
        |S )Nr   ©Úaxisg      à?F)Úsparse_indicesÚbounds_moduleÚcornersÚminÚmaxr$   Útransform_pointsr   ÚarrayÚflagsÚ	writeable)r+   ÚindicesrY   r   s       r-   r   zVoxelGrid.bounds“   s•   € à×%Ñ%ˆä×'Ñ'Ø�[‰[˜aˆ[Ó  3Ñ&¨¯©¸¨Ó(;¸cÑ(AÐBó
ˆð —/‘/×2Ñ2°7Ó;ˆä—‘˜7Ÿ;™;¨A˜;Ó.°·±À°Ó0CÐDÓEˆØ!&ˆ�‰ÔØˆr/   c                 óV   — | j                   }|d   |d   z
  }d|j                  _        |S )Nr   r   F)r   r^   r_   )r+   r   Úextentss      r-   rb   zVoxelGrid.extents¡   s.   € à—‘ˆØ˜‘)˜f Q™iÑ'ˆØ"'ˆ�‰ÔØˆr/   c                 ó.   — | j                   j                  S r4   )r"   Úis_emptyr1   s    r-   rd   zVoxelGrid.is_empty¨   s   € à�}‰}×%Ñ%Ð%r/   c                 ó.   — | j                   j                  S )z13-tuple of ints denoting shape of occupancy grid.)r"   r=   r1   s    r-   r=   zVoxelGrid.shape¬   s   € ð �}‰}×"Ñ"Ð"r/   c                 óJ   — | j                   j                  j                  «       S )z+int, number of occupied voxels in the grid.)r"   ÚsumÚitemr1   s    r-   Úfilled_countzVoxelGrid.filled_count±   s   € ð �}‰}× Ñ ×%Ñ%Ó'Ð'r/   c                 ó„  — t        j                  |«      }| j                  |«      }t        j                  t        j                  |t        j
                  | j                  «      k  d¬«      t        j                  |dk\  d¬«      «      }t        j                  |«      }| j                  j                  ||   «      ||<   |S )a/  
        Query points to see if the voxel cells they lie in are
        filled or not.

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

        Returns
        ---------
        is_filled : (n,) bool
          Is cell occupied or not for each point
        éÿÿÿÿrU   r   )
r   Ú
asanyarrayÚpoints_to_indicesÚlogical_andÚallr]   r=   Ú
zeros_liker"   Ú	gather_nd)r+   Úpointr`   Úin_rangeÚ	is_filleds        r-   rt   zVoxelGrid.is_filled¶   s–   € ô —‘˜eÓ$ˆØ×(Ñ(¨Ó/ˆÜ—>‘>Ü�F‰F�7œRŸX™X d§j¡jÓ1Ñ1¸Ô;¼R¿V¹VÀGÈqÁLÐWYÔ=Zó
ˆô —M‘M (Ó+ˆ	Ø"Ÿm™m×5Ñ5°g¸hÑ6GÓHˆ	�(ÑØÐr/   c                 óT   — t        j                  | j                  fd|i|¤Ž| _        | S )aß  
        Mutates self by filling in the encoding according
        to `morphology.fill`.

        Parameters
        ----------
        method : hashable
          Implementation key, one of
          `trimesh.voxel.morphology.fill.fillers` keys
        **kwargs : dict
          Additional kwargs passed through to
          the keyed implementation.

        Returns
        ----------
        self : VoxelGrid
          After replacing encoding with a filled version.
        Úmethod)r   Úfillr"   )r+   rv   Úkwargss      r-   rw   zVoxelGrid.fillÏ   s'   € ô& #Ÿ™¨¯©ÑO¸fÐOÈÑOˆŒØˆr/   c                 óN   — t        j                  | j                  «      | _        | S )am  
        Mutates self by removing internal voxels
        leaving only surface elements.

        Surviving elements are those in encoding that are
        adjacent to an empty voxel where adjacency is
        controlled by `structure`.

        Returns
        ----------
        self : VoxelGrid
          After replacing encoding with a surface version.
        )r   Úsurfacer"   r1   s    r-   ÚhollowzVoxelGrid.hollowå   s   € ô #×*Ñ*¨4¯=©=Ó9ˆŒØˆr/   c                 óB   — t        j                  | j                  ¬«      S )a²  
        A marching cubes Trimesh representation of the voxels.

        No effort was made to clean or smooth the result in any way;
        it is merely the result of applying the scikit-image
        measure.marching_cubes function to self.encoding.dense.

        Returns
        ---------
        meshed : trimesh.Trimesh
          Representing the current voxel
          object as returned by marching cubes algorithm.
        )rA   )r   Úmatrix_to_marching_cubesrA   r1   s    r-   Úmarching_cubeszVoxelGrid.marching_cubesö   s   € ô ×+Ñ+°4·;±;Ô?Ð?r/   c                 ó.   — | j                   j                  S )zÝ
        Return a DENSE matrix of the current voxel encoding.

        Returns
        -------------
        dense : (a, b, c) bool
          Numpy array of dense matrix
          Shortcut to voxel.encoding.dense
        )r"   Údenser1   s    r-   rA   zVoxelGrid.matrix  s   € ð �}‰}×"Ñ"Ð"r/   c                 ó4   — | j                   | j                  z  S )z¹
        What is the volume of the filled cells in the current
        voxel object.

        Returns
        ---------
        volume : float
          Volume of filled cells.
        )ri   rL   r1   s    r-   ÚvolumezVoxelGrid.volume  s   € ð × Ñ  4×#6Ñ#6Ñ6Ð6r/   c                 ór   — | j                   j                  | j                  j                  t        «      «      S )z¬
        The center of each filled cell as a list of points.

        Returns
        ----------
        points : (self.filled, 3) float
          Points in space.
        )r$   r\   rW   r   Úfloatr1   s    r-   ÚpointszVoxelGrid.points!  s*   € ð �‰×/Ñ/°×0CÑ0C×0JÑ0JÌ5Ó0QÓRÐRr/   c                 ó.   — | j                   j                  S )z6(n, 3) int array of sparse indices of occupied voxels.)r"   rW   r1   s    r-   rW   zVoxelGrid.sparse_indices-  s   € ð �}‰}×+Ñ+Ð+r/   c                 óÚ  — |�™t        j                  |«      }|j                  dk(  rP| j                  }|j                  dd |j                  k(  r||j
                     }n=t        j                  d«       d}n%|j                  dvrt        j                  d«       d}t        j                  | j                  j                  t        «      |¬«      }|j                  | j                  «      }|S )a‘  
        A rough Trimesh representation of the voxels with a box
        for each filled voxel.

        Parameters
        ----------
        colors : None, (3,) or (4,) float or uint8
          (X, Y, Z, 3) or (X, Y, Z, 4) float or uint8
          Where matrix.shape == (X, Y, Z)

        Returns
        ---------
        mesh : trimesh.Trimesh
          Mesh with one box per filled cell.
        Nr   r;   zcolors incorrect shape!)©r;   )r   )ÚcentersÚcolors)r   rl   Úndimr"   r=   r€   r	   Úwarningr   ÚmultiboxrW   r   r„   rN   r,   )r+   rŠ   rx   r"   Úmeshs        r-   Úas_boxeszVoxelGrid.as_boxes2  s¾   € ð" ÐÜ—]‘] 6Ó*ˆFØ�{‰{˜aÒØŸ=™=�Ø—<‘<  Ð# x§~¡~Ò5ð $ H§N¡NÑ3‘Fä—K‘KÐ 9Ô:Ø!‘FØ—‘ \Ñ1Ü—‘Ð5Ô6Ø�ä�|‰| D×$7Ñ$7×$>Ñ$>¼uÓ$EÈfÔUˆà×#Ñ# D§N¡NÓ3ˆØˆr/   c                 óˆ   — | j                   j                  |«      }t        j                  |«      j	                  t
        «      S )z÷
        Convert points to indices in the matrix array.

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

        Returns
        ---------
        indices: (n, 3) int array of indices into self.encoding
        )r$   Úinverse_transform_pointsr   Úroundr   Úint)r+   r…   s     r-   rm   zVoxelGrid.points_to_indicesW  s2   € ð —‘×9Ñ9¸&ÓAˆÜ�x‰x˜Ó×&Ñ&¤sÓ+Ð+r/   c                 ó^   — | j                   j                  |j                  t        «      «      S r4   )r$   r\   r   r„   )r+   r`   s     r-   rQ   zVoxelGrid.indices_to_pointsf  s    € Ø�‰×/Ñ/°·±¼uÓ0EÓFÐFr/   c                 ód   —  | j                  |j                  dd«      «      j                  |i |¤ŽS )zŒ
        Convert the current set of voxels into a trimesh for visualization
        and show that via its built- in preview method.
        rŠ   N)r�   ÚpopÚshow)r+   Úargsrx   s      r-   r—   zVoxelGrid.showi  s0   € ð
 >ˆt�}‰}˜VŸZ™Z¨°$Ó7Ó8×=Ñ=¸tÐNÀvÑNÐNr/   c                 óŽ   — t        | j                  j                  «       | j                  j                  j                  «       «      S r4   )r   r"   Úcopyr$   rA   r1   s    r-   rš   zVoxelGrid.copyp  s/   € Ü˜Ÿ™×+Ñ+Ó-¨t¯©×/EÑ/E×/JÑ/JÓ/LÓMÐMr/   c                 óp  — t        |t        «      r%|€#t        j                  |«      j	                  «       }|dk7  rt        d«      ‚t        | fi |¤Ž}t        |d«      r|j                  |«       |S t        |t        «      r(t        |d«      5 }|j                  |«       ddd«       |S |S # 1 sw Y   |S xY w)aH  
        Export the current VoxelGrid.

        Parameters
        ------------
        file_obj : file-like or str
          File or file-name to export to.
        file_type : None or str
          Only 'binvox' currently supported.

        Returns
        ---------
        export : bytes
          Value of export.
        NÚbinvoxzonly binvox exports supported!ÚwriteÚwb)
r   Ústrr   Úsplit_extensionÚlowerr   r
   Úhasattrr�   Úopen)r+   Úfile_objÚ	file_typerx   ÚexportedÚfs         r-   ÚexportzVoxelGrid.exports  s©   € ô  �h¤Ô$¨Ð):Ü×,Ñ,¨XÓ6×<Ñ<Ó>ˆIà˜Ò ÜÐ=Ó>Ð>ä  Ñ0¨Ñ0ˆÜ�8˜WÔ%Ø�N‰N˜8Ô$ð ˆô ˜¤#Ô&Ü�h Ô%¨Ø—‘˜Ô!÷ &àˆˆxˆ÷ &àˆús   ÂB+Â+B5c                 óR  — t        |«      }| j                  j                  «       }| j                  }t	        j
                  ||«      j                  |dz   «      }| j                  |«      }|t        j                  |«      z  }|d   }t        |t        j                  ||«      ¬«      S )au  
        Create a new VoxelGrid without rotations, reflections
        or shearing.

        Parameters
        ----------
        shape : (3, int)
          The shape of the returned VoxelGrid.

        Returns
        ----------
        vox : VoxelGrid
          Of the given shape with possibly non-uniform
          scale and translation transformation matrix.
        rˆ   r   )r,   )Útupler   rš   rb   r   Úgrid_linspaceÚreshapert   r   rl   r   ÚtrÚscale_and_translate)r+   r=   r   rb   r…   r€   rG   Ú	translates           r-   ÚrevoxelizedzVoxelGrid.revoxelized‘  s�   € ô  �e“ˆØ—‘×!Ñ!Ó#ˆØ—,‘,ˆÜ×#Ñ# F¨EÓ2×:Ñ:¸5À4¹<ÓHˆØ—‘˜vÓ&ˆØœ"Ÿ-™-¨Ó.Ñ.ˆØ˜1‘Iˆ	Ü˜¬"×*@Ñ*@ÀÈ	Ó*RÔSÐSr/   c                 ó   — t        d«      ‚)Nz$TODO : implement voxel concatenation)ÚNotImplementedError)r+   Úothers     r-   Ú__add__zVoxelGrid.__add__ª  s   € Ü!Ð"HÓIÐIr/   )NN)Úholesr4   ))Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   r&   ÚpropertyrŸ   r8   r"   Úsetterr,   rE   rG   rI   rL   rN   rS   r   Úcache_decoratorr   rb   rd   r=   ri   rt   rw   r{   r~   rA   r‚   r…   rW   r�   rm   rQ   r—   rš   r¨   r°   r´   © r/   r-   r   r      s  „ ñóUò&	%ð ð9 ò 9ó ð9ð ñ&ó ð&ð ‡_�_ñ	*ó ð	*ð ñ&ó ð&ð ×Ññ(ó ð(ð ñ+ó ð+ð ñ%ó ð%ð ñ%ó ð%ð$ ñ+ó ð+òòð ×Ññó ðð ×Ññó ðð ×Ññ&ó ð&ð ñ#ó ð#ð ×Ññ(ó ð(òó2ò,ð" ×Ññ@ó ð@ð  ñ
#ó ð
#ð ×Ññ
7ó ð
7ð ×Ññ	Só ð	Sð ñ,ó ð,ó#òJ,òGòOòNóò<Tó2Jr/   r   )r¹   Úhashlibr   Únumpyr   Ú r   rX   r   r   r   r­   Ú	constantsr	   Úexchange.binvoxr
   Úparentr   r   r   r   r"   r   r   r   r½   r/   r-   Ú<module>rÄ      s=   ðñõ ã å &ß Ý $Ý Ý +Ý ß )Ñ )ß -ôUJ�õ UJr/   