Skip to content

Fix mixed coordinate units in Dijkstra grid indexing - #1461

Open
huxiaoyi-ovo wants to merge 3 commits into
AtsushiSakai:masterfrom
huxiaoyi-ovo:fix/dijkstra-grid-index-units-1318
Open

huxiaoyi-ovo wants to merge 3 commits into
AtsushiSakai:masterfrom
huxiaoyi-ovo:fix/dijkstra-grid-index-units-1318

Conversation

@huxiaoyi-ovo

@huxiaoyi-ovo huxiaoyi-ovo commented Oct 12, 2026 •

Copy link
Copy Markdown

Reference issue

Fixes #1318.

What does this implement/fix?

DijkstraPlanner.calc_index() previously subtracted the map's world-coordinate origin (min_x, min_y, in metres) from Node.x and Node.y, even though these node coordinates are already zero-based grid indices (in cells). This mixes units and produces an incorrect nonzero index for the grid-origin cell whenever the world origin is offset.

Use the direct row-major index node.y * self.x_width + node.x. The fix corrects the index definition. It can also change an incorrect result: a valid start cell previously assigned key -1 could collide with the parent-chain sentinel and be omitted from the returned path. The search algorithm and intended path ordering remain unchanged.

A focused regression test checks two maps with different nonzero world origins and non-unit resolutions (0.5 and 2.0 m/cell), including:

  • mapping the world origin to grid cell (0, 0) and index 0;
  • mapping an interior cell to y * width + x;
  • mapping the final grid cell to width * height - 1.

A second end-to-end regression follows Afloat16's reproduction and asserts the complete reversed path [(1.0, 3.0), (1.0, 2.0)] instead of the old goal-only result.

This also addresses the maintainer's earlier request for an offset-origin, non-unit-resolution regression test on #1360 (closed without merge).

Additional information

The original two-commit head was validated against upstream master (cdd0cc888802b584c2d654ca85e3c5460973487d) using fork-only draft PR #2:

  • Linux Python 3.14 full CI — passed on the original head.
  • Windows Python 3.14 full CI — passed on the original head.
  • macOS Python 3.14 full CI — passed on the original head.
  • CodeQL code scanning — passed on the original head.

The end-to-end test was added in commit 9e96bf4; the latest CI status should be checked separately.

  • Diff: only PathPlanning/Dijkstra/dijkstra.py and tests/test_dijkstra.py, with no new dependencies.

CheckList

  • Did you add an unittest for your new example or defect fix?
  • Did you add documents for your new example? (Not applicable: corrects an internal grid-index formula; no public API or algorithm documentation changes.)
  • All CIs are green? (Fork CI: Linux, Windows, macOS, CodeQL; upstream checks will run separately.)

AI assistance was used to review the issue history and prepare the minimal fix and regression test; the final branch was checked against upstream, and the full fork CI passed.

Copy link
Copy Markdown

Automated Codex-assisted supporting check; no human review or approval is claimed.

The index normalization also fixes a path-reconstruction failure, so the description's claim that planned paths are unchanged has an exception:

from PathPlanning.Dijkstra import dijkstra

dijkstra.show_animation = False
planner = dijkstra.DijkstraPlanner(
    [1.0, 5.0, 1.0, 5.0], [1.0, 1.0, 5.0, 5.0], 1.0, 0.0
)
rx, ry = planner.planning(1.0, 2.0, 1.0, 3.0)
print(list(zip(rx, ry)))

With Python 3.13.5 and the repository-pinned NumPy 2.5.3 / Matplotlib 3.11.2, both endpoints pass verify_node:

  • Upstream 8c392897: [(1.0, 3.0)] (the start is missing).
  • This PR's 452164c4: [(1.0, 3.0), (1.0, 2.0)], the complete path in the planner's reverse order.

The old formula assigns start grid cell (0, 1) the key -1, which collides with the parent-chain terminator. The row-major formula gives it key 4.

This would be a useful end-to-end regression alongside the key-value tests. Only this matched case was run against the complete, unmodified module at each immutable revision, not the full suite or CI. Matplotlib emitted Fontconfig cache diagnostics; both runs completed normally.

Copy link
Copy Markdown
Author

Thanks @Afloat16, good catch. The start cell really can end up with key -1, so this affects path reconstruction, not just the internal index values. I added your example as an end-to-end regression in 9e96bf4 and corrected the PR description. The new CI run is underway.

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.

the implement Dijkstra maybe have issue

2 participants