Introduction mlab VTK and TVTK Advanced features
3D visualization with TVTK and Mayavi
Prabhu Ramachandran Gal Varoquaux
Department of Aerospace Engineering IIT Bombay and Enthought Inc.
20, August 2008
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
Introduction
Mayavi A free, cross-platform, general purpose 3D visualization tool
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
Features
Mayavi provides
An application for 3D visualization An easy interface: mlab Ability to embed mayavi in your objects/views Envisage plugins A more general purpose OO library A numpy/Python friendly API
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
The Mayavi application
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
mlab
Example code
dims = [ 6 4 , 64 , 6 4 ] xmin , xmax , ymin , ymax , zmin , zmax = \ [ 5 ,5 , 5 ,5 , 5 ,5] x , y , z = numpy . o g r i d [ xmin : xmax : dims [ 0 ] 1 j , ymin : ymax : dims [ 1 ] 1 j , zmin : zmax : dims [ 2 ] 1 j ] x , y , z = [ t . astype ( f ) f o r t i n ( x , y , z ) ] s c a l a r s = xx 0.5 + yy + zz 2.0 # Contour t h e data . from enthought . mayavi import mlab mlab . contour3d ( s c a l a r s , c o n t o u r s =4 , t r a n s p a r e n t =True )
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
mlab in your dialogs
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
Mayavi in your envisage apps
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Overview Installation
Installation
Requirements:
numpy wxPython-2.8.x or PyQt 4.x VTK-5.x IPython Ideally ETS-3.0.0 if not ETS-2.8.0 will work
Easiest option: install latest EPD Debian packages??
easy_install Mayavi[app]
Get help on enthought-dev@[Link]
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Outline
Exposure to major mlab functionality Introduction to the visualization pipeline Advanced features
Datasets in mlab Filtering data Animating data
Demo of the mayavi2 app via mlab
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Animating data
Animate data without recreating the pipeline Use the mlab_source attribute to modify data Example code
x , y = numpy . mgrid [ 0 : 3 : 1 , 0 : 3 : 1 ] s = mlab . s u r f ( x , y , numpy . a s a r r a y ( x 0 . 1 , d ) ) # Animate t h e data . ms = s . mlab_source # Get t h e source f o r i i n range ( 1 0 ) : # Modify t h e s c a l a r s ms . s c a l a r s = numpy . a s a r r a y ( x 0 . 1 ( i + 1 ) , d )
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Animating data
Typical mlab_source traits x, y, z: x, y, z positions points: generated from x, y, z scalars: scalar data u, v, w: components of vector data vectors: vector data Important methods: set: set multiple traits efciently reset: use when the arrays change shape; slow update: call when you change points/scalars/vectors in-place Check out the examples: mlab.test_*_anim
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction Animating data
Exercise
Show iso-contours of the function sin(xyz )/xyz x , y , z in region [5, 5] with 32 points along each axis
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Introduction
Open source, BSD style license High level library 3D graphics, imaging and visualization Core implemented in C++ for speed Uses OpenGL for rendering Wrappers for Python, Tcl and Java Cross platform: *nix, Windows, and Mac OSX Around 40 developers worldwide Very powerful with lots of features/functionality: > 900 classes Pipeline architecture Not trivial to learn (VTK book helps) Reasonable learning curve
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
VTK / TVTK pipeline
Source Generates data
Filter
Processes data
Writer
Mapper
Graphics primitives
Sinks
Actor
Geometry, Texture, Rendering properties Renderer, RenderWindow, Interactors, Camera, Lights
Display
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Issues with VTK
API is not Pythonic for complex scripts Native array interface Using NumPy arrays with VTK: non-trivial and inelegant Native iterator interface Cant be pickled GUI editors need to be hand-made (> 800 classes!)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
TVTK
Traitied and Pythonic wrapper atop VTK Elementary pickle support
Get/SetAttribute() replaced with an attribute trait
Handles numpy arrays/Python lists transparently Utility modules: pipeline browser, ivtk, mlab Envisage plugins for tvtk scene and pipeline browser
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
The differences
VTK import vtk [Link] no constructor args [Link]() [Link]()
TVTK from [Link] import tvtk [Link] traits set on creation [Link] [Link]=w
vtk3DWidget ThreeDWidget
Method names: consistent with ETS (lower_case_with_underscores) VTK class properties (Set/Get pairs or Getters): traits
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Array example
Any method accepting DataArray, Points, IdList or CellArray instances can be passed a numpy array or a Python list!
>>> from enthought . t v t k . a p i import t v t k >>> from numpy import a r r a y >>> p o i n t s = a r r a y ( [ [ 0 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] , f ) >>> t r i a n g l e s = a r r a y ( [ [ 0 , 1 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 2 , 3 ] , [ 0 , 2 , 1 ] ] ) >>> mesh = t v t k . PolyData ( ) >>> mesh . p o i n t s = p o i n t s >>> mesh . p o l y s = t r i a n g l e s >>> t e mp e r at u r e = a r r a y ( [ 1 0 , 20 , 2 0 , 3 0 ] , f ) >>> mesh . p o i n t _ d a t a . s c a l a r s = t e m pe r a tu r e >>> import o p e r a t o r # A r r a y s are P y t h o n i c . >>> reduce ( o p e r a t o r . add , mesh . p o i n t _ d a t a . s c a l a r s , 0 . 0 ) 80.0 >>> p t s = t v t k . P o i n t s ( ) # Demo o f f r o m _ a r r a y / t o _ a r r a y >>> p t s . f r o m _ a r r a y ( p o i n t s ) >>> p r i n t p t s . t o _ a r r a y ( )
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
More details
TVTK is fully documented More details here: [Link]
projects/mayavi/[Link]
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Datasets: Why the fuss?
2D line plots are easy Visualizing 3D data: requires a little more information Need to specify a topology (i.e. how are the points connected)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
An example of the difculty
Points (0D) Surface (2D)
Interior of sphere: Volume (3D) Wireframe (1D)
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
The general idea
1 2 3 4
Specify the points of the space Specify the connectivity between the points (topology) Connectivity specify cells partitioning the space Specify attribute data at the points or cells
Points
Rectangular cell
Triangular cells
Point data 40 30 10 20
Mayavi
Cell data 20 10
Prabhu Ramachandran, Gal Varoquaux
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Types of datasets
Implicit topology (structured):
Image data (structured points): constant spacing, orthogonal Rectilinear grids: non-uniform spacing, orthogonal Structured grids: explicit points
Explicit topology (unstructured):
Polygonal data (surfaces) Unstructured grids
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Implicit versus explicit topology
Structured grids Implicit topology associated with points:
The X co-ordinate increases rst, Y next and Z last
Easiest example: a rectangular mesh Non-rectangular mesh certainly possible (structured grid)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Implicit versus explicit topology
Unstructured grids Explicit topology specication Specied via connectivity lists Different number of neighbors, different types of cells
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Different types of cells
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Dataset attributes
Associated with each point/cell one may specify an attribute
Scalars Vectors Tensors
Cell and point data attributes Multiple attributes per dataset
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Overview
Creating datasets with TVTK and Numpy: by example Very handy when working with Numpy No need to create VTK data les Can visualize them easily with mlab/mayavi See examples/mayavi/[Link]
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
PolyData
Create the dataset: [Link]() Set the points: points Set the connectivity: polys Set the point/cell attributes (in same order)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
PolyData
from enthought . t v t k . a p i import t v t k # The p o i n t s i n 3D . points = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) # C o n n e c t i v i t y via i n d i c e s to the p o i n t s . t r i a n g l e s = array ( [ [ 0 , 1 , 3 ] , [0 ,3 ,2] , [1 ,2 ,3] , [ 0 , 2 , 1 ] ] ) # C r e a t i n g t h e data o b j e c t . mesh = t v t k . PolyData ( ) mesh . p o i n t s = p o i n t s # t h e p o i n t s mesh . p o l y s = t r i a n g l e s # t r i a n g l e s f o r c o n n e c t i v i t y . # For l i n e s / v e r t s use : mesh . l i n e s = l i n e s ; mesh . v e r t s = v e r t i c e s # Now c r e a t e some p o i n t data . t em p e r at u r e = a r r a y ( [ 1 0 , 20 , 2 0 , 3 0 ] , f ) mesh . p o i n t _ d a t a . s c a l a r s = t em p e ra t u r e mesh . p o i n t _ d a t a . s c a l a r s . name = t em p e ra t u r e # Some v e c t o r s . v e l o c i t y = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) mesh . p o i n t _ d a t a . v e c t o r s = v e l o c i t y mesh . p o i n t _ d a t a . v e c t o r s . name = v e l o c i t y # Thats i t !
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
PolyData
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
ImageData
Orthogonal cube of data: uniform spacing Create the dataset: [Link]() Implicit points and topology: origin: origin of region spacing: spacing of points dimensions: think size of array Set the point/cell attributes (in same order) Implicit topology implies that the data must be ordered The X co-ordinate increases rst, Y next and Z last
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Image Data/Structured Points: 2D
# The s c a l a r v a l u e s . from numpy import arange , s q r t from s c i p y import s p e c i a l x = ( arange ( 5 0 . 0 ) 2 5 ) / 2 . 0 y = ( arange ( 5 0 . 0 ) 2 5 ) / 2 . 0 r = s q r t ( x [ : , None] 2+ y 2) z = 5.0 s p e c i a l . j 0 ( r ) # Bessel f u n c t i o n o f o r d e r 0 # # Can t s p e c i f y e x p l i c i t p o i n t s , t h e p o i n t s are i m p l i c i t . # The volume i s s p e c i f i e d u s i n g an o r i g i n , spacing and dimensions img = t v t k . ImageData ( o r i g i n =( 12.5 , 12.5 ,0) , spacing = ( 0 . 5 , 0 . 5 , 1 ) , dimensions = ( 5 0 , 5 0 , 1 ) ) # Transpose t h e a r r a y data due t o VTK s i m p l i c i t o r d e r i n g . VTK # assumes an i m p l i c i t o r d e r i n g o f t h e p o i n t s : X co o r d i n a t e # i n c r e a s e s f i r s t , Y n e x t and Z l a s t . We f l a t t e n i t so t h e # number o f components i s 1 . img . p o i n t _ d a t a . s c a l a r s = z . T . f l a t t e n ( ) img . p o i n t _ d a t a . s c a l a r s . name = s c a l a r
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
ImageData 2D
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Image data: 3D
from numpy import a r r a y , o g r i d , s i n , r a v e l dims = a r r a y ( ( 1 2 8 , 128 , 1 2 8 ) ) v o l = a r r a y ( ( 5 . , 5 , 5, 5 , 5, 5 ) ) origin = vol [ : : 2 ] spacing = ( v o l [ 1 : : 2 ] o r i g i n ) / ( dims 1) xmin , xmax , ymin , ymax , zmin , zmax = v o l x , y , z = o g r i d [ xmin : xmax : dims [ 0 ] 1 j , ymin : ymax : dims [ 1 ] 1 j , zmin : zmax : dims [ 2 ] 1 j ] x , y , z = [ t . astype ( f ) f o r t i n ( x , y , z ) ] s c a l a r s = s i n ( xyz ) / ( xyz ) # img = t v t k . ImageData ( o r i g i n = o r i g i n , spacing =spacing , dimensions=dims ) # The copy makes t h e data c o n t i g u o u s and t h e t r a n s p o s e # makes i t s u i t a b l e f o r d i s p l a y v i a t v t k . s = s c a l a r s . t r a n s po s e ( ) . copy ( ) img . p o i n t _ d a t a . s c a l a r s = r a v e l ( s ) img . p o i n t _ d a t a . s c a l a r s . name = s c a l a r s
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
ImageData 3D
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
RectilinearGrid
Orthogonal cube of data: non-uniform spacing Create the dataset: [Link]() Explicit points: x_coordinates, y_coordinates,
z_coordinates
Implicit topology: X rst, Y next and Z last Set the point/cell attributes (in same order)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
StructuredGrid
Create the dataset: [Link]() Explicit points: points Implicit topology: X rst, Y next and Z last Set the point/cell attributes (in same order)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Structured Grid
r = numpy . l i n s p a c e ( 1 , 10 , 25) t h e t a = numpy . l i n s p a c e ( 0 , 2 numpy . p i , 51) z = numpy . l i n s p a c e ( 0 , 5 , 25) # C r r e a t e an annulus . x_plane = ( cos ( t h e t a ) r [ : , None ] ) . r a v e l ( ) y_plane = ( s i n ( t h e t a ) r [ : , None ] ) . r a v e l ( ) p t s = empty ( [ l e n ( x_plane ) l e n ( h e i g h t ) , 3 ] ) f o r i , z _ v a l i n enumerate ( z ) : s t a r t = i l e n ( x_plane ) p l a n e _ p o i n t s = p t s [ s t a r t : s t a r t + l e n ( x_plane ) ] p l a n e _ p o i n t s [ : , 0 ] = x_plane p l a n e _ p o i n t s [ : , 1 ] = y_plane plane_points [ : , 2 ] = z_val s g r i d = t v t k . S t r u c t u r e d G r i d ( dimensions =(51 , 25 , 2 5 ) ) sgrid . points = pts s = numpy . s q r t ( p t s [ : , 0 ] 2 + p t s [ : , 1 ] 2 + p t s [ : , 2 ] 2 ) s g r i d . p o i n t _ d a t a . s c a l a r s = numpy . r a v e l ( s . copy ( ) ) s g r i d . p o i n t _ d a t a . s c a l a r s . name = s c a l a r s
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
StructuredGrid
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
UnstructuredGrid
Create the dataset: [Link]() Explicit points: points Explicit topology:
Specify cell connectivity Specify the cell types to use
set_cells(cell_type, cell_connectivity) set_cells(cell_types, offsets, connect)
Set the point/cell attributes (in same order) See examples/mayavi/unstructured_grid.py
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
Unstructured Grid
from numpy import a r r a y points = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) t e t s = array ( [ [ 0 , 1 , 2 , 3 ] ] ) t e t _ t y p e = t v t k . T e t r a ( ) . c e l l _ t y p e # VTK_TETRA == 10 # ug = t v t k . U n s t r u c t u r e d G r i d ( ) ug . p o i n t s = p o i n t s # T h i s s e t s up t h e c e l l s . ug . s e t _ c e l l s ( t e t _ t y p e , t e t s ) # A t t r i b u t e data . t em p e r at u r e = a r r a y ( [ 1 0 , 20 , 2 0 , 3 0 ] , f ) ug . p o i n t _ d a t a . s c a l a r s = te m p er a t ur e ug . p o i n t _ d a t a . s c a l a r s . name = t em p e r at u r e # Some v e c t o r s . v e l o c i t y = array ( [ [ 0 , 0 , 0 ] , [1 ,0 ,0] , [0 ,1 ,0] , [ 0 , 0 , 1 ] ] , f ) ug . p o i n t _ d a t a . v e c t o r s = v e l o c i t y ug . p o i n t _ d a t a . v e c t o r s . name = v e l o c i t y
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Introduction to VTK and TVTK TVTK datasets from numpy arrays
UnstructuredGrid
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
General approach
Create class deriving from HasTraits . . . Add an Instance of MlabSceneModel trait: lets call it scene mlab is available as [Link] Use a SceneEditor as an editor for the MlabSceneModel trait Do the needful in trait handlers Thats it!
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
An example
from enthought . t v t k . pyface . s c e n e _ e d i t o r import SceneEditor from enthought . mayavi . t o o l s . mlab_scene_model import MlabSceneMode from enthought . mayavi . core . u i . mayavi_scene import MayaviScene class A c to r V i e w er ( H a s T r a i t s ) : scene = I n s t a n c e ( MlabSceneModel , ( ) ) view = View ( Item ( name= scene , e d i t o r = SceneEditor ( scene_class=MayaviScene ) , show_label=False , r e s i z a b l e =True , w i d t h =500 , h e i g h t =500) , r e s i z a b l e =True ) def _ _ i n i t _ _ ( s e l f , t r a i t s ) : H a s T r a i t s . _ _ i n i t _ _ ( s e l f , t r a i t s ) s e l f . generate_data ( ) def generate_data ( s e l f ) : X , Y = mgrid [ [Link] j , [Link] j ] R = 10 s q r t (X 2 + Y 2) Z = s i n (R ) / R s e l f . scene . mlab . s u r f ( X , Y , Z , colormap= g i s t _ e a r t h ) a = A c t o r V i e we r ( ) ; a . c o n f i g u r e _ t r a i t s ( )
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Exercise
Take the previous example sin(xyz )/xyz and allow a user to specify a function that is evaluated Hints: Use an Expression trait
Eval the expression given in a namespace with your x, y, z arrays along with numpy/scipy. Use mlab_source to update the scalars
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Outline
1
Introduction Overview Installation
mlab Introduction Animating data
VTK and TVTK Introduction to VTK and TVTK TVTK datasets from numpy arrays Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
The big picture
Mayavi Engine
TVTK Scene Source Filter ModuleManager Lookup tables List of Modules
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Containership relationship
Engine contains: list of Scene Scene contains: list of Source Source contains: list of Filter and/or ModuleManager ModuleManager contains: list of Module Module contains: list of Component
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Class hierarchy
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Class hierarchy
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Interactively scripting Mayavi2
Drag and drop The mayavi instance >>> mayavi . new_scene ( ) # Create a new scene >>> mayavi . s a v e _ v i s u a l i z a t i o n ( f o o . mv2 )
[Link]:
>>> >>> >>> >>> e = mayavi . engine # Get t h e MayaVi engine . e . scenes [ 0 ] # f i r s t scene i n mayavi . e . scenes [ 0 ] . c h i l d r e n [ 0 ] # f i r s t scene s f i r s t source ( v t k f i l e )
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Scripting . . .
mayavi: instance of [Link]
Traits: application, engine Methods (act on current object/scene): open(fname) new_scene() add_source(source) add_filter(filter) add_module(m2_module) save/load_visualization(fname)
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Stand alone scripts
Several approaches to doing this Recommended way:
Save simple interactive script to [Link] le Run it like mayavi2 -x [Link] Can use built-in editor Advantages: easy to write, can edit from mayavi and rerun Disadvantages: not a stand-alone Python script
@standalone decorator to make it a standalone script: from [Link].mayavi2 import standalone
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Creating a simple new lter
New lter specs: Take an iso-surface + optionally compute normals Easy to do if we can reuse code, use the Optional, Collection lters
from enthought . mayavi . f i l t e r s . a p i import ( PolyDataNormals , Contour , O p t i o n a l , C o l l e c t i o n ) c = Contour ( ) # name i s used f o r t h e notebook t a b s . n = PolyDataNormals ( name= Normals ) o = O p t i o n a l ( f i l t e r =n , l a b e l _ t e x t = Compute normals ) f i l t e r = C o l l e c t i o n ( f i l t e r s = [ c , o ] , name= MyIsoSurface ) # Now f i l t e r may be added t o mayavi mayavi . a d d _ f i l t e r ( f i l t e r )
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Creating a simple new module
Using the GenericModule we can easily create a custom module out of existing ones. Here we recreate the ScalarCutPlane module
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
A ScalarCutPlaneModule
from enthought . mayavi . f i l t e r s . a p i import O p t i o n a l , WarpScalar , . . . from enthought . mayavi . components . c o n t o u r import Contour # i m p o r t Contour , A c t o r from components . from enthought . mayavi . modules . a p i import GenericModule cp = CutPlane ( ) w = WarpScalar ( ) warper = O p t i o n a l ( f i l t e r =w, l a b e l _ t e x t = Enable warping , enabled=False ) c = Contour ( ) c t r = O p t i o n a l ( f i l t e r =c , l a b e l _ t e x t = Enable c o n t o u r s , enabled=False ) p = PolyDataNormals ( name= Normals ) normals = O p t i o n a l ( f i l t e r =p , l a b e l _ t e x t = Compute normals , enabled=False ) a = Actor ( ) components = [ cp , warper , c t r , normals , a ] m = GenericModule ( name= ScalarCutPlane , components=components , c o n t o u r =c , a c t o r =a )
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Additional features
Offscreen rendering Animations Envisage plugins Customizing mayavi
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Offscreen rendering
Offscreen rendering: mayavi2 -x [Link] -o Requires a sufciently recent VTK release (5.2 or CVS) No UI is shown May ignore any window shown Normal mayavi and mlab scripts ought to work
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Animation scripts
from os . path import j o i n , e x i s t s from os import mkdir def make_movie ( d i r e c t o r y = / tmp / movie , n_step = 3 6 ) : i f not e x i s t s ( d i r e c t o r y ) : mkdir ( d i r e c t o r y ) scene = mayavi . engine . c u r r e n t _ s c e n e . scene camera = scene . camera da = 3 6 0 . 0 / n_step f o r i i n range ( n_step ) : camera . azimuth ( da ) scene . r e n d e r ( ) scene . save ( j o i n ( d i r e c t o r y , anim%02d . png %i ) ) i f __name__ == __main__ : make_movie ( )
Run using mayavi2 -x [Link]
Prabhu Ramachandran, Gal Varoquaux Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Envisage plugins
Allow us to build extensible apps Mayavi provides two plugins:
MayaviPlugin: contributes a window level Engine and
Script service offer and preferences
MayaviUIPlugin: UI related contributions: menus, tree view, object editor, perspectives
Prabhu Ramachandran, Gal Varoquaux
Mayavi
Introduction mlab VTK and TVTK Advanced features Embedding mayavi/mlab in traits UIs The mayavi library
Customization
Global: site_mayavi.py; placed on [Link] Local: ~/.mayavi2/user_mayavi.py: this directory is placed in [Link] Two things can be done in this le:
Register new sources/modules/lters with mayavis registry:
[Link]:registry get_plugins() returns a list of envisage plugins to add
See examples/mayavi/user_mayavi.py
Prabhu Ramachandran, Gal Varoquaux
Mayavi