import torch


def regress_target_3d(anchors, gt_boxes):
    """
    计算回归目标,输入的gt和anchor为一一对应关系
    :param anchors: [N, (y1, x1, z1, y2, x2, z2)]
    :param gt_boxes: [N, (y1, x1, z1, y2, x2, z2)]
    :return: [N,(dy,dx,dz,dh,dw,dd)]
    """
    # 高度、宽度、深度
    h = anchors[:, 3] - anchors[:, 0]
    w = anchors[:, 4] - anchors[:, 1]
    d = anchors[:, 5] - anchors[:, 2]
    gt_h = gt_boxes[:, 3] - gt_boxes[:, 0]
    gt_w = gt_boxes[:, 4] - gt_boxes[:, 1]
    gt_d = gt_boxes[:, 5] - gt_boxes[:, 2]

    # 中心点
    center_y = (anchors[:, 3] + anchors[:, 0]) * 0.5
    center_x = (anchors[:, 4] + anchors[:, 1]) * 0.5
    center_z = (anchors[:, 5] + anchors[:, 2]) * 0.5
    gt_center_y = (gt_boxes[:, 3] + gt_boxes[:, 0]) * 0.5
    gt_center_x = (gt_boxes[:, 4] + gt_boxes[:, 1]) * 0.5
    gt_center_z = (gt_boxes[:, 5] + gt_boxes[:, 2]) * 0.5

    # 回归目标
    dy = (gt_center_y - center_y) / h
    dx = (gt_center_x - center_x) / w
    dz = (gt_center_z - center_z) / d
    dh = torch.log(gt_h / h)
    dw = torch.log(gt_w / w)
    dd = torch.log(gt_d / d)

    target = torch.stack((dy, dx, dz, dh, dw, dd), dim=1)
    return target