将 mayavi mlab.contour3d 绘图转换为 vtkPolyData [英] Convert mayavi mlab.contour3d plot to vtkPolyData
问题描述
我正在尝试从 mlab.contour3d
图中获取三角化的 vtkPolyData
.我正在使用 mayavi
因为它似乎是获得 最小表面 的最快方法a> 正确三角化.我需要它作为 vtkPolyData
因为我想将它保存为 .stl 文件.
I am trying to get the triangulated vtkPolyData
from a mlab.contour3d
plot. I am using mayavi
because it seems to be the fastest way to get minimal surfaces triangulated properly. I need it as vtkPolyData
because I want to save it as an .stl file.
这是我的代码的 MWE:
Here is a MWE of my code:
import numpy as np
from mayavi import mlab
def fun(x, y, z):
return np.cos(x) + np.cos(y) + np.cos(z)
x, y, z = np.mgrid[-1:1:100j, -1:1:100j, -1:1:100j]
contour = mlab.contour3d(x, y, z, fun)
mlab.show()
然后我从 mayavi
得到的是一个已经使用 VTK
(或 tvtk
)进行三角测量和显示的表面,所以它应该是可以从那里获得 vtkPolyData
.但到目前为止我发现的唯一方法是使用 mlab.savefig(test.obj)
导出 .obj 文件(这很糟糕,因为每次 mayavi
UI 打开)并使用 vtkOBJReader
再次导入该文件,这为我提供了我想要的 vtkPolyData
.
What I get then from mayavi
is a surface that is already triangulated and displayed using VTK
(or tvtk
), so it should be possible to get the vtkPolyData
from there. But the only way I found so far is to use mlab.savefig(test.obj)
to export an .obj-file (which is bad, because it takes time to save the file everytime the mayavi
UI opens) and import that file again using vtkOBJReader
, which gives me the vtkPolyData
I want.
有人知道更直接的方法吗?
Does anyone know a more straight-forward way to do this?
为了进一步澄清我的问题:我可以从可视化中访问数据,例如使用 mayavi.tools.pipeline.get_vtk_src()
,但它以 vtkImageData
的形式出现.如果有人知道将其转换为 vtkPolyData
的方法,那也是一个解决方案.
edit: To clarify my problem a bit more: I can access the data from the visualization e.g. with mayavi.tools.pipeline.get_vtk_src()
, but it comes in form of vtkImageData
. If anyone knows a way to convert that to vtkPolyData
, that would also be a solution.
推荐答案
很巧,我找到了解决方案.
By total coincidence I found a solution.
import numpy as np
from mayavi import mlab
def fun(x, y, z):
return np.cos(x) + np.cos(y) + np.cos(z)
x, y, z = np.mgrid[-1:1:100j, -1:1:100j, -1:1:100j]
contour = mlab.contour3d(x, y, z, fun)
actor = contour.actor.actors[0]
polydata = tvtk.to_vtk(actor.mapper.input) # solution
mlab.show()
诀窍似乎是从管道访问映射器,它是一个 PolyDataMapper.然后我只使用 tvtk.to_vtk()
函数,这样我就可以继续使用 vtk
,我更喜欢 tvtk
,至少现在是这样.
The trick seems to be to access the mapper from the pipeline, which is a PolyDataMapper. Then I just use the tvtk.to_vtk()
function so that I can continue with vtk
which I prefer over tvtk
, at least for now.
这篇关于将 mayavi mlab.contour3d 绘图转换为 vtkPolyData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!