Created
September 25, 2015 18:30
-
-
Save shakthimaan/d8e4f51f3d074926e244 to your computer and use it in GitHub Desktop.
Force render appended lines?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import vtk | |
import Queue | |
from multiprocessing import Process, Queue | |
from datetime import datetime | |
import paho.mqtt.client as mqtt | |
def on_connect(client, userdata, flags, rc): | |
print("MQTT connected with result code "+str(rc)) | |
client.subscribe("hello/world") | |
def on_message(client, userdata, msg): | |
data = msg.payload.split(' ') | |
x = float(data[0]) | |
y = float(data[1]) | |
z = float(data[2]) | |
point = [x, y, z] | |
q.put(point) | |
def mqtt_client(q): | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect("localhost", 1883, 60) | |
print "Started mqtt client ..." | |
client.loop_forever() | |
class vtkTimerCallback(): | |
def __init__(self, q, pts, lines): | |
self.q = q | |
self.timer_count = 0 | |
self.pts = pts | |
self.lines = lines | |
def execute(self, obj, event): | |
if not self.q.empty(): | |
item = self.q.get() | |
m = self.actor.GetMapper() | |
linesPolyData = m.GetInput() | |
l = linesPolyData.GetLines() | |
number_of_lines = int(l.GetNumberOfCells()) | |
self.pts.InsertNextPoint(item) | |
print item, number_of_lines, self.q.qsize() | |
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 "No data" | |
if self.timer_count % 1 == 0: | |
self.actor.GetProperty().SetColor(0,1,1) | |
else: | |
self.actor.GetPropert().SetColor(1,0,1) | |
iren = obj | |
iren.GetRenderWindow().Render() | |
self.timer_count += 1 | |
def vtk_main(q): | |
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(q, pts, lines) | |
cb.actor = actor | |
renderWindowInteractor.AddObserver('TimerEvent', cb.execute) | |
timerId = renderWindowInteractor.CreateRepeatingTimer(3000); | |
renderWindowInteractor.Start() | |
if __name__ == '__main__': | |
q = Queue() | |
mqtt_client = Process(name='mqtt', target=mqtt_client, args=(q,)) | |
vtk_client = Process(name='vtk', target=vtk_main, args=(q,)) | |
mqtt_client.start() | |
vtk_client.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment