[Insight-users] ImageFunction Evaluate constness

Luis Ibanez luis.ibanez at kitware.com
Sun Dec 13 07:04:45 EST 2009


Hi Dan,

              Interesting dilemma...

The short answer is:     Yes,

You are abusing the Functor.    :-)


Given that the functor is, by design, intended
to perform pixel-wise stateless operations.


However,
your algorithmic needs are certainly legitimate,
and therefore we should find a way of making
easy to implement your algorithm.


One option that comes to mind is the design
used in the pairs of LevelSet Function+Filter.

In particular the use of a data structure that
is own by the filter and it is passed to the
Function as "working space". I believe that in
that context the motivation is mostly to deal
with multiple threads, nonetheless, the solution
may be applicable to your problem.

You could implement a functor that at initialization
time receives a pointer to that data structure (own
by the Filter), and then, during the Evaluate() method
that structure will be modified.

This however, requires to mark that structure as
"mutable", and therefore, one would think that
we could have just made your double variable
to be "mutable" in the first place.

Using "mutable" here, however, is another design
conflict, since "mutable" should only be used for
values that perform caching, and not for values that
represent "state".  In your case, it looks like you are
really managing a "state" with this internal number.

It is good that you mention the implications for
threading as well, since, hosting state in the Functor
will make it non-thread-safe.

Could you help us understand the algorithm ?

That is, please describe the algorithm that you
want to implement, instead of describing the
problem that you have found in one of the
potential mechanisms of implementing such
algorithm.

It is quite likely that we may have another trick
in store that could be used for that algorithm.

...and...
if we don't find anything, then we should
design something that have a lot of potential
for reuse, as you pointed out.


    Thanks


          Luis

----------------------------------------------------------------
On Sun, Dec 13, 2009 at 2:44 AM, Dan Mueller <dan.muel at gmail.com> wrote:
> Hi Insight community,
>
> I am writing an ImageFunction for which the value depends on the image
> point AND previous calls to the function.
>
> To implement this I have added a member variable (a double) to my
> subclassed image function. I want to set this member variable within:
>    virtual TOutput Evaluate( const PointType& point ) const
>
> I guess you can see my issue: Evaluate is marked const, so calls such as
>    this->m_MemberVariable = 123.456;
> do not compile. I get the following error with Visual Studio 2005:
>    error C2166: l-value specifies const object
>
> Of course I can get around this using const_cast on the this pointer:
>    MyImageFunction<TInputImage,TOutput>* function =
>      const_cast<MyImageFunction<TInputImage,TOutput>*>( this );
>    function->m_MemberVariable = 123.456;
>
> Some questions:
> Am I abusing ImageFunction?
> Does use of const_cast on the this pointer invoke the wrath of the
> const-correctness police?
> Is there a better way?
>
> I guess I could create my own NonConstSingleThreadedImageFunction, but
> then I'd bring the code-reuse police knocking...
>
> Any suggestions appreciated.
>
> Regards, Dan
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list