
    l0j                        d Z ddlZddlmZ ddlZddlm	Z
  G d de          Z G d de          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zd Zd Zd Zd Zd Zd Zd Zg dZ ee          Z ed eD                       Z g Z! e"            Z#eD ]a\  Z$Z%e$e#vr*e!&                    e$           e#'                    e$           e%e#vr*e!&                    e%           e#'                    e%           bd d!d"d#Z(d$d%d&Z)d!d"d'e
         Z*dS )(z5
NumPy-based PLY format input and output for Python.
    N)islice)	byteorderc                   ,   e Zd ZdZg ddg g fdZd Zd Z eee          Zd Z	d Z
 ee	e
          Zd	 Zd
 Zd Z eee          Zd Zd Z eee          Zed             Zedi fd            Zd Zed             Zd Zd Zd Zd Zd Zd ZdS )PlyDataa  
    PLY file header and data.

    A `PlyData` instance is created in one of two ways: by the static
    method `PlyData.read` (to read a PLY file), or directly from
    `__init__` given a sequence of elements (which can then be written
    to a PLY file).

    Attributes
    ----------
    elements : list of PlyElement
    comments : list of str
    obj_info : list of str
    text : bool
    byte_order : {'<', '>', '='}
    header : str
    F=c                 L    || _         || _        || _        || _        || _        dS )a  
        Parameters
        ----------
        elements : iterable of PlyElement
        text : bool, optional
            Whether the resulting PLY file will be text (True) or
            binary (False).
        byte_order : {'<', '>', '='}, optional
            `'<'` for little-endian, `'>'` for big-endian, or `'='`
            for native.  This is only relevant if `text` is False.
        comments : iterable of str, optional
            Comment lines between "ply" and "format" lines.
        obj_info : iterable of str, optional
            like comments, but will be placed in the header with
            "obj_info ..." instead of "comment ...".
        N)
byte_ordertextcommentsobj_infoelements)selfr   r
   r	   r   r   s         J/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/plyfile.py__init__zPlyData.__init__0   s+    $ %	       c                     | j         S N)	_elementsr   s    r   _get_elementszPlyData._get_elementsI   s
    ~r   c                 V    t          |          | _        |                                  d S r   )tupler   _index)r   r   s     r   _set_elementszPlyData._set_elementsL   s     xr   c                 B    | j         s| j        dk    rt          S | j        S )Nr   )r
   _byte_order_native_byte_orderr   s    r   _get_byte_orderzPlyData._get_byte_orderR   s(    y 	&T-44%%r   c                 :    |dvrt          d          || _        d S )N)<>r   z#byte order must be '<', '>', or '=')
ValueErrorr   r   r	   s     r   _set_byte_orderzPlyData._set_byte_orderW   s*    _,,BCCC%r   c                     t          d | j        D                       | _        t          | j                  t          | j                  k    rt	          d          d S )Nc              3   (   K   | ]}|j         |fV  d S r   name.0elts     r   	<genexpr>z!PlyData._index.<locals>.<genexpr>`   s8       $4 $4SXsO $4 $4 $4 $4 $4 $4r   ztwo elements with same name)dictr   _element_lookuplenr"   r   s    r   r   zPlyData._index_   si    # $4 $4$(N$4 $4 $4  4  4t#$$DN(;(;;;:;;; <;r   c                 *    t          | j                  S r   list	_commentsr   s    r   _get_commentszPlyData._get_commentse       DN###r   c                 L    t          |           t          |          | _        d S r   _check_commentsr2   r3   r   r   s     r   _set_commentszPlyData._set_commentsh   !    !!!hr   c                 *    t          | j                  S r   )r2   	_obj_infor   s    r   _get_obj_infozPlyData._get_obj_infon   r5   r   c                 L    t          |           t          |          | _        d S r   )r8   r2   r=   )r   r   s     r   _set_obj_infozPlyData._set_obj_infoq   r;   r   c                     t          t          |                     }t          d |j        D             |j        dk    t
          |j                 |j        |j                  S )Nc                      g | ]}t          | S  )
PlyElement)r*   es     r   
<listcomp>z)PlyData._parse_header.<locals>.<listcomp>{   s    555Z^555r   ascii)_PlyHeaderParser_PlyHeaderLinesr   r   format_byte_order_mapr   r   )streamparsers     r   _parse_headerzPlyData._parse_headerw   sZ    !/&"9"9::55V_555MW$FM*OO
 
 	
r   cc                    t          | d          \  }} 	 t                              |           }t          |                     d          t
                    r|j        r| }n.t          d          |j        rt          j	        | d          }n| }|D ]?}|
                    ||j        |j        ||                    |j        i                      @	 |r|                                  n# |r|                                  w w xY w|S )a  
        Read PLY data from a readable file-like object or filename.

        Parameters
        ----------
        stream : str or readable open file
        mmap : {'c', 'r', 'r+'} or bool, optional (default='c')
            Configures memory-mapping. Any falsy value disables
            memory mapping, and any non-string truthy value is
            equivalent to 'c', for copy-on-write mapping.
        known_list_len : dict, optional
            Mapping from element names to mappings from list property
            names to their fixed lengths.  This optional argument is
            necessary to enable memory mapping of elements that contain
            list properties. (Note that elements with variable-length
            list properties cannot be memory-mapped.)

        Raises
        ------
        PlyParseError
            If the file cannot be parsed for any reason.
        ValueError
            If `stream` is open in text mode but the PLY header
            indicates binary encoding.
        readr   z-can't read binary-format PLY from text streamrG   )known_list_len)_open_streamr   rN   
isinstancerQ   strr
   r"   _ioTextIOWrapper_readr	   getr(   close)rL   mmaprR   
must_closedatadata_streamr+   s          r   rQ   zPlyData.read   s6   6  ,FF;;V	((00D&++a..#.. 
)9 9"(KK$ &8 9 9 9 9 )"%"3FG"D"DKK"(K K K		+ty$/4)7););CHb)I)I  K K K KK     s   B<C) )Dc                 N   t          |d          \  }}	 	 |                    d           d}n# t          $ r d}Y nw xY w|rC|                    | j                            d                     |                    d           nE| j        st          d          |                    | j                   |                    d           | D ]#}|                    || j        | j                   $	 |r|	                                 d	S d	S # |r|	                                 w w xY w)
a8  
        Write PLY data to a writeable file-like object or filename.

        Parameters
        ----------
        stream : str or writeable open file

        Raises
        ------
        ValueError
            If `stream` is open in text mode and the file to be written
            is binary-format.
        writer   TFrG      
z,can't write binary-format PLY to text stream
N)
rS   r`   	TypeErrorheaderencoder
   r"   _writer	   rZ   )r   rL   r\   binary_streamr+   s        r   r`   zPlyData.write   sZ     ,FG<<V	&S!!! $ & & & %& #T[//88999U####y 4$ &3 4 4 4T[)))T""" ? ?

649do>>>>?   z s"   . D =D =B3D D$c                    dg}| j         r|                    d           n+|                    dt          | j                 z   dz              | j        D ]}|                    d|z              | j        D ]}|                    d|z              |                    d | j        D                        |                    d           d	                    |          S )
z:
        PLY-formatted metadata for the instance.
        plyzformat ascii 1.0zformat z 1.0comment z	obj_info c              3   $   K   | ]}|j         V  d S r   rd   r)   s     r   r,   z!PlyData.header.<locals>.<genexpr>   s$      99CSZ999999r   
end_headerrb   )	r
   append_byte_order_reverser	   r   r   extendr   joinr   linesrO   s      r   rd   zPlyData.header   s    
 9 	!LL+,,,,LL,T_=>  ! ! !  	) 	)ALLa(((( 	* 	*ALLq))))994=999999\"""yyr   c                 *    t          | j                  S )z,
        Iterate over the elements.
        )iterr   r   s    r   __iter__zPlyData.__iter__   s     DM"""r   c                 *    t          | j                  S )z0
        Return the number of elements.
        )r/   r   r   s    r   __len__zPlyData.__len__   s     4=!!!r   c                     || j         v S )zA
        Check if an element with the given name exists.
        r.   r   r(   s     r   __contains__zPlyData.__contains__   s     t+++r   c                     | j         |         S )z
        Retrieve an element by name.

        Parameters
        ----------
        name : str

        Returns
        -------
        PlyElement

        Raises
        ------
        KeyError
            If the element can't be found.
        rz   r{   s     r   __getitem__zPlyData.__getitem__  s    " #D))r   c                     | j         S r   rl   r   s    r   __str__zPlyData.__str__  
    {r   c                 X    d| j         d| j        d| j        d| j        d| j        dS )NzPlyData(z, text=z, byte_order=z, comments=z, obj_info=))r   r
   r	   r   r   r   s    r   __repr__zPlyData.__repr__  s:      			4???/ 	0r   N)__name__
__module____qualname____doc__r   r   r   propertyr   r   r$   r	   r   r4   r:   r   r>   r@   r   staticmethodrN   rQ   r`   rd   rv   rx   r|   r~   r   r   rC   r   r   r   r      s        $ !#3r! ! ! !2     x}55H     
& & & /?;;J< < <$ $ $( ( ( x}55H$ $ $( ( ( x}55H
 
 \
 b / / / \/b" " "H     X 2# # #" " ", , ,* * *&  0 0 0 0 0r   r   c                   T   e Zd ZdZg fdZed             Zd Zd Z eee          Z	d Z
d Zd Z eee          Zd	 Zd
 Z eee          Zd Zd Zed             Zd dZei i g fd            Zi fdZd Zd Zd Zd Zd Zd Zed             Zd Zd Z d Z!d Z"d Z#d Z$dS )!rD   a  
    PLY file element.

    Creating a `PlyElement` instance is generally done in one of two
    ways: as a byproduct of `PlyData.read` (when reading a PLY file) and
    by `PlyElement.describe` (before writing a PLY file).

    Attributes
    ----------
    name : str
    count : int
    data : numpy.ndarray
    properties : list of PlyProperty
    comments : list of str
    header : str
        PLY header block for this element.
    c                     t          |           t          |          | _        || _        t	          |          | _        |                                  || _        t          d | j	        D                       | _
        dS )a  
        This is not part of the public interface.  The preferred methods
        of obtaining `PlyElement` instances are `PlyData.read` (to read
        from a file) and `PlyElement.describe` (to construct from a
        `numpy` array).

        Parameters
        ----------
        name : str
        properties : list of PlyProperty
        count : str
        comments : list of str
        c              3   @   K   | ]}t          |t                    V  d S r   )rT   PlyListPropertyr*   ps     r   r,   z&PlyElement.__init__.<locals>.<genexpr>K  s@       8 8"# )O<< 8 8 8 8 8 8r   N)_check_namerU   _name_countr   _propertiesr   r   any
properties
_have_list)r   r(   r   countr   s        r   r   zPlyElement.__init__4  s}     	DYY
 ,,  8 8'+8 8 8 8 8r   c                     | j         S r   )r   r   s    r   r   zPlyElement.countN  s
    {r   c                     | j         S r   )_datar   s    r   	_get_datazPlyElement._get_dataR  s
    zr   c                 d    || _         t          |          | _        |                                  d S r   )r   r/   r   _check_sanityr   r]   s     r   	_set_datazPlyElement._set_dataU  s.    
$iir   c                 x    | j         D ]1}|j        | j        j        j        vrt          d|j        z            2d S )Nzdangling property %r)r   r(   r   dtypefieldsr"   )r   props     r   r   zPlyElement._check_sanity\  sQ    O 	E 	EDy
 0 777 !7$)!CDDD 8	E 	Er   c                     | j         S r   )r   r   s    r   _get_propertieszPlyElement._get_propertiesa  s    r   c                 ~    t          |          | _        |                                  |                                  d S r   )r   r   r   r   )r   r   s     r   _set_propertieszPlyElement._set_propertiesd  s5     ,,r   c                 *    t          | j                  S r   r1   r   s    r   r4   zPlyElement._get_commentsk  r5   r   c                 L    t          |           t          |          | _        d S r   r7   r9   s     r   r:   zPlyElement._set_commentsn  r;   r   c                     t          d | j        D                       | _        t          | j                  t          | j                  k    rt	          d          d S )Nc              3   (   K   | ]}|j         |fV  d S r   r'   )r*   r   s     r   r,   z$PlyElement._index.<locals>.<genexpr>u  sE       %C %C)- '+i%6 %C %C %C %C %C %Cr   ztwo properties with same name)r-   r   _property_lookupr/   r"   r   s    r   r   zPlyElement._indext  sr     $ %C %C151A%C %C %C !C !Ct$%%T-=)>)>>><=== ?>r   c                     | j         |         S )z
        Look up property by name.

        Parameters
        ----------
        name : str

        Returns
        -------
        PlyProperty

        Raises
        ------
        KeyError
            If the property can't be found.
        r   r{   s     r   ply_propertyzPlyElement.ply_propertyz  s    " $T**r   c                     | j         S r   r   r   s    r   r(   zPlyElement.name  
    zr   r   c                 N    t          j        fd| j        D                       S )a  
        Return the `numpy.dtype` description of the in-memory
        representation of the data.  (If there are no list properties,
        and the PLY format is binary, then this also accurately
        describes the on-disk representation of the element.)

        Parameters
        ----------
        byte_order : {'<', '>', '='}

        Returns
        -------
        numpy.dtype
        c                 H    g | ]}|j         |                              fS rC   )r(   r   )r*   r   r	   s     r   rF   z$PlyElement.dtype.<locals>.<listcomp>  s<     7 7 7"  9djj&<&<= 7 7 7r   )_npr   r   r#   s    `r   r   zPlyElement.dtype  sA     y 7 7 7 7&*o7 7 7 8 8 	8r   c                    t          | t          j                  st          d          t	          | j                  dk    rt          d          t	          |           }g }| j        j        }|D ]}t          |d         t                    st          d          |d         st          d          t	          |          dk    s|d         d         dk    r|d         d         dk    r"t	          |          dk    rt          d	          t          |                    |d         d
                   }	|d         d         dk    r,|                    |d         d          }
t          |
          }nt          |d         dd                   }t          |d         |	|          }n3t          |d         dd                   }t          |d         |          }|                    |           t!          ||||          }| |_        |S )aA  
        Construct a `PlyElement` instance from an array's metadata.

        Parameters
        ----------
        data : numpy.ndarray
            Structured `numpy` array.
        len_types : dict, optional
            Mapping from list property names to type strings
            (`numpy`-style like `'u1'`, `'f4'`, etc., or PLY-style like
            `'int8'`, `'float32'`, etc.), which will be used to encode
            the length of the list in binary-format PLY files.  Defaults
            to `'u1'` (8-bit integer) for all list properties.
        val_types : dict, optional
            Mapping from list property names to type strings as for
            `len_types`, but is used to encode the list elements in
            binary-format PLY files.  Defaults to `'i4'` (32-bit
            integer) for all list properties.
        comments : list of str
            Comments between the "element" line and first property
            definition in the header.

        Returns
        -------
        PlyElement

        Raises
        ------
        TypeError, ValueError
        zonly numpy arrays are supported   z)only one-dimensional arrays are supportedznested records not supportedr   zfield with empty name   Oz&non-scalar object fields not supportedu1i4N)rT   r   ndarrayrc   r/   shaper"   r   descrrU   _data_type_reverserY   _lookup_typer   PlyPropertyrn   rD   r]   )r]   r(   	len_types	val_typesr   r   r   r   tlen_strval_typeval_strr   r+   s                 r   describezPlyElement.describe  s   B $,, 	?=>>>tz??a ) * * * D		

  	$ 	$AadC(( A !?@@@Q4 : !89991vv{{ad1gnn Q47c>>1vv{{( *5 6 6 6 -Y]]1Q4-F-FGQ47c>>(}}QqT488H*844GG*1Q4844G&qtWg>>&qtABBx00"1Q411d####z5(;;
r   c                 v   |r|                      |           nt          d | j        D                       }|t          |          k    }|rCt          |          r4|r2t	          |t
                    r|nd}|                     ||||           n|                     ||           |                                  dS )a  
        Read the actual data from a PLY file.

        Parameters
        ----------
        stream : readable open file
        text : bool
        byte_order : {'<', '>', '='}
        mmap : {'c', 'r', 'r+'} or bool
        known_list_len : dict
        c              3   N   K   | ] }t          |t                    |j        V  !d S r   )rT   r   r(   r   s     r   r,   z#PlyElement._read.<locals>.<genexpr>  sO       "E "EQ%/?%C%C"E!& "E "E "E "E "E "Er   rO   N)		_read_txtsetr   	_can_mmaprT   rU   
_read_mmap	_read_binr   )	r   rL   r
   r	   r[   rR   list_prop_namescan_mmap_lists	mmap_modes	            r   rX   zPlyElement._read  s      	3NN6""""! "E "E$/ "E "E "E E EO,N0C0CCN 3	&)) 3n 3 %/tS$9$9BDDs	
I .0 0 0 0 vz222r   c                    |r|                      |           dS | j        r|                     ||           dS |                    | j                            |                     |          d          j                   dS )z
        Write the data to a PLY file.

        Parameters
        ----------
        stream : writeable open file
        text : bool
        byte_order : {'<', '>', '='}
        F)copyN)
_write_txtr   
_write_binr`   r]   astyper   )r   rL   r
   r	   s       r   rf   zPlyElement._write  s      	@OOF##### @ 
33333 TY--djj.D.D38 . : ::>@ @ @ @ @r   c           	      ^   i }g }| j         D ]}t          |t                    rs|                    |          \  }}	|                    |j        dz   |f           |                    |j        |	||j                 ff           |j        dz   ||j        <   |                    |j        |                    |          f           t          j        |          }
| j        |
j	        z  }|
                                }|                    dd           |
                                |z
  }||k     rt          d| ||
j	        z            t          j        ||
||| j                  | _        |                    || j        |
j	        z  z              |D ]w}||         }| j        |         ||         k    }|                                sBt          j        |dz            d         }t          d| ||                     |                    xd | j         D             }| j        |         | _        dS )	z
        Memory-map an input file as `self.data`.

        Parameters
        ----------
        stream : readable open file
        byte_order : {'<', '>', '='}
        mmap_mode: str
        known_list_len : dict
        z
lenr   r   early end-of-fileTzunexpected list lengthc                     g | ]	}|j         
S rC   r'   r   s     r   rF   z)PlyElement._read_mmap.<locals>.<listcomp>V  s    111A111r   N)r   rT   r   
list_dtypern   r(   r   r   r   itemsizetellseekPlyElementParseErrormemmapr   allflatnonzeror   )r   rL   r	   r   rR   list_len_props	new_dtyper   	len_dtype	val_dtyper   	num_bytesoffset	max_bytesr   field	len_checkrowpropss                      r   r   zPlyElement._read_mmap)  sD    	 
	@ 
	@A!_-- 	@'(||J'?'?$	9  !&7"2I!>???  !&)#1!&#9";"= > > >)*')9qv&&  !&!''**=*=!>????	)$$J/	AqKKMMF*	y  &':D'0EN'BD D DZy&$*MM
FTZ%.88999" 	8 	8D"4(E
5)^D-AAI==?? 8oi$&677:*,#t00668 8 88
 21111Z&


r   c           	         t          j        | j        |                                           | _        d}t          t          |j        d          | j                  D ]}t          |                                	                                          }| j
        D ]f}	 |                    |          | j        |j                 |<   ,# t          $ r t          d| ||          t          $ r t          d| ||          w xY w	 t!          |           t          d| |          # t          $ r Y nw xY w|dz  }|| j        k     r| `t          d| |          d	S )
z
        Load a PLY element from an ASCII-format PLY file.  The element
        may contain list properties.

        Parameters
        ----------
        stream : readable open file
        r   r    zearly end-of-linezmalformed inputzexpected end-of-liner   r   N)r   emptyr   r   r   _isliceru   readlinestripsplitr   _from_fieldsr(   StopIterationr   r"   next)r   rL   kliner   r   s         r   r   zPlyElement._read_txtY  s    Ytz>>>
D"55tzBB 	 	D$**,,,,..//F > >>/3/@/@/H/HDJty)!,,$ > > >./B/3Q> > >! > > >./@/3Q> > >>4V ++A+/4 4 4 !   
 FAAtz>>
&':D!DDD >s   (C8C=D!!
D.-D.c                     | j         D ]Z}g }| j        D ]5}|                    |                    ||j                                      6t          j        ||gdd           [dS )z
        Save a PLY element to an ASCII-format PLY file.  The element may
        contain list properties.

        Parameters
        ----------
        stream : writeable open file
        z%.18grb   )newlineN)r]   r   rp   
_to_fieldsr(   r   savetxt)r   rL   recr   r   s        r   r   zPlyElement._write_txt}  s~     9 	A 	ACF ? ?dooc$)n==>>>>K'4@@@@@	A 	Ar   c           	      B   t          j        | j        |                     |                    | _        t          | j                  D ]V}| j        D ]L}	 |                    ||          | j        |j                 |<   -# t          $ r t          d| ||          w xY wWdS )z
        Load a PLY element from a binary PLY file.  The element may
        contain list properties.

        Parameters
        ----------
        stream : readable open file
        byte_order : {'<', '>', '='}
        r   r   N)r   r   r   r   r   ranger   r   r(   r   r   )r   rL   r	   r   r   s        r   r   zPlyElement._read_bin  s     YtzJ1G1GHHH
tz"" 	> 	>A > >>vz:: Jty)!,,$ > > >./B/3Q> > >>	>	> 	>s   )A>>Bc                 r    | j         D ].}| j        D ]$}|                    ||j                 ||           %/dS )z
        Save a PLY element to a binary PLY file.  The element may
        contain list properties.

        Parameters
        ----------
        stream : writeable open file
        byte_order : {'<', '>', '='}
        N)r]   r   r   r(   )r   rL   r	   r   r   s        r   r   zPlyElement._write_bin  s[     9 	D 	DC D DDI
CCCCD	D 	Dr   c                    d| j         | j        fz  g}| j        D ]}|                    d|z              |                    t          t          t          | j                                       d	                    |          S )Nzelement %s %drj   rb   )
r(   r   r   rn   rp   r2   maprU   r   rq   rr   s      r   rd   zPlyElement.header  s|     DItz#::;  	) 	)ALLa((((T#c4?3344555yyr   c                     | j         S )z;
        Return the number of rows in the element.
        )r   r   s    r   rx   zPlyElement.__len__  s     zr   c                     || j         v S )zE
        Determine if a property with the given name exists.
        r   r{   s     r   r|   zPlyElement.__contains__  s     t,,,r   c                     | j         |         S )zC
        Proxy to `self.data.__getitem__` for convenience.
        r]   )r   keys     r   r~   zPlyElement.__getitem__  s     y~r   c                     || j         |<   dS )zC
        Proxy to `self.data.__setitem__` for convenience.
        Nr	  )r   r
  values      r   __setitem__zPlyElement.__setitem__  s     	#r   c                     | j         S r   rl   r   s    r   r   zPlyElement.__str__  r   r   c                 <    d| j         | j        | j        | j        fz  S )Nz)PlyElement(%r, %r, count=%d, comments=%r))r(   r   r   r   r   s    r   r   zPlyElement.__repr__  s&    ;DOTZ   	!r   Nr   )%r   r   r   r   r   r   r   r   r   r]   r   r   r   r   r4   r:   r   r   r   r(   r   r   r   rX   rf   r   r   r   r   r   rd   rx   r|   r~   r  r   r   rC   r   r   rD   rD   !  sg        $ :< 8 8 8 84   X    
 8Iy))DE E E
       
 /?;;J$ $ $( ( ( x}55H> > >+ + +&   X8 8 8 8$ ')RM M M \M`     >@ @ @..' .' .'`"E "E "EHA A A > > >*D D D 
  
  X
   - - -      ! ! ! ! !r   rD   c                   ~    e Zd ZdZd Zd Zd Z eee          Zed             Z	ddZ
d Zd	 Zd
 Zd Zd Zd ZdS )r   z
    PLY property description.

    This class is pure metadata. The data itself is contained in
    `PlyElement` instances.

    Attributes
    ----------
    name : str
    val_dtype : str
        `numpy.dtype` description for the property's data.
    c                 Z    t          |           t          |          | _        || _        dS )zZ
        Parameters
        ----------
        name : str
        val_dtype : str
        N)r   rU   r   r   )r   r(   r   s      r   r   zPlyProperty.__init__  s*     	DYY
"r   c                     | j         S r   )
_val_dtyper   s    r   _get_val_dtypezPlyProperty._get_val_dtype  
    r   c                 D    t           t          |                   | _        d S r   )_data_typesr   r  )r   r   s     r   _set_val_dtypezPlyProperty._set_val_dtype      %l9&=&=>r   c                     | j         S r   r   r   s    r   r(   zPlyProperty.name  r   r   r   c                     || j         z   S )z
        Return the `numpy.dtype` description for this property.

        Parameters
        ----------
        byte_order : {'<', '>', '='}, default='='

        Returns
        -------
        tuple of str
        )r   r#   s     r   r   zPlyProperty.dtype  s     DN**r   c                     t          j        |                                                               t          |                    S )a:  
        Parse data from generator.

        Parameters
        ----------
        fields : iterator of str

        Returns
        -------
        data
            Parsed data of the correct type.

        Raises
        ------
        StopIteration
            if the property's data could not be read.
        )r   r   typer   )r   r   s     r   r   zPlyProperty._from_fields  s0    $ y&&++DLL999r   c              #      K   t          j        |                                                               |          V  dS )z
        Parameters
        ----------
        data
            Property data to encode.

        Yields
        ------
        encoded_data
            Data with type consistent with `self.val_dtype`.
        N)r   r   r  r   s     r   r   zPlyProperty._to_fields%  s:       i

%%**40000000r   c                     	 t          ||                     |          d          d         S # t          $ r t          w xY w)a  
        Read data from a binary stream.

        Parameters
        ----------
        stream : readable open binary file
        byte_order : {'<'. '>', '='}

        Raises
        ------
        StopIteration
            If the property data could not be read.
        r   r   )_read_arrayr   
IndexErrorr   )r   rL   r	   s      r   r   zPlyProperty._read_bin3  sK    	 vtzz*'='=qAA!DD 	  	  	 	 s   ), >c                     t          |t          j        |                     |                                        |                     dS )z
        Write data to a binary stream.

        Parameters
        ----------
        data
            Property data to encode.
        stream : writeable open binary file
        byte_order : {'<', '>', '='}
        N)_write_arrayr   r   r  )r   r]   rL   r	   s       r   r   zPlyProperty._write_binF  s>     	VSYtzz*'='=>>CCDIIJJJJJr   c                 @    t           | j                 }d|d| j        S )Nz	property  )r   r   r(   )r   r   s     r   r   zPlyProperty.__str__S  s#    $T^4#*77DII66r   c                 B    d| j         dt          | j                  dS )NzPlyProperty(, r   )r(   r   r   r   s    r   r   zPlyProperty.__repr__W  s,     (,			(4T^(D(D(D(DF 	Fr   Nr  )r   r   r   r   r   r  r  r   r   r(   r   r   r   r   r   r   r   rC   r   r   r   r     s         	# 	# 	#  ? ? ? 88I  X+ + + +: : :(1 1 1     &K K K7 7 7F F F F Fr   r   c                   p    e Zd ZdZd Zd Zd Z eee          ZddZ	ddZ
d Zd	 Zd
 Zd Zd Zd ZdS )r   z
    PLY list property description.

    Attributes
    ----------
    name
    val_dtype
    len_dtype : str
        `numpy.dtype` description for the property's length field.
    c                 L    t                               | ||           || _        dS )zr
        Parameters
        ----------
        name : str
        len_dtype : str
        val_dtype : str
        N)r   r   r   )r   r(   r   r   s       r   r   zPlyListProperty.__init__h  s'     	T4333"r   c                     | j         S r   )
_len_dtyper   s    r   _get_len_dtypezPlyListProperty._get_len_dtypet  r  r   c                 D    t           t          |                   | _        d S r   )r  r   r,  )r   r   s     r   _set_len_dtypezPlyListProperty._set_len_dtypew  r  r   r   c                     dS )a)  
        `numpy.dtype` name for the property's field in the element.

        List properties always have a numpy dtype of "object".

        Parameters
        ----------
        byte_order : {'<', '>', '='}

        Returns
        -------
        dtype : str
            Always `'|O'`.
        z|OrC   r#   s     r   r   zPlyListProperty.dtype|  s	     tr   c                 *    || j         z   || j        z   fS )z
        Return the pair `(len_dtype, val_dtype)` (both numpy-friendly
        strings).

        Parameters
        ----------
        byte_order : {'<', '>', '='}

        Returns
        -------
        len_dtype : str
        val_dtype : str
        )r   r   r#   s     r   r   zPlyListProperty.list_dtype  s"     T^+T^+- 	-r   c                 N   |                                  \  }}t          t          j        |                              t          |                              }t          j        t          t          ||                    |d          }t          |          |k     rt          |S )aL  
        Parse data from generator.

        Parameters
        ----------
        fields : iterator of str

        Returns
        -------
        data : numpy.ndarray
            Parsed list data for the property.

        Raises
        ------
        StopIteration
            if the property's data could not be read.
        r   )ndmin)r   intr   r   r  r   loadtxtr2   r   r/   r   )r   r   len_tval_tnr]   s         r   r   zPlyListProperty._from_fields  s    $ **	%  %%d6ll3344{4 2 233U!DDDt99q==r   c              #      K   |                                  \  }}t          j        ||                                          }t          j        |                              |j                  V  |D ]}|V  dS )a;  
        Return generator over the (numerical) PLY representation of the
        list data (length followed by actual data).

        Parameters
        ----------
        data : numpy.ndarray
            Property data to encode.

        Yields
        ------
        Length followed by each list element.
        r   N)r   r   asarrayravelr   r  size)r   r]   r6  r7  xs        r   r   zPlyListProperty._to_fields  s       **{4u---3355i##DI..... 	 	AGGGG	 	r   c                 0   |                      |          \  }}	 t          |t          j        |          d          d         }n# t          $ r t
          w xY wt          |t          j        |          |          }t          |          |k     rt
          |S )aB  
        Read data from a binary stream.

        Parameters
        ----------
        stream : readable open binary file
        byte_order : {'<', '>', '='}

        Returns
        -------
        data : numpy.ndarray

        Raises
        ------
        StopIteration
            If data could not be read.
        r   r   )r   r!  r   r   r"  r   r/   )r   rL   r	   r6  r7  r8  r]   s          r   r   zPlyListProperty._read_bin  s    $ 44	 FCIe$4$4a88;AA 	  	  	 	  639U#3#3Q77t99q==s   )A Ac                     |                      |          \  }}t          j        ||                                          }t	          |t          j        |j        |                     t	          ||           dS )z
        Write data to a binary stream.

        Parameters
        ----------
        data : numpy.ndarray
            Data to encode.
        stream : writeable open binary file
        byte_order : {'<', '>', '='}
        r   N)r   r   r:  r;  r$  arrayr<  )r   r]   rL   r	   r6  r7  s         r   r   zPlyListProperty._write_bin  sp     44{4u---3355VSYty>>>???VT"""""r   c                 j    t           | j                 }t           | j                 }d|d|d| j        S )Nzproperty list r&  )r   r   r   r(   )r   r   r   s      r   r   zPlyListProperty.__str__  s5    $T^4$T^4+277GGGTYYGGr   c                 l    d| j         dt          | j                  dt          | j                  dS )NzPlyListProperty(r(  r   )r(   r   r   r   r   s    r   r   zPlyListProperty.__repr__  s=     dn----dn----/ 	0r   Nr  )r   r   r   r   r   r-  r/  r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   \  s        	 	
# 
# 
#  ? ? ? 88I   "- - - -"  8  ,  ># # #$H H H
0 0 0 0 0r   r   c                       e Zd ZdZdS )PlyParseErrorz,
    Base class for PLY parsing errors.
    N)r   r   r   r   rC   r   r   rD  rD    s          	Dr   rD  c                        e Zd ZdZddZd ZdS )r   z
    Raised when a PLY element cannot be parsed.

    Attributes
    ----------
    message : str
    element : PlyElement
    row : int
    prop : PlyProperty
    Nc                    || _         || _        || _        || _        d}| j        r|d| j        j        z  z  }| j        |d| j        z  z  }| j        r|d| j        j        z  z  }|| j         z  }t
                              | |           d S )Nr   zelement %r: zrow %d: zproperty %r: )messageelementr   r   r(   	Exceptionr   )r   rG  rH  r   r   ss         r   r   zPlyElementParseError.__init__!  s    	< 	4$,"333A8dh&&A9 	249>11A	T\4#####r   c           
      `    | j         j        d| j        d| j        d| j        d| j        d
S )N(z
, element=z, row=z, prop=r   )	__class__r   rG  rH  r   r   r   s    r   r   zPlyElementParseError.__repr__2  s=    (((t|||TXXXtyyyB 	Cr   )NNNr   r   r   r   r   r   rC   r   r   r   r     sF        	 	$ $ $ $"C C C C Cr   r   c                        e Zd ZdZddZd ZdS )PlyHeaderParseErrorz
    Raised when a PLY header cannot be parsed.

    Attributes
    ----------
    line : str
        Which header line the error occurred on.
    Nc                     || _         || _        d}| j        r|d| j        z  z  }|| j         z  }t                              | |           d S )Nr   z	line %r: )rG  r   rI  r   )r   rG  r   rJ  s       r   r   zPlyHeaderParseError.__init__B  sX    	9 	)ty((A	T\4#####r   c                 @    | j         j        d| j        d| j        dS )NrL  z, line=r   )rM  r   rG  r   r   s    r   r   zPlyHeaderParseError.__repr__M  s+    (((tyyy* 	+r   r   rN  rC   r   r   rP  rP  8  sA         	$ 	$ 	$ 	$+ + + + +r   rP  c                   J    e Zd ZdZd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd ZdS )rH   a  
    Parser for PLY format header.

    Attributes
    ----------
    format : str
        "ascii", "binary_little_endian", or "binary_big_endian"
    elements : list of (name, comments, count, properties)
    comments : list of str
    obj_info : list of str
    lines : int
    c                     d| _         g | _        g | _        g | _        d| _        g d| _        |D ]}|                     |           | j        r|                     d           dS dS )z
        Parameters
        ----------
        lines : iterable of str
            Header lines, starting *after* the "ply" line.

        Raises
        ------
        PlyHeaderParseError
        Nr   )rJ   commentr   r   )rJ   r   r   r   rs   _allowedconsume_error)r   rs   r   s      r   r   z_PlyHeaderParser.__init__a  s     
999 	 	DLL= 	-KK+,,,,,	- 	-r   c                    | xj         dz  c_         |s|                     d           |                                }|dk    r| j        S 	 |                    dd          d         }n$# t
          $ r |                                  Y nw xY w|| j        vr0|                     dd                    | j                  z              t          | d|z             |t          |          dz   d                    | j        S )	z:
        Parse and internalize one line of input.
        r   r   r   Nr   zexpected one of {%s}r(  parse_)	rs   rX  r   rV  r   r"  rq   getattrr/   )r   raw_liner   keywords       r   rW  z_PlyHeaderParser.consumew  s    	

a

 	-KK+,,,~~2:: = 	jjq))!,GG 	 	 	KKMMMMM	 $-''KK.		$-001 2 2 2 	*h())$s7||A~*?@@@}s   
A' 'BBparse errorc                 ,    t          || j                  r   )rP  rs   )r   rG  s     r   rX  z_PlyHeaderParser._error  s    !'4:666r   c                 f   |                                                                 }t          |          dk    r|                     d           |d         | _        | j        t
          vr|                     d| j        z             |d         dk    r|                     d           g d| _        d S )	Nr   zexpected "format {format} 1.0"r   zdon't understand format %rr   z1.0zexpected version '1.0')rH  rU  r   rm   )r   r   r/   rX  rJ   rK   rV  )r   r]   r   s      r   parse_formatz_PlyHeaderParser.parse_format  s    ##%%v;;!KK:;;;Qi;o--KK4t{BCCC!9KK0111HHHr   c                     | j         s| j                            |           d S | j         d         d                             |           d S )N   )r   r   rn   r   s     r   parse_commentz_PlyHeaderParser.parse_comment  sN    } 	.M  &&&&&M"a ''-----r   c                 :    | j                             |           d S r   )r   rn   r   s     r   parse_obj_infoz_PlyHeaderParser.parse_obj_info  s    T"""""r   c                 x   |                                                                 }t          |          dk    r|                     d           |d         }	 t	          |d                   }n%# t
          $ r |                     d           Y nw xY w| j                            |g |g f           g d| _        d S )Nr   z!expected "element {name} {count}"r   r   zexpected integer count)rH  rU  r   rm   )	r   r   r/   rX  r4  r"   r   rn   rV  )r   r]   r   r(   r   s        r   parse_elementz_PlyHeaderParser.parse_element  s    ##%%v;;!KK=>>>ay	2q	NNEE 	2 	2 	2KK011111	2 	dBr2333HHHs   A. .BBc                 :   | j         d         d         }|                                                                }t          |          dk     r|                     d           |d         dk    rt          |          dk    r|                     d           	 |                    t          |d	         |d         |d                              d S # t          $ r-}|                     t          |                     Y d }~d S d }~ww xY wt          |          dk    r|                     d
           	 |                    t          |d         |d                              d S # t          $ r-}|                     t          |                     Y d }~d S d }~ww xY w)Nrc  r   r   zbad 'property' liner   r2      z5expected "property list {len_type} {val_type} {name}"rd  z!expected "property {type} {name}")
r   r   r   r/   rX  rn   r   r"   rU   r   )r   r]   r   r   rE   s        r   parse_propertyz_PlyHeaderParser.parse_property  s   ]2&q)
##%%v;;??KK-...!96{{a = > > >$!!#F1Ivay&)DD      $ $ $CFF#########$ 6{{aABBB$!!q	6!955      $ $ $CFF#########$s0   6C 
D"DD2/E# #
F-"FFc                 B    |r|                      d           g | _        d S )Nz"unexpected data after 'end_header')rX  rV  r   s     r   parse_end_headerz!_PlyHeaderParser.parse_end_header  s(     	>KK<===r   N)r^  )r   r   r   r   r   rW  rX  ra  re  rg  ri  rl  rn  rC   r   r   rH   rH   S  s         - - -,  :7 7 7 7I I I. . .# # #I I I$ $ $:    r   rH   c                   4    e Zd ZdZd Zed             Zd ZdS )rI   zc
    Generator over lines in the PLY header.

    LF, CR, and CRLF line endings are supported.
    c                 &   |                      |                    d                    }g | _        |dd         dk    rt          dd          |dd         | _        |dd         dk    rZ|                      |                    d                    }|dk    r| xj        |z  c_        n9| j                            |           n|dd         dk    rt          d	d          || _        t          | j                  | _        d
| _	        d| _
        dS )z
        Parameters
        ----------
        stream : text or binary stream.

        Raises
        ------
        PlyHeaderParseError
        rk  Nrd  ri   zexpected 'ply'r   rb   z!unexpected characters after 'ply'F)_decoderQ   charsrP  nlrn   rL   r/   len_nldoners   )r   rL   rJ  rO   s       r   r   z_PlyHeaderLines.__init__  s    LLQ((
RaR5E>>%&6:::ABB%QRR5D==V[[^^,,ADyy1
!!!$$$$qrrUd]]%3Q8 8 8$'ll	


r   c                 Z    t          | t                    r| S |                     d          S )zk
        Convert input `str` or `bytes` instance to `str`, decoding
        as ASCII if necessary.
        rG   )rT   rU   decode)rJ  s    r   rr  z_PlyHeaderLines._decode  s-     a 	Hxx   r   c              #   N  K   | j         s| xj        dz  c_        d                    | j        | j         d                   | j        k    r|                     | j                            d                    }|st          d| j                  | j        
                    |           d                    | j        | j         d                   | j        k    d                    | j        d| j                            }g | _        |dk    rd| _         |V  | j         dS dS )zi
        Yields
        ------
        line : str
            Decoded line with newline removed.
        r   r   Nr   rm   T)rv  rs   rq   rs  ru  rt  rr  rL   rQ   rP  rn   )r   charr   s      r   rv   z_PlyHeaderLines.__iter__  s0      ) 	JJ!OJJ''$*dk\]]344??||DK$4$4Q$7$788 :-.A.2j: : :
!!$''' ''$*dk\]]344?? 774:m|m455DDJ|## 	JJJ ) 	 	 	 	 	r   N)r   r   r   r   r   r   rr  rv   rC   r   r   rI   rI     sW           : ! ! \!    r   rI   c                     t          | |          rd| fS 	 dt          | |d         dz             fS # t          $ r t          d          w xY w)aC  
    Normalizing function: given a filename or open stream,
    return an open stream.

    Parameters
    ----------
    stream : str or open file-like object
    read_or_write : str
        `"read"` or `"write"`, the method to be used on the stream.

    Returns
    -------
    must_close : bool
        Whether `.close` needs to be called on the file object
        by the caller (i.e., it wasn't already open).
    file : file-like object

    Raises
    ------
    TypeError
        If `stream` is neither a string nor has the
        `read_or_write`-indicated method.
    FTr   bzexpected open file or filename)hasattropenrc   )rL   read_or_writes     r   rS   rS   -  sn    0 v}%% v:d6=#3c#9::;; : : :8999:s	   1 Ac                     | D ]V}dt          |          cxk    rdk     sn t          d| z            |                                rt          d| z            WdS )z
    Check that a string can be safely be used as the name of an element
    or property in a PLY file.

    Parameters
    ----------
    name : str

    Raises
    ------
    ValueError
        If the check failed.
    r      znon-ASCII character in name %rzspace character(s) in name %rN)ordr"   isspace)r(   rz  s     r   r   r   M  s      E ECII########=DEEE<<>> 	E<tCDDD	EE Er   c                     | D ]G}|D ]B}dt          |          cxk    rdk     sn t          d          |dk    rt          d          CHdS )z
    Check that the given comments can be safely used in a PLY header.

    Parameters
    ----------
    comments : list of str

    Raises
    ------
    ValueError
        If the check fails.
    r   r  znon-ASCII character in commentrb   zembedded newline in commentN)r  r"   )r   rU  rz  s      r   r8   r8   b  s      @ @ 	@ 	@DD		''''C'''' !ABBBt|| !>??? 	@@ @r   c                     	 t          t          j        |          j                  t          |          z  }t          j        |                     |          |          S # t          $ r t          w xY w)a/  
    Read `n` elements of type `dtype` from an open stream.

    Parameters
    ----------
    stream : readable open binary file
    dtype : dtype description
    n : int

    Returns
    -------
    numpy.ndarray

    Raises
    ------
    StopIteration
        If `n` elements could not be read.
    )r4  r   r   r   
frombufferrQ   rI  r   )rL   r   r8  r<  s       r   r!  r!  w  sk    &39U##,--A6~fkk$//777   s   AA   A2c                 T    |                      |                                           dS )z
    Write `numpy` array to a binary file.

    Parameters
    ----------
    stream : writeable open binary file
    array : numpy.ndarray
    N)r`   tobytes)rL   r@  s     r   r$  r$    s$     LL!!!!!r   c                     	 |                                  }	 t          j        | dd           |                     |           dS # t          $ r |                     |           Y dS w xY w# t          $ r Y dS w xY w)z
    Determine if a readable stream can be memory-mapped, using some good
    heuristics.

    Parameters
    ----------
    stream : open binary file

    Returns
    -------
    bool
    r   rO   TF)r   r   r   r   rI  )rL   poss     r   r   r     s    
kkmm	JvtS)))KK4 	 	 	KK55	    uus.   A* +A A'#A* &A''A* *
A87A8c                     | t           vr7	 t          |          } n(# t          $ r t          d| dt                    w xY wt           |          S )Nzfield type z not in )r   r  KeyErrorr"   _types_list)type_strs    r   r   r     sk    )))	6"8,HH 	6 	6 	6*&hh5 6 6 6	6 h''s    %>))int8i1)rz  r  )uint8r   )ucharb1)r  r   )int16i2)shortr  )uint16u2)ushortr  )int32r   )r4  r   )uint32u4)uintr  )float32f4)floatr  )float64f8)doubler  c              #   $   K   | ]\  }}||fV  d S r   rC   )r*   ar|  s      r   r,   r,     s*      CCVa1a&CCCCCCr   r   r    r!   )rG   binary_little_endianbinary_big_endianr  r  )r    r!   )littlebig)+r   iorV   	itertoolsr   r   numpyr   sysr   
_byteorderobjectr   rD   r   r   rI  rD  r   rP  rH   rI   rS   r   r8   r!  r$  r   r   _data_type_relationr-   r  r   r  r   
_types_set_a_brn   addrK   ro   r   rC   r   r   <module>r     s  $      ' ' ' ' ' '     ' ' ' ' ' 'A0 A0 A0 A0 A0f A0 A0 A0Hz! z! z! z! z! z! z! z!z{F {F {F {F {F& {F {F {F|n0 n0 n0 n0 n0k n0 n0 n0b	 	 	 	 	I 	 	 	 C  C  C  C  C=  C  C  CF+ + + + +- + + +6S S S S Sv S S SlA A A A Af A A AH: : :@E E E*@ @ @*  4	" 	" 	"  4( ( (   ( d&''TCC/BCCCCC SUU
#  HR	2r	2r   
 	  
 !$C00<   r   