如何将点均匀分布在一个立方体上

2024-10-03 00:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些三维的任意曲线,由XYZ笛卡尔点组成。这些点分布不均匀(有时间因素)。我如何“重建”曲线与给定数量的点应该组成曲线。我在3D建模程序中看到了这一点,所以我很确定这是可能的,我只是不知道怎么做。在

enter image description here

基于这个答案,我需要在python中使用它,所以我开始将interparc转换成python。我得到了线性插值。它可能效率低下并且有冗余,但可能对某些人有用http://pastebin.com/L9NFvJyA


Tags: 答案程序comhttp数量时间建模曲线
3条回答

首先,感谢约翰·德瑞科先生为interparc服务。多好的工作啊!在

我也面临这个问题,但不熟悉MATLAB引擎API。鉴于此,我尝试将部分interparc Matlab代码转换为Python(只包括线性插入,因为它足以解决我的问题)。在

下面是我的代码;希望它能帮助所有寻求类似东西的Python:

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,这是我设计的一个工具。它通过2个或更多维度的常规空间曲线拟合样条曲线,然后选择沿该曲线等距分布的点。在三次样条函数的情况下,该方法使用odesolver进行数值积分,因此速度稍慢,但仍然相当快。在许多情况下,一个简单的线性插值(如我在这里使用的)将是完全足够的,而且非常快。在

这条曲线可能是完全一般的,甚至会越过它自己。我将给出一个三维空间曲线的简单示例:

t = linspace(0,1,500).^3;
x = sin(2*pi*t);
y = sin(pi*t);
z = cos(3*x + y);
plot3(x,y,z,'o')
grid on
box on
view(-9,12)

enter image description here

^{pr2}$

enter image description here

你的“曲线”是一组连接一组点的线段。每条线段都有一个长度;曲线的总长度是这些线段长度的总和。在

因此,计算d = totalCurveLength / (numberOfPoints - 1),并将曲线分割为(numberOfPoints - 1)长度为d的块。在

相关问题 更多 >