[Insight-developers] [ITK + Wrapping] Wrapping classes proposal

Benoit Regrain benoit.regrain at creatis.insa-lyon.fr
Thu Sep 1 08:01:03 EDT 2005


Hi,

Following some mails on the subject :
   [ITK + Python] Wrapping classes proposal

I have worked on the ITK wrapping. Then, I have made 
3 serious changes (presented later). 
It's actually made to the current CVS version of ITK

All of this work can be found on the Creatis CVS server :
   CVSROOT : :pserver:anonymous at cvs.creatis.insa-lyon.fr:2402/cvs/public
   password  : anonymous
   Repository module name : itkWrapping

This contains a README file to explain changes to do to use my work

I hope ITK will be interested by this work and will integrated it in a next 
ITK version.

Cheers
Benoit Regrain




---> Presentation of changes :

1 - Use of CMake to generate wrapping :
------------------------------------------
All wrap_Xxx.cxx files, concerning the ITK classes wrapping,
are directly generated by CMake.
All wrap_XxxLang.cxx files, concerning the module wrapping
are directly generated by CMake

For :
 - There is no enought C++ macros (that are numerous and
   copied between some files... ex : All Transform classes wrapping)
 - All classes are wrapped with consistent rules. The mangled 
   name is then coherent between all classes
 - It simplifies the integration of the two next points (Wrap an own class
   and Tree of templated classes)
 - The itkCSwigMacro.h and itkCSwigImage.h files are now useless...
 - Best support for future ITK addons
Against :
 - The mangled name isn't kept
 
The write of these files is made using CMake macros. These macros
are in CSwig/WrapITK.cmake
The CSwig/WrapType*.cmake files are help  for the basic types and
advanced types creation. These types are used define the template
classes.


2 - Wrap an own class :
----------------------------------

The first change tails this second point : all files are generic-ly created. 

The project to create the corresponding libraries and files to a module
is placed in a generic macro, generic in the sens that this macro independant
to ITK.

This macro and all used macros are placed in the CSwig/itkConfigWrapping.cmake
file.

So, if an user want wrap its own ITK class... or extent the wrapping of an existing
ITK class, it must include the itkConfigWrapping.cmake and WrapITK.cmake
files and use the corresponding macros to his work.

In the itkWrapping CVS repository, there is an example to simply wrap my own
class itkImageNothingFilter (easy filter that do nothing on the image). This 
example is completed with a Python test file.



3 - Tree of templated classes :
-------------------------------
The goal is to have a simplest and generic use of templated ITK classes.
Consider this python example :
   def write( itkImg, file ) :
      # typedef itk::ImageFileWriter< itkImg::Self > writerT
      writerT = itkImageFileWriter[ itkImg ]
      writer = writerT.New()
      writer.SetInput( itkImg )
      writer.SetFileName( file ) 
      writer.Update()

This function will write an image (itkImg) in a file.
But the writer creation is dependant to the itkImg type. 
The goal is : offer the possibility to the programmer to 
instanciate an ITK class without before known of the itkImg
type.

To have it, I will create a new class instance of 
itkPyTemplate type while the wrapping of the ITK classes.
So, I will have : 
   itkImage = itkPyTemplate("itkImage")
For specific internal processing, 
The itkPyTemplate class can be used like a python dictionnary

Next, I will call the set method on this new class instance to add 
different templates :
   itkImage.set("unsigned short,2",itkImageUS2)
First parameter is the template type used as a key and the second
parameter is the definition corresponding to the key.

So, I can write : 
   itkImage[itkUS,2] 
to get the itkImageUS2 class.
After, I call the New method on this class and I get a class instance.

The code of the itkPyTemplate class is at CSwig/Python/itkPyTemplate.py
The code to generate these classes is written by CMake. It's
in the resulting file : itkcommonaPy.py for the common part
Last, we need call the itkcommonaPy module.
All of these created python modules are imported in the itkPython.py file
so, the user only needs to import the itkPython module use this advance
In the CSwig/Tests/Python directory, I added the 
testTemplate.py and testTemplateUse.py files to test the class creation 

The code to write the itkcommonaPy.py file is in CSwig/WrapITKLang.cmake
This code contains the WRITE_LANG_WRAP macro that will
write these additionnal python classes

This solution is actually only in python but it can be implemented for
Java (I don't know if Tcl has classes... but a similar solution may be
found).
This patch can be removed by remove the code contained only in the macros 
in the CSwig/WrapITKLang.cmake file. But I think that patch is a usefull advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.itk.org/mailman/private/insight-developers/attachments/20050901/5827aa44/attachment.htm


More information about the Insight-developers mailing list