
    j&                         d dl ZddlmZ ddlmZmZ  G d de      Z ej                  g d      Z
 ej                  dej                  d	fd
g      ZdedefdZdedefdZdedefdZdefdZdefdZeedZy)    N   )util)DictStreamc                       e Zd Zy)HeaderErrorN)__name__
__module____qualname__     A/DATA/.local/lib/python3.12/site-packages/trimesh/exchange/stl.pyr   r      s    r   r   ))normals<f4   )verticesr   )r   r   )
attributesz<u2headerP   )
face_countz<u4file_objreturnc                     | j                         }	 t        |       S # t        $ r | j                  |       t	        |       cY S w xY w)a  
    Load a binary or an ASCII STL file from a file object.

    Parameters
    ----------
    file_obj
      Containing STL data

    Returns
    ----------
    loaded
      Keyword arguments for a Trimesh constructor with
      data loaded into properly shaped numpy arrays.
    )tellload_stl_binaryr   seekload_stl_ascii)r   kwargsfile_poss      r   load_stlr       sI      }}H(
 x(( (h h''(s   
 %AAc                    t         j                  }| j                  |      }t        |      |k  rt	        d      	 t        j                  |t               }	 dt        j                  t        |d   d               j                         i}| j                         }| j                  dd       | j                         }| j                  |       ||z
  }|d   t        j                  z  }||k7  rt	        d| d	|       t        j                  | j                         t              }	t        |	d
         sdi iS t        j                  |d   d   dz        j!                  d      }
|	d
   j!                  d      |	d   j!                  d      |
d|	d   i|d}|S # t        $ r t	        d      w xY w# t        $ r i }Y 5w xY w)a&  
    Load a binary STL file from a file object.

    Parameters
    ----------
    file_obj : open file- like object
      Containing STL data

    Returns
    ----------
    loaded
      Keyword arguments for a Trimesh constructor with data
      loaded into properly shaped numpy arrays.
    z'Binary STL shorter than a fixed header!dtypezBinary header incorrect typer   r   r   r   z+Binary STL has incorrect length in header: z vs r   geometryr   r   r   stlr   )r   face_normalsfacesface_attributesmetadata)_stl_dtype_headeritemsizereadlenr   np
frombufferBaseExceptionr   decode_textbytesstripr   r   
_stl_dtypearangereshape)r   header_lengthheader_datar   r+   
data_startdata_endlen_datalen_expectedblobr)   results               r   r   r   7   s   " &..M--.K
;-'CDD:{2CD d..uVH5Ea5H/IJPPRS J MM!Q }}HMM*
 *$H,'**=*==L <9(4~V
 	
 ==
;D tJ B IIf\*1-12::7CE
 $,,W5Y//8!4#56F Mo  :899:  s   F 4F3 F03GGc           	      p   t        j                  | j                               j                         }|j	                         }i }d}t        t        |            D ]  }|j                  d|      }|j                  d|      }|t        d      z   }|dk  s|dk  r n||kD  rt        d      ||| }|j                  d      }	t        j                  dj                  d |	dd	 D              dt        j                  
      }
t        |
      dk  rt        |
      dz  dk7  rt        d      |
j                  d      }
t        j                  t        |
            j                  d      }d	}	 |j                  d      }t        j                  dj                  d |dd	 D              dt        j                  
      }t        |      t        |
      k(  r|j                  d      }	 ||||j                  d      z    dd	 j                         }t        j&                  ||      }|
j                  d      ||d|id||<    t        |      dk(  r"t)        t+        |j-                                     S d|iS # t         $ r$ t         j"                  j%                  dd       Y w xY w# t         $ r d	}Y w xY w)a(  
    Load an ASCII STL file from a file object.

    Parameters
    ----------
    file_obj : open file- like object
      Containing input data

    Returns
    ----------
    loaded
      Keyword arguments for a Trimesh constructor with
      data loaded into properly shaped numpy arrays.
    r   solidendsolidz`endsolid` precedes `solid`!vertex c              3   D   K   | ]  }|d |j                  d         y wN
find.0lines     r   	<genexpr>z!load_stl_ascii.<locals>.<genexpr>   s!     IT+DIIdO,       N)sepr#   r   zincorrect number of verticesr%   normalc              3   D   K   | ]  }|d |j                  d         y wrG   rI   rK   s     r   rN   z!load_stl_ascii.<locals>.<genexpr>   s!     M_T/		$0_rO   zfailed to extract face_normalsT)exc_inforH      name)r   r(   r)   r+   r$   )r   r3   r.   r5   lowerranger/   rJ   
ValueErrorsplitr0   
fromstringjoinfloat64r8   r7   r2   logwarningunique_namenextitervalues)r   	raw_mixed	raw_lowerr   position_solid_start	solid_endrB   vertex_textr   r)   r(   normal_textr   rV   s                   r   r   r      s   "   1779I!I F H 3y>"nnWh7NN:x8	 s:. q=K!O ";<< +i0 kk(+==HHIQRII**

 x=1x=1!;<< ##G,		#h-(009 
	N++h/KmmM[_MMjjG
 7|s8},&w7	 [;D9I+IJ12NTTVD f- !((1(	
t{ #H 6{aD)**1  	NHH=M	N  	D	s%   .A8I7'(J'7*J$#J$'J54J5c                 z   t        j                  dt              }t        | d      rt	        | j
                        |d<   |j                         }t        | d      r_t        j                  t	        | j
                        t              }| j                  |d<   | j                  |d<   ||j                         z  }|S )z
    Convert a Trimesh object into a binary STL file.

    Parameters
    ---------
    mesh
      Trimesh object to export.

    Returns
    ---------
    export
      Represents mesh in binary STL form
    rP   r"   r)   r   r   r   )
r0   zerosr,   hasattrr/   r)   tobytesr6   r(   	triangles)meshr   exportpackeds       r   
export_stlrt      s     XXa01FtW"4::|^^FtW#djj/< --y!^^z&..""Mr   c                    t        j                  t        | j                        ddf      }| j                  |dddddf<   | j
                  |ddddddf<   dj                  g d      t        | j                        z  }| j                  j                  dd	      }t        |t              sd	}t        |      d
kD  sd|v rd	}dj                  d|  |j                  |j                  d       dg      S )z
    Convert a Trimesh object into an ASCII STL file.

    Parameters
    ---------
    mesh : trimesh.Trimesh

    Returns
    ---------
    export
      Mesh represented as an ASCII STL file
       r   Nr   rP   rH   )zfacet normal {} {} {}z
outer loopz/vertex {} {} {}
vertex {} {} {}
vertex {} {} {}endloopendfacet rV   ry   r   zsolid r&   z	endsolid
)r0   rm   r/   r)   r(   rp   r\   r+   get
isinstancestrformatr8   )rq   r?   	formatterrV   s       r   export_stl_asciir     s     88S_a+,D%%DAqM^^DABN 					
 	DJJI ==VR(DdC 
4y2~ 99tfo'7y'7'7b9I'JLYZZr   )r'   	stl_ascii)numpyr0   ry   r   typedr   r   	Exceptionr   r#   r6   voidr,   r    r   r   r4   rt   r|   r   _stl_loadersr   r   r   <module>r      s       	) 	 RXXQ
 BHHx"57LMN (v (D (@Of O Odc V c  c L 8)[c )[X  h7r   