
    j)                     x    d dl 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l
mZmZmZ  G d d	e	      Zd
 ZddZy)    N   )cachinggroupingutil   )color)Visuals)PBRMaterialSimpleMaterialempty_materialc                       e Zd ZddZd Zed        Zed        Zd Zed        Z	e	j                  d        Z	dd	Zd
 Zd Zd Zd Zd Zy)TextureVisualsNc                 $   t        j                         | _        t        j                  | j                  j                        | _        || _        |2|t               | _        || _
        yt        |      | _        || _
        y|| _        || _
        y)a  
        Store a single material and per-vertex UV coordinates
        for a mesh.

        If passed UV coordinates and a single image it will
        create a SimpleMaterial for the image.

        Parameters
        --------------
        uv : (n, 2) float
          UV coordinates for the mesh
        material : Material
          Store images and properties
        image : PIL.Image
          Can be passed to automatically create material
        N)image)r   	DataStorevertex_attributesCache__hash___cacheuvr   materialr   face_materials)selfr   r   r   r   s        C/DATA/.local/lib/python3.12/site-packages/trimesh/visual/texture.py__init__zTextureVisuals.__init__   s    & ")!2!2!4mmD$:$:$C$CD } . 0 - !/U ;
 - %DM,    c                 8    | j                   j                          y)z[
        Dump the cache if anything in self.vertex_attributes
        has changed.
        N)r   verifyr   s    r   _verify_hashzTextureVisuals._verify_hash2   s    
 	r   c                      y)z
        Return the type of visual data stored

        Returns
        ----------
        kind : str
          What type of visuals are defined
        texture r   s    r   kindzTextureVisuals.kind9   s     r   c                 "    | j                   du}|S )z
        Check if any data is stored

        Returns
        ----------
        defined : bool
          Are UV coordinates and images set?
        N)r   )r   oks     r   definedzTextureVisuals.definedE   s     ]]$&	r   c                 6    | j                   j                         S )z
        Get a CRC of the stored data.

        Returns
        --------------
        crc : int
          Hash of items in self.vertex_attributes
        )r   r   r   s    r   r   zTextureVisuals.__hash__R   s     %%..00r   c                 :    | j                   j                  dd      S )z
        Get the stored UV coordinates.

        Returns
        ------------
        uv : (n, 2) float or None
          Pixel position per-vertex.
        r   N)r   getr   s    r   r   zTextureVisuals.uv]   s     %%))$55r   c                     || j                   j                  d       yt        j                  |t        j                        | j                   d<   y)z
        Set the UV coordinates.

        Parameters
        --------------
        values : (n, 2) float or None
          Pixel locations on a texture per- vertex
        Nr   dtype)r   popnp
asanyarrayfloat64)r   valuess     r   r   zTextureVisuals.uvi   s<     >""&&t,+-==rzz+RD""4(r   c                     || j                   }||j                         }t        || j                  j                         t        j                  | j                              }|S )z
        Return a copy of the current TextureVisuals object.

        Returns
        ----------
        copied : TextureVisuals
          Contains the same information in a new object
        )r   r   r   )r   copyr   r   r   )r   r   copieds      r   r4   zTextureVisuals.copyx   sX     :B>B]]'')99T%8%89
 r   c                 |    | j                   j                  | j                        }t        j                  |      }|S )z
        Convert textured visuals to a ColorVisuals with vertex
        color calculated from texture.

        Returns
        -----------
        vis : trimesh.visuals.ColorVisuals
          Contains vertex color from texture
        )vertex_colors)r   to_colorr   r   ColorVisuals)r   colorsviss      r   r8   zTextureVisuals.to_color   s1     ''0  v6
r   c                     | j                   Xt        j                  | j                  j                  |   j                               }| j                  | j                   |         S | j                         S )z
        Get a copy of
        )r   r/   uniquemeshfacesflattenr4   )r   
face_indexindicess      r   face_subsetzTextureVisuals.face_subset   sV     77ii		
 ; C C EFG99TWWW-..99;r   c                 J   i }| j                   j                         D ]  \  }}t        |      dk(  r	 ||   ||<    | j                   j                          | j                   j                  |       y# t        $ r& t        j
                  j                  d| d       Y w xY w)z
        Apply a mask to remove or duplicate vertex properties.

        Parameters
        ------------
        mask : (len(vertices),) bool or (n,) int
          Mask which can be used like: `vertex_attribute[mask]`
        r   zfailed to update visual: ``N)	r   itemslenBaseExceptionr   logwarningclearupdate)r   maskupdateskeyvalues        r   update_verticeszTextureVisuals.update_vertices   s     00668JC5zQF$T{ 9 	$$&%%g. ! F  #=cU!!DEFs   A33,B"!B"c                      y)zq
        Apply a mask to remove or duplicate face properties,
        not applicable to texture visuals.
        Nr#   )r   rM   s     r   update_faceszTextureVisuals.update_faces   s    r   c                      ddl m}  || |      S )af  
        Concatenate this TextureVisuals object with others
        and return the result without modifying this visual.

        Parameters
        -----------
        others : (n,) Visuals
          Other visual objects to concatenate

        Returns
        -----------
        concatenated : TextureVisuals
          Concatenated visual objects
        r   )concatenate)objectsrU   )r   othersrU   s      r   rU   zTextureVisuals.concatenate   s     	)4((r   )NNNN)N)__name__
__module____qualname__r   r    propertyr$   r'   r   r   setterr4   r8   rC   rQ   rS   rU   r#   r   r   r   r      s    $-L 	 	 
 
	1 	6 	6 YYS S* /4)r   r   c                    |j                  dd      }|r| g}| j                         }|j                  t        j                  |dz                |D ]  }t        j
                  d|dz   fdt        j                        }t        t        d      | j                  |j                        D ]  \  }}	}
|
||   |	<    t        j                  |dk7  |t        j                        }|j                  t        j                  |d      j                  t        j                                |S t        j                  |       j                  d      g}|D ]5  }|j                  t        j                  |      j                  d             7 t        j                   |      }t#        j$                  |      \  }}||   }|d	d	df   j'                         }||   }t        j(                  t+        |      t        j                        }t        j                  t+        |            ||<   ||   j                  d| j,                  d   f      }|g}|j/                  |j                         |S )
a  
    Textured meshes can come with faces referencing vertex
    indices (`v`) and an array the same shape which references
    vertex texture indices (`vt`) and sometimes even normal (`vn`).

    Vertex locations with different values of any of these can't
    be considered the "same" vertex, and for our simple data
    model we need to not combine these vertices.

    Parameters
    -------------
    faces : (n, d) int
      References vertex indices
    *args : (n, d) int
      Various references of corresponding values
      This is usually UV coordinates or normal indexes
    maintain_faces : bool
      Do not alter original faces and return no-op masks.

    Returns
    -------------
    new_faces : (m, d) int
      New faces for masked vertices
    mask_v : (p,) int
      A mask to apply to vertices
    mask_* : (p,) int
      A mask to apply to vt array to get matching UV coordinates
      Returns as many of these as args were passed
    maintain_facesFr      r,   r   )axisN)r*   maxappendr/   arangefullint64ziprangeTwherenan	nanmedianastyper0   reshapecolumn_stackr   unique_rowsargsortzerosrG   shapeextend)r?   argskwargsr^   resultmax_idxargmasksifa	masks_nan	stackablestackr=   inversepairsorderremap	new_facess                       r   unmerge_facesr      s   @ ZZ 0%8N ))+bii!,- C GGQ!,bAEuQx#%%81aa 9 "eRVV<IMM",,yq9@@JK    u%--b12I s+33B78  OOI&E **51OFG &ME!Q$K!E%LE
 HHSZrxx0E99SZ(E%L g&&EKKN';<I [F
MM%''Mr   c                    t        j                  | j                  t         j                        }dt        j                  t        j
                  |            z  j                  t         j                        }|r6t        j                  dt         j                        |j                         z  }||k7  j                         r| j                  t        |      |      S | j                         S )aF  
    Resize a PIL image so every dimension is a power of two.

    Parameters
    ------------
    image : PIL.Image
      Input image
    resample : int
      Passed to Image.resize
    square : bool
      If True, upsize to a square image

    Returns
    -------------
    resized : PIL.Image
      Input image resized
    r,   r   )resample)r/   arraysizerf   ceillog2rm   onesrb   anyresizetupler4   )r   r   squarer   new_sizes        r   power_resizer   >  s    & 88EJJbhh/D RWWRWWT]++33BHH=H 771BHH-> 	||E(Oh|??::<r   )r   F)r4   numpyr/    r   r   r   r   baser	   r   r
   r   r   r   r   r   r#   r   r   <module>r      s8      & &   A AM)W M)``F r   