[Insight-developers] RE: FunctionBase

Joshua Cates cates@cs.utah.edu
Tue, 18 Sep 2001 12:59:17 -0600 (MDT)


Hi Damion,

Perhaps it's just restating the obvious, but maybe this perspective will
help.

The logic of constness and object state should follow fairly naturally
from the abstract definition of a "function object" and what it is
intended to represent to a user. Specifically, if a user expects the
various parameters that she/he sets to equate to constants in the function
expression, then the user should be guaranteed that an atomic evaluation()
of the function expression will not change those values.  The const tacked
onto evaluate() explicitly makes this guarantee to the user.

For example, If I'm evaluating ax+b, a and b should not change as a
result. Similarly the value x is passed through a const parameter
( evaluate(const int x) const {...} )  so that it is guaranteed not to be
modified as a result of the function call.

Another factor to consider is thread safety. The constness of the
evaluate() function means that it is safe to call evaluate() on the same
object from different threads. Take a look at itkFiniteDifferenceEquation
(soon to be itkFiniteDifferenceFunction) and how it is used in the
FiniteDifferenceImageFilter hierarchy as an example.


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 Tue, 18 Sep 2001, Damion Shelton wrote:

> Hmmm...
> 
> Actually, what I'm doing is the following:
> 
> There are six variables which must be set prior to evaluating the function. 
> These are:
> 
>   InputType m_Origin;
>   TGradientType m_OriginGradient;
>   double m_DistanceMin;
>   double m_DistanceMax;
>   double m_Epsilon;
>   bool m_Polarity;
> 
> What was causing the problem was:
> 
>   // Normalize the origin gradient
>   m_OriginGradient.Get_vnl_vector().normalize();
> 
> Which I fixed by changing the code to:
> 
>   TGradientType originGradient = m_OriginGradient;
> 
>   // Normalize the origin gradient
>   originGradient.Get_vnl_vector().normalize();
> 
> So, I'm operating on a local copy (local to the Evaluate function) rather 
> than on the class member variable. I suppose the more correct way to do 
> this would be to normalize m_OriginGradient in the SetOriginGradient 
> function, rather than at evaluation time. Is this true?
> 
> -Damion-
> 
> 
> --On Tuesday, September 18, 2001 11:34 AM -0400 "Miller, James V (CRD)" 
> <millerjv@crd.ge.com> wrote:
> 
> > Damion,
> >
> > It looks like in some of your code you are trying to cache intermediate
> > values calculated during a call to Evaluate() as ivars of your function.
> > You then allow the user to access these value using a GetMacro().  Of
> > course this violates the "const" designation of your function.
> >
> > I suggest that you do not cache these values.  Instead, change your
> > GetMacro()'s to actual methods that calculate the value in question.  So
> > Evaluate() does not change your object.  The methods you'll add also will
> > not modify your class but rather calculate the value needed.
> >
> > Jim
>