Distance transform algorithm