[Insight-developers] Better Point Casting

Luis Ibanez luis.ibanez@kitware.com
Wed, 20 Feb 2002 16:58:59 -0500


Damion Shelton wrote:

> there are two interpretations of what a point is:
> 1) A point in n-d space
> 2) A vector from the origin n-d space

That precisely what we might want to avoid.

(1) is an itkPoint
(2) is an itkVector

and the difference is very important when we start dealing
with transforms. Even for a relatively simple transforms like
Affine it makes a huge difference to use one or the other.

When we start applying transforms from rectangular to polar
coordinate for ultrasound images the distintion between Points
and Vectors becomes more significant.

>
> Depending on the particular operation, it's very helpful to 
> interconvert the  two directly. The existing implementation of 
> itk::Point requires calling:
>
> VectorType GetVectorFromOrigin (void)
>
> It seems that a well-written cast could accomplish more-or-less the 
> same thing without requiring a function call, and without losing clarity.
>
The generic casting itself will be a function call too. The advantage is 
that
it is avoiding the collateral cost of returning the vector (calling the 
copy constructor)
and then copying it a second time when you use the operator=() to assign it
to a destination vector.

If we change the API  from:

     VectorType point::GetVectorFromOrigin(void) const

to:

     void point::GetVectorFromOrigin( VectorType & outputVector ) const

They will have the same computional load.  
About clarity,...there are two aspects:

1) how clear is what the program is doing (as far as C++ goes)
2) how much sense makes that in the description or a real problem.

Casting is supposed to facilitate the convertion between two classes
that are equivalent.  We can cast a (float) to a (bool)... but what that 
means ?

To give an extreme example: We can cast an (Image*) to an (ImageBase*)
because they are related.  Using Casting for converting an Image to a 
Matrix
just because both can look like M[i][j] is an abuse of the idea.  Their 
intended
uses are not equivalent:

- What is the determinant of an Image ?
- Why to run an edge-detection filter in a Matrix ?

The fact that their data structure representation looks similar does not 
mean
that both clases are equivalent.

In the case where such conversion may be desirable, maybe an
ImageToMatrixAdaptor will provide a clearer approach.


Sorry if I'm being fanatic on this,...is just that:
what is C++ good for if we start writing FORTRAN-like code

One of the restriction of the toolik is that not only the programmer 
writing
the code today has to know what he is doing. The code should be also clear
enough as to allow someone three years from now to understand what the code
is doing.   How is this new observer going to figure this out if we mix 
the use
of the concepts ?



     Luis