pathfinding3d.core.grid module
- pathfinding3d.core.grid.build_nodes(width, height, depth, matrix=None, inverse=False, grid_id=None)[source]
Create nodes according to grid size. If a matrix is given it will be used to determine what nodes are walkable.
- Parameters:
width (int) – The width of the grid.
height (int) – The height of the grid.
depth (int) – The depth of the grid.
matrix (MatrixType) – A 3D array of values (numbers or objects specifying weight) that determine how nodes are connected and if they are walkable. If no matrix is given, all nodes will be walkable.
inverse (bool, optional) – If true, all values in the matrix that are not 0 will be considered walkable. Otherwise all values that are 0 will be considered walkable.
grid_id (int, optional) – The id of the grid.
- Returns:
A list of list of lists containing the nodes in the grid.
- Return type:
List[List[List[GridNode]]]
- class pathfinding3d.core.grid.Grid(width=0, height=0, depth=0, matrix=None, grid_id=None, inverse=False)[source]
Bases:
object
A grid represents the map (as 3d-list of nodes).
- Parameters:
- __init__(width=0, height=0, depth=0, matrix=None, grid_id=None, inverse=False)[source]
Create a new grid.
- Parameters:
width (int, optional) – The width of the grid.
height (int, optional) – The height of the grid.
depth (int, optional) – The depth of the grid.
matrix (MatrixType) – A 3D array of values (numbers or objects specifying weight) that determine how nodes are connected and if they are walkable. If no matrix is given, all nodes will be walkable.
inverse (bool, optional) – If true, all values in the matrix that are not 0 will be considered walkable. Otherwise all values that are 0 will be considered walkable.
grid_id (int | None) –
- calc_cost(node_a, node_b, weighted=False)[source]
Get the distance between current node and the neighbor (cost)
- visualize(path=None, start=None, end=None, visualize_weight=True, save_html=False, save_to='./pathfinding3d_visualization.html', always_show=False)[source]
Creates a 3D visualization of the grid, including optional path, start/end points, and node weights using plotly. This method is designed to help visualize the spatial layout, obstacles, and pathfinding results in three dimensions.
- Parameters:
path (Optional[List[Union[GridNode, Tuple]]], optional) – The path to visualize, specified as a list of GridNode instances or coordinate tuples (x, y, z). If omitted, the grid is visualized without a path. Defaults to None.
start (Optional[Union[GridNode, Tuple]], optional) – The start node for the path, as either a GridNode or a tuple of coordinates. If not provided and a path is given, the first node of the path is used. Defaults to None.
end (Optional[Union[GridNode, Tuple]], optional) – The end node for the path, as either a GridNode or a tuple of coordinates. If not provided and a path is given, the last node of the path is used. Defaults to None.
visualize_weight (bool, optional) – Whether to visualize the weights of the nodes, enhancing the representation of node costs. Defaults to True.
save_html (bool, optional) – If True, the visualization is saved to an HTML file specified by save_to instead of being displayed. Defaults to False.
save_to (str, optional) – File path where the HTML visualization is saved if save_html is True. Defaults to “./pathfinding3d_visualization.html”.
always_show (bool, optional) – If True, displays the visualization in the browser even when save_html is True. Useful for immediate feedback while also saving the result. Defaults to False.
- Raises:
Warning – If plotly is not installed, a warning is logged and the visualization is skipped.
Notes
Requires plotly for visualization. Install with pip install plotly if not already installed.