o
    oi                     @   st   d dl mZ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mZ d dlmZmZ G dd de
Zd	S )
    )AnyDictOptionalTupleUnion)random_generator)GeometricAugmentationBase3D)Resample)Tensorpad)crop_by_transform_mat3dget_perspective_transform3dc                       sV  e Zd ZdZddddejjddddf	deeeef d	e	e
eeeeef eeeeeeef f  d
e	e dedede
eeef dededededdf fddZd"dede	eeef  de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d"dede	eeef  dedef fd d!Z  ZS )#RandomCrop3Da
  Apply random crop on 3D volumes (5D tensor).

    Crops random sub-volumes on a given size.

    Args:
        p: probability of applying the transformation for the whole batch.
        size: Desired output size (out_d, out_h, out_w) of the crop.
            Must be Tuple[int, int, int], then out_d = size[0], out_h = size[1], out_w = size[2].
        padding: Optional padding on each border of the image.
            Default is None, i.e no padding. If a sequence of length 6 is provided, it is used to pad
            left, top, right, bottom, front, back borders respectively.
            If a sequence of length 3 is provided, it is used to pad left/right,
            top/bottom, front/back borders, respectively.
        pad_if_needed: It will pad the image if smaller than the
            desired size to avoid raising an exception. Since cropping is done
            after padding, the padding seems to be done at a random offset.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant.
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
        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, , out_d, out_h, out_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.randn(1, 1, 3, 3, 3)
        >>> aug = RandomCrop3D((2, 2, 2), p=1.)
        >>> aug(inputs)
        tensor([[[[[-1.1258, -1.1524],
                   [-0.4339,  0.8487]],
        <BLANKLINE>
                  [[-1.2633,  0.3500],
                   [ 0.1665,  0.8744]]]]])

    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 = RandomCrop3D((24, 24, 24), p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    NFr   constantT      ?sizepaddingpad_if_neededfillpadding_moderesamplesame_on_batchalign_cornerspkeepdimreturnc                    sB   t  jd||	|
d |||||t||d| _t|d | _d S )Nr   )r   r   p_batchr   )r   r   r   r   r   r   r   )super__init__r	   getflagsrgCropGenerator3D_param_generator)selfr   r   r   r   r   r   r   r   r   r   	__class__ Z/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_3d/geometric/crop.pyr   S   s   	zRandomCrop3D.__init__inputr    c                 C   s4  |d u r| j n|}|d }|d urwt|tr||||||g}nLt|ttfrAt|dkrA|d |d |d |d |d |d g}n*t|ttfrct|dkrc|d |d |d |d |d |d g}ntd	| d
t|||d |d d}|d r|jd |d d k rdddd|d d |jd  |d d |jd  g}t|||d |d d}|d r|jd |d d k rdd|d d |jd  |d d |jd  ddg}t|||d |d d}|d r|jd |d d k r|d d |jd  |d d |jd  ddddg}t|||d |d d}|S )Nr      r                  zD`padding` must be an integer, 3-element-list or 6-element-list. Got .r   r   )valuemoder   r   )	r    
isinstanceinttuplelistlen
ValueErrorr   shape)r$   r)   r    r   r'   r'   r(   precrop_paddingm   s*   
**44"4zRandomCrop3D.precrop_paddingparamsc                 C   s6   t |d ||d |}||jd dd}|S )Nsrcdstr   r5   )r   toexpandr<   r$   r)   r>   r    	transformr'   r'   r(   compute_transformation   s   z#RandomCrop3D.compute_transformationrD   c                 C   s@   t |tstdt| t|||d |d j |d dS )Nz.Expected the transform to be a Tensor. Gotcha r   r   r   )r2   r   )r6   r
   	TypeErrortyper   namelowerrC   r'   r'   r(   apply_transform   s
   
zRandomCrop3D.apply_transformkwargsc                    s   |  |}t ||S N)r=   r   forward)r$   r)   r>   rK   r%   r'   r(   rM      s   
zRandomCrop3D.forwardrL   )__name__
__module____qualname____doc__r	   BILINEARrH   r   r7   r   r   boolstrfloatr   r
   r   r   r=   rE   rJ   rM   __classcell__r'   r'   r%   r(   r      sb    :*	
$*


4
r   N)typingr   r   r   r   r   kornia.augmentationr   r!   &kornia.augmentation._3d.geometric.baser   kornia.constantsr	   kornia.corer
   r   kornia.geometryr   r   r   r'   r'   r'   r(   <module>   s   