import torch


def from_center_representation(center_bboxes):
    """
    transform bbox from center representation to border representation
    :param center_bboxes: tensor of bbox like torch.tensor([center_x, center_y, length, width])
    :return: tensor of bbox like torch.tensor([left, right, bottom, top])
    """
    shape = center_bboxes.shape
    batch_size = center_bboxes.shape[0]
    x_center = center_bboxes[..., 0]
    y_center = center_bboxes[..., 1]
    half_length = center_bboxes[..., 2] / 2
    half_width = center_bboxes[..., 3] / 2
    left = x_center - half_length
    right = x_center + half_length
    bottom = y_center - half_width
    top = y_center + half_width

    _seq = (left, right, bottom, top)
    if len(shape) == 3:
        return torch.cat(_seq, 1).reshape(batch_size, 4, -1).transpose(2, 1)
    elif len(shape) == 2:
        return torch.cat(_seq, 0).reshape(4, -1).transpose(1, 0)
    else:
        return torch.Tensor(_seq)