import torch


def sdf_capsule_to_pt(capsule_base, capsule_tip, capsule_radius, pt):
    """Computes distance between a capsule and a point

    Args:
        capsule_base (tensor): x,y,z in batch [b,3]
        capsule_tip (tensor): x,y,z in batch [b,3]
        capsule_radius (tensor): radius of capsule in batch [b,1]
        pt (tensor): query point x,y,z in batch [b,3]

    Returns:
        (tensor): signed distance (negative outside, positive inside) [b,1]
    """
    pt_base = pt - capsule_base
    tip_base = capsule_tip - capsule_base

    h = torch.clamp(torch.dot(pt_base, tip_base) / torch.dot(tip_base,tip_base), 0.0, 1.0)
    
    dist = torch.norm(pt_base - tip_base * h) - capsule_radius
    return dist