o
    oi                     @   sF   d dl Z d dlZdedejddfddZddededejfd	d
ZdS )    Nfilename
pointcloudreturnc                 C   sV  t | tr|  dstd| t|s!tdt| |jdk s-|j	d dkr7tdt
|j	 |dd}t|jdd	}|| }|j	d
 }t| dddK}|dddd| dddddg |d
kr|  tj}| D ]\}}	}
||dd|	dd|
dd qzW d   dS W d   dS 1 sw   Y  dS )aB  Save to disk a pointcloud in PLY format.

    Args:
        filename: the path to save the pointcloud.
        pointcloud: tensor containing the pointcloud to save.
          The tensor must be in the shape of :math:`(*, 3)` where the last
          component is assumed to be a 3d point coordinate :math:`(X, Y, Z)`.
    .ply=Input filename must be a string with the .ply extension. Got z1Input pointcloud type is not a torch.Tensor. Got       z/Input pointcloud must have shape (..., 3). Got    )dimr   wutf-8)encodingzply
zformat ascii 1.0
zcomment arraiy generated
zelement vertex 
zproperty double x
zproperty double y
zproperty double z
zend_header
z.9g N)
isinstancestrlowerendswith	TypeErrortorch	is_tensortypendimshapetuplereshapeisfiniteanyopen
writelinesdetachcputofloat64tolistwrite)r   r   xyz
valid_maskvalid_pointsvalid_countfarrxyz r0   N/home/ubuntu/.local/lib/python3.10/site-packages/kornia/utils/pointcloud_io.pysave_pointcloud_ply   s<   	


$"r2      header_sizec                 C   s  t | tr|  dstd| tj| stdt |t	r&|dks.td| dt
| d}t|D ]}|  q8| }W d   n1 sMw   Y  |jd	d
d}| }t|d dkrotdt| dtjttt|dt|d d  tjddd}|S )a-  Load from disk a pointcloud in PLY format.

    Args:
        filename: the path to the pointcloud.
        header_size: the number of header lines to skip.

    Return:
        tensor containing the loaded points with shape :math:`(*, 3)` where
        :math:`*` represents the number of points.
    r   r   z'Input filename is not an existing file.r   z2Input header_size must be a positive integer. Got .rbNr   ignore)errorsr	   z-Expected 3 columns per point, got a total of z values.)dtyper   )r   r   r   r   r   ospathisfile
ValueErrorintr   rangereadlinereaddecodesplitlenr   tensorlistmapfloatfloat32view)r   r4   r+   _raw_datatextpartsrE   r0   r0   r1   load_pointcloud_plyG   s"   

6rO   )r3   )r:   r   r   Tensorr2   r>   rO   r0   r0   r0   r1   <module>   s   0