python - 样条与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)
解决方案
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])
推荐阅读
- android - ImageButtons 返回到旧视图 Pager 片段时保持不变
- android - 更新到 Android Studio 3.2 时出现 Gradle 错误
- vue.js - 如何为单页应用程序生成 XML 站点地图?
- oracle - Visual Studio 错误 175:在配置中找不到指定的商店提供程序,或者无效 - 但提供程序在文件中
- node.js - 如何在 CentOS 服务器上安装 Parse Server?
- ruby-on-rails - 未处理的服务器错误!!!属性的未知属性“图像”
- c# - 如何将扩展方法的 this 与 params 关键字结合起来?
- c# - 在 C# 中使用代码授权进行 Azure AD 身份验证
- android - 如何将更复杂的对象(例如 Car)(带有子对象,例如 Tiers,带有子对象,例如 Rims,...)保存到 Google Firebase?
- android - 具有多语言支持的多种“风味”