import torch


def shiftscale_invariant_depthloss(depth, disp):
    """
    Computes the shift- scale- invariant depth loss as proposed in
    https://arxiv.org/pdf/1907.01341.pdf.

    Inputs:
        depth: (N) depth in NDC space.
        disp: (N) disparity in Euclidean space, produced by image-based method.

    Outputs:
        loss: (N)
    """
    t_pred = torch.median(depth)
    s_pred = torch.mean(torch.abs(depth-t_pred))
    t_gt = torch.median(-disp)
    s_gt = torch.mean(torch.abs(-disp-t_gt))

    pred_depth_n = (depth-t_pred)/s_pred
    gt_depth_n = (-disp-t_gt)/s_gt
    loss = (pred_depth_n-gt_depth_n)**2
    return loss