[vtkusers] A question about colormapping and picking of 3d glyphs.

Geoframer geoframer at gmail.com
Wed May 16 10:40:00 EDT 2007


Hello all,

I'm trying two things which I cannot seem to get to work. I would appreciate
any pointers or helpful advice you can give.


Problem 1. : I've created a lookuptable which I want to use to map glyphs
created at certain points to certain colors. In the sample code below i've
created 8 points which i try to map to two colors, the first four points to
one color and the second four points to a different color. From what i've
been able to figure out I'm supposed to give the lookuptable to the
mapper... My visualisation however shows no colors whatever but gives no
errors either. Any clarification how to properly use a lookuptable combined
with pointids would be greatly appreciated.


Problem 2. : I'm currently using a programmable glyph filter to map the
glyphs to the points. I also defined a picker on the glyphs so I'm able to
pick the glyphs in the visualisation. Currently however whenever I pick a
glyph I get a high point-id instead of 0 to 7 (for the points i'm using). I
understand why this happens, but I can't seem to be able to circumvent this
or create a proper workaround. I read the following post :

http://public.kitware.com/pipermail/vtkusers/2001-April/055637.html
but that only seems to work if you don't alter the sources... which is
something I might want to do later. Basically what I want is to be able to
retrieve the pointId which is used to create the glyph. Again any help would
be greatly appreciated.

Regards - Geofram

The sample code :

import vtk
import random
from vtk.util.colors import *

#create some arbitrary vectordata identified by a string
points = vtk.vtkPoints()

data = {'avector':(0,0,0),
'bvector':(0,0,1),'cvector':(0,1,0),'dvector':(1,0,0),
        'evector':(1,1,0),
'fvector':(1,0,1),'gvector':(0,1,1),'hvector':(1,1,1)}

keys = data.keys()
keys.sort()

for i, key in enumerate(keys):
    vector = data.get(key)
    points.InsertPoint(i, vector[0], vector[1], vector[2])

graph = vtk.vtkGraph ()
graph.SetPoints(points)
graph.SetNumberOfVertices(len(data))

[graph.AddEdge(0,i+1) for i in range(len(data)-1)]

graphtopoly = vtk.vtkGraphToPolyData()
graphtopoly.SetInput (graph)

#create the tube, mapper and actor for the edges
tubes = vtk.vtkTubeFilter()
tubes.SetInputConnection(graphtopoly.GetOutputPort())
tubes.SetRadius(0.002)
tubes.SetNumberOfSides(3)

mapEdges = vtk.vtkPolyDataMapper()
mapEdges.SetInputConnection(tubes.GetOutputPort())

edgeActor = vtk.vtkActor()
edgeActor.SetMapper(mapEdges)
edgeActor.GetProperty().SetColor(peacock)
edgeActor.GetProperty().SetSpecularColor(1, 1, 1)
edgeActor.GetProperty().SetSpecular(0.3)
edgeActor.GetProperty().SetSpecularPower(20)
edgeActor.GetProperty().SetAmbient(0.2)
edgeActor.GetProperty().SetDiffuse(0.8)

#create the lookuptable for colors, ball, programmable glyph and glyphactor
lut = vtk.vtkLookupTable()
lut.SetNumberOfColors(2)
lut.SetNumberOfTableValues(8)

lut.SetTableValue(0, 1.0, 1.0, 1.0, 1.0)
lut.SetTableValue(1, 1.0, 1.0, 1.0, 1.0)
lut.SetTableValue(2, 1.0, 1.0, 1.0, 1.0)
lut.SetTableValue (3, 1.0, 1.0, 1.0, 1.0)
lut.SetTableValue(4, 0.5, 0.5, 0.5, 1.0)
lut.SetTableValue(5, 0.5, 0.5, 0.5, 1.0)
lut.SetTableValue(6, 0.5, 0.5, 0.5, 1.0)
lut.SetTableValue(7, 0.5, 0.5, 0.5, 1.0)

def Glyph():
    ptId = glypher.GetPointId()
    #print ptId
    pd = glypher.GetPointData ()
    xyz = glypher.GetPoint()
    x = xyz[0]
    y = xyz[1]
    z = xyz[2]
    ball.SetCenter(xyz)

ball = vtk.vtkSphereSource()
ball.SetRadius(0.010)
ball.SetThetaResolution(12)
ball.SetPhiResolution (12)

glypher = vtk.vtkProgrammableGlyphFilter()
glypher.SetInputConnection(graphtopoly.GetOutputPort())
glypher.SetSource(ball.GetOutput())
glypher.SetGlyphMethod(Glyph)
glyphMapper = vtk.vtkPolyDataMapper()
glyphMapper.SetInputConnection(glypher.GetOutputPort())
glyphMapper.SetLookupTable(lut)

glyphActor = vtk.vtkActor()
glyphActor.SetMapper(glyphMapper)

#create a text mapper and actor
textMapper = vtk.vtkTextMapper()
textProp = textMapper.GetTextProperty()
textProp.SetFontSize(20)
textProp.SetColor(1,0,0)
textActor = vtk.vtkActor2D()
textActor.VisibilityOff()
textActor.SetMapper(textMapper)

#create a point picker.
picker = vtk.vtkPointPicker()
picker.AddPickList (glyphActor)
picker.PickFromListOn()
picker.SetTolerance(0.001)

def pointCoords(object, event):
    global picker, textActor, textMapper
    if picker.GetPointId() < 0:
        textActor.VisibilityOff ()
    else:
        selPt = picker.GetSelectionPoint()
        pickPos = picker.GetPickPosition()
        print picker.GetPointId()
        textMapper.SetInput("(%.6f, %.6f, %.6f)"%pickPos)
        textActor.SetPosition(selPt[:2])
        textActor.VisibilityOn()

# at the end of pick call pointcoords
picker.AddObserver("EndPickEvent", pointCoords)

# create the renderwindow, interactor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

interact = vtk.vtkRenderWindowInteractor()
interact.SetRenderWindow(renWin)
interact.SetPicker(picker)

ren.AddActor2D(textActor)
ren.AddActor(edgeActor)
ren.AddActor(glyphActor)

renWin.GetInteractor().Initialize()
renWin.Render()
renWin.GetInteractor ().Start()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070516/ba10ef6b/attachment.htm>


More information about the vtkusers mailing list