Skip to content

Fix distance transform input cost aliasing - #1473

Draft
yunyv wants to merge 2 commits into
AtsushiSakai:masterfrom
yunyv:fix/distance-transform-cost-alias
Draft

yunyv wants to merge 2 commits into
AtsushiSakai:masterfrom
yunyv:fix/distance-transform-cost-alias

Conversation

@yunyv

@yunyv yunyv commented Oct 24, 2026

Copy link
Copy Markdown

Reference issue

No matching issue found for this defect. Searches for distance-map and distance-transform fixes found no matching open PR.

What does this implement/fix?

The final pass of dt writes its result into the same array that contains the original parabola costs. A later output can therefore read an already transformed value instead of the original cost, underestimating the squared distance.

For example, [0, 4, 9, 9] becomes [0, 1, 4, 5], although min_j((q - j)^2 + cost[j]) gives [0, 1, 4, 8]. This also affects the public 2D distance fields: for [[1,0,0,0], [0,0,1,0], [0,0,0,1], [0,0,0,1]], the unsigned distance at [3,0] is incorrectly sqrt(5) instead of sqrt(8).

Snapshot the original costs before writing the evaluated envelope. Preserve the existing in-place updates, including strided column views, None return, dtype behavior and public APIs. Clarify the in-place contract in the existing docstring. No new dependencies or algorithm examples.

Additional information

Prepared and tested with AI assistance. No hardware verification or human review is claimed.

Validation on Linux, Python 3.14.8, with the repository-pinned requirements:

  • Original distance-map tests: 8 passed on pristine master.
  • Expanded tests before the implementation fix: 4 failed, 21 passed.
  • Expanded tests after the fix: 25 passed with warnings treated as errors.
  • Ruff on both changed files: passed (existing configuration deprecation warning).
  • Mypy on Mapping: passed, 25 source files.
  • Separate exhaustive check: all 4,666 non-uniform binary grids of sizes 2x3, 3x3 and 3x4 agree with SciPy for UDF and SDF; 1,000 random 1D cost arrays, including negative costs, agree with brute force.
  • Full bash runtests.sh: 173 passed in 132.36s, including the repository codestyle and full mypy checks.

The tests retain the finite INF sentinel behavior for all-free/all-occupied maps rather than comparing these cases against SciPy's different boundary convention. Documentation build and macOS/Windows execution were not run locally; there are no RST changes.

CheckList

  • Did you add an unittest for your new example or defect fix?
  • Did you add documents for your new example? (No new example; existing function docstring clarified.)
  • All CIs are green? (Hosted CI has not run yet.)

yunyv added 2 commits October 24, 2026 13:27
Preserve original parabola costs while writing the squared distance transform in place.
Cover in-place and strided costs, brute-force distance fields, rectangular grids, and uniform fields.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant