a
    d@                     @   sf   d dl Z d dlm  mZ d dlmZmZ e je je jdddZ	d
e je je je
e jddd	ZdS )    N)convert_points_from_homogeneousconvert_points_to_homogeneous)point_3dcamera_matrixreturnc                 C   s   t | tjstdt|  t |tjs<tdt| | j|jksPtd| jd dksntd| j|jdd d	kstd
t	| }|d }|d }|d }|d }|d }|d }|| | }	|| | }
tj
|	|
gddS )aY  Project a 3d point onto the 2d camera plane.

    Args:
        point3d: tensor containing the 3d points to be projected
            to the camera plane. The shape of the tensor can be :math:`(*, 3)`.
        camera_matrix: tensor containing the intrinsics camera
            matrix. The tensor shape must be :math:`(*, 3, 3)`.

    Returns:
        tensor of (u, v) cam coordinates with shape :math:`(*, 2)`.

    Example:
        >>> _ = torch.manual_seed(0)
        >>> X = torch.rand(1, 3)
        >>> K = torch.eye(3)[None]
        >>> project_points(X, K)
        tensor([[5.6088, 8.6827]])
    z/Input point_3d type is not a torch.Tensor. Got 4Input camera_matrix type is not a torch.Tensor. Got -Input tensors must be all in the same device.   z6Input points_3d must be in the shape of (*, 3). Got {}Nr
   r
   6Input camera_matrix must be in the shape of (*, 3, 3)..r   .   .r   r   .r   r   .r      .r   r   dim)
isinstancetorchTensor	TypeErrortypedevice
ValueErrorshapeformatr   stack)r   r   Z	xy_coordsx_coordy_coordfxfycxcyu_coordv_coord r*   k/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/camera/perspective.pyproject_points   s(    r,   F)point_2ddepthr   	normalizer   c                 C   sT  t | tjstdt|  t |tjs<tdt| t |tjsZtdt| | j|j  krt|jks~n td| jd dkstd| j|jd dkstd	|j|jd
d dkstd| d }| d }|d }|d }|d }|d }	|| | }
||	 | }tj	|
|gdd}t
|}|rLtj|ddd}|| S )a	  Unproject a 2d point in 3d.

    Transform coordinates in the pixel frame to the camera frame.

    Args:
        point2d: tensor containing the 2d to be projected to
            world coordinates. The shape of the tensor can be :math:`(*, 2)`.
        depth: tensor containing the depth value of each 2d
            points. The tensor shape must be equal to point2d :math:`(*, 1)`.
        camera_matrix: tensor containing the intrinsics camera
            matrix. The tensor shape must be :math:`(*, 3, 3)`.
        normalize: whether to normalize the pointcloud. This
            must be set to `True` when the depth is represented as the Euclidean
            ray length from the camera position.

    Returns:
        tensor of (x, y, z) world coordinates with shape :math:`(*, 3)`.

    Example:
        >>> _ = torch.manual_seed(0)
        >>> x = torch.rand(1, 2)
        >>> depth = torch.ones(1, 1)
        >>> K = torch.eye(3)[None]
        >>> unproject_points(x, depth, K)
        tensor([[0.4963, 0.7682, 1.0000]])
    z/Input point_2d type is not a torch.Tensor. Got z,Input depth type is not a torch.Tensor. Got r   r   r	   r   z6Input points_2d must be in the shape of (*, 2). Got {}r   z2Input depth must be in the shape of (*, 1). Got {}r   Nr   r   r   r   r   r   r   r   r   g       @)r   p)r   r   r   r   r   r   r   r   r    r!   r   Fr/   )r-   r.   r   r/   r(   r)   r$   r%   r&   r'   r"   r#   Zxyzr*   r*   r+   unproject_points?   s6    r2   )F)r   Ztorch.nn.functionalnnZ
functionalr1   Zkornia.geometry.conversionsr   r   r   r,   boolr2   r*   r*   r*   r+   <module>   s   9 