简体   繁体   中英

VTK Python Wrapper - Exporting Surface Plot

I'm using VTK 8.0.1 and python 3.5 and am brand new to VTK. I am trying to export a surface plot using vtkPlotSurface.

By referencing TestSurfacePlot.cxx I have successfully created a surface plot and have been able to render it in python (even though it doesn't really look like a surface plot).

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()

In order to better visualize what I've made, I wanted to try and export it and then visualize using Paraview/Visit. However, I'm struggling to find any concrete examples where this type of vtk object is exported...

I have tried adding the following:

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

But end up with the following type error:

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

Can anyone provide assistance? Thanks in advance.

You might benefit from using PyVista as it makes creating these types of spatially reference datasets and rendering much more user-friendly. I would avoid using a vtkTable like you have above and move towards VTK data objects that actually represent meshes/surfaces.

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM