[vtkusers] picking on a surface
John Hunter
jdhunter at ace.bsd.uchicago.edu
Wed Apr 7 15:08:02 EDT 2004
>>>>> "John" == John Hunter <jdhunter at ace.bsd.uchicago.edu> writes:
John> I would like to interactively select points on a surface.
John> Ultimately, I want to do this with surfaces constructed from
John> marching cubes algorithm on medical data, but I am
John> practicing by trying to select points on the surface of the
John> sphere. In my example below (cell picker) the selected
John> points appear either on the surface of the sphere or at the
John> very center, but the points selected (which I mark with a
John> smaller sphere) seem to bear no relation to the place where
John> I click.
I think I mostly figured this one out. I believe the problem with the
other code was that the id I was using in the data set was not proper
for that data set so I was effectively randomly picking cells.
I'm having much better luck with
picker = vtk.vtkCellPicker()
picker.PickFromListOn()
picker.AddPickList(actor)
picker.SetTolerance(0.01)
picker.Pick(x, y, 0, renderer)
points = picker.GetPickedPositions()
numPoints = points.GetNumberOfPoints()
if numPoints<1: return
pnt = points.GetPoint(0)
I'm still not sure how to best handle the case where numPoints>1, or
what data structure I should be using to get the cell that corresponds
to the id returned by picker.GetCellId(), but the example below works
for my purposes now so on to the real data!
Below is the complete example, for the archives....
JDH
#!/usr/bin/env python
import vtk
renderer = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renWin)
sphere = vtk.vtkSphereSource()
sphere.SetRadius(20)
res = 20
sphere.SetThetaResolution(res)
sphere.SetPhiResolution(res)
sphere.SetCenter(0,0,0)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(sphere.GetOutput())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetOpacity(0.5)
renderer.AddActor(actor)
def mark(x,y,z):
sphere = vtk.vtkSphereSource()
sphere.SetRadius(1)
res = 20
sphere.SetThetaResolution(res)
sphere.SetPhiResolution(res)
sphere.SetCenter(x,y,z)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(sphere.GetOutput())
marker = vtk.vtkActor()
marker.SetMapper(mapper)
renderer.AddActor(marker)
marker.GetProperty().SetColor( (1,0,0) )
interactor.Render()
def pick_cell(renwinInteractor, event):
x, y = renwinInteractor.GetEventPosition()
picker = vtk.vtkCellPicker()
picker.PickFromListOn()
picker.AddPickList(actor)
picker.SetTolerance(0.01)
picker.Pick(x, y, 0, renderer)
points = picker.GetPickedPositions()
numPoints = points.GetNumberOfPoints()
if numPoints<1: return
pnt = points.GetPoint(0)
mark(*pnt)
interactor.AddObserver('LeftButtonPressEvent', pick_cell)
interactor.Initialize()
interactor.Start()
More information about the vtkusers
mailing list