
    j%                        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j2                  eee      Z	 dd
ee   dedeej&                     fdZy)     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                  | j                  dddf      | j                  | j                  dddf      z
  d      j                         }t        t        t        j                  t        j                  ||z                    d      }t        j                  | j                  | j                  ||d      \  }}}t        j                  ||z        j                  t              }	t        j                  |	      \  }
}|	|
   }|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                  C/DATA/.local/lib/python3.12/site-packages/trimesh/voxel/creation.pyvoxelize_subdivider>      sT   . {"Hyy~~MM$**QT*+dmmDJJq!t<L.MMTU & 

#% 	 s277277<(+B#CDEqI **tzzHxVZKAr4 ((1u9

$
$S
)C  ++C0FH [N!%%1%-L"U*O>>  ,!>?((uP     pointradiusfillc                    ddl m} t        j                  |t        j                        j                  d      }t        |t              st        d      t        j                  ||dz   |z  z
  ||dz   |z  z   f      }t        | j                  j                  |            }t        |      dk(  ry| j                  |D 	cg c]  }	|	g c}	d	      }
|
j                  |        t!        |
|fi |}|j"                  dddf   }|j$                  j&                  }t        j(                  | |z        j+                  t        j,                        }t        j.                  ||z
  d      }t        j.                  ||z   d
z   |j0                  z
  d      }t        j2                  |t        j4                  ||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|j7                  |       \  }}|j9                  |       }t;        d
|d
z         D cg c]6  }t        j<                  |||k(  z  j?                         |j0                        8 }}| jA                  t        jB                  |      |z  |z         }t        jD                  |      d   d
z   }t        jF                  |jI                         |      j                  |j0                        }t        jJ                  ||      }tM        jN                  |tQ        jR                  |            S c c}	w c c}w )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      ?NT)appendr   r   constant)moder   )*scipyrD   r   
asanyarrayfloat64reshape
isinstancer$   
ValueErrorconcatenatelisttriangles_treeintersectionlensubmeshapply_translationr>   r   r   denser)   r*   int64maximumshapepadstacklabeldistance_transform_cdtrangeunravel_indexargmaxcontainsasarraywhereisinflatten
logical_orr   r-   r0   translation_matrix)r2   r@   r   rA   rB   kwargsrD   boundsr(   flocalvoxoriginmatrixcenterprepadpostpadvoxelslocal_originregionsndistanceirepresentativesrd   rf   internals                              r=   local_voxelizer}   D   s)   @  MM%rzz2::1=E fc"IJJ ^^	&3,%'	'&3,%1G)GHF
 $$11&9:E 5zQLLu-u!1#u-dL;E 
UF# UE
4V
4C]]2A2q5!F\\F XXvgo&--bhh7F ZZ+Fjj&1,v||;Q?GVVFBHHfg%6R@zRF
fF q	FVAY/!33q	FVAY/!33q	FVAY/!33	5F
 6E>)L ]]F7+
116': 1a!e_
$ h'Q,7??A8>>R$ 	 
 ==O!<u!D|!ST"1%)777??,e4<<W]]Kvx0>>&""7"7"EFFS .@
s   
M;M	per_cellc                    |(t        j                  ddgt         j                        }n4t        j                  |t         j                        j                  d      }t	        |      }| j
                  ddddf   j                         }|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                  j                  ||      d   }t        j                  ||z        j!                  t         j                        }	|	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   rE   r   g      ?r   )step)r   r   r   r   r   )r   arrayrZ   rO   floatrl   copyr   grid_arangeonesrV   column_stack	ones_likerayintersects_locationr)   r*   r,   r.   r/   r   r-   r0   r1   )r2   r   r~   rl   r   ray_orizray_dirhitsru   r;   r   r<   s                r=   voxelize_rayr      s   * 88QF"((388HBHH5==a@ %LE [[BQB$$&F
1I#.))I
1II8DvD1G
GQ!2U!:;Aoowl+Gll7#i/G 88''9!<D XXdUl#**2884F ::1:%L
lF''/H"U*O>>"((uP r?   	dimensionrl   c                 f   ddl m} |M|| j                  }n
|\  }}||z
  }t        t	        j
                  t	        j                  |      |z              }|5d|v rt        d      t	        j                  |      j                         |d<    |j                  dd|i|}	|j                  | |	      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   )binvoxbounding_boxz+Cannot provide both bounds and bounding_boxr    )trimesh.exchanger   extentsr$   r   r%   r#   rQ   rM   rh   Binvoxervoxelize_mesh)
r2   r   r   rl   binvoxer_kwargsr   r   minsmaxsbinvoxers
             r=   voxelize_binvoxr      s    H (>llGJD$TkGw% 789	_,JKK*,--*?*G*G*I'vFFoFHh//r?   )r   	subdivider   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.
    )r2   r   )
voxelizers)r2   r   r   rk   s       r=   voxelizer     s    > f?4u???r?   )
   g       @)T)N)NNN)r   )numpyr    r   r   r   r   r0   	constantsr   typedr	   r
   r   r   r   r   r   r.   r-   r>   boolr}   r   r   FunctionRegistryr   r   r   r?   r=   <module>r      s    % % $   Q Q   
QT55#+G#45HN5	^^5 
5z ^G^G ^G 	^G
 ^G dnn^GB 
9=::#+I#6:	^^: 
:z 
 ##'"&	30F30  30 Y	30 
^^30 
30l #T"" 2?
 '2@F@ $@
 dnn@r?   