o
    oi                     @  s   d dl mZ d dlmZ d dlmZmZ d dlm  m	Z
 d dlmZmZ d dlmZ ddlmZmZ d	d
gZG dd	 d	eZG dd
 d
eZdS )    )annotations)abstractmethod)AnyOptionalN)ModuleTensor)create_meshgrid   )homography_warp	warp_grid
BaseWarperHomographyWarperc                      s<   e Zd Zd fd	d
ZeddddZedddZ  ZS )r   heightintwidthargsr   kwargsreturnNonec                   s"   t  j|i | || _|| _d S N)super__init__r   r   )selfr   r   r   r   	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/transform/homography_warper.pyr   "   s   
zBaseWarper.__init__N	patch_srcr   src_homo_dstOptional[Tensor]c                 C     d S r   r   )r   r   r   r   r   r   forward'      zBaseWarper.forwardc                 C  r    r   r   r   r   r   r   r   precompute_warp_grid*   r"   zBaseWarper.precompute_warp_grid)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname__r   r   r!   r$   __classcell__r   r   r   r   r   !   s    c                      sL   e Zd ZU dZded< 				dd fddZdddZd d!ddZ  ZS )"r   a$  Warp tensors by homographies.

    .. math::

        X_{dst} = H_{src}^{\{dst\}} * X_{src}

    Args:
        height: The height of the destination tensor.
        width: The width of the destination tensor.
        mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        normalized_coordinates: whether to use a grid with normalized coordinates.
        align_corners: interpolation flag.

    r   _warped_gridbilinearzerosTFr   r   r   modestrpadding_modenormalized_coordinatesboolalign_cornersr   r   c                   s@   t  || || _|| _|| _|| _t|||d| _d | _d S )N)r1   )	r   r   r.   r0   r1   r3   r   gridr+   )r   r   r   r.   r0   r1   r3   r   r   r   r   B   s   	
zHomographyWarper.__init__r   r   c                 C  s   t | j|| _dS )a  Compute and store internally the transformations of the points.

        Useful when the same homography/homographies are reused.

        Args:
            src_homo_dst: Homography or homographies (stacked) to
              transform all points in the grid. Shape of the homography
              has to be :math:`(1, 3, 3)` or :math:`(N, 1, 3, 3)`.
              The homography assumes normalized coordinates [-1, 1] if
              normalized_coordinates is True.

        N)r   r4   r+   r#   r   r   r   r$   V   s   z%HomographyWarper.precompute_warp_gridNr   c              	   C  s   | j }|durt||| j| jf| j| j| j| jd}|S |durB|j|jks3t	d|j d|j dt
j||| j| j| jd}|S td)a  Warp a tensor from source into reference frame.

        Args:
            patch_src: The tensor to warp.
            src_homo_dst: The homography or stack of
              homographies from destination to source. The homography assumes
              normalized coordinates [-1, 1] if normalized_coordinates is True.

        Return:
            Patch sampled at locations from source to destination.

        Shape:
            - Input: :math:`(N, C, H, W)` and :math:`(N, 3, 3)`
            - Output: :math:`(N, C, H, W)`

        Example:
            >>> input = torch.rand(1, 3, 32, 32)
            >>> homography = torch.eye(3).view(1, 3, 3)
            >>> warper = HomographyWarper(32, 32)
            >>> # without precomputing the warp
            >>> output = warper(input, homography)  # NxCxHxW
            >>> # precomputing the warp
            >>> warper.precompute_warp_grid(homography)
            >>> output = warper(input)  # NxCxHxW

        N)r.   r0   r3   r1   zDPatch and warped grid must be on the same device. Got patch.device: z warped_grid.device: zm. Whether recall precompute_warp_grid() with the correct device for the homograhy or change the patch device.)r.   r0   r3   zUnknown warping. If homographies are not provided                                 they must be preset using the method:                                 precompute_warp_grid().)r+   r
   r   r   r.   r0   r3   r1   device	TypeErrorFgrid_sampleRuntimeError)r   r   r   r+   warped_patchr   r   r   r!   e   s@   
zHomographyWarper.forward)r,   r-   TF)r   r   r   r   r.   r/   r0   r/   r1   r2   r3   r2   r   r   r&   r   r%   )	r'   r(   r)   __doc____annotations__r   r$   r!   r*   r   r   r   r   r   .   s   
 
)
__future__r   abcr   typingr   r   torch.nn.functionalnn
functionalr7   kornia.corer   r   kornia.utilsr   imgwarpr
   r   __all__r   r   r   r   r   r   <module>   s   