[Insight-developers] SimpleITK - Operator overloading in wrapped languages

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Sat Apr 2 11:16:22 EDT 2011


Le 1 avr. 11 à 23:15, Bradley Lowekamp a écrit :

> Gaetan,
>

Hi Brad,

> I have begun implementing and overload operators for the wrapped  
> languages.  You can see here my recent changes on this topic:
>
> https://github.com/blowekamp/SimpleITK/compare/master...48-OperatorsForC++ 
> #diff-2
>
> I am wondering what you think of this approach?

This is great, I like it!
This is a really great idea which fits very well in SimpleITK.

I think you should also implement the "r" versions of those methods --  
__radd__, __rsub__, ... -- so the user can do

> The in-place operators are currently just place holders until we  
> have inplace filter operations. Basically, each overloaded operator  
> maps to a procedural call which maps to a iITK filter. It not clear  
> how we would take advantage of your current gerrit contribution. I  
> am open to suggestions here.

The question is more on the SimpleITK generator: is it able to  
generate the code to take a constant as the first parameter?
If yes, for the overloaded operators, I guess the only difference in  
the c++ part would be that
   inline Image &operator/( const Image &img1, const Image &img2 )  
{ return Divide(img1, img2 ); }
   inline Image &operator/( const Image &img1, double s ) { return  
Divide(img1, s ); }
would be replaced by
   inline Image &operator/( const Image &img1, const Image &img2 )  
{ return Divide(img1, img2 ); }
   inline Image &operator/( const Image &img1, double s ) { return  
Divide(img1, s ); }
   inline Image &operator/( double s, const Image &img1 ) { return  
Divide(s, img1 ); }

In python, you only have to replace
   def __add__( self, other ):
      if isinstante( other, self ):
         return Add( self, other )
      return AddConstantTo( self, other )

by
   def __add__( self, other ):
      return Add( self, other )
   def __radd__( self, other ):
      return Add( other, self )

And it would be possible to do

   img10 = img / 10

as well as

   img10 = 10 / img

Gaëtan

-- 
Gaëtan Lehmann
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
http://voxel.jouy.inra.fr  http://www.itk.org
http://www.mandriva.org  http://www.bepo.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 203 bytes
Desc: Ceci est une signature ?lectronique PGP
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20110402/c8e165f7/attachment.pgp>


More information about the Insight-developers mailing list