首页 > 解决方案 > 样条与python scipy中的平面相交

问题描述

我有一堆 3D 数据点,我使用 scipy 薄板样条通过它们拟合一个表面,如下所示:

import numpy as np
import scipy as sp
import scipy.interpolate

# x, y, z are the 3D point coordinates

spline = sp.interpolate.Rbf(x, y, z, function='thin_plate', smooth=5, episilon=5)
x_grid = np.linspace(0, 512, 1024)
y_grid = np.linspace(0, 512, 1024)
B1, B2 = np.meshgrid(x_grid, y_grid, indexing='xy')
Z = spline(B1, B2)

如附图所示,这可以根据需要拟合表面。

在此处输入图像描述

现在我想要做的是能够查询该样条与给定平面相交的位置。

因此,给定这个拟合曲面,例如,我如何查询(x, y)该曲面在哪些点切割平面(z = 25)

所以,上面的代码很合适:

z = f(x, y)

现在f安装好了,我想知道是否可以进行反向查找,即我想做f^{-1}(z)

标签: pythonnumpyscipy

解决方案


3D 等高线图将很好地在所需高度插入等高线:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import scipy as sp
import scipy.interpolate

N = 10
x = np.random.uniform(100, 400, N)
y = np.random.uniform(100, 400, N)
z = np.random.uniform(0, 100, N)
# x, y, z are the 3D point coordinates
spline = sp.interpolate.Rbf(x, y, z, function='thin_plate', smooth=5, episilon=5)
x_grid = np.linspace(0, 512, 1024)
y_grid = np.linspace(0, 512, 1024)
B1, B2 = np.meshgrid(x_grid, y_grid, indexing='xy')
Z = spline(B1, B2)

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.contour(B1, B2, Z, levels=[25], offset=25, colors=['red'])
ax.plot_surface(B1, B2, Z, cmap='autumn_r', lw=1, rstride=10, cstride=10, alpha=0.5)

plt.show()

轮廓

PS:如果您需要曲线的 xy 坐标,它们将作为 2d 坐标列表的列表存储在轮廓内

contour = ax.contour(B1, B2, Z, levels=[25], offset=25, colors=['red'])
for segments in contour.allsegs:
    for segment in segments:
        print("X:", segment[:,0])
        print("Y:", segment[:,1])

推荐阅读