import torch


def get_horizontal_bboxes(gt_rbboxes):
    """get_horizontal_bboxes from polygons.

    Args:
        gt_rbboxes (torch.Tensor): Groundtruth polygons, shape (k, 8).
    Returns:
        gt_rect_bboxes (torch.Tensor): The horizontal bboxes, shape (k, 4).
    """
    gt_xs, gt_ys = gt_rbboxes[:, 0::2], gt_rbboxes[:, 1::2]
    gt_xmin, _ = gt_xs.min(1)
    gt_ymin, _ = gt_ys.min(1)
    gt_xmax, _ = gt_xs.max(1)
    gt_ymax, _ = gt_ys.max(1)
    gt_rect_bboxes = torch.cat([
        gt_xmin[:, None], gt_ymin[:, None], gt_xmax[:, None], gt_ymax[:, None]
    ],
                               dim=1)
    return gt_rect_bboxes