Optimize generic distance transform.