o
    oiV                     @   sd   d dl mZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ G dd deZd	S )
    )AnyDictOptional)random_generator)AugmentationBase2D)Tensor)remap)create_meshgridc                       s   e Zd ZdZ			ddededededed	ed
df fddZded
dfddZ	ddede	e
ef de	e
ef dee d
ef
ddZ  ZS )RandomFisheyea  Add random camera radial distortion.

    .. image:: _static/img/RandomFisheye.png

    Args:
        center_x: Ranges to sample respect to x-coordinate center with shape (2,).
        center_y: Ranges to sample respect to y-coordinate center with shape (2,).
        gamma: Ranges to sample for the gamma values respect to optical center with shape (2,).
        same_on_batch: apply the same transformation across the batch.
        p: probability of applying the transformation.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it
                 to the batch form (False).

    Examples:
        >>> import torch
        >>> img = torch.ones(1, 1, 2, 2)
        >>> center_x = torch.tensor([-.3, .3])
        >>> center_y = torch.tensor([-.3, .3])
        >>> gamma = torch.tensor([.9, 1.])
        >>> out = RandomFisheye(center_x, center_y, gamma)(img)
        >>> out.shape
        torch.Size([1, 1, 2, 2])

    To apply the exact augmenation again, you may take the advantage of the previous parameter state:
        >>> input = torch.randn(1, 3, 32, 32)
        >>> aug = RandomFisheye(center_x, center_y, gamma, p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    F      ?center_xcenter_ygammasame_on_batchpkeepdimreturnNc                    s   t  j||d|d | | | | | | t|d d d f dd d f|d d d f dd d f|d d d f dd d f| _d S )Ng      ?)r   r   p_batchr   r   r   r   )super__init___check_tensorrgPlainUniformGenerator_param_generator)selfr   r   r   r   r   r   	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/geometric/fisheye.pyr   ;   s   	



zRandomFisheye.__init__datac                 C   sR   t |tstdt| t|jdkr%|jd dkr'td|j dd S d S )Nz+Invalid input type. Expected Tensor - got:    r      z#Tensor must be of shape (2,). Got: .)
isinstancer   	TypeErrortypelenshape
ValueError)r   r   r   r   r   r   N   s
   
zRandomFisheye._check_tensorinputparamsflags	transformc                 C   s   |j \}}}}t||dd}	|	d |}
|	d |}|d |dd|}|d |dd|}|d |dd|}||
 d	 || d	  d
 }|
|
||   }
||||   }t||
|dddS )NT)normalized_coordinates).r   ).r    r   r    r   r   r!   r   )r-   align_corners)r'   r	   toviewr   )r   r)   r*   r+   r,   B_HWgridfield_xfield_yr   r   r   distancer   r   r   apply_transformU   s   zRandomFisheye.apply_transform)Fr   F)N)__name__
__module____qualname____doc__r   boolfloatr   r   r   strr   r   r9   __classcell__r   r   r   r   r
      sB    $

r
   N)typingr   r   r   kornia.augmentationr   r   kornia.augmentation._2d.baser   kornia.corer   kornia.geometry.transformr   kornia.utilsr	   r
   r   r   r   r   <module>   s   