我需要在Python中评估B样条。为此,我编写了下面的代码,它非常有效。
import numpy as np
import scipy.interpolate as si
def scipy_bspline(cv,n,degree):
""" bspline basis function
c = list of control points.
n = number of points on the curve.
degree = curve degree
"""
# Create a range of u values
c = cv.shape[0]
kv = np.clip(np.arange(c+degree+1)-degree,0,c-degree)
u = np.linspace(0,c-degree,n)
# Calculate result
return np.array(si.splev(u, (kv,cv.T,degree))).T
给定6个控制点并要求在曲线上评估100k个点非常容易:
# Control points
cv = np.array([[ 50., 25., 0.],
[ 59., 12., 0.],
[ 50., 10., 0.],
[ 57., 2., 0.],
[ 40., 4., 0.],
[ 40., 14., 0.]])
n = 100000 # 100k Points
degree = 3 # Curve degree
points_scipy = scipy_bspline(cv,n,degree) #cProfile clocks this at 0.012 seconds
现在,为了使其更快,我可以计算曲线上所有10万个点的基础,并将其存储在内存中。当我需要绘制曲线时,我只需要将新的控制点位置与存储的基础相乘即可得到新的曲线。为了证明我的观点,我编写了一个使用DeBoor算法来计算我的基础的函数:
def basis(c, n, degree):
""" bspline basis function
c = number of control points.
n = number of points on the curve.
degree = curve degree
"""
# Create knot vector and a range of samples on the curve
kv = np.array([0]*degree + range(c-degree+1) + [c-degree]*degree,dtype='int') # knot vector
u = np.linspace(0,c-degree,n) # samples range
# Cox - DeBoor recursive function to calculate basis
def coxDeBoor(u, k, d):
# Test for end conditions
if (d == 0):
if (kv[k] <= u and u < kv[k+1]):
return 1
return 0
Den1 = kv[k+d] - kv[k]
Den2 = 0
Eq1 = 0
Eq2 = 0
if Den1 > 0:
Eq1 = ((u-kv[k]) / Den1) * coxDeBoor(u,k,(d-1))
try:
Den2 = kv[k+d+1] - kv[k+1]
if Den2 > 0:
Eq2 = ((kv[k+d+1]-u) / Den2) * coxDeBoor(u,(k+1),(d-1))
except:
pass
return Eq1 + Eq2
# Compute basis for each point
b = np.zeros((n,c))
for i in xrange(n):
for k in xrange(c):
b[i][k%c] += coxDeBoor(u[i],k,degree)
b[n-1][-1] = 1
return b
现在让我们使用这个来计算一个新的基础,将其与控制点相乘,并确认我们得到与splev相同的结果:
b = basis(len(cv),n,degree) #5600011 function calls (600011 primitive calls) in 10.975 seconds
points_basis = np.dot(b,cv) #3 function calls in 0.002 seconds
print np.allclose(points_basis,points_scipy) # Returns True
我的极慢的函数在11秒内返回了100k个基础值,但由于这些值只需要计算一次,因此通过这种方式计算曲线上的点比通过splev计算要快6倍。
我能够从我的方法和splev获得完全相同的结果,这让我相信,在内部,splev可能像我一样计算基础,只是速度更快。
因此,我的目标是找出如何快速计算我的基础,将其存储在内存中,并只使用np.dot()来计算曲线上的新点。我的问题是:是否可以使用spicy.interpolate获取splev用于计算其结果的基础值?如果可以,怎么做?
[补充]
在unutbu和ev-br非常有用的见解之后,我查找了Fortran代码并尽力编写了一个等效的代码:
def fitpack_basis(c, n=100, d=3, rMinOffset=0, rMaxOffset=0):
""" fitpack's spline basis function
c = number of control points.
n = number of points on the curve.
d = curve degree
"""
# Create knot vector
kv = np.array([0]*d + range(c-d+1) + [c-d]*d, dtype='int')
# Create sample range
u = np.linspace(rMinOffset, rMaxOffset + c - d, n) # samples range
# Create buffers
b = np.zeros((n,c)) # basis
bb = np.zeros((n,c)) # basis buffer
left = np.clip(np.floor(u),0,c-d-1).astype(int) # left knot vector indices
right = left+d+1 # right knot vector indices
# Go!
nrange = np.arange(n)
b[nrange,left] = 1.0
for j in xrange(1, d+1):
crange = np.arange(j)[:,None]
bb[nrange,left+crange] = b[nrange,left+crange]
b[nrange,left] = 0.0
for i in xrange(j):
f = bb[nrange,left+i] / (kv[right+i] - kv[right+i-j])
b[nrange,left+i] = b[nrange,left+i] + f * (kv[right+i] - u)
b[nrange,left+i+1] = f * (u - kv[right+i-j])
return b
针对unutbu版本的我的原始基础函数进行测试:
fb = fitpack_basis(c,n,d) #22 function calls in 0.044 seconds
b = basis(c,n,d) #81 function calls (45 primitive calls) in 0.013 seconds ~5 times faster
print np.allclose(b,fb) # Returns True
我的函数比原来慢了5倍,但仍然相对较快。我喜欢它的原因是它让我可以使用超出边界的样本范围,这在我的应用中非常有用。例如:
print fitpack_basis(c,5,d,rMinOffset=-0.1,rMaxOffset=.2)
[[ 1.331 -0.3468 0.0159 -0.0002 0. 0. ]
[ 0.0208 0.4766 0.4391 0.0635 0. 0. ]
[ 0. 0.0228 0.4398 0.4959 0.0416 0. ]
[ 0. 0. 0.0407 0.3621 0.5444 0.0527]
[ 0. 0. -0.0013 0.0673 -0.794 1.728 ]]
因此,我可能会使用fitpack_basis,因为它相对较快。但我希望能够提供改进其性能的建议,并希望能接近unutbu版本的原始基础函数。
splev
有一个参数可以获取样条的导数(der)。查看源代码,我发现splev
将该参数传递给_fitpack._spl_
,但是我找不到这个函数。我的目标是弄清楚如何计算导数,以便我可以使用这些信息在特定位置上计算样条线的切线。 - Fnordsplder
和https://github.com/scipy/scipy/pull/3174。然而,更多讨论可能需要另外一个问题。祝好运! - ev-br