[vtkusers] Contour, ContourWidget and triangles filler

Cory Quammen cory.quammen at kitware.com
Thu Jul 16 10:41:10 EDT 2015

Hi Oleg,

Welcome to VTK!

Thanks for providing the code sample. You must be using VTK 5-something? In
6 and above, your SetInput() calls need to be changed to SetInputData().
Also, as a general note about using VTK, you can use


to create pipelines rather than pass vtkPolyData objects around. This is
usually preferable as any changes to your upstream algorithms will
propagate down to the renderer automatically. That might save you some code.

Otherwise your example is pretty nice and compact in my opinion.

To update the surface triangles, you need to set up an observer on the
contourWidget's InteractionEvent. You can do this with

contourWidget.AddObserver('InteractionEvent', my_callback)

where my_callback is defined with

def my_callback(contourWidget, unused):
    global contourWidget, mapper

    cd =

    td = gen_triangles(cd)

Note that I've made the mapper global so you can access it in the callback.
The first argument is the contour widget and the second is just the event
type, which you don't need in this callback.

By the way, you will likely get better results for the surface if you use
the following definition of gen_triangles:

def gen_triangles(cd):
    clsdPoly = vtk.vtkPolyData()

    numPts = cd.GetNumberOfPoints()
    idList = vtk.vtkIdList()
    [idList.SetId(i, i) for i in xrange(numPts)]

    clsdPoly.InsertNextCell(vtk.VTK_POLYGON, idList)

    triangles = vtk.vtkTriangleFilter()

    return triangles.GetOutput()

This creates a VTK polygon cell which works better with the
vtkTriangleFilter than the line cells from the contour widget

Hope that helps,

On Wed, Jul 15, 2015 at 1:01 AM, Oleg Krivosheev <
oleg.krivosheev at xcision.com> wrote:

> Hi, All
> what I'm trying to do is to have editable contour in vtkContourWidget,
> with internal
> area filled with somewhat transparent color. What I managed to do is to
> make contour,
> put it into a widget, generate triangles strip from the contour via
> vtkTriangleFilter and
> draw it all together, Python code at the link
> http://codepad.org/VPOa13St
> Now questions. Is it a good way to proceed? Looks very cumbersome to me,
> but I'm new to VTK.
> Another big question is how to connect contour edit action (say, moving or
> adding point)
> with triangle strip update. Right now as soon as I move point, contour is
> updated
> but triangles filler is not
> Any help is greatly appreciated
> regards
> OK
> _______________________________________________
> Powered by www.kitware.com
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Search the list archives at: http://markmail.org/search/?q=vtkusers
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers

Cory Quammen
R&D Engineer
Kitware, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150716/8c2f11df/attachment.html>

More information about the vtkusers mailing list