gpt4 book ai didi

python - python scipy 中样条线与平面的相交

转载 作者:行者123 更新时间:2023-12-02 19:19:18 27 4
gpt4 key购买 nike

我有一堆 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)

这适合所需的表面,如附图所示。

enter image description here

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

因此,给定这个拟合曲面,我如何查询该曲面与平面 (z = 25) 相交的 (x, y) 点。

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

z = f(x, y)

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

最佳答案

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()

contour

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])

关于python - python scipy 中样条线与平面的相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63261363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com