import torch


def voxel_box_decode(box_encodings, anchors):
    """
    box decode for pillar-net in lidar
    :param box_encodings: [N, 7] Tensor, normal boxes: x, y, z, w, l, h, r
    :param anchors: [N, 7] Tensor, anchors
    :param encode_angle_to_vector: whether encoding angle to vector
    :param smooth_dim: whether using smooth dim
    :return: decoded boxes
    """
    box_ndim = anchors.shape[-1]
    cas, cts = [], []
    if box_ndim > 7:
        xa, ya, za, la, wa, ha, ra, *cas = torch.split(anchors, 1, dim=-1)
        xt, yt, zt, lt, wt, ht, rt, *cts = torch.split(box_encodings, 1, dim=-1)
    else:
        xa, ya, za, la, wa, ha, ra = torch.split(anchors, 1, dim=-1)
        xt, yt, zt, lt, wt, ht, rt = torch.split(box_encodings, 1, dim=-1)

    diagonal = torch.sqrt(la ** 2 + wa ** 2)
    xg = xt * diagonal + xa
    yg = yt * diagonal + ya
    zg = zt * ha + za
    lg = torch.exp(lt) * la
    wg = torch.exp(wt) * wa
    hg = torch.exp(ht) * ha
    rg = rt + ra
    cgs = [t + a for t, a in zip(cts, cas)]
    return torch.cat([xg, yg, zg, lg, wg, hg, rg, *cgs], dim=-1)