78
IT Uppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip@cb.uu.se

ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

Embed Size (px)

Citation preview

Page 1: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

IT Uppsala universitet

Introduction to PythonIntroduction to VTK

Filip Malmberg [email protected]

Page 2: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

IT Uppsala universitet

Part 1: Introduction to Python

Page 3: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #3@ UU/IT

What is Python?• Dynamic, interpreted high-level language.• Created in 1991 by Guido van Rossum.• Design philosophy: Short development time is prioritized over

excecution speed.• Syntax similar to C++ or Java.

Page 4: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #4@ UU/IT

Facts about Python• Portable, available for all common platforms.• Python is Open Source and free to use, even for commercial

applications.• (Relatively) Easy to integrate with other languages, such as

Java, C/C++, Fortran and .NET.• Designed for multiple paradigms. Both object oriented and

procedural programming are possible.

Page 5: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #5@ UU/IT

What is Python good for?

• Internet applications, good support for HTTP, FTP, SMTP and CGI.

• Integrating components written in a low-level language, “glue code”.

• Portable system tools, same commands on each platform. Compare with dir (Windows) and ls (Linux).

• Portable GUI:s.• Database handling. • Projects where time of development is more important than

speed of execution.

Page 6: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #6@ UU/IT

What is Python not good for?

• Tasks where performance is critical.• Such tasks can be implemented in C/C++ modules using tools such as SWIG (www.swig.org).

Page 7: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #7@ UU/IT

Python and VTK

• VTK is written in C++, but has bindings to Python, Java, Tcl ...

• In this course, we will use VTK with Python.

Page 8: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #8@ UU/IT

The Python prompt

• Can be used to execute individual Python commands interactively.

• The prompt has a “memory” which is kept until the prompt is closed.

• Start the prompt by typing python in a terminal.

Page 9: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #9@ UU/IT

The Python language

• Variables and types• Control structures• Functions• Classes• File handling

Page 10: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #10@ UU/IT

Variables

•All variables in Python are references

Variable Data

Page 11: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #11@ UU/IT

Variable names

•May contain english letters, numbers and underscores.

• Must not start with a number.

Invalid names

påskmust1_varnamevarname 1var&name

Valid names

varnamevArNaMe1var_name_1_var_name

Page 12: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #12@ UU/IT

Variable assignment

•A reference is created with= a = 10b = 20c = a + b

a 10

b 20

c 30

Creates the following situation:

Page 13: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #13@ UU/IT

More on references

•Multiple references: Many variables can refer to the same object.

•Reference counting: An object is deleted automatically when no variables refer to it.

list_a [1, 2, 3]

list_b

list_c

>>> list = [1, 2, 3]>>> list_a = [1, 2, 3]>>> list_b = list_a>>> list_c = list_b>>> list_c[2] = 78>>> list_a[1, 2, 78]

Page 14: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #14@ UU/IT

Datatypes

• Numbers• Strings• Boolean types• Lists• Tuples• Others...

Page 15: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #15@ UU/IT

Numbers

•Different kinds of numbers are represented by different classes:

Integers (int) Big integers (long) Real numbers (float) Complex numbers (complex)

>>> a = 10>>> a.__class__<type 'int'>>>> big_num = 9875628736L>>> big_num.__class__<type 'long'>>>> pi_constant = 3.1415>>> pi_constant.__class__<type 'float'>>>> z = complex(3.4, 8.35)>>> z(3.399999999+8.349999999j)>>> z.__class__<type 'complex'>

Page 16: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #16@ UU/IT

Operations on numbers

• The operations +, -, * and / work as usual.

• % - Remainder // - Integer division ** - Power• abs(x) int(x) long(x) float(x) complex(a, b)

>>> a = 3.14>>> b = 5>>> c = b / a>>> c.__class__<type 'float'>>>> 5 // 22>>> 5 // float(2)2.0>>> 5 / float(2)2.5>>> b / complex(6, 4)(0.576923072-0.384615381j)>>> 2 / 30

Page 17: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #17@ UU/IT

Strings

• A string is a sequence of characters.

• A string is created using single or double quotes.

>>> s1 = "exempeltext">>> s2 = 'exempeltext igen'>>> s3 = "felaktigt'

File "<stdin>", line 1s3 = "felaktigt'

^SyntaxError: EOL while scanningsingle-quoted string>>> s4 = s1 + s2>>> s4'exempeltextexempeltext igen'>>> s5 = str(3)>>> s5'3'>>> s5.__class__<type 'str'>

Page 18: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #18@ UU/IT

Boolean types

• The following expressions are false:

NoneFalseThe number 0Every empty sequenceEvery empty mapping {}• All other objects are (somewhat

simplified) defined to be true.

>>> a = True>>> a.__class__<type 'bool'>>>> a = 5 > 7>>> aFalse

Page 19: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #19@ UU/IT

Lists

• Lists are containers with an arbitrary number of elements.

• The elements can be any Python object. A single list can contain objects of many different types.

>>> list = [1, 2, 3]>>> list[1, 2, 3]>>> list_2 = [1, "mixed","li"+"st"]>>> list_2[1, 'mixed', 'list']

Page 20: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #20@ UU/IT

More on lists

• Individual element are accessed with an index within square brackets [index]. The first element has index 0.

>>> list_2[1, 'blandad', 'lista']>>> list_2[1]'blandad'>>> list_2[1] = "Nytt element">>> list_2[1, 'Nytt element', 'lista']

Page 21: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #21@ UU/IT

Tuples

• Tuples are static lists.• Tuples have better performance than lists, but are less flexible.

>>> tuple_1 = (1, 2, 3)>>> tuple_2 = (1, "mixed")>>> tuple_2[1]'mixed'>>> tuple_2[1] = "New element"Traceback (most recent call last):

File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support itemassignment

Page 22: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #22@ UU/IT

Printing

• The Python command for writing text to the prompt is print.

>>> print "Hello"Hello>>> print "Hello", "world"Hello world>>> print 10+313

Page 23: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #23@ UU/IT

If-statements

• Note the indentation! In Python, indentation is used to control which block a statement belongs to. A colon indicates that a new block of code begins.

>>> a = 10>>> if a > 5:... print "The number is greater than 5"...The number is greater than 5

Page 24: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #24@ UU/IT

else

>>> a = 10>>> if a < 5:... print "a is less than 5"... else:... print "a is greater than or equal to 5"...a is greater than or equal to 5

Page 25: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #25@ UU/IT

Multiple choices

• Multiple choices are handled with elif.• Many languages have a case-statement for handling multiple choices. This was deemed redundant by the Python developers.

>>> a = 10>>> if a == 1:... print "a is one"... elif a == 2:... print "a is two"... elif a == 3:... print "a is three"... else:... print "a is something else"...a is something else

Page 26: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #26@ UU/IT

for-loops

• Again, use indentation to define a block of code. >>> for i in range(10):

... print i

...0123456789

Page 27: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #27@ UU/IT

Nested loops

>>> for i in range(2):... for j in range(3):... for k in range(4):... print "i=%i, j=%i, k=%i" % (i, j, k)...i=0, j=0, k=0i=0, j=0, k=1i=0, j=0, k=2i=0, j=0, k=3i=0, j=1, k=0i=0, j=1, k=1...i=1, j=1, k=2i=1, j=1, k=3i=1, j=2, k=0i=1, j=2, k=1i=1, j=2, k=2i=1, j=2, k=3

Page 28: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #28@ UU/IT

Beyond the Python prompt

• The python prompt is not suited for larger programs.

• Python programs are stored in regular text files.

• Commonly the filenames end with .py, but this is not required.

Page 29: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #29@ UU/IT

Executing Python programs

• Python files are executed using the python command.

• The search path to this program must be set.

• On windows, this is set by the system variable PYTHONPATH.

Page 30: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #30@ UU/IT

Python is dynamically typed# -*- coding: utf-8 -*-# a refers to a numbera = 1print a, a.__class__

# a refers to a stringa = "lkshjdglgv"print a, a.__class__

# a refers to a lista = [5, 2, 8, 5]print a, a.__class__a.sort()

# a refers to a number againa = 10a.sort()

$> python dynamic_binding.py10 <type 'int'>lkshjdglgv <type 'str'>[5, 2, 8, 5] <type 'list'>Traceback (most recent call last):File "dynamic_binding.py", line 18, in<module>a.sort()AttributeError: 'int' object has noattribute 'sort'

Duck Typing:

"when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."

Page 31: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #31@ UU/IT

Python is strongly typed

• No implicit type conversions

>>> a = 3>>> b = '4'>>> a + bTraceback (most recent call last):

File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s)for +: 'int' and 'str'>>> str(a) + b'34'>>> a + int(b)7

Page 32: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #32@ UU/IT

Functions in Python• A function is create using the reserved word def followed by the function name and a colon.

• The rules for function names are the same as for variable names.

$> python function_01.pyDetta skrivs först.Detta skrivs inuti funktionenDetta skrivs efter metodanropet.

# function_01.py

def function_a():print "Detta skrivs

inuti funktionen."

print "Detta skrivs först."function_a() # Funktionen anropasprint "Detta skrivs efter metodanropet."

Page 33: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #33@ UU/IT

Function arguments

•We communicate with functions by specifying arguments in the function call.

$> python function_02.pyHello Maja. You are23 years old.Hello Pelle. You are31 years old.

# function_02.py

def greeting(name, age):print """Hello %s. You are %i years old.""" %

(name, age)

greeting("Maja", 23)greeting("Pelle", 31)

Page 34: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #34@ UU/IT

Default arguments•Default arguments can be used to avoid having to specify all arguments.

# function_03.py

def greeting(name, age=20):print """Hej %s. Du är %i år gammal.""" % (name,

age)

greeting("Maja", 23)greeting("Pelle")

$> python function_03.pyHej Maja. Du är23 år gammal.Hej Pelle. Du är20 år gammal.

Page 35: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #35@ UU/IT

Order of arguments•Problems with many arguments: Arguments must be given in the order given in the function defintion.

# function_04.py

def greeting(name="Unknown", age=20):print """Hello %s. You are %i years old.""" % (name,

age)

greeting()greeting("Pelle")greeting(45) # Gives the wrong result

$> python function_04.pyHello Unknown. You are 20 years old.Hello Pelle. You are 20 years old.Hello 45. You are 20 years old.

Page 36: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #36@ UU/IT

Arguments by name

• The solution is to give arguments by name.

# function_05.py

def greeting(name="Okänd", age=20):print """Hej %s. Du är %i år gammal.""" % (name, age)

greeting()greeting("Pelle") # Still worksgreeting(name="Pelle") # Eqvivalentgreeting(age=45) # Gives the right resultgreeting("Maja", 33)greeting(name = "Maja", age = 33) # Eqvivalent

$> python function_05.pyHej Okänd. Du är 20 år gammal.Hej Pelle. Du är 20 år gammal.Hej Pelle. Du är 20 år gammal.Hej Okänd. Du är 45 år gammal.Hej Maja. Du är 33 år gammal.Hej Maja. Du är 33 år gammal.

Page 37: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #37@ UU/IT

Return values

•The return statement is used to return a value from a function.

# return_values_01.py

def my_own_join(texts, separator=" "):s = ""for text in texts:

s += text + separators = s[:-len(separator)] + "."

return s

my_text_pieces = ["Detta", "är", "inte","så", "meningsfullt"]print my_own_join(my_text_pieces, "_")

$> python return_values_01.pyDetta_är_inte_så_meningsfullt.

Page 38: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #38@ UU/IT

Multiple return values

• Python allows any number of return values.

# return_values_03.pydef min_max(seq):

return min(seq), max(seq)

a = [3, 573, 234, 24]minimum, maximum = min_max(a)print minimum, maximumresult = min_max(a)print resultprint result.__class__

$> python return_values_03.py3 573(3, 573)<type 'tuple'>

Page 39: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #39@ UU/IT

Modules

•When writing larger programs, it is not practical to keep all code in the same file.

• In python Modules offer a way to separate large programs into smaller units.

•Modules are also used to organize functions and variables into namespaces.

Page 40: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #40@ UU/IT

Standard modules

•Python has a number of standard modules that are always available for import.

•Modules are imported with the import-statement.

>>> sysTraceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name 'sys' is not defined>>> import sys>>> sys<module 'sys' (built-in)>>>> sys.version'2.4.3 (#1, Dec 11 2006, 11:39:03) \n[GCC 4.1.1 20061130 (Red Hat 4.1.1-43)]'

Page 41: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #41@ UU/IT

3rd party modules

• Lots of freely available modules for:– GUI:s– Image Processing– Computer Graphics– Web development– Numerical Computations– ...

Page 42: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #42@ UU/IT

The VTK module VTK in python is implemented as a Module

>>> import vtk

Page 43: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #43@ UU/IT

Object oriented programming

• Python is originally a procedural language, with added support for object orientation.

• Classes are defined using the class keyword:

# -*- coding: utf-8 -*-# io_01.py

class MyClassMyNumer=10def printNumber(self):

print 'The number is ',MyNumber

#Now we use the classanObject=MyClass()anObject.printNumber()

Page 44: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #44@ UU/IT

Object oriented programming

• Python is originally a procedural language, with added support for object orientation.

• Classes are defined using the class keyword:

# -*- coding: utf-8 -*-# io_01.py

class MyClass:MyNumer=10def printNumber(self):

print 'The number is ',MyNumber

#Now we use the classanObject=MyClass()anObject.printNumber()

Page 45: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #45@ UU/IT

Private variables

• Python has limited support for private class variables.

• Variable names starting with two underscores (“__”) are considered private.

• If you really want to, it is still possible to access those variables from outside the class.

Page 46: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #46@ UU/IT

Private variables, cont.• Under the hood, private variables are hidden

by textually replacing the name “__member” with “_classname__member”

# -*- coding: utf-8 -*-# io_01.py

class MyClass:__MyNumber=10 #private variable

#Now we use the classanObject=MyClass()print anObject.__MyNumber #This won't workprint anObject._MyClass__MyNumber #This will work, but is usually a bad idea

Page 47: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #47@ UU/IT

File I/O in python

• Files are opened with the open statement

# -*- coding: utf-8 -*-# io_01.py

f = open("newfile.txt", "r") # Öppna filenprint f.read() # Läs in hela filen

“r” -read only“w”- write only“r+” - read and write“a” - append data at the end of the file“b”- binary file

Page 48: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #48@ UU/IT

Reading parts of a file

# -*- coding: utf-8 -*-# io_01.py

f = open("newfile.txt")for row in f.readlines():

print row,

f.close()f = open("newfile.txt")print f.read(8)print f.read(5)

$> python io_02.pyDetta är textrad 1.Detta är textrad 2.Detta är textrad 3.Detta är textrad 4.Detta är text

Page 49: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #49@ UU/IT

Writing to a file

# -*- coding: utf-8 -*-# io_03.py

f = open("newfile.txt", "w")f.write(str(3) + "\n")f.write(str([1,2,3]) + "\n")f.write(str({"name":"Kalle"}) + "\n")f.close()

f = open("newfile.txt", "a")f.write("Denna rad läggs till.")f.close()

f = open("newfile.txt")print f.read()f.close()

$> python io_03.py3[1, 2, 3]{'name': 'Kalle'}Denna rad läggs till.

Page 50: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #50@ UU/IT

That's it!

• Now you know the basics of Python• More info: www.python.org

Page 51: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

IT Uppsala universitet

Part 2: Introduction to VTK

Page 52: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #52@ UU/IT

What is VTK? What can VTK be used for? How to actually use VTK?

VTK – The Visualization ToolKit

Page 53: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #53@ UU/IT

VTK – The Visualization ToolKit

Open source, freely available software for • 3D computer graphics• image processing• visualization

Managed by Kitware, Inc. Object-oriented design (C++) High-level of abstraction Use C++, Tcl/Tk, Python, Java

Page 54: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #54@ UU/IT

True visualization system Techniques for visualizing

• scalar fields• vector fields• tensor fields

Polygon reduction Mesh smoothing Image processing Your own algorithms

Page 55: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #55@ UU/IT

Additional features Parallel support

• message passing• multi-threading

Stereo support Integrates with Motif, Qt, Tcl/Tk, Python/Tk,

X11, Windows, ... Event handling 3D widgets

Page 56: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #56@ UU/IT

3D graphics Surface rendering Volume rendering

• Ray casting• Texture mapping (2D, 3D)

Lights and cameras Textures Save render window to .png, .jpg, ...

(useful for movie creation)

Page 57: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #57@ UU/IT

Data Representation

Collection of cells & points Cells specify topology

• Shape such as triangle, tetrahedron Points specify geometry

• Point coordinates assigned to a topology Data attributes

• Data associated with topology or geometry

Page 58: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #58@ UU/IT

Data attributes assigned to points or cells Scalar Vector

• magnitude and direction Normal

• a vector of magnitude 1• used for lighting

Texture coordinate• mapping data points into a texture space

Tensor

Page 59: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #59@ UU/IT

Visualization of attributes Scalar fields

• Color Mapping vtkLookupTable vtkScalarsToColors vtkColorTransferFunction

• Contouring vtkContourFilter vtkMarchingCubes

Page 60: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #60@ UU/IT

Visualization of attributes Vector fields

• Glyphs vtkGlyph3D

• Streamlines/points/tubes vtkStreamer vtkStreamLine vtkStreamPoints

Page 61: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #61@ UU/IT

Visualization of attributes Tensor fields

• vtkTensor• vtkTensorGlyph

Page 62: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #62@ UU/IT

The Visualization Pipeline

DATA

FILTER MAPPING

DISPLAY

Visualization algorithms

Interactive feedback

Page 63: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #63@ UU/IT

Objects Data objects

• e.g. vtkPolyData, vtkImageData Process objects

• Source objects (vtkReader, vtkSphereSource)• Filter objects (vtkContourFilter)• Mapper objects (vtkPolyDataMapper)

Page 64: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #64@ UU/IT

7 basic VTK objects to render a scene

• vtkRenderWindow: manages a window on the display device• vtkRenderer: coordinates the rendering process involving

lights, cameras, and actors• vtkLight: a source of light to illuminate the scene• vtkCamera: defines the view position, focal point, etc. • vtkActor: represents an object rendered in the scene,

both its properties and position in the world coordinate system • vtkProperty: defines the appearance properties of an actor

including colour, transparency, and lighting properties such as specular and diffuse. Also representational properties like wireframe and solid surface

• vtkMapper: the geometric representation for an actor. More than one actor may refer to the same mapper

Page 65: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #65@ UU/IT

Cube example

http://www.it.uu.se/edu/course/homepage/vetvis/ht10/

Page 66: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #66@ UU/IT

User interaction vtkRenderWindowInteractor

• allows the user to interact with the objects Try the following key presses

w wireframe mode s surface modej joystick mode t trackball modebutton1 rotate button2 translate button3 scale r reset camera viewe, q exit

Page 67: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #67@ UU/IT

Sphere example

http://www.it.uu.se/edu/course/homepage/vetvis/ht10/

Page 68: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #68@ UU/IT

VTK and C++ Build with CMake and your favorite compiler CMake generates makefiles or project files for your

environment Use the resulting file(s) to build your executable Under Windows you can use Microsoft Visual C++

Express Edition You have to install Tcl and/or Python to run vtk

scripts

Page 69: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #69@ UU/IT

VTK resources www.vtk.org

• Download • Documentation• Mailing lists• Links• FAQ, Search

www.kitware.com• VTK Textbook• VTK User’s guide• Mastering CMake

Page 70: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #70@ UU/IT

VTK Examples Really useful for getting started Available on the VTK webpage or in the VTK

folder.

Page 71: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #71@ UU/IT

VTK documentation

Page 72: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #72@ UU/IT

Summary + Free and open source Create graphics/visualization applications

fairly fast Object oriented - easy to derive new classes Build applications using “interpretive” languages

Tcl, Python, and Java Many (state-of-the-art) algorithms Heavily tested in real-world applications Large user base provides decent support Commercial support and consulting available

Page 73: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #73@ UU/IT

Summary -

Not a super-fast graphics engine due to portability and C++ dynamic binding – you need a decent workstation

Very large class hierarchy learning threshold might be steep

Page 74: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #74@ UU/IT

Computer exercises 2 assignments on VTK

Page 75: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #75@ UU/IT

Computer exercises One project on VTK

Page 76: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #76@ UU/IT

Computer exercises Deadline is next lab session

• Give a demonstration of your program• Show source code• Be able to answer questions

Page 77: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

UU/IT

08-01-29 | #77@ UU/IT

Computer exercises Lab 1

Wednesday September 8 13:15-17:00 P1312D, P1313DBasics of VTK

UpUnet account, UU-LOCAL, password C VTK 5.0.0 is installed in

• G:\Program\ACGV\VTK-5.0.0 You need to set the proper paths, run the

script in a command prompt• G:\Program\ACGV\set_paths.bat

Page 78: ITUppsala universitet Introduction to Python Introduction to VTK Filip Malmberg filip @cb.uu.se

IT Uppsala universitet

Introduction to PythonIntroduction to VTK

Filip Malmberg [email protected]