Skip to content

Instantly share code, notes, and snippets.

@dennda
Created February 4, 2011 16:49
Show Gist options
  • Save dennda/811351 to your computer and use it in GitHub Desktop.
Save dennda/811351 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Import VTK with special EmbedOpenGL renderer
import os
os.environ['VTK_RENDERER'] = 'EmbedOpenGL'
import vtk
import time
from pymt import *
from OpenGL.GL import *
reader = vtk.vtkDICOMImageReader()
reader.SetDirectoryName('MacJob')
reader.Update()
table = vtk.vtkLookupTable()
table.SetRange(0, 2000) # image intensity range
table.SetValueRange(0.0, 1.0) # from black to white
table.SetSaturationRange(0.0, 0.0) # no color saturation
table.SetRampToLinear()
table.Build()
# Map the image through the lookup table
color = vtk.vtkImageMapToColors()
color.SetLookupTable(table)
color.SetInputConnection(reader.GetOutputPort())
# Display the image
actor = vtk.vtkImageActor()
actor.SetInput(color.GetOutput())
ren1 = vtk.vtkRenderer()
ren1.AddActor(actor)
class VTKWidget(MTWidget):
def __init__(self, renderer, **kwargs):
super(VTKWidget, self).__init__(**kwargs)
self.renderer = renderer
self.vtkwin = vtk.vtkRenderWindow()
self.vtkwin.SetSize(self.width, self.height)
self.vtkwin.AddRenderer(self.renderer)
self.fbo = Fbo(size=self.size, push_viewport=True)
def draw(self):
try:
self._draw()
except Exception, e:
pymt_logger.exception('Exception in vtk draw')
print e
drawTexturedRectangle(self.fbo.texture, size=self.size)
def _draw(self):
# draw view
set_color(1, 1, 1)
self.fbo.bind()
glPushAttrib(GL_ALL_ATTRIB_BITS)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
self.vtkwin.Render()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glPopAttrib()
self.fbo.release()
win = MTWindow(fullscreen=False)
vtkwidget = VTKWidget(ren1, size=(256, 256))
scatter = MTScatterWidget(size=vtkwidget.size)
scatter.add_widget(vtkwidget)
win.add_widget(scatter)
runTouchApp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment