Python 中的 3D 插值重温

问题描述 投票:0回答:1

我在 3D 空间中有一条分段线性路径,包含一个 (x, y, z) 值数组和一个给出沿路径距离的值数组。我想找到沿着路径的其他(x,y,z)点的距离,这些点都位于路径“上”(即到路径段之一的距离小于某个公差)。

我发现了这个算法 通过 python 对不规则 (x,y,z) 网格进行 4D 插值并重现了答案。

但是,当我使用我的值时,我会得到一个

nan
数组。非常感谢任何解决此问题的建议 - 提前致谢!

import scipy.interpolate
import numpy as np

nodes = np.array([
 [511.03925, 897.2107, 48.937611],
 [499.58658, 889.2893, 49.988685],
 [474.94204, 872.2437, 51.114033],
 [461.30299, 862.8101, 51.072050],
 [450.27944, 855.1856, 50.847374],
 [425.61826, 838.1285, 50.344743],
 [400.95708, 821.0714, 49.842111]])

vals = np.array([3496.03,  3510.00,  3540.00,  3556.59,  3570.00,  3600.00,  3630.00])

pts = np.array([
 [492.09, 884.11, 50.33],
 [482.34, 877.36, 50.78],
 [488.52, 881.64, 50.49],
 [476.24, 873.14, 51.05],
 [482.34, 877.36, 50.78]])

dist = scipy.interpolate.griddata(nodes, vals, pts)
print(dist)
python scipy
1个回答
0
投票

由于您的点都位于分段线性段上,因此这不是 3D 插值;而是这是一维插值:

import numpy as np

nodes = np.array((
    (511.03925, 897.2107, 48.937611),
    (499.58658, 889.2893, 49.988685),
    (474.94204, 872.2437, 51.114033),
    (461.30299, 862.8101, 51.072050),
    (450.27944, 855.1856, 50.847374),
    (425.61826, 838.1285, 50.344743),
    (400.95708, 821.0714, 49.842111),
))
vals = np.array((
    3496.03,  3510.00,  3540.00,  3556.59,  3570.00,  3600.00,  3630.00,
))
order = nodes[:, 0].argsort()
nodes = nodes[order, :]
vals = vals[order]

pts = np.array((
    (492.09, 884.11, 50.33),
    (482.34, 877.36, 50.78),
    (488.52, 881.64, 50.49),
    (476.24, 873.14, 51.05),
    (482.34, 877.36, 50.78),
))

dist = np.interp(
    x=pts[:, 0], xp=nodes[:, 0], fp=vals,
)
print(dist)
[3519.12564812 3530.99440282 3523.4714383  3538.41998268 3530.99440282]
© www.soinside.com 2019 - 2024. All rights reserved.