o
    oi                     @   s   d dl mZmZmZmZmZ d dl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 d dlmZmZ G d	d
 d
eZdS )    )AnyDictOptionalTupleUnionN)random_generator)GeometricAugmentationBase3D)Resample)Tensor)affine3d)_compute_rotation_matrix3d_compute_tensor_center3dc                       s   e Zd ZdZejjddddfdeee	e
e	e	e	f e
e
e	e	f e
e	e	f e
e	e	f f 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 )RandomRotation3Dag  Apply random rotations to 3D volumes (5D tensor).

    Input should be a tensor of shape (C, D, H, W) or a batch of tensors :math:`(B, C, D, H, W)`.
    If Input is a tuple it is assumed that the first element contains the aforementioned tensors and the second,
    the corresponding transformation matrix that has been applied to them. In this case the module
    will rotate the tensors and concatenate the corresponding transformation matrix to the
    previous one. This is especially useful when using this functionality as part of an ``nn.Sequential`` module.

    Args:
        degrees: Range of degrees to select from.
            If degrees is a number, then yaw, pitch, roll will be generated from the range of (-degrees, +degrees).
            If degrees is a tuple of (min, max), then yaw, pitch, roll will be generated from the range of (min, max).
            If degrees is a list of floats [a, b, c], then yaw, pitch, roll will be generated from (-a, a), (-b, b)
            and (-c, c).
            If degrees is a list of tuple ((a, b), (m, n), (x, y)), then yaw, pitch, roll will be generated from
            (a, b), (m, n) and (x, y).
            Set to 0 to deactivate rotations.
        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)
        >>> input = torch.rand(1, 1, 3, 3, 3)
        >>> aug = RandomRotation3D((15., 20., 20.), p=1.0)
        >>> aug(input), aug.transform_matrix
        (tensor([[[[[0.3819, 0.4886, 0.2111],
                   [0.1196, 0.3833, 0.4722],
                   [0.3432, 0.5951, 0.4223]],
        <BLANKLINE>
                  [[0.5553, 0.4374, 0.2780],
                   [0.2423, 0.1689, 0.4009],
                   [0.4516, 0.6376, 0.7327]],
        <BLANKLINE>
                  [[0.1605, 0.3112, 0.3673],
                   [0.4931, 0.4620, 0.5700],
                   [0.3505, 0.4685, 0.8092]]]]]), tensor([[[ 0.9722,  0.1131, -0.2049,  0.1196],
                 [-0.0603,  0.9669,  0.2478, -0.1545],
                 [ 0.2262, -0.2286,  0.9469,  0.0556],
                 [ 0.0000,  0.0000,  0.0000,  1.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 = RandomRotation3D((15., 20., 20.), p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    Fg      ?degrees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RotationGenerator3D_param_generator)selfr   r   r   r   r   r   	__class__ ^/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_3d/geometric/rotation.pyr   [   s   zRandomRotation3D.__init__inputparamsr   c           
      C   s   |d  |}|d  |}|d  |}t|}t|||||jd d}td|}	|d d df |	d d df< |d d df |	d d df< |d d df |	d d df< |	S )	Nyawpitchrollr            )tor   r   expandshapekorniaeye_like)
r   r"   r#   r   r$   r%   r&   centerrotation_mat	trans_matr    r    r!   compute_transformationm   s   z'RandomRotation3D.compute_transformation	transformc                 C   sL   t |tstdt| t||dd dd df |d j d|d S )Nz.Expected the transform to be a Tensor. Gotcha .   r(   r   zerosr   )
isinstancer
   	TypeErrortyper   namelower)r   r"   r#   r   r4   r    r    r!   apply_transform}   s   
0z RandomRotation3D.apply_transform)N)__name__
__module____qualname____doc__r	   BILINEARr:   r   r
   floatr   strintboolr   r   r   r3   r   r<   __classcell__r    r    r   r!   r      sR    E$	
*

r   )typingr   r   r   r   r   r.   kornia.augmentationr   r   &kornia.augmentation._3d.geometric.baser   kornia.constantsr	   kornia.corer
   kornia.geometryr   !kornia.geometry.transform.affwarpr   r   r   r    r    r    r!   <module>   s   