Reputation: 11
I have a XY points list (trackP[][]) for car track, and i want to "smooth" it. Because in pymunk (and other physics engines) there is no more complex shapes than polys (or segments), in my opinion I need to interpolate data points, to remove sharp corners... How can I do that? I will need that knowledge to generate "Hill climb racing" track too (smooth track from set of points). My track at the moment (it is procedural generated from seed):track
EDIT: I've found an answer, it's chaikin's corner cutting algorithm. Below is my "fork" of code from here
def chaikins_corner_cutting(coords, refinements=5):
coords.insert(0,coords[len(coords)-1])
coords.append(coords[1])
coords = np.array(coords)
for _ in range(refinements):
L = coords.repeat(2, axis=0)
R = np.empty_like(L)
R[0] = L[0]
R[2::2] = L[1:-1:2]
R[1:-1:2] = L[2::2]
R[-1] = L[-1]
coords = L * 0.75 + R * 0.25
coords2 = coords.tolist()
for i in range(2**refinements):
coords2.pop(0)
i+=1
for i in range(2**refinements):
coords2.pop(len(coords2)-1)
return coords2
And here is final effect after some angle fixing:
Upvotes: 1
Views: 372
Reputation: 811
I am sure a lot of implementations exist for Splines in Python. The one I know best is availably in SciPy:
https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.interpolate.spline.html
>>> import scipy.interpolate as spi
>>> x = [0,1,2,3]
>>> y = [1,5,3,4]
>>> fsmooth = spi.InterpolatedUnivariateSpline(x, y)
>>> fsmooth([0.5, 0.9, 1.1, 1.5])
array([4.3125, 5.0185, 4.9215, 4.1875])
However, this works for functions y=f(x)
in a mathematical sense where for each x value only one y value is visited. Your track is not a function in this sense. So you would need to find a different representation first.
Upvotes: 1