
    Miw1                         S r SSKJr  SSKrSSKJr  SSKJrJ	r	  SSKJ
r  SSKJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJrJr   " S S\5      rg)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                   n   \ rS rSrSrS(S jrS r\S\4S j5       r	\S 5       r
\
R                  S	 5       r
\S
 5       r\R                  S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rS r\R(                  S 5       r\R(                  S 5       r\R(                  S 5       r\S 5       r\R(                  S 5       rS rS)S jrS r\R(                  S 5       r\S 5       r\R(                  S 5       r\R(                  S 5       r \S 5       r!S*S jr"S  r#S! r$S" r%S# r&S(S$ jr'S% r(S& r)S'r*g)+	VoxelGrid   z
Store 3D voxels.
Nc                 b   Uc  [         R                  " S5      n[        U[         R                  5      (       a  [	        UR                  [        5      5      nUR                  [        :w  a  [        S5      e[        R                  " 5       U l        Xl        [        R                  " X R                  S9U l        [        R                   " U R                  R"                  S9U l        0 U l        [        U[(        5      (       a  U R&                  R+                  U5        g Ub  [        SU< 35      eg )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       l/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/voxel/base.py__init__VoxelGrid.__init__   s    q	Ih

++$X__T%:;H>>T!<==&&(
 $..yJJOmm

0C0CDh%%MM  *!FxlSTT "    c                 6    U R                   R                  5       $ )zu
Get the hash of the current transformation matrix.

Returns
------------
hash : str
  Hash of transformation matrix
)r"   r'   r,   s    r.   r'   VoxelGrid.__hash__.   s     zz""$$r1   returnc                 b    [        [        U 5      R                  5       5      R                  5       $ N)r   hashto_bytes	hexdigestr3   s    r.   identifier_hashVoxelGrid.identifier_hash9   s#    d4j))+,6688r1   c                      U R                   S   $ )zd
`Encoding` object providing the occupancy grid.

See `trimesh.voxel.encoding` for implementations.
r#   )r"   r3   s    r.   r#   VoxelGrid.encoding=   s     zz*%%r1   c                 z   [        U[        R                  5      (       a  [        U5      nO$[        U[        5      (       d  [        SU< 35      e[        UR                  5      S:w  a  [        SUR                  < 35      eUR                  [        :w  a  [        SUR                   35      eXR                  S'   g )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#   r>   F   s    h

++$X.HHh//A(NOOx~~!#B8>>BTUVV>>T!<X^^<LMNN!)

:r1   c                 .    U R                   R                  $ )&4x4 homogeneous transformation matrix.r%   matrixr3   s    r.   r-   VoxelGrid.transformR   s     %%%r1   c                 $    XR                   l        g)rD   NrE   r,   rF   s     r.   r-   rG   W   s     "(r1   c                 .    U R                   R                  $ )zLocation of voxel at [0, 0, 0].)r%   translationr3   s    r.   rK   VoxelGrid.translation\   s     ***r1   c                 .    U R                   R                  $ )z}
3-element float representing per-axis scale.

Raises a `RuntimeError` if `self.transform` has rotation or
shear components.
)r%   scaler3   s    r.   rN   VoxelGrid.scalea   s     $$$r1   c                 .    U R                   R                  $ )z
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%   pitchr3   s    r.   rQ   VoxelGrid.pitchk   s    " $$$r1   c                 .    U R                   R                  $ r7   )r%   unit_volumer3   s    r.   element_volumeVoxelGrid.element_volume~   s    ***r1   c                 <    U R                   R                  U5        U $ r7   )r%   apply_transformrI   s     r.   rX   VoxelGrid.apply_transform   s    ''/r1   c                     U R                   R                  u  pXl         U R                  USS2S4   5      U R                  R                  SS2S4'   U $ )zr
Mutate self by stripping leading/trailing planes of zeros.

Returns
--------
self after mutation occurs in-place
Nr   r@   )r#   strippedindices_to_pointsr%   rF   )r,   r#   paddings      r.   stripVoxelGrid.strip   sO     !MM22 (,(>(>wq!t}(Mrr1u%r1   c                 J   U R                   n[        R                  " UR                  SS9S-
  UR	                  SS9S-   /5      nU R
                  R                  U5      n[        R                  " UR                  SS9UR	                  SS9/5      nSUR                  l
        U$ )Nr   axisg      ?F)sparse_indicesbounds_modulecornersminmaxr%   transform_pointsr   arrayflags	writeable)r,   indicesre   r   s       r.   r   VoxelGrid.bounds   s    %%''[[a[ 3&(;c(AB
 //227;7;;A;.0CDE!&r1   c                 V    U R                   nUS   US   -
  nSUR                  l        U$ )Nr   r   F)r   rj   rk   )r,   r   extentss      r.   ro   VoxelGrid.extents   s.    )fQi'"'r1   c                 .    U R                   R                  $ r7   )r#   is_emptyr3   s    r.   rr   VoxelGrid.is_empty   s    }}%%%r1   c                 .    U R                   R                  $ )z13-tuple of ints denoting shape of occupancy grid.)r#   rB   r3   s    r.   rB   VoxelGrid.shape   s     }}"""r1   c                 J    U R                   R                  R                  5       $ )z+int, number of occupied voxels in the grid.)r#   sumitemr3   s    r.   filled_countVoxelGrid.filled_count   s     }}  %%''r1   c                    [         R                  " U5      nU R                  U5      n[         R                  " [         R                  " U[         R
                  " U R                  5      :  SS9[         R                  " US:  SS95      n[         R                  " U5      nU R                  R                  X#   5      XC'   U$ )z
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
ra   r   )
r   
asanyarraypoints_to_indiceslogical_andallri   rB   
zeros_liker#   	gather_nd)r,   pointrl   in_range	is_filleds        r.   r   VoxelGrid.is_filled   s     e$((/>>FF7RXXdjj11;RVVGqLWY=Z
 MM(+	"mm55g6GH	r1   c                 V    [         R                  " U R                  4SU0UD6U l        U $ )ag  
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,   r   kwargss      r.   r   VoxelGrid.fill   s'    & #OfOOr1   c                 P    [         R                  " U R                  5      U l        U $ )a  
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#   r3   s    r.   hollowVoxelGrid.hollow   s     #**4==9r1   c                 >    [         R                  " U R                  S9$ )ab  
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.
)rF   )r   matrix_to_marching_cubesrF   r3   s    r.   marching_cubesVoxelGrid.marching_cubes   s     ++4;;??r1   c                 .    U R                   R                  $ )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#   denser3   s    r.   rF   VoxelGrid.matrix  s     }}"""r1   c                 4    U R                   U R                  -  $ )z
What is the volume of the filled cells in the current
voxel object.

Returns
---------
volume : float
  Volume of filled cells.
)ry   rU   r3   s    r.   volumeVoxelGrid.volume  s       4#6#666r1   c                 r    U R                   R                  U R                  R                  [        5      5      $ )z|
The center of each filled cell as a list of points.

Returns
----------
points : (self.filled, 3) float
  Points in space.
)r%   rh   rc   r   floatr3   s    r.   pointsVoxelGrid.points!  s*     //0C0C0J0J50QRRr1   c                 .    U R                   R                  $ )z6(n, 3) int array of sparse indices of occupied voxels.)r#   rc   r3   s    r.   rc   VoxelGrid.sparse_indices-  s     }}+++r1   c                    Ub  [         R                  " U5      nUR                  S:X  aQ  U R                  nUR                  SS UR                  :X  a  XR
                     nOA[        R                  " S5        SnO(UR                  S;  a  [        R                  " S5        Sn[        R                  " U R                  R                  [        5      US9nUR                  U R                  5      nU$ )a1  
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   r}   ndimr#   rB   r   r	   warningr   multiboxrc   r   r   rX   r-   )r,   r   r   r#   meshs        r.   as_boxesVoxelGrid.as_boxes2  s    " ]]6*F{{a==<<#x~~5 $NN3FKK 9:!F\156||D$7$7$>$>u$EfU##DNN3r1   c                     U R                   R                  U5      n[        R                  " U5      R	                  [
        5      $ )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.   r~   VoxelGrid.points_to_indicesW  s2     99&Axx&&s++r1   c                 ^    U R                   R                  UR                  [        5      5      $ r7   )r%   rh   r   r   )r,   rl   s     r.   r\   VoxelGrid.indices_to_pointsf  s     //u0EFFr1   c                 d    U R                  UR                  SS5      5      R                  " U0 UD6$ )zt
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,   argsr   s      r.   r   VoxelGrid.showi  s.    
 }}VZZ$78==tNvNNr1   c                     [        U R                  R                  5       U R                  R                  R                  5       5      $ r7   )r   r#   copyr%   rF   r3   s    r.   r   VoxelGrid.copyp  s/    ++-t/E/E/J/J/LMMr1   c                    [        U[        5      (       a'  Uc$  [        R                  " U5      R	                  5       nUS:w  a  [        S5      e[        U 40 UD6n[        US5      (       a  UR                  U5        U$ [        U[        5      (       a(  [        US5       nUR                  U5        SSS5        U$ U$ ! , (       d  f       U$ = f)z
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_typer   exportedfs         r.   exportVoxelGrid.exports  s      h$$):,,X6<<>I =>> 008W%%NN8$  #&&h%! &x &%s   B==
Cc                 N   [        U5      nU R                  R                  5       nU R                  n[        R
                  " X!5      R                  US-   5      nU R                  U5      nU[        R                  " U5      -  nUS   n[        U[        R                  " Xg5      S9$ )a  
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   ro   r   grid_linspacereshaper   r   r}   r   trscale_and_translate)r,   rB   r   ro   r   r   rN   	translates           r.   revoxelizedVoxelGrid.revoxelized  s      e!!#,,##F2::54<Hv&"--..1I	"*@*@*RSSr1   c                     [        S5      e)Nz$TODO : implement voxel concatenation)NotImplementedError)r,   others     r.   __add__VoxelGrid.__add__  s    !"HIIr1   )r(   r"   r%   r#   r)   )NN)holesr7   )+__name__
__module____qualname____firstlineno____doc__r/   r'   propertyr   r;   r#   setterr-   rK   rN   rQ   rU   rX   r^   r   cache_decoratorr   ro   rr   rB   ry   r   r   r   r   rF   r   r   rc   r   r~   r\   r   r   r   r   r   __static_attributes__ r1   r.   r   r      s   U&	% 9 9 9 & & __	* 	* & & ( ( + + % % % %$ + +     & & # # ( (2," @ @  
# 
# 
7 
7 	S 	S , ,#J,GON<T2Jr1   r   )r   hashlibr   numpyr    r   rd   r   r   r   r   	constantsr	   exchange.binvoxr
   parentr   r   r   r   r#   r   r   r   r   r1   r.   <module>r      s=      &  $  +  ) ) -UJ UJr1   