hjweide / pyastar2d

A very simple A* implementation in C++ callable from Python for pathfinding on a two-dimensional grid.
MIT License
150 stars 57 forks source link

Automating the creation of the weights matrix given an image of a 2D binarised map #40

Closed rllyryan closed 2 years ago

rllyryan commented 2 years ago

Dear @hjweide

This is amazing work, I have been looking for a C++ A* implementation that is callable from Python. However, it seems that the code is reliant on the manual creation of a weights matrix for it to work.

Can I ask how would you utilise your package to traverse through an image of a 2D binarised map (consists of 0's and 255's only)?

Thank you so much!

Ryan

hjweide commented 2 years ago

It's implemented this way to support both this specific case (only 0 and 255) and the general case (arbitrary weights). If you look at the maze_solver.py example, you'll see some example code to transform a binary image to the appropriate weight matrix:

grid = maze.astype(np.float32)
grid[grid == 0] = np.inf
grid[grid == 255] = 1

From here: https://github.com/hjweide/pyastar2d/blob/351588dd7857b0c0913bb72f73700d498e52eca1/examples/maze_solver.py#L41

Note that the minimum cost of any move must be at least 1 to ensure the heuristic remains admissible.

rllyryan commented 2 years ago

It's implemented this way to support both this specific case (only 0 and 255) and the general case (arbitrary weights). If you look at the maze_solver.py example, you'll see some example code to transform a binary image to the appropriate weight matrix:

grid = maze.astype(np.float32)
grid[grid == 0] = np.inf
grid[grid == 255] = 1

From here:

https://github.com/hjweide/pyastar2d/blob/351588dd7857b0c0913bb72f73700d498e52eca1/examples/maze_solver.py#L41

Note that the minimum cost of any move must be at least 1 to ensure the heuristic remains admissible.

Thank you so much for your detailed reply! It makes sense to me!

Also, I did not know that numpy has an infinity attribute, that's is very useful!