import torch


def radian_between_tensor(v1, v2):
    """ Returns the angle in radians between vectors 'v1' and 'v2'::"""
    v1 = v1.reshape(-1, 3)
    v2 = v2.reshape(-1, 3)
    # inner = np.sum(v1.reshape(-1, 3) * v2.reshape(-1, 3), axis=1)
    # norms = np.linalg.norm(v1, axis=1, ord=2) * np.linalg.norm(v2, axis=1, ord=2)
    v1_u = v1 / (torch.norm(v1, dim=1, keepdim=True) + 1e-20)
    v2_u = v2 / (torch.norm(v2, dim=1, keepdim=True) + 1e-20)

    rad = torch.arccos(torch.clip(torch.sum(v1_u * v2_u, dim=1), -1.0, 1.0))

    # deg = torch.rad2deg(rad)
    # deg[deg > 90] = 180 - deg[deg > 90]
    return rad.float()