Resource Menu


by Arnaud Trouche - (no comment)

Objectives

As introduced in my previous article, we have decided to use the OSGi framework at Artenum as the basis of our modular projects.

I have already written about how to embed VTK, the C++ open source Visualization Toolkit, in an OSGi bundle for Mac OS X. The purpose of this new article is to detail how to do it on Linux. As I already detailed how JNI and OSGi work with native libraries in my previous article, I will not go through these points again.

This article is divided in two parts:

  • 1) Compiling VTK, a detailed explanation of how to compile VTK on Linux
    • a) Hardware and operating system
    • b) Prerequisites
    • c) Compiling VTK with Java wrapping
  • 2) Embedding VTK in an OSGi bundle, describing Linux specific constraints encountered.
    • a) Prerequisites
    • b) Embedding VTK in an OSGi bundle

1) Compiling VTK

a) Hardware and operating system

For this article, I have used a clean Ubuntu 10.10 64 bits distribution installed on my MacBook Pro 3.1 with 2.2GHz Intel Core 2 Duo processors. At installation, I have selected the option to automatically download the updates during installation. When I first started opened a session, I was thus proposed to install all updates, including Embedded GNU C Library (libc6 version 2.12.1) that is required to compile VTK.

b) Prerequisites

Here are the prerequisites to compile the VTK library:

  • Sun JDK 6: to install Sun JDK, use the command lines below.
> sudo add-apt-repository "deb http://archive.canonical.com/ubuntu maverick partner"
> sudo apt-get update
> sudo apt-get install sun-java6-jdk

The check that the install was successful with the command:

> java -version
  • g++ to compile C++ code:
> sudo apt-get install g++
  • OpenGL:
> sudo apt-get install libgl1-mesa-dev
  • X11 toolkit intrinsics library (development headers):
> sudo apt-get install libxt-dev
I have created a vtk-5.6.1-src directory in my root folder and unzipped the VTK archive there (beware: unzipping does not create a VTK directory but extract all data in place…). I have then created a build directory inside this source directory
  • CMake, the cross-platform make, used to generate makefiles to compile VTK. To install it, use the command lines below.
> sudo apt-get install cmake-curses-gui

c) Compiling VTK with Java wrapping

We are now ready to compile VTK.

First, create a vtk-5.6.1 directory in your home folder. This is where VTK will be installed.

Then go to the ~/vtk-5.6.1-src/build directory and type:

> ccmake -i ..

A basic graphical user interface will be displayed in the terminal, as shown in figure below:

Type c to configure CMake. It will look for the compilers installed on the machine, the JVM path, etc.

You will then have to select the compilation options, the install directory, the build type, etc. Here is the configuration I used (in bold are the important elements to check):

BUILD_EXAMPLES                                         OFF
BUILD_SHARED_LIBS                                 ON
BUILD_TESTING                                            OFF
CMAKE_BACKWARDS_COMPATIBILITY   2.4
__CMAKE_BUILD_TYPE                                   Release__
CMAKE_INSTALL_PREFIX                           /home/atrouche/vtk-5.8.0
VTK_DATA_ROOT                                          VTK_DATA_ROOT-NOTFOUND
VTK_EXTRA_COMPILER_WARNINGS      OFF
VTK_LARGE_DATA_ROOT                          VTK_LARGE_DATA_ROOT-NOTFOUND
VTK_USE_CHARTS                                      ON
VTK_USE_GEOVIS                                        ON
VTK_USE_INFOVIS                                       ON
VTK_USE_N_WAY_ARRAYS                       ON
VTK_USE_PARALLEL                                  ON
VTK_USE_QT                                                 OFF
VTK_USE_RENDERING                              ON
VTK_USE_TEXT_ANALYSIS                        OFF
VTK_USE_VIEWS                                          ON
VTK_WRAP_JAVA                                          ON
VTK_WRAP_PYTHON                                   OFF
VTK_WRAP_PYTHON_SIP                          OFF
VTK_WRAP_TCL                                           OFF

Type c again to validate those settings (you may have to do it twice) and then g to generate the makefiles.

Sometimes, other flags may be necessary (they are accessible by typing t in ccmake):

  • depending on the machine and what you have already installed on, you may have to indicate the correct compiler by modifying the two following lines (with your path to g++/gcc):
CMAKE_CXX_COMPILER            /usr/bin/g++ 
CMAKE_C_COMPILER                 /usr/bin/gcc
  • if you have an error when typing c such as "JAVA_AWT_INCLUDE_PATH NOT FOUND", you have to define the java following flags (here is an exemple I used with Java 8, path may vary depending on the java version you have, check every line). Also, it is a good way to check that cmake is using the correct java version (seeing the path it found):
JAVA_AWT_INCLUDE_PATH             /usr/lib64/jvm/java/include
JAVA_AWT_LIBRARY                           /usr/lib64/jvm/java/jre/lib/amd64/libjawt.so
JAVA_INCLUDE_PATH                        /usr/lib64/jvm/java/include
JAVA_INCLUDE_PATH2                     /usr/lib64/jvm/java/include/linux
JAVA_JVM_LIBRARY                            /usr/lib64/jvm/java/jre/lib/amd64/server/libjvm.so
Java_IDLJ_EXECUTABLE                  /usr/lib64/jvm/java/bin/idlj
Java_JARSIGNER_EXECUTABLE    /usr/lib64/jvm/java/bin/jarsigner
Java_JAR_EXECUTABLE                   /usr/lib64/jvm/java/bin/jar
Java_JAVAC_EXECUTABLE              /usr/lib64/jvm/java/bin/javac
Java_JAVADOC_EXECUTABLE        /usr/lib64/jvm/java/bin/javadoc
Java_JAVAH_EXECUTABLE              /usr/lib64/jvm/java/bin/javah
Java_JAVA_EXECUTABLE                 /usr/lib64/jvm/java/bin/java

You can now build VTK with the command line:

> make -j 2

Where the "-j 2" option indicates the number of processors on which you want the compilation to be performed This step might take a while (up to one or two hours depending on your configuration).

Finally, you have to install the shared libraries to your installation directory with the command line:

> make install

(If you chose another directory than the one I suggested in your home folder, you might need to use sudo to install VTK).

2) Embedding VTK in an OSGi bundle

a) Prerequisites

  • The sample project provided here are built with Maven. To install it, use the command line:
> sudo apt-get install maven2
It is preferable to download Eclipse from Eclipse website than with Ubuntu Software Centre, which provides an outdated version of Eclipse. You should also install m2eclipse plugin to import and manage Maven projects with Eclipse.
  • Finally, to run the OSGi Framework and execute the VTK bundle, you will need an implementation of the framework. Two popular open-source implementations are Equinox and Felix.

b) Embedding VTK in an OSGi bundle

The OSGi bundle embedding VTK for Linux is very similar to the one in my previous article. You can download the project (with the compiled VTK libraries) here: test-osgi-vtk-linux64b.zip.

In this project, we have created two classes:

  • org.test.vtk.osgi.TestVTK is the same JFrame that contains the method start() to initialize a vtkPanel containing the 3D cone. It also contains the stop()method called when the bundle is stopped.
  • org.test.vtk.osgi.Activator is the BundleActivator class that will be called by OSGi when the bundle is started or stopped. It statically loads the VTK libraries one by one, in the correct order of their dependencies, as shown below:
static {
	System.loadLibrary("vtkproj4");
	System.loadLibrary("vtkalglib");
	System.loadLibrary("vtksys");
	System.loadLibrary("vtkCommon");
	System.loadLibrary("vtkFiltering");
	System.loadLibrary("vtkexpat");
	System.loadLibrary("vtkjpeg");
	System.loadLibrary("vtkzlib");
	System.loadLibrary("vtklibxml2");
	System.loadLibrary("vtktiff");
	System.loadLibrary("vtkpng");
	System.loadLibrary("vtksqlite");
	System.loadLibrary("vtkmetaio");
	System.loadLibrary("vtkNetCDF");
	System.loadLibrary("vtkDICOMParser");
	System.loadLibrary("vtkNetCDF_cxx");
	System.loadLibrary("vtkIO");
	System.loadLibrary("vtkImaging");
	System.loadLibrary("vtkverdict");
	System.loadLibrary("vtkGraphics");
	System.loadLibrary("vtkfreetype");
	System.loadLibrary("vtkftgl");
	System.loadLibrary("vtkGenericFiltering");
	System.loadLibrary("vtkRendering");
	System.loadLibrary("vtkexoIIc");
	System.loadLibrary("Cosmo");
	System.loadLibrary("VPIC");
	System.loadLibrary("vtkParallel");
	System.loadLibrary("vtkHybrid");
	System.loadLibrary("vtkWidgets");
	System.loadLibrary("vtkVolumeRendering");
}

We can execute the bundle in Equinox, for instance:

# First we delete the cache folder contraining previously loaded bundles
rm -rf configuration/org.eclipse.osgi configuration *.log
# Then we execute Equinox with the argument -Xcheck:jni to see JNI-related errors
java -Xcheck:jni -jar org.eclipse.osgi_3.6.1.R36x_v20100806.jar -console
osgi> install file:/home/ben/workspace/test-osgi-vtk-linux64b/target/test-osgi-vtk-linux64b-1.0-SNAPSHOT.jar
osgi> start 1

We obtain the result illustrated in figure below:

At some point in my attempts to embed VTK in a bundle on Linux, I have had an UnsatisfiedLinkError where the JVM was looking for the shared library libmawt.so. This is a know bug of the JVM (http://bugs.sun.com/view_bug.do?bug_id=6539705) that I fixed by adding the path to the library in the LD_LIBRARY_PATH.

export LD_LIBRARY_PATH=/usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/i386/xawt

In the version of the bundle that is provided above, this phenomenon however did not occur anymore. I don't really know what I did to fix it, but I will not complain as far as it works . I let this workaround anyway in case someone one day encounters the problem.

Conclusion

As we have seen, we have successfully created a VTK OSGi bundle for Linux.

We have reviewed in detail the prerequisites of the compilation of VTK on Linux and explained the compilation process. We also provided an example of a VTK bundle running on Linux 64bits.



posted by Benoît Thiébault at Nov 10, 2010 2:33 PM
Quote
Please leave your comments here



Last edited by null at - Edit content - View history - View source