-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharraytogrid.py
More file actions
104 lines (81 loc) · 3 KB
/
arraytogrid.py
File metadata and controls
104 lines (81 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
"""ArrayToGrid.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1BwXneXu-gJQUN1sk_oIcQwMZh-EIxhpe
# Array to Grid
## 2D numpy Array -> Grid of Nodes
"""
import numpy as np
blocked = set()
class Node:
def __init__(self, position):
self.position = position # (row, col)
self.neighbors = [] # List of neighboring Node objects
def __repr__(self):
return f"Node({self.position})"
def boolean_array_to_node_dict(array):
"""
Converts a 2D boolean numpy array into a dictionary of Node objects.
True = blocked / invalid
False = valid
Each node stores its position and valid neighbors.
Returns:
dict: key = position tuple, value = list of neighbor positions
"""
rows, cols = array.shape
node_dict = {}
# Step 1: Create Node objects for all False positions
for r in range(rows):
for c in range(cols):
if not array[r, c]: # Only create nodes for False (valid) cells
node_dict[(r, c)] = Node((r, c))
else:
blocked.add((r, c))
# Step 2: Assign neighbors for each node including diagonals
directions = [
(-1, 0), (1, 0), (0, -1), (0, 1), # up, down, left, right
(-1, -1), (-1, 1), (1, -1), (1, 1) # diagonals
]
for pos, node in node_dict.items():
r, c = pos
for dr, dc in directions:
nr, nc = r + dr, c + dc
if (nr, nc) in node_dict: # Only add neighbors that are valid nodes
node.neighbors.append(node_dict[(nr, nc)])
# Step 3: Convert to dictionary mapping position -> neighbor positions
node_neighbor_dict = {pos: [neighbor.position for neighbor in node.neighbors]
for pos, node in node_dict.items()}
return node_neighbor_dict
def blocked_positions(array):
"""
Returns a set of positions (row, col) where the array has True values.
"""
return blocked
"""## Examples"""
# Example 3x3 usage
bool_array = np.array([[False, True, False],
[False, False, True],
[True, False, False]])
node_dict = boolean_array_to_node_dict(bool_array)
for pos, neighbors in node_dict.items():
print(f"Node {pos} has neighbors: {neighbors}")
# Example usage with previous array
blocked_set = blocked_positions(bool_array)
print("\nBlocked positions:", blocked_set)
# Example 5x5 boolean grid
bool_array = np.array([
[False, True, False, False, False],
[False, False, True, False, False],
[True, False, False, True, False],
[False, False, True, False, False],
[False, False, False, True, False]
])
# Using the function from before
node_dict = boolean_array_to_node_dict(bool_array)
# Print the node dictionary
for pos, neighbors in node_dict.items():
print(f"Node {pos} has neighbors: {neighbors}")
# Example usage with previous array
blocked_set = blocked_positions(bool_array)
print("\nBlocked positions:", blocked_set)