[vtkusers] Looking for an incremental plot in VTK

Eric E. Monson emonson at cs.duke.edu
Fri Sep 9 10:35:33 EDT 2011


Hello Gerald,

There is nothing in VTK that's built specifically to act like an oscilloscope. I think the vtkChartXY is going to be your best bet. 

The Charts classes have been designed for fast drawing of large data sets, so I don't think the actual rendering time for the plot should be an issue. The problem (as you've already discovered) is the same with any VTK rendering: with the standard way all the examples are written you can't do a bunch of calculations while still interacting with the visualization smoothly. The only way I know how to deal with this is to put the update of your data structures in another thread, while keeping the view rendering in the main thread. I'm really just a beginner at this, so others could give you more reliable advice, but you should be okay with this strategy as long as you lock things correctly so only one thread at a time is accessing the data structures.

Here's an example I was playing with. It generates some random amplitudes and plots them as they're randomly being modulated (and colors them by their amplitude with the lookup table). The visual update looks quick, and I'm able to interact with it very smoothly while the data updates are occurring.

It's not embedded in Qt, and it's in Python, so you'll have to translate it to equivalent ways of doing things in C++, but maybe this will give you some ideas. If you try things out and have specific questions about how to accomplish certain parts, write back to the mailing list and hopefully someone can help you get past specific roadblocks.

Good luck,
-Eric

#!/usr/bin/env python
"""A simple example of an animated chart where the data
update occurs in a separate thread from the view update
"""

import numpy as N
import vtk
import vtk.util.numpy_support as VN
import threading
import time

class upThread(threading.Thread):

    def __init__(self, threadID):
        self.threadID = threadID
        threading.Thread.__init__(self)

    def run(self):
        global table, amp
        global update_on

        while (update_on):
            # print 'Maths Update'
            time.sleep(0.001)
            
            # Generate new positions            
            threadLock.acquire()
            amp += 0.02*N.random.randn(num_pts)
            table.Modified()
            threadLock.release()
            
# main program 
# =============

dt = 30 # ms
num_pts = 500

# Set up a 2D scene, add an XY chart to it
view = vtk.vtkContextView()
view.GetRenderer().SetBackground(1.0, 1.0, 1.0)
view.GetRenderWindow().SetSize(400, 300)

chart = vtk.vtkChartXY()
view.GetScene().AddItem(chart)

amp = N.random.randn(num_pts)
amp_vtk = VN.numpy_to_vtk(amp)
amp_vtk.SetName('Amplitude')

X_vtk = VN.numpy_to_vtk(N.arange(num_pts, dtype='f'), deep=True)
X_vtk.SetName('X')

table = vtk.vtkTable()
table.AddColumn(X_vtk)
table.AddColumn(amp_vtk)

# Generate a black-to-red lookup table with fixed alpha
lut = vtk.vtkLookupTable()
lut.SetValueRange(0.2, 1.0)
lut.SetSaturationRange(1, 1)
lut.SetHueRange(0.6667, 0.6667)
lut.SetRampToLinear()
lut.SetRange(-1,1)
lut.SetAlpha(0.75)
lut.Build()

# Add multiple line plots, setting the colors etc
line0 = chart.AddPlot(vtk.vtkChart.LINE)
line0.SetInput(table, 0, 1)
line0.SetColor(100, 100, 100, 255)
line0.SetWidth(2.0)
line0.GetPen().SetLineType(vtk.vtkPen.SOLID_LINE)
line0.SetMarkerStyle(vtk.vtkPlotPoints.CIRCLE)
line0.SetScalarVisibility(1)
line0.SetLookupTable(lut)
line0.SelectColorArray(1)

view.Render()

update_on = True
threadLock = threading.Lock()
up_thread = upThread(0)
up_thread.start()

def animate(obj=None, event=None):
    # print 'Screen Update'
    time.sleep(0.003)

    threadLock.acquire()
    view.Render()
    threadLock.release()

iren = view.GetInteractor()
iren.AddObserver("TimerEvent", animate)

timer_id = iren.CreateRepeatingTimer(dt)

iren.Start()
update_on = False
iren.DestroyTimer(timer_id)

up_thread.join()


On Sep 7, 2011, at 8:26 AM, gdherbom wrote:

> Hi, 
> 
> I come back with my question, does someone know a VTK class that can be used
> as an oscilloscope? 
> 
> Thanks, 
> 
> Gerald
> 
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Looking-for-an-incremental-plot-in-VTK-tp4647485p4778341.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> 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
> 
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list