Skip to content

Instantly share code, notes, and snippets.

@shakthimaan
Last active Sep 25, 2015
Embed
What would you like to do?
Add points and lines using Python VTK callback function with timer
#!/usr/bin/env python
# TODO:
# - Starting point!
# - Use Queue with multi-threading
import vtk
from datetime import datetime
import paho.mqtt.client as mqtt
class vtkTimerCallback():
def __init__(self, pts, lines):
self.timer_count = 0
self.pts = pts
self.lines = lines
self.p2 = [] # Need to lock?
self.changed = False
self.client = mqtt.Client()
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.connect_async("localhost", 1883, 60)
def on_connect(self, client, userdata, flags, rc):
print("MQTT connected with result code "+str(rc))
client.subscribe("hello/world")
def on_message(self, client, userdata, msg):
t = datetime.utcnow().isoformat()
data = msg.payload.split(' ')
x = float(data[0])
y = float(data[1])
z = float(data[2])
self.p2 = [x, y, z]
self.changed = True
def execute(self, obj, event):
self.actor.GetProperty().SetColor(1,1,0)
self.changed = False
self.client.loop_start()
self.client.loop_stop()
if self.changed == True and self.p2:
print self.changed, self.p2
m = self.actor.GetMapper()
linesPolyData = m.GetInput()
l = linesPolyData.GetLines()
number_of_lines = int(l.GetNumberOfCells())
self.pts.InsertNextPoint(self.p2)
print self.p2, number_of_lines
line2 = vtk.vtkLine()
line2.GetPointIds().SetId(0, number_of_lines)
line2.GetPointIds().SetId(1, number_of_lines+1)
self.lines.InsertNextCell(line2)
linesPolyData.SetPoints(self.pts)
linesPolyData.SetLines(self.lines)
else:
print "Else"
self.actor.GetProperty().SetColor(0,1,1)
iren = obj
iren.GetRenderWindow().Render()
self.timer_count += 1
def main():
origin = [0.0, 0.0, 0.0]
p0 = [1.0, 0.0, 0.0]
p1 = [0.0, 1.0, 0.0]
pts = vtk.vtkPoints()
pts.InsertNextPoint(origin)
pts.InsertNextPoint(p0)
pts.InsertNextPoint(p1)
line0 = vtk.vtkLine()
line0.GetPointIds().SetId(0,0) # the second 0 is the index of the Origin in the vtkPoints
line0.GetPointIds().SetId(1,1) # the second 1 is the index of P0 in the vtkPoints
line1 = vtk.vtkLine()
line1.GetPointIds().SetId(0,0) # the second 0 is the index of the Origin in the vtkPoints
line1.GetPointIds().SetId(1,2) # 2 is the index of P1 in the vtkPoints
lines = vtk.vtkCellArray()
lines.InsertNextCell(line0)
lines.InsertNextCell(line1)
linesPolyData = vtk.vtkPolyData()
linesPolyData.SetPoints(pts)
linesPolyData.SetLines(lines)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(linesPolyData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderWindow.Render()
cb = vtkTimerCallback(pts, lines)
cb.actor = actor
renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
timerId = renderWindowInteractor.CreateRepeatingTimer(2000);
renderWindowInteractor.Start()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment