vtk: fundamental algorithms, scalar – color mapping
DESCRIPTION
VTK: Fundamental Algorithms, Scalar – Color Mapping. Shroeder et al. Chapter 6. University of Texas – Pan American CSCI 6361, Spring 2014. After Taku Komura and other lecture sets http://www.inf.ed.ac.uk/teaching/courses/vis/. Overview. Recall …. From last time: Data representation - PowerPoint PPT PresentationTRANSCRIPT
VTK: Fundamental Algorithms,Scalar – Color Mapping
Shroeder et al. Chapter 6
University of Texas – Pan AmericanCSCI 6361, Spring 2014
After Taku Komura and other lecture setshttp://www.inf.ed.ac.uk/teaching/courses/vis/
Overview
Recall …
• From last time:
• Data representation– structure + value– structure = topology &
geometry– value = attribute
• Attribute Classification– scalar (today)– vector– tensor
Visualization Algorithms
• Generally, classified by attribute type– scalar algorithms (e.g. color mapping)– vector algorithms (e.g. glyphs)– tensor algorithms (e.g. tensor ellipses)
Scalar Algorithms
• Visualization algorithms generally:– scalar algorithms (e.g. color mapping)– vector algorithms (e.g. glyphs)– tensor algorithms (e.g. tensor ellipses)
• Scalar data : single value, e.g., 2.72, at each location– Structure of data set may be 1D, 2D or 3D+
– want to visualize the scalar within this structure
• Two fundamental algorithms– color mapping (transformation : value -> color)– contouring (transformation : value transition -> contour)
Color Mapping
• Map scalar value to color range for display– e.g.
• scalar value = height / max elevation• color range = blue -> red
• Color Lookup Tables (LUT)– provide scalar to color conversion– scalar values = indices into LUT
Satellite – Different Color Mappings
CG Color Look up Table, 1(same idea used for compressed images)
• Display hardware may not have sufficient capacity (memory) to store all, e.g, cell phone
– So, choose some to store• e.g., lots of shades of blue for water or sky (and not too many green)
• Any specific 2n colors– (e.g., below, 2^8 = 256 colors), 8 bit color, but which 256, out of 16.7m is important
• Look up table allows 2n colors be used in one picture,
• Color table is a resource managed (usually) by window manager
CG Color Look Up Table, 2
• Pixel value is indexed to color look up table (CLUT) where color is stored.
• CLUT look up done at video rates, overlapped with fetch and DAC
• CLUT allows variety of effects – pseudo coloring (LandSat images, stress diagrams, thermograms...) – fast image changes: change table rather than stored image – multiple images: select or composite/blend
Color Look Up Table, or LUT
• Maps (scalar) values to colors– scalar values Si in range {min -> max}– n unique colors, {color0... colorn-1} in LUT
• Define mapped color C:
• Right from previous satellite imagery
Color Transfer Function
• More general form of color LUT– scalar value S; color value C– color transfer function: f(S) = C– Any functional expression can map scalar value into intensity
values for color components
– e.g. define f() to convert densities to realistic skin/bone/tissue colors
Color Components – R, G, B
• Recall, electromagnetic spectrum visible to humans– continuous range 400-700nm– 3 type of receptors (cones) in eye
for R, G, B
• So, often use the RGB model in computer graphics for visualization
RGB Color Space
• Colors represented as R,G,B intensities– 3D color space (cube) with axes R, G and B– each axis 0 -> 1 (below scaled to 0-255 for 1 byte per color channel)– Black = (0,0,0) (origin); White = (1,1,1) (opposite corner)
• Problem : difficult to map continuous scalar range to 3D space– can use subset (e.g. a diagonal axis) but imperfect
Example: RGB Image
• Below is each of R,G and B in picture– RGB Channel Separation
Color Spaces and Grayscale
• Can map color to gray scale
• Linear combination of R, G, B– Grayscale = (R + G + B) / 3
• Defined as linear range– Easy to map linear scalar range to
grayscale intensity– Can enhance structural detail in
visualization• The shading effect is emphasized• As distraction of color is removed
– Not really using full graphics capability– Loose color associations :
• e.g. red=bad/hot, green=safe, blue=cold
Another Color ModelHSV: Hue, Saturation, Value …VTK uses for color map
• HSV: hue, saturation, value– encapsulates information about a color in terms
that are more familiar to humans:• What color is it?• How vibrant is it?• How light or dark is it?
• H (Hue) = dominant wavelength of color– color type {e.g. red, blue, green...}– 0 -> 360o
• S (Saturation) = amount of Hue present– Center has no saturation (hue), so “white”– “vibrancy” or purity of color– e.g., for Hue ~blue
• 0.5 = sky color, 1.0 = primary blue
• V (Value) = brightness of color– brightness of the color– e.g., 0 = black, 1 = bright
Example: HSV Image Components
• x
Different Color LUTs
• From VTK book
• Visualizing gas density in a combustion chamber
– Scalar = gas density– Color Map =
• A: grayscale• B: hue range blue to red• C: hue range red to blue
• D: specifically designed transfer function
– Highlights contrast
Color Table Design … Which You’ll Do
• “More of an art that a science”– where does visualization end and art begin?
• Key focus of color table design– emphasis important features / distinctions– minimize extraneous detail
• Often task specific– consider application
• e.g. temperature change, use hue red to blue– consider viewer (color associations, color blindness)– Rainbow color maps
• rapid change in color hue representing a ‘rainbow’ of colors.• shows small gradients well as colors change quickly.
Examples – 2D Color Images
• Infra-red intensity viewed as Hue– received from sensor as 2D array of infrared readings– visualize as color image using color mapping
• Temperature: – (left) - Red is “hot” (and white is really hot), blue is cool– (right) - Reddish is “hot” , green is “cool”, and black really cool
Examples – 3D Height Data
• Here’s hawaii …
• HSV based color transfer function– continuous transition of
height represented
• 8 color limited lookup table– discrete height transitions– “rainbow type” effect
Examples: Molecular Visualizations
• Two variables visualized relating to electric properties– mapped to Hue and Saturation
Example: Color Transfer Function
• Are the dimples on this golfball evenly distributed?– (hard to tell with this representation – how about a color map?)
Example: Color Transfer Function
• Answer: – No (Improves flight characteristics)
• Visualization technique: – color map each point based on distance (scalar) from regular sphere
Assignment
• .
Overview
• Context for next assignment– “Color mapping”
• Application of psuedocolor sequences to univariate scalar data– Using terrain, elevation, height data
• What it is, again• Examples and rules for design
• Vtk implementation of color mapping– Example code (available at course internet site), as are many
• Assignment– Design and implement an “interactive general purpose color
mapping program for terrain data”
VTK Color Mapping – In a Nutshell
• Will take a close look - see hawaii.tcl example
• To create a new LUT object with a name lut:– vtkLookupTable lut
• To set the color range in the HSV colorspace:– lut SetHueRange start finish– lut SetSaturationRange start finish– lut SetValueRange start finish– range = [0,1]
• Also define specific N color lookup table
Examples of 3D Height (Terrain) Data
1. HSV based color transfer function– continuous transition of
height represented
2. Color (8) lookup table– discrete height transitions
• segmentation– “rainbow type” effect
Vtk Implementation
• Assignment 4 asks that you design and implement an “interactive terrain data display” program (or some subset)– Use hawaii vtk data set and program
• Look at:– Vtk file formats– Example program segment– Vtk data, methods, documentation:
• vtkElevationFilter• vtkLookupTable• vtkDataSetMapper
Data: honolulu.vtk
• Data set (16m)– Many formats supported,
*.vtk (below) is just one
• Other data sets available– www. …/*.vtk– cube.vtk for test– skin.vtk for halloween
# vtk DataFile Version 1.0Terrain data for Honolulu, HawaiiASCIIDATASET POLYDATAPOINTS 202740 float0.000000 0.000000 0.000000 0.000000 30.000000 0.000000 0.000000 60.000000 0.000000 0.000000 90.000000 0.000000 0.000000 120.000000 0.000000 0.000000 150.000000 0.000000 0.000000 180.000000 0.000000 0.000000 210.000000 0.000000 0.000000 240.000000 0.000000 0.000000 270.000000 0.000000 0.000000 300.000000 0.000000 0.000000 330.000000 0.000000 0.000000 360.000000 0.000000 0.000000 390.000000 0.000000 0.000000 420.000000 0.000000 0.000000 450.000000 0.000000
“Color Mapping” Terrain Data: hawaii.cpp
• Hawaii.cpp (and .tcl)– Also, data set (16mb) and .tcl version– Pretty easy …, but usual “nameology” challenges
• Will look at each
vtkPolyDataReader *polydata = vtkPolyDataReader::New(); polydata->SetFileName("honolulu.vtk");
vtkElevationFilter *colorIt = vtkElevationFilter::New(); colorIt->SetInput((vtkDataSet *)polydata->GetOutput()); colorIt->SetLowPoint(0,0,0); colorIt->SetHighPoint(0,0,1000); colorIt->SetScalarRange(0,1000);
vtkLookupTable *lut = vtkLookupTable::New(); lut->SetHueRange(0.7,0); lut->SetSaturationRange(1.0,0); lut->SetValueRange(.5,1.0);
vtkDataSetMapper *mapper = vtkDataSetMapper::New(); mapper->SetInput(colorIt->GetOutput()); mapper->SetScalarRange(0,1000); mapper->SetLookupTable(lut); mapper->ImmediateModeRenderingOn;
and VTK Documentation Online
• http://www.vtk.org/
“Color Mapping” Terrain Data
• vtkElevationFilter– “generates scalar values from
a dataset”
vtkPolyDataReader *polydata = vtkPolyDataReader::New(); polydata->SetFileName("honolulu.vtk");
vtkElevationFilter *colorIt = vtkElevationFilter::New(); colorIt->SetInput((vtkDataSet *)polydata->GetOutput()); colorIt->SetLowPoint(0,0,0); colorIt->SetHighPoint(0,0,1000); colorIt->SetScalarRange(0,1000);
vtkLookupTable *lut = vtkLookupTable::New(); lut->SetHueRange(0.7,0); lut->SetSaturationRange(1.0,0); lut->SetValueRange(.5,1.0);
vtkDataSetMapper *mapper = vtkDataSetMapper::New(); mapper->SetInput(colorIt->GetOutput()); mapper->SetScalarRange(0,1000); mapper->SetLookupTable(lut); mapper->ImmediateModeRenderingOn;
vtkElevationFilterClass Reference
• Typical of documentation for large class based (oo) systems
• Inheritance diagram, etc.
• E.g., vtkElevationFilter is a derived class, etc.
– i.e., a “subclass” of vtkDataSetToDataSetFilter
– May need to consult other classes to fully understand what a particular subclass does
vtkElevationFilter Detailed Description• Description
– “vtkElevationFilter is a filter to generate scalar values from a dataset. The scalar values lie within a user specified range, and are generated by computing a projection of each dataset point onto a line. The line can be oriented arbitrarily. A typical example is to generate scalars based on elevation or height above a plane.”
– Wow, really great that’s in there and don’t have to program myself!”
• That’s the point!• But, how do you know?
• Public methods– What programmer sets
vtkElevationFilterMethod Descriptions
• Method/function descriptions
// not shown
colorIt->
SetInput((vtkDataSet *)polydata->GetOutput());
// below are shown at right
colorIt->SetLowPoint(0,0,0);
colorIt->SetHighPoint(0,0,1000);
colorIt->SetScalarRange(0,1000);
vtkElevationFilter Include File
• The details, if you want or need to know … it’s all there
• Definitions, etc. in “.h” (include) file Graphics/vtkElevationFilter.h
• Also can look at entire c++ code– For the curious
• Link in documentation
“Color Mapping” Terrain Data
• vtkLookupTable– an object that is used by mapper
objects to map scalar values into rgba
vtkPolyDataReader *polydata = vtkPolyDataReader::New(); polydata->SetFileName("honolulu.vtk");
vtkElevationFilter *colorIt = vtkElevationFilter::New(); colorIt->SetInput((vtkDataSet *)polydata->GetOutput()); colorIt->SetLowPoint(0,0,0); colorIt->SetHighPoint(0,0,1000); colorIt->SetScalarRange(0,1000);
vtkLookupTable *lut = vtkLookupTable::New(); lut->SetHueRange(0.7,0); lut->SetSaturationRange(1.0,0); lut->SetValueRange(.5,1.0);
vtkDataSetMapper *mapper = vtkDataSetMapper::New(); mapper->SetInput(colorIt->GetOutput()); mapper->SetScalarRange(0,1000); mapper->SetLookupTable(lut); mapper->ImmediateModeRenderingOn;
vtkLookupTableClass Reference
• Same song, second verse
• Class reference
vtkLookUpTableDetailed Description• As before …
• “vtkLookupTable is an object that is used by mapper objects to map scalar values into rgba (red-green-blue-alpha transparency) color specification, or rgba into scalar values. The color table can be created by direct insertion of color values, or by specifying hue, saturation, value, and alpha range and generating a table.”
vtkLookUpTableMethods
• lut->SetHueRange(0.7,0);
• lut-> SetSaturationRange(1.0,0);
• lut->SetValueRange(.5,1.0);
vtkLookUpTableMethod Descriptions• lut->SetHueRange(0.7,0);
• lut-> SetSaturationRange(1.0,0);
• lut->SetValueRange(.5,1.0);
“Color Mapping” Terrain Data
• vtkDataSetMapper– mapper to map data sets (i.e., vtkDataSet and
all derived classes) to graphics primitives.
vtkPolyDataReader *polydata = vtkPolyDataReader::New(); polydata->SetFileName("honolulu.vtk");
vtkElevationFilter *colorIt = vtkElevationFilter::New(); colorIt->SetInput((vtkDataSet *)polydata->GetOutput()); colorIt->SetLowPoint(0,0,0); colorIt->SetHighPoint(0,0,1000); colorIt->SetScalarRange(0,1000);
vtkLookupTable *lut = vtkLookupTable::New(); lut->SetHueRange(0.7,0); lut->SetSaturationRange(1.0,0); lut->SetValueRange(.5,1.0);
vtkDataSetMapper *mapper = vtkDataSetMapper::New(); mapper->SetInput(colorIt->GetOutput()); mapper->SetScalarRange(0,1000); mapper->SetLookupTable(lut); mapper->ImmediateModeRenderingOn;
vtkDataSetMapper Class Reference
• Same song, …
• Class reference
vtkDataSetMapper Detailed Description• “vtkDataSetMapper is a mapper to
map data sets (i.e., vtkDataSet and all derived classes) to graphics primitives. The mapping procedure is as follows: all 0D, 1D, and 2D cells are converted into points, lines, and polygons/triangle strips and then mapped to the graphics system. The 2D faces of 3D cells are mapped only if they are used by only one cell, i.e., on the boundary of the data set.”
That Should Get You Started
VTK Events and Interaction
• .
Recall, Software ArchitectureNow will see how to access window system (for keyboard input)
• E.g., your program, using VTK
• Uses VTK classes
Your (application) program
“Application Library” (VTK, GLUT, … or anything)
Graphics Library (OpenGL, DirectX, …)
Graphics Hardware (frame buffers, firmware, …)
Display (and input) Hardware (screen, mouse, ….)
Window System (MS Windows, Apple, Motif) mouse, keyboard, …
• VTK classes use/call:• OpenGL, which accesses Graphics Hardware• Also, Window System• And input devices, through window system
• Also, application can access OpenGL and Window System
User Interface Events
• All os, windowing systems, etc. are based on “event-driven architecture”
• “Events” distributed to programs by window system
• Events can be anything– Or, conversely, everything– E.g., button clicks, key presses, mouse movements, window resize, …
Events to Appropriate Event Queues
OS perspective:
program 1
queue 1
wndproc(){}
program n
queue n
wndproc(){}
Windows - distributes events
Program 1 Program 2 Program 3
Event queue 1 Event queue 2 Event queue 3
Windproc 1
Windproc 2
Windproc 3
User Interface Events
• All os, windowing systems, etc. are based on “event-driven architecture”
• “Events” distributed to programs by window systems
• Events can be anything– Or, conversely, everything– E.g., button clicks, key presses, mouse movements, window resize, …
• VTK “handles” the os distributed events, event handler function– Via API, allows restricted access
• Full access available, though
• Will focus on special case of “u” invoking programmer defined function
• Allows programmer to “do anything”. when user presses “u”
• Hawaii_interactive.cpp demonstrates
VTK Basic Program Structure
• #includes (some more)
• Any functions (none so far, this first)
• Main– Create renderer, renwindows– Read file– Set color attributes– Set up “observer” callback function (event handler, just “u”) <- new– Start program loop
• Clean up
hawaii_interactive.cpp, includes
// Will need access to events// at vtk.net\vtksrc\Common:
#include "vtkCommand.h"
// Will make (more) interactive, e.g., defaults: <- from last time// r = reset view - (but, not always)// f = fly-to point under cursor// e = exit// w = wireframe, s = surface form// p = pick actor under m ptr// u = user-defined function, // allows programmer-defined events, cf., Cone5.tcl
#include "vtkInteractorStyleTrackballCamera.h"
// BTW, example of windows (vs. console) program:// C:\vtk.net\vtksrc\Examples\GUI\Win32\SimpleCxx\Win32Cone.cxx
hawaii_interactive.cpp
// Will be called when char 'u' pressed with vtk window focus// After C:\vtk.net\vtksrc\Examples\Tutorial\Step2\Cone2.cxx
// This is where you can do anything you want …// show camera position, move camera, change color setup, …, get further input!
class vtkMyCallback : public vtkCommand{public: static vtkMyCallback *New() { return new vtkMyCallback; }
virtual void Execute(vtkObject *caller, unsigned long, void*) {
MessageBeep(0); vtkRenderer *renderer = reinterpret_cast<vtkRenderer*>(caller);
cout << "test"; }
hawaii_interactive.cpp
// … before call to “start” window.:vtkRenderer *ren1= vtkRenderer::New();
// Setup the observer, we do a new and ren1 will// eventually free the observer
vtkMyCallback *mo1 = vtkMyCallback::New();iren->AddObserver(vtkCommand::KeyPressEvent,mo1);mo1->Delete(); // interact with datairen->Initialize();ren->Start();
End