a
    d@.                     @   sz   d dl Z d dlmZ d dlmZ G dd deZG dd dZe jdd	d
Z	e jdddZ
e je je jdddZdS )    N)transform_points)create_meshgridc                       s"   e Zd Zed fddZ  ZS )StereoException)msgc                    s*   d}|| }t  j|g|R i | dS )a&  Custom exception for the :module:`~kornia.geometry.camera.stereo` module.

        Adds a general helper module redirecting the user to the proper documentation site.

        Args:
            msg: Custom message to add to the general message.
            *args:
            **kwargs:
        z
 Please check documents here: https://kornia.readthedocs.io/en/latest/geometry.camera.stereo.html for further information and examples.N)super__init__)selfr   argskwargsZdoc_helpZ	final_msg	__class__ f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/camera/stereo.pyr      s    zStereoException.__init__)__name__
__module____qualname__strr   __classcell__r   r   r   r   r      s   r   c                   @   s   e Zd ZejejdddZeejejdddZee	dddZ
eejdd	d
ZeejdddZeejdddZeejdddZeejdddZeejdddZeejdddZejdddZejejdddZdS )StereoCamera)rectified_left_camerarectified_right_camerac                 C   s:   |  || || _|| _| jj| _| jj| _|  | _dS )a>  Class representing a horizontal stereo camera setup.

        Args:
            rectified_left_camera: The rectified left camera projection matrix
              of shape :math:`(B, 3, 4)`
            rectified_right_camera: The rectified right camera projection matrix
              of shape :math:`(B, 3, 4)`
        N)_check_stereo_camerar   r   devicedtype_init_Q_matrix	_Q_matrix)r   r   r   r   r   r   r      s    	

zStereoCamera.__init__c              
   C   s  t | jdkr td| j dt |jdkr@td|j d| jdd dkrltd| jdd  d|jdd dkrtd	|jdd  d| j|jkrtd
| j d|j d| j|jkrtd| j d|j dtt| dddddf |dddddf sVtd| dddddf  d|dddddf  d|d }tt|drtd| ddS )aL  Utility function to ensure user specified correct camera matrices.

        Args:
            rectified_left_camera: The rectified left camera projection matrix
              of shape :math:`(B, 3, 4)`
            rectified_right_camera: The rectified right camera projection matrix
              of shape :math:`(B, 3, 4)`
           z;Expected 'rectified_left_camera' to have 3 dimensions. Got .z;Expected 'rectified_right_camera' to have 3 dimension. Got N   )r      z@Expected each 'rectified_left_camera' to be of shape (3, 4).Got zAExpected each 'rectified_right_camera' to be of shape (3, 4).Got z\Expected 'rectified_left_camera' and 'rectified_right_camera' to be on the same devices.Got z and zTExpected 'rectified_left_camera' and 'rectified_right_camera' tohave same dtype.Got .ztExpected 'left_rectified_camera' and 'rectified_right_camera' to havesame parameters except for the last column.Got .r   r   r   z.Expected :math:`T_x * f_x` to be negative.Got )	lenshaper   r   r   torchalleqgt)r   r   Ztx_fxr   r   r   r   /   s^    8z!StereoCamera._check_stereo_camera)returnc                 C   s   | j jd S )zfReturn the batch size of the storage.

        Returns:
           scalar with the batch size
        r   )r   r"   r   r   r   r   
batch_sizem   s    zStereoCamera.batch_sizec                 C   s
   | j d S )zReturn the focal length in the x-direction.

        Note that the focal lengths of the rectified left and right
        camera are assumed to be equal.

        Returns:
            tensor of shape :math:`(B)`
        ).r   r   r   r(   r   r   r   fxv   s    
zStereoCamera.fxc                 C   s
   | j d S )zReturns the focal length in the y-direction.

        Note that the focal lengths of the rectified left and right
        camera are assumed to be equal.

        Returns:
            tensor of shape :math:`(B)`
        ).r   r   r*   r(   r   r   r   fy   s    
zStereoCamera.fyc                 C   s
   | j d S )zReturn the x-coordinate of the principal point for the left camera.

        Returns:
            tensor of shape :math:`(B)`
        .r      r*   r(   r   r   r   cx_left   s    zStereoCamera.cx_leftc                 C   s
   | j d S )zReturn the x-coordinate of the principal point for the right camera.

        Returns:
            tensor of shape :math:`(B)`
        r-   )r   r(   r   r   r   cx_right   s    zStereoCamera.cx_rightc                 C   s
   | j d S )zReturn the y-coordinate of the principal point.

        Note that the y-coordinate of the principal points
        is assumed to be equal for the left and right camera.

        Returns:
            tensor of shape :math:`(B)`
        ).r   r.   r*   r(   r   r   r   cy   s    
zStereoCamera.cyc                 C   s   | j d  | j S )zsThe horizontal baseline between the two cameras.

        Returns:
            Tensor of shape :math:`(B)`
        r    )r   r+   r(   r   r   r   tx   s    zStereoCamera.txc                 C   s   | j S )aP  The Q matrix of the horizontal stereo setup.

        This matrix is used for reprojecting a disparity tensor to
        the corresponding point cloud. Note that this is in a general form that allows different focal
        lengths in the x and y direction.

        Return:
            The Q matrix of shape :math:`(B, 4, 4)`.
        )r   r(   r   r   r   Q   s    zStereoCamera.Qc                 C   s   t j| jddf| j| jd}| j }| j| |ddddf< | j | j | |ddddf< | j| |ddddf< | j | j	 | |ddddf< | j| j | |ddddf< | j |ddddf< | j| j| j
  |ddddf< |S )zInitialized the Q matrix of the horizontal stereo setup. See the Q property.

        Returns:
            The Q matrix of shape :math:`(B, 4, 4)`.
        r   )r   r   Nr   r   r   r.   )r#   zerosr)   r   r   r2   r,   r/   r+   r1   r0   )r   r3   Zbaseliner   r   r   r      s       zStereoCamera._init_Q_matrix)disparity_tensorr'   c                 C   s   t || jS )zReproject the disparity tensor to a 3D point cloud.

        Args:
            disparity_tensor: Disparity tensor of shape :math:`(B, 1, H, W)`.

        Returns:
            The 3D point cloud of shape :math:`(B, H, W, 3)`
        )reproject_disparity_to_3Dr3   )r   r5   r   r   r   r6      s    	z&StereoCamera.reproject_disparity_to_3DN)r   r   r   r#   Tensorr   staticmethodr   propertyintr)   r+   r,   r/   r0   r1   r2   r3   r   r6   r   r   r   r   r      s*   =r   r5   c                 C   s   t | tjs tdt|  dt| jdkr@td| j d| jd dkr`td| j d| jtjtj	tj
fvrtd| j d	S )
zUtility function to ensure correct user provided correct disparity tensor.

    Args:
        disparity_tensor: The disparity tensor of shape :math:`(B, 1, H, W)`.
    zFExpected 'disparity_tensor' to be an instance of torch.Tensor but got r   r   z5Expected 'disparity_tensor' to have 4 dimensions.Got r   z]Expected dimension 1 of 'disparity_tensor' to be 1 for as single channeled disparity map.Got z\Expected 'disparity_tensor' to have dtype torch.float16, torch.float32 or torch.float64.Got N
isinstancer#   r7   r   typer!   r"   r   float16float32float64r;   r   r   r   _check_disparity_tensor   s$    rC   Q_matrixc                 C   s   t | tjs tdt|  dt| jdks>td| j | jdd dks`td| j | jtjtj	tj
fvrtd	| j dS )
zUtility function to ensure Q matrix is of correct form.

    Args:
        Q_matrix: The Q matrix for reprojecting disparity to a point cloud of shape :math:`(B, 4, 4)`
    z>Expected 'Q_matrix' to be an instance of torch.Tensor but got r   r   z-Expected 'Q_matrix' to have 3 dimensions.Got r   N)r   r   zEExpected last two dimensions of 'Q_matrix' to be of shape (4, 4).Got zTExpected 'Q_matrix' to be of type torch.float16, torch.float32 or torch.float64.Got r=   rD   r   r   r   _check_Q_matrix   s    

rF   )r5   rE   r'   c                 C   s   t | t|  | j\}}}}| j}| j}t||d||d}||ddd}tj|dd\}	}
t	|	dt	|
d }	}
t
|
|	| fd|ddddd}t|||||d}|j|||dfkstd	|||df d
|j d|S )a  Reproject the disparity tensor to a 3D point cloud.

    Args:
        disparity_tensor: Disparity tensor of shape :math:`(B, H, W, 1)`.
        Q_matrix: Tensor of Q matrices of shapes :math:`(B, 4, 4)`.

    Returns:
        The 3D point cloud of shape :math:`(B, H, W, 3)`
    F)Znormalized_coordinatesr   r   r<   )Zdimr   r   r   r.   z]Something went wrong in `reproject_disparity_to_3D`. Expected the final outputto be of shape z(.But the computed point cloud had shape zO. Please ensure input are correct. If this is an error, please submit an issue.)rF   rC   r"   r   r   r   expandr#   ZunbindZ	unsqueezestackZreshapeZpermuter   r   )r5   rE   r)   rowscols_r   r   ZuvvuZuvdZpointsr   r   r   r6     s(    
&
r6   )r#   Zkornia.geometry.linalgr   Zkornia.utils.gridr   	Exceptionr   r   r7   rC   rF   r6   r   r   r   r   <module>   s    D