import torch


def _get_corners_3d(dim, o=[0] * 6):
    """Get eight corners of 3D image volume.

    Parameters
    ----------
    dim : (3,), list or tuple
        Image dimensions.
    o : (6, ), default=[0] * 6
        Offsets.

    Returns
    -------
    c : (8, 4), tensor_like[torch.float64]
        Corners of volume.

    """
    # Get corners
    c = torch.tensor(
        [[     1,      1,      1, 1],
         [     1,      1, dim[2], 1],
         [     1, dim[1],      1, 1],
         [     1, dim[1], dim[2], 1],
         [dim[0],      1,      1, 1],
         [dim[0],      1, dim[2], 1],
         [dim[0], dim[1],      1, 1],
         [dim[0], dim[1], dim[2], 1]])
    # Include offset
    # Plane 1
    c[0, 0] = c[0, 0] + o[0]
    c[1, 0] = c[1, 0] + o[0]
    c[2, 0] = c[2, 0] + o[0]
    c[3, 0] = c[3, 0] + o[0]
    # Plane 2
    c[4, 0] = c[4, 0] - o[1]
    c[5, 0] = c[5, 0] - o[1]
    c[6, 0] = c[6, 0] - o[1]
    c[7, 0] = c[7, 0] - o[1]
    # Plane 3
    c[0, 1] = c[0, 1] + o[2]
    c[1, 1] = c[1, 1] + o[2]
    c[4, 1] = c[4, 1] + o[2]
    c[5, 1] = c[5, 1] + o[2]
    # Plane 4
    c[2, 1] = c[2, 1] - o[3]
    c[3, 1] = c[3, 1] - o[3]
    c[6, 1] = c[6, 1] - o[3]
    c[7, 1] = c[7, 1] - o[3]
    # Plane 5
    c[0, 2] = c[0, 2] + o[4]
    c[2, 2] = c[2, 2] + o[4]
    c[4, 2] = c[4, 2] + o[4]
    c[6, 2] = c[6, 2] + o[4]
    # Plane 6
    c[1, 2] = c[1, 2] - o[5]
    c[3, 2] = c[3, 2] - o[5]
    c[5, 2] = c[5, 2] - o[5]
    c[7, 2] = c[7, 2] - o[5]

    return c