-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added grid processing and neighbour lookup table for fast access
- Loading branch information
Showing
3 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
Author: Haoran Peng | ||
Email: [email protected] | ||
''' | ||
from typing import Tuple | ||
import numpy as np | ||
|
||
class Grid: | ||
|
||
def __init__(self, grid_size, static_obstacles): | ||
self.grid_size = grid_size | ||
self.minx, self.maxx, self.miny, self.maxy = self.calculate_boundaries(static_obstacles) | ||
self.grid = self.make_grid(grid_size, self.minx, self.maxx, self.miny, self.maxy) | ||
|
||
@staticmethod | ||
def calculate_boundaries(static_obstacles: np.ndarray) -> Tuple[int, int, int, int]: | ||
min_ = np.min(static_obstacles, axis=0) | ||
max_ = np.max(static_obstacles, axis=0) | ||
return min_[0], max_[0], min_[1], max_[1] | ||
|
||
@staticmethod | ||
def make_grid(grid_size: int, minx: int, maxx: int, miny: int, maxy: int) -> np.ndarray: | ||
# Calculate the size of the sides | ||
x_size = (maxx - minx) // grid_size | ||
y_size = (maxy - miny) // grid_size | ||
# Initialize the grid, assuming grid is 2D | ||
grid = np.zeros([y_size, x_size, 2], dtype=np.uint16) | ||
# Fill the grid in | ||
y = miny - grid_size / 2 | ||
for i in range(y_size): | ||
y += grid_size | ||
x = minx - grid_size / 2 | ||
for j in range(x_size): | ||
x += grid_size | ||
grid[i][j] = np.array([y, x]) | ||
return grid | ||
|
||
''' | ||
Snap an arbitrary position to the center of the grid | ||
''' | ||
def snap_to_grid(self, position: np.ndarray) -> np.ndarray: | ||
i = (position[1] - self.miny) // self.grid_size | ||
j = (position[0] - self.minx) // self.grid_size | ||
if i >= len(self.grid): | ||
i -= 1 | ||
if j >= len(self.grid[0]): | ||
j -= 1 | ||
return self.grid[i][j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
Author: Haoran Peng | ||
Email: [email protected] | ||
''' | ||
import numpy as np | ||
|
||
class NeighbourTable: | ||
|
||
def __init__(self, grid: np.ndarray): | ||
dimy, dimx = len(grid), len(grid[0]) | ||
table = dict() | ||
for i in range(dimy): | ||
for j in range(dimx): | ||
neighbours = [] | ||
for dx, dy in (1, 0), (-1, 0), (0, 1), (0, -1): | ||
y, x = i + dy, j + dx, | ||
if x >= 0 and x < dimx and y >= 0 and y < dimy: | ||
neighbours.append([y, x]) | ||
table[self.hash(grid[i][j])] = np.array(neighbours) | ||
self.table = table | ||
|
||
def lookup(self, position: np.ndarray) -> np.ndarray: | ||
return self.table[self.hash(position)] | ||
|
||
@staticmethod | ||
def hash(grid_pos: np.ndarray) -> int: | ||
concat = str(grid_pos[0]) + str(grid_pos[1]) | ||
return int(concat) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters