
    MiH                        S r SSKrSSKrSSKrSSKJr  SSKrSSKJ	r	  SSK
Jr  SSK	JrJr  \	R                  " S5      r\R                   " S	/ S
Q5      rS rSS jrS rSS jrSS jrSS jrSS jr " S S5      rSS jrS\0rg)z
Parsing functions for Binvox files.

https://www.patrickmin.com/binvox/binvox.html

Exporting meshes as binvox files requires the
`binvox` executable to be in your path.
    N)TemporaryDirectory   )util)Trimesh)comment_stripdecode_textbinvoxBinvox)rle_datashape	translatescalec                 Z   [        U R                  5       5      R                  5       nSUR                  5       ;  a  [	        S5      e0 nSn[        S5       Hx  n[        [        U R                  5       5      R                  5       S5      R                  5       R                  5       n[        U5      S:X  a  Ma  US   S:X  a  Sn  OUS	S
 X%S   '   Mz     U(       d  [	        S5      eSUR                  5       ;  a  [	        SUR                  5        S35      e[        R                  " US   [        R                  S9n[        R                  " UR                  S/ SQ5      [        R                  5      n[        R                  " UR                  SS	/5      [        R                  S9nXgUS   4$ )at  
Read the header from a binvox file.
Spec available:
https://www.patrickmin.com/binvox/binvox.html

Parameters
------------
fp: file-object
  File like object with binvox file

Returns
----------
shape : tuple
  Shape of binvox according to binvox spec
translate : tuple
  Translation
scale : float
  Scale of voxels

Raises
------------
IOError
  If invalid binvox file.
r	   z!File is not in the binvox format!Fd   #r   dataT   Nz1Didn't reach header termination magic word `data`dimz;Malformed binvox header: `dim` is required, only received ``dtyper   r   r   r   r   )r   readlinestriplower
ValueErrorranger   splitlenkeysnparrayint64getfloat64)	file_objfirstheaderreached_data_liner   r   r   s	            q/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/exchange/binvox.py_parse_binvox_headerr-      so   6 ))+,224Eu{{}$<==FL3Z +h&7&7&9:@@BCHNNPVVX 	 t9>!W  L qr(Aw! $ LMMFKKM!I&++-XYZ
 	

 HHVE]"((3E K;RZZHIHHVZZ!-RZZ@EU1X%%    c                     [        U 5      u  p#nU R                  5       n[        R                  " U[        R                  S9nU(       a  UR                  5       n[        XbX45      $ )a4  
Read a binvox file, spec at
https://www.patrickmin.com/binvox/binvox.html

Parameters
------------
fp: file-object
  File like object with binvox file

Returns
----------
binvox : namedtuple
  Containing data
rle : numpy array
  Run length encoded data

Raises
------------
IOError
  If invalid binvox file
r   )r-   readr!   
frombufferuint8copyr
   )fp	writeabler   r   r   r   r   s          r,   parse_binvoxr6   `   sM    . 326Ee779D}}T2H==?(944r.   c                 T    S U  5       u  p4nUu  pgnSU SU SU SU SU SU SU S3$ )a/  
    Get a binvox header string.

    Parameters
    --------
    shape: length 3 iterable of ints denoting shape of voxel grid.
    translate: length 3 iterable of floats denoting translation.
    scale: num length of entire voxel grid.

    Returns
    --------
    string including "data
" line.
    c              3   8   #    U  H  n[        U5      v   M     g 7fN)int).0ss     r,   	<genexpr> binvox_header.<locals>.<genexpr>   s     (%Q#a&&%   z'#binvox 1
# generated in `trimesh`
dim  z
translate z
scale z
data
 )	r   r   r   sxsysztxtytzs	            r,   binvox_headerrH      s`     )%(JBBJBBD"Qrd $at1RD g 	 r.   c                     U R                   [        R                  :w  a  [        SU R                    35      e[	        XU5      R                  5       nX@R                  5       -   $ )aC  Get a binary representation of binvox data.

Parameters
--------
rle_data : numpy array
  Run-length encoded numpy array.
shape : (3,) int
  Shape of voxel grid.
translate : (3,) float
  Translation of voxels
scale : float
  Length of entire voxel grid.

Returns
--------
data : bytes
  Suitable for writing to binary file
z%rle_data.dtype must be np.uint8, got )r   r!   r2   r   rH   encodetobytes)r   r   r   r   r(   s        r,   binvox_bytesrL      sS    & ~~!@@PQRR5U3::<F$$&&&r.   c                   ^ SSK Jn  SSKJn  SSKJn  [        XR                  5      (       a  U nOUR                  U [        S9nUR                  U[        R                  " T5      S-
  -  US9n	US:X  a8  S	n
[        U4S
 jU
 5       5      mUR                  T5      R                  U
5      nO'Ub  US:X  a  UR                  T5      nO[        SU5      eUR                   T:X  d   eU" X5      $ )a  
Factory for building from data associated with binvox files.

Parameters
---------
rle_data : numpy
  Run-length-encoded of flat voxel
  values, or a `trimesh.rle.RunLengthEncoding` object.
  See `trimesh.rle` documentation for description of encoding
shape : (3,) int
  Shape of voxel grid.
translate : (3,) float
  Translation of voxels
scale : float
  Length of entire voxel grid.
encoded_axes : iterable
  With values in ('x', 'y', 'z', 0, 1, 2),
  where x => 0, y => 1, z => 2
  denoting the order of axes in the encoded data. binvox by
  default saves in xzy order, but using `xyz` (or (0, 1, 2)) will
  be faster in some circumstances.

Returns
---------
result : VoxelGrid
  Loaded voxels
r   )transformations)encoding)	VoxelGridr   r   )r   r   xzyr   r   r   c              3   .   >#    U  H
  nTU   v   M     g 7fr9   rA   )r;   pr   s     r,   r=   $voxel_from_binvox.<locals>.<genexpr>   s     -1eAhs   xyzz5Invalid axis_order '%s': must be None, 'xyz' or 'xzy') rN   voxelrO   
voxel.baserP   
isinstanceRunLengthEncodingboolscale_and_translater!   r"   tuplereshape	transposer   r   )r   r   r   r   
axis_orderrN   encrP   rO   	transformperms    `         r,   voxel_from_binvoxre      s    : #'&(1122(((>  33rxx*+y 4 I U---##E*44T:		zU2##E*CZ
 	
 >>U"""X))r.   c                     Ub  US:w  a  [        SU 35      e[        U SS9n[        UR                  UR                  UR
                  UR                  US9$ )aV  
Load trimesh `VoxelGrid` instance from file.

Parameters
-----------
file_obj : file-like object
  Contains binvox data
resolver : unused
axis_order : str
  Order of axes in encoded data.
  Binvox default is 'xzy', but 'xyz' may be faster
  where this is not relevant.

Returns
---------
result : trimesh.voxel.VoxelGrid
  Loaded voxel data
r	   z&file_type must be None or binvox, got T)r5   )r   r   r   r   ra   )r   r6   re   r   r   r   r   )r&   resolverra   	file_typer   s        r,   load_binvoxri      s\    & h!6A)MNND1Djj..jj r.   c                 R   U R                   nU R                  [        R                  " U R                  5      S-
  -  n[        R
                  " US:  5      u  nU R                  R                  U5      n[        R                  " U5      n[        R                  " US   USS SUS   -  S-   5      (       d  [        S5      eUS   nUS:X  a  UR                  S5      nOUS	:w  a  [        S
5      eUR                  R                  [        R                  S9n[!        X`R                  X#S9$ )a  
Export `trimesh.voxel.VoxelGrid` instance to bytes

Parameters
------------
voxel : `trimesh.voxel.VoxelGrid`
  Assumes axis ordering of `xyz` and encodes
  in binvox default `xzy` ordering.
axis_order : str
  Eements in ('x', 'y', 'z', 0, 1, 2), the order
  of axes to encode data (standard is 'xzy' for binvox). `voxel`
  data is assumed to be in order 'xyz'.

Returns
-----------
result : bytes
  Representation according to binvox spec
r   r   Ngư>g:0yE>z)Can only export binvox with uniform scalerQ   rR   rV   z1Invalid axis_order: must be one of ("xyz", "xzy")r   )r   r   r   )translationr   r!   r"   r   whererO   flipabsr   allcloser   r`   flatrun_length_datar2   rL   )rX   ra   r   r   	neg_scalerO   r   s          r,   export_binvoxrs     s    & !!IKK288EKK0145E88EAI&LY~~""9-HFF5ME==q59dU1Xo.DEEDEE!HEU%%i0	u	LMM}},,288,<HyVVr.   c                   l    \ rS rSrSrSrSr                    S
S jr\S 5       r	SS jr
S	rg)Binvoxeri4  a  
Interface for binvox CL tool.

This class is responsible purely for making calls to the CL tool. It
makes no attempt to integrate with the rest of trimesh at all.

Constructor args configure command line options.

`Binvoxer.__call__` operates on the path to a mode file.

If using this interface in published works, please cite the references
below.

See CL tool website for further details.

https://www.patrickmin.com/binvox/

@article{nooruddin03,
    author = {Fakir S. Nooruddin and Greg Turk},
    title = {Simplification and Repair of Polygonal Models Using Volumetric
             Techniques},
    journal = {IEEE Transactions on Visualization and Computer Graphics},
    volume = {9},
    number = {2},
    pages = {191--205},
    year = {2003}
}

@Misc{binvox,
    author = {Patrick Min},
    title =  {binvox},
    howpublished = {{       t http://www.patrickmin.com/binvox} or
                    {       t https://www.google.com/search?q=binvox}},
    year =  {2004 - 2019},
    note = {Accessed: yyyy-mm-dd}
}
)	ugobjoffdfxxglpovbrepplyjot)r	   hipsmiravtkraw	schematicmshNc                 P   Uc  [         nOUnUc  [        SR                  / SQ5      5      eUS:  a  U(       d  [        SU5      eU[        R
                  ;  a   [        SU S[        R
                  < 35      eUS[        U5      S	U/nU(       a  UR                  S
5        U(       a  U(       a  O6UR                  S5        O$U(       a  UR                  S5        O[        S5      eU(       a  UR                  S5        UbC  [        U5      S:w  a  [        S5      eUR                  S5        UR                  S U 5       5        U(       a  UR                  S5        U	(       a  UR                  S5        UR                  SU
-  5        UR                  SU-  5        U(       a  UR                  S5        U(       a  UR                  S5        Ub  UR                  SU45        U(       a  UR                  S5        U(       a  UR                  S5        UbN  [        R                  " U5      n[        U5      U:w  a  [        SU5      eUR                  S[        U5      -  5        Ub  UR                  S[        U5      45        UR                  S5        UU l        X l        UU l        g) a\  
Configure the voxelizer.

Parameters
------------
dimension: voxel grid size (max 1024 when not using exact)
file_type: str
  Output file type, supported types are:
    'binvox'
    'hips'
    'mira'
    'vtk'
    'raw'
    'schematic'
    'msh'
z_buffer_carving : use z buffer based carving. At least one of
    `z_buffer_carving` and `z_buffer_voting` must be True.
z_buffer_voting: use z-buffer based parity voting method.
dilated_carving: stop carving 1 voxel before intersection.
exact: any voxel with part of a triangle gets set. Does not use
    graphics card.
bounding_box: 6-element float list/tuple of min, max values,
    (minx, miny, minz, maxx, maxy, maxz)
remove_internal: remove internal voxels if True. Note there is some odd
    behaviour if boundary voxels are occupied.
center: center model inside unit cube.
rotate_x: number of 90 degree ccw rotations around x-axis before
    voxelizing.
rotate_z: number of 90 degree cw rotations around z-axis before
    voxelizing.
wireframe: also render the model in wireframe (helps with thin parts).
fit: only write voxels in the voxel bounding box.
block_id: when converting to schematic, use this as the block ID.
use_matrial_block_id: when converting from obj to schematic, parse
    block ID from material spec "usemtl blockid_<id>" (ids 1-255 only).
use_offscreen_pbuffer: use offscreen pbuffer instead of onscreen
    window.
downsample_factor: downsample voxels by this factor in each dimension.
    Must be a power of 2 or None. If not None/1 and `core dumped`
    errors occur, try slightly adjusting dimensions.
downsample_threshold: when downsampling, destination voxel is on if
    more than this number of voxels are on.
verbose : bool
  If False, silences stdout/stderr from subprocess call.
binvox_path : str
  Path to binvox executable. The default looks for an
  executable called `binvox` on your `PATH`.
Nr@   )z8No `binvox_path` provided and no binvox executable foundz<on PATH, please go to https://www.patrickmin.com/binvox/ andz!download the appropriate version.i   z-Maximum dimension using exact is 1024, got %d
file_type z& not in set of supported output types z-dz-tz-ez-cz-vz;One of `z_buffer_carving` or `z_buffer_voting` must be Truez-dc   z!bounding_box must have 6 elementsz-bbc              3   8   #    U  H  n[        U5      v   M     g 7fr9   )str)r;   bs     r,   r=   $Binvoxer.__init__.<locals>.<genexpr>  s     51Ar?   z-riz-cb)z-rotx)z-rotzz-awz-fitz-biz-mbz-pbz.downsample_factor must be a power of 2, got %d)z-downz-dminPATH)binvox_encoderOSErrorjoinr   ru   SUPPORTED_OUTPUT_TYPESr   appendr   extendr!   log2r:   _args
_file_typeverbose)self	dimensionrh   z_buffer_carvingz_buffer_votingdilated_carvingexactbounding_boxremove_internalcenterrotate_xrotate_z	wireframefitblock_iduse_material_block_iduse_offscreen_pbufferdownsample_factordownsample_thresholdr   binvox_pathencoderargstimess                           r,   __init__Binvoxer.__init__q  sH   N $G!G?  tELiXXH;;;YK'MhNmNmMpq  s9~tY?KKD!KKM  KK #< A% !DEEKKKK555KKKKJ)*J)*KKKKKK)* KK KK(GG-.E5zU" DFW  KK
SZ/0+KK#&:";<=F
#r.   c                     U R                   $ r9   )r   )r   s    r,   rh   Binvoxer.file_type  s    r.   c                 :   [         R                  R                  U5      u  p4USS R                  5       nU[        R
                  ;  a   [        SU S[        R
                  < 35      eU SU R                   3n[         R                  R                  U5      (       a  U(       d  [        S5      eXR                  S'   [        R                  " U R                  [        R                  S9nU R                  (       a  [        R                   R#                  U5        U$ )	a  
Create an voxel file in the same directory as model at `path`.

Parameters
------------
path: string path to model file. Supported types:
    'ug'
    'obj'
    'off'
    'dfx'
    'xgl'
    'pov'
    'brep'
    'ply'
    'jot' (polygongs only)
overwrite: if False, checks the output path (head.file_type) is empty
    before running. If True and a file exists, raises an IOError.

Returns
------------
string path to voxel file. File type give by file_type in constructor.
r   Nr   z% not in set of supported input types .z-Attempted to voxelize object at existing path)stderr)ospathsplitextr   ru   SUPPORTED_INPUT_TYPESr   r   isfiler   r   
subprocesscheck_outputSTDOUTr   r   logdebug)r   r   	overwriteheadextout_path	verbositys          r,   __call__Binvoxer.__call__  s    . GG$$T*	!"gmmoh444SE!FxGeGeFhi  V1T__-.77>>(##IIJJ

2 ++DJJz?P?PQ	 <<HHNN9%r.   )r   r   r   )    r	   TTFTNFFr   r   FFNFFNNFNF)__name__
__module____qualname____firstlineno____doc__r   r   r   propertyrh   r   __static_attributes__rA   r.   r,   ru   ru   4  sv    $L
 ##!+Qf  *r.   ru   c                 B   [        U [        5      (       d  [        SU < 35      eUc  [        S0 UD6nO[	        U5      S:  a  [        S5      eUR
                  S:w  a  [        S5      e[        5        n[        R                  R                  USU 35      n[        US5       nU R                  XbS	9  SSS5        U" U5      n[        US
5       n[        U5      nSSS5        SSS5        W$ ! , (       d  f       N@= f! , (       d  f       N)= f! , (       d  f       W$ = f)aK  
Interface for voxelizing Trimesh object via the binvox tool.

Implementation simply saved the mesh in the specified export_type then
runs the `Binvoxer.__call__` (using either the supplied `binvoxer` or
creating one via `binvoxer_kwargs`)

Parameters
------------
mesh: Trimesh object to voxelize.
binvoxer: optional Binvoxer instance.
export_type: file type to export mesh as temporarily for Binvoxer to
    operate on.
**binvoxer_kwargs: kwargs for creating a new Binvoxer instance. If binvoxer
    if provided, this must be empty.

Returns
------------
`VoxelGrid` object resulting.
z#mesh must be Trimesh instance, got Nr   z+Cannot provide binvoxer and binvoxer_kwargsr	   z6Only "binvox" binvoxer `file_type` currently supportedzmodel.wb)rh   rbrA   )rZ   r   r   ru   r   rh   r   r   r   r   openexportri   )	meshbinvoxerexport_typebinvoxer_kwargsfolder
model_pathr4   r   	out_models	            r,   voxelize_meshr   5  s    * dG$$>thGHH.o.	_		!FGGX%QRR		WW\\&F;-*@A
*d#rKKK2 $J'(D!R#BI " 
  $# "! 
	 s<   30D#C-3DC>D-
C;	7D>
D	D
Dr   )r   r   )Ng      ?rQ   )NrQ   N)rQ   )Nrx   )r   collectionsr   r   tempfiler   numpyr!   rW   r   baser   r   r   whichr   
namedtupler
   r-   r6   rH   rL   re   ri   rs   ru   r   _binvox_loadersrA   r.   r,   <module>r      s     	  '    - H%			*U	VC&L5D4'49*x> WF~ ~B%P [)r.   