VTK Python Wrapper - 导出曲面图

时间:2017-09-11 18:24:50

标签: c++ python-3.5 vtk paraview

我使用VTK 8.0.1和python 3.5并且是VTK的新手。我正在尝试使用vtkPlotSurface导出曲面图。

通过引用TestSurfacePlot.cxx我已经成功创建了一个曲面图并且能够在python中渲染它(尽管它看起来并不像表面图)。

import vtk
import math as m
import numpy as np

## Set things up
chart = vtk.vtkChartXYZ()
view = vtk.vtkContextView()
view.GetRenderWindow().SetSize(800,800)
view.GetScene().AddItem(chart)

## Create a surface
table = vtk.vtkTable()
numPoints = 70;
inc = 9.424778 / (numPoints - 1);
for i in range(0,numPoints):
    arr = vtk.vtkFloatArray()
    table.AddColumn(arr)

table.SetNumberOfRows(numPoints)
for i in range(0,numPoints):
    x = i * inc;
    for j in range(0,numPoints):
        y  = j * inc;
        table.SetValue(i, j, m.sin(m.sqrt(x*x + y*y)))

# Using table, create a surface plot
test = vtk.vtkPlotSurface()
test.SetXRange(0,9.424778)
test.SetYRange(0,9.424778)
test.SetInputData(table)

# Start visualizing the surface plot
chart.AddPlot(test)
view.GetRenderWindow().SetMultiSamples(0)
view.GetInteractor().Initialize()
view.GetRenderWindow().Render()

out = vtk.vtkOBJExporter()
out.SetFilePrefix("test")
out.SetInput(chart)
out.Write()


view.GetInteractor().Start()

为了更好地想象我所制作的内容,我想尝试导出它,然后使用Paraview / Visit进行可视化。但是,我很难找到导出这种类型的vtk对象的具体例子...

我尝试添加以下内容:

out = vtk.vtkOBJExporter()
out.SetFilePrefix("test")
out.SetInput(chart)
out.Write()

但最终会出现以下类型错误:

TypeError: SetInput argument 1: method requires a vtkRenderWindow, a vtkContextView was provided.

有人可以提供帮助吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用PyVista可能会让您受益,因为它可以创建这些类型的空间参考数据集并呈现更加用户友好的格式。我会避免像上面一样使用vtkTable,而转向实际上代表网格/曲面的VTK数据对象。

import pyvista as pv
import numpy as np

# Create a spatial reference
numPoints = 70
inc = 9.424778 / (numPoints - 1)
x = np.arange(0, numPoints) * inc
y = np.arange(0, numPoints) * inc
xx, yy, _ = np.meshgrid(x, y, [0])
zz = np.sin(np.sqrt(xx*xx + yy*yy))
# Make a PyVista/VTK mesh
surface = pv.StructuredGrid(xx, yy, zz)

# Plot it!
surface.plot(show_edges=True, show_grid=True, notebook=False)

# or save it out for opening in ParaView
surface.save("my_surface.vtk")

enter image description here