VTK/Using JRuby

From KitwarePublic
< VTK
Jump to navigationJump to search

Introduction

JRuby is an implementation of a Ruby interpreter in the Java language. JRuby makes Java classes available for use from the Ruby language, and this includes natively-implemented Java classes, such as the Java bindings for VTK. This page provides some information about using VTK with JRuby via its Java bindings.

This page is just a quick introduction to document initial work in this area. It is intended to be extended as time progresses. Please contribute!

Setup

VTK Java Bindings

Initially, you will need to compile VTK with Java bindings enabled. Make sure you have the Cone.java tutorial example (available in the Examples/Tutorial/Step1/Java directory) working before proceeding.

TODO: Add more information on compiling the Java bindings.

CLASSPATH and Shared Library Setup

You will need to make sure that your CLASSPATH environment variable includes vtk.jar, otherwise JRuby will not be able to find any of the Java bindings for VTK. You will further need to ensure that your environment is set up so that Java can find any of the shared libraries required for the Java bindings. Under the *nix environment, this is typically achieved by setting the LD_LIBRARY_PATH environment variable.

Example Code

The following example is a translation of the Cone.java tutorial example to run in JRuby. If you have the Java bindings for VTK compiled, and your JRuby CLASSPATH and shared libraries are properly configured then this example should run.

#
# This example creates a polygonal model of a cone, and then renders it to
# the screen.  It will rotate the cone 360 degrees and then exit.  The basic
# setup of source -> mapper -> actor -> renderer -> renderwindow is typical
# of most VTK programs.
# 
# Modified for JRuby from the Java Step1 tutorial example.
# 
# Jonathan Merritt, January 2006.
# 

# We include Java to begin with, so that JRuby can utilise the Java bindings
# to VTK.
require 'java'

# Load JNI libraries via the java.lang.System class.  The libraries must be
# in your path to work.
System = java.lang.System
System.loadLibrary 'vtkCommonJava'
System.loadLibrary 'vtkFilteringJava'
System.loadLibrary 'vtkIOJava'
System.loadLibrary 'vtkImagingJava'
System.loadLibrary 'vtkGraphicsJava'
System.loadLibrary 'vtkRenderingJava'

# Ruby likes its classes to start with capital letters, so here we include
# all required Java classes with capital letters.  There may be a better
# (or at least automated) way to do this.
include_class('vtk.vtkConeSource')     { 'VTKConeSource' }
include_class('vtk.vtkPolyDataMapper') { 'VTKPolyDataMapper' }
include_class('vtk.vtkActor')          { 'VTKActor' }
include_class('vtk.vtkRenderer')       { 'VTKRenderer' }
include_class('vtk.vtkRenderWindow')   { 'VTKRenderWindow' }

# Create an instance of vtkConeSource, and set some of its parameters.
# Ruby doesn't require parentheses around single-argument methods.
cone = VTKConeSource.new
cone.SetHeight     3.0
cone.SetRadius     1.0
cone.SetResolution 10.0

# Map the cone polygon information into graphics primitives.
cone_mapper = VTKPolyDataMapper.new
cone_mapper.SetInputConnection cone.GetOutputPort

# Create an actor to represent the cone.
cone_actor = VTKActor.new
cone_actor.SetMapper cone_mapper

# Create the renderer and assign actors to it.
ren1 = VTKRenderer.new
ren1.AddActor cone_actor
ren1.SetBackground(0.1, 0.2, 0.4)

# Finally, create the render window that will show up on the screen.
ren_win = VTKRenderWindow.new
ren_win.AddRenderer ren1
ren_win.SetSize(300, 300)

# Now we loop over 360 degrees and render the cone each time.
360.times do
  ren_win.Render                   # Render the image
  ren1.GetActiveCamera.azimuth 1   # Rotate active camera by 1 deg
end

Gotchas

Some important notes:

  1. Use java.lang.System.LoadLibrary() to load the native libraries required by VTK's Java bindings.
  2. Ruby likes its classes to start with capital letters. Hence, when importing VTK classes, make sure you change their names so that they start with a capital letter.



VTK: [Welcome | Site Map]