ITK/Scripting: Difference between revisions

From KitwarePublic
< ITK
Jump to navigationJump to search
(Created from antonym)
 
No edit summary
Line 54: Line 54:


Java is a modern object-oriented language.
Java is a modern object-oriented language.
== How to wrap additional classes ==
Given that ITK is based on Generic Programming, most of the C++ classes are implemented as templates. Therefore, their final type is not defined until the actual template is  instantiated with specific template arguments. This generality makes difficult to automatically wrap ITK classes for scripting languages such as Tcl and Python. The solution adopted by ITK is to use the powerful Cable-Swig platform and to select specific instantiation for the most commonly used types.  In practice it is impossible to wrap all ITK classes, simply because the combinations of types will result in an very large number of classes.
It is then likely that you may find that your favorite class is not being wrapped...
What to do in that case ?
You have two independent lines of action:
A) Write an email to the users-list and request the developers to wrap the class that you need.
B) Wrap the class yourself by following the instructions in the remaining part of this section.
Since we have learned that programmers are great at learning-by-example, the following section will teach you how to wrap a new class by walking step-by-step through the process of wrapping a new class. The subject of our example is the itkOtsuMultipleThresholdCalculator, simply because at the time of writing this text there was a request pending for adding this class to the wrapping, so we thought that by writing these instructions we will solve two problems simultaneously :-)

Revision as of 01:46, 15 November 2004

ITK itself is written in C++, however a tool called CableSwig can be used to generate bindings for other languages. This allows you to use the same runtime (running in optimised C++ code) but develop in a scripting language (and take advantage of all the attendant benefits).

The following languages are supported via the bindings:

In order to build the bindings, you will need to:

  1. Build and install CableSwig (more info)
  2. Ensure you have installed both the runtime and the development versions of your chosen language
  3. Build ITK using the appropriate settings for your platform, and enable the CABLE_* option for your language
  4. Install ITK as per normal
  5. Test!

Tcl Bindings

Tcl (Tool Command Language) is a very simple language for writing scripts.

Python Bindings

Python is a very elegant and powerful language.

Here is some example Python code for ITK (taken from `Insight/Examples/Filtering`):

#!/usr/bin/env python
 
# BinaryThresholdImageFilter.py
 
from InsightToolkit import *
 
from sys import argv
 
reader = itkImageFileReaderUS2_New()
writer = itkImageFileWriterUS2_New()
 
filter  = itkBinaryThresholdImageFilterUS2US2_New()
 
filter.SetInput( reader.GetOutput() )
writer.SetInput( filter.GetOutput() )
 
reader.SetFileName( argv[1] )
writer.SetFileName( argv[2] )
 
filter.SetLowerThreshold( eval( argv[3] )  )
filter.SetUpperThreshold( eval( argv[4] )  )
filter.SetOutsideValue(   eval( argv[5] )  )
filter.SetInsideValue(    eval( argv[6] )  )
 
writer.Update()

Java Bindings

Java is a modern object-oriented language.

How to wrap additional classes

Given that ITK is based on Generic Programming, most of the C++ classes are implemented as templates. Therefore, their final type is not defined until the actual template is instantiated with specific template arguments. This generality makes difficult to automatically wrap ITK classes for scripting languages such as Tcl and Python. The solution adopted by ITK is to use the powerful Cable-Swig platform and to select specific instantiation for the most commonly used types. In practice it is impossible to wrap all ITK classes, simply because the combinations of types will result in an very large number of classes.

It is then likely that you may find that your favorite class is not being wrapped...

What to do in that case ?

You have two independent lines of action:

A) Write an email to the users-list and request the developers to wrap the class that you need.

B) Wrap the class yourself by following the instructions in the remaining part of this section.


Since we have learned that programmers are great at learning-by-example, the following section will teach you how to wrap a new class by walking step-by-step through the process of wrapping a new class. The subject of our example is the itkOtsuMultipleThresholdCalculator, simply because at the time of writing this text there was a request pending for adding this class to the wrapping, so we thought that by writing these instructions we will solve two problems simultaneously :-)