[vtkusers] vtkCellPicker failing to pick vtkPolyLine (using Python and wx)

David vanEe david.vanee at convergent.ca
Tue Dec 11 12:51:00 EST 2012


Sure, this version will work with just python/vtk.  Thanks for taking a look.


import vtk

class MyInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
    def __init__(self, actorData, actorList):
        
        self.actorData = actorData
        self.actorList = actorList
        self.cellPicker = vtk.vtkCellPicker()
        
        self.AddObserver("MouseMoveEvent", self.OnMotion)
        
    def OnMotion(self, obj, event):
        
        iren = obj.GetInteractor()
        renwin = iren.GetRenderWindow()
        ren = renwin.GetRenderers().GetFirstRenderer()
        
        x, y = iren.GetEventPosition()
        
        # invert Y value
        actualY = ren.GetSize()[1] - y
        
        # dim all actors
        for (index, actor) in enumerate(self.actorList):
            (zValue, cellType, dimColor, brightColor) = self.actorData[index]
            actor.GetProperty().SetDiffuseColor(*dimColor)
        
        self.cellPicker.Pick(x, actualY, 0, ren)
        dataSet = self.cellPicker.GetDataSet()
        
        if dataSet is not None:
            # highlight picked actor
            for (index, actor) in enumerate(self.actorList):
                if actor.GetMapper().GetInput() == dataSet:
                    (zOffset, cellType, dimColor, brightColor) = self.actorData[index]
                    actor.GetProperty().SetDiffuseColor(*brightColor)
                    
        renwin.Render()
        
# simple point coords used by both actors
coords = [(0.0, 0.0),
          (1.0, 0.0),
          (1.0, 1.0),
          (0.0, 1.0)]

actorData = [(1.0, vtk.VTK_POLY_LINE, (0.5,0.0,0.0), (1.0, 0.0, 0.0)),
             (0.0, vtk.VTK_QUAD, (0.0, 0.0, 0.5), (0.0, 0.0, 1.0))]

ren = vtk.vtkRenderer()

renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)

actorList = []
for data in actorData:
    (zValue, cellType, dimColor, brightColor) = data
    
    points = vtk.vtkPoints()
    for (pointIndex, coord) in enumerate(coords):
        points.InsertPoint(pointIndex, coord[0], coord[1], zValue)
    
    idList = vtk.vtkIdList()
    idList.SetNumberOfIds(len(coords))
    for pointIndex in range(len(coords)):
        idList.SetId(pointIndex, pointIndex)
    cells = vtk.vtkCellArray()
    cells.InsertNextCell(idList)
    
    grid = vtk.vtkUnstructuredGrid()
    grid.SetPoints(points)
    grid.SetCells(cellType, cells)
    
    mapper = vtk.vtkDataSetMapper()
    mapper.SetInput(grid)
    
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetRepresentation(1)
    actor.GetProperty().SetLineWidth(5.0)
    actor.GetProperty().SetDiffuseColor(*dimColor)
    
    ren.AddActor(actor)
    actorList.append(actor)
    
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetInteractorStyle(MyInteractorStyle(actorData, actorList))
interactor.SetRenderWindow(renwin)

interactor.Initialize()
interactor.Start()

    

-----Original Message-----
From: David Gobbi [mailto:david.gobbi at gmail.com] 
Sent: Tuesday, December 11, 2012 9:08 AM
To: David vanEe
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] vtkCellPicker failing to pick vtkPolyLine (using Python and wx)

Hi David,

I was the one who made the changes to the vtkCellPicker code, and I'd be glad to investigate to see why the behavior changed.

Can you send me an example that uses just python, i.e. no wx?

 - David

On Tue, Dec 11, 2012 at 9:26 AM, David vanEe <david.vanee at convergent.ca> wrote:
> Hi all,
>
>
>
> I've recently updated my VTK from 5.4 to 5.10.1, and some old 
> vtkCellPicker code isn't doing what I want anymore.  This 'simple' 
> example works fine in
> 5.4 (both the blue and red actor highlight when you mouse-over them), 
> but in
> 5.10.1 only the blue (quad) actor works.  The only difference between 
> the two actors (aside from coordinates and colors) is the cellType of 
> vtk.VTK_POLY_LINE vs vtk.VTK_QUAD.
>
>
>
> I've tried looking for changes in the pick routine, but 
> vtkCellPicker.cxx has undergone substantial changes and I was unable 
> to locate the cause.  Any suggestions for getting the picker to pick the polyline?
>
>
>
> Thanks in advance,
>
> Dave
>
>
>
>
>
> import wx
>
> import vtk
>
>
>
> from vtk.wx.wxVTKRenderWindowInteractor import 
> wxVTKRenderWindowInteractor
>
>
>
> class myVTKInteractor(wxVTKRenderWindowInteractor):
>
>     def __init__(self, parent):
>
>         wxVTKRenderWindowInteractor.__init__(self, parent, -1)
>
>
>
>         self.Enable(1)
>
>         self.AddObserver("ExitEvent", lambda o,e,f=self: f.Close())
>
>
>
>         self.ren = vtk.vtkRenderer()
>
>         self.GetRenderWindow().AddRenderer(self.ren)
>
>
>
>         # simple point coords used by both actors
>
>         coords = [(0.0, 0.0),
>
>                   (1.0, 0.0),
>
>                   (1.0, 1.0),
>
>                   (0.0, 1.0)]
>
>
>
>         self.actorData = [(1.0, vtk.VTK_POLY_LINE, (0.5,0.0,0.0), 
> (1.0, 0.0, 0.0)),
>
>                           (0.0, vtk.VTK_QUAD, (0.0, 0.0, 0.5), (0.0, 
> 0.0, 1.0))]
>
>
>
>         self.actorList = []
>
>         for data in self.actorData:
>
>             (zValue, cellType, dimColor, brightColor) = data
>
>
>
>             points = vtk.vtkPoints()
>
>             for (pointIndex, coord) in enumerate(coords):
>
>                 points.InsertPoint(pointIndex, coord[0], coord[1], 
> zValue)
>
>
>
>             idList = vtk.vtkIdList()
>
>             idList.SetNumberOfIds(len(coords))
>
>             for pointIndex in range(len(coords)):
>
>                 idList.SetId(pointIndex, pointIndex)
>
>             cells = vtk.vtkCellArray()
>
>             cells.InsertNextCell(idList)
>
>
>
>             grid = vtk.vtkUnstructuredGrid()
>
>             grid.SetPoints(points)
>
>             grid.SetCells(cellType, cells)
>
>
>
>             mapper = vtk.vtkDataSetMapper()
>
>             mapper.SetInput(grid)
>
>
>
>             actor = vtk.vtkActor()
>
>             actor.SetMapper(mapper)
>
>             actor.GetProperty().SetRepresentation(1)
>
>             actor.GetProperty().SetLineWidth(5.0)
>
>             actor.GetProperty().SetDiffuseColor(*dimColor)
>
>
>
>             self.ren.AddActor(actor)
>
>             self.actorList.append(actor)
>
>
>
>         self.cellPicker = vtk.vtkCellPicker()
>
>
>
>
>
>     def OnMotion(self, event):
>
>
>
>         # invert Y value
>
>         actualY = self.ren.GetSize()[1] - event.GetY()
>
>
>
>         # dim all actors
>
>         for (index, actor) in enumerate(self.actorList):
>
>             (zValue, cellType, dimColor, brightColor) = 
> self.actorData[index]
>
>             actor.GetProperty().SetDiffuseColor(*dimColor)
>
>
>
>         self.cellPicker.Pick(event.GetX(), actualY, 0, self.ren)
>
>         dataSet = self.cellPicker.GetDataSet()
>
>
>
>         if dataSet is not None:
>
>             # highlight picked actor
>
>             for (index, actor) in enumerate(self.actorList):
>
>                 if actor.GetMapper().GetInput() == dataSet:
>
>                     (zOffset, cellType, dimColor, brightColor) = 
> self.actorData[index]
>
>                     actor.GetProperty().SetDiffuseColor(*brightColor)
>
>
>
>         self.Render()
>
>
>
>
>
> def wxVTKRenderWindowInteractorExample():
>
>     app = wx.PySimpleApp()
>
>
>
>     frame = wx.Frame(None, -1, "wxVTKRenderWindowInteractor",
> size=(400,400))
>
>
>
>     widget = myVTKInteractor(frame)
>
>     sizer = wx.BoxSizer(wx.VERTICAL)
>
>     sizer.Add(widget, 1, wx.EXPAND)
>
>     frame.SetSizer(sizer)
>
>     frame.Layout()
>
>
>
>     frame.Show()
>
>
>
>     app.MainLoop()
>
>
>
> if __name__ == "__main__":
>
>     wxVTKRenderWindowInteractorExample()
>
>
>
>
>
>
>
> --
>
> David A. Van Ee, BASc, EIT
>
> Convergent Manufacturing Technologies Inc.
>
> 6190 Agronomy Rd, Suite 403
>
> Vancouver BC Canada V6T 1Z3
>
>
>
> Email: david.vanee at convergent.ca | Tel: 604-822-9682 x103
>
> WWW: http://www.convergent.ca | Fax: 604-822-9659



More information about the vtkusers mailing list