import numpy as np
def interpcurve(N,pX,pY):
#equally spaced in arclength
N=np.transpose(np.linspace(0,1,N))
#how many points will be uniformly interpolated?
nt=N.size
#number of points on the curve
n=pX.size
pxy=np.array((pX,pY)).T
p1=pxy[0,:]
pend=pxy[-1,:]
last_segment= np.linalg.norm(np.subtract(p1,pend))
epsilon= 10*np.finfo(float).eps
#IF the two end points are not close enough lets close the curve
if last_segment > epsilon*np.linalg.norm(np.amax(abs(pxy),axis=0)):
pxy=np.vstack((pxy,p1))
nt = nt + 1
else:
print('Contour already closed')
pt=np.zeros((nt,2))
#Compute the chordal arclength of each segment.
chordlen = (np.sum(np.diff(pxy,axis=0)**2,axis=1))**(1/2)
#Normalize the arclengths to a unit total
chordlen = chordlen/np.sum(chordlen)
#cumulative arclength
cumarc = np.append(0,np.cumsum(chordlen))
tbins= np.digitize(N,cumarc) # bin index in which each N is in
#catch any problems at the ends
tbins[np.where(tbins<=0 | (N<=0))]=1
tbins[np.where(tbins >= n | (N >= 1))] = n - 1
s = np.divide((N - cumarc[tbins]),chordlen[tbins-1])
pt = pxy[tbins,:] + np.multiply((pxy[tbins,:] - pxy[tbins-1,:]),(np.vstack([s]*2)).T)
return pt
首先,感谢约翰·德瑞科先生为interparc服务。多好的工作啊!在
我也面临这个问题,但不熟悉MATLAB引擎API。鉴于此,我尝试将部分interparc Matlab代码转换为Python(只包括线性插入,因为它足以解决我的问题)。在
下面是我的代码;希望它能帮助所有寻求类似东西的Python:
我会使用interparc,这是我设计的一个工具。它通过2个或更多维度的常规空间曲线拟合样条曲线,然后选择沿该曲线等距分布的点。在三次样条函数的情况下,该方法使用odesolver进行数值积分,因此速度稍慢,但仍然相当快。在许多情况下,一个简单的线性插值(如我在这里使用的)将是完全足够的,而且非常快。在
这条曲线可能是完全一般的,甚至会越过它自己。我将给出一个三维空间曲线的简单示例:
^{pr2}$你的“曲线”是一组连接一组点的线段。每条线段都有一个长度;曲线的总长度是这些线段长度的总和。在
因此,计算
d = totalCurveLength / (numberOfPoints - 1)
,并将曲线分割为(numberOfPoints - 1)
长度为d
的块。在相关问题 更多 >
编程相关推荐