在空间中的单个点处插入3d数据(Python 2.7)

时间:2016-04-11 19:52:36

标签: python-2.7 numpy 3d linear-interpolation

我有一个4维的点云,其中云中的每个点都有一个位置和一个值(x,y,z,Value)。另外,我有一个特殊的'点,S0,在3d点云内;我使用this示例来查找云中最接近的10个点,相对于S0。现在,我为10个最近的点及其值中的每一个都有一个numpy数组。如何插入这10个点,以找到S0点的插值?示例代码如下所示:

import numpy as np
import matplotlib.pyplot as plt

numpoints = 20
linexs = 320
lineys = 40
linezs = 60
linexe = 20
lineye = 20
lineze = 0

# Create vectors of points
xpts = np.linspace(linexs, linexe, numpoints)
ypts = np.linspace(lineys, lineye, numpoints)
zpts = np.linspace(linezs, lineze, numpoints)
lin = np.dstack((xpts,ypts,zpts))

# Image line of points
fig = plt.figure()
ax = fig.add_subplot(211, projection='3d')
ax.set_xlim(0,365); ax.set_ylim(-85, 85); ax.set_zlim(0, 100)
ax.plot_wireframe(xpts, ypts, zpts)
ax.view_init(elev=12, azim=78)

def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) + vmin

n = 10
for n in range(21):
    xs = randrange(n, 0, 350)
    ys = randrange(n, -75, 75)
    zs = randrange(n, 0, 100)
    ax.scatter(xs, ys, zs)
dat = np.dstack((xs,ys,zs))
ax.set_xlabel('X Label')
ax.set_xlim(0,350)
ax.set_ylabel('Y Label')
ax.set_ylim(-75,75)
ax.set_zlabel('Z Label')
ax.set_zlim(0,100)

ax = fig.add_subplot(212, projection='3d')
ax.set_xlim(0,365); ax.set_ylim(-85, 85); ax.set_zlim(0, 100)
ax.plot_wireframe(xpts,ypts,zpts)
ax.view_init(elev=12, azim=78)

plt.show()
dist = []

# Calculate distance from first point to all other points in cloud
for l in range(len(xpts)):
    aaa = lin[0][0]-dat
    dist.append(np.sqrt(aaa[0][l][0]**2+aaa[0][l][1]**2+aaa[0][l][2]**2))
full = np.dstack((dat,dist))
aaa = full[0][full[0][:,3].argsort()]
print(aaa[0:10])

1 个答案:

答案 0 :(得分:2)

一个基本的例子。请注意,插值不需要meshgrid,而只需快速ufunc生成示例函数A=f(x,y,z),此处为A=x+y+z

from scipy.interpolate import interpn
import numpy as np

#make up a regular 3d grid 
X=np.linspace(-5,5,11)
Y=np.linspace(-5,5,11)
Z=np.linspace(-5,5,11)
xv,yv,zv = np.meshgrid(X,Y,Z)

# make up a function   
# see http://docs.scipy.org/doc/numpy/reference/ufuncs.html
A = np.add(xv,np.add(yv,zv))   
#this one is easy enough for us to know what to expect at (.5,.5,.5)

# usage : interpn(points, values, xi, method='linear', bounds_error=True, fill_value=nan) 
interpn((X,Y,Z),A,[0.5,0.5,0.5])

输出:

array([ 1.5])

如果你传递了一系列兴趣点,它会给你多个答案。