o
    oi+8                     @   s  d dl mZ d dlZd dlmZmZ ddlmZmZ g dZ			dd	ej
d
ej
deeeef dededej
fddZ	d d	ej
deeeef dededej
f
ddZ		dd	ej
dej
dej
dededej
fddZ			d!d	ej
dej
deeeef dedededej
fddZdS )"    )TupleN)infer_bbox_shape3dvalidate_bbox3d   )get_perspective_transform3dwarp_affine3d)center_crop3dcrop_and_resize3dcrop_by_boxes3dcrop_by_transform_mat3dbilinearFtensorboxessizeinterpolationalign_cornersreturnc           
      C   sH  t | tjstdt|  t |tjstdt| t |ttfs2t|dkr2td| t| j	dkrBt
d| j	 d|d |d	 |d
 }}}|}tjg d|d	 ddg|d	 |d	 dgd|d	 dgdd|d	 g|d	 d|d	 g|d	 |d	 |d	 gd|d	 |d	 ggg| j| jd|j	d dd}	t| ||	||S )a  Extract crops from 3D volumes (5D tensor) and resize them.

    Args:
        tensor: the 3D volume tensor with shape (B, C, D, H, W).
        boxes: a tensor with shape (B, 8, 3) containing the coordinates of the bounding boxes
            to be extracted. The tensor must have the shape of Bx8x3, where each box is defined in the clockwise
            order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
            back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in x, y, z order.
        size: a tuple with the height and width that will be
            used to resize the extracted patches.
        interpolation: Interpolation flag.
        align_corners: mode for grid_generation.

    Returns:
        tensor containing the patches with shape (Bx)CxN1xN2xN3.

    Example:
        >>> input = torch.arange(64, dtype=torch.float32).view(1, 1, 4, 4, 4)
        >>> input
        tensor([[[[[ 0.,  1.,  2.,  3.],
                   [ 4.,  5.,  6.,  7.],
                   [ 8.,  9., 10., 11.],
                   [12., 13., 14., 15.]],
        <BLANKLINE>
                  [[16., 17., 18., 19.],
                   [20., 21., 22., 23.],
                   [24., 25., 26., 27.],
                   [28., 29., 30., 31.]],
        <BLANKLINE>
                  [[32., 33., 34., 35.],
                   [36., 37., 38., 39.],
                   [40., 41., 42., 43.],
                   [44., 45., 46., 47.]],
        <BLANKLINE>
                  [[48., 49., 50., 51.],
                   [52., 53., 54., 55.],
                   [56., 57., 58., 59.],
                   [60., 61., 62., 63.]]]]])
        >>> boxes = torch.tensor([[
        ...     [1., 1., 1.],
        ...     [3., 1., 1.],
        ...     [3., 3., 1.],
        ...     [1., 3., 1.],
        ...     [1., 1., 2.],
        ...     [3., 1., 2.],
        ...     [3., 3., 2.],
        ...     [1., 3., 2.],
        ... ]])  # 1x8x3
        >>> crop_and_resize3d(input, boxes, (2, 2, 2), align_corners=True)
        tensor([[[[[21.0000, 23.0000],
                   [29.0000, 31.0000]],
        <BLANKLINE>
                  [[37.0000, 39.0000],
                   [45.0000, 47.0000]]]]])

    -Input tensor type is not a torch.Tensor. Got z,Input boxes type is not a torch.Tensor. Got    1Input size must be a tuple/list of length 3. Got    6Only tensor with shape (B, C, D, H, W) supported. Got .r   r      r   r   r   )dtypedevice)
isinstancetorchTensor	TypeErrortypetuplelistlen
ValueErrorshapeAssertionErrorr   r   r   expandr
   )
r   r   r   r   r   dst_ddst_hdst_w
points_src
points_dst r/   T/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/transform/crop3d.pyr	      s6   ?r	   Tc                 C   s  t | tjstdt|  t| jdkrtd| j dt |tt	fs3t|dkr3t
d| |\}}}| jdd \}}}	|d	 }
|d	 }|d	 }|d	 }|d	 }|	d	 }|| }|| }||
 }|| d
 }|| d
 }|| d
 }tj|||g|||g|||g|||g|||g|||g|||g|||ggg| jd}tjg d|d
 ddg|d
 |d
 dgd|d
 dgdd|d
 g|d
 d|d
 g|d
 |d
 |d
 gd|d
 |d
 ggg| jd|jd dd}t| || j|| j||S )a  Crop the 3D volumes (5D tensor) at the center.

    Args:
        tensor: the 3D volume tensor with shape (B, C, D, H, W).
        size: a tuple with the expected depth, height and width
            of the output patch.
        interpolation: Interpolation flag.
        align_corners : mode for grid_generation.

    Returns:
        the output tensor with patches.

    Examples:
        >>> input = torch.arange(64, dtype=torch.float32).view(1, 1, 4, 4, 4)
        >>> input
        tensor([[[[[ 0.,  1.,  2.,  3.],
                   [ 4.,  5.,  6.,  7.],
                   [ 8.,  9., 10., 11.],
                   [12., 13., 14., 15.]],
        <BLANKLINE>
                  [[16., 17., 18., 19.],
                   [20., 21., 22., 23.],
                   [24., 25., 26., 27.],
                   [28., 29., 30., 31.]],
        <BLANKLINE>
                  [[32., 33., 34., 35.],
                   [36., 37., 38., 39.],
                   [40., 41., 42., 43.],
                   [44., 45., 46., 47.]],
        <BLANKLINE>
                  [[48., 49., 50., 51.],
                   [52., 53., 54., 55.],
                   [56., 57., 58., 59.],
                   [60., 61., 62., 63.]]]]])
        >>> center_crop3d(input, (2, 2, 2), align_corners=True)
        tensor([[[[[21.0000, 22.0000],
                   [25.0000, 26.0000]],
        <BLANKLINE>
                  [[37.0000, 38.0000],
                   [41.0000, 42.0000]]]]])

    r   r   r   r   r   r   Nr   r   )r   r   r   r   )r   r   r    r!   r"   r%   r'   r(   r#   r$   r&   r   r   r)   r
   tor   )r   r   r   r   r*   r+   r,   src_dsrc_hsrc_w
dst_d_half
dst_h_half
dst_w_half
src_d_half
src_h_half
src_w_halfstart_xstart_ystart_zend_xend_yend_zr-   r.   r/   r/   r0   r      sf   -
r   src_boxdst_boxc                 C   s(  t | t | t| jdkrtd| j dt|| j|| j}|| jd dd| }t	|}|d |d d k
 rZ|d |d d k
 rZ|d |d d k
 sntd|d  d	|d  d
|d  dt| |t|d d  t|d d  t|d d  f||d}|S )a  Perform crop transform on 3D volumes (5D tensor) by bounding boxes.

    Given an input tensor, this function selected the interested areas by the provided bounding boxes (src_box).
    Then the selected areas would be fitted into the targeted bounding boxes (dst_box) by a perspective transformation.
    So far, the ragged tensor is not supported by PyTorch right now. This function hereby requires the bounding boxes
    in a batch must be rectangles with same width, height and depth.

    Args:
        tensor : the 3D volume tensor with shape (B, C, D, H, W).
        src_box : a tensor with shape (B, 8, 3) containing the coordinates of the bounding boxes
            to be extracted. The tensor must have the shape of Bx8x3, where each box is defined in the clockwise
            order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
            back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in x, y, z order.
        dst_box: a tensor with shape (B, 8, 3) containing the coordinates of the bounding boxes
            to be placed. The tensor must have the shape of Bx8x3, where each box is defined in the clockwise
            order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
            back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in x, y, z order.
        interpolation: Interpolation flag.
        align_corners: mode for grid_generation.

    Returns:
        the output tensor with patches.

    Examples:
        >>> input = torch.tensor([[[
        ...         [[ 0.,  1.,  2.,  3.],
        ...          [ 4.,  5.,  6.,  7.],
        ...          [ 8.,  9., 10., 11.],
        ...          [12., 13., 14., 15.]],
        ...         [[16., 17., 18., 19.],
        ...          [20., 21., 22., 23.],
        ...          [24., 25., 26., 27.],
        ...          [28., 29., 30., 31.]],
        ...         [[32., 33., 34., 35.],
        ...          [36., 37., 38., 39.],
        ...          [40., 41., 42., 43.],
        ...          [44., 45., 46., 47.]]]]])
        >>> src_box = torch.tensor([[
        ...     [1., 1., 1.],
        ...     [3., 1., 1.],
        ...     [3., 3., 1.],
        ...     [1., 3., 1.],
        ...     [1., 1., 2.],
        ...     [3., 1., 2.],
        ...     [3., 3., 2.],
        ...     [1., 3., 2.],
        ... ]])  # 1x8x3
        >>> dst_box = torch.tensor([[
        ...     [0., 0., 0.],
        ...     [2., 0., 0.],
        ...     [2., 2., 0.],
        ...     [0., 2., 0.],
        ...     [0., 0., 1.],
        ...     [2., 0., 1.],
        ...     [2., 2., 1.],
        ...     [0., 2., 1.],
        ... ]])  # 1x8x3
        >>> crop_by_boxes3d(input, src_box, dst_box, interpolation='nearest', align_corners=True)
        tensor([[[[[21., 22., 23.],
                   [25., 26., 27.],
                   [29., 30., 31.]],
        <BLANKLINE>
                  [[37., 38., 39.],
                   [41., 42., 43.],
                   [45., 46., 47.]]]]])

    r   r   r   r   r   r   r   zJCropping height, width and depth must be exact same in a batch.Got height z, width z and depth )moder   )r   r%   r'   r(   r   r2   r   r)   type_asr   allr   intitem)r   rB   rC   r   r   dst_trans_srcbboxpatchesr/   r/   r0   r
      s2   JH8r
   zeros	transformout_sizerD   padding_modec                 C   sB   | | jd dd}t| |ddddddf ||||d}|S )az  Perform crop transform on 3D volumes (5D tensor) given a perspective transformation matrix.

    Args:
        tensor: the 2D image tensor with shape (B, C, H, W).
        transform: a perspective transformation matrix with shape (B, 4, 4).
        out_size: size of the output image (depth, height, width).
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: mode for grid_generation.

    Returns:
        the output tensor with patches.

    r   r   Nr   )flagsrO   r   )r)   r'   r   )r   rM   rN   rD   rO   r   rI   rK   r/   r/   r0   r   _  s
   "r   )r   F)r   T)r   rL   T)typingr   r   kornia.geometry.bboxr   r   imgwarpr   r   __all__r    rG   strboolr	   r   r
   r   r/   r/   r/   r0   <module>   s   
g
x
l