【发布时间】:2016-02-06 01:23:15
【问题描述】:
我需要在 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
现在,为了加快速度,我可以计算曲线上所有 100k 点的基础,将其存储在内存中,当我需要绘制曲线时,我所需要做的就是将新的控制点位置乘以存储基础以获得新曲线。为了证明我的观点,我编写了一个函数,使用DeBoor's algorithm 来计算我的基础:
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() 来计算曲线上的新点,我的问题是:是否可以使用辣味.interpolate获得(我假设) splev 用来计算其结果的基础值?如果有,怎么做?
[附录]
根据 unutbu 和 ev-br 关于 scipy 如何计算样条基的非常有用的见解,我查找了 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”到底是什么意思?您想出了如何给出 splev 结和系数,您到底在寻找什么?
-
@unutbu 我完全重写了我的问题 + 添加代码以(缓慢)获得基础
-
@ev-br 我重新写了我的问题,希望这能澄清我在寻找什么。
-
scipy.interpolate.splev将工作委托给FITPACK Fortran routine SPLEV。该例程调用fpbspl,它使用de Boor 和Cox 递归关系评估b 样条。不幸的是,scipy 没有公开要从 Python 调用的 fpbspl。它似乎只能由其他 Fortran 例程在内部调用。