[vtkusers] Plot a surface graph with VTK

Marco Nawijn nawijn at gmail.com
Thu Nov 21 05:57:08 EST 2013


Hi,

Find below a Python sample that generates an unstructured grid file in VTK
XML format. Based on this code it should be easy to create the corresponding
C++ code (I don't have time now to create the sample myself in C++).

If you run the Python code (note that you have to install the VTK Python
bindings), it will generate a datafile that can be read into Paraview.


===== SCRIPT ========================================
#!/usr/bin/env python

from vtk import vtkPoints, vtkCellArray, vtkVertex, vtkUnstructuredGrid
from vtk import vtkFloatArray
from vtk import vtkXMLUnstructuredGridWriter

from random import random

coords = (
(0,0,2),
(1,0,2),
(1,1,2),
(0,1,2),
(2,0,2),
(3,0,2),
(3,1,2),
(2,1,2))

#=== Create a VTK array to store points
points = vtkPoints()

#=== Loop over the list of coordinates and insert them into the array; store
# corresponding point index (or id)
pids = []
for crd in coords:
   pids.append(points.InsertNextPoint(crd))

# Create a VTK array to store cells
cells = vtkCellArray()

vertex = vtkVertex()
typecode = vertex.GetCellType()
ncellpoints = vertex.GetNumberOfPoints()

for p in pids:
   # Notify the cells array of how many cellpoints are needed to describe
the
   # next cell that we want to insert
   cells.InsertNextCell(ncellpoints)

# For a vertex, we only need to insert a single point index; for a triangle
# this would typically be a loop
cells.InsertCellPoint(p)

# Create a data array
#

temperature = vtkFloatArray()
temperature.SetName('temperature')

npoints = points.GetNumberOfPoints()
temperature.SetNumberOfValues(npoints)

# Insert some random data
for i in range(npoints):
    temperature.SetValue(i, random())

grid = vtkUnstructuredGrid()
grid.SetPoints(points)
grid.SetCells(typecode, cells)

grid.GetPointData().SetScalars(temperature)

writer = vtkXMLUnstructuredGridWriter()
writer.SetDataModeToAscii()
writer.SetInput(grid)
writer.SetFileName('vertex_demo.vtu')
writer.Write()



On Thu, Nov 21, 2013 at 11:14 AM, madz <madaramh at gmail.com> wrote:

> Thanks for the suggestion but Mayavi2 will not work since it's python and
> I'm
> working on a c++ project. :(
>
>
>
>
> --
> View this message in context:
> http://vtk.1045678.n5.nabble.com/Plot-a-surface-graph-with-VTK-tp5724519p5724544.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20131121/1c3647a7/attachment.htm>


More information about the vtkusers mailing list