3d data visualization with mayavi and...

85
3D Data visualization with Mayavi and TVTK Prabhu Ramachandran Department of Aerospace Engineering IIT Bombay Advanced tutorials at SciPy09 Caltech, Pasadena Aug. 18, 2009 Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 1 / 73

Upload: others

Post on 20-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

3D Data visualization with Mayaviand TVTK

Prabhu Ramachandran

Department of Aerospace EngineeringIIT Bombay

Advanced tutorials at SciPy09Caltech, Pasadena

Aug. 18, 2009

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 1 / 73

Page 2: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Objectives

At the end of this session you will be able to:1 Use mlab effectively to visualize numpy array data

of various kinds2 Apply some of mayavi’s advanced features3 Embed mayavi visualizations in your dialogs4 Create TVTK datasets for more effective

visualization (if time permits)

Page 3: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Outline

1 Quick introduction to Mayavi

2 mlab

3 Embedding mayavi

4 Creating and working with datasets

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 3 / 73

Page 4: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Outline

1 Quick introduction to Mayavi

2 mlab

3 Embedding mayavi

4 Creating and working with datasets

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 4 / 73

Page 5: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Who are we?

Prabhu Ramachandran Creator and lead, 2001 –Gaël Varoquaux Mlab, documentation, usability,

2007 –Enthought Inc. ETS, Hosting, support, sprints,

initial funding, distribution

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 5 / 73

Page 6: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

History

Mayavi-1.x: 2001TVTK: 2004, EnthoughtMayavi2: 2005, Enthought, IITB2008: Mayavi sprint

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 6 / 73

Page 7: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Overview of features

Page 8: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

mlab: interactive work

from enthought . mayavi import mlabfrom numpy import ogr idx , y , z = ogr id [ −5:5:64 j ,

−5:5:64 j ,−5:5:64 j ]

mlab . contour3d ( x∗x∗0.5 + y∗y +z∗z∗2)

mlab . show ( )

Page 9: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively
Page 10: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Live in your dialogs

Page 11: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Mayavi in applications

Page 12: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exploring the documentation

Page 13: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other features

Easy customizationOffscreen animationsAutomatic script generationPowerful command line options

Page 14: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Summary

http://code.enthought.com/projects/mayavi

Uses VTK (www.vtk.org)BSD licenseLinux, win32 and Mac OS XHighly scriptableEmbed in Traits UIs (wxPython and PyQt4)Envisage PluginsDebian/Ubuntu/FedoraPythonic

10

Page 15: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Outline

1 Quick introduction to Mayavi

2 mlab

3 Embedding mayavi

4 Creating and working with datasets

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 15 / 73

Page 16: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Overview

SimpleConvenientFull-featured

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 16 / 73

Page 17: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Getting started

Vanilla:$ ipy thon −wthread

with Pylab:$ ipy thon −pylab −wthread

Page 18: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Using mlab:

>>> from enthought . mayavi import mlab

Try these:

>>> mlab . tes t_ <TAB>>>> mlab . tes t_contour3d ( )>>> mlab . tes t_contour3d ??

Page 19: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exploring the view

MouseKeyboardToolbarMayavi icon

15

Page 20: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

mlab plotting functions

0D data

>>> from numpy import ∗>>> t = l inspace (0 , 2∗pi , 50)>>> u = cos ( t )∗ p i>>> x , y , z = s in ( u ) , cos ( u ) , s in ( t )

>>> mlab.points3d(x, y, z)

Page 21: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Changing how things look

Clearing the view>>> mlab.clf()

IPython is your friend!>>> mlab.points3d?

Extra argument: ScalarsKeyword argumentsUI

>>> mlab . po in ts3d ( x , y , z , t ,scale_mode= ’ none ’ )

Page 22: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Changing how things look

Clearing the view>>> mlab.clf()

IPython is your friend!>>> mlab.points3d?

Extra argument: ScalarsKeyword argumentsUI

>>> mlab . po in ts3d ( x , y , z , t ,scale_mode= ’ none ’ )

Page 23: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Changing how things look

Clearing the view>>> mlab.clf()

IPython is your friend!>>> mlab.points3d?

Extra argument: ScalarsKeyword argumentsUI

>>> mlab . po in ts3d ( x , y , z , t ,scale_mode= ’ none ’ )

Page 24: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

1D data

>>> mlab.plot3d(x, y, z, t )

Plots lines between the points

Page 25: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

2D data

>>> x = mgrid [ −3:3:100 j , −3:3:100 j ]>>> z = s in ( x∗x + y∗y )

>>> mlab.surf(x, y, z)

Assumes the points are rectilinear

Page 26: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

2D data: mlab.mesh

>>> mlab.mesh(x, y, z)

Points needn’t be regular

>>> phi , the ta = numpy . mgrid [ 0 : p i :20 j ,. . . 0:2∗ p i :20 j ]>>> x = s in ( ph i )∗ cos ( the ta )>>> y = s in ( ph i )∗ s in ( the ta )>>> z = cos ( ph i )>>> mlab . mesh( x , y , z ,. . . r ep resen ta t i on = ’ wireframe ’ )

Page 27: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

3D data

>>> x , y , z = ogr id [ −5:5:64 j ,. . . −5:5:64 j ,. . . −5:5:64 j ]>>> mlab . contour3d ( x∗x∗0.5 + y∗y +

z∗z∗2)

Page 28: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

3D vector data: mlab.quiver3d

>>> mlab . tes t_qu ive r3d ( )

obj = mlab.quiver3d(x, y, z, u, v, w)

Page 29: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

3D vector data: mlab.flow

>>> x , y , z = mgrid [ −2:3 , −2:3 , −2:3]>>> r = s q r t ( x∗∗2 + y∗∗2 + z∗∗4)>>> u = y∗ s in ( r ) / ( r +0.001)>>> v = −x∗ s in ( r ) / ( r +0.001)>>> w = ze ros_ l i ke ( z )>>> ob j = mlab . f low ( x , y , z , u , v , w,

seedtype= ’ plane ’ )>>> ob j . s t ream_tracer . i n t e g r a t o r _ t y p e = \

’ runge_kutta45 ’

35

Page 30: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exercise: Lorenz equationdxdt

= s(y − x)

dydt

= rx − y − xz

dzdt

= xy − bz

Let s = 10, r = 28, b = 8./3.

Region of interestx , y , z = mgrid [ −50:50:20 j , −50:50:20 j ,

−10:60:20 j ]

Use mlab.quiver3d

Page 31: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Solutiondef l o renz ( x , y , z , s =10. , r =28. , b = 8 . / 3 . ) :

u = s ∗ ( y−x )v = r ∗x −y − x∗zw = x∗y − b∗zreturn u , v , w

x , y , z = mgrid [ −50:50:20 j , −50:50:20 j ,−10:60:20 j ]

u , v , w = lo renz ( x , y , z )

# Your p l o t here#mlab . show ( )

45

Page 32: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Issues and solutions

Basic visualization: not very usefulTweak parameters:mask_points, scale_factorExplore parameters on UImlab.flow is a lot better!

Good visualization involves work

Page 33: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 34: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 35: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 36: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 37: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 38: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 39: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Other utility functions

gcf: get current figuresavefig, figureaxes, outlinetitle , xlabel , ylabel , zlabel

colorbar, scalarbar, vectorbarshow: Standalone mlab scriptsOthers, see UG

Page 40: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Can we do more?

Yes!

Page 41: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

quiver3d ( x , y , z ,u , v , w,sca l e_ fac to r =0.01 ,mask_points =5)

Page 42: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Looking inside

Page 43: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

The pipeline

55

Page 44: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Lookup tables

List of Modules

TVTK Scene

Filter

Source

Mayavi Engine

ModuleManager

Page 45: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Changing the pipeline

On UIRight click on nodedrag drop

Script

Or use mlab.pipelineExample: mlab.pipeline. outline ()obj.remove()

Page 46: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exercise

>>> mlab . tes t_qu ive r3d ( )

Hide vectors, add a Vector Cut Plane

>>> mlab . t e s t _ f l o w ( )

Add a Vector Cut Plane

Page 47: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exercise

>>> mlab . tes t_qu ive r3d ( )

Hide vectors, add a Vector Cut Plane

>>> mlab . t e s t _ f l o w ( )

Add a Vector Cut Plane

Page 48: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Surprised?

Page 49: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

So what is the problem?

Page 50: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Points?

Page 51: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Curve?

Page 52: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Surface?

Page 53: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Interior of sphere?

Page 54: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Datasets

Quiver v/s Flow

Get back to this later!70

Page 55: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Recap

mlab gets you startedPipeline and data flowDatasets are important

Page 56: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Changing the pipeline

On UIRight click on nodedrag drop

Script

Or use mlab.pipelineExample: mlab.pipeline. outline ()obj.remove()

Page 57: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

mlab and Mayavi2?

mlab is just a thin layer over the Mayavi OO APImlab commands return mayavi objects

Page 58: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exercise

1 Start with flow for the Lorenz system2 Now extract the vector norm (use a filter)3 Plot iso-contours of this4 Figure out how to do this from the UI and

mlab.pipeline

80

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 49 / 73

Page 59: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

So how do you make a fancier script?

Use script recordingDemo

Page 60: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

So how do you make a fancier script?

Use script recordingDemo

Page 61: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Animating data

>>> s = mlab . f low ( x , y , z , u , v , w)>>> s . mlab_source . u = u∗z

mlab_source.set: multiple attributesIf you change the shape of the arrays use the resetmethod

Page 62: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Setting the view

>>> pr in t mlab . view ( )>>> mlab . view ( azimuth=None ,

e l ev a t i on =None ,d is tance=None ,f o c a l p o i n t =None )

95

Page 63: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Outline

1 Quick introduction to Mayavi

2 mlab

3 Embedding mayavi

4 Creating and working with datasets

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 53 / 73

Page 64: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

General approach

Embed Mayavi into a dialog boxUse traits to wire up everythingFull power of mayavi at your disposal

Page 65: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Simple example

105

Page 66: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Exercise: Lorenz trajectory

Use the provided skeleton script1 Create a simple UI to show a trajectory2 Create sliders to change the position of the initial

condition3 Create a UI element to change the integration time

110

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 56 / 73

Page 67: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Outline

1 Quick introduction to Mayavi

2 mlab

3 Embedding mayavi

4 Creating and working with datasets

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 57 / 73

Page 68: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Motivation

Datasets are fundamental to doing visualizationcorrectly

Motivational problemAtmospheric data of temperature over the surface ofthe earth.Let temperature (T ) vary linearly with height (z):

T = 288.15 − 6.5z

Page 69: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Simple solution

l a t = l inspace (−89, 89 , 37)lon = l inspace (0 , 360 , 37)z = l inspace (0 , 100 , 11)

x , y , z = mgrid [0 :360 :37 j , −89:89:37 j ,0:100:11 j ]

t = 288.15 − 6.5∗zmlab . contour3d ( x , y , z , t )mlab . o u t l i n e ( )mlab . co lo rba r ( )

Page 70: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Simple solution

l a t = l inspace (−89, 89 , 37)lon = l inspace (0 , 360 , 37)z = l inspace (0 , 100 , 11)

x , y , z = mgrid [0 :360 :37 j , −89:89:37 j ,0:100:11 j ]

t = 288.15 − 6.5∗zmlab . contour3d ( x , y , z , t )mlab . o u t l i n e ( )mlab . co lo rba r ( )

Page 71: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

What happens underneath?

P = mlab . p i p e l i n esrc = P. s c a l a r _ f i e l d ( x , y , z , t )i so = P. iso_sur face ( src )# Try t h i s .pr in t src

Page 72: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

The underlying datasetfrom enthought . t v t k . ap i import t v t ko r i g = (0 , −90, 0)spacing = (10 , 5 , 10)dims = (37 , 37 , 11)i d = t v t k . ImageData ( o r i g i n =or ig ,

spacing=spacing ,dimensions=dims )

i d . po in t_data . sca la rs = t . T . f l a t t e n ( )i d . po in t_data . sca la rs . name = ’T ’# View i t .src = P. add_dataset ( i d )i so = P. iso_sur face ( src )

Page 73: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

The general ideaSpecify the points (explicitly or implicitly)Specify the connectivity between the points(explicit/implicit)The connectivity lets you build “cells” that breakthe space into piecesSpecify “attribute” data at the points or cells

Cell data

10 20

3040

Points Rectangular cell

Triangular cells Point data

2010

Page 74: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Types of datasets

Implicit topology (structured):Image data (structured points)Rectilinear gridsStructured grids

Explicit topology (unstructured):Polygonal data (surfaces)Unstructured grids

Page 75: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Implicit versus explicit topology

Implicit topology associated with points:The X co-ordinate increases first, Y next and Z last

Easiest example: a rectangular meshNon-rectangular mesh certainly possible

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 64 / 73

Page 76: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

On a sphere?lon , l a t , h t = x∗ p i /180 , (90+y )∗ p i /180 , zr = (1+0.005∗ ht )tmp = r ∗ s in ( l a t )# Poin ts on the spherex , y , z = tmp∗cos ( lon ) , tmp∗ s in ( lon ) , r ∗cos ( l a t )p ts = empty ( x . shape + ( 3 , ) , dtype= f l o a t )p ts [ . . . , 0 ] = xpts [ . . . , 1 ] = ypts [ . . . , 2 ] = z

# Reorder the po in t s / sca la rs f o r VTKpts = pts . t ranspose (2 , 1 , 0 , 3 ) . copy ( )p ts . shape = pts . s i ze /3 , 3t = t . T . copy ( )

Page 77: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

sg = t v t k . S t ruc tu redGr id ( )sg . dimensions = x . shapesg . po in t s = ptssg . po in t_data . sca la rs = t . r ave l ( )sg . po in t_data . sca la rs . name = ’T ’

P = mlab . p i p e l i n esrc = P. add_dataset ( sg )P . gr id_p lane ( src )P . i so_sur face ( src , contours =1)

mlab . show ( )

Page 78: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

# Save a dataset to d isk .from enthought . t v t k . ap i import wr i te_da tawr i te_da ta ( dataset , fname )

# Open back the data .mlab . p i p e l i n e . open ( fname )

Try right clicking a node!

Page 79: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Unstructured grids

Explicit topology specificationSpecified via connectivity listsDifferent number of neighbors, different types ofcells

Page 80: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

PolyDatafrom enthought . t v t k . ap i import t v t k# The po in t s i n 3D.po in t s = ar ray ( [ [ 0 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] , ’ f ’ )# Connec t i v i t y v ia i nd i ces to the po in t s .t r i a n g l e s = ar ray ( [ [ 0 , 1 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 2 , 3 ] , [ 0 , 2 , 1 ] ] )# Creat ing the data ob jec t .

mesh = t v t k . PolyData ( )mesh . po in t s = po in t s # the po in t smesh . polys = 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 / ve r t s use : mesh . l i n e s = l i n e s ; mesh . ve r t s = v e r t i c e s# Now create some po in t data .temperature = ar ray ( [ 1 0 , 20 ,20 , 30 ] , ’ f ’ )mesh . po in t_data . sca la rs = temperaturemesh . po in t_data . sca la rs . name = ’ temperature ’

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 69 / 73

Page 81: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

PolyDatafrom enthought . t v t k . ap i import t v t k# The po in t s i n 3D.po in t s = ar ray ( [ [ 0 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] , ’ f ’ )# Connec t i v i t y v ia i nd i ces to the po in t s .t r i a n g l e s = ar ray ( [ [ 0 , 1 , 3 ] , [ 0 , 3 , 2 ] , [ 1 , 2 , 3 ] , [ 0 , 2 , 1 ] ] )# Creat ing the data ob jec t .

mesh = t v t k . PolyData ( )mesh . po in t s = po in t s # the po in t smesh . polys = 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 / ve r t s use : mesh . l i n e s = l i n e s ; mesh . ve r t s = v e r t i c e s# Now create some po in t data .temperature = ar ray ( [ 1 0 , 20 ,20 , 30 ] , ’ f ’ )mesh . po in t_data . sca la rs = temperaturemesh . po in t_data . sca la rs . name = ’ temperature ’

Prabhu Ramachandran (IIT Bombay) Mayavi2 tutorial 69 / 73

Page 82: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Summary

125

Page 83: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Advanced features

Command line arguments, timeseries,scripting

A demo with files

Page 84: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

$ mayavi2 −h

$ mayavi2 −d boundary . xml −m Surface \−d f l u i d _ 0 . xml −m Surface

Page 85: 3D Data visualization with Mayavi and TVTKconference.scipy.org/static/wiki/m2_tutorial_scipy09.pdfObjectives At the end of this session you will be able to: 1 Use mlab effectively

Thank you!