[vtkusers] Mouse picking using vtk + wxpython
Tatiana Al-Chueyr Pereira Martins
tatiana.martins at cenpra.gov.br
Fri Jun 10 12:59:47 EDT 2005
:. vtk 4.4
:. wxpython 2.4
:. madrake 10.0
I'm newbie to both vtk and wxpython, so I guess I'm making lots of
mistakes here. Hope someone may help me.
I would like to pick an actor under the cursor in a render window
pressing a mouse button.
In my code a 2d actor is included in a vtk render window. That 2d actor
was created with a png image and it was put in the "center" (or
somewhere near it) of the render window.
In the program, whenever I click on the actor (the cube image attached),
"picked actor" should be printed in the konsole. However, when I run the
program this message is not printed when the actor is clicked. It works
as if the cube was in the vtk origin (0,0), although you see the cube in
the center. So, the message only appears when I click in a area equal to
the cube's, begining in (0,0)/the left down corner. Why does this
occur?! Am I positioning in the wrong way the cube actor?
You can see the relevant piece of the code here:
--------------------------------------------------------------------------------
from wxPython.wx import *
from vtkpython import *
def create(parent):
return wxFrame1(parent)
[wxID_WXFRAME1, wxID_WXFRAME1PANEL1, wxID_WXFRAME1PANEL2,
] = map(lambda _init_ctrls: wxNewId(), range(3))
class wxFrame1(wxFrame):
def _init_ctrls(self, prnt):
# generated method, don't edit
wxFrame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,
pos=wxPoint(0, 200), size=wxSize(272, 255),
style=wxDEFAULT_FRAME_STYLE, title='wxFrame1')
self.SetClientSize(wxSize(264, 227))
self.panel1 = wxPanel(id=wxID_WXFRAME1PANEL1, name='panel1',
parent=self, pos=wxPoint(0, 0), size=wxSize(264, 227),
style=wxTAB_TRAVERSAL)
self.panel2 = wxPanel(id=wxID_WXFRAME1PANEL2, name='panel2',
parent=self.panel1, pos=wxPoint(0, 0), size=wxSize(60, 60),
style=wxTAB_TRAVERSAL)
self.panel2.SetBackgroundColour(wxColour(128, 128, 128))
def __init__(self, parent):
self._init_ctrls(parent)
self.iniciaVtkCubo()
def iniciaVtkCubo(self):
windowSize = 60
self.picker = vtkPropPicker()
# obtain the image of a cube
cubeReader = vtkPNGReader()
cubeReader.SetFileName("cube.png")
# create cube mapper
cubeMapper = vtkImageMapper()
cubeMapper.SetInput(cubeReader.GetOutput())
cubeMapper.SetColorLevel(127)
cubeMapper.SetColorWindow(255)
cubeMapper.SetZSlice(0)
# create cube actor
cubeActor = vtkActor2D()
cubeActor.SetMapper(cubeMapper)
cubeActor.SetPosition(10, 13)
cubeActor.AddObserver("PickEvent", lambda actor ,event ,i=0:
self.ClickActor(actor,event,i))
# create renderer
ren = vtkRenderer()
ren.SetBackground(1,1,1)
# assign our actor to the renderer
ren.AddActor2D(cubeActor)
paneMainSlices2D =
wxVTKRenderWindowInteractor(self.panel2,-1,width = windowSize,height =
windowSize,stereo = 1)
paneMainSlices2D.GetRenderWindow().AddRenderer(ren)
paneMainSlices2D.Render()
EVT_LEFT_DOWN(paneMainSlices2D, self.UpdateVolume)
def ClickActor(self,actor,event,point):
print "picked actor ", point
def UpdateVolume(self,event):
y = event.GetY()
x = event.GetX()
markers = self.ren.GetActors2D()
self.picker.PickProp(x, windowSize-y ,self.ren, markers)
event.Skip()
--------------------------------------------------------------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cubo.png
Type: image/png
Size: 353 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20050610/1c9db43f/attachment.png>
More information about the vtkusers
mailing list