[Insight-users] how to cast itkAffineTransform to itkScalableAffineTransform?

Luis Ibanez luis.ibanez at kitware.com
Sun Nov 1 16:32:32 EST 2009


Hi Darren,

I'm not sure that the casting that you are attempting
to do can be done in a consistent way.

First,
let me explain why is that you code is segfaulting.

1) you are reading an AffineTransform from a file,
2) you are dynamically casting it to its derived class the
    ScalableAffineTransform.
3) BUT, the class is NOT a ScalableAffineTransform,
     therefore the dynamic cast is returning a NULL
     pointer that you are not checking against NULL,
4) You are calling a method using a null pointer.



Second,

You could create an new ScalableAffineTransform
and copy the element of the Affinetransform that
you read from the file, by using the following code:

    ScalableAffineTransformType::Pointer sat =
               ScalableAffineTransformType::New();

     AffineTransformType::Pointer  at =
                                                xfmRead.GetPointer() );

     sat->SetMatrix(  at->GetMatrix() );
     sat->SetOffset(  at->GetOffset() );



HOWEVER,
It will still be the case that if you call the GetScales()
method, you will not get the scaling factors of the
AffineTransform....

but...

this is mostly because the ScalableAffineTransform
is a mathematical anomaly.  This transform has a
redundant approach for setting the scaling factors
of the Affine transform.  When setting the Matrix
as described above, the ScalableAffineTransform
does not update its internal m_Scale parameters.

It is arguable,
whether this is a "bug" or a "feature"...


       Regards,


              Luis


----------------------------------------------------------------------------------------------------------
On Wed, Oct 28, 2009 at 6:25 PM, Darren Weber
<darren.weber.lists at gmail.com> wrote:
>
> itkAffineTransform objects have been calculated and saved to ascii files.
>
> The following code is supposed to read the file and convert the object to a
> ScalableAffineTransform (assume there is an object called transforms that
> holds the return transform list from a transform reader, as in transforms =
> transformReader->GetTransformList() ):
>
>     ScalableAffineTransformType::Pointer xfm, xfmRead;
>     ScalableAffineTransformType::InputVectorType scale;
>     for( unsigned short i = 0; i < imgDimension; i++ )
>         scale[i] = 1.0;
>     // Use an STL iterator on the list of transforms and apply
>     // the proper casting of the resulting transform.
>     itk::TransformFileReader::TransformListType::const_iterator transformIt;
>     transformIt = --(transforms->end()); // get last transform in list
>     if( ! strcmp( (*transformIt)->GetNameOfClass(),"AffineTransform"))
>         {
>         xfmRead = static_cast<ScalableAffineTransformType*>(
> (*transformIt).GetPointer() );
>         xfm = dynamic_cast<ScalableAffineTransformType*>(
> xfmRead.GetPointer() );
>         // remove the scale component of the transform
>         //const double scale[] = {1.0, 1.0};
>         //xfm->SetScale( scale );
>         }
>
> This code appears to work fine, but once the xfm-SetScale() method is
> called, there are seg faults or bus errors.
>
> Anyone had experience with this?
>
> Thanks in advance,
> Darren
>
>
> _____________________________________
> 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