从一组不规则的点开始,在 3D 表面中插入 Z 值 [关闭]

2023-12-08

我有一个类似于图 A 的表面,想象一下这是俯视图。该表面已计算 Z 值。

enter image description here

Now I need to find all Z values in new points like figure B. How to do this? I tried scipy.interpolate.interp2d but it gives some weird results like this: enter image description here

我只想在“图”内找到自定义 x 和 y 的自定义 z 。

最小代码示例

func_int = scipy.interpolate.interp2d([point[0] for point in pointsbottom],[point[1] for point in pointsbottom],[point[2] for point in pointsbottom], kind = 'linear')
pointscaption = map(lambda point:(point[0],point[1],func_int(point[0],point[1])),pointscaption)

Where pointsbottom是 (x,y,z) 的列表并且pointscaption是 (x,y,z) 的列表,但我需要找到新的 z。


尝试使用griddata反而:

    grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')

不同之处在于 griddata 期望常规数据作为输入(嗯......,我认为)。这并不是说您应该得到不同的结果,而是您应该能够更快地发现问题。您可以轻松屏蔽“常规网格”数据。

我的第一个猜测是这些输入坐标不是您所期望的(可能与您正在计算的函数具有不同的比例),但如果不进行测试就很难说。

在任何情况下,您似乎都需要一个表面,根据定义,它是一种网格数据,因此使用这个不同的框架应该很容易发现问题。

编辑(对海报的疑问的进一步考虑):

假设您想要某个对象并在其中输入一些数据。完成此操作后,您希望能够使用该数据来估计任何位置。为此,您可以构建一个如下所示的类:

    import numpy as np

    class Estimation():
        def __init__(self,datax,datay,dataz):
            self.x = datax
            self.y = datay
            self.v = dataz

        def estimate(self,x,y,using='ISD'):
            """
            Estimate point at coordinate x,y based on the input data for this
            class.
            """
            if using == 'ISD':
                return self._isd(x,y)
        
        def _isd(self,x,y):
            d = np.sqrt((x-self.x)**2+(y-self.y)**2)
            if d.min() > 0:
                v = np.sum(self.v*(1/d**2)/np.sum(1/d**2))
                return v
            else:
                return self.v[d.argmin()]

此示例使用反平方距离方法,该方法对于估计非常稳定(如果避免除以零)。它不会很快,但我希望它是可以理解的。从此时起,您可以通过执行以下操作来估计 2D 空间中的任何点:

    e = Estimation(datax,datay,dataz)
    newZ = e.estimate(30,55) # the 30 and 55 are just example coordinates

如果您要对整个网格执行此操作:

    datax,datay = np.random.randint(0,100,10),np.random.randint(0,100,10)
    dataz       = datax/datay

    e = Estimation(datax,datay,dataz)

    surf = np.zeros((100,100))
    for i in range(100):
        for j in range(100):
            surf[i,j] = e.estimate(i,j)

您将获得一个可以使用 matplotlib 等工具看到的图像(其中颜色代表表面的高度):

    import matplotlib.pyplot as plt
    plt.imshow(surf.T,origin='lower',interpolation='nearest')
    plt.scatter(datax,datay,c=dataz,s=90)
    plt.show()

这个实验的结果是这样的:

Estimated Surface

如果您不想使用 ISD(平方反比距离),只需在 Estimation 类上实现一个新方法即可。这是你想要的?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从一组不规则的点开始,在 3D 表面中插入 Z 值 [关闭] 的相关文章

随机推荐