[Insight-developers] Removing typeids, removing block-copied code, UseVectorBasedHistogram

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Fri Nov 5 10:23:16 EDT 2010


Le 5 nov. 10 à 15:16, kent williams a écrit :

> This has to do with bug report http://public.kitware.com/Bug/view.php?id=7006
>
> I did a find/grep for typeid and eliminated the ones in I/O  
> statements.
>
> One of the things I found was this member function  
> UseVectorBasedHistogram, which was in 4 different places:
>
> ./Code/Algorithms/itkAnchorErodeDilateLine.h:  bool  
> UseVectorBasedHistogram()
> ./Code/Algorithms/itkAnchorOpenCloseLine.h:  bool  
> UseVectorBasedHistogram()
> ./Code/Review/itkMaskedRankImageFilter.h:  bool  
> UseVectorBasedHistogram()
> ./Code/Review/itkRankImageFilter.h:  bool UseVectorBasedHistogram()
>
> This was implemented exactly the same in all classes, but the  
> RankImageFilter classes returned true only for one-byte types:
>
>   bool UseVectorBasedHistogram()
>    {
>      // bool, short and char are acceptable for vector based  
> algorithm: they do
>      // not require
>      // too much memory. Other types are not usable with that  
> algorithm
>     return typeid( InputPixelType ) == typeid( unsigned char )
>            || typeid( InputPixelType ) == typeid( signed char )
>            || typeid(InputPixelType) == typeid(unsigned short)
>            || typeid(InputPixelType) == typeid(signed short)
>            || typeid( InputPixelType ) == typeid( bool );
>     }
>
> I replaced these functions with this template/overloaded function  
> version:
>
>    template <class PixelType>
>    static bool UseVectorBasedHistogram(PixelType &) { return false; }
>    static bool UseVectorBasedHistogram(unsigned char &) { return  
> true; }
>    static bool UseVectorBasedHistogram(signed char &) { return true; }
>    static bool UseVectorBasedHistogram(unsigned short &) { return  
> true; }
>    static bool UseVectorBasedHistogram(signed short &) { return  
> true; }
>    static bool UseVectorBasedHistogram(bool &) { return true; }
>    bool UseVectorBasedHistogram()
>    {
>    InputImagePixelType p;
>    return Self::UseVectorBasedHistogram(p);
>    }
>
> But there are two problems with this — it duplicates identical code  
> in different files, and I don’t believe I’ve done an optimal  
> implementation.
>
> In the first matter, I believe there should be a common class to  
> encapsulate this information. I’m not sure what to call such a class  
> or where to put it.
>
> In the second matter, there should be a way with specialization  
> where it’s boiled down to a template class w/partial specialization  
> that boils it down to a boolean constant with no member function  
> necessary.  I’m just not getting my head around how to do this this  
> morning.
>
> Advice wanted. Thanks!

What about waiting for that change to be merged in ITK repository?

   http://review.source.kitware.com/#change,293

It should remove all the runtime type comparisons listed above. Just  
let me know if I've missed one!

Regards,

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/20101105/fe36f261/attachment.pgp>


More information about the Insight-developers mailing list