Installing VTK on Mac OS X

Mac OS X is a great platform for 3D visualization. It includes OpenGL as standard, which means many existing libraries and applications can easily be ported to the platform. One such library is the Visualization Toolkit (VTK), an open source project run by Kitware, Inc. VTK is at a level above OpenGL: where OpenGL deals with simple polygons, VTK deals with isosurfaces, cut planes, and other 3D actors. It’s at a level useful to scientists, and relatively easy to learn.

VTK works great on the Mac, and even has built in support for Carbon and Cocoa. In the coming weeks, I will show you how to use VTK in Cocoa applications, but in this tutorial we will do a bit of preparation: we are going to compile and install VTK.

Name Your Sources

I want to begin by giving credit where it’s due: most of the information presented here has been stolen from a great piece on the VTK wiki by Ryan Glover. He covers building and installing VTK on the Mac for use with Cocoa, and I will follow his instructions fairly closely here, just adding my experiences where appropriate.

I also want to thank Marc Baaden before I get started, because this tutorial, and future ones, were largely inspired by him. He recently rewrote some code that I developed to demonstrate Cocoa and VTK integration many years ago, making it work on the latest versions of Mac OS X. We’ll meet that code in the next tutorial.

Installing CMake

VTK uses an autoconf like tool called CMake to configure the make files used to build VTK. So before you can build VTK, you need to install CMake.

Go to the CMake download site and locate the most recent binaries for your system. At the time of writing, the file was called cmake-2.4.6-Darwin-universal.dmg. Open the disk image, and double click the package to install.

Installing VTK5

Now to VTK itself. Go to the VTK download page and download the latest source files. I downloaded the vtk-5.0.3.tar.gz file. Decide where you would like to build VTK and unpack the sources there by double clicking the tar bundle. I choose to unpack the sources in the directory /Users/drew/Develop, and will use that path for the rest of this tutorial. After unpacking, the sources themselves were in /Users/drew/Develop/VTK.

Now we need to create a couple of directories: one is used for building, and one is for installing the VTK binaries. I created these directories as follows:

mkdir /Users/drew/Develop/VTKBuild
mkdir /Users/drew/Develop/VTKBin

To configure ready for building, change to the build directory, and run cmake passing the path to the VTK source root as argument.

cd /Users/drew/Develop/VTKBuild
cmake /Users/drew/Develop/VTK

This will generate a file called CMakeCache.txt in the VTKBuild directory; this file contains the configuration that will be used to build VTK.

The default configuration targets Carbon, rather than Cocoa, so we need to make a few changes. Use your favorite text editor to open CMakeCache.txt, and one by one, locate the lines shown below. Edit them as shown.

VTK_USE_CARBON:BOOL=OFF
VTK_USE_COCOA:BOOL=ON
CMAKE_INSTALL_PREFIX:PATH=/Users/drew/Develop/VTKBin

Ryan Glover also suggests changing the following line

CMAKE_OSX_ARCHITECTURES:STRING=ppc;i386

such that VTK is built to run universally, but when I tried it I got compile errors. If you are a daredevil, you might give it a try. Maybe by the time you read this, the problem will be fixed.

To update the build settings based on the changes you just made, rerun cmake from the VTKBuild directory. It will read the CMakeCache.txt file, and propagate the changes to the make files.

cmake /Users/drew/Develop/VTK

We are finally ready to compile. Set the following environment variable to target Tiger

export MACOSX_DEPLOYMENT_TARGET=10.4

and then enter

make -j 2

The -j option will run the build in parallel, so that if you have a multi-core Mac, things will go much faster.

Assuming that all goes well, the last step is to install the binaries in the VTKBin directory:

make install

With any luck, you should see that VTKBin contains include and lib directories, and you are ready to start using VTK in your Cocoa apps.

Comments

ccmake

When installing VTK I usually like using ccmake instead of cmake. This loads a 'graphical' interface rather than having to look for the fields in the cache text file.

I've been looking forward to the VTK tutorial since you started the scientific cocoa series! Thanks!

Tcl bindings

Looking very much forward to the rest on VTK, and to finally get my hands on CG after lots of low level struggling ;)

Which might save readers some time, is to include the mentioning of enabling
the tcl-bindings bool in the CMakeCache.txt. I just recompiled vtk, with the tcl-binding to be able to checkout some examples.

I know, you tend to write the article
based on Cocoa, but getting your hands on VTK goes rather intuitive in tcl as well imho.

EDIT : The default mac-tcl shell doesn't work out of the box with vtk-tcl scripts. Instead I found paraview : http://www.paraview.org/New/index.html, which does provide these bindings. But might be an overkill to your system if all you want to do is program vtk in cocoa.

Keep up the good work, drew!

Re: Tcl Bindings

Yes, I'll be mainly covering using VTK from Cocoa, but others may be interested in Tcl or Python bindings. These are very easy to enable, and allow you to use VTK from your scripts, rather than C++, which is not the most user-friendly of languages.

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

Nice article

Good article!

A few side notes:
1. You can have a easy way to install cmake if you are using MacPorts. Just do `sudo port install cmake'
2. The VTK Wiki entry (http://www.vtk.org/Wiki/Cocoa_VTK) also documented some steps and tips from building VTK to use it in MacOSX Xcode `the Mac way'.
3. Some my notes: http://www.evl.uic.edu/julian/mac/vtk/

python wrapping VTK, carbon - not in leopard

Hi,

nice article.
If you want more details about this topic you can look at the BioImageXD wiki at
http://wiki.bioimagexd.net/index.php?title=GettingStarted

We use VTK and ITK with python wrapping in BioImageXD
for ITK its a bit more complex as you need to use the Wrap ITK wrapping machanism.

also i hear that carbon will not be 64 bit.
thats bad news for us as we are using carbon...
so we will have to figure out how to use python wrapped cocoa VTK.. which wasnt working a while ago at least.

we are very interesting in 64 bit for out very large image datasets...
bought a 8 core mac pro for that job... so looking forward to being able to use all 16 Gb of RAM that are in it!

Got Tcl/Python bindings to work with Cocoa??

Hi,
I wonder whether anybody has managed to get Tcl/Python VTK bindings to work properly with Cocoa (instead of X11). I could compile it (as Drew described), but there are some "flaws" that I pointed out recently at [1] without ever getting an answer.

I'd appreciate if people could either confirm they have the same problems, or provide some details about compilation (I suspect some magic cmake options) if they got it to work.

Thanks in advance,
Marc

[1] http://public.kitware.com/pipermail/vtkusers/2007-May/091052.html
--
Marc Baaden -- http://www.baaden.ibpc.fr & http://baaden.free.fr/news/

sh versus tcsh

For fellow command line shortbus riders: The export command mentioned above is not recognized in a 'tcsh' or 'csh' shell. For me this problem was solved by simply typing "sh" into my Terminal window. The cursor line changed to "sh-2.05b$" and the export command was accepted.

I don't actually understand why any of this happened, but perhaps someone else will explain.

export vs setenv

There are two basic families of shell: ones derived from the Bourne shell, and ones derived from the C shell. bash is a Bourne Shell, and tcsh is a C Shell.

In Bourne shells, you use export to set an environment variable. In C shells, you use setenv

setenv SOME_VAR /path/to/something

The reason it all worked when you entered 'sh' was that sh is a Bourne shell: you started it up, and from then on, it interpreted commands entered.

Hope that helps.

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

VTK also available through paraview

Paraview is a visualization project built on top of VTK. When I built paraview, VTK came with it; perhaps that is another way to get VTK. I didn't have any trouble building paraview from cvs, although it took a while to compile everything. (If you want just paraview there is an installer for a binary; then I guess VTK wouldn't be accessible.)

http://www.paraview.org/HTML/Index.html

V. Gregory Weirs
Sandia National Laboratories

vtknetcdf/ncx.c: ix_float_implementation" error when building VT

I followed the instruction described in the tutorial to build VTK 5.0.3 on Mac OS X using CMake 2.4.6.

First I changed the “VTK_USE_CARBON:BOOL=OFF VTK_USE_COCOA:BOOL=ON” in the file CMakeCache.txt, and run cmake /users/../VTK, I got a message "Cannot determine repository type. Please set UPDATA_TYPE to 'cvs' or 'svn'. CTest update iwll not work". I just ignored it and run the command make.

Then I got such errors:

Linking CXX static library ../bin/libvtkWidgets.a

[ 86%] Built target vtkWidgets

Scanning dependencies of target vtkNetCDF

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/var.o

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/attr.o

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/dim.o

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/error.o

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/libvers.o

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/nc.o

[ 86%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/ncio.o

[ 87%] Building C object Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/ncx.o

/users/jl/develop/VTK/Utilities/vtknetcdf/ncx.c:1120:2: error: #error "ix_float implementation"

make[2]: *** [Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/ncx.o] Error 1

make[1]: *** [Utilities/vtknetcdf/CMakeFiles/vtkNetCDF.dir/all] Error 2

make: *** [all] Error 2

dhcp-39pool-08:/users/jl/develop/VTKBuild jli023$

And the building stopped.

Any idea about it?

Thanks you in advance,

Jing

Python (maybe other scripting?) and Cocoa w/ VTK

Apparently, the Cocoa and shared lib or python wrapper settings are incompatible... so for those of you hoping to do cocoa python VTK, you may need to wait until a later date!

For now, I get fine results with carbon=ON (cocoa=OFF), as well as building the python wrappers.

Re: Python and Cocoa w/VTK

You can do it if you build twice:

First build the cocoa library as described in this great article.

Second use ccmake to change the configuration and build the python wrapper with carbon=on, cocoa= off as you describe in your post.

You will end up with all what you need to use VTK with Python or Cocoa

Error building with python wrapper - help?

Hi,

I'm trying to build VTK for use with python (using carbon after reading the comments on here, although I get identical issues with cocoa).

I have shared libraries, carbon and python wrapper turned on, apart from which I followed the instructions above. The error output is below.

I'd be incredibly grateful for any assistance!

Thanks!

Phil

[ 98%] Built target CommonCxxTests
[ 98%] Built target TestCxxFeatures
[ 98%] Built target TestInstantiator
[ 98%] Built target FilteringCxxTests
[ 99%] Built target GraphicsCxxTests
[ 99%] Converting Tcl test
Converting /Users/Phil/Desktop/vtk-5/Graphics/Testing/Tcl/capSphere.tcl
Conversion failed!
make[2]: *** [Graphics/Testing/Python/conversion_complete] Error 1
make[1]: *** [Graphics/Testing/Python/CMakeFiles/GraphicsPythonTests.dir/all] Error 2
make: *** [all] Error 2

To those who are suffering

To those who are suffering the compilation error: invalid conversion from 'GLint*' to 'long int*' .
Try CVS VTK codes, lots of fixes have been added for Mac support since last official release.

Fix for those who are suffering

In the file vtkCocoaRenderWindow.mm, change "int pfd" to "GLint pfd".

VTK 5.0.3 vs VTK CVS

If you are using VTK on Mac OS X, you really should not use VTK 5.0.3. Do yourself a favour and use the latest code from CVS. As others have said, 5.0.3 needs (minor) code changes to even compile on 10.5. That should be a big hint than _running_ the code was not tested. Also, 5.0.3 cannot be built Universal, it has all sorts of endianess problems. In summary, don't bother with 5.0.3 or earlier. :)

CMAKE_INSTALL_PREFIX

With recommended CMAKE_INSTALL_PREFIX:PATH=/Users/drew/Develop/VTKBin it doesn't work. It finished with error:

> [100%] Built target ViewsCxxTestsInstall the project...
> -- Install configuration: ""
> CMake Error at cmake_install.cmake:31 (FILE):
> file INSTALL destination: ~/Development/VTKBin/lib/vtk-5.4 is not a
> directory.
>
>
> make: *** [install] Error 1
> confero-4s-imac:VTKBuild confero_4$

If I tried it with default CMAKE_INSTALL_PREFIX:PATH=/usr/local/ it works!

Martin