# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch, math
from torch import nn
from torch.autograd import Function
from torch.autograd.function import once_differentiable
from torch.nn.modules.utils import _pair
from SparseConvNet.sparseconvnet.tools_3d_2d import sparse_3d_to_dense_2d
import _C


class _ROIAlignRotated3D(Function):
    @staticmethod
    def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio):
        ctx.save_for_backward(roi)
        ctx.output_size = _pair(output_size)
        ctx.spatial_scale = spatial_scale
        ctx.sampling_ratio = sampling_ratio
        ctx.input_shape = input.size()
        # input: [4, 256, 304, 200, 7]
        # roi: [171, 8]
        # spatial_scale: 0.25
        # output_size: [7,7,7]
        # sampling_ratio: 2
        output = _C.roi_align_rotated_3d_forward(
            input, roi, spatial_scale, output_size[0], output_size[1], output_size[2], sampling_ratio
        ) # [171, 256, 7, 7]
        return output

    @staticmethod
    @once_differentiable
    def backward(ctx, grad_output):
        rois, = ctx.saved_tensors
        output_size = ctx.output_size
        spatial_scale = ctx.spatial_scale
        sampling_ratio = ctx.sampling_ratio
        bs, ch, h, w, zsize = ctx.input_shape
        grad_input = _C.roi_align_rotated_3d_backward(
            grad_output,
            rois,
            spatial_scale,
            output_size[0],
            output_size[1],
            output_size[2],
            bs,
            ch,
            h,
            w,
            zsize,
            sampling_ratio,
        )
        return grad_input, None, None, None, None


roi_align_rotated_3d = _ROIAlignRotated3D.apply


class ROIAlignRotated3D(nn.Module):
    def __init__(self, output_size, spatial_scale, sampling_ratio):
        '''
        output_size:[pooled_height, pooled_width]
        spatial_scale: size_of_map/size_of_original_image
        sampling_ratio: how many points to use for bilinear_interpolate
        '''
        super(ROIAlignRotated3D, self).__init__()
        self.output_size = output_size # (7,7,7)
        self.spatial_scale = spatial_scale # 0.25
        self.sampling_ratio = sampling_ratio # 2

    def forward(self, input_s3d, rois_3d):
        '''
        input0: sparse 3d tensor
        rois_3d: 3d box, xyz order is same as input0,
                yaw unit is rad, anti-clock wise is positive

        input: [batch_size, feature, h, w]
        rois: [n,5] [batch_ind, center_w, center_h, roi_width, roi_height, theta]
        theta unit: degree, anti-clock wise is positive

        Note: the order of w and h inside of input and rois is different.
        '''
        input_d3d = sparse_3d_to_dense_2d(input_s3d)
        output = roi_align_rotated_3d(
            input_d3d, rois_3d, self.output_size, self.spatial_scale, self.sampling_ratio
        )
        return output

    def __repr__(self):
        tmpstr = self.__class__.__name__ + "("
        tmpstr += "output_size=" + str(self.output_size)
        tmpstr += ", spatial_scale=" + str(self.spatial_scale)
        tmpstr += ", sampling_ratio=" + str(self.sampling_ratio)
        tmpstr += ")"
        return tmpstr


[((15, 26, 15, 44), 'torch.nn.modules.utils._pair', '_pair', ({(15, 32, 15, 43): 'output_size'}, {}), '(output_size)', False, 'from torch.nn.modules.utils import _pair\n'), ((24, 17, 26, 9), '_C.roi_align_rotated_3d_forward', '_C.roi_align_rotated_3d_forward', ({(25, 12, 25, 17): 'input', (25, 19, 25, 22): 'roi', (25, 24, 25, 37): 'spatial_scale', (25, 39, 25, 53): 'output_size[0]', (25, 55, 25, 69): 'output_size[1]', (25, 71, 25, 85): 'output_size[2]', (25, 87, 25, 101): 'sampling_ratio'}, {}), '(input, roi, spatial_scale, output_size[0],\n    output_size[1], output_size[2], sampling_ratio)', False, 'import _C\n'), ((37, 21, 50, 9), '_C.roi_align_rotated_3d_backward', '_C.roi_align_rotated_3d_backward', ({(38, 12, 38, 23): 'grad_output', (39, 12, 39, 16): 'rois', (40, 12, 40, 25): 'spatial_scale', (41, 12, 41, 26): 'output_size[0]', (42, 12, 42, 26): 'output_size[1]', (43, 12, 43, 26): 'output_size[2]', (44, 12, 44, 14): 'bs', (45, 12, 45, 14): 'ch', (46, 12, 46, 13): 'h', (47, 12, 47, 13): 'w', (48, 12, 48, 17): 'zsize', (49, 12, 49, 26): 'sampling_ratio'}, {}), '(grad_output, rois, spatial_scale,\n    output_size[0], output_size[1], output_size[2], bs, ch, h, w, zsize,\n    sampling_ratio)', False, 'import _C\n'), ((81, 20, 81, 52), 'SparseConvNet.sparseconvnet.tools_3d_2d.sparse_3d_to_dense_2d', 'sparse_3d_to_dense_2d', ({(81, 42, 81, 51): 'input_s3d'}, {}), '(input_s3d)', False, 'from SparseConvNet.sparseconvnet.tools_3d_2d import sparse_3d_to_dense_2d\n')]