Created
August 21, 2013 18:22
-
-
Save cpatrick/6298081 to your computer and use it in GitHub Desktop.
VTK Web Ray Cast Try
This file contains hidden or 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
r""" | |
This module is a VTK Web server application. | |
The following command line illustrate how to use it:: | |
$ vtkpython .../vtk_web_cone.py | |
Any VTK Web executable script come with a set of standard arguments that | |
can be overriden if need be:: | |
--port 8080 | |
Port number on which the HTTP server will listen to. | |
--content /path-to-web-content/ | |
Directory that you want to server as static web content. | |
By default, this variable is empty which mean that we rely on another server | |
to deliver the static content and the current process only focus on the | |
WebSocket connectivity of clients. | |
--authKey vtk-secret | |
Secret key that should be provided by the client to allow it to make any | |
WebSocket communication. The client will assume if none is given that the | |
server expect "vtk-secret" as secret key. | |
""" | |
# import to process args | |
import sys | |
import os | |
# import vtk modules. | |
import vtk | |
from vtk.web import server, wamp, protocols | |
# import annotations | |
from autobahn.wamp import exportRpc | |
try: | |
import argparse | |
except ImportError: | |
# since Python 2.6 and earlier don't have argparse, we simply provide | |
# the source for the same as _argparse and we use it instead. | |
import _argparse as argparse | |
# ============================================================================= | |
# Create custom File Opener class to handle clients requests | |
# ============================================================================= | |
class _WebRayCast(wamp.ServerProtocol): | |
# Application configuration | |
view = None | |
authKey = "vtkweb-secret" | |
def initialize(self): | |
global ren, renWin, iren, volume, volumeMapper | |
# Bring used components | |
self.registerVtkWebProtocol(protocols.vtkWebMouseHandler()) | |
self.registerVtkWebProtocol(protocols.vtkWebViewPort()) | |
self.registerVtkWebProtocol(protocols.vtkWebViewPortImageDelivery()) | |
self.registerVtkWebProtocol(protocols.vtkWebViewPortGeometryDelivery()) | |
# Update authentication key to use | |
self.updateSecret(_WebRayCast.authKey) | |
# Create default pipeline (Only once for all the session) | |
if not _WebRayCast.view: | |
# VTK specific code | |
# Create the standard renderer, render window and interactor | |
ren = vtk.vtkRenderer() | |
renWin = vtk.vtkRenderWindow() | |
renWin.AddRenderer(ren) | |
iren = vtk.vtkRenderWindowInteractor() | |
iren.SetRenderWindow(renWin) | |
# Create the reader for the data | |
reader = vtk.vtkMetaImageReader() | |
fname = '/Users/cpatrick/Projects/Kitware/MidasGeneral/Normal049-T1-MPRage.mha' | |
reader.SetFileName(fname) | |
# Create transfer mapping scalar value to opacity | |
opacityTransferFunction = vtk.vtkPiecewiseFunction() | |
opacityTransferFunction.AddPoint(20, 0.0) | |
opacityTransferFunction.AddPoint(255, 0.2) | |
# Create transfer mapping scalar value to color | |
colorTransferFunction = vtk.vtkColorTransferFunction() | |
colorTransferFunction.AddRGBPoint(0.0, 0.0, 0.0, 0.0) | |
colorTransferFunction.AddRGBPoint(64.0, 1.0, 0.0, 0.0) | |
colorTransferFunction.AddRGBPoint(128.0, 0.0, 0.0, 1.0) | |
colorTransferFunction.AddRGBPoint(192.0, 0.0, 1.0, 0.0) | |
colorTransferFunction.AddRGBPoint(255.0, 0.0, 0.2, 0.0) | |
# The property describes how the data will look | |
volumeProperty = vtk.vtkVolumeProperty() | |
volumeProperty.SetColor(colorTransferFunction) | |
volumeProperty.SetScalarOpacity(opacityTransferFunction) | |
volumeProperty.ShadeOn() | |
volumeProperty.SetInterpolationTypeToLinear() | |
# The mapper / ray cast function know how to render the data | |
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction() | |
volumeMapper = vtk.vtkVolumeRayCastMapper() | |
volumeMapper.SetVolumeRayCastFunction(compositeFunction) | |
volumeMapper.SetInputConnection(reader.GetOutputPort()) | |
# The volume holds the mapper and the property and | |
# can be used to position/orient the volume | |
volume = vtk.vtkVolume() | |
volume.SetMapper(volumeMapper) | |
volume.SetProperty(volumeProperty) | |
ren.AddVolume(volume) | |
ren.SetBackground(1, 1, 1) | |
renWin.SetSize(600, 600) | |
renWin.Render() | |
iren.Initialize() | |
renWin.Render() | |
iren.Start() | |
# VTK Web application specific | |
_WebRayCast.view = renWin | |
self.Application.GetObjectIdMap().SetActiveObject("VIEW", renWin) | |
# ============================================================================= | |
# Main: Parse args and start server | |
# ============================================================================= | |
if __name__ == "__main__": | |
# Create argument parser | |
parser = argparse.ArgumentParser(description="VTK/Web Ray Cast web-application") | |
# Add default arguments | |
server.add_arguments(parser) | |
# Exctract arguments | |
args = parser.parse_args() | |
# Configure our current application | |
_WebRayCast.authKey = args.authKey | |
# Start server | |
server.start_webserver(options=args, protocol=_WebRayCast) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment