-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuxillaryFunctions.py
49 lines (40 loc) · 1.67 KB
/
AuxillaryFunctions.py
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
import numpy as np
from numpy import linalg as LA
def eight_neighbors(v,occ_grid):
neighbor_set = set()
i = v[0]
j = v[1]
for p in range(-1,2): # Checking three rows around the current pixel for neighbors.
for q in range(-1,2): # Checking three columns around the current pixel for neighbors.
if p==0 and q==0:
continue
elif i+p>=0 and i+p<=occ_grid.shape[0]-1 and j+q>=0 and j+q<=occ_grid.shape[1]-1 : # Inside the square.
if occ_grid[i+p,j+q] == 0 or occ_grid[i+p,j+q] == 1: # Adding a neighbor only if its pixel value is 0 or 1.
neighbor_set.add((i+p,j+q))
else : continue
return neighbor_set
occ_grid = np.load('risk_zones.npy')
# print(occ_grid.shape)
vertices = set() # Set of vertices
neighbors = dict() # dictiionary to store set of neighbors
for i in range(occ_grid.shape[0]):
for j in range(occ_grid.shape[1]):
if occ_grid[i,j] == 0 or occ_grid[i,j] == 1: # Adding a vertex to the graph only if its pixel value is 0 or 1.
vertex = (i,j)
vertices.add(vertex)
neighbors[vertex] = eight_neighbors(vertex,occ_grid)
def euclidean_distance(v1,v2):
# print(v1)
return LA.norm([v2[0]-v1[0],v2[1]-v1[1]])
def RecoverPath(s,g,pred): # Takes in the predecessor dictionary
list = [g]
v = g # Setting v as the goal node.
while v!=s:
list.insert(0,pred[v])
v = pred[v] # Setting v as the predecessor
return list
def pathlength(path):
length = 0
for i in range(0,len(path)-1):
length = length + euclidean_distance(path[i],path[i+1])
return length