Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
Better constraints on locating initial neighborhood around the input vertex
  • Loading branch information
oskay committed Jan 3, 2022
1 parent 4c5ffa4 commit 4c738c4
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions inkscape driver/spatial_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"""
spatial_grid.py
Specialized grid spatial index class for calculating nearest neighbors
Specialized flat grid spatial index class for calculating nearest neighbors
"""


Expand All @@ -41,7 +41,7 @@

class Index:
''' Grid index class '''
grid = [] # The grid; list of path ends in each cell
grid = [] # The grid: List of cells, each of which will contain a list of path ends
adjacents = [] # Adjacency list; list of neighboring cells for each cell
lookup = [] # List of which grid cell each path end can be found inside.
path_count = 0 # Initial number of paths
Expand Down Expand Up @@ -135,7 +135,7 @@ def __init__(self, vertices, bins_per_side, reverse):

def find_adjacents(self):
'''
Also populate an adjacency list, where each cell contains a list of
Populate an adjacency list, where each cell contains a list of
which cells are that cell or its neighbors; up to 9 possible.
'''
max_bin = self.bins_per_side - 1
Expand Down Expand Up @@ -187,9 +187,10 @@ def nearest(self, vertex_in):

max_bin = self.bins_per_side - 1

x_bin = min(math.floor((vertex_in[0] - self.xmin) / self.bin_size_x), max_bin)
y_bin = min(math.floor((vertex_in[1] - self.ymin) / self.bin_size_y), max_bin)
last_cell = max(x_bin + self.bins_per_side * y_bin, 0)
# Use max/min to constrain the initial row and column of our first cell to check
x_bin = max(min(math.floor((vertex_in[0] - self.xmin) / self.bin_size_x), max_bin), 0)
y_bin = max(min(math.floor((vertex_in[1] - self.ymin) / self.bin_size_y), max_bin), 0)
last_cell = x_bin + self.bins_per_side * y_bin

neighborhood_cells = self.adjacents[last_cell].copy()

Expand Down Expand Up @@ -229,7 +230,7 @@ def nearest(self, vertex_in):
def remove_path(self, path_index):
'''
Remove the vertex with the given path_index from the spatial index.
path_index should be less than self.path_count.
Input path_index must be < self.path_count.
If reversing is enabled, also remove the vertex with index
path_index + self.path_count
Expand All @@ -238,9 +239,7 @@ def remove_path(self, path_index):
cell_number = self.lookup[path_index]
self.grid[cell_number].remove(path_index)

if not self.reverse:
return

other_index = path_index + self.path_count
cell_number = self.lookup[other_index]
self.grid[cell_number].remove(other_index)
if self.reverse:
other_index = path_index + self.path_count
cell_number = self.lookup[other_index]
self.grid[cell_number].remove(other_index)

0 comments on commit 4c738c4

Please sign in to comment.