Skip to content

Commit

Permalink
updated doc strings and types
Browse files Browse the repository at this point in the history
  • Loading branch information
hariIVI committed Jan 25, 2024
1 parent eb2de6b commit daf65f5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
1 change: 1 addition & 0 deletions pathfinding3d/core/diagonal_movement.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class DiagonalMovement:
"""Enum for diagonal movement"""
always = 1
never = 2
if_at_most_one_obstacle = 3
Expand Down
19 changes: 15 additions & 4 deletions pathfinding3d/core/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def build_nodes(
Returns
-------
List
List[List[List[GridNode]]]
A list of list of lists containing the nodes in the grid.
"""
nodes: List = []
Expand All @@ -67,6 +67,9 @@ def build_nodes(


class Grid:
"""
A grid represents the map (as 3d-list of nodes).
"""
def __init__(
self,
width: int = 0,
Expand All @@ -77,7 +80,7 @@ def __init__(
inverse: bool = False,
):
"""
A grid represents the map (as 3d-list of nodes).
Create a new grid.
Parameters
----------
Expand Down Expand Up @@ -115,6 +118,14 @@ def _validate_dimensions(self, width: int, height: int, depth: int, matrix: Matr
return width, height, depth

def is_valid_grid(self) -> bool:
"""
Check if grid is valid
Returns
-------
bool
True, if grid is valid
"""
return self.width > 0 and self.height > 0 and self.depth > 0

def node(self, x: int, y: int, z: int) -> Optional[GridNode]:
Expand Down Expand Up @@ -247,8 +258,8 @@ def neighbors(
Returns
-------
list
list of neighbor nodes
List[GridNode]
list of all neighbors
"""
x, y, z = node.x, node.y, node.z

Expand Down
52 changes: 48 additions & 4 deletions pathfinding3d/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@

@dataclasses.dataclass
class Node:
"""
Basic node class, saves calculated values for pathfinding
Attributes
----------
h : float
The cost from this node to the goal (for A* including the heuristic).
g : float
The cost from the start node to this node.
f : float
The overall cost for a path using this node (f = g + h).
opened : int
The number of times this node has been opened.
closed : bool
Whether this node is closed.
parent : Optional[Node]
The parent node of this node.
retain_count : int
Used for recursion tracking of IDA*.
tested : bool
Used for IDA* and Jump-Point-Search.
"""
__slots__ = ["h", "g", "f", "opened", "closed", "parent", "retain_count", "tested"]

def __init__(self):
Expand Down Expand Up @@ -41,8 +63,24 @@ def cleanup(self):
@dataclasses.dataclass
class GridNode(Node):
"""
basic node, saves X, Y and Z coordinates on some grid and determine if
it is walkable.
Basic node in a grid.
Attributes
----------
x : int
The x coordinate of the node.
y : int
The y coordinate of the node.
z : int
The z coordinate of the node.
walkable : bool
Whether this node can be walked through.
weight : float
The weight of the node.
grid_id : Optional[int]
The id of the grid this node belongs to.
connections : Optional[List]
The connections of this node.
"""

# Coordinates
Expand All @@ -64,8 +102,6 @@ class GridNode(Node):

connections: Optional[List] = None

identifier: Optional[Tuple] = None

def __post_init__(self):
super().__init__()
# for heap
Expand All @@ -81,6 +117,14 @@ def __iter__(self):
yield self.grid_id

def connect(self, other_node: "GridNode"):
"""
Connect this node to another node.
Parameters
----------
other_node : GridNode
The node to connect to.
"""
if not self.connections:
self.connections = [other_node]
else:
Expand Down
11 changes: 11 additions & 0 deletions pathfinding3d/core/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@

# a world connects grids but can have multiple grids.
class World:
"""
A world connects grids but can have multiple grids.
"""
def __init__(self, grids: Dict[int, Grid]):
"""
Initialize a new world.
Parameters
----------
grids : Dict[int, Grid]
grids in this world
"""
self.grids = grids

def neighbors(self, node: GridNode, diagonal_movement: int) -> List[GridNode]:
Expand Down

0 comments on commit daf65f5

Please sign in to comment.