-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcatmull_rom_spline.py
104 lines (88 loc) · 3.37 KB
/
catmull_rom_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import numpy as np
def catmull_rom_one_point(x, v0, v1, v2, v3):
"""Computes interpolated y-coord for given x-coord using Catmull-Rom.
Computes an interpolated y-coordinate for the given x-coordinate between
the support points v1 and v2. The neighboring support points v0 and v3 are
used by Catmull-Rom to ensure a smooth transition between the spline
segments.
Args:
x: the x-coord, for which the y-coord is needed
v0: 1st support point
v1: 2nd support point
v2: 3rd support point
v3: 4th support point
"""
c1 = 1. * v1
c2 = -.5 * v0 + .5 * v2
c3 = 1. * v0 + -2.5 * v1 + 2. * v2 -.5 * v3
c4 = -.5 * v0 + 1.5 * v1 + -1.5 * v2 + .5 * v3
return (((c4 * x + c3) * x + c2) * x + c1)
def catmull_rom(p_x, p_y, res):
"""Computes Catmull-Rom Spline for given support points and resolution.
Args:
p_x: array of x-coords
p_y: array of y-coords
res: resolution of a segment (including the start point, but not the
endpoint of the segment)
"""
# create arrays for spline points
x_intpol = np.empty(res*(len(p_x)-1) + 1)
y_intpol = np.empty(res*(len(p_x)-1) + 1)
# set the last x- and y-coord, the others will be set in the loop
x_intpol[-1] = p_x[-1]
y_intpol[-1] = p_y[-1]
# loop over segments (we have n-1 segments for n points)
for i in range(len(p_x)-1):
# set x-coords
x_intpol[i*res:(i+1)*res] = np.linspace(
p_x[i], p_x[i+1], res, endpoint=False)
if i == 0:
# need to estimate an additional support point before the first
y_intpol[:res] = np.array([
catmull_rom_one_point(
x,
p_y[0] - (p_y[1] - p_y[0]), # estimated start point,
p_y[0],
p_y[1],
p_y[2])
for x in np.linspace(0.,1.,res, endpoint=False)])
elif i == len(p_x) - 2:
# need to estimate an additional support point after the last
y_intpol[i*res:-1] = np.array([
catmull_rom_one_point(
x,
p_y[i-1],
p_y[i],
p_y[i+1],
p_y[i+1] + (p_y[i+1] - p_y[i]) # estimated end point
) for x in np.linspace(0.,1.,res, endpoint=False)])
else:
y_intpol[i*res:(i+1)*res] = np.array([
catmull_rom_one_point(
x,
p_y[i-1],
p_y[i],
p_y[i+1],
p_y[i+2]) for x in np.linspace(0.,1.,res, endpoint=False)])
return (x_intpol, y_intpol)
if __name__ == '__main__':
import matplotlib.pyplot as plt
# set the resolution (number of interpolated points between each pair of
# points, including the start point, but excluding the endpoint of each
# interval)
res = 50
# generate some random support points
p_x = np.arange(-10,11, dtype='float32')
p_y = np.zeros_like(p_x)
for i in range(len(p_x)):
p_y[i] = np.random.rand()*3. - 1.5
# do the catmull-rom
x_intpol, y_intpol = catmull_rom(p_x, p_y, res)
# fancy plotting
plt.figure()
plt.scatter(p_x, p_y)
plt.plot(x_intpol, y_intpol)
plt.show()
# vim: set ts=4 sw=4 sts=4 expandtab: