Skip to content

Instantly share code, notes, and snippets.

@nathnim
Forked from jaems33/path_smoothing.py
Created May 30, 2022 12:01
Show Gist options
  • Save nathnim/ea3907b74eab75d542030abaae4ef29c to your computer and use it in GitHub Desktop.
Save nathnim/ea3907b74eab75d542030abaae4ef29c to your computer and use it in GitHub Desktop.
Path smoothing
from copy import deepcopy
def printpaths(path, newpath):
for old, new in zip(path, newpath):
print('[' + ', '.join('%.3f' % x for x in old) +
'] -> [' + ', '.join('%.3f' % x for x in new) + ']')
path = [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [4, 3], [4, 4]]
def smooth(path, weight_data=0.5, weight_smooth=0.1, tolerance=0.000001):
"""
Creates a smooth path for a n-dimensional series of coordinates.
Arguments:
path: List containing coordinates of a path
weight_data: Float, how much weight to update the data (alpha)
weight_smooth: Float, how much weight to smooth the coordinates (beta).
tolerance: Float, how much change per iteration is necessary to keep iterating.
Output:
new: List containing smoothed coordinates.
"""
new = deepcopy(path)
dims = len(path[0])
change = tolerance
while change >= tolerance:
change = 0.0
for i in range(1, len(new) - 1):
for j in range(dims):
x_i = path[i][j]
y_i, y_prev, y_next = new[i][j], new[i - 1][j], new[i + 1][j]
y_i_saved = y_i
y_i += weight_data * (x_i - y_i) + weight_smooth * (y_next + y_prev - (2 * y_i))
new[i][j] = y_i
change += abs(y_i - y_i_saved)
return new
printpaths(path, smooth(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment