[Insight-developers] CastImageFilter

Joshua Cates cates@cs.utah.edu
Fri, 22 Mar 2002 10:29:25 -0700 (MST)


Luis is right,  SetFunctor will not work in this case.

It looks like the functionality I was looking for is already in
itkUnaryFunctorImageFilter.  But maybe a more correct way to approach this
is, as Jim suggests, to require the cast operator be defined on the data
type.  I see no clear advantage of any one method over the other, so I
think it's just a question of figuring out what is easiest for potential
users.

Josh.


______________________________
 Josh Cates			
 School of Computer Science	
 University of Utah
 Email: cates@cs.utah.edu
 Phone: (801) 587-7697
 URL:   www.cs.utk.edu/~cates


On Fri, 22 Mar 2002, Luis Ibanez wrote:

> 
> Jim,
> 
> Maybe I'm missing something...please correct me if I'm wrong.
> 
> The Functor type is defined at compile time using the third parameter
> of the Unary/Binary/Ternary filters.  This same Functor type is expected
> by  the Set/Get Functor methods:
> 
>  - SetFunctor( Functor & );
>  - Functor & GetFunctor();
> 
> These two methods allow to manage Functors that have ivars and
> facilitate to users to set this Ivars.   For example, Thresholding can
> be done very easily with a functor with line 46 =
> 
>     return   A >  m_Threshold ? 0 : 1;
> 
> here, the user may want to set the m_Threshold value at will.
> But the Functor itself is always of type "ThresholdingFunctor".
> 
> We cannot pluggin a "RGBtoFloatRedFunctor" here because the
> type will not fit the SetFunctor() method signature.  It is not like
> passing a pointer to a base class for calling a virtual function.
> 
> It is a bit missleading because it has almost the same API as an
> approach of type:
> 
>   SetFunctor( FunctorBaseClass * );
>   where new functor classes oveload a virtual function "Evaluate()".
> 
> The advantage of the current templated approach is that the function
> is fully resolved at compile time and the code of line 46 end up
> being inlined (at least when optimization is on ).
> 
> The polymorphic approach, on the other hand, will cost a virtual call
> per pixel which is probably too much for a simple operation like
> casting.
> 
> 
>     Luis
> 
> 
> 
> =====================================
> 
> Miller, James V (CRD) wrote:
> 
> >Luis,
> >
> >Why doesn't SetFunctor() work?
> >
> >I think this should work for the same reason that you can set the comparitor used in
> >std::priority_queue
> >
> >Jim
> >
> >
> >-----Original Message-----
> >From: Luis Ibanez [mailto:luis.ibanez@kitware.com]
> >Sent: Friday, March 22, 2002 9:54 AM
> >To: Miller, James V (CRD)
> >Cc: 'Joshua Cates'; Insight-Developers
> >Subject: Re: [Insight-developers] CastImageFilter
> >
> >
> >
> >Josh, Jim,
> >
> >
> >Miller, James V (CRD) wrote:
> >
> >>I think you can use the CastImageFilter as it is:
> >>
> >>1) As long as a cast operator is defined for converting RGB to itk::Vector, it
> >>should work out of the box.
> >>
> >
> >That's right, the basic castings are all covered by this filter.
> >That is: 
> >all the C/C++ normal castings and, as Jim already said, all those involving
> >classes for which the cast operator to the output class is defined.
> >
> >>2) You can call SetFunctor() on the CastImageFilter (defined in UnaryFunctorImageFilter) to set the
> >>functor to anyone you would like.
> >>
> >There is no "SetFunctor()" in this case.
> >The Functor has to be defined as a type at compile time.
> >It is not set at run time.
> >
> >>There is no real problem adding the template parameter. Just not sure whether 
> >>it is really needed to accomplish what you want.
> >>
> >
> >Adding another template parameter to CastFilter will make it equal to 
> >the UnaryFilter.  
> >
> >If some fancy casting is needed the easiest way to obtain it is to copy 
> >the file itkCastingFilter.h
> >into  "myCastingFilter" and redefine the "Functor" that is already 
> >inside. That will require to
> >modify only one line of code: Line 46 where the current conversion is 
> >defined as:
> >
> >   return  static_cast< TOutput >( A );
> >
> >Let's say casting from RGB to float by extracting only the Red component 
> >will be done
> >with:
> >
> >    return static_cast< float >( A[0] );         or
> >    return static_cast< float >( A.GetRed() );
> >
> >
> >The whole file is 87 lines  so it is almost to the point were in can be 
> >managed by a macro
> >that creates the whole filter is we provide : (1) Filter name  (2) code 
> >for line 46
> >
> >
> >Luis
> >
> >
> >
> >
> 
> 
>