我正在使用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.
任何人都可以提供帮助吗?预先感谢。
您可能会受益于使用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")