[vtkusers] deleting a face from polydata

John Hunter jdhunter at ace.bsd.uchicago.edu
Wed Apr 7 16:32:48 EDT 2004


I am interested in removing some data from my scene by clicking on it
with a mouse.  I've returned to my trusty sphere as a demo case.  I
use a cell picker to get the cell id.  When I call
polyData.DeleteCell(cellId) on the poly data, I would like that cell
to be removed from my scene

I initialize my scene with 

    sphere = vtk.vtkSphereSource()
    sphere.SetRadius(20)
    res = 5
    sphere.SetThetaResolution(res)
    sphere.SetPhiResolution(res)
    sphere.SetCenter(0,0,0)
    polyData = sphere.GetOutput()
    polyData.BuildCells()
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInput(polyData)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    renderer.AddActor(actor)

and this is my pick function

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)
    cellId = picker.GetCellId()
    if cellId==-1:
        print 'swing and a miss'
        return

    polyData.DeleteCell(cellId)
    print 'deleted cell', cellId
    mapper.Update()

I am pretty sure I am selecting the right cell, because I can insert a
marker on each of the cell points and correctly mark the vertices
under the triangle that I click on.   If I use cell =
polyData.GetCell(cellId) the type is identified as vtkTriangle.  If I
successively click on the same point, I never get the same cell id
twice, suggesting the cells are being removed from the poly data.  But
they are not disappearing from the rendered scene.

Any suggestions?  Complete example below.

John Hunter

#!/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 = 5
sphere.SetThetaResolution(res)
sphere.SetPhiResolution(res)
sphere.SetCenter(0,0,0)
polyData = sphere.GetOutput()
polyData.BuildCells()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(polyData)

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)
    cellId = picker.GetCellId()
    if cellId==-1:
        print 'swing and a miss'
        return

    polyData.DeleteCell(cellId)
    print 'deleted cell', cellId
    mapper.Update()


interactor.AddObserver('LeftButtonPressEvent', pick_cell)


interactor.Initialize()
interactor.Start()





More information about the vtkusers mailing list