[Insight-developers] [ITK + Python] Wrapping classes proposal - V.2

Benoit Regrain benoit.regrain at creatis.insa-lyon.fr
Wed Jun 22 11:14:50 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 
2 serious changes (presented later). 
It's actually made only on the Common part and for python

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 
   Module name : itkWrapping


Concerning this patch, I would now :
 - if ITK is interested by that, and integrate it
   (thus, I will continue and write the wrap_Xxx.cmake
   files for other ITK parts)
 - if there have conceptual or other problems with this proposal
   (for each of 2 points)
Thanks to ITK to answer me concerning their interest for
this proposal.

Cheers
Benoit Regrain



---> Presentation of changes :

1 - Use of CMake to generate wrapping :
------------------------------------------
All wrap_Xxx.cxx files 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 second point (Tree of
   templated classes)
 - The itkCSwigMacro.h and itkCSwigImage.h files are now useless...
   Thus, it's easier to wrap personal ITK classes without integrate them
   in the ITK folder tree.
 - 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.
In CSwig/Common : all wrap_Xxx.cxx files are replaced by 
wrap_Xxx.cmake when it's needed


2 - 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","I")
For specific internal processing, I will set the class name and the
mangling prefix (prefix found in the CSwig/ITKTypePrefix.cmake file).
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("US2",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["US2"] 
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 : itkcommonPy.py for the common part
Last, we need call the itkcommonPy module while the import of 
InsightToolkit package.
In the CSwig/Tests/Python directory, I added the 
testTemplate.py file to test the class creation 

The code to write the itkcommonPy.py file is in CSwig/WrapITK.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).



More information about the Insight-developers mailing list