vtk visualizationtoolkit introduction

Upload: marco-piamba

Post on 02-Mar-2016

98 views

Category:

Documents


0 download

TRANSCRIPT

  • VTK Visualization Toolkit

    Introduction

    Carlos A. [email protected]

    Department of Physics ISEP School of EngineeringPolytechnic Institute of Porto

    Porto, PORTUGAL

  • Outline

    VTK Introduction Resources

    Installation

    VTK Architecture Low level object model

    VTK file format

    Rendering Engine

    Visualization pipeline

    Minimal Example

    VTK Programming

    VTK Interactors

    Computer Hands-On Build VTK with Cmake

    Test VTK Helloworld

    Cone1.cxx

    Cone2.cxx

    Cone3.cxx

    SingleScreenShot

    Learn by Examples 3D Graphics

    Visualization Techniques

    Imaging

    Carlos Vinhais VTK - Visualization Toolkit 2

  • VTK - Visualization Toolkit

    Introduction Free open source software for 3D computer graphics, image processing

    and visualization

    Consists of a C++ class library VTK classes implemented with .h and .cxx file

    Several interpreted interface layers, including Python, Tcl/Tk, and Java

    Supports a wide variety of visualization algorithms including scalar, vector, tensor, texture, and volumetric

    Advanced modeling techniques implicit modeling, polygon reduction, mesh smoothing, cutting, contouring

    and Delaunay triangulation

    Design and implementation influenced by object-oriented principles

    Carlos Vinhais VTK - Visualization Toolkit 3

  • VTK - Visualization Toolkit

    Resources Source distribution (source and binaries)

    www.vtk.org, Kitware, Inc.

    Distribution comes with many examples

    DocumentationOnline help, HTML based

    http://www.vtk.org/doc/release/5.8/html/

    Companion Text Books The Visualization Toolkit

    The VTK Users Guide

    www.kitware.com

    Mailing lists, Links, FAQ, Search

    Carlos Vinhais VTK - Visualization Toolkit 4

  • VTK - Visualization Toolkit

    Installation

    1. Choose OS and install compiler/IDE Tested on Windows 7

    MS Visual C++ 2010 Express Edition

    2. CMake installation requiredwww.cmake.org (cmake-2.8.7-win32-x86.exe)

    3. Get VTK source code vtk-5.8.0.zip

    4. Run Cmake to configure and generate the VTK project

    5. Compile VTK libraries

    6. Test VTK with the exrecises

    Carlos Vinhais VTK - Visualization Toolkit 5

    Cmake used to generate projects, makefiles or

    workspaces For different compilers and OS. Cmake

    is Cross platform.

  • VTK Architecture

    Object Model Dataset types found in VTK

    Image data

    Rectilinear grid

    Structured grid

    Unstructured points

    Polygonal Data

    Unstructured grid

    Data objects have geometric and topological structure (points and cells)

    Cells are topological arrangements of points

    Carlos Vinhais VTK - Visualization Toolkit 6

  • VTK Architecture

    Object Model

    Reference Counting

    vtkObjectBase * obj = vtkExampleClass::New();otheObject -> SetExample(obj);Obj -> Delete();

    Smart Pointers

    Class template vtkSmartPointer

    vtkSmartPointer obj = vtkSmartPointer ::New();

    otheObject -> SetExample(obj);

    Carlos Vinhais VTK - Visualization Toolkit 7

  • VTK Architecture

    Object Model

    Associated with the

    points and cells of a

    dataset

    scalar

    vector

    normal

    texture coordinate

    tensor

    field data

    Carlos Vinhais VTK - Visualization Toolkit 8

  • VTK File Format

    STRUCTURED POINTS

    (e.g. volume leon22.vtk)

    # vtk DataFile Version 3.0VTK File Generated by Insight Segmentation and Registration Toolkit (ITK)BINARYDATASET STRUCTURED_POINTSDIMENSIONS 512 512 64SPACING 0.703125 0.703125 5ORIGIN -173.3 -180 -269.75POINT_DATA 16777216SCALARS scalars short 1LOOKUP_TABLE default ......

    POLYDATA - unstructured points

    (e.g. teste_02_001.vtk)

    # vtk DataFile Version 3.0vtk outputASCIIDATASET POLYDATAPOINTS 77268 float75.9892 41.9058 932.34975.7524 41.8747 932.064...

    VERTICES 77268 1545361 0 1 1 ...

    Carlos Vinhais VTK - Visualization Toolkit 9

  • VTK Architecture

    Rendering EngineA VTK scene consists of:

    vtkRenderWindowInteractor window interaction

    vtkRenderWindow contains the final image

    vtkRenderer draws into the render window

    vtkActor combines properties/geometry

    vtkMapper represents geometry

    vtkProp, vtkProp3D Superclasses

    vtkProperty

    vtkLights illuminate actors

    vtkCamera renders the scene

    vtkTransform position actors

    Carlos Vinhais VTK - Visualization Toolkit 10

  • VTK Architecture

    Visualization Pipeline

    Visualization pipeline transforms information into graphical data Uses a data flow approach

    Two basic types of objects: vtkDataObject and vtkProcessObject

    Pipeline topology based on filter I/O Filters operate on data objects to produce new data objects

    thisFilter -> setInput( thatFilter->getOutput() );

    Pipeline execution

    Visualization pipelines use lazy evaluation

    Only executes when data is required for computation

    Carlos Vinhais VTK - Visualization Toolkit 11

  • VTK Architecture

    Visualization Pipeline

    data objects combined with process object to create the

    visualization pipeline

    Pipeline execution

    Carlos Vinhais VTK - Visualization Toolkit 12

  • VTK Architecture

    Visualization Pipeline

    Types of algorithms

    multiplicity of input and output

    Carlos Vinhais VTK - Visualization Toolkit 13

  • Minimal Example

    Cone1

    Basic setup for VTK programs Programming implementation of basic

    VTK exercises:

    Data source Procedural data, cone, sphere, cylinder,

    etc.

    Or data read from a file

    vtkMapper and vtkLookupTable Transform and render geometry

    Interface between the viz pipeline and the graphics model

    vtkScalarToColorsmaps data values to color

    vtkActors Combine object properties, geometries

    and orientation in virtual coordinates

    vtkRenderer Coordinates lights, cameras and actors

    to create an image

    vtkRenderWindow Manages the rendering process on the

    render window(s) on display device

    vtkRenderWindowInteractor Connection between the operating

    system and the VTK rendering engine

    Carlos Vinhais VTK - Visualization Toolkit 14

  • Minimal Example

    Cone1

    Carlos Vinhais VTK - Visualization Toolkit 15

    // C++// The basic setup of:// source->mapper->actor->renderer->renderwindow// is typical of most VTK programs.

    vtkConeSource *cone = vtkConeSource::New();cone->SetHeight( 3.0 );cone->SetRadius( 1.0 );cone->SetResolution( 10 );

    vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();

    coneMapper -> SetInput(cone->GetOutput());

    vtkActor *coneActor = vtkActor::New();coneActor -> SetMapper( coneMapper );

    vtkRenderer *renderer = vtkRenderer::New();renderer -> AddActor( coneActor );renderer -> SetBackground(0.1, 0.2, 0.4);

    vtkRenderWindow *renderWindow = vtkRenderWindow::New();

    renderWindow -> AddRenderer( renderer );renderWindow -> SetSize( 300, 300 );

    vtkRenderWindowInteractor *renderWindowInteractor = vtkRenderWindowInteractor::New();

    renderWindowInteractor -> SetRenderWindow( renderWindow );

    renderWindow -> Render();renderWindowInteractor -> Start();

  • VTK Programming

    Conversions between languages relatively straightforward

    Class names and method names remain the same

    Implementation details change (syntax)

    GUI details change

    Example

    C++ ren1->GetActiveCamera()->Azimuth( 1 ); Python ren1.GetActiveCamera().Azimuth( 1 ) Tcl [ren1 GetActiveCamera] Azimuth 1 Java ren1.GetActiveCamera().Azimuth( 1 );

    Carlos Vinhais VTK - Visualization Toolkit 16

  • VTK Programming

    Carlos Vinhais VTK - Visualization Toolkit 17

    # Python# The basic setup of: # source->mapper->actor->renderer->renderwindow# is typical of most VTK programs.

    cone = vtk.vtkConeSource()cone.SetHeight( 3.0 )cone.SetRadius( 1.0 )cone.SetResolution( 10 )

    coneMapper = vtk.vtkPolyDataMapper()coneMapper.SetInput( cone.GetOutput() )

    coneActor= vtk.vtkActor()coneActor.SetMapper( coneMapper)

    ren1= vtk.vtkRenderer()ren1.AddActor( coneActor)ren1.SetBackground( 0.1, 0.2, 0.4 )

    renWin= vtk.vtkRenderWindow()renWin.AddRenderer( ren1 )renWin.SetSize( 300, 300 )

    ...

    // C++// The basic setup of:// source->mapper->actor->renderer->renderwindow// is typical of most VTK programs.

    vtkConeSource *cone = vtkConeSource::New();cone->SetHeight( 3.0 );cone->SetRadius( 1.0 );cone->SetResolution( 10 );

    vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();

    coneMapper -> SetInput(cone->GetOutput());

    vtkActor *coneActor = vtkActor::New();coneActor -> SetMapper( coneMapper );

    vtkRenderer *renderer = vtkRenderer::New();renderer -> AddActor( coneActor );renderer -> SetBackground(0.1, 0.2, 0.4);

    vtkRenderWindow *renderWindow = vtkRenderWindow::New();

    renderWindow -> AddRenderer( renderer );renderWindow -> SetSize( 300, 300 );

    vtkRenderWindowInteractor *renderWindowInteractor = vtkRenderWindowInteractor::New();

    renderWindowInteractor -> SetRenderWindow( renderWindow );

    renderWindow -> Render();renderWindowInteractor -> Start();

  • VTK Interactors

    vtkRenderWindowInteractor

    Keypress: j|t Toggle between joystick or trackball mode

    In joystick style the motion occurs continuously as long as the mouse button is pressed

    In trackball style the motion occurs when the mouse button is pressed and the mouse cursor moves

    Keypress: c|a Toggle between camera and actor modes

    In camera mode, the mouse events affect the camera position and focal point

    In actor mode, the mouse events affect the object under the mouse pointer

    Keypress: e|q Exit/quit application

    Keypress: 3 Toggle in and out of stereo model

    Default is red-blue stereo pairs

    Left Mouse Button Rotate camera or actor

    Camera rotated around its focal point

    Actor rotated around its origin

    Middle Mouse Button Pan camera or translate actor

    In joystick mode direction of pan/translation is from center of the viewporttoward the mouse position

    In trackball mode, direction of motion in the direction of the mouse movement MMB

    Right Mouse Button Zoom camera or scale actor

    Zoom in/increase scale in top half of the viewport

    Zoom out/decrease scale in lower half of the viewport

    In joystick mode amount is controlled by distance of the pointer from the horizontal center line

    Carlos Vinhais VTK - Visualization Toolkit 18

  • VTK Interactors

    Keypress u Invokes user-defined mode

    brings up an command interactor window

    Rerender using irenRender

    Keypress f Fly-to the point under the cursor

    Sets the focal point allowing rotations about that point

    Keypress p Pick operation

    Render window has an internal instance of vtkPropPickerfor picking

    Keypress r Reset the camera along the viewing direction

    Centers the actors

    All actors visible

    Keypress s All actors represented as surfaces

    Keypress w All actors represented in wire frame

    Carlos Vinhais VTK - Visualization Toolkit 19

    solid

    wired

    stereo

  • Computer Hands-On

    Build and Test VTK

    Build VTK

    Follow the notes!

    Install_Notes_VTK5_VC2010_Win64.pdf

    Test VTK

    Get the source code!

    Helloworld.zip (CMakeLists.txt + Helloworld.cxx)

    Requires ITK installed!

    Carlos Vinhais VTK - Visualization Toolkit 20

  • Computer Hands-On

    VTK Exercises

    Cone1

    Cone2

    Cone3

    SingleScreenShot

    Carlos Vinhais VTK - Visualization Toolkit 21

    Full screen, stereo

    1 window, 3 viewports

  • Computer Hands-On

    SingleScreenShot

    #include #include

    #include #include

    ...

    renderWindow -> SetSize( 512, 512 );// set window size

    renderWindow -> StereoCapableWindowOn();renderWindow -> StereoRenderOn();renderWindow -> SetStereoTypeToAnaglyph();//renderWindow -> SetStereoTypeToCrystalEyes();//renderWindow -> SetStereoTypeToLeft();renderWindow -> StereoUpdate();

    renderWindow->FullScreenOn(); //WILL RENDER IN STEREO

    renderWindow -> Render();renderWindowInteractor -> Start();

    Carlos Vinhais VTK - Visualization Toolkit 22

    Full screen, stereo

  • Computer Hands-On

    SingleScreenShot (cont.)...

    renderWindow -> Render();renderWindowInteractor -> Start();

    vtkSmartPointer< vtkWindowToImageFilter > windowToImageFilter = vtkSmartPointer< vtkWindowToImageFilter >::New();

    windowToImageFilter -> SetInput( renderWindow );windowToImageFilter -> Update();

    vtkSmartPointer< vtkPNGWriter > writer = vtkSmartPointer< vtkPNGWriter >::New();

    writer -> SetFileName( "SingleScreenshot.png" );writer -> SetInput( windowToImageFilter->GetOutput() );writer -> Write();

    ...

    Carlos Vinhais VTK - Visualization Toolkit 23

  • 3D Graphics

    Surface rendering

    Volume rendering

    Ray casting

    Texture mapping (2D)

    Lights and cameras

    Textures

    Save render window to .png, .jpg, ...

    (useful for movie creation)

    ...

    Carlos Vinhais VTK - Visualization Toolkit 24

  • Visualization Techniques

    Scalar algorithms

    Contouring

    Color mapping

    Vector algorithms

    Streamlines

    streamtubes

    Tensor algorithms

    Tensor ellipsoids

    Carlos Vinhais VTK - Visualization Toolkit 25

  • Imaging

    vtkImageToImageFilter

    Diffusion

    High-pass / Low-pass (Fourier)

    Convolution

    Gradient (magnitude)

    Distance map

    Morphology

    Skeletons

    Carlos Vinhais VTK - Visualization Toolkit 26

  • Visualization Techniques

    Contouring Contouring

    Also Iso-surfaces

    Filter vtkContourFilter performs the function

    Using SetValue() method contours SetValue 0.0 0.5

    Using GenerateValues() method Contours generateValues 8 0.0 1.2

    Specify range and number of contours

    Many methods perform contouring vtkMarchingCubes

    vtkMarchingSquares

    Carlos Vinhais VTK - Visualization Toolkit 27

  • Visualization Techniques

    Color Mapping Coloring objects by scalar values

    Scalar values mapped through lookup table

    Color applied during rendering

    Modifies appearance of points or cells

    Use any data array Method ColorByArrayComponent()

    If not specified, a default lookup table is created by the mapper

    Carlos Vinhais VTK - Visualization Toolkit 28

  • 3D Widgets

    Watch for events invoked by vtkRenderWindowInteractor

    Subclasses of vtkInteractorObserver

    List of most important widgets vtkScalarBarWidget

    vtkPointWidget

    vtkLineWidget

    vtkPlaneWidget

    vtkImplicitPlane

    vtkBoxWidget

    vtkImagePlaneWidget

    vtkSphereWidget

    vtkSplineWidget

    Carlos Vinhais VTK - Visualization Toolkit 29

  • Visualization Techniques

    Cutting Create cross-section of dataset

    Any implicit function Planes create planar cuts

    Cutting surface interpolates the data

    Result is always type vtkPolyData

    vtkCutter needs an implicit function to cut

    May use more cut values SetValue() method

    GenerateValues() method

    Values specify the value of the implicit function

    Cutting values Zero precisely on the implicit function

    Less than zero, below

    Greater than zero, above

    Only strictly true for vtkPlane

    Carlos Vinhais VTK - Visualization Toolkit 30

  • Visualization Techniques

    Merging and Probing Merge data

    Pipelines could have loops

    Multiple streams of the pipeline

    vtkMergeFilter merges data from several datasets to a new dataset

    Probing vtkAppendFilter builds new

    dataset by appending datasets Specialized filter

    vtkAppendPolyData

    Only data attributes common to all data input are appended

    Carlos Vinhais VTK - Visualization Toolkit 31

  • Visualization Techniques

    Glyphing Represent data using symbols, or

    glyphs

    Simple glyphs Cone oriented to a vector

    Complex Symbolic representation of the

    human face

    Expression controlled by the data values

    vtkGlyph3D class Scaled, colored

    Orientated along a direction

    Glyphs copied to each point of the dataset

    Glyphs defined by second input to the filter

    Glyphs of type vtkPolyData

    Carlos Vinhais VTK - Visualization Toolkit 32

  • Visualization Techniques

    Glyphing

    The glyph uses the point attribute normals for orientation

    Use vector data using the SetVectorMethodToUseVector()

    Scale glyphs by scalar data using SetScaleModeToScaleByScalar()

    Turn data scaling off using SetScaleModeToDataScalingOff()

    Many other options

    Carlos Vinhais VTK - Visualization Toolkit 33

  • Visualization Techniques

    Streamlines

    Streamlines

    Path of a massless particle in a vector field

    Requires starting point(s)

    Integration direction

    Along the flow

    Opposite the flow

    Both

    Carlos Vinhais VTK - Visualization Toolkit 34

  • Visualization Techniques

    Streamsurfaces

    Series of ordered points, used to generate streamlines

    vtkRuledSurfaceFilter used to create a surface

    Points must be ordered carefully

    Assumes points lie next to one another

    Within a specified distance of neighbors (DistanceFactorvariable)

    Carlos Vinhais VTK - Visualization Toolkit 35

  • VTK Example

    The Virtual Frog

    Carlos Vinhais VTK - Visualization Toolkit 36

    http://www-itg.lbl.gov/Frog

  • End of VTK Lecture.

    Thank you!

    Carlos A. [email protected]

    Department of Physics ISEP School of EngineeringPolytechnic Institute of Porto

    Porto, PORTUGAL