在三维直线上为给定z插值xy值的最快方法

2024-09-30 02:18:10 发布

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

我有一系列的3d线条,我知道每一条线条的顶点x和z构成了它们。 Example

我需要的是得到每条线的xy坐标,给定该线中包含的z坐标。从数字上讲,我知道如何解决这个问题,但是我希望在python中找到执行此任务的最快/最有效的方法

例如,给定一条xyz值为​​我已按以下方式存储:

>>> arr = array([[  6.04691088e+05,   4.11484364e+06,  -7.35610000e+00],
   [  6.04671604e+05,   4.11484372e+06,  -7.00000000e+00],
   [  6.04665386e+05,   4.11484374e+06,  -6.83720000e+00],
   [  6.04659607e+05,   4.11484376e+06,  -6.69120000e+00],
   [  6.04633141e+05,   4.11484387e+06,  -6.00000000e+00],
   [  6.04615502e+05,   4.11484394e+06,  -5.41870000e+00],
   [  6.04604279e+05,   4.11484398e+06,  -5.00000000e+00],
   [  6.04599722e+05,   4.11484400e+06,  -4.69160000e+00],
   [  6.04585486e+05,   4.11484406e+06,  -4.00000000e+00],
   [  6.04585333e+05,   4.11484406e+06,  -3.99290000e+00],
   [  6.04584247e+05,   4.11484406e+06,  -3.93210000e+00],
   [  6.04567577e+05,   4.11484413e+06,  -3.00000000e+00],
   [  6.04558717e+05,   4.11484416e+06,  -2.41210000e+00],
   [  6.04553510e+05,   4.11484419e+06,  -2.06660000e+00],
   [  6.04552563e+05,   4.11484419e+06,  -2.00000000e+00],
   [  6.04552018e+05,   4.11484419e+06,  -1.96420000e+00],
   [  6.04537647e+05,   4.11484425e+06,  -1.00000000e+00],
   [  6.04530721e+05,   4.11484428e+06,  -5.93300000e-01],
   [  6.04519556e+05,   4.11484432e+06,   0.00000000e+00],
   [  6.04519355e+05,   4.11484432e+06,   0.00000000e+00],
   [  6.04518720e+05,   4.11484432e+06,   0.00000000e+00],
   [  6.04518556e+05,   4.11484432e+06,   0.00000000e+00],
   [  6.04518296e+05,   4.11484433e+06,   0.00000000e+00],
   [  6.04516286e+05,   4.11484433e+06,   5.36200000e-01],
   [  6.04514899e+05,   4.11484434e+06,   8.77000000e-01],
   [  6.04514371e+05,   4.11484434e+06,   1.00000000e+00],
   [  6.04513926e+05,   4.11484434e+06,   1.12940000e+00],
   [  6.04511400e+05,   4.11484435e+06,   2.00000000e+00],
   [  6.04509132e+05,   4.11484436e+06,   2.38240000e+00],
   [  6.04508615e+05,   4.11484436e+06,   2.50000000e+00],
   [  6.04507578e+05,   4.11484437e+06,   2.68050000e+00],
   [  6.04506375e+05,   4.11484437e+06,   2.91140000e+00]])

我希望获得任意z的插值xy坐标,例如1.38,以便:

>>> intrp3line(arr,z_value=1.38)
[[x1,y1],[x2,y2],...,[xn,yn]] 

它应该返回一个列表,其中所有xy坐标对都进行了插值,以实现给定的z

我发现的几乎所有关于插值的信息都是为了解决相反的问题,也就是说,插值给定xy的坐标z a,但与我提出的问题相关的信息很少。另一方面,考虑到我有N条线,它们的xyz坐标和nz值列表​​(每行一条),我还想知道是否有任何方法可以通过处理一组行来优化插值,而不是逐个迭代


Tags: 方法信息列表valueexample方式数字array
1条回答
网友
1楼 · 发布于 2024-09-30 02:18:10

一种方法是像这样使用scikit learn的线性回归

from sklearn.linear_model import LinearRegression

line = LinearRegression().fit(arr[:,2].reshape(-1, 1), arr[:,0:2])

要获得积分,您只需使用line.predict

z = np.linspace(-8,2,10**5)
xy = line.predict(z.reshape(-1, 1))

对于10^5z值,这需要约1.26毫秒,并且似乎近似线性缩放

你也可以画出来

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)
ax.view_init(elev=0, azim=0)#
ax.scatter(arr[:,0],arr[:,1],arr[:,2])
ax.plot(xy[:,0], xy[:,1], z)

enter image description here

相关问题 更多 >

    热门问题