def relate(rect, qrt):
    """
    Spatial relationship between two nd-boxes.

    Outcome can be:
        0: equal
        1: contains
        2: intersects
        -1: no overlap
    """
    # how many dimensions to check?
    dims = rect.dims

    # equal, all coordinates are equal
    ncmp = 1
    for d in range(dims):
        ncmp &= rect.lo[d] == qrt.lo[d] and rect.hi[d] == qrt.hi[d]
    if ncmp:
        return 0

    # fully contains, rect fully contains qrt
    ncmp = 1
    for d in range(dims):
        ncmp &= rect.lo[d] <= qrt.lo[d] and rect.hi[d] >= qrt.hi[d]
    if ncmp:
        return 1

    # intersects, the two nd-boxes interact 
    # (either on the boundary or internally)
    ncmp = 1
    for d in range(dims):
        ncmp &= rect.lo[d] < qrt.hi[d] and rect.hi[d] > qrt.lo[d]
    if ncmp:
        return 2

    # no overlap
    return -1