Commit ea628ef7 authored by Prabhu Ramachandran's avatar Prabhu Ramachandran
Browse files

NEW: Added useful utility code from VTK 3.2. Updated and tested for

VTK 4.x.  This code was originally written by David Gobbi.
parent 7cc3d2ae
"""
Miscellaneous constants copied from vtkSystemIncludes.h
(It would be nice if this file were automatically generated,
but the constants don't change very often)
"""
# Some constants used throughout code
VTK_LARGE_FLOAT = 1.0e+38
VTK_LARGE_INTEGER = 2147483647 # 2^31 - 1
# These types are returned by GetDataType to indicate pixel type.
VTK_VOID = 0
VTK_BIT = 1
VTK_CHAR = 2
VTK_UNSIGNED_CHAR = 3
VTK_SHORT = 4
VTK_UNSIGNED_SHORT = 5
VTK_INT = 6
VTK_UNSIGNED_INT = 7
VTK_LONG = 8
VTK_UNSIGNED_LONG = 9
VTK_FLOAT =10
VTK_DOUBLE =11
VTK_ID_TYPE =12
# These types are not currently supported by GetDataType, but are
# for completeness.
VTK_STRING =13
VTK_OPAQUE =14
# Some constant required for correct template performance
VTK_BIT_MIN = 0
VTK_BIT_MAX = 1
VTK_CHAR_MIN = -128
VTK_CHAR_MAX = 127
VTK_UNSIGNED_CHAR_MIN = 0
VTK_UNSIGNED_CHAR_MAX = 255
VTK_SHORT_MIN = -32768
VTK_SHORT_MAX = 32767
VTK_UNSIGNED_SHORT_MIN = 0
VTK_UNSIGNED_SHORT_MAX = 65535
VTK_INT_MIN = (-VTK_LARGE_INTEGER-1)
VTK_INT_MAX = VTK_LARGE_INTEGER
#VTK_UNSIGNED_INT_MIN = 0
#VTK_UNSIGNED_INT_MAX = 4294967295
VTK_LONG_MIN = (-VTK_LARGE_INTEGER-1)
VTK_LONG_MAX = VTK_LARGE_INTEGER
#VTK_UNSIGNED_LONG_MIN = 0
#VTK_UNSIGNED_LONG_MAX = 4294967295
VTK_FLOAT_MIN = -VTK_LARGE_FLOAT
VTK_FLOAT_MAX = VTK_LARGE_FLOAT
VTK_DOUBLE_MIN = -1.0e+99
VTK_DOUBLE_MAX = 1.0e+99
# These types are returned to distinguish dataset types
VTK_POLY_DATA = 0
VTK_STRUCTURED_POINTS = 1
VTK_STRUCTURED_GRID = 2
VTK_RECTILINEAR_GRID = 3
VTK_UNSTRUCTURED_GRID = 4
VTK_PIECEWISE_FUNCTION = 5
VTK_IMAGE_DATA = 6
VTK_DATA_OBJECT = 7
VTK_DATA_SET = 8
VTK_POINT_SET = 9
# These types define error codes for vtk functions
VTK_OK = 1
VTK_ERROR = 2
# These types define different text properties
VTK_ARIAL = 0
VTK_COURIER = 1
VTK_TIMES = 2
VTK_TEXT_LEFT = 0
VTK_TEXT_CENTERED = 1
VTK_TEXT_RIGHT = 2
VTK_TEXT_BOTTOM = 0
VTK_TEXT_TOP = 2
VTK_TEXT_GLOBAL_ANTIALIASING_SOME = 0
VTK_TEXT_GLOBAL_ANTIALIASING_NONE = 1
VTK_TEXT_GLOBAL_ANTIALIASING_ALL = 2
VTK_LUMINANCE = 1
VTK_LUMINANCE_ALPHA = 2
VTK_RGB = 3
VTK_RGBA = 4
VTK_COLOR_MODE_DEFAULT = 0
VTK_COLOR_MODE_MAP_SCALARS = 1
# Constants for InterpolationType
VTK_NEAREST_INTERPOLATION = 0
VTK_LINEAR_INTERPOLATION = 1
# For volume rendering
VTK_MAX_VRCOMP = 4
# A macro to get the name of a type
__vtkTypeNameDict = {VTK_VOID:"void",
VTK_DOUBLE:"double",
VTK_FLOAT:"float",
VTK_LONG:"long",
VTK_UNSIGNED_LONG:"unsigned long",
VTK_INT:"int",
VTK_UNSIGNED_INT:"unsigned int",
VTK_SHORT:"short",
VTK_UNSIGNED_SHORT:"unsigned short",
VTK_CHAR:"char",
VTK_UNSIGNED_CHAR:"unsigned char",
VTK_BIT:"bit"}
def vtkImageScalarTypeNameMacro(type):
return __vtkTypeNameDict[type]
"""
vtkImageExportToArray: a NumPy front-end to vtkImageExport
This class converts a VTK image to a Numeric Python array.
To use this class, you must have the LLNL Numeric Python distribution
(http://numpy.sf.net)
Methods:
SetInput(input) -- connect to VTK image pipeline
GetArray() -- execute pipeline and return a Numeric array
Convert VTK_UNSIGNED_SHORT to python Int:
(this might be necessary because Python doesn't support unsigned short,
the default is to cast unsigned short to signed short).
SetConvertUnsignedShortToInt(yesno)
ConvertUnsignedShortToIntOn()
ConvertUnsignedShortToIntOff()
From vtkImageExport:
GetDataExtent()
GetDataSpacing()
GetDataOrigin()
"""
import Numeric
import umath
from vtk import vtkImageExport
from vtkConstants import *
class vtkImageExportToArray:
def __init__(self):
self.__export = vtkImageExport()
self.__ConvertUnsignedShortToInt = 0
# type dictionary: note that python doesn't support
# unsigned integers!
__typeDict = { VTK_CHAR:Numeric.Int8,
VTK_UNSIGNED_CHAR:Numeric.UnsignedInt8,
VTK_SHORT:Numeric.Int16,
VTK_UNSIGNED_SHORT:Numeric.Int16,
VTK_INT:Numeric.Int32,
VTK_FLOAT:Numeric.Float32,
VTK_DOUBLE:Numeric.Float64 }
__sizeDict = { VTK_CHAR:1,
VTK_UNSIGNED_CHAR:1,
VTK_SHORT:2,
VTK_UNSIGNED_SHORT:2,
VTK_INT:4,
VTK_FLOAT:4,
VTK_DOUBLE:8 }
# convert unsigned shorts to ints, to avoid sign problems
def SetConvertUnsignedShortToInt(self,yesno):
self.__ConvertUnsignedShortToInt = yesno
def GetConvertUnsignedShortToInt(self):
return self.__ConvertUnsignedShortToInt
def ConvertUnsignedShortToIntOn(self):
self.__ConvertUnsignedShortToInt = 1
def ConvertUnsignedShortToIntOff(self):
self.__ConvertUnsignedShortToInt = 0
# set the input
def SetInput(self,input):
return self.__export.SetInput(input)
def GetInput(self):
return self.__export.GetInput()
def GetArray(self):
input = self.__export.GetInput()
type = input.GetScalarType()
extent = input.GetWholeExtent()
numComponents = input.GetNumberOfScalarComponents()
dim = (extent[5]-extent[4]+1,
extent[3]-extent[2]+1,
extent[1]-extent[0]+1)
if (numComponents > 1):
dim = dim + (numComponents,)
size = dim[0]*dim[1]*dim[2]*numComponents*self.__sizeDict[type]
imString = Numeric.zeros((size,),Numeric.UnsignedInt8).tostring()
self.__export.Export(imString)
imArray = Numeric.fromstring(imString,self.__typeDict[type])
imArray = Numeric.reshape(imArray,dim)
# convert unsigned short to int to avoid sign issues
if (type == VTK_UNSIGNED_SHORT and self.__ConvertUnsignedShortToInt):
imArray = umath.bitwise_and(imArray.astype(Numeric.Int32),0xffff)
# just to remind myself of the dangers of memory management
del imString
return imArray
def GetDataExtent(self):
return self.__export.GetInput().GetWholeExtent()
def GetDataSpacing(self):
return self.__export.GetInput().GetSpacing()
def GetDataOrigin(self):
return self.__export.GetInput().GetOrigin()
"""
vtkImageImportFromArray: a NumPy front-end to vtkImageImport
Load a Numeric Python array into a VTK image.
To use this class, you must have the LLNL Numeric Python distribution
(http://numpy.sf.net)
Methods:
GetOutput() -- connect to VTK image pipeline
SetArray() -- set the array to load in
Convert python 'Int' to VTK_UNSIGNED_SHORT:
(python doesn't support unsigned short, so this might be necessary)
SetConvertIntToUnsignedShort(yesno)
ConvertIntToUnsignedShortOn()
ConvertIntToUnsignedShortOff()
Methods from vtkImageImport:
(if you don't set these, sensible defaults will be used)
SetDataExtent()
SetDataSpacing()
SetDataOrigin()
"""
import Numeric
from vtk import vtkImageImport
from vtkConstants import *
class vtkImageImportFromArray:
def __init__(self):
self.__import = vtkImageImport()
self.__ConvertIntToUnsignedShort = 0
self.__Array = None
# type dictionary: note that python doesn't support
# unsigned integers properly!
__typeDict = {'c':VTK_UNSIGNED_CHAR,
'b':VTK_UNSIGNED_CHAR,
'1':VTK_CHAR,
's':VTK_SHORT,
'i':VTK_INT,
'l':VTK_LONG,
'f':VTK_FLOAT,
'd':VTK_DOUBLE,
'F':VTK_FLOAT,
'D':VTK_DOUBLE }
# convert 'Int32' to 'unsigned short'
def SetConvertIntToUnsignedShort(self,yesno):
self.__ConvertIntToUnsignedShort = yesno
def GetConvertIntToUnsignedShort(self):
return self.__ConvertIntToUnsignedShort
def ConvertIntToUnsignedShortOn(self):
self.__ConvertIntToUnsignedShort = 1
def ConvertIntToUnsignedShortOff(self):
self.__ConvertIntToUnsignedShort = 0
# get the output
def GetOutput(self):
return self.__import.GetOutput()
# import an array
def SetArray(self,imArray):
self.__Array = imArray
imString = imArray.tostring()
numComponents = 1
dim = imArray.shape
if (len(dim) == 4):
numComponents = dim[3]
dim = (dim[0],dim[1],dim[2])
type = self.__typeDict[imArray.typecode()]
if (imArray.typecode() == 'F' or imArray.typecode == 'D'):
numComponents = numComponents * 2
if (self.__ConvertIntToUnsignedShort and imArray.typecode() == 'i'):
imString = imArray.astype(Numeric.Int16).tostring()
type = VTK_UNSIGNED_SHORT
else:
imString = imArray.tostring()
self.__import.CopyImportVoidPointer(imString,len(imString))
self.__import.SetDataScalarType(type)
self.__import.SetNumberOfScalarComponents(numComponents)
extent = self.__import.GetDataExtent()
self.__import.SetDataExtent(extent[0],extent[0]+dim[2]-1,
extent[2],extent[2]+dim[1]-1,
extent[4],extent[4]+dim[0]-1)
self.__import.SetWholeExtent(extent[0],extent[0]+dim[2]-1,
extent[2],extent[2]+dim[1]-1,
extent[4],extent[4]+dim[0]-1)
def GetArray(self):
return self.__Array
# a whole bunch of methods copied from vtkImageImport
def SetDataExtent(self,extent):
self.__import.SetDataExtent(extent)
def GetDataExtent(self):
return self.__import.GetDataExtent()
def SetDataSpacing(self,spacing):
self.__import.SetDataSpacing(spacing)
def GetDataSpacing(self):
return self.__import.GetDataSpacing()
def SetDataOrigin(self,origin):
self.__import.SetDataOrigin(origin)
def GetDataOrigin(self):
return self.__import.GetDataOrigin()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment