o
    oi                     @   sl   d dl mZ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mZ G dd de	Zd	S )
    )AnyDictOptionalUnion)random_generator)GeometricAugmentationBase3D)Resample)Tensor)get_perspective_transform3dwarp_perspective3dc                       s   e Zd ZdZdejjddddfdeee	f dee
eef dedede	d	ed
df fddZdedee
ef dee
ef d
e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 )RandomPerspective3Da  Apply andom perspective transformation to 3D volumes (5D tensor).

    Args:
        p: probability of the image being perspectively transformed.
        distortion_scale: it controls the degree of distortion and ranges from 0 to 1.
        resample: resample mode from "nearest" (0) or "bilinear" (1).
        same_on_batch: apply the same transformation across the batch.
        align_corners: interpolation flag.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it
          to the batch form (False).

    Shape:
        - Input: :math:`(C, D, H, W)` or :math:`(B, C, D, H, W)`, Optional: :math:`(B, 4, 4)`
        - Output: :math:`(B, C, D, H, W)`

    Note:
        Input tensor must be float and normalized into [0, 1] for the best differentiability support.
        Additionally, this function accepts another transformation tensor (:math:`(B, 4, 4)`), then the
        applied transformation will be merged int to the input transformation tensor and returned.

    Examples:
        >>> import torch
        >>> rng = torch.manual_seed(0)
        >>> inputs= torch.tensor([[[
        ...    [[1., 0., 0.],
        ...     [0., 1., 0.],
        ...     [0., 0., 1.]],
        ...    [[1., 0., 0.],
        ...     [0., 1., 0.],
        ...     [0., 0., 1.]],
        ...    [[1., 0., 0.],
        ...     [0., 1., 0.],
        ...     [0., 0., 1.]]
        ... ]]])
        >>> aug = RandomPerspective3D(0.5, p=1., align_corners=True)
        >>> aug(inputs)
        tensor([[[[[0.3976, 0.5507, 0.0000],
                   [0.0901, 0.3668, 0.0000],
                   [0.0000, 0.0000, 0.0000]],
        <BLANKLINE>
                  [[0.2651, 0.4657, 0.0000],
                   [0.1390, 0.5174, 0.0000],
                   [0.0000, 0.0000, 0.0000]],
        <BLANKLINE>
                  [[0.0000, 0.1153, 0.0000],
                   [0.0000, 0.0000, 0.0000],
                   [0.0000, 0.0000, 0.0000]]]]])

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

    g      ?Fdistortion_scaleresamplesame_on_batchalign_cornerspkeepdimreturnNc                    s4   t  j|||d t||d| _t|| _d S )N)r   r   r   )r   r   )super__init__r   getflagsrgPerspectiveGenerator3D_param_generator)selfr   r   r   r   r   r   	__class__ a/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_3d/geometric/perspective.pyr   T   s   	zRandomPerspective3D.__init__inputparamsr   c                 C   s   t |d |d |S )Nstart_points
end_points)r
   to)r   r    r!   r   r   r   r   compute_transformationa   s   z*RandomPerspective3D.compute_transformation	transformc                 C   sT   t |tstdt| t|||jd |jd |jd f|d j |d dS )Nz.Expected the transform to be a Tensor. Gotcha r   r   )r   r   )
isinstancer	   	TypeErrortyper   shapenamelower)r   r    r!   r   r&   r   r   r   apply_transformd   s   
z#RandomPerspective3D.apply_transform)N)__name__
__module____qualname____doc__r   BILINEARr.   r   r	   floatstrintboolr   r   r   r%   r   r0   __classcell__r   r   r   r   r      sH    :
*

r   N)typingr   r   r   r   kornia.augmentationr   r   &kornia.augmentation._3d.geometric.baser   kornia.constantsr   kornia.corer	   kornia.geometryr
   r   r   r   r   r   r   <module>   s   