ENH Faster multi-dimensional distance transform

A 3x speed up by not repeatedly testing the same things.