import torch


def segment2distance(points, segment, max_dis=None, eps=0.1):
    """Encode segment based on distances.

    Args:
        points (Tensor): Shape (n,), [center].
        segment (Tensor): Shape (n, 2), "start, end" format
        max_dis (float): Upper bound of the distance.
        eps (float): a small value to ensure target < max_dis, instead <=
    Returns:
        Tensor: Decoded distances.
    """
    left = points[:, 0] - segment[:, 0]
    right = segment[:, 1] - points[:, 0]
    if max_dis is not None:
        left = left.clamp(min=0, max=max_dis - eps)
        right = right.clamp(min=0, max=max_dis - eps)
    return torch.stack([left, right], -1)