
    j                     H    d dl ZddlmZmZ ddlmZ ddlmZ  G d d      Z	y)    N   )cachingutil)transformations)Optionalc                   Z   e Zd ZdZddeej                     fdZd Ze	d        Z
e	d        Zej                  d        Zej                  d	        Zej                  d
        Zej                  d        Zd Zd Zd Zd Zd Zej                  d        Zd Zej                  d        Zy)	Transformz
    Class for caching metadata associated with 4x4 transformations.

    The transformation matrix is used to define relevant properties
    for the voxels, including pitch and origin.
    N	datastorec                    t        j                  |t         j                        }|j                  dk7  st        j                  |dddf   g d      st        d      |t        j                         | _        n9t        |t        j                        r|| _        nt        t        |       d      || j                  d<   t        j                  | j                  j                  	      | _        y)
a  
        Initialize with a transform.

        Parameters
        -----------
        matrix : (4, 4) float
          Homogeneous transformation matrix
        datastore
          If passed store the actual values in a reference to
          another datastore.
        dtype   r      N)r   r   r      zmatrix is invalid!z != caching.DataStoretransform_matrix)id_function)np
asanyarrayfloat64shapeallclose
ValueErrorr   	DataStore_data
isinstancetypeCache__hash___cache)selfmatrixr
   s      E/DATA/.local/lib/python3.12/site-packages/trimesh/voxel/transforms.py__init__zTransform.__init__   s     vRZZ8<<6!VAqD\<)P122  **,DJ	7#4#45"DJY00EFGG)/

%&mm

0C0CD    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Transform.__hash__,   s     zz""$$r%   c                 .    | j                   d   dddf   S )z
        Get the translation component of the matrix

        Returns
        ------------
        translation : (3,) float
          Cartesian translation
        r   Nr   r   r'   s    r#   translationzTransform.translation7   s!     zz,-bqb!e44r%   c                      | j                   d   S )z
        Get the homogeneous transformation matrix.

        Returns
        -------------
        matrix : (4, 4) float
          Transformation matrix
        r   r)   r'   s    r#   r"   zTransform.matrixC   s     zz,--r%   c                     t        j                  |t         j                        }|j                  dk7  rt	        d      || j
                  d<   y)z
        Set the homogeneous transformation matrix.

        Parameters
        -------------
        matrix : (4, 4) float
          Transformation matrix
        r   r   zmatrix must be (4, 4)!r   N)r   r   r   r   r   r   )r!   valuess     r#   r"   zTransform.matrixO   s?     vRZZ8<<6!566)/

%&r%   c                     | j                   }t        j                  |ddddf         }t        j                  |ddddf   |t        j                  d      z  |dz  dz         st        d      |S )z
        Get the scale factor of the current transformation.

        Returns
        -------------
        scale : (3,) float
          Scale factor from the matrix
        Nr   ư>:0yE>z&transform features a shear or rotation)r"   r   diagr   eyeRuntimeError)r!   r"   scales      r#   r4   zTransform.scale^   sr     rr2A2v'{{6"1"bqb&>5266!9+<edlT>QRGHHr%   c           	          | j                   }t        j                  |d   |dd  t        j                  t        j
                  |            dz  dz         st        d      |S )Nr   r   r/   r0   z&transform features non-uniform scaling)r4   r   r   r   maxabsr3   r!   r4   s     r#   pitchzTransform.pitchp   sR    

}}U1XuQRy"&&2G$2NQU2UVGHHr%   c                 l    t         j                  j                  | j                  d   ddddf         S )z"Volume of a transformed unit cube.r   Nr   )r   linalgdetr   r'   s    r#   unit_volumezTransform.unit_volumew   s2     yy}}TZZ(:;BQBFCDDr%   c                 P    t        j                  || j                        | _        | S ).Mutate the transform in-place and return self.)r   matmulr"   )r!   r"   s     r#   apply_transformzTransform.apply_transform|   s    ii4r%   c                 <    | j                   dddfxx   |z  cc<   | S r?   Nr   r"   )r!   r*   s     r#   apply_translationzTransform.apply_translation   s#    BQBEk)r%   c                 6    | j                   ddxxx |z  ccc | S rC   rD   r8   s     r#   apply_scalezTransform.apply_scale   s    BQ5 r%   c                     | j                   r|j                         S t        j                  |j	                  dd      | j
                        j	                  |j                        S )a  
        Apply the transformation to points (not in-place).

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

        Returns
        ----------
        transformed : (n, 3) float
          Points transformed by matrix
        r   )is_identitycopytrtransform_pointsreshaper"   r   r!   pointss     r#   rM   zTransform.transform_points   sO     ;;= ""6>>"a#8$++FNNLL
 	
r%   c                     | j                   r|S t        j                  |j                  dd      | j                        j                  |j
                        S )z:Apply the inverse transformation to points (not in-place).rI   r   )rJ   rL   rM   rN   inverse_matrixr   rO   s     r#   inverse_transform_pointsz"Transform.inverse_transform_points   sH    M""6>>"a#8$:M:MNVVLL
 	
r%   c                 z    t         j                  j                  | j                        }d|j                  _        |S )NF)r   r;   invr"   flags	writeable)r!   rU   s     r#   rR   zTransform.inverse_matrix   s)    iimmDKK(#		
r%   c                 .    t        | j                        S )NrD   )r	   r"   r'   s    r#   rK   zTransform.copy   s    ,,r%   c                 j    t        j                  | j                  t        j                  d      d      S )zO
        Flags this transformation being sufficiently close to eye(4).
        r   r0   )r   r   r"   r   r2   r'   s    r#   rJ   zTransform.is_identity   s#    
 }}T[["&&)T::r%   )N)__name__
__module____qualname____doc__r   r   r   r$   r   propertyr*   r"   settercache_decoratorr4   r9   r=   rA   rE   rG   rM   rS   rR   rK   rJ    r%   r#   r	   r	      s   E(73D3D*E E8	% 	5 	5 	. 	. ]]0 0  "   E E



(
  
- ; ;r%   r	   )
numpyr    r   r   r   rL   typedr   r	   ra   r%   r#   <module>re      s      $ m; m;r%   