def get_nonzero_limit(img, axis, idx_range):
    """Get the index first hyperplane containing nonzeros.

    Input:
        img (np.ndarray): tensor, could be 2d or 3d
        axis (int): the axis to scan
        idx_range (list-like): ordered indice to search

    Output:
        the first index at which contains a nonzero hyperplane.
    """
    dim = len(img.shape)
    s = [slice(None)] * dim

    # scan every plane until a nonzero item is found
    for idx in idx_range:
        # the plane cuts through this point
        s[axis] = idx
        if img[s].any():
            return idx

    # otherwise, return the last index
    return idx_range[-1]