[vtkusers] Update ChartXY Data and re-render in real-time

Eric E. Monson emonson at cs.duke.edu
Tue Jan 17 12:08:15 EST 2012


Hey Andy,

Since nobody else seems to be jumping forward with code, I'll pass along something that I happen to have around, but isn't exactly what you're looking for. This is a piece of Python code where I was playing with adapting someone else's threaded VTK animation example to use with the Charts. Unfortunately, I don't know a lot about threading in C++ or Python, but this might at least get you started.

The basic idea is to set up a timer on the interactor which will render the view at set intervals, and in the background thread there is a loop running which updates the data behind the chart. Then there is a global thread lock which each piece has to acquire so you're not rendering or updating at the same time.

Of course, people with more experience should chime in with corrections or better examples, but this at least shows it's possible to update the chart while you're interacting with it.

Good luck,
-Eric

· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
Eric E Monson
Duke Visualization Technology Group


# ===========================
#!/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 Jan 12, 2012, at 6:02 AM, ajp wrote:

> Hi,
> 
> I'm teaching myself to use vtk plotting within a small c++ code by using:
> 
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/Plotting/LinePlot
> 
> Everything works fine.  However, I would like to have control returned to
> the main code after start() is called, rather than not returning until I
> kill the window, and then after start() add some code to modify, say, the
> cosine curve and see it automatically update the chart (after calling
> something appropriate in the vtk api).
> 
> I understand I need to get to grips with event loops, probably need to use
> multi-threading etc, that's all fine, just need a few pointers to get
> started....
> 
> As a concrete example could somebody post some code that modifies the above
> example and add some suitable code after start() so that the cosine for
> example moves and I see it updated in the chart?  I still would not want the
> program to not exit until I close the render window.
> 
> That would be really helpful,
> 
> Many thanks,
> Andy
> 
> 
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Update-ChartXY-Data-and-re-render-in-real-time-tp5139657p5139657.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