[vtkusers] Translate all VTK glyphs individually in large dataset

Charles Kind charles.kind at bristol.ac.uk
Thu Nov 30 09:41:53 EST 2017


I am imaging unit 3vector data on a 2d grid of points. The vectors are cartesian. I want to display these vectors as 3d arrows centered with the mid point of the arrows total length on the plane. My idea was to create another matrix of translations, equal to -(vector/2), and then move the glyphs (or change each vector origin or whatever would work)! I therefore wanted to apply these new vectors pointwise as translations to the glyphs. I have failed and need help.

The translation needs to preserve the magnitude of the vector. Are there perhaps other ways of doing this. I note that Mayavi2 can do this with a check button but does not have the other functionality I need.

I should mention that I have only started using Python and VTK this week so please point out any idiocy in the code. Working code:

import vtk
import math
import sys #Just to break flow

# The source file
file_name = "Stable.vts"

# Read the source file.
reader = vtk.vtkXMLStructuredGridReader()
reader.SetFileName(file_name)
reader.Update()
output = reader.GetOutput()
numPoints = output.GetNumberOfPoints()
dimensions = output.GetDimensions()

#Tuple to hold spherical polar coords
mpolar = vtk.vtkFloatArray()
mpolar.SetNumberOfTuples(numPoints)
mpolar.SetNumberOfComponents(3)
mpolar.SetNumberOfValues(3*numPoints)
mpolar.SetName("mpolar")

#Tuple to hold translations
mtrans = vtk.vtkFloatArray()
mtrans.SetNumberOfTuples(numPoints)
mtrans.SetNumberOfComponents(3)
mtrans.SetNumberOfValues(3*numPoints)
mtrans.SetName("mtrans")

#Tuple copy of cartesian data
mxyz = vtk.vtkFloatArray()
mxyz = output.GetPointData().GetAbstractArray(0)
mxyz.SetName("mxyz")

#Loop through input data and convert to spherical polar
#Also set points equal to input points
#Also create vector transform for each vector
for x in range(numPoints):
    azi = math.atan2(output.GetPointData().GetAbstractArray(0).GetTuple(x)[0],output.GetPointData().GetAbstractArray(0).GetTuple(x)[1])
    pol = math.acos(output.GetPointData().GetAbstractArray(0).GetTuple(x)[2])
    rad = 1
    mpolar.SetTuple3(x, azi, pol, rad)
    mtrans.SetTuple3(x, -output.GetPointData().GetAbstractArray(0).GetTuple(x)[0]/2, \
        -output.GetPointData().GetAbstractArray(0).GetTuple(x)[1]/2, \
        -output.GetPointData().GetAbstractArray(0).GetTuple(x)[2]/2)

#Define new structured grid
skrGrid = vtk.vtkStructuredGrid()
skrGrid.SetDimensions(dimensions)
skrGrid.SetPoints(output.GetPoints())
skrGrid.GetPointData().AddArray(mpolar)
skrGrid.GetPointData().AddArray(mxyz)
skrGrid.GetPointData().AddArray(mtrans)

#Write new structured grid
#writer = vtk.vtkXMLStructuredGridWriter()
#writer.SetFileName("skrGrid.vts")
#writer.SetInputData(skrGrid)
#writer.Write()

#Subsample grid
subs = 11
extract = vtk.vtkExtractGrid()
extract.SetInputData(skrGrid)
extract.SetSampleRate(subs, subs, 1)
extract.Update()
#extract.IncludeBoundaryOn() #Get boundary even if not sampled

#Cast data into PolyData format for Glyphs
pd = vtk.vtkPolyData()
pd.SetPoints(extract.GetOutput().GetPoints())
pd.GetPointData().SetVectors(extract.GetOutput().GetPointData().GetAbstractArray(1))

arrowSource = vtk.vtkArrowSource()

glyph3D = vtk.vtkGlyph3D()
glyph3D.SetSourceConnection(arrowSource.GetOutputPort())
glyph3D.SetVectorModeToUseVector()
glyph3D.SetInputData(pd)
glyph3D.SetScaleFactor(1e-07)
glyph3D.Update()

# Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(glyph3D.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Visualize
camera = vtk.vtkCamera()
camera.SetPosition(5e-07, -15e-07, 1e-06)
camera.SetFocalPoint(5e-07, 5e-07, 0)

# Create a renderer, render window, and interactor
renderer = vtk.vtkRenderer()
renderer.SetActiveCamera(camera)

renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Add the actor to the scene
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)  # Background color white
renderer.ResetCameraClippingRange() # Call because without it you need window input to make render display!

# enable user interface interactor
# Render and interact
renderWindow.SetSize(800,600)
renderWindowInteractor.Initialize()
renderWindow.Render()
renderWindowInteractor.Start()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171130/44a7a843/attachment-0001.html>


More information about the vtkusers mailing list