[Insight-developers] reinterpret_cast : asking for help

Luis Ibanez luis.ibanez at kitware.com
Thu Feb 19 10:31:44 EST 2009


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 ?

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.

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 ?


   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
> 
> 
> 
> 


More information about the Insight-developers mailing list