[ITK] templating typdef question

Matt McCormick matt.mccormick at kitware.com
Mon Nov 3 10:50:31 EST 2014


Hi Bernhard,

>
> The (dimension) template class wrapping around my own image object MyIMG and
> one of the functions I want to use the wrapper in:
>
> ( Methods are static methods of my class. My own stuff is marked in orange
> below. (Do emails in this list preserve formatting?) )

Yes, looks very readable -- thank you.

>
>
>
> void MyITKWrapper::SomeCoolITKMethod( MyIMG img )
>
> {
>
>        switch ( img.GetDimensionality() )
>
>        {
>
>               case( 2 ):    MyITKWrapper::WrapScalarImage < 2 >( img );
> break;
>
>               case( 3 ):    MyITKWrapper::WrapScalarImage < 3 >( img );
> break;
>
>        }
>
> }
>
>
>
> template< unsigned int VDimension >
>
> void MyITKWrapper::WrapScalarImage( MyIMG img )
>
> {
>
>        typedef itk::ImportImageFilter<float,VDimension> ImportFilterType;
>
>        ImportFilterType::Pointer importFilter = ImportFilterType::New();
>
>
>
>        ImportFilterType::SizeType  size;
>
>        ImportFilterType::IndexType start;
>
>        itk::SpacePrecisionType origin[VDimension];
>
>        itk::SpacePrecisionType spacing[VDimension];
>
>        long nPixels=1;
>
>        for ( int dim = 0; dim<VDimension; dim++ )
>
>        {
>
>               size[dim] = img.GetSizeAlongDimension(dim);
>
>               nPixels *= size[dim];
>
>               start[dim] = 0;
>
>               origin[dim] = 0.0;
>
>               spacing[dim] = 1.0;
>
>        }
>
>
>
>        ImportFilterType::RegionType region;
>
>        region.SetIndex( start );
>
>        region.SetSize(  size  );
>
>        importFilter->SetRegion( region );
>
>        importFilter->SetOrigin( origin );
>
>        importFilter->SetSpacing( spacing );
>
>
>
>        float *localBuffer = (float *) img.GetArrayPointer();
>
>        const bool importImageFilterWillOwnTheBuffer = false;
>
>        importFilter->SetImportPointer( localBuffer, nPixels,
> importImageFilterWillOwnTheBuffer );
>
>        return;
>
> }
>

Very nice!



> This compiles and is fine. The problem, of course, is, that I now want to
> use the “importFilter” in my “SomeCoolITKMethod” method, so that I can go on
> like:
>
>
>
>
>
> void MyITKWrapper::SomeCoolITKMethod( MyIMG img )
>
> {
>
>        switch ( img.GetDimensionality() )
>
>        {
>
>               case( 2 ):    importFilter = MyITKWrapper::WrapDMScalarImage <
> 2 >( img );  break;
>
>               case( 3 ):    importFilter = MyITKWrapper::WrapDMScalarImage <
> 3 >( img );  break;
>
>        }
>
>
>
>        otherCoolITKFilter->SetInput( importFilter->GetOutput() );
>
>        […]
>
> a lot more stuff
>
> […]
>
> }
>
>
>
> …but I don’t know how I can “define” importFilter generically in this
> method, and I also can’t make
>
>
>
> itk::ImportImageFilter<float,VDimension>
>
>
>
> the return-type of my templated wrapper method.
>
> Do I have to make “SomeCoolITKMethod” a template function itself and have
> the switch-case in the method calling SomeCoolITKMethod ? Or is there a
> better way?
>

It generally works best to put all templated code inside the template
instead of moving in and out of templates.  However, if this is neally
not desired, itk::ImageBase< VImageDimension >::Pointer or
itk::DataObject::Pointer can be returned, which are base classes that
do not have the template parameters.  They would then have to be
downcast when you need to operate on the specific type.

HTH,
Matt


More information about the Community mailing list