MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


3-D Data Visualization on Mac OS X

by Michael J. Norton
06/28/2002

I like knowing that my little 450MHz G4-powered Internet browser is also capable of computational fluid dynamics. My grassroots are in computational physics. I learned the craft of programming for the sole purpose of designing incredulous space engines that would take mankind to the farthest reaches of the stars.

That was a great idea years ago in college, but I soon discovered my vision was not keenly embraced by real-world employers. Don't get me wrong; I never gave up on my vision--I just got older and hopefully wiser. I still wanted to investigate the possibilities of computational physics and space travel. Yet, at the same time I wanted to be lazy and script it in Tcl--and on my Mac. My searches to the ends of the Internet revealed a remarkable package called the Visualization Toolkit, or VTK, which satisfied all my criteria.

Visualization

Users and developers of the VTK make a distinction between computer graphics and visualization. Visualization is the process of transforming and mapping data into graphic primitives. The computer-graphics end handles rendering and displaying. An example of visualization would be the scanning of points on a human skull and entering these data points into a computer. The visualization is shown in Figure 1.

Figure 1
Figure 1. Visualization of the human skull using VTK.

The image in Figure 1 was in fact generated by VTK. So, what is VTK used for? There is a wide range of applications for which VTK is being implemented. Medical imaging is one place you'll find this tool being put to extensive use. Data points collected from an MRI scan can be transformed into a visual image using VTK. Weather-map visualization is another place you'll find VTK at work. Researchers are evening applying the VTK to virtual reality test beds. Engineers are using VTK to extract components from CAD drawings to produce visualizations of their products. The VTK is an extensive and continuously growing, feature-rich software tool for data visualization.

Now, I think I have your attention. The next question you may be asking is how do I get this software up and running on my Mac OS X supercomputer? Well, I thought you'd never ask.

Related Reading

Developing Bioinformatics Computer Skills
By Cynthia Gibas, Per Jambeck

The CMake Utility

The Visualization Toolkit has the flexibility to support cross-platform environments such as Unix and Windows. The software is open source and can operate in multiple development environments, both compiled and interpreted. Each build platform has its own dependencies and directory hierarchies. The solution to these complexities is the CMake utility. Downloading the latest CMake from CVS for Unix, you can configure CMake to run on SGI platforms, Solaris, Linux, and FreeBSD.

The Mac OS X kernel, Darwin, is a Mach kernel based on FreeBSD. CMake can be configured to install software for both Mac OS X, using Carbon-based calls or Darwin using XFree86. Under the Mac OS X umbrella, C++ (compiled), Python (interpreted), and Tcl (interpreted) are supported.

Retrieving CMake from CVS

The CMake utility is available from www.cmake.org. Download the latest src tree from CVS. You can do this from your Mac OS X terminal application. If you're a GNU Emacs for Aqua user then you should use the emacs shell (option-x shell). The emacs shell isn't a necessary step for installation but it sure makes life easier when you have to scroll back through the make logs to locate errors in the build process. I installed the CMake utility in my root directory.

cd /

From your shell, type the following to retrieve the latest source for CMake:

cvs -d :pserver:anonymous@www.cmake.org:/cvsroot/CMake login

The password is cmake. That will log you into the CVS. Now you'll need to check out the source tree:

cvs -d :pserver:anonymous@www.cmake.org:/cvsroot/CMake co CMake

Once you've completed the CVS source tree update you're ready to install the CMake utility.

Installing the CMake Utility

There are three simple steps for installing CMake. First, from your Unix shell run the configure utility. You will need to move into the CMake directory.


cd  /CMake
./configure

You'll see a lot of "checking and creating" messages scroll by as the configure utility sets up the environment and Makefiles. Once the configure utility completes, you need to run the make utility. The make utility must be run from the /CMake directory.

make

After invoking the make utility, you will see messages regarding the compilation steps. Once this completes successfully you'll need to run "make install" as a privileged user. The easiest way to do this is to use the sudo command. You will be prompted for the superuser password.


sudo make install
password:

When the make install completes with no errors, the CMake utility installation will be completed. The next objective is to install the VTK software. VTK relies on CMake, which is why you had to install it first.

Mac OS X Installation of VTK

Now you will need to download the latest software revision of VTK. First you will want to create two directories. The first directory is /Develop.


mkdir /Develop
cd /Develop

Inside the /Develop directory, create the following directory:

mkdir /Develop/osxBin

The reasoning behind this is that the VTK directory will contain the source code and the osxBin directory will be the location where we create our VTK builds. The recommendation from the VTK experts is to keep these directories separate. Now you need to make sure you are in the /Develop directory for CVS VTK software download.

cd /Develop

Again, we will use CVS to retrieve the software and build the source tree. You will start by logging into the CVS system:

cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK login

The password is VTK. Once you have logged in you may proceed with checking out the source code tree. Make sure you are in the directory you wish to install the VTK directory. I installed VTK in the root directory.


cd /
cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK checkout VTK

Quick note: If you're a developer you can frequently update your source VTK source tree with the following CVS command:

cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTK update -dAP

Once the VTK source tree is installed you need to run CMake from inside the /Develop/osxBin directory.


cd /Develop/osxBin
cmake ../VTK

This invocation of CMake will set up VTK for a build and it will generate a /Develop/osxBin/CMakeCache.txt file. We will need to edit the /Develop/VTK/CMakeCache.txt file for the CMake utility to build our Mac OS X-specific VTK. Use any Unix editor utility that is familiar to you. I prefer to use Emacs and vi. Mac OS X has an editor called pico included with the base operating system installation. You can use pico if you don't have gnu emacs or you don't want to bother with the hassle of the vi editor.

pico CMakeCache.txt

You need to add to CMakeCache.txt the following configurations:


// OS X CMake build VTK with shared libraries
BUILD_SHARED_LIBS:BOOL=ON

The key sequence CTRL-X exits pico. Once you have made these changes save the /Develop/VTK/CMakeCache.txt file and change your directory back to your /Develop/osxBin directory. Run CMake.


cd /Develop/osxBin
cmake ../VTK

Once the CMake utility is successfully completed run the make utility from the /Develop/osxBin directory.

make

The make utility will execute for some time. This may be a good time for a bathroom break or to go make a sandwich. When the make utility finally completes the VTK software installation is done.

My G4 450 executed the VTK make in about 40 minutes. While you're waiting you may also wish to go ahead and CVS check out the VTK image data.


cd /Develop
cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTKData login
password: vtk
cvs -d :pserver:anonymous@public.kitware.com:/cvsroot/VTKData co VTKData

The VTK Data directory contains files and images that allow you to run the VTK demomstrations.

Simple Sanity Check for VTK

Now that the VTK software installation is completed you may want some sort of instant gratification for your hard work. Try running the TimeRenderer and TimeRender2 examples in the bin directory.


cd /Develop/osxBin/bin
./TimeRenderer

Both the TimeRendered and TimeRenderer2 examples will execute on their own for a few seconds and then abort. Don't panic when you see the abort message. This is exactly how the example programs should behave. You should see,


%./TimeRenderer2
Wall Time = 8.35666
FrameRate =14.3598
TriRate = 1.4879e+06

For a real quick-and-dirty test run the Mace test in the following manner:

./Mace -I &

This simple test will generate the image in Figure 2.

Figure 2
Figure 2. Sample output of the Mace test program.

For a more interesting test, edit the Mace.cxx source code. First go over to the source code directory for Mace and make a backup of the source code. Then edit the source code for Mace.cxx.


cd /Develop/VTK/Graphics/testing/Cxx
cp Mace.cxx Mace.cxx.bak
emacs Mace.cxx

The emacs editor is provided for example only. Use whatever text editor you're accustomed to. Now make the following changes to the Mace.cxx source code:


if (retVal == vtRegressionTester::DO_INTERACTOR)

{

	iren->Start();

}

Remove the conditional statement, so the code reads as follows:

iren->Start();

You will then need to recompile the source. Be sure you change directories back over to the /Develop/osxBin/Graphics/testing/Cxx. We want to leave the original VTK directories in tact.


cd /Develop/osxBin/Graphics/testing/Cxx
sudo make
password:

This will take a minute or two (on a Macintosh G4 450). Then go back to the VTK/bin directory and execute the Mace program.


cd /Develop/osxBin/bin
./Mace

Click your mouse inside the window to interact with the Mace program. Typing the letter 'q' exits the application. If you're playing around with the Mace application you have successfully installed the VTK software.

Setting Up VTK for Tcl

Now that you've had some fun with VTK, let's get back to business and finish setting up VTK to run with Tcl. Before I forget, let's set the environment variable for Tcl to find the VTK packages. You'll need to determine which Unix shell you are using. If you're not sure, use the following command from your terminal prompt:

echo $SHELL

I am using the tcsh - C shell. So I would edit my tcsh resource file to include this environment variable:


emacs ~/.tcshrc
setenv TCLLIBPATH "/Develop/osxBin/Wrapping/Tcl"
setenv VTK_ROOT_DATA "/Develop/VTKDATA"

Close and save the file (emacs CTRL X-C) and source the file to update the changes made. The environment variable VTK_ROOT_DATA tells VTK where to find the image examples we'll be using.

source ~/.tcshrc

Getting the Tcl/Tk Frameworks Files for Mac OS X

A special disk image file is available from the atamai Web site. Download the Tcl/Tk Frameworks. This image is required for the Carbon Mac OS X build.

Editing the /VTK/CmakeCache.txt File

We will need to edit the /VTK/CmakeCache.txt file once more. First of all, we need to turn on the Tcl wrapping functionality. Edit the /Develop/osxBin/CMakeCache.txt and search for the string "VTK_WRAP_TCL". Change the default setting from:

VTK_WRAP_TCL:BOOL=OFF

to

VTK_WRAP_TCL:BOOL=ON

Also insert the following definitions into the /Develop/osxBin/CMakeCache.txt file.

      
// set path to the tcl 0header files
TCL_INCLUDE_PATH:PATH=/Library/Frameworks/Tcl.framework/Headers

// set path to Tcl libraries
TCL_LIBRARY:FILEPATH=-framework Tcl

// set to the tclsh file.
TCL_TCLSH:FILEPATH=/Applications/Wish\ Shell.app/Contents/
MacOS/Wish\ Shell // set the path to the tk.h header file TK_INCLUDE_PATH:PATH=/Library/Frameworks/Tk.framework/Headers // set the path to the Tk libraries TK_LIBRARY:FILEPATH=-framework Tk

Once you have completed these changes, you need to run CMake and then make again.

cd /Develop/osxBin cmake ../VTK make

Moment of Truth

The installation and builds are now complete. Try running a simple demo from the /Develop/osxBin/bin directory.


cd /Develop/osxBin/bin

./vtk -f /Develop/VTK/Examples/Modelling/Tcl/expCos.tcl

Figure 3 displays the Bessel function generated by VTK. Pretty impressive, huh?

Figure 3
Figure 3. Running the expCos.tcl Bessel function example.

Another interesting test to run to verify installation is the VisualizationAlgorithms/Tcl directory script, bandContourterrain.tcl.

./vtk -f /Develop/VTK/Examples/VisualizationAlgorithms/Tcl/
bandContourterrain.tcl

Figure 4
Figure 4. Running the bandContourterrain.tcl example.

The VTK is now installed on your system. Now the fun and the learning curve begins!

Visualization on the Mac

I guess a whole new world has been opened up to you. Now what? To completely embrace the power of VTK, I opted to pick up the accompanying book, The Visualization Toolkit, An Object-Oriented Approach to 3D Graphics, 2nd Edition, by Schroeder, Martin, and Lorensen. The book isn't light reading--it's a textbook. If you're using the Visualization Toolkit this is a must-have. I have it open on my lap while I am looking through old MatLab source code trying to figure out how to port it over to VTK. There is a learning curve but the toolkit is well worth the effort.

Some truly impressive work with VTK is being carried out in medical research. Yves Starreveld, the key Mac OS X developer for VTK, and the person to whom I owe a great deal in helping me out, has his Web site at www.atamai.com. At Starreveld's site computer science meets brain surgery. Pretty cool! Sebastian Barre has another interesting site using VTK for biomedical engineering.

As for me, there are still some projects in computational fluid dynamics I would like to explore. Well, I guess it's time to see if the G4 is really worthy of its supercomputer reputation.

Michael J. Norton is a software engineer at Cisco Systems.


Return to the Mac DevCenter

Copyright © 2009 O'Reilly Media, Inc.