[Insight-developers] reinterpret_cast : asking for help

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Thu Feb 19 13:28:15 EST 2009


Le 19 févr. 09 à 16:31, Luis Ibanez a écrit :

>
> Hi Gaetan,
>
> Thanks for the clarification.
>
> Wouldn't it be better then to have the KernelImageFilter be
> templated over the Kernel type, just as we do with the
> UnaryFunctorImageFilter ?

It is, and that's a part of the problem :-)

>
> Doing casts trying to guess, (or hope) that the actual classes
> are of a certain type is not consistent with the principles
> of Generic Programming.
>

Right. It was made that way to be able to use that filter as a base  
class for the filters already in ITK, without breaking the backward  
compatibility.

> In this way, then the KernelImageFilter, simply needs to
> access the common API of all the valid "Kernel" classes
> that you listed:
>
>  * Neighborhood
>  * BinaryBallStructuringElement
>  * FlatStructuringElement
>
>
> Do you think this could be done here ?

It would be possible for those classes, but would break the build with  
any other custom classes a user may have defined.
I guess the backward compatibility policy doesn't allow us to do that  
- we can also talk about breaking it in a future ITK release ;-)

Don't you think that the overloaded templated/non templated methods  
(the patch I provided with the last mail), or the use of a specialized  
helper function would be good enough?

Gaëtan

>
>
>
>  Luis
>
>
> -----------------------
> Gaëtan Lehmann wrote:
>> Le 18 févr. 09 à 04:51, Luis Ibanez a écrit :
>>>
>>> Hi Gaetan,
>>>
>>> Could you help us understand the use of "reinterpret_cast" in the
>>> itkKernelImageFilter.txx file ?
>>>
>>> Currently it generates a warning, and... further reading about
>>> the reinterpret_cast lead us to think that it shouldn't be used
>>> in this case (e.g. to convert high level classes to others).
>>>
>>> The preceding test of typeid() comparisons may also, not be
>>> performing the expected task. Please note that in the Events
>>> we end up using dynamic_cast in order to check whether one
>>> class was actually derived from another.
>> Hi Luis,
>> The reinterpret_cast was used to avoid a warning - it seems to not  
>> be  as successful has expected...
>> KernelImageFilter has to work with any kind of kernel -   
>> itk::Neighborhood, BinaryBallStructuringElement, ..., and   
>> FlatStructuringElement.
>> FlatStructuringElement is a bit special, because it can't be  
>> simply  filled as the other structuring element to produce the  
>> best  performances in some circonstances: for a box structuring  
>> element, we  have to call Box() to produce a decomposable kernel  
>> suitable for the  van Herk / Gil Werman / anchor filters. The  
>> method SetRadius(), where  the reinterpret_cast is, produce a box  
>> structuring element, and so it  calls FlatStructuringElement::Box()  
>> to get the best performance when  possible.
>> The problem is to make that build without warning with any type of   
>> structuring element used to instantiate the KernelImageFilter.
>> An example: the user instantiate KernelImageFilter with   
>> BinaryBallStructuringElement< float, 3 > as template parameter. In   
>> that case, the user defined structuring element has no super class  
>> in  common with FlatStructuringElement< 3 > required to call Box(),  
>> and  thus it becomes impossible to make a static_cast build, and  
>> impossible  to make a dynamic cast build without warning like this  
>> one:
>> /Users/glehmann/src/Insight/Code/Review/itkKernelImageFilter.txx: 
>> 50:  warning: dynamic_cast of ‘itk::FlatStructuringElement<2u>  
>> flatKernel’  to ‘class itk::BinaryBallStructuringElement<float,  
>> 2u,  itk::NeighborhoodAllocator<float> >*’ can never succeed
>> The reinterpret_cast let the code build without warning (here),  
>> and  the type check is there to be sure that something nasty is done.
>> Explicit specialization of a method is not allowed in a non   
>> specialized class, and I guess partial specialization is still not   
>> usable in ITK to be able to build with msvc6. There is still the   
>> overloaded templated/not templated method workaround, but I'm not  
>> sure  how it works on all the supported compilers - see attached  
>> patch.
>> The last option would be to use a specialized helper function.
>> Those options seemed to have less chance to build everywhere, at  
>> the  time I've implemented that, so I preferred the  
>> reinterpret_cast way.  But as you get warnings, it may be better to  
>> try the other ones!
>> 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.mandriva.org
http://www.itk.org  http://www.clavier-dvorak.org

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


More information about the Insight-developers mailing list