Given a list of points obstacles
(given as a list of row, column
matrix coordinates, an ndarray of shape (n, 2)
), return a map of size size
(where size
is the shape of the 2D NumPy array) in which the value of r, c
is the Euclidean distance to the closest "obstacle."
def gen_distgrid(size, obstacles):
n_obstacles = obstacles.shape[0]
distgrids = np.zeros((n_obstacles + 4, size[0], size[1]))
for layer in range(n_obstacles):
for i in range(size[0]):
for j in range(size[1]):
distgrids[layer, i, j] = np.linalg.norm(obstacles[layer,:] - [i,j])
for i in range(size[0]):
for j in range(size[1]):
distgrids[n_obstacles + 0, i, j] = i
distgrids[n_obstacles + 1, i, j] = (size[0] - i)
distgrids[n_obstacles + 2, i, j] = j
distgrids[n_obstacles + 3, i, j] = (size[1] - j)
distgrid = np.min(distgrids, axis=0)
return distgrid
My method is really slow, and I feel like there should be a better one.