VRMLplot.m

VRMLplot is a Matlab function for generating interactive 3-D VRML 2.0 graphs and animations. It generates output files which may be viewed using a WWW browser with a VRML 2.0 plugin.

To create new animations using VRMLplot you'll need a copy of Matlab but to view the output, you need only have Netscape and a VRML 2.0 compatible plug-in such as SGI's free CosmoPlayer.

Some example worlds created using VRMLplot are now on-line.

NOTE - there are known bugs in some browsers which may prevent them from working with these files - if you have difficulty see section 2.



1. Using VRMLplot to create output files

Using VRMLplot is no more complicated than Matlab's built-in plotting functions yet the resulting output is fully interactive. You can walk on parametric surfaces, fly over a Gaussian or take a roller-coaster ride on a sine wave.

1.1 Plots


plot.wrl.gz

plot.wrl
Call VRMLplot with 3 vectors containing lists of x, y and z coordinates to generate a 3-D graph. For example:

>> t=[0:0.2:6]; x=sin(2*t); y=cos(t); z=sin(t);
>> VRMLplot(x,y,z)
ans=
out.wrl

This causes a VRML file to be created and written to the file out.wrl - assuming you have a VRML 2.0 compliant plugin, you can view the resulting file using netscape:

>> !netscape out.wrl &

(The ampersand may not work on non-unix machines). Its not necessary to restart netscape for each new graph, just leave it running and hit reload after creating each new graph.

1.2 Surfaces

surf.wrl.gz
surf.wrl
VRMLplot can also handle surfaces. Pass it a matrix of heights and it will create a surface plot:

>> Z=peaks;
>> VRMLplot(Z)
ans=
out.wrl

Pass it three matrices, containing X,Y and Z coordinates and it will plot a parametric surface:

>> [X Y Z] = peaks;
>> VRMLplot(X,Y,Z)
ans=
out.wrl

VRMLplot will automatically break the surface up into patches, compute colors and normals and generate multiple levels of detail. If you wish you can also set colors manually by specifying a fourth matrix containing intensity values (between 0 and 1) for each vertex.

Since the generated files (particularly surface plots) can be fairly large you should probably take the time to compress them. For example, surf.wrl takes 500K while surf.wrl.gz (created by using gzip) consumes less than 130K.

1.3 Animations

anim.wrl.gz
anim.wrl
To animate motion of a point over time, simply append a list of times to those for the x,y and z coordinates. For example:

>> t=[0:0.1:6]; x=sin(2*t); y=cos(t); z=sin(t);
>> VRMLplot(x,y,z,t)
ans=
out.wrl

To animate motion of a vehicle or articulated figure you'll need a matrix containing values for the animation and a wrl file to describe the thing you wish animated.

Here's an example which animates a two-link robot. First we'll create a matrix containing values for time and each of the two rotating joints, then we'll give that matrix and a model of a two link robot arm to VRMLplot:

robot.wrl.gz
robot.wrl
>> L=[ 0 0 0; 1 pi/2 0; 2 pi/2 pi/2; 4 0 0 ]
L =
         0         0         0
    1.0000    1.5708         0
    2.0000    1.5708    1.5708
    4.0000         0         0

>> VRMLplot(L,'animation=robotTemplate.wrl')
ans =
out.wrl
The resulting animation shows a two link robot manipulator which starts in the zero configuration, rotates joint 1 by pi/2 radians, then rotates joint 2 by pi/2 radians and then finally moves back to the starting position.

The first column of the input matrix always represents time. Subsequent columns may currently represent either translations or rotations. VRMLplot parses the input model file to determine the mapping between values in the input matrix and motion of the animated model.

Here's another example, this time we'll animate the motion of an object which can translate and rotate in any 3-D Cartesian direction. Once again we create a matrix describing the desired motion. In this case the matrix columns contain time,x,y,z translations and the rotation about the z,y and x axes (in radians).

vehicle.wrl.gz
vehicle.wrl
>> L=[ 0  0  0  0  0    0    0;
       1  0  0  1  0    0    0;
       2  0  1  1  0    0    0; 
       3  1  1  1  0    0    0; 
       4  1  1  1  pi/2 0    0; 
       5  1  1  1  pi/2 pi/2 0; 
       6  1  1  1  pi/2 pi/2 pi/2; 
       8  0  0  0  0    0    0];
>> VRMLplot(L, 'animation=vehicleTemplate.wrl')
ans =
out.wrl

The resulting file animates motion of an object (in this case a set of axes) which moves one unit along each axis and then rotates by pi/2 radians about each axis before returning to the starting position. Note that the order in which the translations/rotations are applied to the vehicle changes the way in which it moves. That ordering is defined within each input model file.

For the above examples, motions were deliberately chosen to fall within the default axis sizes. In most cases you'll need to manually set min/max values for each axis so they are appropriate for your particular model and animated motion.

Both the included template files are well commented so you should not have too much difficulty modifying then to create your own models. Much more complex worlds are of course possible, for example you could have an input file describing several vehicles, each of which had several manipulators attached to it.

If you wish to animate several vehicles or manipulators you also have the option of animating each one separately and appending the animations together (see the section 1.5 for more details).

1.4 Customizing the output

In addition to vectors or matrices, VRMLplot also accepts a string of options which you can use to customize the appearance of the resulting graphs. These options all take the general form:

'option1=value1; option2=value2; ... optionN=valueN'

For example, the options xlabel, ylabel and zlabel can be used to change the axis labels:

>> VRMLplot(x,y,z,'xlabel=X axis; ylabel=Y axis; zlabel=Z axis')

Note that the value of an option may contain spaces. Remember to use semicolons and not commas to separate options.

Here's a complete list of options currently supported:

Option Description
animation use to set the name of a VRML file defining a model which is to be animated - see section 1.3 for details.
append Append allows multiple graphs to be combined together. See the next subsection for more details.
color0
color1
Use to manually specify surface colors. By default color1 (corresponding to the highest vertices on the surface) is a light grey, while color0 (corresponding to the lowest points) is a very dark grey. Setting colors requires a three element matrix containing red, green and blue values, each of which is in the range 0 to 1. e.g. to pseudocolor a plot from blue (lowest) to red (highest) use:
VRMLplot(Z,'color0=[0 0 1]; color1=[1 0 0]')
linecolor
meshcolor
modelcolor
spotcolor
Use to specify the color of lines, surface meshes, animated models and spots.Setting colors requires a three element matrix containing red, green and blue values, each of which is in the range 0 to 1. e.g. to produce a plot with blue lines and red spots use: VRMLplot(Z,'linecolor=[0 0 1]; spotcolor=[1 0 0]')

(Both American and Britsh spellings of color/colour are accepted).

mesh Enables or disables mesh lines on surface plots. Set mesh=1 to add grid lines (default is 0).
labelsize use to manually set the height of text labels on the graph. In most cases the system should automatically choose a suitable value.
linesize use in plots to manually set the size of the lines to use in the graph. At this time only sizes of 0 and 1 are supported. To plot points without connecting lines set linesize=0.
shadow Enables or disables shadows. Set shadow=0 to remove shadows (default is 1).
spotsize use in plots to manually set the size of the spots used to represent each point in the graph. In most cases the system should automatically choose a suitable value. To generate line graphs without spots, set spotsize=0.
text If you are on a slow machine or using an old browser it may be advantageous to turn off the text labels on the graph axes. Do this by setting the 'text' option to 0.
title use to set a title for the graph. Depending on your browser this may appear somewhere in the window border when your VRML file is displayed.
tscale By default the system animates one unit of time in one real second. Setting the tscale option allows you to stretch or compress time. For example, tscale=10 causes one unit of time to be animated over 10 real seconds.
xlabel
ylabel
zlabel
tlabel
use to set the text under the x,y,z and time axes.
xmax
ymax
zmax
tmax
use to set the maximum value for each axis. In most cases the system should automatically choose suitable values.
xmin
ymin
zmin
tmin
use to set the minimum value for each axis. In most cases the system should automatically choose suitable values.
xstep
ystep
zstep
tstep
use to set the step size along each axis. In most cases the system should automatically choose suitable values.
xscale
yscale
zscale
use these to set an absolute scaling factor between units along the graph axes and global coordinates within the VRML world. By default the system will either scale each dimension so the resulting graph fits within a 1 meter cube within the VRML world or, in the case of animated worlds, it will choose a scaling factor of unity. By explicitly setting a scale you can override this behavior. For example, setting xscale=1,yscale=1,zscale=1 will cause a world to be created where one unit along any axis is represented by 1 meter within the VRML world. Setting scales of 0.001 will cause each unit to be represented by 1mm within the VRML. In many cases changes in scale won't be obvious since the system recomputes viewpoints based on the scale values you choose.

1.5 Combining multiple plots

multiPlot.wrl.gz
multiPlot.wrl
The append option allows multiple graphs to be combined on the same axes. For example, here we combine several animated plots:

>> t=[0:0.1:6]; x=sin(2*t); y=cos(t); z=sin(t);
>> VRMLplot(x,y,z,t);
>> VRMLplot(x,y*0.9,z,t*0.95,'append=1');
>> VRMLplot(x,y*0.8,z,t*0.9,'append=1')
ans=
out.wrl

Note that all user interface components (the graph axes and time bar) are created only for the first graph. Thus if you wish to append any animations you should create an animation as the very first world and then append other plots/animations to it. Also, if you have several graphs with widely varying values you'll probably want to manually specify min, step and max axis values for the first graph so that the axes are appropriate for all appended graphs.

By default all the appended graphs will use the same axes and scaling factors so points which have the same values, even if on different graphs, will always appear at the same spot.

2. Viewing output files created with VRMLplot

VRMLplot generates VRML 2.0 files. Some graphs will contain text, extrusions, elevationGrids and make use of VRMLscript. Thus, to view these files you'll need a fully VRML 2.0 compatible browser.

To check if your browser is compatible try some of the on-line examples from Hartman and Wernecke's VRML 2.0 Handbook. If you can view those then you'll probably also be able to view VRMLplot's output files. If you see text in place of a VRML world then you probably don't have a VRML plug-in installed.

If you get a blank screen then you're probably using either a VRML 1.0 viewer or an old version of CosmoPlayer.

If you get a warning dialog referring to "just" then you're probably using a beta version of CosmoPlayer which does not support text.

If the axes don't change when you select different viewpoints then your viewer may not support VRMLscript.

On SGI machines, using Netscape 3.0 and CosmoPlayer 1.0 you should see a screen similar to this:

Try choosing different viewpoints and notice how the axes rearrange themselves. With animations, try choosing the "view behind spot" viewpoint and then clicking on the spot to start the animation.

3. Changes and Additions

Added in version 1.2.1

  • Minor changes to remove warnings when using Matlab 5.0.

Added in version 1.2

  • Automatic fragmentation and level-of-detail generation for surface plots - this gives greatly improved performance on very large surfaces.
  • Also added two levels of detail for points in line graphs as well as fragmentation of lines to improve performance and support graphs with more than 1024 data values.
  • Ability to manually alter the color of graph elements.
  • Option of adding grid lines and changing the coloring of surface plots.
  • Error checking and warning messages are further improved.
  • Fixed a number of minor bugs - a trailing semicolon is now permitted in the options list, multiple graphs may now also be combined.

Added in version 1.1

  • Ability to animate complex models (previously you could only animate motion of a point over time).
  • Ability to append multiple animations (previously you could only have one animation per world, now the number of animations is unlimited).
  • The system is much smarter when appending multiple graphs - in particular it automatically parses existing files to determine suitable scale factors and axis values for each appended graph.
  • Error checking and warning messages are improved.
  • Generated files now contain additional spaces around numeric values (some VRML browsers had difficulty parsing the compact code produced previously).

4. Known bugs and limitations

This version has only been tested using Matlab 4.2/5.0 and Netscape 3 with CosmoPlayer 1.0 on an SGI machine. This code is the result of only a few weeks experience with VRML.

The beta SGI versions of CosmoPlayer will probably not work with these files - upgrade to the full release version 1.0.

The beta PC versions of CosmoPlayer do not yet fully implement the VRML spec and will thus may be unable to display some of these files correctly.

5. Downloading VRMLplot

This early version of VRMLplot, written while standard VRML 2.0 browsers are still evolving, is provided for your use in hopes of obtaining feedback for future implementations - we welcome reports of problems and suggestions for improvements. You are welcome to copy and distribute this version provided that all files in the distribution are included. However, you may not alter it, include it in another product or distribute it in a commercial product without the prior written consent of the author. Provided free of charge, it comes with no warranty of any kind.

Here's a tar'ed and gzipped copy of the code - along with VRMLplot.m it contains several other .m files (which VRMLplot calls) as well as some example animation templates.

Current version (tested on Matlab 5.0): VRMLplot.1.2.1.tar.gz

Previous version (tested on Matlab 4.2c): VRMLplot.1.2.tar.gz

Dr Craig Sayers
The Deep Submergence Laboratory
Woods Hole Oceanographic Institution
Woods Hole MA 02543, U.S.A.

http://www.dsl.whoi.edu/~sayers/VRMLplot


webmaster@dsl.whoi.edu, last update 13 Jan 1997 , © 1996,97