[vtkusers] Re: 3D density surface plot

Raymond Chan rchan79 at gmail.com
Wed Jun 1 03:58:11 EDT 2005


Was facing a similar problem recently so I hacked together some code
in Python to mimic a matlab surf plot function. Its not really good
but it does work. Hope it helps.

from scipy import *
from vtk import *

#analytic solution
xa = arange(0, 1, .01)
ta = arange(0, 0.1, .001)

za = e**(-4*pi**2*ta)*sin(2*pi*xa[:,NewAxis])

#create points
points = vtkPoints()
points.SetNumberOfPoints(len(xa)*len(ta))
for i in arange(0, len(xa)):
	for j in arange(0, len(ta)):
		points.InsertPoint(i+len(xa)*j, xa[i], 10*ta[j], za[i,j]); 

#create quad unstructured grid
quadgrida = vtkUnstructuredGrid()
quad = vtkQuad()

for i in arange(1, len(xa)):
	for j in arange(1, len(ta)):
		quad.GetPointIds().SetId(0, (i-1)+len(xa)*(j-1))
		quad.GetPointIds().SetId(1, (i-1)+len(xa)*j)
		quad.GetPointIds().SetId(2, i+len(xa)*j)
		quad.GetPointIds().SetId(3, i+len(xa)*(j-1))
		quadgrida.InsertNextCell(quad.GetCellType(), quad.GetPointIds())

quadgrida.SetPoints(points)

#terminate pipeline with mapper process object
dataMappera = vtkDataSetMapper()
dataMappera.SetInput(quadgrida)

#create an actor
dataActora = vtkActor()
dataActora.SetMapper(dataMappera)
#dataActora.GetProperty().SetRepresentationToWireframe()
dataActora.GetProperty().SetColor(1, 0, 1)

#create renderer and assign actors to it
ren1 = vtkRenderer()
ren1.AddActor(dataActora)
ren1.SetBackground(1, 1, 1)

#add renderer to render window
renWin = vtkRenderWindow()
renWin.AddRenderer(ren1)
renWin.SetSize(300, 300)

#create the interactor
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

#create an interaction style and attach it to the interactor
style = vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)

#start event loop
iren.Initialize()
iren.Start()

-- 
Regards,
Raymond Chan



More information about the vtkusers mailing list