-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspline.py
53 lines (46 loc) · 1.28 KB
/
spline.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
import numpy as np
# f(x) = a*x*x*x + b*x*x + c*x + d
# f'(x) = 3*a*x*x + 2*b*x + c
#
# d = x0
# c = dx0
# a + b + c + d = x1
# 3*a + 2*b + c = dx1
#
# a + b + dx0 + x0 = x1
# a + b = x1 - x0 - dx0
# a = x1 - x0 - dx0 - b
#
# 3*a + 2*b + dx0 = dx1
# 3*a + 2*b = dx1 - dx0
# 3*(x1 - x0 - dx0 - b) + 2*b = dx1 - dx0
# -3*b + 2*b = dx1 - dx0 - 3*(x1 - x0 - dx0)
# b = -dx1 + dx0 + 3*(x1 - x0 - dx0)
#
# a = x1 - x0 - dx0 - 0.5 * (dx1 - dx0 - 3*(x1 - x0 - dx0))
def cubic_spline_coeffs(p0, v0, p1, v1):
d = p0
c = v0
b = -v1 + v0 + 3*(p1 - p0 - v0)
a = p1 - p0 - v0 - b
return [a,b,c,d]
def cubic_spline_coeffs_list(ps, vs):
return [ cubic_spline_coeffs(ps[i], vs[i], ps[i+1], vs[i+1]) for i in range(len(ps)-1) ]
def cubic_spline(N, ps=None, vs=None, coeffs_list=None):
t = np.linspace(0,1,N)[np.newaxis].T
if coeffs_list is None:
coeffs_list = cubic_spline_coeffs_list(ps, vs)
vs = []
for a,b,c,d in coeffs_list:
v = a*t**3 + b*t**2 + c*t + d
vs.append(v)
return np.concatenate(vs).T
if __name__ == "__main__":
import matplotlib.pyplot as plt
Np=4
Nt=100
p = np.random.random((Np,2))*2-1
v = np.random.random((Np,2))*2-1
xy = cubic_spline(p, v, Nt)
plt.plot(xy[0,:], xy[1,:])
plt.show()