bspline怎么使用 python_如何获取scipy.interpolate.splev使用的样条线基础

我需要在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个控制点并要求它评估曲线上的10万个点很容易:

# 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

这是“ points_scipy”的图:

现在,要使其更快,我可以为曲线上的所有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 f

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值