[vtkusers] cleaning up a surface rendering

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Mar 25 15:40:05 EST 2004


>>>>> "Amy" == Amy Henderson <amy.henderson at kitware.com> writes:

    Amy> Hi John, If you turn on ColorRegions in the
    Amy> vtkConnectivityFilter, then the filter adds a point data
    Amy> scalar array containing the region ids.

Sorry to be dense, but I am still plagued by two problems:

 * It is not clear to me how I can retrieve the id for the region that
   is closest to the x,y,z world coords set by
   connect.SetClosestPoint.  I need this so I can call
   DeleteSpecifiedRegion.  Even if I use a cell picker and get the
   cellid, this is not the same as the region id.  


 * I am having some trouble with my picking.  I am currently doing

    x, y = interactor.GetEventPosition()

    picker = vtk.vtkCellPicker()
    picker.Pick(x, y, 0, renderer)

   Is 0 correct for the display z value?  If I add a sphere marker at
   the x,y,z coord returned by picker.GetPickPosition(), it is not
   coincident with any of the regions (I'm displaying
   SetExtractionModeToAllRegions).  This surprises me because I
   assumed that the cell picker would return the location of a cell in
   the dataset and hence the sphere should overlap one of those
   regions.


I'll post a complete example below.  Let me know if you have any more
advice for me.

Thanks!
John Hunter

#!/usr/local/bin/python
import os

import pygtk
pygtk.require('2.0')

import gtk
import vtk
from random import random
from vtk.gtk.GtkGLExtVTKRenderWindow import GtkGLExtVTKRenderWindow
from vtk.gtk.GtkGLExtVTKRenderWindowInteractor import GtkGLExtVTKRenderWindowInteractor


window = gtk.Window()
window.set_title("A GtkGLExtVTKRenderWindow Demo!")
window.connect("destroy", gtk.mainquit)
window.connect("delete_event", gtk.mainquit)
window.set_border_width(10)
winwidth, winheight = 600,600
window.set_size_request(winwidth, winheight)

renwinInteractor = GtkGLExtVTKRenderWindowInteractor()        
renwinInteractor.set_size_request(winwidth, winheight)
renwinInteractor.show()
renderer = vtk.vtkRenderer()
renWin = renwinInteractor.GetRenderWindow()
renWin.AddRenderer(renderer)
interactor = renWin.GetInteractor()

vbox = gtk.VBox(spacing=3)
vbox.show()
vbox.pack_start(renwinInteractor, gtk.TRUE, gtk.TRUE)

button = gtk.Button('Quit')
button.show()
button.connect('clicked', gtk.mainquit)
vbox.pack_start(button, gtk.FALSE, gtk.FALSE)
window.add(vbox)


reader = vtk.vtkImageReader2()
reader.SetDataScalarTypeToUnsignedShort()
reader.SetDataByteOrderToLittleEndian()
reader.SetFileNameSliceOffset(120)
reader.SetDataExtent(0, 511, 0, 511, 0, 106)
reader.SetFilePrefix('/home/jdhunter/seizure/data/ThompsonK/CT/raw/1.2.840.113619.2.55.1.1762864819.1957.1074338393.')
reader.SetFilePattern( '%s%d.raw')
reader.SetDataSpacing(25.0/512, 25.0/512, 0.125 )

reader.Update()

iso = vtk.vtkMarchingCubes()
iso.SetInput(reader.GetOutput())
iso.SetValue(0,3000)

connect = vtk.vtkPolyDataConnectivityFilter()
connect.SetInput( iso.GetOutput())
connect.SetExtractionModeToAllRegions()
#connect.SetExtractionModeToLargestRegion()
#connect.SetExtractionModeToSpecifiedRegions ()
connect.ColorRegionsOn ()

isoMapper = vtk.vtkPolyDataMapper()
isoMapper.SetInput(connect.GetOutput())
isoMapper.ScalarVisibilityOff()

isoActor = vtk.vtkActor()
isoActor.SetMapper(isoMapper)
isoActor.GetProperty().SetColor(0.9804,0.9216,0.8431)

# Add the actors to the renderer, set the background and size
renderer.AddActor(isoActor)
renderer.SetBackground(0.2,0.3,0.4)

def press_left(interactor, event):

    x, y = interactor.GetEventPosition()

    picker = vtk.vtkCellPicker()
    picker.Pick(x, y, 0, renderer)

    print picker.GetCellId(), picker.GetSubId()

    connect.SetClosestPoint(picker.GetPickPosition())
    # now I just need to get the id of the region closest point and
    # then call
    #connect.DeleteSpecifiedRegion (cid)

    # add a sphere marker to verify pick location; why is this not
    # coincident with a region?
    sphere = vtk.vtkSphereSource()
    sphere.SetRadius(0.1)
    res = 20
    sphere.SetThetaResolution(res)
    sphere.SetPhiResolution(res)
    sphere.SetCenter(picker.GetPickPosition())
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInput(sphere.GetOutput())

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    renderer.AddActor(actor)
    actor.GetProperty().SetColor( (1,0,0) )

    interactor.Render()

renwinInteractor.AddObserver('LeftButtonPressEvent', press_left)


# show the main window and start event processing.
window.show()
gtk.mainloop()





More information about the vtkusers mailing list