
    Ti%                        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mZmZ ddlmZ dd	lmZ e		 ddedee         dedej        fd            Z	 d dededededeej                 f
dZe		 d!dedee         dej        fd            Ze		 	 	 d"dee         dee         dee         dej        fd            Z ej        eee          Z	 d#dee         dedeej                 fdZdS )$    N   )groupingremeshutil)transformations)log_time)	ArrayLikeIntegerNumberOptionalVoxelizationMethodsType   )base)encoding
          @pitchmax_iteredge_factorreturnc           
      
   ||z  }|t           j                            | j        | j        dddf                  | j        | j        dddf                  z
  d                                          }t          t          t          j        t          j        ||z                                d          }t          j
        | j        | j        ||d          \  }}}t          j        ||z                                t                    }	t          j        |	          \  }
}|	|
         }|                    d          }||z  }t#          j        t'          j        ||z
            t+          j        ||                    S )	a  
    Voxelize a surface by subdividing a mesh until every edge is
    shorter than: (pitch / edge_factor)

    Parameters
    -----------
    mesh : trimesh.Trimesh
      Source mesh
    pitch
      Side length of a single voxel cube
    max_iter
      Cap maximum subdivisions or None for no limit.
    edge_factor
      Proportion of pitch maximum edge length.

    Returns
    -----------
    VoxelGrid instance representing the voxelized mesh.
    Nr   r   axisT)max_edger   return_indexscale	translate)	transform)nplinalgnormverticesedgesmaxintceillog2r   subdivide_to_sizefacesroundastyper   unique_rowsminr   	VoxelGridencSparseBinaryEncodingtrscale_and_translate)meshr   r   r   r   longest_edgev_f_idxhitunique_inverseoccupied_indexorigin_indexorigin_positions                  P/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/voxel/creation.pyvoxelize_subdivider@      sv   . {"Hy~~M$*QQQT*+dmDJqqq!t<L.MMTU & 
 

#%% 	 s2727<(+B#C#CDDEEqII *tzHxVZ  KAr4 (1u9


$
$S
)
)C  +C00FH [N!%%1%--L"U*O> ,!>??(uPPP       Tpointradiusfillc                 l   ddl m} t          j        |t          j                                      d          }t          |t                    st          d          t          j	        ||dz   |z  z
  ||dz   |z  z   f          }t          | j                            |                    }t          |          dk    rdS |                     d |D             d	
          }	|	                    |            t!          |	|fi |}
|
j        dddf         }|
j        j        }t          j        | |z                                t          j                  }t          j        ||z
  d          }t          j        ||z   dz   |j        z
  d          }t          j        |t          j        ||fd          d          }||z  }||d         |z
  |d         |z   dz   |d         |z
  |d         |z   dz   |d         |z
  |d         |z   dz   f         }|||z  z
  }|r|                    |           \  }|                    |           fdt;          d|dz             D             }|                     t          j        |          |z  |z             }t          j         |          d         dz   }t          j!        "                                |                              j                  }t          j#        ||          }tI          j%        |tM          j'        |                    S )a  
    Voxelize a mesh in the region of a cube around a point. When fill=True,
    uses proximity.contains to fill the resulting voxels so may be meaningless
    for non-watertight meshes. Useful to reduce memory cost for small values of
    pitch as opposed to global voxelization.

    Parameters
    -----------
    mesh : trimesh.Trimesh
      Source geometry
    point : (3, ) float
      Point in space to voxelize around
    pitch
      Side length of a single voxel cube
    radius
      Number of voxel cubes to return in each direction.
    kwargs
      Parameters to pass to voxelize_subdivide

    Returns
    -----------
    voxels : VoxelGrid instance with resolution (m, m, m) where m=2*radius+1
        or None if the volume is empty
    r   )ndimagedtype   z.radius needs to be an integer number of cubes!g      ?Nc                     g | ]}|gS  rK   ).0fs     r?   
<listcomp>z"local_voxelize.<locals>.<listcomp>y   s    ---!1#---rA   T)appendr   r   constant)moder   c                 v    g | ]5}t          j        |k    z                                  j                  6S rK   )r    unravel_indexargmaxshape)rL   idistanceregionss     r?   rN   z"local_voxelize.<locals>.<listcomp>   sM     
 
 
 h'Q,7??AA8>RR
 
 
rA   )(scipyrF   r    
asanyarrayfloat64reshape
isinstancer&   
ValueErrorconcatenatelisttriangles_treeintersectionlensubmeshapply_translationr@   r   r   denser+   r,   int64maximumrV   padstacklabeldistance_transform_cdtrangecontainsasarraywhereisinflatten
logical_orr   r/   r2   translation_matrix)r4   rB   r   rC   rD   kwargsrF   boundsr*   localvoxoriginmatrixcenterprepadpostpadvoxelslocal_originnrepresentativesro   rq   internalrX   rY   s                          @@r?   local_voxelizer   D   s\   @  M%rz222::1==E fc"" KIJJJ ^	&3,%'	'&3,%1G)GH F
 $11&99::E 5zzQtLL--u---dL;;E 
UF### UE
4
4V
4
4C]2A2q5!F\F Xvgo&&--bh77F Z++Fj&1,v|;Q??GVFBHfg%6R@@@zRRRF
fF q	FVAY/!33q	FVAY/!33q	FVAY/!33	5F
 6E>)L  
1]]F7++
116'::
 
 
 
 
1a!e__
 
 
 ==O!<!<u!D|!STT""1%)77??,,e44<<W]KKvx00>&""7"E"EFFFrA   per_cellc                    |#t          j        ddgt           j                  }n3t          j        |t           j                                      d          }t	          |          }| j        ddddf                                         }|dxx         |d|z   z  z  cc<   |dxx         |z  cc<   ||z  }t          j        ||          }t          j	        t          |                    | j        d         d         |z
  z  }t          j        ||f          }t          j        |          g dz  }| j                            ||          d         }t          j        ||z                                t           j                  }	|	                    d	          }
|	|
z  }	t%          j        |	          }|
|z  }t)          j        |t-          j        ||
                    S )a=  
    Voxelize a mesh using ray queries.

    Parameters
    -------------
    mesh
      Mesh to be voxelized
    pitch
      Length of voxel cube
    per_cell : (2,) int
      How many ray queries to make per cell

    Returns
    -------------
    grid
      VoxelGrid instance representing the voxelized mesh.
    Nr   rG   r   g      ?r   )step)r   r   r   r   r   )r    arrayrh   r]   floatrw   copyr   grid_arangeonesrd   column_stack	ones_likerayintersects_locationr+   r,   r.   r0   r1   r   r/   r2   r3   )r4   r   r   rw   r   ray_orizray_dirhitsr   r=   r   r>   s                r?   voxelize_rayr      s   * 8QF"(3338HBH555==a@@ %LLE [BQB$$&&F
1III#.))III
1IIIIII8DvD111G
GQ!2U!:;Aowl++Gl7##iii/G 8''99!<D XdUl##**2844F ::1:%%L
lF'//H"U*O>"(uPPP  rA   	dimensionrw   c                 l   ddl m} |J|| j        }n
|\  }}||z
  }t          t	          j        t	          j        |          |z                      }|<d|v rt          d          t	          j        |          	                                |d<    |j
        dd|i|}	|                    | |	          S )a"  
    Voxelize via binvox tool.

    Parameters
    --------------
    mesh : trimesh.Trimesh
      Mesh to voxelize
    pitch : float
      Side length of each voxel. Ignored if dimension is provided
    dimension: int
      Number of voxels along each dimension. If not provided, this is
        calculated based on pitch and bounds/mesh extents
    bounds: (2, 3) float
      min/max values of the returned `VoxelGrid` in each instance. Uses
      `mesh.bounds` if not provided.
    **binvoxer_kwargs:
      Passed to `trimesh.exchange.binvox.Binvoxer`.
      Should not contain `bounding_box` if bounds is not None.

    Returns
    --------------
    grid
      `VoxelGrid` instance

    Raises
    --------------
    `ValueError` if `bounds is not None and 'bounding_box' in binvoxer_kwargs`.
    r   )binvoxNbounding_boxz+Cannot provide both bounds and bounding_boxr   rK   )trimesh.exchanger   extentsr&   r    r'   r%   r_   r[   rs   Binvoxervoxelize_mesh)
r4   r   r   rw   binvoxer_kwargsr   r   minsmaxsbinvoxers
             r?   voxelize_binvoxr      s    H ('''''>lGGJD$TkGw% 78899	_,,JKKK*,-*?*?*G*G*I*I'vFFFoFFHh///rA   )r   	subdivider   r   methodc                 "    t          |f| |d|S )a_  
    Voxelize the given mesh using the specified implementation.

    See `voxelizers` for available implementations or to add your own, e.g. via
    `voxelizers['custom_key'] = custom_fn`.

    `custom_fn` should have signature `(mesh, pitch, **kwargs) -> VoxelGrid`
    and should not modify encoding.

    Parameters
    --------------
    mesh
      Geometry to voxelize
    pitch
      Side length of each voxel.
    method
      Which voxelization method to use.
    kwargs
      Passed through to the specified implementation.

    Returns
    --------------
    grid
      A VoxelGrid instance.
    )r4   r   )
voxelizers)r4   r   r   rv   s       r?   voxelizer     s!    > f?4u?????rA   )r   r   )T)N)NNN)r   )numpyr     r   r   r   r   r2   	constantsr   typedr	   r
   r   r   r   r   r   r0   r/   r@   boolr   r   r   FunctionRegistryr   r   rK   rA   r?   <module>r      s       % % % % % % % % % % $ $ $ $ $ $             Q Q Q Q Q Q Q Q Q Q Q Q Q Q             
QT5 55#+G#45HN5	^5 5 5 
5z ^G ^G^G ^G 	^G
 ^G dn^G ^G ^G ^GB 
9=: ::#+I#6:	^: : : 
:z 
 ##'"&	30 30F30  30 Y	30 
^30 30 30 
30l #T" 2?  
 '2@ @F@ $@
 dn@ @ @ @ @ @rA   