如何在表面图上投影一条线? [英] How to project a line on a surfaceplot?

查看:30
本文介绍了如何在表面图上投影一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 CSV 文件中存储的点数据创建的曲面图.如果我想在 3D 中创建的表面上投影一条线(它漂浮在表面上方).方法是什么?

我尝试了
我生成了一个曲面,其中给定的散点存储在 CSV 文件中.现在我想将表面顶部的线(红线)投影到表面(作为绿线).

解决方案

让我们构建一个通用的 MCVE,首先我们导入所需的包:

将 numpy 导入为 np从 scipy 导入插值导入 matplotlib.pyplot 作为 plt从 mpl_toolkits 导入 mplot3d导入 matplotlib.tri 作为 mtrinp.random.seed(123456) # 修复随机种子

现在我们为表面S生成一组3D点(注意它是一个不规则的网格):

NS = 100Sx = np.random.uniform(low=-1., high=1., size=(NS,))Sy = np.random.uniform(low=-1., high=1., size=(NS,))Sz = -(Sx**2 + Sy**2) + 0.1*np.random.normal(size=(NS,))

和参数曲线P:

NP = 100t = np.linspace(-1, 1, NP)Px = tPy = t**2 - 0.5Pz = t**3 + 1

解决问题的关键是

完整的 3D 结果如下所示:

axe = plt.axes(projection='3d')axe.plot_trisurf(tri, Sz, cmap='jet', alpha=0.5)axe.plot(Px, Py, Pz)axe.plot(Px, Py, PSz, linewidth=2, color='black')axe.scatter(Sx, Sy, Sz)axe.view_init(elev=25, azim=-45)

axe.view_init(elev=75, azim=-45)

I Have a surface plot created from point data stored in a CSV file. If I want to project a line (which is floating above the surface) on the surface created in 3D. What is the method?

I have tried a code from the following post for projecting a line on xy-xz-yz plane.
I can see that it is projecting the endpoint of line on the xy-xz-yz plane.

If I want to project on the surface created with point data. I don't have an equation of the surface. I have created with point data available.

Here is a mockup image of what I'm trying to achieve:

I have generated a curved surface with given scattered points stored in a CSV file. Now I want to project the line on top of the surface(red line) to the surface(as a green line).

解决方案

Lets build a generic MCVE, first we import required packages:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.tri as mtri
np.random.seed(123456) # Fix the random seed

Now we generate a collection of 3D points for a surface S (notice it is an irregular mesh):

NS = 100
Sx = np.random.uniform(low=-1., high=1., size=(NS,))
Sy = np.random.uniform(low=-1., high=1., size=(NS,))
Sz = -(Sx**2 + Sy**2) + 0.1*np.random.normal(size=(NS,))

And a parametric curve P:

NP = 100
t = np.linspace(-1, 1, NP)
Px = t
Py = t**2 - 0.5
Pz = t**3 + 1

The key to solve your problem is LinearNDInterpolator which performs a piecewise linear interpolation in N dimensions:

PSz = interpolate.LinearNDInterpolator(list(zip(Sx, Sy)), Sz)(list(zip(Px,Py)))

There is just the need to reshape data to fit the method signature from separate vectors to matrix of shape (Nsample,Ndims) which can be translated to:

list(zip(Sx, Sy))

We can check the data from the top:

tri = mtri.Triangulation(Sx, Sy)
fig, axe = plt.subplots()
axe.plot(Sx, Sy, '+')
axe.plot(Px, Py)
axe.triplot(tri, linewidth=1, color='gray')
axe.set_aspect('equal')
axe.grid()

The complete 3D result is shown bellow:

axe = plt.axes(projection='3d')
axe.plot_trisurf(tri, Sz, cmap='jet', alpha=0.5)
axe.plot(Px, Py, Pz)
axe.plot(Px, Py, PSz, linewidth=2, color='black')
axe.scatter(Sx, Sy, Sz)
axe.view_init(elev=25, azim=-45)

axe.view_init(elev=75, azim=-45)

这篇关于如何在表面图上投影一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆