-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
Copy pathspline_continuity.py
55 lines (42 loc) · 1.38 KB
/
spline_continuity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
class Spline2D:
def __init__(self, x, y, kind="cubic"):
self.s = self.__calc_s(x, y)
self.sx = interpolate.interp1d(self.s, x, kind=kind)
self.sy = interpolate.interp1d(self.s, y, kind=kind)
def __calc_s(self, x, y):
self.ds = np.hypot(np.diff(x), np.diff(y))
s = [0.0]
s.extend(np.cumsum(self.ds))
return s
def calc_position(self, s):
x = self.sx(s)
y = self.sy(s)
return x, y
def main():
x = [-2.5, 0.0, 2.5, 5.0, 7.5, 3.0, -1.0]
y = [0.7, -6, -5, -3.5, 0.0, 5.0, -2.0]
ds = 0.1 # [m] distance of each interpolated points
plt.subplots(1)
plt.plot(x, y, "xb", label="Data points")
for (kind, label) in [("linear", "C0 (Linear spline)"),
("quadratic", "C0 & C1 (Quadratic spline)"),
("cubic", "C0 & C1 & C2 (Cubic spline)")]:
rx, ry = [], []
sp = Spline2D(x, y, kind=kind)
s = np.arange(0, sp.s[-1], ds)
for i_s in s:
ix, iy = sp.calc_position(i_s)
rx.append(ix)
ry.append(iy)
plt.plot(rx, ry, "-", label=label)
plt.grid(True)
plt.axis("equal")
plt.xlabel("x[m]")
plt.ylabel("y[m]")
plt.legend()
plt.show()
if __name__ == '__main__':
main()