import torch


def calc_normal(p):
    """Calculates the vector that is perpendicular to the plane defined by
    p."""
    if p.shape[0] == 2:
        normal = torch.cross(p[0], p[1])
        norm = torch.norm(normal)
        if norm != 0:
            normal = normal / torch.norm(normal)
        return normal

    a = p[1] - p[0]
    b = p[2] - p[0]

    normal = torch.cross(a, b)
    norm = torch.norm(normal)
    if norm == 0:
        return normal
    return normal / norm