U
    `=(                     @   s|   d dl Zd dlZedddddddd	d
dddddddgZddddZdd Zdd Zd#ddZdd Z	d$dd Z
d!d" ZdS )%    N)s   int8i1)s   charr   )s   uint8u1)s   ucharr   )s   int16i2)s   shortr   )s   uint16u2)s   ushortr   )s   int32i4)s   intr   )s   uint32u4)s   uintr   )s   float32f4)s   floatr   )s   float64f8)s   doubler	    ><)asciiZbinary_big_endianZbinary_little_endianc                 C   s~   g }g }d }d|krv|dkrv|   }d|krB| }t|d }qd|kr| }||d  |t|d   f q||fS )N
   end_header    s   element      property   )readlinesplitintappenddecode
ply_dtypes)plyfileextline
properties
num_points r   %/workspace/OverlapPredator/lib/ply.pyparse_header<   s    $r    c                 C   s   g }g }d }d }d }d|kr|dkr|   }d|krNd}| }t|d }qd|krpd}| }t|d }qd|kr|dkr| }||d  |t|d	   f q|dkr|d
std| q|||fS )Nr   r   s   element vertexvertexr   s   element facefacer   r   zproperty list uchar intzUnsupported faces property : )r   r   r   r   r   r   
startswith
ValueError)r   r   r   Zvertex_propertiesr   	num_facesZcurrent_elementr   r   r   parse_mesh_headerP   s.    $
r&   Fc              	   C   s   t | d}d| kr td|  d  }|dkrDtdt| }|rt||\}}}tj|||d}d|d	 fd
|d fd|d fd|d fg}	tj||	|d}
t	|
d
 |
d |
d fj
}||g}nt||\}}tj|||d}W 5 Q R X |S )a  
    Read ".ply" files

    Parameters
    ----------
    filename : string
        the name of the file to read.

    Returns
    -------
    result : array
        data stored in the file

    Examples
    --------
    Store data in file

    >>> points = np.random.rand(5, 3)
    >>> values = np.random.randint(2, size=10)
    >>> write_ply('example.ply', [points, values], ['x', 'y', 'z', 'values'])

    Read the file

    >>> data = read_ply('example.ply')
    >>> values = data['values']
    array([0, 0, 1, 1, 0])

    >>> points = np.vstack((data['x'], data['y'], data['z'])).T
    array([[ 0.466  0.595  0.324]
           [ 0.538  0.407  0.654]
           [ 0.850  0.018  0.988]
           [ 0.395  0.394  0.363]
           [ 0.873  0.996  0.092]])

    rbs   plyz*The file does not start whith the word plyr   r   zThe file is not binary)dtypecountkr   v1r   v2v3)openr   r$   r   r   valid_formatsr&   npfromfilevstackTr    )filenameZtriangular_meshr   fmtr   r   r%   r   Zvertex_dataZface_propertiesZ
faces_datafacesdatar   r   r   read_plyq   s*    %




r8   c                 C   s\   g }| d| d jd   d}| D ]2}|jD ]&}| d|jj|| f  |d7 }q.q$|S )Nzelement vertex %dr   zproperty %s %sr   )r   shaper3   r(   name)
field_listfield_nameslinesifieldsfieldr   r   r   header_properties   s    
rA   c              	   C   s  t |tkst |tkr t|nt|f}t|D ]:\}}|jdk rT|dd||< |jdkr2td  dS q2dd |D }tt	||d std	 dS t
d
d |D }|t|krtd dS | ds| d7 } t| d~}dg}	|	dtj d  |	t|| |dk	r<|	d|jd  |	d |	d |	D ]}
|d|
  qJW 5 Q R X t| d@}d}g }|D ]2}|jD ]$}||| |jjfg7 }|d7 }qqtj|d jd |d}d}|D ](}|jD ]}|||| < |d7 }qq|| |dk	r|tj}dgdd tdD  }tj|jd |d}tj|jd fdtjd|d< |dddf |d< |dddf |d< |dddf |d< || W 5 Q R X dS ) aK  
    Write ".ply" files

    Parameters
    ----------
    filename : string
        the name of the file to which the data is saved. A '.ply' extension will be appended to the
        file name if it does no already have one.

    field_list : list, tuple, numpy array
        the fields to be saved in the ply file. Either a numpy array, a list of numpy arrays or a
        tuple of numpy arrays. Each 1D numpy array and each column of 2D numpy arrays are considered
        as one field.

    field_names : list
        the name of each fields as a list of strings. Has to be the same length as the number of
        fields.

    Examples
    --------
    >>> points = np.random.rand(10, 3)
    >>> write_ply('example1.ply', points, ['x', 'y', 'z'])

    >>> values = np.random.randint(2, size=10)
    >>> write_ply('example2.ply', [points, values], ['x', 'y', 'z', 'values'])

    >>> colors = np.random.randint(255, size=(10,3), dtype=np.uint8)
    >>> field_names = ['x', 'y', 'z', 'red', 'green', 'blue', values']
    >>> write_ply('example3.ply', [points, colors, values], field_names)

    r   r   z"fields have more than 2 dimensionsFc                 S   s   g | ]}|j d  qS )r   r9   .0r@   r   r   r   
<listcomp>   s     zwrite_ply.<locals>.<listcomp>r   zwrong field dimensionsc                 S   s   g | ]}|j d  qS )r   rC   rD   r   r   r   rF     s     zwrong number of field namesz.plywZplyzformat binary_z_endian 1.0Nzelement face {:d}z&property list uchar int vertex_indicesZ
end_headerz%s
ab)r(   )r*   uint8c                 S   s   g | ]}t |d fqS )int32)str)rE   indr   r   r   rF   ;  s        r*   012T) typelisttuple	enumeratendimreshapeprintr0   allequalsumlenendswithr.   r   sys	byteorderextendrA   formatr9   writer3   r(   rK   emptytofileastyperJ   rangefullrI   )r4   r;   r<   Ztriangular_facesr>   r@   n_pointsn_fieldsr   headerr   Z	type_listr?   r7   r   r   r   	write_ply   sf    "*









rj   c                 C   s   dddd}d|  d t t| g}| dkr:|d nHtt|jD ]8}|t |j| d	  }|d
| d |jj|   qH|S )z Takes the columns of the dataframe and builds a ply-like description

    Parameters
    ----------
    name: str
    df: pandas DataFrame

    Returns
    -------
    element: list[str]
    floatZucharr   )fur>   zelement  r"   z&property list uchar int points_indicesr   z	property )rK   r[   r   re   columnsdtypesvalues)r:   dfZproperty_formatselementr>   rl   r   r   r   describe_elementF  s     rt   )F)N)numpyr0   r]   dictr   r/   r    r&   r8   rA   rj   rt   r   r   r   r   <module>   s8   !
R
r