[vtkusers] Color by vector of glyph

Eric E. Monson emonson at cs.duke.edu
Fri Oct 22 10:15:32 EDT 2010


Hello,

When you say "using their direction" you mean you want to color your glyphs by a certain vector component, you need to build a lookup table and specify there which vector component to use (or whether to use the magnitude instead). I'll attach an example that I made by modifying your code (including using a different data source just for convenience in my testing).

-Eric

------------------------------------------------------
Eric E Monson
Duke Visualization Technology Group


# =======================

import vtk
import math

# The Wavelet Source is nice for generating a test vtkImageData set
rt = vtk.vtkRTAnalyticSource()
rt.SetWholeExtent(-5,5,-5,5,-5,5)
 
# Take the gradient of the only scalar 'RTData' to get a vector attribute
grad = vtk.vtkImageGradient()
grad.SetDimensionality(3)
grad.SetInputConnection(rt.GetOutputPort())

# For some reason gradient filter output doesn't get assigned to 'vectors' by default
assign = vtk.vtkAssignAttribute()
assign.SetInputConnection(grad.GetOutputPort())
assign.Assign('RTDataGradient', \
               vtk.vtkDataSetAttributes.VECTORS, \
               vtk.vtkAssignAttribute.POINT_DATA)
# Older versions of VTK wrapping use:
# assign.Assign('RTDataGradient', 1, 0)

mapper = vtk.vtkPolyDataMapper()
ren = vtk.vtkRenderer()

# Only glyph a random subset of the points
maskP = vtk.vtkMaskPoints()
maskP.SetOnRatio(4)
maskP.RandomModeOn()
maskP.SetInputConnection(assign.GetOutputPort())

# Source for the glyph filter
arw= vtk.vtkArrowSource()
arrow = vtk.vtkArrowSource()
arrow.SetTipResolution(16)
arrow.SetTipLength(0.3)
arrow.SetTipRadius(0.1)

glyp = vtk.vtkGlyph3D()
glyp.SetSource(arrow.GetOutput())
glyp.SetInput(maskP.GetOutput())
# Tell glyph which arrays to use for 'scalars' and 'vectors'
glyp.SetInputArrayToProcess(0,0,0,0,'RTData')		# scalars
glyp.SetInputArrayToProcess(1,0,0,0,'RTDataGradient')		# vectors

glyp.SetVectorModeToUseVector()
glyp.SetScaleFactor(0.075)
glyp.SetColorModeToColorByVector()
glyp.SetScaleModeToScaleByVector()
glyp.OrientOn()
glyp.Update()

# Generate a lookup table for coloring by vector components or magnitude
lut = vtk.vtkLookupTable()
lut.SetValueRange(0.5, 1.0)
lut.SetSaturationRange(0.1, 1.0)
lut.SetHueRange(0.4,0.6)
lut.SetRampToLinear()
# When using a vector component for coloring
lut.SetVectorModeToComponent()
lut.SetVectorComponent(1)
# When using vector magnitude for coloring
# lut.SetVectorModeToMagnitude()
lut.Build()
  
mapper.SetInput(glyp.GetOutput())
mapper.SetLookupTable(lut)
mapper.ScalarVisibilityOn()
mapper.SetScalarModeToUsePointFieldData()
mapper.SelectColorArray('RTDataGradient')
# When using a vector component for coloring
mapper.SetScalarRange(assign.GetOutput().GetPointData().GetVectors().GetRange(1))
# When using vector magnitude for coloring
# mapper.SetScalarRange(assign.GetOutput().GetPointData().GetVectors().GetRange(-1))

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

# outline
outline = vtk.vtkOutlineFilter()
outline.SetInput(glyp.GetOutput())
mapper2 = vtk.vtkPolyDataMapper()
mapper2.SetInput(outline.GetOutput())
actor2 = vtk.vtkActor()
actor2.SetMapper(mapper2)

ren.AddActor(actor)
ren.AddActor(actor2)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
istyle = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(istyle)
iren.SetRenderWindow(renWin)
ren.ResetCamera()
renWin.Render()

iren.Start()



On Oct 21, 2010, at 9:01 AM, ravikg wrote:

> 
> Hi,
> 
> I am new to VTK. I am trying a lot and searched a lot but could not get any
> help to the color working. I want to colour the vector arrow using thier
> direction. Please help.
> Below is the code of my program:
> 
> import vtk
> import math
> 
> input_data = 'uvadata/tornado.vtk'
> 
> reader = vtk.vtkStructuredPointsReader()
> mapper = vtk.vtkPolyDataMapper()
> ren = vtk.vtkRenderer()
> 
> reader.SetFileName(input_data)
> 
> mapper.SetScalarRange(0.0,51.7)
> 
> #filter
> maskP = vtk.vtkMaskPoints()
> maskP.SetOnRatio(50)
> maskP.RandomModeOn()
> maskP.SetInputConnection(reader.GetOutputPort())
> 
> #arrow
> arw= vtk.vtkArrowSource()
> arrow = vtk.vtkArrowSource()
> arrow.SetTipResolution(16)
> arrow.SetTipLength(0.3)
> arrow.SetTipRadius(0.1)
> 
> glyp = vtk.vtkGlyph3D()
> glyp.SetSource(arrow.GetOutput())
> glyp.SetInput(maskP.GetOutput())
> glyp.SetVectorModeToUseVector()
> glyp.SetScaleFactor(0.15)
> glyp.SetColorModeToColorByVector()
> glyp.SetScaleModeToScaleByVector()
> glyp.OrientOn()
> glyp.Update()
> 
> mapper.SetInput(glyp.GetOutput())
> mapper.SetScalarRange(reader.GetOutput().GetScalarRange())
> mapper.ScalarVisibilityOn()
> mapper.SetScalarModeToUsePointFieldData()
> 
> actor = vtk.vtkActor()
> actor.SetMapper(mapper)
> 
> # outline
> outline = vtk.vtkOutlineFilter()
> outline.SetInput(glyp.GetOutput())
> mapper2 = vtk.vtkPolyDataMapper()
> mapper2.SetInput(outline.GetOutput())
> actor2 = vtk.vtkActor()
> actor2.SetMapper(mapper2)
> 
> ren.AddActor(actor)
> ren.AddActor(actor2)
> renWin = vtk.vtkRenderWindow()
> renWin.AddRenderer(ren)
> iren = vtk.vtkRenderWindowInteractor()
> istyle = vtk.vtkInteractorStyleTrackballCamera()
> iren.SetInteractorStyle(istyle)
> iren.SetRenderWindow(renWin)
> ren.ResetCamera()
> renWin.Render()
> 
> iren.Start()
> 
> -- 
> View this message in context: http://vtk.1045678.n5.nabble.com/Color-by-vector-of-glyph-tp3230451p3230451.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




More information about the vtkusers mailing list