# Matlab Vis Spring 2011

Document Sample

```					Scientific Visualization Using MATLAB

Robert Putnam
putnam@bu.edu

Scientific Visualization Using MATLAB - Spring 2011
Outline
   Introduction
   Geometry / Topology / Data Types
   Camera, lights, …
   Scalar visualization
   Vector visualization
   Extras – scattered data, animation, volume vis, sliceomatic, etc.
   Sources

Scientific Visualization Using MATLAB - Spring 2011
MATLAB
MATrix LABoratory
– High-level language for technical computing

– Interactive GUI

– Command line interface (Command Window)

– Application-specific toolboxes available (Parallel Computing, Statistics, etc.)

– Coupled with Maple for Symbolic computation

– Good documentation available (user guides, demos, videos, etc.)

– Professional support services available from MathWorks

Scientific Visualization Using MATLAB - Spring 2011
Scientific Visualization
• Visualization: converting raw data to a form that is
viewable and understandable to humans.

• Scientific visualization: specifically concerned
with data that has a well-defined representation in
2D or 3D space (e.g., from simulation mesh or
scanner).

Tutorial, Moreland

Scientific Visualization Using MATLAB - Spring 2011
Generic visualization pipeline

---------------------
Source(s)                     Filters(s)                             Output
(Rendering)

data/geometry/topology                                       graphics

Scientific Visualization Using MATLAB – Spring 2011
Geometry v. Topology
   Geometry of a dataset ~= points

0,1         1,1       2,1         3,1

0,0         1,0       2,0         3,0

    Topology ~= connections among points, which
define cells

    So, what’s the topology here?

Scientific Visualization Using MATLAB – Spring 2011
Geometry v. Topology

0,1         1,1       2,1         3,1

0,0         1,0       2,0         3,0

Scientific Visualization Using MATLAB – Spring 2011
Geometry v. Topology

0,1         1,1        2,1        3,1

0,0         1,0        2,0        3,0

or

0,1         1,1        2,1        3,1

0,0         1,0        2,0        3,0

Scientific Visualization Using MATLAB – Spring 2011
Geometry v. Topology
or

0,1         1,1        2,1        3,1        0,1   1,1        2,1   3,1

0,0         1,0        2,0        3,0        0,0   1,0        2,0   3,0

or

0,1         1,1        2,1        3,1

0,0         1,0        2,0        3,0

Scientific Visualization Using MATLAB – Spring 2011
Geometry v. Topology
or

0,1         1,1        2,1        3,1        0,1   1,1        2,1   3,1

0,0         1,0        2,0        3,0        0,0   1,0        2,0   3,0

or                                     or

0,1         1,1        2,1        3,1        0,1   1,1        2,1   3,1

0,0         1,0        2,0        3,0        0,0   1,0        2,0   3,0

Scientific Visualization Using MATLAB – Spring 2011
Geometry/Topology Structure
   Structure may be regular or irregular
– Regular (structured)
• need to store only beginning position, spacing, number of points
• smaller memory footprint per cell (topology can be generated on the fly)
• examples: image data, rectilinear grid, structured grid

– Irregular (unstructured)
• information can be represented more densely where it changes quickly
• higher memory footprint (topology must be explicitly written) but more freedom
• examples: polygonal data, unstructured grid

Scientific Visualization Using MATLAB – Spring 2011
Examples of Dataset Types
   Structured Points (Image Data)
– regular in both topology and geometry
– examples: lines, pixels, voxels
– applications: imaging CT, MRI

   Rectilinear Grid
– regular topology but geometry only partially
regular
– examples: pixels, voxels

   Structured Grid (Curvilinear)
– regular topology and irregular geometry
– applications: fluid flow, heat transfer

Scientific Visualization Using MATLAB – Spring 2011
Examples of Dataset Types (cont)
   Polygonal Data
– irregular in both topology and geometry
– examples: vertices, polyvertices, lines,
polylines, polygons, triangle strips

   Unstructured Grid
– irregular in both topology and geometry
– examples: any combination of cells
– applications: finite element analysis,
structural design, vibration

Scientific Visualization Using MATLAB – Spring 2011
Characteristics of Data
   Data is organized into datasets for visualization
– Datasets consist of two pieces
• organizing structure
– points (geometry)
– cells (topology)
• data attributes associated with the structure
– File format derived from organizing structure

   Data is discrete
– Interpolation functions generate data values in between known points

Scientific Visualization Using MATLAB – Spring 2011
MATLAB – Data Types
Volume Data
– Data defined on three-dimensional grids
– Multidimensional arrays of scalar or vector data
– Two basic types
• Scalar volume data
• Single data values for each point
• Examples: temperature, pressure, density, elevation
• Vector volume data
• Two or three values for each point (components of a vector)
• Magnitude and direction
• Examples: velocity, momentum

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – Data Types
Basic data type
– The basic data type is the array.

– A vector is an Nx1 (or 1XN) array (and a scalar is a 1x1 array):

– “Arrays in MATLAB are N-dimensional, with an infinite number of trailing singleton

dimensions. Trailing singleton dimensions past the second are not displayed or

reported on, e.g., with size. No array has fewer than two dimensions.”

– Array dimensions are generally reported in “row by column” order (i.e., y by x!).

Scientific Visualization Using MATLAB - Spring 2011
Figure windows, GUIs
•   “Handle graphics”

• Numerical id assigned to figure window and its components

•   GUI (for menus, sliders, etc.)

Scientific Visualization Using MATLAB - Spring 2011
Figure Window Hierarchy
•    Root object
•    Figure object(s)

Scientific Visualization Using MATLAB - Spring 2011
Graphics Model from the bottom up
•    Core graphics objects
• axes (define coordinate system)
• line
• text
• rectangle
• light
• patch (filled polygons)
• surface (3D grid of
• images (2D representation
of image)

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – figure function
Figure
– All graphical output directed to a graphics window called a figure

– Separate from the Command Window

– Can contain menus, toolbars, user-interface objects, context menus, axes, or any

other type of graphics object.

–    To create a new figure, use the figure function

•   (type figure in a Command Window)

--    Note: most plotting/graphics functions will create a figure if one does not exist,

so in many cases, explicitly calling ‘figure’ is unnecessary.

Scientific Visualization Using MATLAB - Spring 2011
Code – figure function
Command Window
f = figure

figure creates a new figure object using
default property values. This automatically
becomes the current figure and raises it
above all other figures on the screen until
a new figure is either created or called.

gcf: Get current figure

figure(f): make f current figure

close all: close all figure windows

Scientific Visualization Using MATLAB - Spring 2011
Code – property list
Command Window:figure1.m
f = figure('Name','Test Window','Position',[100 500 350 350],'MenuBar','none')

or

set (f, 'Name', 'Test Window')
set (f, 'Position', [100 500 350 350])

Type
get(f)
to see all figure properties.
Tip: use ‘more on’ to keep list from scrolling.

Scientific Visualization Using MATLAB - Spring 2011
MATLAB - Viewing
Viewing
– The process of displaying a graphical scene from various directions by adjusting
the camera position, changing the perspective, changing the aspect ratio, etc.

– The particular orientation you set to display the visualization

– Composed from two basic functions:

• Positioning the viewpoint to orient the scene – view function

• Setting the aspect ratio and relative axis scaling – axis function

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – view function
view
– The viewpoint is specified by
defining azimuth and elevation with
respect to the axis origin

– azimuth is a polar angle in the x-y
plane, with positive angles
indicating counterclockwise
rotation of the viewpoint. Elevation
is the angle above (positive angle)
or below (negative angle) the x-y
plane.

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – view function (cont.)
view
– MATLAB automatically selects a viewpoint that is determined by whether the plot
is 2-D or 3-D

•   For 2-D plots, the default is azimuth = 0° and elevation = 90°

• For 3-D plots, the default is azimuth = -37.5° and elevation = 30°

– view(2) sets the default 2D view, with az = 0, el = 90.

– view(3) sets the default 3D view, with az = –37.5, el = 30.

– view(az,el) or view([az,el]) set the viewing angle for a 3D view.

Scientific Visualization Using MATLAB - Spring 2011
Code – view function
Command Window: view1
samplefig;
view([-20,25]);

Things to try:
- Rotate graph (note scaling!)
- Examine values

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – axis function
axis
– Enables you to adjust the aspect ratio of graphs.

– Enables you to adjust the scaling of graphs.

– axis([xmin xmax ymin ymax zmin zmax]) sets the limits for the x-axis, y-axis and
z-axis of the current axes.

– axis vis3d freezes aspect ratio properties to enable rotation of 3-D objects (If you
will be interactively rotating the visualization in the figure window you should use
the vis3d option.)

Scientific Visualization Using MATLAB - Spring 2011
Code – axis function
Command Window: axis1
samplefig;
view([-20,25]);
axis([0 30 0 30 -15 15]);
axis vis3d;

Scientific Visualization Using MATLAB - Spring 2011
MATLAB - Lighting
Lighting
– Enhances the visibility of surface shape and provides a 3D perspective to your
visualization.

– Several commands enable you to position light sources and adjust the
characteristics of lit objects:
• light - creates a light object
• lighting - selects a lighting method
• material - sets the reflectance properties of lit objects
• camlight - creates or moves a light with respect to the camera position
• shading - controls the color shading of surface and patch graphic objects

Scientific Visualization Using MATLAB - Spring 2011
Code - lighting
Command Window:lighting1
samplefig;
light;
lighting phong;
material (‘shiny’);
camlight left;

Try changing
lighting, material,
etc., using command
or function syntax.
E.g.
material dull;
or
material(‘dull’);

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – Vis Algorithms
• Modeling
– Matrix to Surface
– Slicing

• Scalar
– Color Mapping
– Contours / Isosurfaces

• Vector
– Oriented Glyphs
– Streamlines

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – modeling algorithms
Matrix to Surface
– A surface is defined by the z-coordinates of points above a rectangular grid in the
x-y plane.

– formed by joining adjacent points with straight lines.

– useful for visualizing large matrices.

– surf(X,Y,Z) creates a shaded surface using Z for the color data as well as
surface height. X and Y are vectors or matrices defining the x and y components
of a surface.

Scientific Visualization Using MATLAB - Spring 2011
Code – surf function
Command Window:surf1
[X,Y] = meshgrid(-3:0.25:3);
Z = peaks(X,Y);
surf(X,Y,Z);
view(3);
axis([-3 3 -3 3 -10 10]);
grid on;
light;
lighting phong;
camlight left;

meshgrid(x,y) transforms the domain specified by vectors x
and y into arrays X and Y, which can be used to evaluate
functions of two variables and three-dimensional mesh/surface
plots. meshgrid(x) is the same as [X,Y] = meshgrid(x,x).

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – modeling algorithms
Slicing
– A slice is a "cross-section" of the dataset.

– Any kind of surface can be used to slice the volume.

– The simplest technique is to use a plane to define the cutting surface.

– slice (X,Y,Z,V,sx,sy,sz) draws slices of the volume V along the x, y, z directions
in the volume V at the points in the vectors sx, sy, and sz. X, Y, and Z are 3D
arrays specifying the coordinates for V.

– The color at each point is determined by 3-D interpolation into the volume V.

Scientific Visualization Using MATLAB - Spring 2011
Code – slice function
Command Window:slice1
[x,y,z,v] = flow;
xslice = 5;
yslice = 0;
zslice = 0;
slice(x,y,z,v,xslice,yslice,zslice);
view(3);
axis on;
grid on;
light;
lighting phong;
camlight('left');

The flow dataset represents the speed profile of a submerged jet
within an infinite tank (an example of scalar volume data).

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – scalar algorithms
Color Mapping
– Each scalar value in data set is mapped through a lookup table to a specific color.
– The color lookup table is called the colormap:
• Three-column 2-D matrix

• Each row of the matrix defines a single color by specifying three values in the
range of zero to one (RGB components).

• Created with either array operations or with one of the several color table
generating functions (jet, hsv, hot, cool, summer, and gray).

• colormap(map) sets the colormap to the matrix map.

Scientific Visualization Using MATLAB - Spring 2011
Code – colormap function
Command Window:colormap1
[x,y,z,v] = flow;
xslice = 5;
yslice = 0;
zslice = 0;
slice(x,y,z,v,xslice,yslice,zslice);
view(3);
axis([0 10 -4 4 -3 3]);
grid on;
colormap (flipud(jet(64)));
colorbar('vertical');

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – colormap editor
If you want even more control over the color
mapping, you can use the colormap editor.
You open the colormap editor by selecting
Colormap from the Edit menu of the figure.

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – scalar algorithms
Contours / Isosurfaces
– Construct a boundary between distinct regions in the data.

– contours are lines or surfaces of constant scalar value.

– isolines for two-dimensional data and isosurfaces for three-dimensional data.

– contour(X,Y,Z,v) draws a contour plot of matrix Z with isolines at the data values
specified in the vector v.

– isosurface(X,Y,Z,V,isovalue) computes isosurface data from the volume data V
at the isosurface value specified in isovalue. The isosurface function connects
points that have the specified value the same way isolines connect points of
equal elevation.
Scientific Visualization Using MATLAB - Spring 2011
Code – contour function
Command Window:contour1
[X,Y] = meshgrid(-3:0.25:3);
Z = peaks(X,Y);
isovalues = (-3.0:0.5:3.0);
contour(X,Y,Z,isovalues);
view(2);
axis on;
grid on;

Scientific Visualization Using MATLAB - Spring 2011
Code – isosurface function
Command Window:isosurface1
[x,y,z,v] = flow;
isovalue = -1;
purple = [1.0 0.5 1.0];
i = isosurface(x,y,z,v,isovalue);
p = patch(i);
isonormals(x,y,z,v,p);
set(p,'FaceColor',purple,'EdgeColor','none');
view([-10 40]);
axis on;
grid on;
light;
lighting phong;
camlight('left');

patch is the low-level graphics function that creates patch
graphics objects. A patch object is one or more polygons
defined by the coordinates of its vertices.

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – vector algorithms
Oriented Glyphs
– Draw an oriented, scaled glyph for each vector.
– Glyphs are polygonal objects such as a cone or an arrow.

– Orientation and scale of glyph indicate the direction and magnitude of the vector.

– Glyph may be colored according to vector magnitude or some other scalar value.

– coneplot(X,Y,Z,U,V,W,Cx,Cy,Cz) plots vectors as cones or arrows.
• X, Y, Z define the coordinates for the vector field
• U, V, W define the vector field
• Cx, Cy, Cz define the location of the cones in the vector field
• coneplot(...,'quiver') draws arrows instead of cones.

Scientific Visualization Using MATLAB - Spring 2011
Code – coneplot function
Command Window:coneplot1
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
zmax = max(z(:));
scale = 4;
[cx cy cz] = meshgrid(xmin:5:xmax,ymin:5:ymax,zmin:2:zmax);
coneplot(x,y,z,u,v,w,cx,cy,cz,scale,'quiver');
view([-35 60]);
axis on;
grid off;

The wind dataset represents air currents over North America. The dataset contains
six 3-D arrays: x, y, and z are coordinate arrays which specify the coordinates of
each point in the volume and u, v, and w are the vector components for each point in
the volume.

Scientific Visualization Using MATLAB - Spring 2011
MATLAB – vector algorithms
Streamlines
– The path a massless particle takes flowing through a velocity field (i.e. vector
field)
– Can be used to convey the structure of a vector field by providing a snapshot of
the flow at a given instant in time.

– Multiple streamlines can be created to explore interesting features in the field.

– Computed via numerical integration (integrating product of velocity times delta T).

– streamline(X,Y,Z,U,V,W,startx,starty,startz) draws stream lines from the vector
volume data.
• X, Y, Z define the coordinates for the vector field
• U, V, W define the vector field
• startx, starty, startz define the starting positions of the streams
Scientific Visualization Using MATLAB - Spring 2011
Code – streamline function
Command Window:streamline1
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
zmin = min(z(:));
zmax = max(z(:));
purple = [1.0 0.5 1.0];
[sx sy sz] = meshgrid(xmin,ymin:10:ymax,zmin:2:zmax);
h = streamline(x,y,z,u,v,w,sx,sy,sz);
set(h,'LineWidth',1,'Color',purple);
view([-40 50]);
axis on;
grid off;

Scientific Visualization Using MATLAB - Spring 2011
MATLAB - Resources
    IS&T tutorials
– Introduction to MATLAB
www.bu.edu/tech/research/training/tutorials/MATLAB/
– Using MATLAB to Visualize Scientific Data
www.bu.edu/tech/research/training/tutorials/visualization-with-MATLAB/

    Websites
– www.mathworks.com/products/MATLAB/
– www.mathworks.com/access/helpdesk/help/techdoc/index.html

    Wiki
– http://MATLABwiki.mathworks.com/

Scientific Visualization Using MATLAB - Spring 2011
Sources
   Getting Started with MATLAB, version 7, The MathWorks, Inc.

   Using MATLAB, version 7, The MathWorks, Inc.

   Using MATLAB Graphics, version 7, The MathWorks, Inc.

   http://www.mathworks.com/access/helpdesk/help/techdoc/index.html

Scientific Visualization Using MATLAB - Spring 2011
Questions?
   Tutorial survey:
- http://scv.bu.edu/survey/spring11tut_survey.html

Scientific Visualization using MATLAB – Spring 2011

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 19 posted: 2/21/2012 language: Latin pages: 48