2018-12-01 19:37:18 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
|
|
class EdgeRule(Enum):
|
|
|
|
IGNORE_MISSING_NEIGHBORS_OF_EDGE_CELLS = 0
|
|
|
|
IGNORE_EDGE_CELLS = 1
|
|
|
|
FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS = 2
|
|
|
|
|
|
|
|
|
2018-12-02 16:45:08 +00:00
|
|
|
class Neighborhood:
|
2018-12-01 19:37:18 +00:00
|
|
|
def __init__(self, neighbors: list, edge_rule: EdgeRule):
|
2018-12-08 21:33:13 +00:00
|
|
|
""" Defines a neighborhood for cells.
|
|
|
|
:param neighbors: List of relative coordinates for the neighbors.
|
|
|
|
:param edge_rule: A EdgeRule to define, how Cells on the edge of the grid will be handled.
|
|
|
|
"""
|
2018-12-01 19:37:18 +00:00
|
|
|
self._neighbors = neighbors
|
|
|
|
self.edge_rule = edge_rule
|
2018-12-08 21:33:13 +00:00
|
|
|
self.grid_dimensions = []
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2018-12-08 21:33:13 +00:00
|
|
|
def calculate_cell_neighbor_coordinates(self, cell_coordinate, grid_dimensions):
|
|
|
|
""" Get a list of coordinates for the cell neighbors. The EdgeRule can reduce the returned neighbor count.
|
|
|
|
:param cell_coordinate: The coordinate of the cell to get the neighbors
|
|
|
|
:param grid_dimensions: The dimensions of the grid, to apply edge the rule.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.grid_dimensions = grid_dimensions
|
2018-12-02 16:45:08 +00:00
|
|
|
if self._does_ignore_edge_cell_rule_apply(cell_coordinate):
|
|
|
|
return []
|
|
|
|
else:
|
2018-12-08 21:33:13 +00:00
|
|
|
return self._apply_edge_rule_to_neighbours(cell_coordinate)
|
2018-12-01 19:37:18 +00:00
|
|
|
|
|
|
|
def _does_ignore_edge_cell_rule_apply(self, coordinate):
|
|
|
|
if self.edge_rule == EdgeRule.IGNORE_EDGE_CELLS and self._is_coordinate_on_an_edge(coordinate):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _is_coordinate_on_an_edge(self, coordinate):
|
2018-12-08 21:33:13 +00:00
|
|
|
for neighbor_dimension, dimension in zip(coordinate, self.grid_dimensions):
|
|
|
|
if neighbor_dimension == 0 or neighbor_dimension == dimension - 1:
|
2018-12-01 19:37:18 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-12-08 21:33:13 +00:00
|
|
|
def _apply_edge_rule_to_neighbours(self, coordinate):
|
2018-12-01 19:37:18 +00:00
|
|
|
remaining_neighbours = []
|
|
|
|
for neighbour in self._neighbors:
|
2018-12-08 21:33:13 +00:00
|
|
|
if not self._does_ignore_edge_cell_neighbours_rule_apply(neighbour, coordinate):
|
|
|
|
remaining_neighbours.append(self._calculate_neighbour_coordinate(neighbour, coordinate))
|
2018-12-02 16:45:08 +00:00
|
|
|
return remaining_neighbours
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2018-12-08 21:33:13 +00:00
|
|
|
def _does_ignore_edge_cell_neighbours_rule_apply(self, neighbour, coordinate):
|
2018-12-01 19:37:18 +00:00
|
|
|
if self.edge_rule == EdgeRule.IGNORE_MISSING_NEIGHBORS_OF_EDGE_CELLS:
|
2018-12-08 21:33:13 +00:00
|
|
|
for rel_neighbour_dim, cell_dim, dim in zip(neighbour, coordinate, self.grid_dimensions):
|
|
|
|
neighbor_dimension = cell_dim + rel_neighbour_dim
|
|
|
|
if neighbor_dimension < 0 or neighbor_dimension >= dim:
|
2018-12-01 19:37:18 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _calculate_neighbour_coordinate(self, neighbour, cell_coordinate):
|
2018-12-02 16:45:08 +00:00
|
|
|
new_coordinate = []
|
2018-12-08 21:33:13 +00:00
|
|
|
for rel_neighbour_dim, cell_dim, dim in zip(neighbour, cell_coordinate, self.grid_dimensions):
|
|
|
|
neighbor_dim = cell_dim + rel_neighbour_dim
|
|
|
|
neighbor_dim = self._calculate_neighbour_dimension_of_edge_cells(dim, neighbor_dim)
|
|
|
|
new_coordinate.append(neighbor_dim)
|
2018-12-02 16:45:08 +00:00
|
|
|
return new_coordinate
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2018-12-08 21:33:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def _calculate_neighbour_dimension_of_edge_cells(dim, neighbor_dim):
|
|
|
|
if neighbor_dim < 0:
|
|
|
|
neighbor_dim = dim - 1
|
|
|
|
elif neighbor_dim >= dim:
|
|
|
|
neighbor_dim = 0
|
|
|
|
return neighbor_dim
|
|
|
|
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2018-12-02 16:45:08 +00:00
|
|
|
class MooreNeighborhood(Neighborhood):
|
2018-12-01 19:37:18 +00:00
|
|
|
def __init__(self, edge_rule: EdgeRule):
|
|
|
|
super().__init__([[-1, -1], [0, -1], [1, -1],
|
2018-12-02 16:45:08 +00:00
|
|
|
[-1, 0], [1, 0],
|
2018-12-01 19:37:18 +00:00
|
|
|
[-1, 1], [0, 1], [1, 1]],
|
|
|
|
edge_rule)
|